Fix dlog format error
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-utils.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23
24 #include <dlog.h>
25 #include <glib.h>
26
27 #include "bt-hal-msg.h"
28 #include "bt-hal.h"
29 #include "bt-hal-utils.h"
30 #include "bt-hal-log.h"
31
32 #include <hardware/bluetooth.h>
33
34 /*
35  * converts uuid to string
36  * buf should be at least 39 bytes
37  *
38  * returns string representation of uuid
39  */
40 const char *bt_uuid_t2str(const uint8_t *uuid, char *buf)
41 {
42         int shift = 0;
43         unsigned int i;
44         int is_bt;
45
46         if (!uuid)
47                 return strncpy(buf, "NULL", strlen("NULL") + 1);
48
49         is_bt = !memcmp(&uuid[4], &BT_BASE_UUID[4], HAL_UUID_LEN - 4);
50
51         for (i = 0; i < HAL_UUID_LEN; i++) {
52                 if (i == 4 && is_bt)
53                         break;
54
55                 if (i == 4 || i == 6 || i == 8 || i == 10) {
56                         buf[i * 2 + shift] = '-';
57                         shift++;
58                 }
59                 snprintf(buf + i * 2 + shift, (HAL_UUID_LEN -(i * 2 + shift)), "%02x", uuid[i]);
60         }
61
62         return buf;
63 }
64
65 const char *btuuid2str(const uint8_t *uuid)
66 {
67         static char buf[MAX_UUID_STR_LEN];
68
69         return bt_uuid_t2str(uuid, buf);
70 }
71
72 /* Find first index of given value in table m */
73 int int2str_findint(int v, const struct int2str m[])
74 {
75         int i;
76
77         for (i = 0; m[i].str; ++i) {
78                 if (m[i].val == v)
79                         return i;
80         }
81         return -1;
82 }
83
84 /* Find first index of given string in table m */
85 int int2str_findstr(const char *str, const struct int2str m[])
86 {
87         int i;
88
89         for (i = 0; m[i].str; ++i) {
90                 if (strcmp(m[i].str, str) == 0)
91                         return i;
92         }
93         return -1;
94 }
95
96 /*
97  * convert bd_addr to string
98  * buf must be at least 18 char long
99  *
100  * returns buf
101  */
102 const char *bt_bdaddr_t2str(const bt_bdaddr_t *bd_addr, char *buf)
103 {
104         const uint8_t *p;
105
106         if (!bd_addr)
107                 return strncpy(buf, "NULL", strlen("NULL") + 1);
108
109         p = bd_addr->address;
110
111         snprintf(buf, MAX_ADDR_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
112                         p[0], p[1], p[2], p[3], p[4], p[5]);
113
114         return buf;
115 }
116
117 /* converts string to bt_bdaddr_t */
118 void str2bt_bdaddr_t(const char *str, bt_bdaddr_t *bd_addr)
119 {
120         uint8_t *p = bd_addr->address;
121
122         sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
123                         &p[0], &p[1], &p[2], &p[3], &p[4], &p[5]);
124 }
125
126 /* converts string to uuid */
127 void str2bt_uuid_t(const char *str, bt_uuid_t *uuid)
128 {
129         int i = 0;
130
131         memcpy(uuid, BT_BASE_UUID, sizeof(bt_uuid_t));
132
133         while (*str && i < (int) sizeof(bt_uuid_t)) {
134                 while (*str == '-')
135                         str++;
136
137                 if (sscanf(str, "%02hhx", &uuid->uu[i]) != 1)
138                         break;
139
140                 i++;
141                 str += 2;
142         }
143 }
144
145 const char *_bt_hal_dump_uuid_name(int uuid_no)
146 {
147         switch (uuid_no) {
148         CASE_RETURN_STR(BASE)
149         CASE_RETURN_STR(SDP)
150         CASE_RETURN_STR(UDP)
151         CASE_RETURN_STR(RFCOMM)
152         CASE_RETURN_STR(TCP)
153         CASE_RETURN_STR(TCS_BIN)
154         CASE_RETURN_STR(TCS_AT)
155         CASE_RETURN_STR(ATT)
156         CASE_RETURN_STR(OBEX)
157         CASE_RETURN_STR(IP)
158         CASE_RETURN_STR(FTP)
159         CASE_RETURN_STR(HTTP)
160         CASE_RETURN_STR(WSP)
161         CASE_RETURN_STR(BNEP)
162         CASE_RETURN_STR(UPNP)
163         CASE_RETURN_STR(HIDP)
164         CASE_RETURN_STR(HardcopyControlChannel)
165         CASE_RETURN_STR(HardcopyDataChannel)
166         CASE_RETURN_STR(HardcopyNotification)
167         CASE_RETURN_STR(AVCTP)
168         CASE_RETURN_STR(AVDTP)
169         CASE_RETURN_STR(CMTP)
170         CASE_RETURN_STR(MCAPControlChannel)
171         CASE_RETURN_STR(MCAPDataChannel)
172         CASE_RETURN_STR(L2CAP)
173         CASE_RETURN_STR(ServiceDiscoveryServerServiceClassID)
174         CASE_RETURN_STR(BrowseGroupDescriptorServiceClassID)
175         CASE_RETURN_STR(SerialPort)
176         CASE_RETURN_STR(LANAccessUsingPPP)
177         CASE_RETURN_STR(DialupNetworking)
178         CASE_RETURN_STR(IrMCSync)
179         CASE_RETURN_STR(OBEXObjectPush)
180         CASE_RETURN_STR(OBEXFileTransfer)
181         CASE_RETURN_STR(IrMCSyncCommand)
182         CASE_RETURN_STR(Headset)
183         CASE_RETURN_STR(CordlessTelephony)
184         CASE_RETURN_STR(AudioSource)
185         CASE_RETURN_STR(AudioSink)
186         CASE_RETURN_STR(AV_RemoteControlTarget)
187         CASE_RETURN_STR(AdvancedAudioDistribution)
188         CASE_RETURN_STR(AV_RemoteControl)
189         CASE_RETURN_STR(AV_RemoteControlController)
190         CASE_RETURN_STR(Intercom)
191         CASE_RETURN_STR(Fax)
192         CASE_RETURN_STR(Headset_Audio_Gateway)
193         CASE_RETURN_STR(WAP)
194         CASE_RETURN_STR(WAP_CLIENT)
195         CASE_RETURN_STR(PANU)
196         CASE_RETURN_STR(NAP)
197         CASE_RETURN_STR(GN)
198         CASE_RETURN_STR(DirectPrinting)
199         CASE_RETURN_STR(ReferencePrinting)
200         CASE_RETURN_STR(Basic_Imaging_Profile)
201         CASE_RETURN_STR(ImagingResponder)
202         CASE_RETURN_STR(ImagingAutomaticArchive)
203         CASE_RETURN_STR(ImagingReferencedObjects)
204         CASE_RETURN_STR(Handsfree)
205         CASE_RETURN_STR(HandsfreeAudioGateway)
206         CASE_RETURN_STR(DirectPrintingReferenceObjectsService)
207         CASE_RETURN_STR(ReflectedUI)
208         CASE_RETURN_STR(BasicPrinting)
209         CASE_RETURN_STR(PrintingStatus)
210         CASE_RETURN_STR(HumanInterfaceDeviceService)
211         CASE_RETURN_STR(HardcopyCableReplacement)
212         CASE_RETURN_STR(HCR_Print)
213         CASE_RETURN_STR(HCR_Scan)
214         CASE_RETURN_STR(Common_ISDN_Access)
215         CASE_RETURN_STR(SIM_Access)
216         CASE_RETURN_STR(Phonebook_Access_PCE)
217         CASE_RETURN_STR(Phonebook_Access_PSE)
218         CASE_RETURN_STR(Phonebook_Access)
219         CASE_RETURN_STR(Headset_HS)
220         CASE_RETURN_STR(Message_Access_Server)
221         CASE_RETURN_STR(Message_Notification_Server)
222         CASE_RETURN_STR(Message_Access_Profile)
223         CASE_RETURN_STR(GNSS)
224         CASE_RETURN_STR(GNSS_Server)
225         CASE_RETURN_STR(ThreeD_Display)
226         CASE_RETURN_STR(ThreeD_Glasses)
227         CASE_RETURN_STR(ThreeD_Synchronization)
228         CASE_RETURN_STR(MPS_Profile)
229         CASE_RETURN_STR(MPS_SC)
230         CASE_RETURN_STR(CTN_Access_Service)
231         CASE_RETURN_STR(CTN_Notification_Service)
232         CASE_RETURN_STR(CTN_Profile)
233         CASE_RETURN_STR(PnPInformation)
234         CASE_RETURN_STR(GenericNetworking)
235         CASE_RETURN_STR(GenericFileTransfer)
236         CASE_RETURN_STR(GenericAudio)
237         CASE_RETURN_STR(GenericTelephony)
238         CASE_RETURN_STR(UPNP_Service)
239         CASE_RETURN_STR(UPNP_IP_Service)
240         CASE_RETURN_STR(ESDP_UPNP_IP_PAN)
241         CASE_RETURN_STR(ESDP_UPNP_IP_LAP)
242         CASE_RETURN_STR(ESDP_UPNP_L2CAP)
243         CASE_RETURN_STR(VideoSource)
244         CASE_RETURN_STR(VideoSink)
245         CASE_RETURN_STR(VideoDistribution)
246         CASE_RETURN_STR(HDP)
247         CASE_RETURN_STR(HDP_Source)
248         CASE_RETURN_STR(HDP_Sink)
249         CASE_RETURN_STR(Generic_Access)
250         CASE_RETURN_STR(Generic_Attribute)
251         CASE_RETURN_STR(Immediate_Alert)
252         CASE_RETURN_STR(Link_Loss)
253         CASE_RETURN_STR(Tx_Power)
254         CASE_RETURN_STR(Current_Time)
255         CASE_RETURN_STR(Reference_Time_Update)
256         CASE_RETURN_STR(Next_Dst_Change)
257         CASE_RETURN_STR(Glucose)
258         CASE_RETURN_STR(Health_Thermometer)
259         CASE_RETURN_STR(Device_Information)
260         CASE_RETURN_STR(Heart_Rate)
261         CASE_RETURN_STR(Phone_Alert_Status)
262         CASE_RETURN_STR(Battery_Service)
263         CASE_RETURN_STR(Blood_Pressure)
264         CASE_RETURN_STR(Alert_Notification)
265         CASE_RETURN_STR(Human_Interface_Device)
266         CASE_RETURN_STR(Scan_Parameters)
267         CASE_RETURN_STR(Running_Speed_And_Cadence)
268         CASE_RETURN_STR(Automation_IO)
269         CASE_RETURN_STR(Cycling_Speed_And_Cadence)
270         CASE_RETURN_STR(Cycling_Power)
271         CASE_RETURN_STR(Location_And_Navigation)
272         CASE_RETURN_STR(Environmental_Sensing)
273         CASE_RETURN_STR(Body_Composition)
274         CASE_RETURN_STR(User_Data)
275         CASE_RETURN_STR(Weight_Scale)
276         CASE_RETURN_STR(Bond_Management)
277         CASE_RETURN_STR(Continuous_Glucose_Monitoring)
278         CASE_RETURN_STR(Internet_Protocol_Support)
279         CASE_RETURN_STR(Indoor_Positioning)
280         CASE_RETURN_STR(Pulse_Oximeter)
281         CASE_RETURN_STR(Http_Proxy)
282         CASE_RETURN_STR(Transport_Discovery)
283         CASE_RETURN_STR(Object_Transfer)
284         CASE_RETURN_STR(Gap_Device_Name)
285         CASE_RETURN_STR(Gap_Appearance)
286         CASE_RETURN_STR(Gap_Peripheral_Privacy_Flag)
287         CASE_RETURN_STR(Gap_Reconnection_Address)
288         CASE_RETURN_STR(Gap_Peripheral_Preferred_Connection_Parameters)
289         CASE_RETURN_STR(Gatt_Service_Changed)
290         CASE_RETURN_STR(Alert_Level)
291         CASE_RETURN_STR(Tx_Power_Level)
292         CASE_RETURN_STR(Date_Time)
293         CASE_RETURN_STR(Day_Of_Week)
294         CASE_RETURN_STR(Day_Date_Time)
295         CASE_RETURN_STR(Exact_Time_256)
296         CASE_RETURN_STR(Dst_Offset)
297         CASE_RETURN_STR(Time_Zone)
298         CASE_RETURN_STR(Local_Time_Information)
299         CASE_RETURN_STR(Time_With_Dst)
300         CASE_RETURN_STR(Time_Accuracy)
301         CASE_RETURN_STR(Time_Source)
302         CASE_RETURN_STR(Reference_Time_Information)
303         CASE_RETURN_STR(Time_Update_Control_Point)
304         CASE_RETURN_STR(Time_Update_State)
305         CASE_RETURN_STR(Glucose_Measurement)
306         CASE_RETURN_STR(Battery_Level)
307         CASE_RETURN_STR(Temperature_Measurement)
308         CASE_RETURN_STR(Temperature_Type)
309         CASE_RETURN_STR(Intermediate_Temperature)
310         CASE_RETURN_STR(Measurement_Interval)
311         CASE_RETURN_STR(Boot_Keyboard_Input_Report)
312         CASE_RETURN_STR(System_Id)
313         CASE_RETURN_STR(Model_Number_String)
314         CASE_RETURN_STR(Serial_Number_String)
315         CASE_RETURN_STR(Firmware_Revision_String)
316         CASE_RETURN_STR(Hardware_Revision_String)
317         CASE_RETURN_STR(Software_Revision_String)
318         CASE_RETURN_STR(Manufacturer_Name_String)
319         CASE_RETURN_STR(Regulatory_Certification_Data_List)
320         CASE_RETURN_STR(Current_Time_Charac)
321         CASE_RETURN_STR(Magnetic_Declination)
322         CASE_RETURN_STR(Scan_Refresh)
323         CASE_RETURN_STR(Boot_Keyboard_Output_Report)
324         CASE_RETURN_STR(Boot_Mouse_Input_Report)
325         CASE_RETURN_STR(Glucose_Measurement_Context)
326         CASE_RETURN_STR(Blood_Pressure_Measurement)
327         CASE_RETURN_STR(Intermediate_Cuff_Pressure)
328         CASE_RETURN_STR(Heart_Rate_Measurement)
329         CASE_RETURN_STR(Body_Sensor_Location)
330         CASE_RETURN_STR(Heart_Rate_Control_Point)
331         CASE_RETURN_STR(Alert_Status)
332         CASE_RETURN_STR(Ringer_Control_Point)
333         CASE_RETURN_STR(Ringer_Setting)
334         CASE_RETURN_STR(Alert_Category_Id_Bit_Mask)
335         CASE_RETURN_STR(Alert_Category_Id)
336         CASE_RETURN_STR(Alert_Notification_Control_Point)
337         CASE_RETURN_STR(Unread_Alert_Status)
338         CASE_RETURN_STR(New_Alert)
339         CASE_RETURN_STR(Supported_New_Alert_Category)
340         CASE_RETURN_STR(Supported_Unread_Alert_Category)
341         CASE_RETURN_STR(Blood_Pressure_Feature)
342         CASE_RETURN_STR(Hid_Information)
343         CASE_RETURN_STR(Report_Map)
344         CASE_RETURN_STR(Hid_Control_Point)
345         CASE_RETURN_STR(Report)
346         CASE_RETURN_STR(Protocol_Mode)
347         CASE_RETURN_STR(Scan_Interval_Window)
348         CASE_RETURN_STR(Pnp_Id)
349         CASE_RETURN_STR(Glucose_Feature)
350         CASE_RETURN_STR(Record_Access_Control_Point)
351         CASE_RETURN_STR(Rsc_Measurement)
352         CASE_RETURN_STR(Rsc_Feature)
353         CASE_RETURN_STR(Sc_Control_Point)
354         CASE_RETURN_STR(Digital)
355         CASE_RETURN_STR(Analog)
356         CASE_RETURN_STR(Aggregate)
357         CASE_RETURN_STR(Csc_Measurement)
358         CASE_RETURN_STR(Csc_Feature)
359         CASE_RETURN_STR(Sensor_Location)
360         CASE_RETURN_STR(Plx_Spot_Check_Measurement)
361         CASE_RETURN_STR(Plx_Continuous_Measurement)
362         CASE_RETURN_STR(Plx_Features)
363         CASE_RETURN_STR(Cycling_Power_Measurement)
364         CASE_RETURN_STR(Cycling_Power_Vector)
365         CASE_RETURN_STR(Cycling_Power_Feature)
366         CASE_RETURN_STR(Cycling_Power_Control_Point)
367         CASE_RETURN_STR(Location_And_Speed)
368         CASE_RETURN_STR(Navigation)
369         CASE_RETURN_STR(Position_Quality)
370         CASE_RETURN_STR(Ln_Feature)
371         CASE_RETURN_STR(Ln_Control_Point)
372         CASE_RETURN_STR(Elevation)
373         CASE_RETURN_STR(Pressure)
374         CASE_RETURN_STR(Temperature)
375         CASE_RETURN_STR(Humidity)
376         CASE_RETURN_STR(True_Wind_Speed)
377         CASE_RETURN_STR(True_Wind_Direction)
378         CASE_RETURN_STR(Apparent_Wind_Speed)
379         CASE_RETURN_STR(Apparent_Wind_Direction)
380         CASE_RETURN_STR(Gust_Factor)
381         CASE_RETURN_STR(Pollen_Concentration)
382         CASE_RETURN_STR(Uv_Index)
383         CASE_RETURN_STR(Irradiance)
384         CASE_RETURN_STR(Rainfall)
385         CASE_RETURN_STR(Wind_Chill)
386         CASE_RETURN_STR(Heat_Index)
387         CASE_RETURN_STR(Dew_Point)
388         CASE_RETURN_STR(Descriptor_Value_Changed)
389         CASE_RETURN_STR(Aerobic_Threshold)
390         CASE_RETURN_STR(Age)
391         CASE_RETURN_STR(Anaerobic_Heart_Rate_Lower_Limit)
392         CASE_RETURN_STR(Anaerobic_Heart_Rate_Upper_Limit)
393         CASE_RETURN_STR(Anaerobic_Threshold)
394         CASE_RETURN_STR(Aerobic_Heart_Rate_Upper_Limit)
395         CASE_RETURN_STR(Date_Of_Birth)
396         CASE_RETURN_STR(Date_Of_Threshold_Assessment)
397         CASE_RETURN_STR(Email_Address)
398         CASE_RETURN_STR(Fat_Burn_Heart_Rate_Lower_Limit)
399         CASE_RETURN_STR(Fat_Burn_Heart_Rate_Upper_Limit)
400         CASE_RETURN_STR(First_Name)
401         CASE_RETURN_STR(Five_Zone_Heart_Rate_Limits)
402         CASE_RETURN_STR(Gender)
403         CASE_RETURN_STR(Heart_Rate_Max)
404         CASE_RETURN_STR(Height)
405         CASE_RETURN_STR(Hip_Circumference)
406         CASE_RETURN_STR(Last_Name)
407         CASE_RETURN_STR(Maximum_Recommended_Heart_Rate)
408         CASE_RETURN_STR(Resting_Heart_Rate)
409         CASE_RETURN_STR(Sport_Type_For_Aerobic_And_Anaerobic_Thresholds)
410         CASE_RETURN_STR(Three_Zone_Heart_Rate_Limits)
411         CASE_RETURN_STR(Two_Zone_Heart_Rate_Limit)
412         CASE_RETURN_STR(Vo2_Max)
413         CASE_RETURN_STR(Waist_Circumference)
414         CASE_RETURN_STR(Weight)
415         CASE_RETURN_STR(Database_Change_Increment)
416         CASE_RETURN_STR(User_Index)
417         CASE_RETURN_STR(Body_Composition_Feature)
418         CASE_RETURN_STR(Body_Composition_Measurement)
419         CASE_RETURN_STR(Weight_Measurement)
420         CASE_RETURN_STR(Weight_Scale_Feature)
421         CASE_RETURN_STR(User_Control_Point)
422         CASE_RETURN_STR(Magnetic_Flux_Density_2D)
423         CASE_RETURN_STR(Magnetic_Flux_Density_3D)
424         CASE_RETURN_STR(Language)
425         CASE_RETURN_STR(Barometric_Pressure_Trend)
426         CASE_RETURN_STR(Bond_Management_Control_Point)
427         CASE_RETURN_STR(Bond_Management_Feature)
428         CASE_RETURN_STR(Gap_Central_Address_Resolution_Support)
429         CASE_RETURN_STR(Cgm_Measurement)
430         CASE_RETURN_STR(Cgm_Feature)
431         CASE_RETURN_STR(Cgm_Status)
432         CASE_RETURN_STR(Cgm_Session_Start_Time)
433         CASE_RETURN_STR(Cgm_Session_Run_Time)
434         CASE_RETURN_STR(Cgm_Specific_Ops_Control_Point)
435         CASE_RETURN_STR(Indoor_Positioning_Configuration)
436         CASE_RETURN_STR(Latitude)
437         CASE_RETURN_STR(Longitude)
438         CASE_RETURN_STR(Local_North_Coordinate)
439         CASE_RETURN_STR(Local_East_Coordinate)
440         CASE_RETURN_STR(Floor_Number)
441         CASE_RETURN_STR(Altitude)
442         CASE_RETURN_STR(Uncertainty)
443         CASE_RETURN_STR(Location_Name)
444         CASE_RETURN_STR(Uri)
445         CASE_RETURN_STR(Http_Headers)
446         CASE_RETURN_STR(Http_Status_Code)
447         CASE_RETURN_STR(Http_Entity_Body)
448         CASE_RETURN_STR(Http_Control_Point)
449         CASE_RETURN_STR(Https_Security)
450         CASE_RETURN_STR(Tds_Control_Point)
451         CASE_RETURN_STR(Ots_Feature)
452         CASE_RETURN_STR(Object_Name)
453         CASE_RETURN_STR(Object_Type)
454         CASE_RETURN_STR(Object_Size)
455         CASE_RETURN_STR(Object_First_Created)
456         CASE_RETURN_STR(Object_Last_Modified)
457         CASE_RETURN_STR(Object_Id)
458         CASE_RETURN_STR(Object_Properties)
459         CASE_RETURN_STR(Object_Action_Control_Point)
460         CASE_RETURN_STR(Object_List_Control_Point)
461         CASE_RETURN_STR(Object_List_Filter)
462         CASE_RETURN_STR(Object_Changed)
463         CASE_RETURN_STR(Fitness_Machine_Control_Point)
464         CASE_RETURN_STR(Gatt_Characteristic_Extended_Properties)
465         CASE_RETURN_STR(Gatt_Characteristic_User_Description)
466         CASE_RETURN_STR(Gatt_Client_Characteristic_Configuration)
467         CASE_RETURN_STR(Gatt_Server_Characteristic_Configuration)
468         CASE_RETURN_STR(Gatt_Characteristic_Presentation_Format)
469         CASE_RETURN_STR(Gatt_Characteristic_Aggregate_Format)
470         CASE_RETURN_STR(Valid_Range)
471         CASE_RETURN_STR(External_Report_Reference)
472         CASE_RETURN_STR(Report_Reference)
473         CASE_RETURN_STR(Number_Of_Digitals)
474         CASE_RETURN_STR(Value_Trigger_Setting)
475         CASE_RETURN_STR(Es_Configuration)
476         CASE_RETURN_STR(Es_Measurement)
477         CASE_RETURN_STR(Es_Trigger_Setting)
478         CASE_RETURN_STR(Time_Trigger_Setting)
479         CASE_RETURN_STR(Gatt_Primary_Service_Declaration)
480         CASE_RETURN_STR(Gatt_Secondary_Service_Declaration)
481         CASE_RETURN_STR(Gatt_Include_Declaration)
482         CASE_RETURN_STR(Gatt_Characteristic_Declaration)
483         default:
484                         return "--";
485         }
486 }
487
488 const char *enum_defines(void *v, int i)
489 {
490         const struct int2str *m = v;
491
492         return m[i].str != NULL ? m[i].str : NULL;
493 }
494
495 const char *enum_strings(void *v, int i)
496 {
497         const char **m = v;
498
499         return m[i] != NULL ? m[i] : NULL;
500 }
501
502 const char *enum_one_string(void *v, int i)
503 {
504         const char *m = v;
505
506         return (i == 0) && (m[0] != 0) ? m : NULL;
507 }
508
509 const char *bdaddr2str(const bt_bdaddr_t *bd_addr)
510 {
511         static char buf[MAX_ADDR_STR_LEN];
512
513         return bt_bdaddr_t2str(bd_addr, buf);
514 }
515
516 static void bonded_devices2string(char *str, void *prop, int prop_len)
517 {
518         int count = prop_len / sizeof(bt_bdaddr_t);
519         bt_bdaddr_t *addr = prop;
520
521         strncat(str, "{", strlen("{"));
522
523         while (count--) {
524                 strncat(str, bdaddr2str(addr), MAX_ADDR_STR_LEN);
525                 if (count)
526                         strncat(str, ", ", strlen(", "));
527                 addr++;
528         }
529
530         strncat(str, "}", strlen("}"));
531 }
532
533 static void uuids2string(char *str, void *prop, int prop_len)
534 {
535         int count = prop_len / sizeof(bt_uuid_t);
536         bt_uuid_t *uuid = prop;
537
538         strncat(str, "{",  strlen("{"));
539
540         while (count--) {
541                 strncat(str, btuuid2str(uuid->uu), MAX_UUID_STR_LEN);
542                 if (count)
543                         strncat(str, ", ",  strlen(", "));
544                 uuid++;
545         }
546
547         strncat(str, "}", strlen("}"));
548 }
549
550 const char* bt_property_type_t2str(bt_property_type_t prop_type)
551 {
552         switch (prop_type) {
553
554         case BT_PROPERTY_BDNAME:
555                 return "[Bluetooth Name]";
556         case BT_PROPERTY_BDADDR:
557                 return "[Bluetooth Address]";
558         case BT_PROPERTY_UUIDS:
559                 return "[UUIDS]";
560         case BT_PROPERTY_CLASS_OF_DEVICE:
561                 return "[Class of Device]";
562         case BT_PROPERTY_TYPE_OF_DEVICE:
563                 return "[Bluetooth Type of Device]";
564         case BT_PROPERTY_SERVICE_RECORD:
565                 return "[Bluetooth Service record]";
566         case BT_PROPERTY_ADAPTER_SCAN_MODE:
567                 return "[Bluetooth Adapter Scan Mode]";
568         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
569                 return "[Bluetooth Bonded Devices]";
570         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
571                 return "[Bluetooth Adapter Discovery Timeout]";
572         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
573                 return "[Bluetooth Friendly Name]";
574         case BT_PROPERTY_REMOTE_RSSI:
575                 return "[Bluetooth Rmote RSSI]";
576         case BT_PROPERTY_REMOTE_VERSION_INFO:
577                 return "[Bluetooth Version Info]";
578         case BT_PROPERTY_LOCAL_LE_FEATURES:
579                 return "[Bluetooth LE Features]";
580         case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
581                 return "[Bluetooth Remote Device Timestamp]";
582         default:
583                 return "[Default Property]";
584         }
585 }
586
587 const char* bt_device_type_t2str(bt_device_type_t device_type)
588 {
589         switch (device_type) {
590         case BT_DEVICE_DEVTYPE_BREDR:
591                 return "BREDR Device";
592                 break;
593         case BT_DEVICE_DEVTYPE_BLE:
594                 return "BLE Device";
595                 break;
596         case BT_DEVICE_DEVTYPE_DUAL:
597                 return "Dual Device";
598                 break;
599         default:
600                 return "Unknown Device Type";
601         }
602 }
603
604 const char* bt_scan_mode_t2str(bt_scan_mode_t scan_mode)
605 {
606         switch (scan_mode) {
607         case BT_SCAN_MODE_NONE:
608                 return "Non Scannable";
609         case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
610                 return "Connectable And Discoverable";
611         case BT_SCAN_MODE_CONNECTABLE:
612                 return "Connectable";
613         default:
614                 return "Unknown Scan Mode";
615         }
616
617 }
618
619 static void local_le_feat2string(char *str, const bt_local_le_features_t *f, int buf_len)
620 {
621         uint16_t scan_num;
622         int ret;
623
624         ret = snprintf(str, buf_len, "{\n");
625         if (0 > ret) {
626                 ERR("snprintf failed with %d", ret);
627                 return;
628         }
629         str += ret;
630         buf_len -= ret;
631
632         ret = snprintf(str, buf_len, "Privacy supported: %s,\n",
633                         f->local_privacy_enabled ? "TRUE" : "FALSE");
634         if (0 > ret) {
635                 ERR("snprintf failed with %d", ret);
636                 return;
637         }
638         str += ret;
639         buf_len -= ret;
640
641         ret = snprintf(str, buf_len, "Num of advertising instances: %u,\n",
642                         f->max_adv_instance);
643         if (0 > ret) {
644                 ERR("snprintf failed with %d", ret);
645                 return;
646         }
647         str += ret;
648         buf_len -= ret;
649
650         ret = snprintf(str, buf_len, "PRA offloading support: %s,\n",
651                         f->rpa_offload_supported ? "TRUE" : "FALSE");
652         if (0 > ret) {
653                 ERR("snprintf failed with %d", ret);
654                 return;
655         }
656         str += ret;
657         buf_len -= ret;
658
659         ret = snprintf(str, buf_len, "Num of offloaded IRKs: %u,\n",
660                         f->max_irk_list_size);
661         if (0 > ret) {
662                 ERR("snprintf failed with %d", ret);
663                 return;
664         }
665         str += ret;
666         buf_len -= ret;
667
668         ret = snprintf(str, buf_len, "Num of offloaded scan filters: %u,\n",
669                         f->max_adv_filter_supported);
670         if (0 > ret) {
671                 ERR("snprintf failed with %d", ret);
672                 return;
673         }
674         str += ret;
675         buf_len -= ret;
676
677         scan_num = f->scan_result_storage_size;
678
679         ret = snprintf(str, buf_len, "Num of offloaded scan results: %u,\n", scan_num);
680         if (0 > ret) {
681                 ERR("snprintf failed with %d", ret);
682                 return;
683         }
684         str += ret;
685         buf_len -= ret;
686
687         ret = snprintf(str, buf_len, "Activity & energy report support: %s\n",
688                         f->activity_energy_info_supported ? "TRUE" : "FALSE");
689         if (0 > ret) {
690                 ERR("snprintf failed with %d", ret);
691                 return;
692         }
693         str += ret;
694         buf_len -= ret;
695
696         ret = snprintf(str, buf_len, "LE 2M PHY Support: %s\n",
697                         f->le_2m_phy_supported ? "TRUE" : "FALSE");
698         if (0 > ret) {
699                 ERR("snprintf failed with %d", ret);
700                 return;
701         }
702         str += ret;
703         buf_len -= ret;
704
705         ret = snprintf(str, buf_len, "LE CODED PHY Support: %s\n",
706                         f->le_coded_phy_supported ? "TRUE" : "FALSE");
707         if (0 > ret) {
708                 ERR("snprintf failed with %d", ret);
709                 return;
710         }
711         str += ret;
712         buf_len -= ret;
713
714         ret = snprintf(str, buf_len, "}");
715         if (0 > ret) {
716                 ERR("snprintf failed with %d", ret);
717                 return;
718         }
719 }
720
721 const char *btproperty2str(const bt_property_t *property)
722 {
723         bt_service_record_t *rec;
724         static char buf[4096];
725         int buf_len = 4096;
726         int ret;
727         char *p;
728
729         memset(buf, 0x00, 4096);
730         ret = snprintf(buf, buf_len, "type=%s len=%d val=",
731                         bt_property_type_t2str(property->type),
732                         property->len);
733         if (0 > ret) {
734                 ERR("snprintf failed with %d", ret);
735                 goto failed;
736         }
737         p = buf + ret;
738         buf_len -= ret;
739
740         switch (property->type) {
741         case BT_PROPERTY_BDNAME:
742         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
743                 ret = snprintf(p, buf_len, "%s",
744                                 ((bt_bdname_t *) property->val)->name);
745                 if (0 > ret) {
746                         ERR("snprintf failed with %d", ret);
747                         goto failed;
748                 }
749                 buf_len -= ret;
750                 break;
751         case BT_PROPERTY_BDADDR:
752                 ret = snprintf(p, buf_len, "%s", bdaddr2str((bt_bdaddr_t *) property->val));
753                 if (0 > ret) {
754                         ERR("snprintf failed with %d", ret);
755                         goto failed;
756                 }
757                 buf_len -= ret;
758                 break;
759         case BT_PROPERTY_CLASS_OF_DEVICE:
760                 ret = snprintf(p, buf_len, "%06x", *((unsigned int *) property->val));
761                 if (0 > ret) {
762                         ERR("snprintf failed with %d", ret);
763                         goto failed;
764                 }
765                 buf_len -= ret;
766                 break;
767         case BT_PROPERTY_TYPE_OF_DEVICE:
768                 ret = snprintf(p, buf_len, "%s", bt_device_type_t2str(
769                                         *((bt_device_type_t *) property->val)));
770                 if (0 > ret) {
771                         ERR("snprintf failed with %d", ret);
772                         goto failed;
773                 }
774                 buf_len -= ret;
775                 break;
776         case BT_PROPERTY_REMOTE_RSSI:
777                 ret = snprintf(p, buf_len, "%d", *((char *) property->val));
778                 if (0 > ret) {
779                         ERR("snprintf failed with %d", ret);
780                         goto failed;
781                 }
782                 buf_len -= ret;
783                 break;
784         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
785                 ret = snprintf(p, buf_len, "%d", *((unsigned int *) property->val));
786                 if (0 > ret) {
787                         ERR("snprintf failed with %d", ret);
788                         goto failed;
789                 }
790                 buf_len -= ret;
791                 break;
792         case BT_PROPERTY_ADAPTER_SCAN_MODE:
793                 ret = snprintf(p, buf_len, "%s",
794                                 bt_scan_mode_t2str(*((bt_scan_mode_t *) property->val)));
795                 if (0 > ret) {
796                         ERR("snprintf failed with %d", ret);
797                         goto failed;
798                 }
799                 buf_len -= ret;
800                 break;
801         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
802                 bonded_devices2string(p, property->val, property->len);
803                 break;
804         case BT_PROPERTY_UUIDS:
805                 uuids2string(p, property->val, property->len);
806                 break;
807         case BT_PROPERTY_SERVICE_RECORD:
808                 rec = property->val;
809                 ret = snprintf(p, buf_len, "{%s, %d, %s}", btuuid2str(rec->uuid.uu),
810                                 rec->channel, rec->name);
811                 if (0 > ret) {
812                         ERR("snprintf failed with %d", ret);
813                         goto failed;
814                 }
815                 buf_len -= ret;
816                 break;
817         case BT_PROPERTY_LOCAL_LE_FEATURES:
818                 local_le_feat2string(p, property->val, buf_len);
819                 break;
820         case BT_PROPERTY_REMOTE_VERSION_INFO:
821         case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
822         default:
823                 ret = snprintf(p, buf_len, "%p", property->val);
824                 if (0 > ret) {
825                         ERR("snprintf failed with %d", ret);
826                         goto failed;
827                 }
828                 buf_len -= ret;
829                 break;
830         }
831
832 failed:
833         return buf;
834 }
835
836 bt_service_id_t _bt_convert_uuid_string_to_service_id(const char *uuid)
837 {
838         bt_service_id_t service_id = BT_RES_SERVICE_ID;
839
840         DBG("+");
841
842         if (!strcasecmp(uuid, BT_HAL_HFP_AUDIO_GATEWAY_UUID))
843                 service_id = BT_HFP_SERVICE_ID;
844         else if (!strcasecmp(uuid, BT_HAL_HSP_AUDIO_GATEWAY_UUID))
845                 service_id = BT_HSP_SERVICE_ID;
846         else if (!strcasecmp(uuid, BT_HAL_A2DP_SINK_UUID))
847                 service_id = BT_A2DP_SERVICE_ID;
848         else if (!strcasecmp(uuid, BT_HAL_A2DP_PROFILE_UUID))
849                 service_id = BT_A2DP_SERVICE_ID;
850         else if (!strcasecmp(uuid, BT_HAL_A2DP_SOURCE_UUID))
851                 service_id = BT_A2DP_SRC_SERVICE_ID;
852         else if (!strcasecmp(uuid, BT_HAL_AVRCP_TARGET_UUID))
853                 service_id = BT_AVRCP_SERVICE_ID;
854         else if (!strcasecmp(uuid, BT_HAL_AVRCP_REMOTE_UUID))
855                 service_id = BT_AVRCP_CT_SERVICE_ID;
856         else if (!strcasecmp(uuid, BT_HAL_OPP_UUID))
857                 service_id = BT_OPP_SERVICE_ID;
858         else if (!strcasecmp(uuid, BT_HAL_FTP_UUID))
859                 service_id = BT_FTP_SERVICE_ID;
860         else if (!strcasecmp(uuid, BT_HAL_SPP_UUID))
861                 service_id = BT_SPP_SERVICE_ID;
862         else if (!strcasecmp(uuid, BT_HAL_PBAP_UUID))
863                 service_id = BT_PBAP_SERVICE_ID;
864         else if (!strcasecmp(uuid, BT_HAL_MAP_UUID))
865                 service_id = BT_MAP_SERVICE_ID;
866         else if (!strcasecmp(uuid, BT_HAL_NAP_UUID))
867                 service_id = BT_NAP_SERVICE_ID;
868         else if (!strcasecmp(uuid, BT_HAL_GN_UUID))
869                 service_id = BT_GN_SERVICE_ID;
870         else if (!strcasecmp(uuid, BT_HAL_HID_UUID))
871                 service_id = BT_HID_SERVICE_ID;
872         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_OLD))
873                 service_id = BT_SAP_SERVICE_ID;
874         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_NEW))
875                 service_id = BT_SAP_SERVICE_ID;
876         else if (!strcasecmp(uuid, BT_HAL_HSP_HS_UUID))
877                 service_id = BT_HSP_HS_SERVICE_ID;
878         else if (!strcasecmp(uuid, BT_HAL_HFP_HF_UUID))
879                 service_id = BT_HFP_HS_SERVICE_ID;
880 #ifdef TIZEN_BT_HAL
881         else if (!strcasecmp(uuid, BT_HAL_HID_DEVICE_UUID))
882                 service_id = BT_HID_SERVICE_ID;
883         else if (!strcasecmp(uuid, BT_HAL_IOTIVITY_UUID))
884                 service_id = BT_IOTIVITY_SERVICE_ID;
885 #endif
886         else
887                 ERR("Unknwon Service uuid, return BT_RES_SERVICE_ID");
888
889         DBG("service_id = [%d]", service_id);
890         DBG("-");
891         return service_id;
892 }
893
894 char *_bt_convert_service_id_to_uuid_string(bt_service_id_t service_id)
895 {
896         DBG("+");
897
898         switch (service_id) {
899         case BT_HFP_SERVICE_ID:
900                 return g_strdup(BT_HAL_HFP_AUDIO_GATEWAY_UUID);
901         case BT_HSP_SERVICE_ID:
902                 return g_strdup(BT_HAL_HSP_AUDIO_GATEWAY_UUID);
903         case BT_A2DP_SERVICE_ID:
904                 return g_strdup(BT_HAL_A2DP_SINK_UUID);
905         case BT_A2DP_SRC_SERVICE_ID:
906                 return g_strdup(BT_HAL_A2DP_SOURCE_UUID);
907         case BT_AVRCP_SERVICE_ID:
908                 return g_strdup(BT_HAL_AVRCP_TARGET_UUID);
909         case BT_AVRCP_CT_SERVICE_ID:
910                 return g_strdup(BT_HAL_AVRCP_REMOTE_UUID);
911         case BT_OPP_SERVICE_ID:
912                 return g_strdup(BT_HAL_OPP_UUID);
913         case BT_FTP_SERVICE_ID:
914                 return g_strdup(BT_HAL_FTP_UUID);
915         case BT_SPP_SERVICE_ID:
916                 return g_strdup(BT_HAL_SPP_UUID);
917         case BT_PBAP_SERVICE_ID:
918                 return g_strdup(BT_HAL_PBAP_UUID);
919         case BT_MAP_SERVICE_ID:
920                 return g_strdup(BT_HAL_MAP_UUID);
921         case BT_NAP_SERVICE_ID:
922                 return g_strdup(BT_HAL_NAP_UUID);
923         case BT_GN_SERVICE_ID:
924                 return g_strdup(BT_HAL_GN_UUID);
925         case BT_HID_SERVICE_ID:
926                 return g_strdup(BT_HAL_HID_UUID);
927         case BT_SAP_SERVICE_ID:
928                 return g_strdup(BT_HAL_SAP_UUID_NEW);
929         /* TODO: Add UUID for following service_ids */
930         case BT_DUN_SERVICE_ID:
931         case BT_LAP_SERVICE_ID:
932         case BT_ICP_SERVICE_ID:
933         case BT_SYNC_SERVICE_ID:
934         case BT_BPP_SERVICE_ID:
935         case BT_BIP_SERVICE_ID:
936         case BT_PANU_SERVICE_ID:
937         case BT_VDP_SERVICE_ID:
938         case BT_HSP_HS_SERVICE_ID:
939                 return g_strdup(BT_HAL_HSP_HS_UUID);
940         case BT_HFP_HS_SERVICE_ID:
941                 return g_strdup(BT_HAL_HFP_HF_UUID);
942         case BT_MN_SERVICE_ID:
943         case BT_HDP_SERVICE_ID:
944         case BT_PCE_SERVICE_ID:
945 #ifdef TIZEN_BT_HAL
946         case BT_IOTIVITY_SERVICE_ID:
947                 return g_strdup(BT_HAL_IOTIVITY_UUID);
948 #endif
949         default:
950                 ERR("Unknwon Service id: %d, return NULL", service_id);
951         }
952
953         DBG("-");
954         return NULL;
955 }