Skip to content

Commit abb79e4

Browse files
chhajedjiespressif-bot
authored andcommitted
Bluetooth HCI common components.
Separate Bluetooth HCI component example which adds some macros and functions used by HCI Layer defined by the spec.
1 parent 9222f54 commit abb79e4

File tree

8 files changed

+187
-89
lines changed

8 files changed

+187
-89
lines changed

examples/bluetooth/hci/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ See the [README.md](./controller_hci_uart/README.md) file in the example [contro
1717
Demonstrates interaction with controller though virtual HCI layer. In this example, simple BLE advertising is done.
1818

1919
See the [README.md](./controller_vhci_ble_adv/README.md) file in the example [controller_vhci_ble_adv](./controller_vhci_ble_adv).
20+
21+
## hci_common_component
22+
23+
This is separate component adding functionalities for HCI Layer. Since this component is just used by HCI examples, it is not placed in global components.

examples/bluetooth/hci/controller_vhci_ble_adv/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
# CMakeLists in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.5)
44

5+
# This example uses an extra component for common functions for Bluetooth HCI layer.
6+
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/hci/hci_common_component)
7+
58
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
69
project(ble_adv)

examples/bluetooth/hci/controller_vhci_ble_adv/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55

66
PROJECT_NAME := ble_adv
77

8+
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/bluetooth/hci/hci_common_component
9+
810
include $(IDF_PATH)/make/project.mk

examples/bluetooth/hci/controller_vhci_ble_adv/main/app_bt.c

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,10 @@
1313
#include "esp_bt.h"
1414
#include "esp_log.h"
1515
#include "nvs_flash.h"
16+
#include "bt_hci_common.h"
1617

1718
static const char *tag = "BLE_ADV";
1819

19-
#define HCI_H4_CMD_PREAMBLE_SIZE (4)
20-
21-
/* HCI Command opcode group field(OGF) */
22-
#define HCI_GRP_HOST_CONT_BASEBAND_CMDS (0x03 << 10) /* 0x0C00 */
23-
#define HCI_GRP_BLE_CMDS (0x08 << 10)
24-
25-
#define HCI_RESET (0x0003 | HCI_GRP_HOST_CONT_BASEBAND_CMDS)
26-
#define HCI_BLE_WRITE_ADV_ENABLE (0x000A | HCI_GRP_BLE_CMDS)
27-
#define HCI_BLE_WRITE_ADV_PARAMS (0x0006 | HCI_GRP_BLE_CMDS)
28-
#define HCI_BLE_WRITE_ADV_DATA (0x0008 | HCI_GRP_BLE_CMDS)
29-
30-
#define HCIC_PARAM_SIZE_WRITE_ADV_ENABLE (1)
31-
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS (15)
32-
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA (31)
33-
34-
#define BD_ADDR_LEN (6) /* Device address length */
35-
typedef uint8_t bd_addr_t[BD_ADDR_LEN]; /* Device address */
36-
37-
#define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);}
38-
#define UINT8_TO_STREAM(p, u8) {*(p)++ = (uint8_t)(u8);}
39-
#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];}
40-
#define ARRAY_TO_STREAM(p, a, len) {int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[ijk];}
41-
42-
enum {
43-
H4_TYPE_COMMAND = 1,
44-
H4_TYPE_ACL = 2,
45-
H4_TYPE_SCO = 3,
46-
H4_TYPE_EVENT = 4
47-
};
48-
4920
static uint8_t hci_cmd_buf[128];
5021

