Fix SVACE issues in Tizen New BT Architecture
[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"));
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"));
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     {
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, "{", strlen("{"));
523
524         while (count--) {
525                 strncat(str, bdaddr2str(addr), MAX_ADDR_STR_LEN);
526                 if (count)
527                         strncat(str, ", ", strlen(", "));
528                 addr++;
529         }
530
531         strncat(str, "}", strlen("}"));
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, "{",  strlen("{"));
540
541         while (count--) {
542                 strncat(str, btuuid2str(uuid->uu), MAX_UUID_STR_LEN);
543                 if (count)
544                         strncat(str, ", ",  strlen(", "));
545                 uuid++;
546         }
547
548         strncat(str, "}", strlen("}"));
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_hibyte << 8) +
679                 f->scan_result_storage_size_lobyte;
680
681         ret = snprintf(str, buf_len, "Num of offloaded scan results: %u,\n", scan_num);
682         if (0 > ret) {
683                 ERR("snprintf failed with %d", ret);
684                 return;
685         }
686         str += ret;
687         buf_len -= ret;
688
689         ret = snprintf(str, buf_len, "Activity & energy report support: %s\n",
690                         f->activity_energy_info_supported ? "TRUE" : "FALSE");
691         if (0 > ret) {
692                 ERR("snprintf failed with %d", ret);
693                 return;
694         }
695         str += ret;
696         buf_len -= ret;
697
698         ret = snprintf(str, buf_len, "}");
699         if (0 > ret) {
700                 ERR("snprintf failed with %d", ret);
701                 return;
702         }
703 }
704
705 const char *btproperty2str(const bt_property_t *property)
706 {
707         bt_service_record_t *rec;
708         static char buf[4096];
709         int buf_len = 4096;
710         int ret;
711         char *p;
712
713         memset(buf, 0x00, 4096);
714         ret = snprintf(buf, buf_len, "type=%s len=%d val=",
715                         bt_property_type_t2str(property->type),
716                         property->len);
717         if (0 > ret) {
718                 ERR("snprintf failed with %d", ret);
719                 goto failed;
720         }
721         p = buf + ret;
722         buf_len -= ret;
723
724         switch (property->type) {
725         case BT_PROPERTY_BDNAME:
726         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
727                 ret = snprintf(p, buf_len, "%s",
728                                 ((bt_bdname_t *) property->val)->name);
729                 if (0 > ret) {
730                         ERR("snprintf failed with %d", ret);
731                         goto failed;
732                 }
733                 buf_len -= ret;
734                 break;
735         case BT_PROPERTY_BDADDR:
736                 ret = snprintf(p, buf_len, "%s", bdaddr2str((bt_bdaddr_t *) property->val));
737                 if (0 > ret) {
738                         ERR("snprintf failed with %d", ret);
739                         goto failed;
740                 }
741                 buf_len -= ret;
742                 break;
743         case BT_PROPERTY_CLASS_OF_DEVICE:
744                 ret = snprintf(p, buf_len, "%06x", *((unsigned int *) property->val));
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_TYPE_OF_DEVICE:
752                 ret = snprintf(p, buf_len, "%s", bt_device_type_t2str(
753                                         *((bt_device_type_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_REMOTE_RSSI:
761                 ret = snprintf(p, buf_len, "%d", *((char *) 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_ADAPTER_DISCOVERY_TIMEOUT:
769                 ret = snprintf(p, buf_len, "%d", *((unsigned int *) 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_ADAPTER_SCAN_MODE:
777                 ret = snprintf(p, buf_len, "%s",
778                                 bt_scan_mode_t2str(*((bt_scan_mode_t *) 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_BONDED_DEVICES:
786                 bonded_devices2string(p, property->val, property->len);
787                 break;
788         case BT_PROPERTY_UUIDS:
789                 uuids2string(p, property->val, property->len);
790                 break;
791         case BT_PROPERTY_SERVICE_RECORD:
792                 rec = property->val;
793                 ret = snprintf(p, "{%s, %d, %s}", btuuid2str(rec->uuid.uu),
794                                 rec->channel, rec->name);
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_LOCAL_LE_FEATURES:
802                 local_le_feat2string(p, property->val, buf_len);
803                 break;
804         case BT_PROPERTY_REMOTE_VERSION_INFO:
805         case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
806         default:
807                 ret = snprintf(p, buf_len, "%p", property->val);
808                 if (0 > ret) {
809                         ERR("snprintf failed with %d", ret);
810                         goto failed;
811                 }
812                 buf_len -= ret;
813                 break;
814         }
815
816 failed:
817         return buf;
818 }
819
820 bt_service_id_t _bt_convert_uuid_string_to_service_id(const char *uuid)
821 {
822         bt_service_id_t service_id = BT_RES_SERVICE_ID;
823
824         DBG("+");
825
826         if (!strcasecmp(uuid, BT_HAL_HFP_AUDIO_GATEWAY_UUID))
827                 service_id = BT_HFP_SERVICE_ID;
828         else if (!strcasecmp(uuid, BT_HAL_HSP_AUDIO_GATEWAY_UUID))
829                 service_id = BT_HSP_SERVICE_ID;
830         else if (!strcasecmp(uuid, BT_HAL_A2DP_SINK_UUID))
831                 service_id = BT_A2DP_SERVICE_ID;
832         else if (!strcasecmp(uuid, BT_HAL_A2DP_PROFILE_UUID))
833                 service_id = BT_A2DP_SERVICE_ID;
834         else if (!strcasecmp(uuid, BT_HAL_A2DP_SOURCE_UUID))
835                 service_id = BT_A2DP_SRC_SERVICE_ID;
836         else if (!strcasecmp(uuid, BT_HAL_AVRCP_TARGET_UUID))
837                 service_id = BT_AVRCP_SERVICE_ID;
838         else if (!strcasecmp(uuid, BT_HAL_AVRCP_REMOTE_UUID))
839                 service_id = BT_AVRCP_CT_SERVICE_ID;
840         else if (!strcasecmp(uuid, BT_HAL_OPP_UUID))
841                 service_id = BT_OPP_SERVICE_ID;
842         else if (!strcasecmp(uuid, BT_HAL_FTP_UUID))
843                 service_id = BT_FTP_SERVICE_ID;
844         else if (!strcasecmp(uuid, BT_HAL_SPP_UUID))
845                 service_id = BT_SPP_SERVICE_ID;
846         else if (!strcasecmp(uuid, BT_HAL_PBAP_UUID))
847                 service_id = BT_PBAP_SERVICE_ID;
848         else if (!strcasecmp(uuid, BT_HAL_MAP_UUID))
849                 service_id = BT_MAP_SERVICE_ID;
850         else if (!strcasecmp(uuid, BT_HAL_NAP_UUID))
851                 service_id = BT_NAP_SERVICE_ID;
852         else if (!strcasecmp(uuid, BT_HAL_GN_UUID))
853                 service_id = BT_GN_SERVICE_ID;
854         else if (!strcasecmp(uuid, BT_HAL_HID_UUID))
855                 service_id = BT_HID_SERVICE_ID;
856         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_OLD))
857                 service_id = BT_SAP_SERVICE_ID;
858         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_NEW))
859                 service_id = BT_SAP_SERVICE_ID;
860         else if (!strcasecmp(uuid, BT_HAL_HSP_HS_UUID))
861                 service_id = BT_HSP_HS_SERVICE_ID;
862         else if (!strcasecmp(uuid, BT_HAL_HFP_HF_UUID))
863                 service_id = BT_HFP_HS_SERVICE_ID;
864 #ifdef TIZEN_BT_HAL
865         else if (!strcasecmp(uuid, BT_HAL_HID_DEVICE_UUID))
866                 service_id = BT_HID_SERVICE_ID;
867         else if (!strcasecmp(uuid, BT_HAL_IOTIVITY_UUID))
868                 service_id = BT_IOTIVITY_SERVICE_ID;
869 #endif
870         else
871                 ERR("Unknwon Service uuid, return BT_RES_SERVICE_ID");
872
873         DBG("service_id = [%d]", service_id);
874         DBG("-");
875         return service_id;
876 }
877
878 char *_bt_convert_service_id_to_uuid_string(bt_service_id_t service_id)
879 {
880         DBG("+");
881
882         switch (service_id) {
883         case BT_HFP_SERVICE_ID:
884                 return g_strdup(BT_HAL_HFP_AUDIO_GATEWAY_UUID);
885         case BT_HSP_SERVICE_ID:
886                 return g_strdup(BT_HAL_HSP_AUDIO_GATEWAY_UUID);
887         case BT_A2DP_SERVICE_ID:
888                 return g_strdup(BT_HAL_A2DP_SINK_UUID);
889         case BT_A2DP_SRC_SERVICE_ID:
890                 return g_strdup(BT_HAL_A2DP_SOURCE_UUID);
891         case BT_AVRCP_SERVICE_ID:
892                 return g_strdup(BT_HAL_AVRCP_TARGET_UUID);
893         case BT_AVRCP_CT_SERVICE_ID:
894                 return g_strdup(BT_HAL_AVRCP_REMOTE_UUID);
895         case BT_OPP_SERVICE_ID:
896                 return g_strdup(BT_HAL_OPP_UUID);
897         case BT_FTP_SERVICE_ID:
898                 return g_strdup(BT_HAL_FTP_UUID);
899         case BT_SPP_SERVICE_ID:
900                 return g_strdup(BT_HAL_SPP_UUID);
901         case BT_PBAP_SERVICE_ID:
902                 return g_strdup(BT_HAL_PBAP_UUID);
903         case BT_MAP_SERVICE_ID:
904                 return g_strdup(BT_HAL_MAP_UUID);
905         case BT_NAP_SERVICE_ID:
906                 return g_strdup(BT_HAL_NAP_UUID);
907         case BT_GN_SERVICE_ID:
908                 return g_strdup(BT_HAL_GN_UUID);
909         case BT_HID_SERVICE_ID:
910                 return g_strdup(BT_HAL_HID_UUID);
911         case BT_SAP_SERVICE_ID:
912                 return g_strdup(BT_HAL_SAP_UUID_NEW);
913         /* TODO: Add UUID for following service_ids */
914         case BT_DUN_SERVICE_ID:
915         case BT_LAP_SERVICE_ID:
916         case BT_ICP_SERVICE_ID:
917         case BT_SYNC_SERVICE_ID:
918         case BT_BPP_SERVICE_ID:
919         case BT_BIP_SERVICE_ID:
920         case BT_PANU_SERVICE_ID:
921         case BT_VDP_SERVICE_ID:
922         case BT_HSP_HS_SERVICE_ID:
923                 return g_strdup(BT_HAL_HSP_HS_UUID);
924         case BT_HFP_HS_SERVICE_ID:
925                 return g_strdup(BT_HAL_HFP_HF_UUID);
926         case BT_MN_SERVICE_ID:
927         case BT_HDP_SERVICE_ID:
928         case BT_PCE_SERVICE_ID:
929 #ifdef TIZEN_BT_HAL
930         case BT_IOTIVITY_SERVICE_ID:
931                 return g_strdup(BT_HAL_IOTIVITY_UUID);
932 #endif
933         default:
934                 ERR("Unknwon Service id: %d, return NULL");
935         }
936
937         DBG("-");
938         return NULL;
939 }