1 /******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "common/bt_defs.h"
20
21 #include "osi/allocator.h"
22 #include "stack/bt_types.h"
23 #include "stack/hcidefs.h"
24 #include "stack/hcimsgs.h"
25 #include "hci/hci_internals.h"
26 #include "hci/hci_layer.h"
27 #include "hci/hci_packet_factory.h"
28
29
30 static BT_HDR *make_packet(size_t data_size);
31 static BT_HDR *make_command_no_params(uint16_t opcode);
32 static BT_HDR *make_command(uint16_t opcode, size_t parameter_size, uint8_t **stream_out);
33
34 // Interface functions
35
make_reset(void)36 static BT_HDR *make_reset(void)
37 {
38 return make_command_no_params(HCI_RESET);
39 }
40
make_read_buffer_size(void)41 static BT_HDR *make_read_buffer_size(void)
42 {
43 return make_command_no_params(HCI_READ_BUFFER_SIZE);
44 }
45
make_set_c2h_flow_control(uint8_t enable)46 static BT_HDR *make_set_c2h_flow_control(uint8_t enable)
47 {
48 uint8_t *stream;
49 const uint8_t parameter_size = 1;
50 BT_HDR *packet = make_command(HCI_SET_HC_TO_HOST_FLOW_CTRL, parameter_size, &stream);
51
52 UINT8_TO_STREAM(stream, enable);
53 return packet;
54 }
55
make_set_adv_report_flow_control(uint8_t enable,uint16_t num,uint16_t lost_threshold)56 static BT_HDR *make_set_adv_report_flow_control(uint8_t enable, uint16_t num, uint16_t lost_threshold)
57 {
58 uint8_t *stream;
59 const uint8_t parameter_size = 1 + 2 + 2;
60 BT_HDR *packet = make_command(HCI_VENDOR_BLE_SET_ADV_FLOW_CONTROL, parameter_size, &stream);
61
62 UINT8_TO_STREAM(stream, enable);
63 UINT16_TO_STREAM(stream, num);
64 UINT16_TO_STREAM(stream, lost_threshold);
65 return packet;
66 }
67
make_host_buffer_size(uint16_t acl_size,uint8_t sco_size,uint16_t acl_count,uint16_t sco_count)68 static BT_HDR *make_host_buffer_size(uint16_t acl_size, uint8_t sco_size, uint16_t acl_count, uint16_t sco_count)
69 {
70 uint8_t *stream;
71 const uint8_t parameter_size = 2 + 1 + 2 + 2; // from each of the parameters
72 BT_HDR *packet = make_command(HCI_HOST_BUFFER_SIZE, parameter_size, &stream);
73
74 UINT16_TO_STREAM(stream, acl_size);
75 UINT8_TO_STREAM(stream, sco_size);
76 UINT16_TO_STREAM(stream, acl_count);
77 UINT16_TO_STREAM(stream, sco_count);
78 return packet;
79 }
80
make_read_local_version_info(void)81 static BT_HDR *make_read_local_version_info(void)
82 {
83 return make_command_no_params(HCI_READ_LOCAL_VERSION_INFO);
84 }
85
make_read_bd_addr(void)86 static BT_HDR *make_read_bd_addr(void)
87 {
88 return make_command_no_params(HCI_READ_BD_ADDR);
89 }
90
make_read_local_supported_commands(void)91 static BT_HDR *make_read_local_supported_commands(void)
92 {
93 return make_command_no_params(HCI_READ_LOCAL_SUPPORTED_CMDS);
94 }
95
make_read_local_supported_features(void)96 static BT_HDR *make_read_local_supported_features(void)
97 {
98 return make_command_no_params(HCI_READ_LOCAL_FEATURES);
99 }
100
make_read_local_extended_features(uint8_t page_number)101 static BT_HDR *make_read_local_extended_features(uint8_t page_number)
102 {
103 uint8_t *stream;
104 const uint8_t parameter_size = 1;
105 BT_HDR *packet = make_command(HCI_READ_LOCAL_EXT_FEATURES, parameter_size, &stream);
106
107 UINT8_TO_STREAM(stream, page_number);
108 return packet;
109 }
110
make_write_simple_pairing_mode(uint8_t mode)111 static BT_HDR *make_write_simple_pairing_mode(uint8_t mode)
112 {
113 uint8_t *stream;
114 const uint8_t parameter_size = 1;
115 BT_HDR *packet = make_command(HCI_WRITE_SIMPLE_PAIRING_MODE, parameter_size, &stream);
116
117 UINT8_TO_STREAM(stream, mode);
118 return packet;
119 }
120
make_write_secure_connections_host_support(uint8_t mode)121 static BT_HDR *make_write_secure_connections_host_support(uint8_t mode)
122 {
123 uint8_t *stream;
124 const uint8_t parameter_size = 1;
125 BT_HDR *packet = make_command(HCI_WRITE_SECURE_CONNS_SUPPORT, parameter_size, &stream);
126
127 UINT8_TO_STREAM(stream, mode);
128 return packet;
129 }
130
make_set_event_mask(const bt_event_mask_t * event_mask)131 static BT_HDR *make_set_event_mask(const bt_event_mask_t *event_mask)
132 {
133 uint8_t *stream;
134 uint8_t parameter_size = sizeof(bt_event_mask_t);
135 BT_HDR *packet = make_command(HCI_SET_EVENT_MASK, parameter_size, &stream);
136
137 ARRAY8_TO_STREAM(stream, event_mask->as_array);
138 return packet;
139 }
140
make_ble_write_host_support(uint8_t supported_host,uint8_t simultaneous_host)141 static BT_HDR *make_ble_write_host_support(uint8_t supported_host, uint8_t simultaneous_host)
142 {
143 uint8_t *stream;
144 const uint8_t parameter_size = 1 + 1;
145 BT_HDR *packet = make_command(HCI_WRITE_LE_HOST_SUPPORT, parameter_size, &stream);
146
147 UINT8_TO_STREAM(stream, supported_host);
148 UINT8_TO_STREAM(stream, simultaneous_host);
149 return packet;
150 }
151
make_ble_read_white_list_size(void)152 static BT_HDR *make_ble_read_white_list_size(void)
153 {
154 return make_command_no_params(HCI_BLE_READ_WHITE_LIST_SIZE);
155 }
156
make_ble_read_buffer_size(void)157 static BT_HDR *make_ble_read_buffer_size(void)
158 {
159 return make_command_no_params(HCI_BLE_READ_BUFFER_SIZE);
160 }
161
make_ble_read_supported_states(void)162 static BT_HDR *make_ble_read_supported_states(void)
163 {
164 return make_command_no_params(HCI_BLE_READ_SUPPORTED_STATES);
165 }
166
make_ble_read_local_supported_features(void)167 static BT_HDR *make_ble_read_local_supported_features(void)
168 {
169 return make_command_no_params(HCI_BLE_READ_LOCAL_SPT_FEAT);
170 }
171
make_ble_read_resolving_list_size(void)172 static BT_HDR *make_ble_read_resolving_list_size(void)
173 {
174 return make_command_no_params(HCI_BLE_READ_RESOLVING_LIST_SIZE);
175 }
176
make_ble_read_suggested_default_data_length(void)177 static BT_HDR *make_ble_read_suggested_default_data_length(void)
178 {
179 return make_command_no_params(HCI_BLE_READ_DEFAULT_DATA_LENGTH);
180 }
181
make_ble_write_suggested_default_data_length(uint16_t SuggestedMaxTxOctets,uint16_t SuggestedMaxTxTime)182 static BT_HDR *make_ble_write_suggested_default_data_length(uint16_t SuggestedMaxTxOctets, uint16_t SuggestedMaxTxTime)
183 {
184 uint8_t *stream;
185 uint8_t parameter_size = sizeof(uint16_t) + sizeof(uint16_t);
186 BT_HDR *packet = make_command(HCI_BLE_WRITE_DEFAULT_DATA_LENGTH, parameter_size, &stream);
187
188 UINT16_TO_STREAM(stream, SuggestedMaxTxOctets);
189 UINT16_TO_STREAM(stream, SuggestedMaxTxTime);
190 return packet;
191 }
192
make_ble_set_event_mask(const bt_event_mask_t * event_mask)193 static BT_HDR *make_ble_set_event_mask(const bt_event_mask_t *event_mask)
194 {
195 uint8_t *stream;
196 uint8_t parameter_size = sizeof(bt_event_mask_t);
197 BT_HDR *packet = make_command(HCI_BLE_SET_EVENT_MASK, parameter_size, &stream);
198
199 ARRAY8_TO_STREAM(stream, event_mask->as_array);
200 return packet;
201 }
202
make_write_sync_flow_control_enable(uint8_t enable)203 static BT_HDR *make_write_sync_flow_control_enable(uint8_t enable)
204 {
205 uint8_t *stream;
206 const uint8_t parameter_size = 1;
207 BT_HDR *packet = make_command(HCI_WRITE_SCO_FLOW_CTRL_ENABLE, parameter_size, &stream);
208
209 UINT8_TO_STREAM(stream, enable);
210 return packet;
211 }
212
make_write_default_erroneous_data_report(uint8_t enable)213 static BT_HDR *make_write_default_erroneous_data_report(uint8_t enable)
214 {
215 uint8_t *stream;
216 const uint8_t parameter_size = 1;
217 BT_HDR *packet = make_command(HCI_WRITE_ERRONEOUS_DATA_RPT, parameter_size, &stream);
218
219 UINT8_TO_STREAM(stream, enable);
220 return packet;
221 }
222 #if (BLE_50_FEATURE_SUPPORT == TRUE)
make_read_max_adv_data_len(void)223 static BT_HDR *make_read_max_adv_data_len(void)
224 {
225 return make_command_no_params(HCI_BLE_RD_MAX_ADV_DATA_LEN);
226 }
227 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
228 // Internal functions
229
make_command_no_params(uint16_t opcode)230 static BT_HDR *make_command_no_params(uint16_t opcode)
231 {
232 return make_command(opcode, 0, NULL);
233 }
234
make_command(uint16_t opcode,size_t parameter_size,uint8_t ** stream_out)235 static BT_HDR *make_command(uint16_t opcode, size_t parameter_size, uint8_t **stream_out)
236 {
237 BT_HDR *packet = make_packet(HCI_COMMAND_PREAMBLE_SIZE + parameter_size);
238
239 uint8_t *stream = packet->data;
240 UINT16_TO_STREAM(stream, opcode);
241 UINT8_TO_STREAM(stream, parameter_size);
242
243 if (stream_out != NULL) {
244 *stream_out = stream;
245 }
246
247 return packet;
248 }
249
make_packet(size_t data_size)250 static BT_HDR *make_packet(size_t data_size)
251 {
252 BT_HDR *ret = (BT_HDR *)osi_calloc(sizeof(BT_HDR) + data_size);
253 assert(ret);
254 ret->event = 0;
255 ret->offset = 0;
256 ret->layer_specific = 0;
257 ret->len = data_size;
258 return ret;
259 }
260
261 static const hci_packet_factory_t interface = {
262 make_reset,
263 make_read_buffer_size,
264 make_set_c2h_flow_control,
265 make_set_adv_report_flow_control,
266 make_host_buffer_size,
267 make_read_local_version_info,
268 make_read_bd_addr,
269 make_read_local_supported_commands,
270 make_read_local_supported_features,
271 make_read_local_extended_features,
272 make_write_simple_pairing_mode,
273 make_write_secure_connections_host_support,
274 make_set_event_mask,
275 make_ble_write_host_support,
276 make_ble_read_white_list_size,
277 make_ble_read_buffer_size,
278 make_ble_read_supported_states,
279 make_ble_read_local_supported_features,
280 make_ble_read_resolving_list_size,
281 #if (BLE_50_FEATURE_SUPPORT == TRUE)
282 make_read_max_adv_data_len,
283 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
284 make_ble_read_suggested_default_data_length,
285 make_ble_write_suggested_default_data_length,
286 make_ble_set_event_mask,
287 make_write_sync_flow_control_enable,
288 make_write_default_erroneous_data_report,
289 };
290
hci_packet_factory_get_interface(void)291 const hci_packet_factory_t *hci_packet_factory_get_interface(void)
292 {
293 return &interface;
294 }
295