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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
115
116
117
118
121
122
123
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
267
268
269
270
271
272
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
307
308
309
310
311
312
313
317
318
322
323
328
329
334
335
336
342
343
348
349
355
356
362
363
368
369
374
375
380
381
386
387
392
393
398
399
404
405
410
411
416
417
423
424
430
431
437
438
443
444
450
451
456
457
462
463
469
470
476
477
482
483
489
490
496
497
503
504
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
582
583
584
585
586
587
588
589
590
591
593
594
595
596
597
598
599
600
/* ... */
#include <stdbool.h>
#include "common/bt_target.h"
#include "common/bt_trace.h"
#include "device/bdaddr.h"
#include "stack/bt_types.h"
#include "device/controller.h"
#include "device/event_mask.h"
#include "stack/hcimsgs.h"
#include "hci/hci_layer.h"
#include "hci/hci_packet_factory.h"
#include "hci/hci_packet_parser.h"
#include "stack/btm_ble_api.h"
#include "device/version.h"
#include "osi/future.h"
#include "config/stack_config.h"15 includes
#if (BLE_50_FEATURE_SUPPORT == TRUE)
const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\xff\xff\xff" };
#else
const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
#endif
#if (BLE_INCLUDED)
const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
#else
const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
#endif
const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
#define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
#define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
#define BLE_SUPPORTED_STATES_SIZE 8
#define BLE_SUPPORTED_FEATURES_SIZE 8
#define BLE_EXT_ADV_DATA_LEN_MAX 16505 defines
typedef struct {
const hci_t *hci;
const hci_packet_factory_t *packet_factory;
const hci_packet_parser_t *packet_parser;
bt_version_t bt_version;
bt_bdaddr_t address;
uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
uint8_t last_features_classic_page_index;
bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
uint16_t acl_data_size_classic;
uint16_t acl_data_size_ble;
uint16_t acl_buffer_count_classic;
uint8_t acl_buffer_count_ble;
uint8_t sco_data_size;
uint16_t sco_buffer_count;
uint8_t ble_white_list_size;
uint8_t ble_resolving_list_max_size;
uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
bt_device_features_t features_ble;
uint16_t ble_suggested_default_data_length;
uint16_t ble_suggested_default_data_txtime;
bool readable;
bool ble_supported;
bool simple_pairing_supported;
bool secure_connections_supported;
#if (BLE_50_FEATURE_SUPPORT == TRUE)
uint16_t ble_ext_adv_data_max_len;
#endif
}{ ... } controller_local_param_t;
#if BT_BLE_DYNAMIC_ENV_MEMORY == FALSE
static controller_local_param_t controller_param;
#else
static controller_local_param_t *controller_param_ptr;
#define controller_param (*controller_param_ptr)/* ... */
#endif
#define AWAIT_COMMAND(command) future_await(controller_param.hci->transmit_command_futured(command))
static void start_up(void)
{
BT_HDR *response;
response = AWAIT_COMMAND(controller_param.packet_factory->make_reset());
controller_param.packet_parser->parse_generic_command_complete(response);
#if (CLASSIC_BT_INCLUDED)
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_buffer_size());
controller_param.packet_parser->parse_read_buffer_size_response(
response, &controller_param.acl_data_size_classic, &controller_param.acl_buffer_count_classic,
&controller_param.sco_data_size, &controller_param.sco_buffer_count);/* ... */
#endif
#if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
response = AWAIT_COMMAND(controller_param.packet_factory->make_set_c2h_flow_control(HCI_HOST_FLOW_CTRL_ACL_ON));
controller_param.packet_parser->parse_generic_command_complete(response);/* ... */
#endif
#if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
response = AWAIT_COMMAND(controller_param.packet_factory->make_set_adv_report_flow_control(HCI_HOST_FLOW_CTRL_ADV_REPORT_ON, (uint16_t)BLE_ADV_REPORT_FLOW_CONTROL_NUM, (uint16_t)BLE_ADV_REPORT_DISCARD_THRSHOLD));
controller_param.packet_parser->parse_generic_command_complete(response);/* ... */
#endif
response = AWAIT_COMMAND(
controller_param.packet_factory->make_host_buffer_size(
L2CAP_MTU_SIZE,
SCO_HOST_BUFFER_SIZE,
L2CAP_HOST_FC_ACL_BUFS,
10
)
);
controller_param.packet_parser->parse_generic_command_complete(response);
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_version_info());
controller_param.packet_parser->parse_read_local_version_info_response(response, &controller_param.bt_version);
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_bd_addr());
controller_param.packet_parser->parse_read_bd_addr_response(response, &controller_param.address);
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_supported_commands());
controller_param.packet_parser->parse_read_local_supported_commands_response(
response,
controller_param.supported_commands,
HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
);
#if (CLASSIC_BT_INCLUDED)
uint8_t page_number = 0;
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_extended_features(page_number));
controller_param.packet_parser->parse_read_local_extended_features_response(
response,
&page_number,
&controller_param.last_features_classic_page_index,
controller_param.features_classic,
MAX_FEATURES_CLASSIC_PAGE_COUNT
);
assert(page_number == 0);
page_number++;/* ... */
#endif
controller_param.simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(
controller_param.features_classic[0].as_array) &&
(bluedriod_config_get()->get_ssp_enabled());
if (controller_param.simple_pairing_supported) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
controller_param.packet_parser->parse_generic_command_complete(response);
}{...}
#if (BLE_INCLUDED == TRUE)
if (HCI_LE_SPT_SUPPORTED(controller_param.features_classic[0].as_array)) {
uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(controller_param.features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
response = AWAIT_COMMAND(
controller_param.packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
);
controller_param.packet_parser->parse_generic_command_complete(response);
}{...}
#endif/* ... */
#if (CLASSIC_BT_INCLUDED)
while (page_number <= controller_param.last_features_classic_page_index &&
page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_extended_features(page_number));
controller_param.packet_parser->parse_read_local_extended_features_response(
response,
&page_number,
&controller_param.last_features_classic_page_index,
controller_param.features_classic,
MAX_FEATURES_CLASSIC_PAGE_COUNT
);
page_number++;
}{...}
#endif/* ... */
#if (SC_MODE_INCLUDED == TRUE)
controller_param.secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(controller_param.features_classic[2].as_array);
if (controller_param.secure_connections_supported) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
controller_param.packet_parser->parse_generic_command_complete(response);
}{...}
#endif/* ... */
#if (BLE_INCLUDED == TRUE)
#if (CLASSIC_BT_INCLUDED)
controller_param.ble_supported = controller_param.last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(controller_param.features_classic[1].as_array);
#else
controller_param.ble_supported = true;
#endif
if (controller_param.ble_supported) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_white_list_size());
controller_param.packet_parser->parse_ble_read_white_list_size_response(response, &controller_param.ble_white_list_size);
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_buffer_size());
controller_param.packet_parser->parse_ble_read_buffer_size_response(
response,
&controller_param.acl_data_size_ble,
&controller_param.acl_buffer_count_ble
);
if (controller_param.acl_data_size_ble == 0) {
controller_param.acl_data_size_ble = controller_param.acl_data_size_classic;
}{...}
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_supported_states());
controller_param.packet_parser->parse_ble_read_supported_states_response(
response,
controller_param.ble_supported_states,
sizeof(controller_param.ble_supported_states)
);
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_local_supported_features());
controller_param.packet_parser->parse_ble_read_local_supported_features_response(
response,
&controller_param.features_ble
);
if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(controller_param.features_ble.as_array)) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_resolving_list_size());
controller_param.packet_parser->parse_ble_read_resolving_list_size_response(
response,
&controller_param.ble_resolving_list_max_size);
}{...}
#if BLE_50_FEATURE_SUPPORT == TRUE
controller_param.ble_ext_adv_data_max_len = BLE_EXT_ADV_DATA_LEN_MAX;
#endif
#if (BLE_50_FEATURE_SUPPORT == TRUE && BLE_42_FEATURE_SUPPORT == FALSE)
if (HCI_LE_EXT_ADV_SUPPORTED(controller_param.features_ble.as_array)) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_read_max_adv_data_len());
controller_param.packet_parser->parse_ble_read_adv_max_len_response(
response,
&controller_param.ble_ext_adv_data_max_len);
}{...}
#endif/* ... */
if (HCI_LE_DATA_LEN_EXT_SUPPORTED(controller_param.features_ble.as_array)) {
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_write_suggested_default_data_length(BTM_BLE_DATA_SIZE_MAX, BTM_BLE_DATA_TX_TIME_MAX));
controller_param.packet_parser->parse_generic_command_complete(response);
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_suggested_default_data_length());
controller_param.packet_parser->parse_ble_read_suggested_default_data_length_response(
response,
&controller_param.ble_suggested_default_data_length,
&controller_param.ble_suggested_default_data_txtime);
}{...}
response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
controller_param.packet_parser->parse_generic_command_complete(response);
}{...}
#endif/* ... */
response = AWAIT_COMMAND(controller_param.packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
controller_param.packet_parser->parse_generic_command_complete(response);
#if (BTM_SCO_HCI_INCLUDED == TRUE)
response = AWAIT_COMMAND(controller_param.packet_factory->make_write_sync_flow_control_enable(1));
controller_param.packet_parser->parse_generic_command_complete(response);
response = AWAIT_COMMAND(controller_param.packet_factory->make_write_default_erroneous_data_report(1));
controller_param.packet_parser->parse_generic_command_complete(response);/* ... */
#endif
controller_param.readable = true;
return;
}{ ... }
static void shut_down(void)
{
controller_param.readable = false;
}{ ... }
static bool get_is_ready(void)
{
return controller_param.readable;
}{ ... }
static const bt_bdaddr_t *get_address(void)
{
assert(controller_param.readable);
return &controller_param.address;
}{ ... }
static const bt_version_t *get_bt_version(void)
{
assert(controller_param.readable);
return &controller_param.bt_version;
}{ ... }
static const bt_device_features_t *get_features_classic(int index)
{
assert(controller_param.readable);
assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
return &controller_param.features_classic[index];
}{ ... }
static uint8_t get_last_features_classic_index(void)
{
assert(controller_param.readable);
return controller_param.last_features_classic_page_index;
}{ ... }
static const bt_device_features_t *get_features_ble(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return &controller_param.features_ble;
}{ ... }
static const uint8_t *get_ble_supported_states(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_supported_states;
}{ ... }
static bool supports_simple_pairing(void)
{
assert(controller_param.readable);
return controller_param.simple_pairing_supported;
}{ ... }
static bool supports_secure_connections(void)
{
assert(controller_param.readable);
return controller_param.secure_connections_supported;
}{ ... }
static bool supports_simultaneous_le_bredr(void)
{
assert(controller_param.readable);
return HCI_SIMUL_LE_BREDR_SUPPORTED(controller_param.features_classic[0].as_array);
}{ ... }
static bool supports_reading_remote_extended_features(void)
{
assert(controller_param.readable);
return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(controller_param.supported_commands);
}{ ... }
static bool supports_interlaced_inquiry_scan(void)
{
assert(controller_param.readable);
return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(controller_param.features_classic[0].as_array);
}{ ... }
static bool supports_rssi_with_inquiry_results(void)
{
assert(controller_param.readable);
return HCI_LMP_INQ_RSSI_SUPPORTED(controller_param.features_classic[0].as_array);
}{ ... }
static bool supports_extended_inquiry_response(void)
{
assert(controller_param.readable);
return HCI_EXT_INQ_RSP_SUPPORTED(controller_param.features_classic[0].as_array);
}{ ... }
static bool supports_master_slave_role_switch(void)
{
assert(controller_param.readable);
return HCI_SWITCH_SUPPORTED(controller_param.features_classic[0].as_array);
}{ ... }
static bool supports_ble(void)
{
assert(controller_param.readable);
return controller_param.ble_supported;
}{ ... }
static bool supports_ble_privacy(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(controller_param.features_ble.as_array);
}{ ... }
static bool supports_ble_packet_extension(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return HCI_LE_DATA_LEN_EXT_SUPPORTED(controller_param.features_ble.as_array);
}{ ... }
static bool supports_ble_connection_parameters_request(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_param.features_ble.as_array);
}{ ... }
static uint16_t get_acl_data_size_classic(void)
{
assert(controller_param.readable);
return controller_param.acl_data_size_classic;
}{ ... }
static uint16_t get_acl_data_size_ble(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.acl_data_size_ble;
}{ ... }
static uint16_t get_acl_packet_size_classic(void)
{
assert(controller_param.readable);
return controller_param.acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
}{ ... }
static uint16_t get_acl_packet_size_ble(void)
{
assert(controller_param.readable);
return controller_param.acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
}{ ... }
static uint16_t get_ble_suggested_default_data_length(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_suggested_default_data_length;
}{ ... }
static uint16_t get_ble_suggested_default_data_txtime(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_suggested_default_data_txtime;
}{ ... }
static uint16_t get_acl_buffer_count_classic(void)
{
assert(controller_param.readable);
return controller_param.acl_buffer_count_classic;
}{ ... }
static uint8_t get_acl_buffer_count_ble(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.acl_buffer_count_ble;
}{ ... }
static uint8_t get_ble_white_list_size(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_white_list_size;
}{ ... }
static uint8_t get_ble_resolving_list_max_size(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_resolving_list_max_size;
}{ ... }
static void set_ble_resolving_list_max_size(int resolving_list_max_size)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
controller_param.ble_resolving_list_max_size = resolving_list_max_size;
}{ ... }
#if (BLE_50_FEATURE_SUPPORT == TRUE)
static uint16_t ble_get_ext_adv_data_max_len(void)
{
assert(controller_param.readable);
assert(controller_param.ble_supported);
return controller_param.ble_ext_adv_data_max_len;
}{...}
/* ... */#endif
#if (BTM_SCO_HCI_INCLUDED == TRUE)
static uint8_t get_sco_data_size(void)
{
assert(controller_param.readable);
return controller_param.sco_data_size;
}{...}
static uint8_t get_sco_buffer_count(void)
{
assert(controller_param.readable);
return controller_param.sco_buffer_count;
}{...}
/* ... */#endif
static const controller_t interface = {
start_up,
shut_down,
get_is_ready,
get_address,
get_bt_version,
get_features_classic,
get_last_features_classic_index,
get_features_ble,
get_ble_supported_states,
supports_simple_pairing,
supports_secure_connections,
supports_simultaneous_le_bredr,
supports_reading_remote_extended_features,
supports_interlaced_inquiry_scan,
supports_rssi_with_inquiry_results,
supports_extended_inquiry_response,
supports_master_slave_role_switch,
supports_ble,
supports_ble_packet_extension,
supports_ble_connection_parameters_request,
supports_ble_privacy,
get_acl_data_size_classic,
get_acl_data_size_ble,
get_acl_packet_size_classic,
get_acl_packet_size_ble,
get_ble_suggested_default_data_length,
get_ble_suggested_default_data_txtime,
get_acl_buffer_count_classic,
get_acl_buffer_count_ble,
get_ble_white_list_size,
get_ble_resolving_list_max_size,
set_ble_resolving_list_max_size,
#if (BLE_50_FEATURE_SUPPORT == TRUE)
ble_get_ext_adv_data_max_len,
#endif
#if (BTM_SCO_HCI_INCLUDED == TRUE)
get_sco_data_size,
get_sco_buffer_count,/* ... */
#endif
}{...};
const controller_t *controller_get_interface(void)
{
static bool loaded = false;
if (!loaded) {
loaded = true;
#if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE)
controller_param_ptr = (controller_local_param_t *)osi_calloc(sizeof(controller_local_param_t));
assert(controller_param_ptr);/* ... */
#endif
controller_param.hci = hci_layer_get_interface();
controller_param.packet_factory = hci_packet_factory_get_interface();
controller_param.packet_parser = hci_packet_parser_get_interface();
}{...}
return &interface;
}{ ... }