|
| 1 | +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include "freertos/FreeRTOS.h" |
| 17 | +#include "freertos/task.h" |
| 18 | +#include "bt.h" |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +#define HCI_H4_CMD_PREAMBLE_SIZE (4) |
| 22 | + |
| 23 | +/* HCI Command opcode group field(OGF) */ |
| 24 | +#define HCI_GRP_HOST_CONT_BASEBAND_CMDS (0x03 << 10) /* 0x0C00 */ |
| 25 | +#define HCI_GRP_BLE_CMDS (0x08 << 10) |
| 26 | + |
| 27 | +#define HCI_RESET (0x0003 | HCI_GRP_HOST_CONT_BASEBAND_CMDS) |
| 28 | +#define HCI_BLE_WRITE_ADV_ENABLE (0x000A | HCI_GRP_BLE_CMDS) |
| 29 | +#define HCI_BLE_WRITE_ADV_PARAMS (0x0006 | HCI_GRP_BLE_CMDS) |
| 30 | +#define HCI_BLE_WRITE_ADV_DATA (0x0008 | HCI_GRP_BLE_CMDS) |
| 31 | + |
| 32 | +#define HCIC_PARAM_SIZE_WRITE_ADV_ENABLE (1) |
| 33 | +#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS (15) |
| 34 | +#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA (31) |
| 35 | + |
| 36 | +#define BD_ADDR_LEN (6) /* Device address length */ |
| 37 | +typedef uint8_t bd_addr_t[BD_ADDR_LEN]; /* Device address */ |
| 38 | + |
| 39 | +#define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);} |
| 40 | +#define UINT8_TO_STREAM(p, u8) {*(p)++ = (uint8_t)(u8);} |
| 41 | +#define BDADDR_TO_STREAM(p, a) {int ijk; for (ijk = 0; ijk < BD_ADDR_LEN; ijk++) *(p)++ = (uint8_t) a[BD_ADDR_LEN - 1 - ijk];} |
| 42 | +#define ARRAY_TO_STREAM(p, a, len) {int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[ijk];} |
| 43 | + |
| 44 | +enum { |
| 45 | + H4_TYPE_COMMAND = 1, |
| 46 | + H4_TYPE_ACL = 2, |
| 47 | + H4_TYPE_SCO = 3, |
| 48 | + H4_TYPE_EVENT = 4 |
| 49 | +}; |
| 50 | + |
| 51 | +static uint8_t hci_cmd_buf[128]; |
| 52 | + |
| 53 | +/* |
| 54 | + * @brief: BT controller callback function, used to notify the upper layer that |
| 55 | + * controller is ready to receive command |
| 56 | + */ |
| 57 | +static void controller_rcv_pkt_ready(void) |
| 58 | +{ |
| 59 | + printf("controller rcv pkt ready\n"); |
| 60 | +} |
| 61 | + |
| 62 | +/* |
| 63 | + * @brief: BT controller callback function, to transfer data packet to upper |
| 64 | + * controller is ready to receive command |
| 65 | + */ |
| 66 | +static int host_rcv_pkt(uint8_t *data, uint16_t len) |
| 67 | +{ |
| 68 | + printf("host rcv pkt: "); |
| 69 | + for (uint16_t i = 0; i < len; i++) { |
| 70 | + printf("%02x", data[i]); |
| 71 | + } |
| 72 | + printf("\n"); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +static esp_vhci_host_callback_t vhci_host_cb = { |
| 77 | + controller_rcv_pkt_ready, |
| 78 | + host_rcv_pkt |
| 79 | +}; |
| 80 | + |
| 81 | +static uint16_t make_cmd_reset(uint8_t *buf) |
| 82 | +{ |
| 83 | + UINT8_TO_STREAM (buf, H4_TYPE_COMMAND); |
| 84 | + UINT16_TO_STREAM (buf, HCI_RESET); |
| 85 | + UINT8_TO_STREAM (buf, 0); |
| 86 | + return HCI_H4_CMD_PREAMBLE_SIZE; |
| 87 | +} |
| 88 | + |
| 89 | +static uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable) |
| 90 | +{ |
| 91 | + UINT8_TO_STREAM (buf, H4_TYPE_COMMAND); |
| 92 | + UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_ENABLE); |
| 93 | + UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE); |
| 94 | + UINT8_TO_STREAM (buf, adv_enable); |
| 95 | + return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE; |
| 96 | +} |
| 97 | + |
| 98 | +static uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max, |
| 99 | + uint8_t adv_type, uint8_t addr_type_own, |
| 100 | + uint8_t addr_type_dir, bd_addr_t direct_bda, |
| 101 | + uint8_t channel_map, uint8_t adv_filter_policy) |
| 102 | +{ |
| 103 | + UINT8_TO_STREAM (buf, H4_TYPE_COMMAND); |
| 104 | + UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_PARAMS); |
| 105 | + UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS ); |
| 106 | + |
| 107 | + UINT16_TO_STREAM (buf, adv_int_min); |
| 108 | + UINT16_TO_STREAM (buf, adv_int_max); |
| 109 | + UINT8_TO_STREAM (buf, adv_type); |
| 110 | + UINT8_TO_STREAM (buf, addr_type_own); |
| 111 | + UINT8_TO_STREAM (buf, addr_type_dir); |
| 112 | + BDADDR_TO_STREAM (buf, direct_bda); |
| 113 | + UINT8_TO_STREAM (buf, channel_map); |
| 114 | + UINT8_TO_STREAM (buf, adv_filter_policy); |
| 115 | + return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data) |
| 120 | +{ |
| 121 | + UINT8_TO_STREAM (buf, H4_TYPE_COMMAND); |
| 122 | + UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_DATA); |
| 123 | + UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1); |
| 124 | + |
| 125 | + memset(buf, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA); |
| 126 | + |
| 127 | + if (p_data != NULL && data_len > 0) { |
| 128 | + if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) { |
| 129 | + data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA; |
| 130 | + } |
| 131 | + |
| 132 | + UINT8_TO_STREAM (buf, data_len); |
| 133 | + |
| 134 | + ARRAY_TO_STREAM (buf, p_data, data_len); |
| 135 | + } |
| 136 | + return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1; |
| 137 | +} |
| 138 | + |
| 139 | +static void hci_cmd_send_reset(void) |
| 140 | +{ |
| 141 | + uint16_t sz = make_cmd_reset (hci_cmd_buf); |
| 142 | + esp_vhci_host_send_packet(hci_cmd_buf, sz); |
| 143 | +} |
| 144 | + |
| 145 | +static void hci_cmd_send_ble_adv_start(void) |
| 146 | +{ |
| 147 | + uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1); |
| 148 | + esp_vhci_host_send_packet(hci_cmd_buf, sz); |
| 149 | +} |
| 150 | + |
| 151 | +static void hci_cmd_send_ble_set_adv_param(void) |
| 152 | +{ |
| 153 | + uint16_t adv_intv_min = 256; // 160ms |
| 154 | + uint16_t adv_intv_max = 256; // 160ms |
| 155 | + uint8_t adv_type = 0; // connectable undirected advertising (ADV_IND) |
| 156 | + uint8_t own_addr_type = 0; // Public Device Address |
| 157 | + uint8_t peer_addr_type = 0; // Public Device Address |
| 158 | + uint8_t peer_addr[6] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85}; |
| 159 | + uint8_t adv_chn_map = 0x07; // 37, 38, 39 |
| 160 | + uint8_t adv_filter_policy = 0; // Process All Conn and Scan |
| 161 | + |
| 162 | + uint16_t sz = make_cmd_ble_set_adv_param(hci_cmd_buf, |
| 163 | + adv_intv_min, |
| 164 | + adv_intv_max, |
| 165 | + adv_type, |
| 166 | + own_addr_type, |
| 167 | + peer_addr_type, |
| 168 | + peer_addr, |
| 169 | + adv_chn_map, |
| 170 | + adv_filter_policy); |
| 171 | + esp_vhci_host_send_packet(hci_cmd_buf, sz); |
| 172 | +} |
| 173 | + |
| 174 | +static void hci_cmd_send_ble_set_adv_data(void) |
| 175 | +{ |
| 176 | + char *adv_name = "ESP-BLE-HELLO"; |
| 177 | + uint8_t name_len = (uint8_t)strlen(adv_name); |
| 178 | + uint8_t adv_data[31] = {0x02, 0x01, 0x06, 0x0, 0x09}; |
| 179 | + uint8_t adv_data_len; |
| 180 | + |
| 181 | + adv_data[3] = name_len + 1; |
| 182 | + for (int i = 0; i < name_len; i++) { |
| 183 | + adv_data[5 + i] = (uint8_t)adv_name[i]; |
| 184 | + } |
| 185 | + adv_data_len = 5 + name_len; |
| 186 | + |
| 187 | + uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data); |
| 188 | + esp_vhci_host_send_packet(hci_cmd_buf, sz); |
| 189 | +} |
| 190 | + |
| 191 | +/* |
| 192 | + * @brief: send HCI commands to perform BLE advertising; |
| 193 | + */ |
| 194 | +void bleAdvtTask(void *pvParameters) |
| 195 | +{ |
| 196 | + int cmd_cnt = 0; |
| 197 | + bool send_avail = false; |
| 198 | + esp_vhci_host_register_callback(&vhci_host_cb); |
| 199 | + printf("BLE advt task start\n"); |
| 200 | + while (1) { |
| 201 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 202 | + send_avail = esp_vhci_host_check_send_available(); |
| 203 | + if (send_avail) { |
| 204 | + switch (cmd_cnt) { |
| 205 | + case 0: hci_cmd_send_reset(); ++cmd_cnt; break; |
| 206 | + case 1: hci_cmd_send_ble_set_adv_param(); ++cmd_cnt; break; |
| 207 | + case 2: hci_cmd_send_ble_set_adv_data(); ++cmd_cnt; break; |
| 208 | + case 3: hci_cmd_send_ble_adv_start(); ++cmd_cnt; break; |
| 209 | + } |
| 210 | + } |
| 211 | + printf("BLE Advertise, flag_send_avail: %d, cmd_sent: %d\n", send_avail, cmd_cnt); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void app_main() |
| 216 | +{ |
| 217 | + esp_bt_controller_init(); |
| 218 | + xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0); |
| 219 | +} |
0 commit comments