Change the name of dbus property of EIR manufacturer data
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 <glib.h>
21 #include <dlog.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <termios.h>
25 #include <net_connection.h>
26 #include <dbus/dbus.h>
27 #include <glib.h>
28 #include <dlog.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <termios.h>
32 #include <net_connection.h>
33 #include <bundle.h>
34 #include <eventsystem.h>
35
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
39
40 static GDBusConnection *system_conn;
41 static GDBusConnection *session_conn;
42 static GDBusProxy *manager_proxy;
43 static GDBusProxy *adapter_proxy;
44 static void *net_conn;
45
46 static GDBusProxy *adapter_properties_proxy;
47
48 GDBusConnection *_bt_gdbus_init_system_gconn(void)
49 {
50         GError *error = NULL;
51
52         dbus_threads_init_default();
53
54         if (system_conn != NULL)
55                 return system_conn;
56
57         system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
58
59         if (!system_conn) {
60                 BT_ERR("Unable to connect to dbus: %s", error->message);
61                 g_clear_error(&error);
62         }
63
64         return system_conn;
65 }
66
67 GDBusConnection *_bt_gdbus_get_system_gconn(void)
68 {
69         GDBusConnection *local_system_gconn = NULL;
70         GError *error = NULL;
71
72         if (system_conn == NULL) {
73                 system_conn = _bt_gdbus_init_system_gconn();
74         } else if (g_dbus_connection_is_closed(system_conn)) {
75                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
76
77                 if (!local_system_gconn) {
78                         BT_ERR("Unable to connect to dbus: %s", error->message);
79                         g_clear_error(&error);
80                 }
81
82                 system_conn = local_system_gconn;
83         }
84
85         return system_conn;
86 }
87
88 GDBusConnection *_bt_gdbus_init_session_gconn(void)
89 {
90         GError *error = NULL;
91
92         if (!g_thread_supported())
93                 g_thread_init(NULL);
94
95         dbus_threads_init_default();
96
97         if (session_conn != NULL)
98                 return session_conn;
99
100         session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
101
102         if (!session_conn) {
103                 BT_ERR("Unable to connect to dbus: %s", error->message);
104                 g_clear_error(&error);
105         }
106
107         return session_conn;
108 }
109
110 GDBusConnection *_bt_gdbus_get_session_gconn(void)
111 {
112         GDBusConnection *local_session_gconn = NULL;
113         GError *error = NULL;
114
115         if (session_conn == NULL) {
116                 session_conn = _bt_gdbus_init_session_gconn();
117         } else if (g_dbus_connection_is_closed(session_conn)) {
118                 local_session_gconn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
119
120                 if (!local_session_gconn) {
121                         BT_ERR("Unable to connect to dbus: %s", error->message);
122                         g_clear_error(&error);
123                 }
124
125                 session_conn = local_session_gconn;
126         }
127
128         return session_conn;
129 }
130
131 static GDBusProxy *__bt_init_manager_proxy(void)
132 {
133         GDBusConnection *g_conn;
134         GDBusProxy *proxy;
135
136         dbus_threads_init_default();
137
138         g_conn = _bt_gdbus_get_system_gconn();
139         retv_if(g_conn == NULL, NULL);
140
141         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
142                                                                 NULL, BT_BLUEZ_NAME,
143                                                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE,  NULL, NULL);
144
145         if (!proxy) {
146                 BT_ERR("Unable to get proxy");
147                 return NULL;
148         }
149
150         manager_proxy = proxy;
151
152         return proxy;
153 }
154
155 static GDBusProxy *__bt_init_adapter_proxy(void)
156 {
157         GDBusConnection *g_conn;
158         GDBusProxy *manager_proxy;
159         GDBusProxy *proxy;
160         char *adapter_path = NULL;
161
162         dbus_threads_init_default();
163
164         g_conn = _bt_gdbus_get_system_gconn();
165         retv_if(g_conn == NULL, NULL);
166
167         manager_proxy = _bt_get_manager_proxy();
168         retv_if(manager_proxy == NULL, NULL);
169
170         adapter_path = _bt_get_adapter_path();
171         retv_if(adapter_path == NULL, NULL);
172
173         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
174                                                                 NULL, BT_BLUEZ_NAME,
175                                                                 adapter_path, BT_ADAPTER_INTERFACE,  NULL, NULL);
176
177         g_free(adapter_path);
178
179         retv_if(proxy == NULL, NULL);
180
181         adapter_proxy = proxy;
182
183         return proxy;
184 }
185
186 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
187 {
188         GDBusConnection *g_conn;
189         GDBusProxy *manager_proxy;
190         GDBusProxy *proxy;
191         char *adapter_path = NULL;
192
193         dbus_threads_init_default();
194
195         g_conn = _bt_gdbus_get_system_gconn();
196         retv_if(g_conn == NULL, NULL);
197
198         manager_proxy = _bt_get_manager_proxy();
199         retv_if(manager_proxy == NULL, NULL);
200
201         adapter_path = _bt_get_adapter_path();
202         retv_if(adapter_path == NULL, NULL);
203
204         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
205                                                                         NULL, BT_BLUEZ_NAME,
206                                                                         adapter_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
207
208         g_free(adapter_path);
209
210         retv_if(proxy == NULL, NULL);
211
212         adapter_properties_proxy = proxy;
213
214         return proxy;
215 }
216
217 GDBusProxy *_bt_get_manager_proxy(void)
218 {
219         if (manager_proxy) {
220                 const gchar *path =  g_dbus_proxy_get_object_path(manager_proxy);
221                 if (path == NULL) {
222                         BT_ERR("Already proxy released hence creating new proxy");
223                         return  __bt_init_manager_proxy();
224                 }
225                 return manager_proxy;
226         }
227         return  __bt_init_manager_proxy();
228 }
229
230 static void *__bt_init_net_conn(void)
231 {
232         int result;
233         connection_h connection = NULL;
234
235         if (net_conn == NULL) {
236                 result = connection_create(&connection);
237
238         if (result != CONNECTION_ERROR_NONE ||
239                                         connection == NULL) {
240                 BT_DBG("connection_create() failed: %d", result);
241                 net_conn = NULL;
242                 return NULL;
243         }
244                 net_conn = connection;
245         }
246         return net_conn;
247 }
248
249 void *_bt_get_net_conn(void)
250 {
251         return (net_conn) ? net_conn : __bt_init_net_conn();
252 }
253
254 GDBusProxy *_bt_get_adapter_proxy(void)
255 {
256         if (adapter_proxy) {
257                 const char *path =  g_dbus_proxy_get_object_path(adapter_proxy);
258                 if (path == NULL) {
259                         BT_ERR("Already proxy released hence creating new proxy");
260                         return  __bt_init_adapter_proxy();
261                 }
262
263                 return adapter_proxy;
264         }
265         return  __bt_init_adapter_proxy();
266
267 }
268
269 GDBusProxy *_bt_get_adapter_properties_proxy(void)
270 {
271         return (adapter_properties_proxy) ? adapter_properties_proxy :
272                                         __bt_init_adapter_properties_proxy();
273 }
274
275 static char *__bt_extract_adapter_path(GVariantIter *iter)
276 {
277         char *object_path = NULL;
278         GVariantIter *interface_iter;
279         GVariantIter *svc_iter;
280         char *interface_str = NULL;
281
282         /* Parse the signature: oa{sa{sv}}} */
283         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
284                         &interface_iter)) {
285
286                 if (object_path == NULL)
287                         continue;
288
289                 while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
290                                 &interface_str, &svc_iter)) {
291                         if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
292                                 continue;
293
294                         BT_DBG("Object Path: %s", object_path);
295                         g_variant_iter_free(svc_iter);
296                         g_variant_iter_free(interface_iter);
297                         return g_strdup(object_path);
298                 }
299         }
300         return NULL;
301 }
302
303 char *_bt_get_adapter_path(void)
304 {
305         GDBusConnection *conn;
306         GDBusProxy *manager_proxy;
307         GVariant *result = NULL;
308         GVariantIter *iter = NULL;
309         char *adapter_path = NULL;
310
311         conn = _bt_gdbus_get_system_gconn();
312         retv_if(conn == NULL, NULL);
313
314         manager_proxy = _bt_get_manager_proxy();
315         retv_if(manager_proxy == NULL, NULL);
316
317         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
318                                 NULL,
319                                 G_DBUS_CALL_FLAGS_NONE,
320                                 -1,
321                                 NULL,
322                                 NULL);
323         if (!result) {
324                 BT_ERR("Can't get managed objects");
325                 return NULL;
326         }
327
328         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
329         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
330
331         adapter_path = __bt_extract_adapter_path(iter);
332         g_variant_iter_free(iter);
333         g_variant_unref(result);
334         return adapter_path;
335 }
336
337 void _bt_deinit_bluez_proxy(void)
338 {
339         if (manager_proxy) {
340                 g_object_unref(manager_proxy);
341                 manager_proxy = NULL;
342         }
343
344         if (adapter_proxy) {
345                 g_object_unref(adapter_proxy);
346                 adapter_proxy = NULL;
347         }
348         if (adapter_properties_proxy) {
349                 g_object_unref(adapter_properties_proxy);
350                 adapter_properties_proxy = NULL;
351         }
352 }
353
354 void _bt_deinit_proxys(void)
355 {
356         int ret;
357         _bt_deinit_bluez_proxy();
358
359         if (system_conn) {
360                 g_object_unref(system_conn);
361                 system_conn = NULL;
362         }
363
364         if (session_conn) {
365                 g_object_unref(session_conn);
366                 session_conn = NULL;
367         }
368
369         if (net_conn) {
370                 ret = connection_destroy(net_conn);
371                 net_conn = NULL;
372                 if (ret != 0)
373                         BT_ERR("connection_destroy failed : %d", ret);
374         }
375 }
376
377 void _bt_convert_device_path_to_address(const char *device_path,
378                                                 char *device_address)
379 {
380         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
381         char *dev_addr;
382
383         ret_if(device_path == NULL);
384         ret_if(device_address == NULL);
385
386         dev_addr = strstr(device_path, "dev_");
387         if (dev_addr != NULL) {
388                 char *pos = NULL;
389                 dev_addr += 4;
390                 g_strlcpy(address, dev_addr, sizeof(address));
391
392                 while ((pos = strchr(address, '_')) != NULL)
393                         *pos = ':';
394
395                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
396         }
397 }
398
399
400 void _bt_convert_addr_string_to_type(unsigned char *addr,
401                                         const char *address)
402 {
403         int i;
404         char *ptr = NULL;
405
406         ret_if(address == NULL);
407         ret_if(addr == NULL);
408
409         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
410                 addr[i] = strtol(address, &ptr, 16);
411                 if (ptr[0] != '\0') {
412                         if (ptr[0] != ':')
413                                 return;
414
415                 address = ptr + 1;
416                 }
417         }
418 }
419
420 void _bt_convert_addr_string_to_secure_string(char *addr,
421                                         const char *address)
422 {
423         int len;
424
425         ret_if(address == NULL);
426         ret_if(addr == NULL);
427
428         len = strlen(address);
429         ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
430
431         strncpy(addr, address, len);
432         addr[len] = '\0';
433
434         addr[len-7] = 'X';
435         addr[len-8] = 'X';
436         addr[len-10] = 'X';
437         addr[len-11] = 'X';
438         addr[len-13] = 'X';
439         addr[len-14] = 'X';
440         addr[len-16] = 'X';
441         addr[len-17] = 'X';
442
443         return;
444 }
445
446 void _bt_convert_addr_type_to_string(char *address,
447                                 unsigned char *addr)
448 {
449         ret_if(address == NULL);
450         ret_if(addr == NULL);
451
452         snprintf(address, BT_ADDRESS_STRING_SIZE,
453                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
454                         addr[0], addr[1], addr[2],
455                         addr[3], addr[4], addr[5]);
456 }
457
458 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
459 {
460         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
461                                 addr->addr[3], addr->addr[4], addr->addr[5]);
462 }
463
464 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
465                                 unsigned int cod)
466 {
467         ret_if(device_class == NULL);
468
469         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
470         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
471         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
472
473         if (cod & 0x002000) {
474                 device_class->service_class |=
475                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
476         }
477 }
478
479 void _bt_free_device_info(bt_remote_dev_info_t *dev_info)
480 {
481         int i;
482
483         ret_if(dev_info == NULL);
484
485         g_free(dev_info->address);
486         g_free(dev_info->name);
487         g_free(dev_info->manufacturer_data);
488
489         if (dev_info->uuids) {
490                 for (i = 0; i < dev_info->uuid_count && dev_info->uuids[i]; i++)
491                         g_free(dev_info->uuids[i]);
492
493                 g_free(dev_info->uuids);
494         }
495
496         g_free(dev_info);
497 }
498
499 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
500 {
501         ret_if(le_dev_info == NULL);
502
503         g_free(le_dev_info->adv_data);
504         g_free(le_dev_info);
505 }
506
507 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
508 {
509         int i;
510         const char *p = src;
511         char *next;
512         int count;
513
514         if (dest == NULL || src == NULL)
515                 return BLUETOOTH_ERROR_INVALID_PARAM;
516
517         BT_DBG("+src : %s", src);
518         BT_DBG("+dest : %s", dest);
519
520         i = 0;
521         while (*p != '\0' && i < length) {
522                 next = g_utf8_next_char(p);
523                 count = next - p;
524
525                 while (count > 0 && ((i + count) < length)) {
526                         dest[i++] = *p;
527                         p++;
528                         count--;
529                 }
530                 p = next;
531         }
532         return BLUETOOTH_ERROR_NONE;
533 }
534
535 gboolean _bt_utf8_validate(char *name)
536 {
537         BT_DBG("+");
538         gunichar2 *u16;
539         glong items_written = 0;
540
541         if (FALSE == g_utf8_validate(name, -1, NULL))
542                 return FALSE;
543
544         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
545         if (u16 == NULL)
546                 return FALSE;
547
548         g_free(u16);
549
550         if (items_written != g_utf8_strlen(name, -1))
551                 return FALSE;
552
553         BT_DBG("-");
554         return TRUE;
555 }
556
557 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
558 {
559         BT_DBG("+");
560         if (!_bt_agent_register_osp_server(type, uuid, path, fd))
561                 return BLUETOOTH_ERROR_INTERNAL;
562
563         return BLUETOOTH_ERROR_NONE;
564 }
565
566 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
567 {
568         BT_DBG("+");
569         if (!_bt_agent_unregister_osp_server(type, uuid))
570                 return BLUETOOTH_ERROR_INTERNAL;
571
572         return BLUETOOTH_ERROR_NONE;
573 }
574
575 int _bt_set_socket_non_blocking(int socket_fd)
576 {
577         /* Set Nonblocking */
578         long arg;
579
580         arg = fcntl(socket_fd, F_GETFL);
581
582         if (arg < 0)
583                 return -errno;
584
585         if (arg & O_NONBLOCK)
586                 BT_ERR("Already Non-blocking \n");
587
588         arg |= O_NONBLOCK;
589
590         if (fcntl(socket_fd, F_SETFL, arg) < 0)
591                 return -errno;
592
593         return BLUETOOTH_ERROR_NONE;
594 }
595
596 int _bt_set_non_blocking_tty(int sk)
597 {
598         struct termios ti = {0,};
599         int err;
600
601         err = _bt_set_socket_non_blocking(sk);
602
603         if (err < 0) {
604                 BT_ERR("Error in set non blocking!\n");
605                 return err;
606         }
607
608         tcflush(sk, TCIOFLUSH);
609
610         /* Switch tty to RAW mode */
611         cfmakeraw(&ti);
612         tcsetattr(sk, TCSANOW, &ti);
613
614         return BLUETOOTH_ERROR_NONE;
615 }
616
617 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
618 {
619         char *object_path = NULL;
620         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
621
622         /* Parse the signature: oa{sa{sv}}} */
623         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
624                         NULL)) {
625                 if (!object_path) {
626                         BT_ERR("Unable to get object path");
627                         return NULL;
628                 }
629                 _bt_convert_device_path_to_address(object_path, device_address);
630                 if (g_strcmp0(address, device_address) == 0)
631                         return g_strdup(object_path);
632
633         }
634
635         BT_ERR("Unable to get object path");
636         return NULL;
637 }
638
639 char *_bt_get_device_object_path(char *address)
640 {
641         char *object_path = NULL;
642         GDBusConnection *conn;
643         GDBusProxy *manager_proxy;
644         GVariant *result = NULL;
645         GVariantIter *iter = NULL;
646
647         conn = _bt_gdbus_get_system_gconn();
648         retv_if(conn == NULL, NULL);
649
650         manager_proxy = _bt_get_manager_proxy();
651         retv_if(manager_proxy == NULL, NULL);
652
653         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
654                                 NULL,
655                                 G_DBUS_CALL_FLAGS_NONE,
656                                 -1,
657                                 NULL,
658                                 NULL);
659         if (!result) {
660                 BT_ERR("Can't get managed objects");
661                 return NULL;
662         }
663
664         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
665         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
666         object_path = __bt_extract_device_path(iter, address);
667         g_variant_iter_free(iter);
668         g_variant_unref(result);
669         return object_path;
670 }
671
672 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
673 {
674         switch (profile_type) {
675         case BT_PROFILE_CONN_RFCOMM:
676                 return strdup(RFCOMM_UUID_STR);
677         case BT_PROFILE_CONN_A2DP:
678                 return strdup(A2DP_SINK_UUID);
679         case BT_PROFILE_CONN_A2DP_SINK:
680                 return strdup(A2DP_SOURCE_UUID);
681         case BT_PROFILE_CONN_HSP:
682                 return strdup(HFP_HS_UUID);
683         case BT_PROFILE_CONN_HID:
684                 return strdup(HID_UUID);
685         case BT_PROFILE_CONN_NAP:
686                 return strdup(NAP_UUID);
687         case BT_PROFILE_CONN_HFG:
688                 return strdup(HFP_AG_UUID);
689         case BT_PROFILE_CONN_PBAP:
690                 return strdup(PBAP_UUID);
691         case BT_PROFILE_CONN_GATT:
692         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
693         default:
694                 return NULL;
695         };
696 }
697
698 const char *_bt_convert_uuid_to_string(const char *uuid)
699 {
700 #define SHORT_UUID_COUNT        162
701 #define LONG_UUID_COUNT         17
702
703         if (!uuid)
704                 return NULL;
705
706         int offset = 0;
707         int uuid_len = 4;
708         typedef struct {
709                 const char *uuid;
710                 const char *specification_name;
711         } uuid_name_s;
712         static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = {
713                 // List should be sorted by UUID
714                 /* BT Classic Services */
715                 {"1101", "Serial Port Service"},
716                 {"1102", "LAN Access Using PPP Service"},
717                 {"1103", "Dialup Networking Service"},
718                 {"1104", "IrMCSync Service"},
719                 {"1105", "OBEX Object Push Service"},
720                 {"1106", "OBEX File Transfer Service"},
721                 {"1107", "IrMC Sync Command Service"},
722                 {"1108", "Headset Service"},
723                 {"1109", "Cordless Telephony Service"},
724                 {"110A", "Audio Source Service"},
725                 {"110B", "Audio Sink Service"},
726                 {"110C", "AV Remote Control Target Service"},
727                 {"110D", "Advanced Audio Distribution Profile"},
728                 {"110E", "AV Remote Control Service"},
729                 {"110F", "Video Conferencing Service"},
730                 {"1110", "Intercom Service"},
731                 {"1111", "Fax Service"},
732                 {"1112", "Headset Audio Gateway Service"},
733                 {"1113", "WAP Service"},
734                 {"1114", "WAP Client Service"},
735                 {"1115", "PANU Service"},
736                 {"1116", "NAP Service"},
737                 {"1117", "GN Service"},
738                 {"1118", "Direct Printing Service"},
739                 {"1119", "Reference Printing Service"},
740                 {"111A", "Basic Imaging Profile"},
741                 {"111B", "Imaging Responder Service"},
742                 {"111C", "Imaging Automatic Archive Service"},
743                 {"111D", "Imaging Reference Objects Service"},
744                 {"111E", "Handsfree Service"},
745                 {"111F", "Handsfree Audio Gateway Service"},
746                 {"1120", "Direct Printing Reference Objects Service"},
747                 {"1121", "Reflected UI Service"},
748                 {"1122", "Basic Printing Profile"},
749                 {"1123", "Printing Status Service"},
750                 {"1124", "Human Interface Device Service"},
751                 {"1125", "Hardcopy Cable Replacement Profile"},
752                 {"1126", "HCR Print Service"},
753                 {"1127", "HCR Scan Service"},
754                 {"112D", "SIM Access Service"},
755                 {"112E", "Phonebook Access PCE Service"},
756                 {"112F", "Phonebook Access PSE Service"},
757                 {"1130", "Phonebook Access Profile"},
758                 {"1132", "Message Access Server Service"},
759                 {"1133", "Message Notification Server Service"},
760                 {"1134", "Message Access Profile"},
761                 {"1200", "PnP Information Service"},
762                 {"1201", "Generic Networking Service"},
763                 {"1202", "Generic File Transfer Service"},
764                 {"1203", "Generic Audio Service"},
765                 {"1204", "Generic Telephony Service"},
766                 {"1205", "UPnP Service"},
767                 {"1206", "UPnP Ip Service"},
768                 {"1303", "Video Source Service"},
769                 {"1304", "Video Sink Service"},
770                 {"1305", "Video Distribution Profile"},
771                 {"1400", "Health Device Profile"},
772                 {"1401", "HDP Source Service"},
773                 {"1402", "HDP Sink Service"},
774
775                 /* GATT Services */
776                 {"1800", "Generic Access"},
777                 {"1801", "Generic Attribute"},
778                 {"1802", "Immediate Alert"},
779                 {"1803", "Link Loss"},
780                 {"1804", "Tx Power"},
781                 {"1805", "Current Time Service"},
782                 {"1806", "Reference Time Update Service"},
783                 {"1807", "Next DST Change Service"},
784                 {"1808", "Glucose"},
785                 {"1809", "Health Thermometer"},
786                 {"180A", "Device Information"},
787                 {"180D", "Heart Rate"},
788                 {"180F", "Battery Service"},
789                 {"1810", "Blood Pressure"},
790                 {"1811", "Alert Notification Service"},
791                 {"1812", "Human Interface Device"},
792                 {"1813", "Scan Parameters"},
793                 {"1814", "Running Speed and Cadence"},
794                 {"1815", "Automation IO"},
795                 {"1816", "Cycling Speed and Cadence"},
796                 {"1818", "Cycling Power"},
797                 {"1819", "Location and Navigation"},
798                 {"181A", "Environmental Sensing"},
799                 {"181B", "Body Composition"},
800                 {"181C", "User Data"},
801                 {"181D", "Weight Scale"},
802                 {"181E", "Bond Management"},
803                 {"181F", "Continuous Glucose Monitoring"},
804
805                 /* GATT Declarations */
806                 {"2800", "Primary Service Declaration"},
807                 {"2801", "Secondary Service Declaration"},
808                 {"2802", "Include Declaration"},
809                 {"2803", "Characteristic Declaration"},
810
811                 /* GATT Descriptors */
812                 {"2900", "Characteristic Extended Properties"},
813                 {"2901", "Characteristic User Description"},
814                 {"2902", "Client Characteristic Configuration"},
815                 {"2903", "Server Characteristic Configuration"},
816                 {"2904", "Characteristic Format"},
817                 {"2905", "Characteristic Aggregate Formate"},
818                 {"2906", "Valid Range"},
819                 {"2907", "External Report Reference"},
820                 {"2908", "Report Reference"},
821
822                 /* GATT Characteristics */
823                 {"2A00", "Device Name"},
824                 {"2A01", "Appearance"},
825                 {"2A02", "Peripheral Privacy Flag"},
826                 {"2A03", "Reconnection Address"},
827                 {"2A04", "Peripheral Preferred Connection Parameters"},
828                 {"2A05", "Service Changed"},
829                 {"2A06", "Alert Level"},
830                 {"2A07", "Tx Power Level"},
831                 {"2A08", "Date Time"},
832                 {"2A09", "Day of Week"},
833                 {"2A0A", "Day Date Time"},
834                 {"2A11", "Time with DST"},
835                 {"2A12", "Time Accuracy"},
836                 {"2A13", "Time Source"},
837                 {"2A14", "Reference Time Information"},
838                 {"2A16", "Time Update Control Point"},
839                 {"2A17", "Time Update State"},
840                 {"2A18", "Glucose Measurement"},
841                 {"2A19", "Battery Level"},
842                 {"2A1C", "Temperature Measurement"},
843                 {"2A1D", "Temperature Type"},
844                 {"2A1E", "Intermediate Temperature"},
845                 {"2A21", "Measurement Interval"},
846                 {"2A23", "System ID"},
847                 {"2A24", "Model Number String"},
848                 {"2A25", "Serial Number String"},
849                 {"2A26", "Firmware Revision String"},
850                 {"2A27", "Hardware Revision String"},
851                 {"2A28", "Software Revision String"},
852                 {"2A29", "Manufacturer Name String"},
853                 {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
854                 {"2A2B", "Current Time"},
855                 {"2A34", "Glucose Measurement Context"},
856                 {"2A35", "Blood Pressure Measurement"},
857                 {"2A37", "Heart Rate Measurement"},
858                 {"2A38", "Body Sensor Location"},
859                 {"2A39", "Heart Rate Control Point"},
860                 {"2A3F", "Alert Status"},
861                 {"2A46", "New Alert"},
862                 {"2A49", "Blood Pressure Feature"},
863                 {"2A4A", "HID Information"},
864                 {"2A4C", "HID Control Point"},
865                 {"2A50", "PnP ID"},
866                 {"2A51", "Glucose Feature"},
867                 {"2A52", "Record Access Control Point"},
868                 {"2A53", "RSC Measurement"},
869                 {"2A54", "RSC Feature"},
870                 {"2A55", "SC Control Point"},
871                 {"2A56", "Digital"},
872                 {"2A58", "Analog"},
873                 {"2A5A", "Aggregate"},
874                 {"2A5B", "CSC Measurement"},
875                 {"2A5C", "CSC Feature"},
876                 {"2A5D", "Sensor Location"},
877                 {"2A63", "Cycling Power Measurement"},
878                 {"2A64", "Cycling Power Vector"},
879                 {"2A65", "Cycling Power Feature"},
880                 {"2A66", "Cycling Power Control Point"},
881                 {"2A67", "Location and Speed"},
882                 {"2A68", "Navigation"},
883                 {"2A6D", "Pressure"},
884                 {"2A6E", "Temperature"},
885         };
886         static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = {
887                 // List should be sorted by UUID
888                 /* Custom uuids */
889                 {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"},
890                 {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
891                 {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
892                 {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
893                 {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
894                 {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
895                 {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
896                 {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
897                 {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
898                 {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
899                 {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"},
900                 {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
901                 {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
902                 {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"},
903                 {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
904                 {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"},
905                 {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
906         };
907         const uuid_name_s *uuid_name = short_uuid_name;
908         static const char *unknown_name = "Unknown";
909         int start = 0;
910         int end = SHORT_UUID_COUNT - 1;
911         int p;
912         int ret;
913
914         if (strlen(uuid) == 36) {
915                 if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
916                         offset = 4;
917                 else {
918                         offset = 0;
919                         uuid_len = 36;
920                         end = LONG_UUID_COUNT - 1;
921                         uuid_name = long_uuid_name;
922                 }
923         } else if (strlen(uuid) >= 8)
924                 offset = 4;
925
926         while (start <= end) {
927                 p = start + (end - start) / 2;
928                 ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len);
929                 if (ret == 0)
930                         return uuid_name[p].specification_name;
931                 else if (ret < 0)
932                         end = p - 1;
933                 else
934                         start = p + 1;
935         }
936
937         BT_INFO("Unknown uuid : %s", uuid);
938         return unknown_name;
939 }
940
941 const char *_bt_convert_error_to_string(int error)
942 {
943         switch (error) {
944         case BLUETOOTH_ERROR_CANCEL:
945                 return "CANCELLED";
946         case BLUETOOTH_ERROR_INVALID_PARAM:
947                 return "INVALID_PARAMETER";
948         case BLUETOOTH_ERROR_INVALID_DATA:
949                 return "INVALID DATA";
950         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
951         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
952                 return "OUT_OF_MEMORY";
953         case BLUETOOTH_ERROR_TIMEOUT:
954                 return "TIMEOUT";
955         case BLUETOOTH_ERROR_NO_RESOURCES:
956                 return "NO_RESOURCES";
957         case BLUETOOTH_ERROR_INTERNAL:
958                 return "INTERNAL";
959         case BLUETOOTH_ERROR_NOT_SUPPORT:
960                 return "NOT_SUPPORT";
961         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
962                 return "NOT_ENABLED";
963         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
964                 return "ALREADY_ENABLED";
965         case BLUETOOTH_ERROR_DEVICE_BUSY:
966                 return "DEVICE_BUSY";
967         case BLUETOOTH_ERROR_ACCESS_DENIED:
968                 return "ACCESS_DENIED";
969         case BLUETOOTH_ERROR_MAX_CLIENT:
970                 return "MAX_CLIENT";
971         case BLUETOOTH_ERROR_NOT_FOUND:
972                 return "NOT_FOUND";
973         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
974                 return "SERVICE_SEARCH_ERROR";
975         case BLUETOOTH_ERROR_PARING_FAILED:
976                 return "PARING_FAILED";
977         case BLUETOOTH_ERROR_NOT_PAIRED:
978                 return "NOT_PAIRED";
979         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
980                 return "SERVICE_NOT_FOUND";
981         case BLUETOOTH_ERROR_NOT_CONNECTED:
982                 return "NOT_CONNECTED";
983         case BLUETOOTH_ERROR_ALREADY_CONNECT:
984                 return "ALREADY_CONNECT";
985         case BLUETOOTH_ERROR_CONNECTION_BUSY:
986                 return "CONNECTION_BUSY";
987         case BLUETOOTH_ERROR_CONNECTION_ERROR:
988                 return "CONNECTION_ERROR";
989         case BLUETOOTH_ERROR_MAX_CONNECTION:
990                 return "MAX_CONNECTION";
991         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
992                 return "NOT_IN_OPERATION";
993         case BLUETOOTH_ERROR_CANCEL_BY_USER:
994                 return "CANCEL_BY_USER";
995         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
996                 return "REGISTRATION_FAILED";
997         case BLUETOOTH_ERROR_IN_PROGRESS:
998                 return "IN_PROGRESS";
999         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
1000                 return "AUTHENTICATION_FAILED";
1001         case BLUETOOTH_ERROR_HOST_DOWN:
1002                 return "HOST_DOWN";
1003         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
1004                 return "END_OF_DEVICE_LIST";
1005         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
1006                 return "AGENT_ALREADY_EXIST";
1007         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
1008                 return "AGENT_DOES_NOT_EXIST";
1009         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
1010                 return "ALREADY_INITIALIZED";
1011         case BLUETOOTH_ERROR_PERMISSION_DEINED:
1012                 return "PERMISSION_DEINED";
1013         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
1014                 return "ALREADY_DEACTIVATED";
1015         case BLUETOOTH_ERROR_NOT_INITIALIZED:
1016                 return "NOT_INITIALIZED";
1017         case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
1018                 return "AUTHENTICATION REJECTED";
1019         default:
1020                 return "UNKNOWN";
1021         }
1022 }
1023
1024 const char *_bt_convert_disc_reason_to_string(int reason)
1025 {
1026         switch (reason) {
1027         case (int)BLUETOOTH_ERROR_PAGE_TIMEOUT:
1028                 return "Page timeout";
1029         case (int)BLUETOOTH_ERROR_CONNECTION_TIMEOUT:
1030                 return "Link loss";
1031         case (int)BLUETOOTH_ERROR_PIN_OR_KEY_MISSING:
1032                 return "PIN or Key missing";
1033         case (int)BLUETOOTH_ERROR_LOCAL_HOST_TERM:
1034                 return "Connection terminated by local host";
1035         case (int)BLUETOOTH_ERROR_REMOTE_USER_TERM:
1036         case (int)BLUETOOTH_ERROR_REMOTE_LOW_RESOURCES:
1037         case (int)BLUETOOTH_ERROR_REMOTE_POWER_OFF:
1038                 return "Remote user terminated connection";
1039         case (int)BLUETOOTH_ERROR_AUTH_FAILURE:
1040                 return "Authentication Failure";
1041         case (int)BLUETOOTH_ERROR_REPEATED_ATTEMPTS:
1042                 return "Repeated attempts";
1043         case (int)BLUETOOTH_ERROR_LMP_RESPONSE_TIMEOUT:
1044                 return "LMP response timeout";
1045         case (int)BLUETOOTH_ERROR_LMP_TRANSACTION_COLLISION:
1046                 return "LMP transaction collision";
1047         case (int)BLUETOOTH_ERROR_INSTANT_PASSED:
1048                 return "Instant passed";
1049         case (int)BLUETOOTH_ERROR_INSUFFICIENT_SECURITY:
1050                 return "Insufficient security";
1051         case (int)BLUETOOTH_ERROR_CONNECTION_TERMINATED_DUE_TO_MIC_FAILURE:
1052                 return "Connection terminated due to MIC failure";
1053         case (int)BLUETOOTH_ERROR_CONNECTION_FAILED_TO_BE_ESTABLISHED:
1054                 return "Connection failed to be established";
1055         default:
1056                 return "Unknown";
1057         }
1058 }
1059
1060 const char *_bt_convert_profile_state_to_string(bt_profile_state_t state)
1061 {
1062         switch (state) {
1063         case BT_PROFILE_STATE_UNAVAILABLE:
1064                 return "UNAVAILABLE";
1065         case BT_PROFILE_STATE_DISCONNECTED:
1066                 return "DISCONNECTED";
1067         case BT_PROFILE_STATE_CONNECTING:
1068                 return "CONNECTING";
1069         case BT_PROFILE_STATE_CONNECTED:
1070                 return "CONNECTED";
1071         case BT_PROFILE_STATE_DISCONNECTING:
1072                 return "DISCONNECTING";
1073         }
1074
1075         return NULL;
1076 }
1077
1078 void _bt_logging_connection(gboolean connect, int addr_type)
1079 {
1080         static int le_conn = 0;
1081         static int le_disc = 0;
1082         static int edr_conn = 0;
1083         static int edr_disc = 0;
1084
1085         if (connect) {
1086                 if (addr_type)
1087                         le_conn++;
1088                 else
1089                         edr_conn++;
1090         } else {
1091                 if (addr_type)
1092                         le_disc++;
1093                 else
1094                         edr_disc++;
1095         }
1096
1097         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
1098                         le_conn, le_disc, edr_conn, edr_disc);
1099 }
1100
1101 void _bt_swap_byte_ordering(char *data, int data_len)
1102 {
1103         char temp;
1104         int i, j;
1105         int half = data_len / 2;
1106
1107         ret_if(data == NULL);
1108         /* Swap to opposite endian */
1109         for (i = 0, j = data_len - 1; i < half; i++, j--) {
1110                 temp = data[i];
1111                 data[i] = data[j];
1112                 data[j] = temp;
1113         }
1114 }
1115
1116 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
1117 {
1118         int i;
1119
1120         retv_if(data1 == NULL, -1);
1121         retv_if(data2 == NULL, -1);
1122         for (i = 0; i < data_len; i++) {
1123                 if (data1[i] != data2[i])
1124                         return data1[i] - data2[i];
1125                 }
1126         return 0;
1127 }
1128 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
1129         const char *mask, int data_len)
1130 {
1131         int i;
1132         char a, b;
1133
1134         retv_if(data1 == NULL, -1);
1135         retv_if(data2 == NULL, -1);
1136         retv_if(mask == NULL, -1);
1137         for (i = 0; i < data_len; i++) {
1138                 a = data1[i] & mask[i];
1139                 b = data2[i] & mask[i];
1140                 if (a != b)
1141                         return (int)(a - b);
1142                 }
1143         return 0;
1144 }
1145
1146 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
1147 {
1148         int ret = ES_R_OK;
1149 /* Send event system event in bt-core process because bt-service's permission is not system in now */
1150
1151 #if 0
1152         bundle *b = NULL;
1153
1154         b = bundle_create();
1155
1156         bundle_add_str(b, key, value);
1157
1158         ret = eventsystem_send_system_event(event, b);
1159
1160         BT_DBG("eventsystem_send_system_event result: %d", ret);
1161
1162         bundle_free(b);
1163 #endif
1164         return ret;
1165 }
1166
1167 void __bt_get_auth_info(GVariant *reply, char *auth_info)
1168 {
1169         int cursor;
1170         GVariant *tmp_value;
1171         char *manufacturer_data = NULL;
1172         int manufacturer_data_len;
1173         gboolean is_alias_set;
1174         GVariantIter *value_iter;
1175         guint8 m_value;
1176         int i = 0;
1177
1178         tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
1179                                                                 G_VARIANT_TYPE_BOOLEAN);
1180         if (tmp_value) {
1181                 is_alias_set = g_variant_get_boolean(tmp_value);
1182                 g_variant_unref(tmp_value);
1183         } else {
1184                 is_alias_set = FALSE;
1185         }
1186         if (is_alias_set == FALSE) {
1187                 tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerDataLen",
1188                                                                 G_VARIANT_TYPE_UINT16);
1189                 if (tmp_value) {
1190                         manufacturer_data_len = g_variant_get_uint16(tmp_value);
1191                         if (manufacturer_data_len >
1192                                         BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
1193                                 BT_ERR("manufacturer_data_len is too long");
1194                                 manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
1195                         }
1196                         g_variant_unref(tmp_value);
1197                 } else
1198                         manufacturer_data_len = 0;
1199
1200                 tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerData",
1201                                                                 G_VARIANT_TYPE_ARRAY);
1202                 if (tmp_value) {
1203                         if ((manufacturer_data_len == 0) ||
1204                                         manufacturer_data_len != g_variant_get_size(tmp_value)) {
1205                                 BT_ERR("manufacturer data length doesn't match");
1206                                 manufacturer_data_len = 0;
1207                                 manufacturer_data = NULL;
1208                         } else {
1209                                 manufacturer_data = g_malloc0(manufacturer_data_len);
1210                                 g_variant_get(tmp_value, "ay", &value_iter);
1211                                 while (g_variant_iter_loop(value_iter, "y", &m_value))
1212                                         manufacturer_data[i++] = m_value;
1213                         }
1214                         g_variant_unref(tmp_value);
1215                 } else {
1216                         BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
1217                         manufacturer_data_len = 0;
1218                         manufacturer_data = NULL;
1219                 }
1220                 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
1221                 if (manufacturer_data_len < 30) {
1222                         g_free(manufacturer_data);
1223                         return;
1224                 }
1225                 if (manufacturer_data[0] != 0x00 || manufacturer_data[1] != 0x75) {
1226                         BT_DBG("This is not a samsung specific manufaturer data");
1227                         g_free(manufacturer_data);
1228                         return;
1229                 }
1230
1231                 /* 2  samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
1232                 1 (discovery version) + 1 (associated service ID)
1233                 2 (Proxamity and locality) + 2 (Device type and icon) */
1234
1235                 cursor = 10;
1236
1237                 memcpy(auth_info, &(manufacturer_data[cursor]), 5);
1238         }
1239          g_free(manufacturer_data);
1240 }
1241
1242 int _bt_convert_gerror(GError *g_error)
1243 {
1244         int ret = BLUETOOTH_ERROR_INTERNAL;
1245         gchar *str;
1246
1247         if (!g_dbus_error_strip_remote_error(g_error))
1248                 return ret;
1249
1250         str = g_error->message;
1251
1252         if (g_strcmp0(str, "Connection refused") == 0)
1253                 ret = BLUETOOTH_ERROR_AUTHENTICATION_REJECTED;
1254         else if (g_strcmp0(str, "Connection timed out") == 0)
1255                 ret = BLUETOOTH_ERROR_TIMEOUT;
1256         else if (g_strcmp0(str, "In Progress") == 0)
1257                 ret = BLUETOOTH_ERROR_IN_PROGRESS;
1258
1259         return ret;
1260 }