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