1 /*
2 This example code is in the Public Domain (or CC0 licensed, at your option.)
3
4 Unless required by applicable law or agreed to in writing, this
5 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6 CONDITIONS OF ANY KIND, either express or implied.
7 */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include "freertos/FreeRTOS.h"
12 #include "freertos/task.h"
13 #include "esp_bt.h"
14 #include "esp_log.h"
15 #include "nvs_flash.h"
16 #include "bt_hci_common.h"
17
18 static const char *tag = "BLE_ADV";
19
20 static uint8_t hci_cmd_buf[128];
21
22 /*
23 * @brief: BT controller callback function, used to notify the upper layer that
24 * controller is ready to receive command
25 */
controller_rcv_pkt_ready(void)26 static void controller_rcv_pkt_ready(void)
27 {
28 printf("controller rcv pkt ready\n");
29 }
30
31 /*
32 * @brief: BT controller callback function, to transfer data packet to upper
33 * controller is ready to receive command
34 */
host_rcv_pkt(uint8_t * data,uint16_t len)35 static int host_rcv_pkt(uint8_t *data, uint16_t len)
36 {
37 printf("host rcv pkt: ");
38 for (uint16_t i = 0; i < len; i++) {
39 printf("%02x", data[i]);
40 }
41 printf("\n");
42 return 0;
43 }
44
45 static esp_vhci_host_callback_t vhci_host_cb = {
46 controller_rcv_pkt_ready,
47 host_rcv_pkt
48 };
hci_cmd_send_reset(void)49 static void hci_cmd_send_reset(void)
50 {
51 uint16_t sz = make_cmd_reset (hci_cmd_buf);
52 esp_vhci_host_send_packet(hci_cmd_buf, sz);
53 }
54
hci_cmd_send_ble_adv_start(void)55 static void hci_cmd_send_ble_adv_start(void)
56 {
57 uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1);
58 esp_vhci_host_send_packet(hci_cmd_buf, sz);
59 }
60
hci_cmd_send_ble_set_adv_param(void)61 static void hci_cmd_send_ble_set_adv_param(void)
62 {
63 uint16_t adv_intv_min = 256; // 160ms
64 uint16_t adv_intv_max = 256; // 160ms
65 uint8_t adv_type = 0; // connectable undirected advertising (ADV_IND)
66 uint8_t own_addr_type = 0; // Public Device Address
67 uint8_t peer_addr_type = 0; // Public Device Address
68 uint8_t peer_addr[6] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85};
69 uint8_t adv_chn_map = 0x07; // 37, 38, 39
70 uint8_t adv_filter_policy = 0; // Process All Conn and Scan
71
72 uint16_t sz = make_cmd_ble_set_adv_param(hci_cmd_buf,
73 adv_intv_min,
74 adv_intv_max,
75 adv_type,
76 own_addr_type,
77 peer_addr_type,
78 peer_addr,
79 adv_chn_map,
80 adv_filter_policy);
81 esp_vhci_host_send_packet(hci_cmd_buf, sz);
82 }
83
hci_cmd_send_ble_set_adv_data(void)84 static void hci_cmd_send_ble_set_adv_data(void)
85 {
86 char *adv_name = "ESP-BLE-HELLO";
87 uint8_t name_len = (uint8_t)strlen(adv_name);
88 uint8_t adv_data[31] = {0x02, 0x01, 0x06, 0x0, 0x09};
89 uint8_t adv_data_len;
90
91 adv_data[3] = name_len + 1;
92 for (int i = 0; i < name_len; i++) {
93 adv_data[5 + i] = (uint8_t)adv_name[i];
94 }
95 adv_data_len = 5 + name_len;
96
97 uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data);
98 esp_vhci_host_send_packet(hci_cmd_buf, sz);
99 }
100
101 /*
102 * @brief: send HCI commands to perform BLE advertising;
103 */
bleAdvtTask(void * pvParameters)104 void bleAdvtTask(void *pvParameters)
105 {
106 int cmd_cnt = 0;
107 bool send_avail = false;
108 esp_vhci_host_register_callback(&vhci_host_cb);
109 printf("BLE advt task start\n");
110 while (1) {
111 vTaskDelay(1000 / portTICK_PERIOD_MS);
112 send_avail = esp_vhci_host_check_send_available();
113 if (send_avail) {
114 switch (cmd_cnt) {
115 case 0: hci_cmd_send_reset(); ++cmd_cnt; break;
116 case 1: hci_cmd_send_ble_set_adv_param(); ++cmd_cnt; break;
117 case 2: hci_cmd_send_ble_set_adv_data(); ++cmd_cnt; break;
118 case 3: hci_cmd_send_ble_adv_start(); ++cmd_cnt; break;
119 }
120 }
121 printf("BLE Advertise, flag_send_avail: %d, cmd_sent: %d\n", send_avail, cmd_cnt);
122 }
123 }
124
app_main(void)125 void app_main(void)
126 {
127 /* Initialize NVS — it is used to store PHY calibration data */
128 esp_err_t ret = nvs_flash_init();
129 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
130 ESP_ERROR_CHECK(nvs_flash_erase());
131 ret = nvs_flash_init();
132 }
133 ESP_ERROR_CHECK( ret );
134 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
135
136 ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
137 if (ret) {
138 ESP_LOGI(tag, "Bluetooth controller release classic bt memory failed: %s", esp_err_to_name(ret));
139 return;
140 }
141
142 if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
143 ESP_LOGI(tag, "Bluetooth controller initialize failed: %s", esp_err_to_name(ret));
144 return;
145 }
146
147 if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) {
148 ESP_LOGI(tag, "Bluetooth controller enable failed: %s", esp_err_to_name(ret));
149 return;
150 }
151
152 /*
153 * If call mem release here, also work. Input ESP_BT_MODE_CLASSIC_BT, the function will
154 * release the memory of classic bt mode.
155 * esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
156 *
157 */
158
159 /*
160 * If call mem release here, also work. Input ESP_BT_MODE_BTDM, the function will calculate
161 * that the BLE mode is already used, so it will release of only classic bt mode.
162 * esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
163 */
164
165 xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
166 }
167