5122
/*
@@ -75,65 +46,6 @@ static esp_vhci_host_callback_t vhci_host_cb = {
7546
controller_rcv_pkt_ready,
7647
host_rcv_pkt
7748
};
78-
79-
static uint16_t make_cmd_reset(uint8_t *buf)
80-
{
81-
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
82-
UINT16_TO_STREAM (buf, HCI_RESET);
83-
UINT8_TO_STREAM (buf, 0);
84-
return HCI_H4_CMD_PREAMBLE_SIZE;
85-
}
86-
87-
static uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable)
88-
{
89-
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
90-
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_ENABLE);
91-
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE);
92-
UINT8_TO_STREAM (buf, adv_enable);
93-
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE;
94-
}
95-
96-
static uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max,
97-
uint8_t adv_type, uint8_t addr_type_own,
98-
uint8_t addr_type_dir, bd_addr_t direct_bda,
99-
uint8_t channel_map, uint8_t adv_filter_policy)
100-
{
101-
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
102-
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_PARAMS);
103-
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS );
104-
105-
UINT16_TO_STREAM (buf, adv_int_min);
106-
UINT16_TO_STREAM (buf, adv_int_max);
107-
UINT8_TO_STREAM (buf, adv_type);
108-
UINT8_TO_STREAM (buf, addr_type_own);
109-
UINT8_TO_STREAM (buf, addr_type_dir);
110-
BDADDR_TO_STREAM (buf, direct_bda);
111-
UINT8_TO_STREAM (buf, channel_map);
112-
UINT8_TO_STREAM (buf, adv_filter_policy);
113-
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS;
114-
}
115-
116-
117-
static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data)
118-
{
119-
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
120-
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_DATA);
121-
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
122-
123-
memset(buf, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA);
124-
125-
if (p_data != NULL && data_len > 0) {
126-
if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
127-
data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
128-
}
129-
130-
UINT8_TO_STREAM (buf, data_len);
131-
132-
ARRAY_TO_STREAM (buf, p_data, data_len);
133-
}
134-
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1;
135-
}
136-
13749
static void hci_cmd_send_reset(void)
13850
{
13951
uint16_t sz = make_cmd_reset (hci_cmd_buf);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "bt_hci_common.c"
2+
INCLUDE_DIRS "include"
3+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Basic functionality for Bluetooth Host Controller Interface Layer.
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
10+
#include "bt_hci_common.h"
11+
12+
13+
uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable)
14+
{
15+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
16+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_ENABLE);
17+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE);
18+
UINT8_TO_STREAM (buf, adv_enable);
19+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE;
20+
}
21+
22+
uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max,
23+
uint8_t adv_type, uint8_t addr_type_own,
24+
uint8_t addr_type_dir, bd_addr_t direct_bda,
25+
uint8_t channel_map, uint8_t adv_filter_policy)
26+
{
27+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
28+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_PARAMS);
29+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS );
30+
31+
UINT16_TO_STREAM (buf, adv_int_min);
32+
UINT16_TO_STREAM (buf, adv_int_max);
33+
UINT8_TO_STREAM (buf, adv_type);
34+
UINT8_TO_STREAM (buf, addr_type_own);
35+
UINT8_TO_STREAM (buf, addr_type_dir);
36+
BDADDR_TO_STREAM (buf, direct_bda);
37+
UINT8_TO_STREAM (buf, channel_map);
38+
UINT8_TO_STREAM (buf, adv_filter_policy);
39+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS;
40+
}
41+
42+
uint16_t make_cmd_reset(uint8_t *buf)
43+
{
44+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
45+
UINT16_TO_STREAM (buf, HCI_RESET);
46+
UINT8_TO_STREAM (buf, 0);
47+
return HCI_H4_CMD_PREAMBLE_SIZE;
48+
}
49+
50+
51+
52+
uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data)
53+
{
54+
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
55+
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_DATA);
56+
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
57+
58+
memset(buf, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA);
59+
60+
if (p_data != NULL && data_len > 0) {
61+
if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
62+
data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
63+
}
64+
65+
UINT8_TO_STREAM (buf, data_len);
66+
67+
ARRAY_TO_STREAM (buf, p_data, data_len);
68+
}
69+
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1;
70+
}

examples/bluetooth/hci/hci_common_component/component.mk

Whitespace-only changes.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* Baisc macros and functions for Bluetooth Host Controller Interface Layer.
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
10+
#pragma once
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#include "stdio.h"
17+
#include "string.h"
18+
19+
#define HCI_H4_CMD_PREAMBLE_SIZE (4)
20+
21+
/* HCI Command Opcode group Field (OGF) */
22+
#define HCI_GRP_HOST_CONT_BASEBAND_CMDS (0x03 << 10) /* 0x0C00 */
23+
#define HCI_GRP_BLE_CMDS (0x08 << 10)
24+
25+
/* HCI Command Opcode Command Field (OCF) */
26+
#define HCI_RESET (0x0003 | HCI_GRP_HOST_CONT_BASEBAND_CMDS)
27+
#define HCI_BLE_WRITE_ADV_ENABLE (0x000A | HCI_GRP_BLE_CMDS)
28+
#define HCI_BLE_WRITE_ADV_DATA (0x0008 | HCI_GRP_BLE_CMDS)
29+
#define HCI_BLE_WRITE_ADV_PARAMS (0x0006 | HCI_GRP_BLE_CMDS)
30+
31+
/* HCI Command length. */
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+
/**
52+
* @brief Writes reset bit in buf and returns size of input buffer after
53+
* writing in it.
54+
*
55+
* @param buf Input buffer to write which will be sent to controller.
56+
*
57+
* @return Size of buf after writing into it.
58+
*/
59+
uint16_t make_cmd_reset(uint8_t *buf);
60+
61+
/**
62+
* @brief This function is used to request the Controller to start or stop advertising.
63+
*
64+
* @param buf Input buffer to write which will be sent to controller.
65+
* @param adv_enable 1 to enable advertising and 0 to disable.
66+
*
67+
* @return Size of buf after writing into it.
68+
*/
69+
uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable);
70+
71+
/**
72+
* @brief This function is used by the Host to set the advertising parameters.
73+
*
74+
* @param buf Input buffer to write which will be sent to controller.
75+
* @param adv_int_min Minimum advertising interval.
76+
* @param adv_int_max Maximum advertising interval.
77+
* @param adv_type Advertising type.
78+
* @param addr_type_own Own advertising type.
79+
* @param addr_type_peer Peer device's address type.
80+
* @param peer_addr Peer device's BD address.
81+
* @param channel_map Advertising channel map.
82+
* @param adv_filter_policy Advertising Filter Policy.
83+
*
84+
* @return Size of buf after writing into it.
85+
*/
86+
uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max,
87+
uint8_t adv_type, uint8_t addr_type_own,
88+
uint8_t addr_type_peer, bd_addr_t peer_addr,
89+
uint8_t channel_map, uint8_t adv_filter_policy);
90+
91+
/**
92+
* @brief This function is used to set the data used in advertising packets that have a data field.
93+
*
94+
* @param buf Input buffer to write which will be sent to controller.
95+
* @param data_len Length of p_data.
96+
* @param p_data Data to be set.
97+
*
98+
* @return Size of buf after writing into it.
99+
*/
100+
uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data);
101+
102+
#ifdef __cplusplus
103+
}
104+
#endif

0 commit comments

Comments
 (0)