Merge branch 'master' into tizen_2.1
[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 int _bt_get_adapter_path(DBusGConnection *conn, char *path)
181 {
182         GError *err = NULL;
183         DBusGProxy *manager_proxy = NULL;
184         char *adapter_path = NULL;
185         int ret = BLUETOOTH_ERROR_NONE;
186
187         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
188
189         manager_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
190                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
191
192         retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
193
194         if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &err,
195                                 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
196                                 &adapter_path,
197                                 G_TYPE_INVALID)) {
198                 if (err != NULL) {
199                         BT_ERR("Getting DefaultAdapter failed: [%s]\n", err->message);
200                         g_error_free(err);
201                 }
202                 g_object_unref(manager_proxy);
203                 return BLUETOOTH_ERROR_INTERNAL;
204         }
205
206         if (adapter_path == NULL || strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
207                 BT_ERR("Adapter path is inproper\n");
208                 ret = BLUETOOTH_ERROR_INTERNAL;
209                 goto done;
210         }
211
212         if (path)
213                 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
214 done:
215         g_free(adapter_path);
216         g_object_unref(manager_proxy);
217
218         return ret;
219 }
220
221 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
222 {
223         GError *err = NULL;
224         DBusGProxy *manager_proxy = NULL;
225         DBusGProxy *adapter_proxy = NULL;
226         char *adapter_path = NULL;
227
228         retv_if(conn == NULL, NULL);
229
230         manager_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
231                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
232
233         retv_if(manager_proxy == NULL, NULL);
234
235         if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &err,
236                                 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
237                                 &adapter_path,
238                                 G_TYPE_INVALID)) {
239                 if (err != NULL) {
240                         BT_ERR("Getting DefaultAdapter failed: [%s]\n", err->message);
241                         g_error_free(err);
242                 }
243                 g_object_unref(manager_proxy);
244                 return NULL;
245         }
246
247         if (adapter_path == NULL || strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
248                 BT_ERR("Adapter path is inproper\n");
249                 g_free(adapter_path);
250                 g_object_unref(manager_proxy);
251                 return NULL;
252         }
253
254         adapter_proxy = dbus_g_proxy_new_for_name(conn,
255                                         BT_BLUEZ_NAME,
256                                         adapter_path,
257                                         BT_ADAPTER_INTERFACE);
258         g_free(adapter_path);
259         g_object_unref(manager_proxy);
260
261         return adapter_proxy;
262 }
263
264 void _bt_device_path_to_address(const char *device_path, char *device_address)
265 {
266         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
267         char *dev_addr = NULL;
268
269         if (!device_path || !device_address)
270                 return;
271
272         dev_addr = strstr(device_path, "dev_");
273         if (dev_addr != NULL) {
274                 char *pos = NULL;
275                 dev_addr += 4;
276                 g_strlcpy(address, dev_addr, sizeof(address));
277
278                 while ((pos = strchr(address, '_')) != NULL) {
279                         *pos = ':';
280                 }
281
282                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
283         }
284 }
285
286 DBusGConnection *__bt_init_system_gconn(void)
287 {
288         g_type_init();
289
290         if (system_conn == NULL)
291                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
292
293         return system_conn;
294 }
295
296 DBusGConnection *_bt_get_system_gconn(void)
297 {
298         return (system_conn) ? system_conn : __bt_init_system_gconn();
299 }
300
301 DBusConnection *_bt_get_system_conn(void)
302 {
303         DBusGConnection *g_conn;
304
305         if (system_conn == NULL) {
306                 g_conn = __bt_init_system_gconn();
307         } else {
308                 g_conn = system_conn;
309         }
310
311         retv_if(g_conn == NULL, NULL);
312
313         return dbus_g_connection_get_connection(g_conn);
314 }
315
316 BT_EXPORT_API int bluetooth_is_supported(void)
317 {
318         int is_supported = 0;
319         int len = 0;
320         int fd = -1;
321         rfkill_event event;
322
323         fd = open(RFKILL_NODE, O_RDONLY);
324         if (fd < 0) {
325                 BT_DBG("Fail to open RFKILL node");
326                 return BLUETOOTH_ERROR_INTERNAL;
327         }
328
329         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
330                 BT_DBG("Fail to set RFKILL node to non-blocking");
331                 close(fd);
332                 return BLUETOOTH_ERROR_INTERNAL;
333         }
334
335         while (1) {
336                 len = read(fd, &event, sizeof(event));
337                 if (len < 0) {
338                         BT_DBG("Fail to read events");
339                         break;
340                 }
341
342                 if (len != RFKILL_EVENT_SIZE) {
343                         BT_DBG("The size is wrong\n");
344                         continue;
345                 }
346
347                 if (event.type == RFKILL_TYPE_BLUETOOTH) {
348                         is_supported = 1;
349                         break;
350                 }
351         }
352
353         close(fd);
354
355         BT_DBG("supported: %d", is_supported);
356
357         return is_supported;
358 }
359
360 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
361 {
362         int ret;
363
364         __bt_init_system_gconn();
365
366         ret = _bt_init_event_handler();
367
368         if (ret != BLUETOOTH_ERROR_NONE &&
369              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
370                 BT_ERR("Fail to init the event handler");
371                 return ret;
372         }
373
374         _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
375
376         /* Register All events */
377         _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
378         _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
379         _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
380         _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
381         _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
382
383         return BLUETOOTH_ERROR_NONE;
384 }
385
386 BT_EXPORT_API int bluetooth_unregister_callback(void)
387 {
388         _bt_unregister_event(BT_ADAPTER_EVENT);
389         _bt_unregister_event(BT_DEVICE_EVENT);
390         _bt_unregister_event(BT_NETWORK_EVENT);
391         _bt_unregister_event(BT_RFCOMM_CLIENT_EVENT);
392         _bt_unregister_event(BT_RFCOMM_SERVER_EVENT);
393
394         _bt_set_user_data(BT_COMMON, NULL, NULL);
395
396         if (system_conn) {
397                 dbus_g_connection_unref(system_conn);
398                 system_conn = NULL;
399         }
400
401         return BLUETOOTH_ERROR_NONE;
402 }
403