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