Merge tizen_next codes into tizen branch
[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 strcpy(buf, "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                 sprintf(buf + 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 strcpy(buf, "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         strcat(str, "{");
523
524         while (count--) {
525                 strcat(str, bdaddr2str(addr));
526                 if (count)
527                         strcat(str, ", ");
528                 addr++;
529         }
530
531         strcat(str, "}");
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         strcat(str, "{");
540
541         while (count--) {
542                 strcat(str, btuuid2str(uuid->uu));
543                 if (count)
544                         strcat(str, ", ");
545                 uuid++;
546         }
547
548         strcat(str, "}");
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)
621 {
622         uint16_t scan_num;
623
624         str += sprintf(str, "{\n");
625
626         str += sprintf(str, "Privacy supported: %s,\n",
627                         f->local_privacy_enabled ? "TRUE" : "FALSE");
628
629         str += sprintf(str, "Num of advertising instances: %u,\n",
630                         f->max_adv_instance);
631
632         str += sprintf(str, "PRA offloading support: %s,\n",
633                         f->rpa_offload_supported ? "TRUE" : "FALSE");
634
635         str += sprintf(str, "Num of offloaded IRKs: %u,\n",
636                         f->max_irk_list_size);
637
638         str += sprintf(str, "Num of offloaded scan filters: %u,\n",
639                         f->max_adv_filter_supported);
640
641         scan_num = (f->scan_result_storage_size_hibyte << 8) +
642                 f->scan_result_storage_size_lobyte;
643
644         str += sprintf(str, "Num of offloaded scan results: %u,\n", scan_num);
645
646         str += sprintf(str, "Activity & energy report support: %s\n",
647                         f->activity_energy_info_supported ? "TRUE" : "FALSE");
648
649         sprintf(str, "}");
650 }
651
652 const char *btproperty2str(const bt_property_t *property)
653 {
654         bt_service_record_t *rec;
655         static char buf[4096];
656         char *p;
657
658         p = buf + sprintf(buf, "type=%s len=%d val=",
659                         bt_property_type_t2str(property->type),
660                         property->len);
661
662         switch (property->type) {
663         case BT_PROPERTY_BDNAME:
664         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
665                 snprintf(p, property->len + 1, "%s",
666                                 ((bt_bdname_t *) property->val)->name);
667                 break;
668         case BT_PROPERTY_BDADDR:
669                 sprintf(p, "%s", bdaddr2str((bt_bdaddr_t *) property->val));
670                 break;
671         case BT_PROPERTY_CLASS_OF_DEVICE:
672                 sprintf(p, "%06x", *((unsigned int *) property->val));
673                 break;
674         case BT_PROPERTY_TYPE_OF_DEVICE:
675                 sprintf(p, "%s", bt_device_type_t2str(
676                                         *((bt_device_type_t *) property->val)));
677                 break;
678         case BT_PROPERTY_REMOTE_RSSI:
679                 sprintf(p, "%d", *((char *) property->val));
680                 break;
681         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
682                 sprintf(p, "%d", *((unsigned int *) property->val));
683                 break;
684         case BT_PROPERTY_ADAPTER_SCAN_MODE:
685                 sprintf(p, "%s",
686                                 bt_scan_mode_t2str(*((bt_scan_mode_t *) property->val)));
687                 break;
688         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
689                 bonded_devices2string(p, property->val, property->len);
690                 break;
691         case BT_PROPERTY_UUIDS:
692                 uuids2string(p, property->val, property->len);
693                 break;
694         case BT_PROPERTY_SERVICE_RECORD:
695                 rec = property->val;
696                 sprintf(p, "{%s, %d, %s}", btuuid2str(rec->uuid.uu),
697                                 rec->channel, rec->name);
698                 break;
699         case BT_PROPERTY_LOCAL_LE_FEATURES:
700                 local_le_feat2string(p, property->val);
701                 break;
702         case BT_PROPERTY_REMOTE_VERSION_INFO:
703         case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
704         default:
705                 sprintf(p, "%p", property->val);
706                 break;
707         }
708
709         return buf;
710 }
711
712 bt_service_id_t _bt_convert_uuid_string_to_service_id(const char *uuid)
713 {
714         bt_service_id_t service_id = BT_RES_SERVICE_ID;
715
716         DBG("+");
717
718         if (!strcasecmp(uuid, BT_HAL_HFP_AUDIO_GATEWAY_UUID))
719                 service_id = BT_HFP_SERVICE_ID;
720         else if (!strcasecmp(uuid, BT_HAL_HSP_AUDIO_GATEWAY_UUID))
721                 service_id = BT_HSP_SERVICE_ID;
722         else if (!strcasecmp(uuid, BT_HAL_A2DP_SINK_UUID))
723                 service_id = BT_A2DP_SERVICE_ID;
724         else if (!strcasecmp(uuid, BT_HAL_A2DP_PROFILE_UUID))
725                 service_id = BT_A2DP_SERVICE_ID;
726         else if (!strcasecmp(uuid, BT_HAL_A2DP_SOURCE_UUID))
727                 service_id = BT_A2DP_SRC_SERVICE_ID;
728         else if (!strcasecmp(uuid, BT_HAL_AVRCP_TARGET_UUID))
729                 service_id = BT_AVRCP_SERVICE_ID;
730         else if (!strcasecmp(uuid, BT_HAL_AVRCP_REMOTE_UUID))
731                 service_id = BT_AVRCP_CT_SERVICE_ID;
732         else if (!strcasecmp(uuid, BT_HAL_OPP_UUID))
733                 service_id = BT_OPP_SERVICE_ID;
734         else if (!strcasecmp(uuid, BT_HAL_FTP_UUID))
735                 service_id = BT_FTP_SERVICE_ID;
736         else if (!strcasecmp(uuid, BT_HAL_SPP_UUID))
737                 service_id = BT_SPP_SERVICE_ID;
738         else if (!strcasecmp(uuid, BT_HAL_PBAP_UUID))
739                 service_id = BT_PBAP_SERVICE_ID;
740         else if (!strcasecmp(uuid, BT_HAL_MAP_UUID))
741                 service_id = BT_MAP_SERVICE_ID;
742         else if (!strcasecmp(uuid, BT_HAL_NAP_UUID))
743                 service_id = BT_NAP_SERVICE_ID;
744         else if (!strcasecmp(uuid, BT_HAL_GN_UUID))
745                 service_id = BT_GN_SERVICE_ID;
746         else if (!strcasecmp(uuid, BT_HAL_HID_UUID))
747                 service_id = BT_HID_SERVICE_ID;
748         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_OLD))
749                 service_id = BT_SAP_SERVICE_ID;
750         else if (!strcasecmp(uuid, BT_HAL_SAP_UUID_NEW))
751                 service_id = BT_SAP_SERVICE_ID;
752         else if (!strcasecmp(uuid, BT_HAL_HSP_HS_UUID))
753                 service_id = BT_HSP_HS_SERVICE_ID;
754         else if (!strcasecmp(uuid, BT_HAL_HFP_HF_UUID))
755                 service_id = BT_HFP_HS_SERVICE_ID;
756 #ifdef TIZEN_BT_HAL
757         else if (!strcasecmp(uuid, BT_HAL_HID_DEVICE_UUID))
758                 service_id = BT_HID_SERVICE_ID;
759         else if (!strcasecmp(uuid, BT_HAL_IOTIVITY_UUID))
760                 service_id = BT_IOTIVITY_SERVICE_ID;
761 #endif
762         else
763                 ERR("Unknwon Service uuid, return BT_RES_SERVICE_ID");
764
765         DBG("service_id = [%d]", service_id);
766         DBG("-");
767         return service_id;
768 }
769
770 char *_bt_convert_service_id_to_uuid_string(bt_service_id_t service_id)
771 {
772         DBG("+");
773
774         switch (service_id) {
775         case BT_HFP_SERVICE_ID:
776                 return g_strdup(BT_HAL_HFP_AUDIO_GATEWAY_UUID);
777         case BT_HSP_SERVICE_ID:
778                 return g_strdup(BT_HAL_HSP_AUDIO_GATEWAY_UUID);
779         case BT_A2DP_SERVICE_ID:
780                 return g_strdup(BT_HAL_A2DP_SINK_UUID);
781         case BT_A2DP_SRC_SERVICE_ID:
782                 return g_strdup(BT_HAL_A2DP_SOURCE_UUID);
783         case BT_AVRCP_SERVICE_ID:
784                 return g_strdup(BT_HAL_AVRCP_TARGET_UUID);
785         case BT_AVRCP_CT_SERVICE_ID:
786                 return g_strdup(BT_HAL_AVRCP_REMOTE_UUID);
787         case BT_OPP_SERVICE_ID:
788                 return g_strdup(BT_HAL_OPP_UUID);
789         case BT_FTP_SERVICE_ID:
790                 return g_strdup(BT_HAL_FTP_UUID);
791         case BT_SPP_SERVICE_ID:
792                 return g_strdup(BT_HAL_SPP_UUID);
793         case BT_PBAP_SERVICE_ID:
794                 return g_strdup(BT_HAL_PBAP_UUID);
795         case BT_MAP_SERVICE_ID:
796                 return g_strdup(BT_HAL_MAP_UUID);
797         case BT_NAP_SERVICE_ID:
798                 return g_strdup(BT_HAL_NAP_UUID);
799         case BT_GN_SERVICE_ID:
800                 return g_strdup(BT_HAL_GN_UUID);
801         case BT_HID_SERVICE_ID:
802                 return g_strdup(BT_HAL_HID_UUID);
803         case BT_SAP_SERVICE_ID:
804                 return g_strdup(BT_HAL_SAP_UUID_NEW);
805         /* TODO: Add UUID for following service_ids */
806         case BT_DUN_SERVICE_ID:
807         case BT_LAP_SERVICE_ID:
808         case BT_ICP_SERVICE_ID:
809         case BT_SYNC_SERVICE_ID:
810         case BT_BPP_SERVICE_ID:
811         case BT_BIP_SERVICE_ID:
812         case BT_PANU_SERVICE_ID:
813         case BT_VDP_SERVICE_ID:
814         case BT_HSP_HS_SERVICE_ID:
815                 return g_strdup(BT_HAL_HSP_HS_UUID);
816         case BT_HFP_HS_SERVICE_ID:
817                 return g_strdup(BT_HAL_HFP_HF_UUID);
818         case BT_MN_SERVICE_ID:
819         case BT_HDP_SERVICE_ID:
820         case BT_PCE_SERVICE_ID:
821 #ifdef TIZEN_BT_HAL
822         case BT_IOTIVITY_SERVICE_ID:
823                 return g_strdup(BT_HAL_IOTIVITY_UUID);
824 #endif
825         default:
826                 ERR("Unknwon Service id: %d, return NULL");
827         }
828
829         DBG("-");
830         return NULL;
831 }