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 /************************************************************************************
16 *
17 * Filename: btc_util.c
18 *
19 * Description: Miscellaneous helper functions
20 *
21 *
22 ***********************************************************************************/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "btc/btc_util.h"
30 #if (BTA_AV_INCLUDED == TRUE)
31 #include "bta/bta_av_api.h"
32 #endif ///BTA_AV_INCLUDED == TRUE
33
34 #if (BTA_AG_INCLUDED == TRUE)
35 #include "bta/bta_ag_api.h"
36 #endif ///BTA_AG_INCLUDED == TRUE
37
38 #if (BTA_HH_INCLUDED == TRUE)
39 #include "bta/bta_hh_api.h"
40 #endif ///BTA_HH_INCLUDED == TRUE
41 #include "bta/bta_hd_api.h"
42 #include "common/bt_defs.h"
43 #include "stack/btm_api.h"
44 #include "bta/bta_api.h"
45
46 /************************************************************************************
47 ** Constants & Macros
48 ************************************************************************************/
49 #define ISDIGIT(a) ((a>='0') && (a<='9'))
50 #define ISXDIGIT(a) (((a>='0') && (a<='9'))||((a>='A') && (a<='F'))||((a>='a') && (a<='f')))
51
52 /************************************************************************************
53 ** Local type definitions
54 ************************************************************************************/
55
56 /************************************************************************************
57 ** Static variables
58 ************************************************************************************/
59
60 /************************************************************************************
61 ** Static functions
62 ************************************************************************************/
63
64 /************************************************************************************
65 ** Externs
66 ************************************************************************************/
67
68 /************************************************************************************
69 ** Functions
70 ************************************************************************************/
71
72 /*****************************************************************************
73 ** Logging helper functions
74 *****************************************************************************/
75 #if(BTA_AV_INCLUDED == TRUE)
dump_rc_event(UINT8 event)76 const char *dump_rc_event(UINT8 event)
77 {
78 switch (event) {
79 CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
80 CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
81 CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
82 CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
83 CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
84 CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
85 CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
86 CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
87 default:
88 return "UNKNOWN_EVENT";
89 }
90 }
91
dump_rc_notification_event_id(UINT8 event_id)92 const char *dump_rc_notification_event_id(UINT8 event_id)
93 {
94 switch (event_id) {
95 CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE)
96 CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE)
97 CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END)
98 CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START)
99 CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED)
100 CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE)
101 CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE)
102 CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE)
103 CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE)
104
105 default:
106 return "Unhandled Event ID";
107 }
108 }
109
dump_rc_pdu(UINT8 pdu)110 const char *dump_rc_pdu(UINT8 pdu)
111 {
112 switch (pdu) {
113 CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR)
114 CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES)
115 CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE)
116 CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE)
117 CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT)
118 CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT)
119 CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET)
120 CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT)
121 CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR)
122 CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS)
123 CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION)
124 CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP)
125 CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP)
126 CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME)
127 default:
128 return "Unknown PDU";
129 }
130 }
131 #endif ///BTA_AV_INCLUDED == TRUE
132
133 #if (BTA_AG_INCLUDED == TRUE)
dump_hf_conn_state(UINT16 event)134 const char* dump_hf_conn_state(UINT16 event)
135 {
136 switch(event)
137 {
138 CASE_RETURN_STR(ESP_HF_CONNECTION_STATE_DISCONNECTED)
139 CASE_RETURN_STR(ESP_HF_CONNECTION_STATE_CONNECTING)
140 CASE_RETURN_STR(ESP_HF_CONNECTION_STATE_CONNECTED)
141 CASE_RETURN_STR(ESP_HF_CONNECTION_STATE_SLC_CONNECTED)
142 CASE_RETURN_STR(ESP_HF_CONNECTION_STATE_DISCONNECTING)
143 default:
144 return "UNKNOWN MSG ID";
145 }
146 }
147
dump_hf_event(UINT16 event)148 const char* dump_hf_event(UINT16 event)
149 {
150 switch(event)
151 {
152 CASE_RETURN_STR(BTA_AG_ENABLE_EVT)
153 CASE_RETURN_STR(BTA_AG_REGISTER_EVT)
154 CASE_RETURN_STR(BTA_AG_OPEN_EVT)
155 CASE_RETURN_STR(BTA_AG_CLOSE_EVT)
156 CASE_RETURN_STR(BTA_AG_CONN_EVT)
157 CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT)
158 CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT)
159 CASE_RETURN_STR(BTA_AG_SPK_EVT)
160 CASE_RETURN_STR(BTA_AG_MIC_EVT)
161 CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT)
162 CASE_RETURN_STR(BTA_AG_DISABLE_EVT)
163 #if (BTM_WBS_INCLUDED == TRUE)
164 CASE_RETURN_STR(BTA_AG_WBS_EVT)
165 #endif
166 CASE_RETURN_STR(BTA_AG_AT_A_EVT)
167 CASE_RETURN_STR(BTA_AG_AT_D_EVT)
168 CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT)
169 CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT)
170 CASE_RETURN_STR(BTA_AG_AT_CIND_EVT)
171 CASE_RETURN_STR(BTA_AG_AT_VTS_EVT)
172 CASE_RETURN_STR(BTA_AG_AT_BINP_EVT)
173 CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT)
174 CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT)
175 CASE_RETURN_STR(BTA_AG_AT_NREC_EVT)
176 CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT)
177 CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT)
178 CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT)
179 CASE_RETURN_STR(BTA_AG_AT_COPS_EVT)
180 CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT)
181 CASE_RETURN_STR(BTA_AG_AT_CBC_EVT)
182 CASE_RETURN_STR(BTA_AG_AT_BAC_EVT)
183 CASE_RETURN_STR(BTA_AG_AT_BCS_EVT)
184
185 default:
186 return "UNKNOWN MSG ID";
187 }
188 }
189
dump_hf_call_state(esp_hf_call_status_t call_state)190 const char* dump_hf_call_state(esp_hf_call_status_t call_state)
191 {
192 switch(call_state)
193 {
194 CASE_RETURN_STR(ESP_HF_CALL_STATUS_NO_CALLS)
195 CASE_RETURN_STR(ESP_HF_CALL_STATUS_CALL_IN_PROGRESS)
196 default:
197 return "UNKNOWN CALL STATE";
198 }
199 }
200
dump_hf_call_setup_state(esp_hf_call_setup_status_t call_setup_state)201 const char* dump_hf_call_setup_state(esp_hf_call_setup_status_t call_setup_state)
202 {
203 switch(call_setup_state)
204 {
205 CASE_RETURN_STR(ESP_HF_CALL_SETUP_STATUS_IDLE)
206 CASE_RETURN_STR(ESP_HF_CALL_SETUP_STATUS_INCOMING)
207 CASE_RETURN_STR(ESP_HF_CALL_SETUP_STATUS_OUTGOING_DIALING)
208 CASE_RETURN_STR(ESP_HF_CALL_SETUP_STATUS_OUTGOING_ALERTING)
209 default:
210 return "UNKNOWN CALL SETUP STATE";
211 }
212 }
213
214 #endif // #if (BTA_AG_INCLUDED == TRUE)
215
216 #if (BTA_HH_INCLUDED == TRUE)
dump_hh_event(uint16_t event)217 const char *dump_hh_event(uint16_t event)
218 {
219 switch (event) {
220 CASE_RETURN_STR(BTA_HH_ENABLE_EVT)
221 CASE_RETURN_STR(BTA_HH_DISABLE_EVT)
222 CASE_RETURN_STR(BTA_HH_OPEN_EVT)
223 CASE_RETURN_STR(BTA_HH_CLOSE_EVT)
224 CASE_RETURN_STR(BTA_HH_GET_RPT_EVT)
225 CASE_RETURN_STR(BTA_HH_SET_RPT_EVT)
226 CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT)
227 CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT)
228 CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT)
229 CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT)
230 CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT)
231 CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT)
232 CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT)
233 CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT)
234 CASE_RETURN_STR(BTA_HH_DATA_EVT)
235 CASE_RETURN_STR(BTA_HH_API_ERR_EVT)
236 CASE_RETURN_STR(BTA_HH_UPDATE_SCPP_EVT)
237 CASE_RETURN_STR(BTA_HH_DATA_IND_EVT)
238 default:
239 return "UNKNOWN MSG ID";
240 }
241 }
242 #endif ///BTA_HH_INCLUDED
243
244 #if BTA_HD_INCLUDED == TRUE
dump_hd_event(uint16_t event)245 const char* dump_hd_event(uint16_t event) {
246 switch (event) {
247 CASE_RETURN_STR(BTA_HD_ENABLE_EVT)
248 CASE_RETURN_STR(BTA_HD_DISABLE_EVT)
249 CASE_RETURN_STR(BTA_HD_REGISTER_APP_EVT)
250 CASE_RETURN_STR(BTA_HD_UNREGISTER_APP_EVT)
251 CASE_RETURN_STR(BTA_HD_OPEN_EVT)
252 CASE_RETURN_STR(BTA_HD_CLOSE_EVT)
253 CASE_RETURN_STR(BTA_HD_GET_REPORT_EVT)
254 CASE_RETURN_STR(BTA_HD_SET_REPORT_EVT)
255 CASE_RETURN_STR(BTA_HD_SET_PROTOCOL_EVT)
256 CASE_RETURN_STR(BTA_HD_INTR_DATA_EVT)
257 CASE_RETURN_STR(BTA_HD_VC_UNPLUG_EVT)
258 //CASE_RETURN_STR(BTA_HD_CONN_STATE_EVT)
259 CASE_RETURN_STR(BTA_HD_API_ERR_EVT)
260 default:
261 return "UNKNOWN MSG ID";
262 }
263 }
264 #endif ///BTA_HD_INCLUDED
265
devclass2uint(DEV_CLASS dev_class)266 UINT32 devclass2uint(DEV_CLASS dev_class)
267 {
268 UINT32 cod = 0;
269
270 if (dev_class != NULL) {
271 /* if COD is 0, irrespective of the device type set it to Unclassified device */
272 cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16);
273 }
274 return cod;
275 }
276
uint2devclass(UINT32 cod,DEV_CLASS dev_class)277 void uint2devclass(UINT32 cod, DEV_CLASS dev_class)
278 {
279 dev_class[2] = (UINT8)cod;
280 dev_class[1] = (UINT8)(cod >> 8);
281 dev_class[0] = (UINT8)(cod >> 16);
282 }
283
284 static const UINT8 base_uuid_be[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
285 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
286
uuid128_be_to_esp_uuid(esp_bt_uuid_t * u,uint8_t * uuid128)287 void uuid128_be_to_esp_uuid(esp_bt_uuid_t *u, uint8_t* uuid128)
288 {
289 if (memcmp(base_uuid_be+4, uuid128 + 4, 12) != 0) {
290 u->len = ESP_UUID_LEN_128;
291 uint8_t *p_i = uuid128 + ESP_UUID_LEN_128 - 1;
292 uint8_t *p_o = u->uuid.uuid128;
293 uint8_t *p_end = p_o + ESP_UUID_LEN_128;
294 for (; p_o != p_end; *p_o++ = *p_i--)
295 ;
296 } else if (uuid128[0] == 0 && uuid128[1] == 0) {
297 u->len = 2;
298 u->uuid.uuid16 = (uuid128[2] << 8) + uuid128[3];
299 } else {
300 u->len = 4;
301 u->uuid.uuid32 = (uuid128[2] << 8) + uuid128[3];
302 u->uuid.uuid32 += (uuid128[0] << 24) + (uuid128[1] << 16);
303 }
304
305 return;
306 }
307
uuid_to_string_legacy(bt_uuid_t * p_uuid,char * str)308 void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str)
309 {
310 uint32_t uuid0, uuid4;
311 uint16_t uuid1, uuid2, uuid3, uuid5;
312
313 memcpy(&uuid0, &(p_uuid->uu[0]), 4);
314 memcpy(&uuid1, &(p_uuid->uu[4]), 2);
315 memcpy(&uuid2, &(p_uuid->uu[6]), 2);
316 memcpy(&uuid3, &(p_uuid->uu[8]), 2);
317 memcpy(&uuid4, &(p_uuid->uu[10]), 4);
318 memcpy(&uuid5, &(p_uuid->uu[14]), 2);
319
320 sprintf((char *)str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
321 ntohl(uuid0), ntohs(uuid1),
322 ntohs(uuid2), ntohs(uuid3),
323 ntohl(uuid4), ntohs(uuid5));
324 return;
325 }
326
btc_hci_to_esp_status(uint8_t hci_status)327 esp_bt_status_t btc_hci_to_esp_status(uint8_t hci_status)
328 {
329 esp_bt_status_t esp_status = ESP_BT_STATUS_FAIL;
330 switch(hci_status) {
331 case HCI_SUCCESS:
332 esp_status = ESP_BT_STATUS_SUCCESS;
333 break;
334 case HCI_ERR_HOST_TIMEOUT:
335 esp_status = ESP_BT_STATUS_TIMEOUT;
336 break;
337 case HCI_ERR_ILLEGAL_COMMAND:
338 esp_status = ESP_BT_STATUS_PENDING;
339 break;
340 case HCI_ERR_UNACCEPT_CONN_INTERVAL:
341 esp_status = ESP_BT_STATUS_UNACCEPT_CONN_INTERVAL;
342 break;
343 case HCI_ERR_PARAM_OUT_OF_RANGE:
344 esp_status = ESP_BT_STATUS_PARAM_OUT_OF_RANGE;
345 break;
346 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
347 esp_status = ESP_BT_STATUS_ERR_ILLEGAL_PARAMETER_FMT;
348 break;
349 default:
350 esp_status = ESP_BT_STATUS_FAIL;
351 break;
352 }
353
354 return esp_status;
355 }
356
btc_btm_status_to_esp_status(uint8_t btm_status)357 esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status)
358 {
359 esp_bt_status_t esp_status = ESP_BT_STATUS_FAIL;
360 switch(btm_status){
361 case BTM_SUCCESS:
362 esp_status = ESP_BT_STATUS_SUCCESS;
363 break;
364 case BTM_BUSY:
365 esp_status = ESP_BT_STATUS_BUSY;
366 break;
367 case BTM_NO_RESOURCES:
368 esp_status = ESP_BT_STATUS_NOMEM;
369 break;
370 case BTM_ILLEGAL_VALUE:
371 esp_status = ESP_BT_STATUS_PARM_INVALID;
372 break;
373 case BTM_ERR_PROCESSING:
374 esp_status = ESP_BT_STATUS_PENDING;
375 break;
376 case BTM_PEER_LE_DATA_LEN_UNSUPPORTED:
377 esp_status = ESP_BT_STATUS_PEER_LE_DATA_LEN_UNSUPPORTED;
378 break;
379 case BTM_CONTROL_LE_DATA_LEN_UNSUPPORTED:
380 esp_status = ESP_BT_STATUS_CONTROL_LE_DATA_LEN_UNSUPPORTED;
381 break;
382 case BTM_SET_PRIVACY_SUCCESS:
383 esp_status = ESP_BT_STATUS_SUCCESS;
384 break;
385 case BTM_SET_PRIVACY_FAIL:
386 esp_status = ESP_BT_STATUS_FAIL;
387 break;
388 default:
389 esp_status = ESP_BT_STATUS_FAIL;
390 break;
391 }
392
393 return esp_status;
394 }
395
btc_bta_status_to_esp_status(uint8_t bta_status)396 esp_bt_status_t btc_bta_status_to_esp_status (uint8_t bta_status)
397 {
398 esp_bt_status_t esp_status = ESP_BT_STATUS_FAIL;
399 switch(bta_status){
400 case BTA_SUCCESS:
401 esp_status = ESP_BT_STATUS_SUCCESS;
402 break;
403 case BTA_FAILURE:
404 esp_status = ESP_BT_STATUS_FAIL;
405 break;
406 case BTA_PENDING:
407 esp_status = ESP_BT_STATUS_PENDING;
408 break;
409 case BTA_BUSY:
410 esp_status = ESP_BT_STATUS_BUSY;
411 break;
412 case BTA_NO_RESOURCES:
413 esp_status = ESP_BT_STATUS_NOMEM;
414 break;
415 case BTA_WRONG_MODE:
416 esp_status = ESP_BT_STATUS_NOT_READY;
417 break;
418 case BTA_EIR_TOO_LARGE:
419 esp_status = ESP_BT_STATUS_EIR_TOO_LARGE;
420 break;
421 default:
422 esp_status = ESP_BT_STATUS_FAIL;
423 break;
424 }
425
426 return esp_status;
427 }
428