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 #define LOG_TAG "bt_hci"
20 
21 #include <assert.h>
22 
23 #include "buffer_allocator.h"
24 #include "bt_types.h"
25 #include "hcimsgs.h"
26 #include "hci_layer.h"
27 #include "hci_packet_parser.h"
28 #include "osi/include/log.h"
29 
30 static const command_opcode_t NO_OPCODE_CHECKING = 0;
31 
32 static const allocator_t *buffer_allocator;
33 
34 static uint8_t *read_command_complete_header(
35   BT_HDR *response,
36   command_opcode_t expected_opcode,
37   size_t minimum_bytes_after);
38 
parse_generic_command_complete(BT_HDR * response)39 static void parse_generic_command_complete(BT_HDR *response) {
40   read_command_complete_header(response, NO_OPCODE_CHECKING, 0 /* bytes after */);
41 
42   buffer_allocator->free(response);
43 }
44 
parse_read_buffer_size_response(BT_HDR * response,uint16_t * data_size_ptr,uint16_t * acl_buffer_count_ptr)45 static void parse_read_buffer_size_response(
46     BT_HDR *response,
47     uint16_t *data_size_ptr,
48     uint16_t *acl_buffer_count_ptr) {
49 
50   uint8_t *stream = read_command_complete_header(response, HCI_READ_BUFFER_SIZE, 5 /* bytes after */);
51   assert(stream != NULL);
52   STREAM_TO_UINT16(*data_size_ptr, stream);
53   STREAM_SKIP_UINT8(stream); // skip the sco packet length
54   STREAM_TO_UINT16(*acl_buffer_count_ptr, stream);
55 
56   buffer_allocator->free(response);
57 }
58 
parse_read_local_version_info_response(BT_HDR * response,bt_version_t * bt_version)59 static void parse_read_local_version_info_response(
60     BT_HDR *response,
61     bt_version_t *bt_version) {
62 
63   uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_VERSION_INFO, 8 /* bytes after */);
64   assert(stream != NULL);
65   STREAM_TO_UINT8(bt_version->hci_version, stream);
66   STREAM_TO_UINT16(bt_version->hci_revision, stream);
67   STREAM_TO_UINT8(bt_version->lmp_version, stream);
68   STREAM_TO_UINT16(bt_version->manufacturer, stream);
69   STREAM_TO_UINT16(bt_version->lmp_subversion, stream);
70 
71   buffer_allocator->free(response);
72 }
73 
parse_read_bd_addr_response(BT_HDR * response,bt_bdaddr_t * address_ptr)74 static void parse_read_bd_addr_response(
75     BT_HDR *response,
76     bt_bdaddr_t *address_ptr) {
77 
78   uint8_t *stream = read_command_complete_header(response, HCI_READ_BD_ADDR, sizeof(bt_bdaddr_t) /* bytes after */);
79   assert(stream != NULL);
80   STREAM_TO_BDADDR(address_ptr->address, stream);
81 
82   buffer_allocator->free(response);
83 }
84 
parse_read_local_supported_commands_response(BT_HDR * response,uint8_t * supported_commands_ptr,size_t supported_commands_length)85 static void parse_read_local_supported_commands_response(
86     BT_HDR *response,
87     uint8_t *supported_commands_ptr,
88     size_t supported_commands_length) {
89 
90   uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_SUPPORTED_CMDS, supported_commands_length /* bytes after */);
91   assert(stream != NULL);
92   STREAM_TO_ARRAY(supported_commands_ptr, stream, (int)supported_commands_length);
93 
94   buffer_allocator->free(response);
95 }
96 
parse_read_local_extended_features_response(BT_HDR * response,uint8_t * page_number_ptr,uint8_t * max_page_number_ptr,bt_device_features_t * feature_pages,size_t feature_pages_count)97 static void parse_read_local_extended_features_response(
98     BT_HDR *response,
99     uint8_t *page_number_ptr,
100     uint8_t *max_page_number_ptr,
101     bt_device_features_t *feature_pages,
102     size_t feature_pages_count) {
103 
104   uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_EXT_FEATURES, 2 + sizeof(bt_device_features_t) /* bytes after */);
105   if (stream != NULL) {
106     STREAM_TO_UINT8(*page_number_ptr, stream);
107     STREAM_TO_UINT8(*max_page_number_ptr, stream);
108 
109     assert(*page_number_ptr < feature_pages_count);
110     STREAM_TO_ARRAY(feature_pages[*page_number_ptr].as_array, stream, (int)sizeof(bt_device_features_t));
111   } else {
112     LOG_ERROR("%s() - WARNING: READING EXTENDED FEATURES FAILED. "
113                 "THIS MAY INDICATE A FIRMWARE/CONTROLLER ISSUE.", __func__);
114   }
115 
116   buffer_allocator->free(response);
117 }
118 
parse_ble_read_white_list_size_response(BT_HDR * response,uint8_t * white_list_size_ptr)119 static void parse_ble_read_white_list_size_response(
120     BT_HDR *response,
121     uint8_t *white_list_size_ptr) {
122 
123   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_WHITE_LIST_SIZE, 1 /* byte after */);
124   assert(stream != NULL);
125   STREAM_TO_UINT8(*white_list_size_ptr, stream);
126 
127   buffer_allocator->free(response);
128 }
129 
parse_ble_read_buffer_size_response(BT_HDR * response,uint16_t * data_size_ptr,uint8_t * acl_buffer_count_ptr)130 static void parse_ble_read_buffer_size_response(
131     BT_HDR *response,
132     uint16_t *data_size_ptr,
133     uint8_t *acl_buffer_count_ptr) {
134 
135   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_BUFFER_SIZE, 3 /* bytes after */);
136   assert(stream != NULL);
137   STREAM_TO_UINT16(*data_size_ptr, stream);
138   STREAM_TO_UINT8(*acl_buffer_count_ptr, stream);
139 
140   buffer_allocator->free(response);
141 }
142 
parse_ble_read_supported_states_response(BT_HDR * response,uint8_t * supported_states,size_t supported_states_size)143 static void parse_ble_read_supported_states_response(
144     BT_HDR *response,
145     uint8_t *supported_states,
146     size_t supported_states_size) {
147 
148   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_SUPPORTED_STATES, supported_states_size /* bytes after */);
149   assert(stream != NULL);
150   STREAM_TO_ARRAY(supported_states, stream, (int)supported_states_size);
151 
152   buffer_allocator->free(response);
153 }
154 
parse_ble_read_local_supported_features_response(BT_HDR * response,bt_device_features_t * supported_features)155 static void parse_ble_read_local_supported_features_response(
156     BT_HDR *response,
157     bt_device_features_t *supported_features) {
158 
159   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_LOCAL_SPT_FEAT, sizeof(bt_device_features_t) /* bytes after */);
160   assert(stream != NULL);
161   STREAM_TO_ARRAY(supported_features->as_array, stream, (int)sizeof(bt_device_features_t));
162 
163   buffer_allocator->free(response);
164 }
165 
parse_ble_read_resolving_list_size_response(BT_HDR * response,uint8_t * resolving_list_size_ptr)166 static void parse_ble_read_resolving_list_size_response(
167     BT_HDR *response,
168     uint8_t *resolving_list_size_ptr) {
169 
170   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_RESOLVING_LIST_SIZE, 1 /* bytes after */);
171   STREAM_TO_UINT8(*resolving_list_size_ptr, stream);
172 
173   buffer_allocator->free(response);
174 }
175 
parse_ble_read_suggested_default_data_length_response(BT_HDR * response,uint16_t * ble_default_packet_length_ptr)176 static void parse_ble_read_suggested_default_data_length_response(
177     BT_HDR *response,
178     uint16_t *ble_default_packet_length_ptr) {
179 
180   uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_DEFAULT_DATA_LENGTH, 2 /* bytes after */);
181   STREAM_TO_UINT8(*ble_default_packet_length_ptr, stream);
182 
183   buffer_allocator->free(response);
184 }
185 
186 // Internal functions
187 
read_command_complete_header(BT_HDR * response,command_opcode_t expected_opcode,size_t minimum_bytes_after)188 static uint8_t *read_command_complete_header(
189     BT_HDR *response,
190     command_opcode_t expected_opcode,
191     size_t minimum_bytes_after) {
192 
193   uint8_t *stream = response->data + response->offset;
194 
195   // Read the event header
196   uint8_t event_code;
197   uint8_t parameter_length;
198   STREAM_TO_UINT8(event_code, stream);
199   STREAM_TO_UINT8(parameter_length, stream);
200 
201   const size_t parameter_bytes_we_read_here = 4;
202 
203   // Check the event header values against what we expect
204   assert(event_code == HCI_COMMAND_COMPLETE_EVT);
205   assert(parameter_length >= (parameter_bytes_we_read_here + minimum_bytes_after));
206 
207   // Read the command complete header
208   command_opcode_t opcode;
209   uint8_t status;
210   STREAM_SKIP_UINT8(stream); // skip the number of hci command packets field
211   STREAM_TO_UINT16(opcode, stream);
212 
213   // Check the command complete header values against what we expect
214   if (expected_opcode != NO_OPCODE_CHECKING) {
215     assert(opcode == expected_opcode);
216   }
217 
218   // Assume the next field is the status field
219   STREAM_TO_UINT8(status, stream);
220 
221   if (status != HCI_SUCCESS)
222     return NULL;
223 
224   return stream;
225 }
226 
227 static const hci_packet_parser_t interface = {
228   parse_generic_command_complete,
229   parse_read_buffer_size_response,
230   parse_read_local_version_info_response,
231   parse_read_bd_addr_response,
232   parse_read_local_supported_commands_response,
233   parse_read_local_extended_features_response,
234   parse_ble_read_white_list_size_response,
235   parse_ble_read_buffer_size_response,
236   parse_ble_read_supported_states_response,
237   parse_ble_read_local_supported_features_response,
238   parse_ble_read_resolving_list_size_response,
239   parse_ble_read_suggested_default_data_length_response
240 };
241 
hci_packet_parser_get_interface()242 const hci_packet_parser_t *hci_packet_parser_get_interface() {
243   buffer_allocator = buffer_allocator_get_interface();
244   return &interface;
245 }
246 
hci_packet_parser_get_test_interface(allocator_t * buffer_allocator_interface)247 const hci_packet_parser_t *hci_packet_parser_get_test_interface(
248     allocator_t *buffer_allocator_interface) {
249   buffer_allocator = buffer_allocator_interface;
250   return &interface;
251 }
252