Rollback changes to submit TIZEN:COMMON project
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25
26 #include "bluetooth-api.h"
27 #include "bluetooth-audio-api.h"
28 #include "bluetooth-hid-api.h"
29 #include "bluetooth-media-control.h"
30 #include "bt-internal-types.h"
31
32 #include "bt-common.h"
33 #include "bt-request-sender.h"
34 #include "bt-event-handler.h"
35
36 static bt_user_info_t user_info[BT_MAX_USER_INFO];
37 static DBusGConnection *system_conn = NULL;
38
39 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
40 {
41         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
42                                 addr->addr[3], addr->addr[4], addr->addr[5]);
43 }
44
45 void _bt_set_user_data(int type, void *callback, void *user_data)
46 {
47         user_info[type].cb = callback;
48         user_info[type].user_data = user_data;
49 }
50
51 bt_user_info_t *_bt_get_user_data(int type)
52 {
53         return &user_info[type];
54 }
55
56 void _bt_common_event_cb(int event, int result, void *param,
57                                         void *callback, void *user_data)
58 {
59         bluetooth_event_param_t bt_event = { 0, };
60         bt_event.event = event;
61         bt_event.result = result;
62         bt_event.param_data = param;
63
64         if (callback)
65                 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
66                                         user_data);
67 }
68
69 void _bt_input_event_cb(int event, int result, void *param,
70                                         void *callback, void *user_data)
71 {
72         hid_event_param_t bt_event = { 0, };
73         bt_event.event = event;
74         bt_event.result = result;
75         bt_event.param_data = param;
76
77         if (callback)
78                 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
79                                         user_data);
80 }
81
82 void _bt_headset_event_cb(int event, int result, void *param,
83                                         void *callback, void *user_data)
84 {
85         bt_audio_event_param_t bt_event = { 0, };
86         bt_event.event = event;
87         bt_event.result = result;
88         bt_event.param_data = param;
89
90         if (callback)
91                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
92                                         user_data);
93 }
94
95 void _bt_avrcp_event_cb(int event, int result, void *param,
96                                         void *callback, void *user_data)
97 {
98         media_event_param_t bt_event = { 0, };
99         bt_event.event = event;
100         bt_event.result = result;
101         bt_event.param_data = param;
102
103         if (callback)
104                 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
105                                         user_data);
106 }
107
108 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
109                                 unsigned int cod)
110 {
111         ret_if(device_class == NULL);
112
113         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
114         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
115         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
116
117         if (cod & 0x002000) {
118                 device_class->service_class |=
119                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
120         }
121 }
122
123 void _bt_convert_addr_string_to_type(unsigned char *addr,
124                                         const char *address)
125 {
126         int i;
127         char *ptr = NULL;
128
129         ret_if(address == NULL);
130         ret_if(addr == NULL);
131
132         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
133                 addr[i] = strtol(address, &ptr, 16);
134                 if (ptr != NULL) {
135                         if (ptr[0] != ':')
136                                 return;
137
138                         address = ptr + 1;
139                 }
140         }
141 }
142
143 void _bt_convert_addr_type_to_string(char *address,
144                                 unsigned char *addr)
145 {
146         ret_if(address == NULL);
147         ret_if(addr == NULL);
148
149         g_snprintf(address, BT_ADDRESS_STRING_SIZE,
150                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
151                         addr[0], addr[1], addr[2],
152                         addr[3], addr[4], addr[5]);
153 }
154
155 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
156 {
157         int i;
158         char *p = src;
159         char *next;
160         int count;
161
162         if (dest == NULL || src == NULL)
163                 return BLUETOOTH_ERROR_INVALID_PARAM;
164
165         i = 0;
166         while (*p != '\0' && i < length) {
167                 next = g_utf8_next_char(p);
168                 count = next - p;
169
170                 while (count > 0 && ((i + count) < length)) {
171                         dest[i++] = *p;
172                         p++;
173                         count --;
174                 }
175                 p = next;
176         }
177         return BLUETOOTH_ERROR_NONE;
178 }
179
180 static char *__bt_extract_adapter_path(DBusMessageIter *msg_iter)
181 {
182         char *object_path = NULL;
183         DBusMessageIter value_iter;
184
185         /* Parse the signature:  oa{sa{sv}}} */
186         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
187                                 DBUS_TYPE_OBJECT_PATH, NULL);
188
189         dbus_message_iter_get_basic(msg_iter, &object_path);
190         retv_if(object_path == NULL, NULL);
191
192         /* object array (oa) */
193         retv_if(dbus_message_iter_next(msg_iter) == FALSE, NULL);
194         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
195                                 DBUS_TYPE_ARRAY, NULL);
196
197         dbus_message_iter_recurse(msg_iter, &value_iter);
198
199         /* string array (sa) */
200         while (dbus_message_iter_get_arg_type(&value_iter) ==
201                                         DBUS_TYPE_DICT_ENTRY) {
202                 char *interface_name = NULL;
203                 DBusMessageIter interface_iter;
204
205                 dbus_message_iter_recurse(&value_iter, &interface_iter);
206
207                 retv_if(dbus_message_iter_get_arg_type(&interface_iter) !=
208                         DBUS_TYPE_STRING, NULL);
209
210                 dbus_message_iter_get_basic(&interface_iter, &interface_name);
211
212                 if (g_strcmp0(interface_name, "org.bluez.Adapter1") == 0) {
213                         /* Tizen don't allow the multi-adapter */
214                         BT_DBG("Found an adapter: %s", object_path);
215                         return g_strdup(object_path);
216                 }
217
218                 dbus_message_iter_next(&value_iter);
219         }
220
221         return NULL;
222 }
223
224 /* TO DO */
225 /* Change DBusGConnection to DBusConnection*/
226 int _bt_get_adapter_path(DBusGConnection *g_conn, char *path)
227 {
228         DBusMessage *msg;
229         DBusMessage *reply;
230         DBusMessageIter reply_iter;
231         DBusMessageIter value_iter;
232         DBusError err;
233         DBusConnection *conn;
234         char *adapter_path = NULL;
235
236         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
237
238         conn = dbus_g_connection_get_connection(g_conn);
239         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
240
241         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
242                                                 BT_MANAGER_INTERFACE,
243                                                 "GetManagedObjects");
244
245         if (msg == NULL) {
246                 BT_ERR("Can't allocate D-Bus message");
247                 goto fail;
248         }
249
250         /* Synchronous call */
251         dbus_error_init(&err);
252         reply = dbus_connection_send_with_reply_and_block(
253                                         conn, msg,
254                                         -1, &err);
255         dbus_message_unref(msg);
256
257         if (!reply) {
258                 BT_ERR("Can't get managed objects");
259
260                 if (dbus_error_is_set(&err)) {
261                         BT_ERR("%s", err.message);
262                         dbus_error_free(&err);
263                 }
264                 goto fail;
265         }
266
267         if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
268             BT_ERR("Fail to iterate the reply");
269             goto fail;
270         }
271
272         dbus_message_iter_recurse(&reply_iter, &value_iter);
273
274         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
275         while (dbus_message_iter_get_arg_type(&value_iter) ==
276                                                 DBUS_TYPE_DICT_ENTRY) {
277                 DBusMessageIter msg_iter;
278
279                 dbus_message_iter_recurse(&value_iter, &msg_iter);
280
281                 adapter_path = __bt_extract_adapter_path(&msg_iter);
282                 if (adapter_path != NULL) {
283                         BT_DBG("Found the adapter path");
284                         break;
285                 }
286
287                 dbus_message_iter_next(&value_iter);
288         }
289
290         if (adapter_path == NULL ||
291              strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
292                 BT_ERR("Adapter path is inproper\n");
293                 goto fail;
294         }
295
296         BT_DBG("adapter path: %s", adapter_path);
297
298         if (path)
299                 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
300
301         g_free(adapter_path);
302
303         return BLUETOOTH_ERROR_NONE;
304
305 fail:
306         g_free(adapter_path);
307
308         return BLUETOOTH_ERROR_INTERNAL;
309 }
310
311 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
312 {
313         DBusGProxy *adapter_proxy = NULL;
314
315         retv_if(conn == NULL, NULL);
316
317         adapter_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
318                         BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
319
320         return adapter_proxy;
321 }
322
323 void _bt_device_path_to_address(const char *device_path, char *device_address)
324 {
325         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
326         char *dev_addr = NULL;
327
328         if (!device_path || !device_address)
329                 return;
330
331         dev_addr = strstr(device_path, "dev_");
332         if (dev_addr != NULL) {
333                 char *pos = NULL;
334                 dev_addr += 4;
335                 g_strlcpy(address, dev_addr, sizeof(address));
336
337                 while ((pos = strchr(address, '_')) != NULL) {
338                         *pos = ':';
339                 }
340
341                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
342         }
343 }
344
345 DBusGConnection *__bt_init_system_gconn(void)
346 {
347         g_type_init();
348
349         if (system_conn == NULL)
350                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
351
352         return system_conn;
353 }
354
355 DBusGConnection *_bt_get_system_gconn(void)
356 {
357         return (system_conn) ? system_conn : __bt_init_system_gconn();
358 }
359
360 DBusConnection *_bt_get_system_conn(void)
361 {
362         DBusGConnection *g_conn;
363
364         if (system_conn == NULL) {
365                 g_conn = __bt_init_system_gconn();
366         } else {
367                 g_conn = system_conn;
368         }
369
370         retv_if(g_conn == NULL, NULL);
371
372         return dbus_g_connection_get_connection(g_conn);
373 }
374
375 BT_EXPORT_API int bluetooth_is_supported(void)
376 {
377         int is_supported = 0;
378         int len = 0;
379         int fd = -1;
380         rfkill_event event;
381
382         fd = open(RFKILL_NODE, O_RDONLY);
383         if (fd < 0) {
384                 BT_DBG("Fail to open RFKILL node");
385                 return BLUETOOTH_ERROR_INTERNAL;
386         }
387
388         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
389                 BT_DBG("Fail to set RFKILL node to non-blocking");
390                 close(fd);
391                 return BLUETOOTH_ERROR_INTERNAL;
392         }
393
394         while (1) {
395                 len = read(fd, &event, sizeof(event));
396                 if (len < 0) {
397                         BT_DBG("Fail to read events");
398                         break;
399                 }
400
401                 if (len != RFKILL_EVENT_SIZE) {
402                         BT_DBG("The size is wrong\n");
403                         continue;
404                 }
405
406                 if (event.type == RFKILL_TYPE_BLUETOOTH) {
407                         is_supported = 1;
408                         break;
409                 }
410         }
411
412         close(fd);
413
414         BT_DBG("supported: %d", is_supported);
415
416         return is_supported;
417 }
418
419 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
420 {
421         int ret;
422
423         __bt_init_system_gconn();
424
425         ret = _bt_init_event_handler();
426
427         if (ret != BLUETOOTH_ERROR_NONE &&
428              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
429                 BT_ERR("Fail to init the event handler");
430                 return ret;
431         }
432
433         _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
434
435         /* Register All events */
436         _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
437         _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
438         _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
439         _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
440         _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
441
442         return BLUETOOTH_ERROR_NONE;
443 }
444
445 BT_EXPORT_API int bluetooth_unregister_callback(void)
446 {
447         _bt_unregister_event(BT_ADAPTER_EVENT);
448         _bt_unregister_event(BT_DEVICE_EVENT);
449         _bt_unregister_event(BT_NETWORK_EVENT);
450         _bt_unregister_event(BT_RFCOMM_CLIENT_EVENT);
451         _bt_unregister_event(BT_RFCOMM_SERVER_EVENT);
452
453         _bt_set_user_data(BT_COMMON, NULL, NULL);
454
455         if (system_conn) {
456                 dbus_g_connection_unref(system_conn);
457                 system_conn = NULL;
458         }
459
460         return BLUETOOTH_ERROR_NONE;
461 }
462