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