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