Tizen 2.1 base
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-event-handler.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 <dbus/dbus-glib.h>
22 #include <dbus/dbus-glib-lowlevel.h>
23 #include <dbus/dbus.h>
24 #include <glib.h>
25 #include <dlog.h>
26 #include <vconf.h>
27
28 #include "bluetooth-api.h"
29 #include "bluetooth-audio-api.h"
30 #include "bt-internal-types.h"
31
32 #include "bt-common.h"
33 #include "bt-event-handler.h"
34 #include "bt-request-sender.h"
35
36 typedef struct {
37         int server_fd;
38 } bt_server_info_t;
39
40 typedef struct {
41         int request_id;
42 } bt_sending_info_t;
43
44 static int obex_server_id;
45 static gboolean is_initialized;
46 static GSList *sending_list = NULL;
47 static GSList *server_list = NULL;
48 static GSList *event_list = NULL;
49
50 void _bt_add_push_request_id(int request_id)
51 {
52         bt_sending_info_t *info;
53
54         info = g_new0(bt_sending_info_t, 1);
55         info->request_id = request_id;
56
57         sending_list = g_slist_append(sending_list, info);
58 }
59
60 static gboolean __bt_is_request_id_exist(int request_id)
61 {
62         GSList *l;
63         bt_sending_info_t *info;
64
65         for (l = sending_list; l != NULL; l = g_slist_next(l)) {
66                 info = l->data;
67                 if (info == NULL)
68                         continue;
69
70                 retv_if(info->request_id == request_id, TRUE);
71         }
72
73         return FALSE;
74 }
75
76 static void __bt_remove_push_request_id(int request_id)
77 {
78         GSList *l;
79         bt_sending_info_t *info;
80
81         for (l = sending_list; l != NULL; l = g_slist_next(l)) {
82                 info = l->data;
83                 if (info == NULL)
84                         continue;
85
86                 if (info->request_id == request_id) {
87                         sending_list = g_slist_remove(sending_list, (void *)info);
88                         g_free(info);
89                         break;
90                 }
91         }
92 }
93
94 static void __bt_remove_all_push_request_id(void)
95 {
96         GSList *l;
97         bt_sending_info_t *info;
98
99         for (l = sending_list; l != NULL; l = g_slist_next(l)) {
100                 info = l->data;
101                 g_free(info);
102         }
103
104         g_slist_free(sending_list);
105         sending_list = NULL;
106 }
107
108 static void __bt_remove_all_server(void)
109 {
110         GSList *l;
111         bt_server_info_t *info;
112
113         for (l = server_list; l != NULL; l = g_slist_next(l)) {
114                 info = l->data;
115                 g_free(info);
116         }
117
118         g_slist_free(server_list);
119         server_list = NULL;
120 }
121
122 static gboolean __bt_is_server_exist(int server_fd)
123 {
124         GSList *l;
125         bt_server_info_t *info;
126
127         for (l = server_list; l != NULL; l = g_slist_next(l)) {
128                 info = l->data;
129                 if (info == NULL)
130                         continue;
131
132                 retv_if(info->server_fd == server_fd, TRUE);
133         }
134
135         return FALSE;
136 }
137
138 static void __bt_get_uuid_info(bluetooth_device_info_t *dev_info,
139                                 char **uuids,
140                                 int uuid_count)
141 {
142         int i;
143         char **parts;
144
145         ret_if(dev_info == NULL);
146         ret_if(uuids == NULL);
147         ret_if(uuid_count <= 0);
148
149         dev_info->service_index = uuid_count;
150
151         for (i = 0; uuids[i] != NULL && i < uuid_count; i++) {
152                 g_strlcpy(dev_info->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
153
154                 parts = g_strsplit(uuids[i], "-", -1);
155
156                 if (parts == NULL || parts[0] == NULL) {
157                         g_strfreev(parts);
158                         continue;
159                 }
160
161                 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
162                 g_strfreev(parts);
163         }
164 }
165
166 static bluetooth_device_info_t *__bt_get_device_info_in_message(DBusMessage *msg, int *ret)
167 {
168         bluetooth_device_info_t *dev_info;
169         char *address = NULL;
170         char *name = NULL;
171         char **uuids = NULL;
172         unsigned int class = 0;
173         int rssi = 0;
174         gboolean paired = FALSE;
175         gboolean connected = FALSE;
176         gboolean trust = FALSE;
177         int uuid_count = 0;
178         int result = BLUETOOTH_ERROR_NONE;
179
180         if (!dbus_message_get_args(msg, NULL,
181                 DBUS_TYPE_INT32, &result,
182                 DBUS_TYPE_STRING, &address,
183                 DBUS_TYPE_UINT32, &class,
184                 DBUS_TYPE_INT16, &rssi,
185                 DBUS_TYPE_STRING, &name,
186                 DBUS_TYPE_BOOLEAN, &paired,
187                 DBUS_TYPE_BOOLEAN, &connected,
188                 DBUS_TYPE_BOOLEAN, &trust,
189                 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
190                 &uuids, &uuid_count,
191                 DBUS_TYPE_INVALID)) {
192                 BT_ERR("Unexpected parameters in signal");
193                 return NULL;
194         }
195
196         dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
197
198         dev_info->rssi = rssi;
199         dev_info->paired = paired;
200         dev_info->connected = connected;
201         dev_info->paired = paired;
202         dev_info->trust = trust;
203
204         g_strlcpy(dev_info->device_name.name, name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
205
206         _bt_divide_device_class(&dev_info->device_class, class);
207
208         _bt_convert_addr_string_to_type(dev_info->device_address.addr,
209                                         address);
210
211         if (uuid_count > 0)
212                 __bt_get_uuid_info(dev_info, uuids, uuid_count);
213
214         *ret = result;
215
216         return dev_info;
217 }
218
219 static DBusHandlerResult __bt_adapter_event_filter(DBusConnection *conn,
220                                            DBusMessage *msg, void *data)
221 {
222         bt_event_info_t *event_info;
223         int result = BLUETOOTH_ERROR_NONE;
224         const char *member = dbus_message_get_member(msg);
225
226         event_info = (bt_event_info_t *)data;
227         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
228
229         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
230                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
231
232         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
233                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
234
235         if (!dbus_message_has_path(msg, BT_ADAPTER_PATH))
236                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
237
238
239         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
240
241         if (strcasecmp(member, BT_ENABLED) == 0) {
242                 if (!dbus_message_get_args(msg, NULL,
243                         DBUS_TYPE_INT32, &result,
244                         DBUS_TYPE_INVALID)) {
245                         BT_ERR("Unexpected parameters in signal");
246                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
247                 }
248
249                 if (result == BLUETOOTH_ERROR_NONE) {
250                         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
251                                 BT_ERR("Set vconf failed\n");
252                 }
253
254                 _bt_common_event_cb(BLUETOOTH_EVENT_ENABLED,
255                                 result, NULL,
256                                 event_info->cb, event_info->user_data);
257         } else if (strcasecmp(member, BT_DISABLED) == 0) {
258                 _bt_common_event_cb(BLUETOOTH_EVENT_DISABLED,
259                                 BLUETOOTH_ERROR_NONE, NULL,
260                                 event_info->cb, event_info->user_data);
261
262                 obex_server_id = BT_NO_SERVER;
263                 __bt_remove_all_server();
264                 __bt_remove_all_push_request_id();
265         } else if (strcasecmp(member, BT_DISCOVERABLE_MODE_CHANGED) == 0) {
266                 int mode = 0;
267
268                 if (!dbus_message_get_args(msg, NULL,
269                         DBUS_TYPE_INT32, &result,
270                         DBUS_TYPE_INT16, &mode,
271                         DBUS_TYPE_INVALID)) {
272                         BT_ERR("Unexpected parameters in signal");
273                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
274                 }
275
276                 _bt_common_event_cb(BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
277                                 result, &mode,
278                                 event_info->cb, event_info->user_data);
279         } else if (strcasecmp(member, BT_DISCOVERABLE_TIMEOUT_CHANGED) == 0) {
280                 int timeout = 0;
281
282                 if (!dbus_message_get_args(msg, NULL,
283                         DBUS_TYPE_INT32, &result,
284                         DBUS_TYPE_INT16, &timeout,
285                         DBUS_TYPE_INVALID)) {
286                         BT_ERR("Unexpected parameters in signal");
287                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288                 }
289
290                 _bt_common_event_cb(BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
291                                 result, &timeout,
292                                 event_info->cb, event_info->user_data);
293         } else if (strcasecmp(member, BT_ADAPTER_NAME_CHANGED) == 0) {
294                 char *adapter_name = NULL;
295
296                 if (!dbus_message_get_args(msg, NULL,
297                         DBUS_TYPE_INT32, &result,
298                         DBUS_TYPE_STRING, &adapter_name,
299                         DBUS_TYPE_INVALID)) {
300                         BT_ERR("Unexpected parameters in signal");
301                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
302                 }
303
304                 _bt_common_event_cb(BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
305                                 result, adapter_name,
306                                 event_info->cb, event_info->user_data);
307         } else if (strcasecmp(member, BT_DISCOVERY_STARTED) == 0) {
308                 _bt_common_event_cb(BLUETOOTH_EVENT_DISCOVERY_STARTED,
309                                 BLUETOOTH_ERROR_NONE, NULL,
310                                 event_info->cb, event_info->user_data);
311         } else if (strcasecmp(member, BT_DISCOVERY_FINISHED) == 0) {
312                 if (!dbus_message_get_args(msg, NULL,
313                         DBUS_TYPE_INT32, &result,
314                         DBUS_TYPE_INVALID)) {
315                         BT_ERR("Unexpected parameters in signal");
316                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
317                 }
318
319                 _bt_common_event_cb(BLUETOOTH_EVENT_DISCOVERY_FINISHED,
320                                 result, NULL,
321                                 event_info->cb, event_info->user_data);
322         } else if (strcasecmp(member, BT_DEVICE_FOUND) == 0) {
323                 int event;
324                 bluetooth_device_info_t *device_info;
325
326                 device_info = __bt_get_device_info_in_message(msg, &result);
327                 retv_if(device_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
328
329                 if (strlen(device_info->device_name.name) > 0) {
330                         event = BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED;
331                 } else {
332                         event = BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND;
333                 }
334
335                 _bt_common_event_cb(event,
336                                 result, device_info,
337                                 event_info->cb, event_info->user_data);
338
339                 g_free(device_info);
340         } else if (strcasecmp(member, BT_BOND_CREATED) == 0) {
341                 bluetooth_device_info_t *device_info;
342
343                 device_info = __bt_get_device_info_in_message(msg, &result);
344
345                 _bt_common_event_cb(BLUETOOTH_EVENT_BONDING_FINISHED,
346                                 result, device_info,
347                                 event_info->cb, event_info->user_data);
348
349                 g_free(device_info);
350         } else if (strcasecmp(member, BT_BOND_DESTROYED) == 0) {
351                 char *address = NULL;
352                 bluetooth_device_address_t dev_address = { {0} };
353
354                 if (!dbus_message_get_args(msg, NULL,
355                         DBUS_TYPE_INT32, &result,
356                         DBUS_TYPE_STRING, &address,
357                         DBUS_TYPE_INVALID)) {
358                         BT_ERR("Unexpected parameters in signal");
359                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
360                 }
361
362                 _bt_convert_addr_string_to_type(dev_address.addr,
363                                                 address);
364
365                 _bt_common_event_cb(BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
366                                 result, &dev_address,
367                                 event_info->cb, event_info->user_data);
368         } else if (strcasecmp(member, BT_SERVICE_SEARCHED) == 0) {
369                 bluetooth_device_info_t *device_info;
370                 bt_sdp_info_t sdp_info;
371
372                 device_info = __bt_get_device_info_in_message(msg, &result);
373
374                 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
375
376                 sdp_info.service_index = device_info->service_index;
377
378                 memcpy(&sdp_info.device_addr,
379                         &device_info->device_address,
380                         BLUETOOTH_ADDRESS_LENGTH);
381
382                 memcpy(sdp_info.service_list_array,
383                         device_info->service_list_array,
384                         BLUETOOTH_MAX_SERVICES_FOR_DEVICE);
385
386                 memcpy(sdp_info.uuids,
387                         device_info->uuids,
388                         BLUETOOTH_MAX_SERVICES_FOR_DEVICE * BLUETOOTH_UUID_STRING_MAX);
389
390                 _bt_common_event_cb(BLUETOOTH_EVENT_SERVICE_SEARCHED,
391                                 result, &sdp_info,
392                                 event_info->cb, event_info->user_data);
393
394                 g_free(device_info);
395         }
396
397         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
398 }
399
400 static DBusHandlerResult __bt_device_event_filter(DBusConnection *conn,
401                                            DBusMessage *msg, void *data)
402 {
403         bt_event_info_t *event_info;
404         int result = BLUETOOTH_ERROR_NONE;
405         const char *member = dbus_message_get_member(msg);
406
407         event_info = (bt_event_info_t *)data;
408         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
409
410         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
411                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412
413         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
414                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
415
416         if (!dbus_message_has_path(msg, BT_DEVICE_PATH))
417                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
418
419
420         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
421
422         if (strcasecmp(member, BT_DEVICE_CONNECTED) == 0) {
423                 char *address = NULL;
424                 bluetooth_device_address_t dev_address = { {0} };
425
426                 if (!dbus_message_get_args(msg, NULL,
427                         DBUS_TYPE_INT32, &result,
428                         DBUS_TYPE_STRING, &address,
429                         DBUS_TYPE_INVALID)) {
430                         BT_DBG("Unexpected parameters in signal");
431                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
432                 }
433
434                 _bt_convert_addr_string_to_type(dev_address.addr,
435                                                 address);
436
437                 _bt_common_event_cb(BLUETOOTH_EVENT_DEVICE_CONNECTED,
438                                 result, &dev_address,
439                                 event_info->cb, event_info->user_data);
440         } else if (strcasecmp(member, BT_DEVICE_DISCONNECTED) == 0) {
441                 char *address = NULL;
442                 bluetooth_device_address_t dev_address = { {0} };
443
444                 if (!dbus_message_get_args(msg, NULL,
445                         DBUS_TYPE_INT32, &result,
446                         DBUS_TYPE_STRING, &address,
447                         DBUS_TYPE_INVALID)) {
448                         BT_DBG("Unexpected parameters in signal");
449                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
450                 }
451
452                 _bt_convert_addr_string_to_type(dev_address.addr,
453                                                 address);
454
455                 _bt_common_event_cb(BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
456                                 result, &dev_address,
457                                 event_info->cb, event_info->user_data);
458         }
459
460         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
461 }
462
463 static DBusHandlerResult __bt_hid_event_filter(DBusConnection *conn,
464                                            DBusMessage *msg, void *data)
465 {
466         bt_event_info_t *event_info;
467         int result = BLUETOOTH_ERROR_NONE;
468         const char *member = dbus_message_get_member(msg);
469
470         event_info = (bt_event_info_t *)data;
471         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
472
473         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
474                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
475
476         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
477                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
478
479         if (!dbus_message_has_path(msg, BT_HID_PATH))
480                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
481
482         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
483
484         if (strcasecmp(member, BT_INPUT_CONNECTED) == 0) {
485                 char *address = NULL;
486                 bluetooth_device_address_t dev_address = { {0} };
487
488                 if (!dbus_message_get_args(msg, NULL,
489                         DBUS_TYPE_INT32, &result,
490                         DBUS_TYPE_STRING, &address,
491                         DBUS_TYPE_INVALID)) {
492                         BT_DBG("Unexpected parameters in signal");
493                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
494                 }
495
496                 _bt_convert_addr_string_to_type(dev_address.addr,
497                                                 address);
498
499                 _bt_input_event_cb(BLUETOOTH_HID_CONNECTED,
500                                 result, &dev_address,
501                                 event_info->cb, event_info->user_data);
502         } else if (strcasecmp(member, BT_INPUT_DISCONNECTED) == 0) {
503                 char *address = NULL;
504                 bluetooth_device_address_t dev_address = { {0} };
505
506                 if (!dbus_message_get_args(msg, NULL,
507                         DBUS_TYPE_INT32, &result,
508                         DBUS_TYPE_STRING, &address,
509                         DBUS_TYPE_INVALID)) {
510                         BT_DBG("Unexpected parameters in signal");
511                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
512                 }
513
514                 BT_DBG("address: %s", address);
515
516                 _bt_convert_addr_string_to_type(dev_address.addr,
517                                                 address);
518
519                 _bt_input_event_cb(BLUETOOTH_HID_DISCONNECTED,
520                                 result, &dev_address,
521                                 event_info->cb, event_info->user_data);
522         }
523
524         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
525 }
526
527 static DBusHandlerResult __bt_headset_event_filter(DBusConnection *conn,
528                                            DBusMessage *msg, void *data)
529 {
530         bt_event_info_t *event_info;
531         int result = BLUETOOTH_ERROR_NONE;
532         const char *member = dbus_message_get_member(msg);
533
534         event_info = (bt_event_info_t *)data;
535         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
536
537         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
538                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
539
540         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
541                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
542
543         if (!dbus_message_has_path(msg, BT_HEADSET_PATH))
544                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545
546         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
547
548         if (strcasecmp(member, BT_HEADSET_CONNECTED) == 0) {
549                 char *address = NULL;
550
551                 if (!dbus_message_get_args(msg, NULL,
552                         DBUS_TYPE_INT32, &result,
553                         DBUS_TYPE_STRING, &address,
554                         DBUS_TYPE_INVALID)) {
555                         BT_ERR("Unexpected parameters in signal");
556                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
557                 }
558
559                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_CONNECTED,
560                                 result, address,
561                                 event_info->cb, event_info->user_data);
562         } else if (strcasecmp(member, BT_HEADSET_DISCONNECTED) == 0) {
563                 char *address = NULL;
564
565                 if (!dbus_message_get_args(msg, NULL,
566                         DBUS_TYPE_INT32, &result,
567                         DBUS_TYPE_STRING, &address,
568                         DBUS_TYPE_INVALID)) {
569                         BT_ERR("Unexpected parameters in signal");
570                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
571                 }
572
573                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_DISCONNECTED,
574                                 result, address,
575                                 event_info->cb, event_info->user_data);
576         } else if (strcasecmp(member, BT_STEREO_HEADSET_CONNECTED) == 0) {
577                 char *address = NULL;
578
579                 if (!dbus_message_get_args(msg, NULL,
580                         DBUS_TYPE_INT32, &result,
581                         DBUS_TYPE_STRING, &address,
582                         DBUS_TYPE_INVALID)) {
583                         BT_ERR("Unexpected parameters in signal");
584                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
585                 }
586
587                 _bt_headset_event_cb(BLUETOOTH_EVENT_AV_CONNECTED,
588                                 result, address,
589                                 event_info->cb, event_info->user_data);
590         } else if (strcasecmp(member, BT_STEREO_HEADSET_DISCONNECTED) == 0) {
591                 char *address = NULL;
592
593                 if (!dbus_message_get_args(msg, NULL,
594                         DBUS_TYPE_INT32, &result,
595                         DBUS_TYPE_STRING, &address,
596                         DBUS_TYPE_INVALID)) {
597                         BT_ERR("Unexpected parameters in signal");
598                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
599                 }
600
601                 _bt_headset_event_cb(BLUETOOTH_EVENT_AV_DISCONNECTED,
602                                 result, address,
603                                 event_info->cb, event_info->user_data);
604         } else if (strcasecmp(member, BT_SCO_CONNECTED) == 0) {
605                 char *address = NULL;
606                 bluetooth_device_address_t dev_address = { {0} };
607
608                 if (!dbus_message_get_args(msg, NULL,
609                         DBUS_TYPE_INT32, &result,
610                         DBUS_TYPE_STRING, &address,
611                         DBUS_TYPE_INVALID)) {
612                         BT_ERR("Unexpected parameters in signal");
613                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
614                 }
615
616                 _bt_convert_addr_string_to_type(dev_address.addr,
617                                                 address);
618
619                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_AUDIO_CONNECTED,
620                                 result, &dev_address,
621                                 event_info->cb, event_info->user_data);
622         } else if (strcasecmp(member, BT_SCO_DISCONNECTED) == 0) {
623                 char *address = NULL;
624                 bluetooth_device_address_t dev_address = { {0} };
625
626                 if (!dbus_message_get_args(msg, NULL,
627                         DBUS_TYPE_INT32, &result,
628                         DBUS_TYPE_STRING, &address,
629                         DBUS_TYPE_INVALID)) {
630                         BT_ERR("Unexpected parameters in signal");
631                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
632                 }
633
634                 _bt_convert_addr_string_to_type(dev_address.addr,
635                                                 address);
636
637                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_AUDIO_DISCONNECTED,
638                                 result, &dev_address,
639                                 event_info->cb, event_info->user_data);
640         } else if (strcasecmp(member, BT_SPEAKER_GAIN) == 0) {
641                 unsigned int gain;
642                 guint16 spkr_gain;
643                 char *address = NULL;
644
645                 if (!dbus_message_get_args(msg, NULL,
646                         DBUS_TYPE_INT32, &result,
647                         DBUS_TYPE_STRING, &address,
648                         DBUS_TYPE_UINT16, &spkr_gain,
649                         DBUS_TYPE_INVALID)) {
650                         BT_ERR("Unexpected parameters in signal");
651                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
652                 }
653                 gain = (unsigned int)spkr_gain;
654
655                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_SPEAKER_GAIN,
656                                 result, &gain,
657                                 event_info->cb, event_info->user_data);
658         } else if (strcasecmp(member, BT_MICROPHONE_GAIN) == 0) {
659                 unsigned int gain;
660                 guint16 mic_gain;
661                 char *address = NULL;
662
663                 if (!dbus_message_get_args(msg, NULL,
664                         DBUS_TYPE_INT32, &result,
665                         DBUS_TYPE_STRING, &address,
666                         DBUS_TYPE_UINT16, &mic_gain,
667                         DBUS_TYPE_INVALID)) {
668                         BT_ERR("Unexpected parameters in signal");
669                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
670                 }
671                 gain = (unsigned int)mic_gain;
672
673                 _bt_headset_event_cb(BLUETOOTH_EVENT_AG_MIC_GAIN,
674                                 result, &gain,
675                                 event_info->cb, event_info->user_data);
676         }
677
678         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
679 }
680
681 static DBusHandlerResult __bt_network_event_filter(DBusConnection *conn,
682                                            DBusMessage *msg, void *data)
683 {
684         bt_event_info_t *event_info;
685         int result = BLUETOOTH_ERROR_NONE;
686         const char *member = dbus_message_get_member(msg);
687
688         event_info = (bt_event_info_t *)data;
689         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
690
691         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
692                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
693
694         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
695                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
696
697         if (!dbus_message_has_path(msg, BT_NETWORK_PATH))
698                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
699
700         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
701
702         if (strcasecmp(member, BT_NETWORK_CONNECTED) == 0) {
703                 char *address = NULL;
704                 bluetooth_device_address_t dev_address = { {0} };
705
706                 if (!dbus_message_get_args(msg, NULL,
707                         DBUS_TYPE_INT32, &result,
708                         DBUS_TYPE_STRING, &address,
709                         DBUS_TYPE_INVALID)) {
710                         BT_ERR("Unexpected parameters in signal");
711                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
712                 }
713
714                 _bt_convert_addr_string_to_type(dev_address.addr,
715                                                 address);
716
717                 _bt_common_event_cb(BLUETOOTH_EVENT_NETWORK_CONNECTED,
718                                 result, &dev_address,
719                                 event_info->cb, event_info->user_data);
720         } else if (strcasecmp(member, BT_NETWORK_DISCONNECTED) == 0) {
721                 char *address = NULL;
722                 bluetooth_device_address_t dev_address = { {0} };
723
724                 if (!dbus_message_get_args(msg, NULL,
725                         DBUS_TYPE_INT32, &result,
726                         DBUS_TYPE_STRING, &address,
727                         DBUS_TYPE_INVALID)) {
728                         BT_ERR("Unexpected parameters in signal");
729                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
730                 }
731
732                 _bt_convert_addr_string_to_type(dev_address.addr,
733                                                 address);
734
735                 _bt_common_event_cb(BLUETOOTH_EVENT_NETWORK_DISCONNECTED,
736                                 result, &dev_address,
737                                 event_info->cb, event_info->user_data);
738         } else if (strcasecmp(member, BT_NETWORK_SERVER_CONNECTED) == 0) {
739                 char *device = NULL;
740                 char *address = NULL;
741                 bluetooth_network_device_info_t network_info;
742
743                 if (!dbus_message_get_args(msg, NULL,
744                         DBUS_TYPE_INT32, &result,
745                         DBUS_TYPE_STRING, &device,
746                         DBUS_TYPE_STRING, &address,
747                         DBUS_TYPE_INVALID)) {
748                         BT_ERR("Unexpected parameters in signal");
749                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
750                 }
751
752                 memset(&network_info, 0x00, sizeof(bluetooth_network_device_info_t));
753
754                 _bt_convert_addr_string_to_type(network_info.device_address.addr,
755                                                 address);
756
757                 _bt_print_device_address_t(&network_info.device_address);
758                 g_strlcpy(network_info.interface_name, device, BLUETOOTH_INTERFACE_NAME_LENGTH);
759
760                 BT_DBG("name: %s", network_info.interface_name);
761
762                 _bt_common_event_cb(BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
763                                 result, &network_info,
764                                 event_info->cb, event_info->user_data);
765         } else if (strcasecmp(member, BT_NETWORK_SERVER_DISCONNECTED) == 0) {
766                 char *device = NULL;
767                 char *address = NULL;
768                 bluetooth_network_device_info_t network_info;
769
770                 if (!dbus_message_get_args(msg, NULL,
771                         DBUS_TYPE_INT32, &result,
772                         DBUS_TYPE_STRING, &device,
773                         DBUS_TYPE_STRING, &address,
774                         DBUS_TYPE_INVALID)) {
775                         BT_ERR("Unexpected parameters in signal");
776                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
777                 }
778
779                 memset(&network_info, 0x00, sizeof(bluetooth_network_device_info_t));
780
781                 _bt_convert_addr_string_to_type(network_info.device_address.addr,
782                                                 address);
783
784                 _bt_print_device_address_t(&network_info.device_address);
785
786                 _bt_common_event_cb(BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
787                                 result, &network_info,
788                                 event_info->cb, event_info->user_data);
789         }
790
791
792         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
793 }
794
795 static DBusHandlerResult __bt_avrcp_event_filter(DBusConnection *conn,
796                                            DBusMessage *msg, void *data)
797 {
798         bt_event_info_t *event_info;
799         int result = BLUETOOTH_ERROR_NONE;
800         const char *member = dbus_message_get_member(msg);
801
802         event_info = (bt_event_info_t *)data;
803         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
804
805         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
806                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
807
808         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
809                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
810
811         if (!dbus_message_has_path(msg, BT_AVRCP_PATH))
812                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
813
814         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
815
816         if (strcasecmp(member, BT_STEREO_HEADSET_CONNECTED) == 0) {
817                 char *address = NULL;
818
819                 if (!dbus_message_get_args(msg, NULL,
820                         DBUS_TYPE_INT32, &result,
821                         DBUS_TYPE_STRING, &address,
822                         DBUS_TYPE_INVALID)) {
823                         BT_ERR("Unexpected parameters in signal");
824                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
825                 }
826
827                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_CONNECTED,
828                                 result, address,
829                                 event_info->cb, event_info->user_data);
830         } else if (strcasecmp(member, BT_STEREO_HEADSET_DISCONNECTED) == 0) {
831                 char *address = NULL;
832
833                 if (!dbus_message_get_args(msg, NULL,
834                         DBUS_TYPE_INT32, &result,
835                         DBUS_TYPE_STRING, &address,
836                         DBUS_TYPE_INVALID)) {
837                         BT_ERR("Unexpected parameters in signal");
838                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
839                 }
840
841                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_DISCONNECTED,
842                                 result, address,
843                                 event_info->cb, event_info->user_data);
844         } else if (strcasecmp(member, BT_MEDIA_SHUFFLE_STATUS) == 0) {
845                 unsigned int status;
846                 if (!dbus_message_get_args(msg, NULL,
847                         DBUS_TYPE_UINT32, &status,
848                         DBUS_TYPE_INVALID)) {
849                         BT_ERR("Unexpected parameters in signal");
850                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
851                 }
852                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_SETTING_SHUFFLE_STATUS,
853                                 result, &status,
854                                 event_info->cb, event_info->user_data);
855         } else if (strcasecmp(member, BT_MEDIA_EQUALIZER_STATUS) == 0) {
856                 unsigned int status;
857                 if (!dbus_message_get_args(msg, NULL,
858                         DBUS_TYPE_UINT32, &status,
859                         DBUS_TYPE_INVALID)) {
860                         BT_ERR("Unexpected parameters in signal");
861                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
862                 }
863                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_SETTING_EQUALIZER_STATUS,
864                                 result, &status,
865                                 event_info->cb, event_info->user_data);
866         } else if (strcasecmp(member, BT_MEDIA_REPEAT_STATUS) == 0) {
867                 unsigned int status;
868                 if (!dbus_message_get_args(msg, NULL,
869                         DBUS_TYPE_UINT32, &status,
870                         DBUS_TYPE_INVALID)) {
871                         BT_ERR("Unexpected parameters in signal");
872                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
873                 }
874                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_SETTING_REPEAT_STATUS,
875                                 result, &status,
876                                 event_info->cb, event_info->user_data);
877         }  else if (strcasecmp(member, BT_MEDIA_SCAN_STATUS) == 0) {
878                 unsigned int status;
879                 if (!dbus_message_get_args(msg, NULL,
880                         DBUS_TYPE_UINT32, &status,
881                         DBUS_TYPE_INVALID)) {
882                         BT_ERR("Unexpected parameters in signal");
883                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
884                 }
885                 _bt_avrcp_event_cb(BLUETOOTH_EVENT_AVRCP_SETTING_SCAN_STATUS,
886                                 result, &status,
887                                 event_info->cb, event_info->user_data);
888         }
889
890         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
891 }
892
893 static DBusHandlerResult __bt_opp_client_event_filter(DBusConnection *conn,
894                                            DBusMessage *msg, void *data)
895 {
896         bt_event_info_t *event_info;
897         int result = BLUETOOTH_ERROR_NONE;
898         const char *member = dbus_message_get_member(msg);
899
900         event_info = (bt_event_info_t *)data;
901         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
902
903         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
904                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
905
906         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
907                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
908
909         if (!dbus_message_has_path(msg, BT_OPP_CLIENT_PATH))
910                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
911
912         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
913
914         if (strcasecmp(member, BT_OPP_CONNECTED) == 0) {
915                 char *address = NULL;
916                 int request_id = 0;
917                 bluetooth_device_address_t dev_address = { {0} };
918
919                 if (!dbus_message_get_args(msg, NULL,
920                         DBUS_TYPE_INT32, &result,
921                         DBUS_TYPE_STRING, &address,
922                         DBUS_TYPE_INT32, &request_id,
923                         DBUS_TYPE_INVALID)) {
924                         BT_ERR("Unexpected parameters in signal");
925                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
926                 }
927
928                 if (__bt_is_request_id_exist(request_id) == FALSE) {
929                         BT_ERR("Different request id!");
930                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
931                 }
932
933                 _bt_convert_addr_string_to_type(dev_address.addr,
934                                                 address);
935
936                 _bt_common_event_cb(BLUETOOTH_EVENT_OPC_CONNECTED,
937                                 result, &dev_address,
938                                 event_info->cb, event_info->user_data);
939
940                 if (result != BLUETOOTH_ERROR_NONE) {
941                         __bt_remove_push_request_id(request_id);
942                 }
943         } else if (strcasecmp(member, BT_OPP_DISCONNECTED) == 0) {
944                 char *address = NULL;
945                 int request_id = 0;
946                 bluetooth_device_address_t dev_address = { {0} };
947
948                 if (!dbus_message_get_args(msg, NULL,
949                         DBUS_TYPE_INT32, &result,
950                         DBUS_TYPE_STRING, &address,
951                         DBUS_TYPE_INT32, &request_id,
952                         DBUS_TYPE_INVALID)) {
953                         BT_ERR("Unexpected parameters in signal");
954                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
955                 }
956
957                 if (__bt_is_request_id_exist(request_id) == FALSE) {
958                         BT_ERR("Different request id!");
959                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
960                 }
961
962                 _bt_convert_addr_string_to_type(dev_address.addr,
963                                                 address);
964
965                 _bt_common_event_cb(BLUETOOTH_EVENT_OPC_DISCONNECTED,
966                                 result, &dev_address,
967                                 event_info->cb, event_info->user_data);
968
969                 __bt_remove_push_request_id(request_id);
970         } else if (strcasecmp(member, BT_TRANSFER_STARTED) == 0) {
971                 char *file_name = NULL;
972                 int request_id = 0;
973                 guint64 size = 0;
974                 bt_opc_transfer_info_t transfer_info;
975
976                 if (!dbus_message_get_args(msg, NULL,
977                         DBUS_TYPE_INT32, &result,
978                         DBUS_TYPE_STRING, &file_name,
979                         DBUS_TYPE_UINT64, &size,
980                         DBUS_TYPE_INT32, &request_id,
981                         DBUS_TYPE_INVALID)) {
982                         BT_ERR("Unexpected parameters in signal");
983                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
984                 }
985
986                 if (__bt_is_request_id_exist(request_id) == FALSE) {
987                         BT_ERR("Different request id!");
988                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
989                 }
990
991                 memset(&transfer_info, 0x00, sizeof(bt_opc_transfer_info_t));
992
993                 transfer_info.filename = g_strdup(file_name);
994                 transfer_info.size = size;
995
996                 _bt_common_event_cb(BLUETOOTH_EVENT_OPC_TRANSFER_STARTED,
997                                 result, &transfer_info,
998                                 event_info->cb, event_info->user_data);
999
1000                 g_free(transfer_info.filename);
1001         } else if (strcasecmp(member, BT_TRANSFER_PROGRESS) == 0) {
1002                 char *file_name = NULL;
1003                 int request_id = 0;
1004                 guint64 size = 0;
1005                 int progress = 0;
1006                 bt_opc_transfer_info_t transfer_info;
1007
1008                 if (!dbus_message_get_args(msg, NULL,
1009                         DBUS_TYPE_INT32, &result,
1010                         DBUS_TYPE_STRING, &file_name,
1011                         DBUS_TYPE_UINT64, &size,
1012                         DBUS_TYPE_INT32, &progress,
1013                         DBUS_TYPE_INT32, &request_id,
1014                         DBUS_TYPE_INVALID)) {
1015                         BT_ERR("Unexpected parameters in signal");
1016                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1017                 }
1018
1019                 if (__bt_is_request_id_exist(request_id) == FALSE) {
1020                         BT_ERR("Different request id!");
1021                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1022                 }
1023
1024                 memset(&transfer_info, 0x00, sizeof(bt_opc_transfer_info_t));
1025
1026                 transfer_info.filename = g_strdup(file_name);
1027                 transfer_info.size = size;
1028                 transfer_info.percentage = progress;
1029
1030                 _bt_common_event_cb(BLUETOOTH_EVENT_OPC_TRANSFER_PROGRESS,
1031                                 result, &transfer_info,
1032                                 event_info->cb, event_info->user_data);
1033
1034                 g_free(transfer_info.filename);
1035         } else if (strcasecmp(member, BT_TRANSFER_COMPLETED) == 0) {
1036                 char *file_name = NULL;
1037                 int request_id = 0;
1038                 guint64 size = 0;
1039                 bt_opc_transfer_info_t transfer_info;
1040
1041                 if (!dbus_message_get_args(msg, NULL,
1042                         DBUS_TYPE_INT32, &result,
1043                         DBUS_TYPE_STRING, &file_name,
1044                         DBUS_TYPE_UINT64, &size,
1045                         DBUS_TYPE_INT32, &request_id,
1046                         DBUS_TYPE_INVALID)) {
1047                         BT_ERR("Unexpected parameters in signal");
1048                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1049                 }
1050
1051                 if (__bt_is_request_id_exist(request_id) == FALSE) {
1052                         BT_ERR("Different request id!");
1053                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1054                 }
1055
1056                 memset(&transfer_info, 0x00, sizeof(bt_opc_transfer_info_t));
1057
1058                 transfer_info.filename = g_strdup(file_name);
1059                 transfer_info.size = size;
1060
1061                 _bt_common_event_cb(BLUETOOTH_EVENT_OPC_TRANSFER_COMPLETE,
1062                                 result, &transfer_info,
1063                                 event_info->cb, event_info->user_data);
1064
1065                 g_free(transfer_info.filename);
1066         }
1067
1068         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1069 }
1070
1071 static DBusHandlerResult __bt_opp_server_event_filter(DBusConnection *conn,
1072                                            DBusMessage *msg, void *data)
1073 {
1074         bt_event_info_t *event_info;
1075         int result = BLUETOOTH_ERROR_NONE;
1076         const char *member = dbus_message_get_member(msg);
1077
1078         event_info = (bt_event_info_t *)data;
1079         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1080
1081         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
1082                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1083
1084         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
1085                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1086
1087         if (!dbus_message_has_path(msg, BT_OPP_SERVER_PATH))
1088                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1089
1090         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1091
1092         if (strcasecmp(member, BT_TRANSFER_AUTHORIZED) == 0) {
1093                 /* Native only event */
1094                 char *file_name = NULL;
1095                 guint64 size = 0;
1096                 bt_obex_server_authorize_into_t auth_info;
1097
1098                 if (!dbus_message_get_args(msg, NULL,
1099                         DBUS_TYPE_INT32, &result,
1100                         DBUS_TYPE_STRING, &file_name,
1101                         DBUS_TYPE_UINT64, &size,
1102                         DBUS_TYPE_INVALID)) {
1103                         BT_ERR("Unexpected parameters in signal");
1104                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1105                 }
1106
1107                 /* OSP server: Don't get this event */
1108                 retv_if(obex_server_id == BT_CUSTOM_SERVER,
1109                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1110
1111                 memset(&auth_info, 0x00, sizeof(bt_obex_server_authorize_into_t));
1112
1113                 auth_info.filename = g_strdup(file_name);
1114                 auth_info.length = size;
1115
1116                 _bt_common_event_cb(BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE,
1117                                 result, &auth_info,
1118                                 event_info->cb, event_info->user_data);
1119
1120                 g_free(auth_info.filename);
1121         } else if (strcasecmp(member, BT_CONNECTION_AUTHORIZED) == 0) {
1122                 /* OSP only event */
1123                 char *address = NULL;
1124                 char *name = NULL;
1125                 bluetooth_device_address_t dev_address = { {0} };
1126
1127                 if (!dbus_message_get_args(msg, NULL,
1128                         DBUS_TYPE_INT32, &result,
1129                         DBUS_TYPE_STRING, &address,
1130                         DBUS_TYPE_STRING, &name,
1131                         DBUS_TYPE_INVALID)) {
1132                         BT_ERR("Unexpected parameters in signal");
1133                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1134                 }
1135
1136                 /* Native server: Don't get this event */
1137                 retv_if(obex_server_id == BT_NATIVE_SERVER,
1138                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1139
1140                 _bt_convert_addr_string_to_type(dev_address.addr,
1141                                                 address);
1142
1143                 _bt_common_event_cb(BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
1144                                 result, &dev_address,
1145                                 event_info->cb, event_info->user_data);
1146         } else if (strcasecmp(member, BT_TRANSFER_STARTED) == 0) {
1147                 char *file_name = NULL;
1148                 char *type = NULL;
1149                 int transfer_id = 0;
1150                 int server_type = 0; /* bt_server_type_t */
1151                 guint64 size = 0;
1152                 bt_obex_server_transfer_info_t transfer_info;
1153
1154                 if (!dbus_message_get_args(msg, NULL,
1155                         DBUS_TYPE_INT32, &result,
1156                         DBUS_TYPE_STRING, &file_name,
1157                         DBUS_TYPE_STRING, &type,
1158                         DBUS_TYPE_UINT64, &size,
1159                         DBUS_TYPE_INT32, &transfer_id,
1160                         DBUS_TYPE_INT32, &server_type,
1161                         DBUS_TYPE_INVALID)) {
1162                         BT_ERR("Unexpected parameters in signal");
1163                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1164                 }
1165
1166                 /* Other server's event */
1167                 retv_if(obex_server_id != server_type &&
1168                         server_type != BT_FTP_SERVER,
1169                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1170
1171                 memset(&transfer_info, 0x00, sizeof(bt_obex_server_transfer_info_t));
1172
1173                 transfer_info.filename = g_strdup(file_name);
1174                 transfer_info.type = g_strdup(type);
1175                 transfer_info.file_size = size;
1176                 transfer_info.transfer_id = transfer_id;
1177
1178                 _bt_common_event_cb(BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
1179                                 result, &transfer_info,
1180                                 event_info->cb, event_info->user_data);
1181
1182                 g_free(transfer_info.filename);
1183                 g_free(transfer_info.type);
1184         } else if (strcasecmp(member, BT_TRANSFER_PROGRESS) == 0) {
1185                 char *file_name = NULL;
1186                 char *type = NULL;
1187                 int transfer_id = 0;
1188                 int progress = 0;
1189                 int server_type = 0; /* bt_server_type_t */
1190                 guint64 size = 0;
1191                 bt_obex_server_transfer_info_t transfer_info;
1192
1193                 if (!dbus_message_get_args(msg, NULL,
1194                         DBUS_TYPE_INT32, &result,
1195                         DBUS_TYPE_STRING, &file_name,
1196                         DBUS_TYPE_STRING, &type,
1197                         DBUS_TYPE_UINT64, &size,
1198                         DBUS_TYPE_INT32, &transfer_id,
1199                         DBUS_TYPE_INT32, &progress,
1200                         DBUS_TYPE_INT32, &server_type,
1201                         DBUS_TYPE_INVALID)) {
1202                         BT_ERR("Unexpected parameters in signal");
1203                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1204                 }
1205
1206                 /* Other server's event */
1207                 retv_if(obex_server_id != server_type &&
1208                         server_type != BT_FTP_SERVER,
1209                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1210
1211                 memset(&transfer_info, 0x00, sizeof(bt_obex_server_transfer_info_t));
1212
1213                 transfer_info.filename = g_strdup(file_name);
1214                 transfer_info.type = g_strdup(type);
1215                 transfer_info.file_size = size;
1216                 transfer_info.transfer_id = transfer_id;
1217                 transfer_info.percentage = progress;
1218
1219                 _bt_common_event_cb(BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS,
1220                                 result, &transfer_info,
1221                                 event_info->cb, event_info->user_data);
1222
1223                 g_free(transfer_info.filename);
1224                 g_free(transfer_info.type);
1225         } else if (strcasecmp(member, BT_TRANSFER_COMPLETED) == 0) {
1226                 char *file_name = NULL;
1227                 char *device_name = NULL;
1228                 char *type = NULL;
1229                 int transfer_id = 0;
1230                 int server_type = 0; /* bt_server_type_t */
1231                 guint64 size = 0;
1232                 bt_obex_server_transfer_info_t transfer_info;
1233
1234                 if (!dbus_message_get_args(msg, NULL,
1235                         DBUS_TYPE_INT32, &result,
1236                         DBUS_TYPE_STRING, &file_name,
1237                         DBUS_TYPE_STRING, &type,
1238                         DBUS_TYPE_STRING, &device_name,
1239                         DBUS_TYPE_UINT64, &size,
1240                         DBUS_TYPE_INT32, &transfer_id,
1241                         DBUS_TYPE_INT32, &server_type,
1242                         DBUS_TYPE_INVALID)) {
1243                         BT_ERR("Unexpected parameters in signal");
1244                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1245                 }
1246
1247                 /* Other server's event */
1248                 retv_if(obex_server_id != server_type &&
1249                         server_type != BT_FTP_SERVER,
1250                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1251
1252                 memset(&transfer_info, 0x00, sizeof(bt_obex_server_transfer_info_t));
1253
1254                 transfer_info.filename = g_strdup(file_name);
1255                 transfer_info.type = g_strdup(type);
1256                 transfer_info.device_name = g_strdup(device_name);
1257                 transfer_info.file_size = size;
1258                 transfer_info.transfer_id = transfer_id;
1259
1260                 _bt_common_event_cb(BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
1261                                 result, &transfer_info,
1262                                 event_info->cb, event_info->user_data);
1263
1264                 g_free(transfer_info.filename);
1265                 g_free(transfer_info.type);
1266                 g_free(transfer_info.device_name);
1267         }
1268
1269         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1270 }
1271
1272 static DBusHandlerResult __bt_rfcomm_client_event_filter(DBusConnection *conn,
1273                                            DBusMessage *msg, void *data)
1274 {
1275         bt_event_info_t *event_info;
1276         int result = BLUETOOTH_ERROR_NONE;
1277         const char *member = dbus_message_get_member(msg);
1278
1279         event_info = (bt_event_info_t *)data;
1280         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1281
1282         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
1283                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1284
1285         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
1286                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1287
1288         if (!dbus_message_has_path(msg, BT_RFCOMM_CLIENT_PATH))
1289                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1290
1291         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1292
1293         if (strcasecmp(member, BT_RFCOMM_CONNECTED) == 0) {
1294                 char *address = NULL;
1295                 char *uuid = NULL;
1296                 int socket_fd = 0;
1297                 bluetooth_rfcomm_connection_t conn_info;
1298
1299                 if (!dbus_message_get_args(msg, NULL,
1300                         DBUS_TYPE_INT32, &result,
1301                         DBUS_TYPE_STRING, &address,
1302                         DBUS_TYPE_STRING, &uuid,
1303                         DBUS_TYPE_INT16, &socket_fd,
1304                         DBUS_TYPE_INVALID)) {
1305                         BT_ERR("Unexpected parameters in signal");
1306                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1307                 }
1308
1309                 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
1310                 conn_info.device_role = RFCOMM_ROLE_CLIENT;
1311                 g_strlcpy(conn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1312                 conn_info.socket_fd = socket_fd;
1313                 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
1314                                                 address);
1315
1316                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
1317                                 result, &conn_info,
1318                                 event_info->cb, event_info->user_data);
1319         } else if (strcasecmp(member, BT_RFCOMM_DISCONNECTED) == 0) {
1320                 char *address = NULL;
1321                 char *uuid = NULL;
1322                 int socket_fd = 0;
1323                 bluetooth_rfcomm_disconnection_t disconn_info;
1324
1325                 if (!dbus_message_get_args(msg, NULL,
1326                         DBUS_TYPE_INT32, &result,
1327                         DBUS_TYPE_STRING, &address,
1328                         DBUS_TYPE_STRING, &uuid,
1329                         DBUS_TYPE_INT16, &socket_fd,
1330                         DBUS_TYPE_INVALID)) {
1331                         BT_ERR("Unexpected parameters in signal");
1332                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1333                 }
1334
1335                 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
1336                 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
1337                 g_strlcpy(disconn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1338                 disconn_info.socket_fd = socket_fd;
1339                 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
1340                                                 address);
1341
1342                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
1343                                 result, &disconn_info,
1344                                 event_info->cb, event_info->user_data);
1345         } else if (strcasecmp(member, BT_RFCOMM_DATA_RECEIVED) == 0) {
1346                 char *buffer = NULL;
1347                 int buffer_len = 0;
1348                 int socket_fd = 0;
1349                 bluetooth_rfcomm_received_data_t data_r;
1350
1351                 if (!dbus_message_get_args(msg, NULL,
1352                         DBUS_TYPE_INT32, &result,
1353                         DBUS_TYPE_INT16, &socket_fd,
1354                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
1355                         &buffer, &buffer_len,
1356                         DBUS_TYPE_INVALID)) {
1357                         BT_ERR("Unexpected parameters in signal");
1358                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1359                 }
1360
1361                 data_r.socket_fd = socket_fd;
1362                 data_r.buffer_size = buffer_len;
1363                 data_r.buffer = g_memdup(buffer, buffer_len);
1364
1365                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
1366                                 result, &data_r,
1367                                 event_info->cb, event_info->user_data);
1368
1369                 g_free(data_r.buffer);
1370         }
1371
1372         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1373 }
1374
1375 static DBusHandlerResult __bt_rfcomm_server_event_filter(DBusConnection *conn,
1376                                            DBusMessage *msg, void *data)
1377 {
1378         bt_event_info_t *event_info;
1379         int result = BLUETOOTH_ERROR_NONE;
1380         const char *member = dbus_message_get_member(msg);
1381
1382         event_info = (bt_event_info_t *)data;
1383         retv_if(event_info == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1384
1385         if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
1386                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1387
1388         if (!dbus_message_has_interface(msg, BT_EVENT_SERVICE))
1389                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1390
1391         if (!dbus_message_has_path(msg, BT_RFCOMM_SERVER_PATH))
1392                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1393
1394         retv_if(member == NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1395
1396         if (strcasecmp(member, BT_RFCOMM_CONNECTED) == 0) {
1397                 char *address = NULL;
1398                 char *uuid = NULL;
1399                 int socket_fd = 0;
1400                 bluetooth_rfcomm_connection_t conn_info;
1401
1402                 if (!dbus_message_get_args(msg, NULL,
1403                         DBUS_TYPE_INT32, &result,
1404                         DBUS_TYPE_STRING, &address,
1405                         DBUS_TYPE_STRING, &uuid,
1406                         DBUS_TYPE_INT16, &socket_fd,
1407                         DBUS_TYPE_INVALID)) {
1408                         BT_ERR("Unexpected parameters in signal");
1409                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1410                 }
1411
1412                 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
1413                 conn_info.device_role = RFCOMM_ROLE_SERVER;
1414                 g_strlcpy(conn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1415                 conn_info.socket_fd = socket_fd;
1416                 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
1417                                                 address);
1418
1419                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
1420                                 result, &conn_info,
1421                                 event_info->cb, event_info->user_data);
1422         } else if (strcasecmp(member, BT_RFCOMM_DISCONNECTED) == 0) {
1423                 char *address = NULL;
1424                 char *uuid = NULL;
1425                 int socket_fd = 0;
1426                 bluetooth_rfcomm_disconnection_t disconn_info;
1427
1428                 if (!dbus_message_get_args(msg, NULL,
1429                         DBUS_TYPE_INT32, &result,
1430                         DBUS_TYPE_STRING, &address,
1431                         DBUS_TYPE_STRING, &uuid,
1432                         DBUS_TYPE_INT16, &socket_fd,
1433                         DBUS_TYPE_INVALID)) {
1434                         BT_ERR("Unexpected parameters in signal");
1435                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1436                 }
1437
1438                 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
1439                 disconn_info.device_role = RFCOMM_ROLE_SERVER;
1440                 g_strlcpy(disconn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1441                 disconn_info.socket_fd = socket_fd;
1442                 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
1443                                                 address);
1444
1445                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
1446                                 result, &disconn_info,
1447                                 event_info->cb, event_info->user_data);
1448         } else if (strcasecmp(member, BT_CONNECTION_AUTHORIZED) == 0) {
1449                 /* OSP only event */
1450                 bluetooth_rfcomm_connection_request_t req_ind;
1451                 char *address = NULL;
1452                 char *uuid = NULL;
1453                 char *name = NULL;
1454                 int socket_fd = 0;
1455
1456                 if (!dbus_message_get_args(msg, NULL,
1457                         DBUS_TYPE_INT32, &result,
1458                         DBUS_TYPE_STRING, &address,
1459                         DBUS_TYPE_STRING, &uuid,
1460                         DBUS_TYPE_STRING, &name,
1461                         DBUS_TYPE_INT16, &socket_fd,
1462                         DBUS_TYPE_INVALID)) {
1463                         BT_ERR("Unexpected parameters in signal");
1464                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1465                 }
1466
1467                 /* Don't send the authorized event to other server */
1468                 retv_if(__bt_is_server_exist(socket_fd) == FALSE,
1469                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1470
1471                 memset(&req_ind, 0x00, sizeof(bluetooth_rfcomm_connection_request_t));
1472                 _bt_convert_addr_string_to_type(req_ind.device_addr.addr,
1473                                                 address);
1474
1475                 req_ind.socket_fd = socket_fd;
1476
1477                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
1478                                 result, &req_ind,
1479                                 event_info->cb, event_info->user_data);
1480         } else if (strcasecmp(member, BT_RFCOMM_SERVER_REMOVED) == 0) {
1481                 /* OSP only event */
1482                 int socket_fd = 0;
1483
1484                 if (!dbus_message_get_args(msg, NULL,
1485                         DBUS_TYPE_INT32, &result,
1486                         DBUS_TYPE_INT16, &socket_fd,
1487                         DBUS_TYPE_INVALID)) {
1488                         BT_ERR("Unexpected parameters in signal");
1489                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1490                 }
1491
1492                 retv_if(__bt_is_server_exist(socket_fd) == FALSE,
1493                                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1494
1495                 _bt_remove_server(socket_fd);
1496         } else if (strcasecmp(member, BT_RFCOMM_DATA_RECEIVED) == 0) {
1497                 char *buffer = NULL;
1498                 int buffer_len = 0;
1499                 int socket_fd = 0;
1500                 bluetooth_rfcomm_received_data_t data_r;
1501
1502                 if (!dbus_message_get_args(msg, NULL,
1503                         DBUS_TYPE_INT32, &result,
1504                         DBUS_TYPE_INT16, &socket_fd,
1505                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
1506                         &buffer, &buffer_len,
1507                         DBUS_TYPE_INVALID)) {
1508                         BT_ERR("Unexpected parameters in signal");
1509                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1510                 }
1511
1512                 data_r.socket_fd = socket_fd;
1513                 data_r.buffer_size = buffer_len;
1514                 data_r.buffer = g_memdup(buffer, buffer_len);
1515
1516                 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
1517                                 result, &data_r,
1518                                 event_info->cb, event_info->user_data);
1519
1520                 g_free(data_r.buffer);
1521         }
1522
1523         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1524 }
1525
1526 static void __bt_remove_all_events(void)
1527 {
1528         GSList *l;
1529         bt_event_info_t *info;
1530
1531         for (l = event_list; l != NULL; l = g_slist_next(l)) {
1532                 info = l->data;
1533
1534                 if (info)
1535                         _bt_unregister_event(info->event_type);
1536         }
1537
1538         g_slist_free(event_list);
1539         event_list = NULL;
1540 }
1541
1542 static gboolean __bt_event_is_registered(int event_type)
1543 {
1544         GSList *l;
1545         bt_event_info_t *info;
1546
1547         for (l = event_list; l != NULL; l = g_slist_next(l)) {
1548                 info = l->data;
1549                 if (info == NULL)
1550                         continue;
1551
1552                 retv_if(info->event_type == event_type, TRUE);
1553         }
1554
1555         return FALSE;
1556 }
1557
1558 bt_event_info_t* __bt_event_get_cb_data(int event_type)
1559 {
1560         GSList *l;
1561         bt_event_info_t *info;
1562
1563         for (l = event_list; l != NULL; l = g_slist_next(l)) {
1564                 info = l->data;
1565                 if (info == NULL)
1566                         continue;
1567
1568                 if (info->event_type == event_type)
1569                         return info;
1570         }
1571
1572         return NULL;
1573 }
1574
1575 void _bt_add_server(int server_fd)
1576 {
1577         bt_server_info_t *info;
1578
1579         info = g_new0(bt_server_info_t, 1);
1580         info->server_fd = server_fd;
1581
1582         server_list = g_slist_append(server_list, info);
1583 }
1584
1585 void _bt_remove_server(int server_fd)
1586 {
1587         GSList *l;
1588         bt_server_info_t *info;
1589
1590         for (l = server_list; l != NULL; l = g_slist_next(l)) {
1591                 info = l->data;
1592                 if (info == NULL)
1593                         continue;
1594
1595                 if (info->server_fd == server_fd) {
1596                         server_list = g_slist_remove(server_list, (void *)info);
1597                 }
1598
1599                 g_free(info);
1600         }
1601 }
1602
1603 void _bt_set_obex_server_id(int server_type)
1604 {
1605         obex_server_id = server_type;
1606 }
1607
1608 int _bt_get_obex_server_id(void)
1609 {
1610         return obex_server_id;
1611 }
1612
1613 int _bt_init_event_handler(void)
1614 {
1615         if (is_initialized == TRUE) {
1616                 BT_ERR("Connection already exist");
1617                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1618         }
1619
1620         __bt_remove_all_events();
1621
1622         is_initialized = TRUE;
1623
1624         return BLUETOOTH_ERROR_NONE;
1625 }
1626
1627 int _bt_deinit_event_handler(void)
1628 {
1629         if (is_initialized == FALSE) {
1630                 BT_ERR("Connection dose not exist");
1631                 return BLUETOOTH_ERROR_INTERNAL;
1632         }
1633
1634         __bt_remove_all_events();
1635
1636         is_initialized = FALSE;
1637
1638         return BLUETOOTH_ERROR_NONE;
1639 }
1640
1641 static void __bt_event_data_free(void *data)
1642 {
1643         bt_event_info_t *cb_data = data;
1644
1645         ret_if(cb_data == NULL);
1646
1647         if (cb_data->conn)
1648                 dbus_connection_unref(cb_data->conn);
1649
1650         g_free(cb_data);
1651 }
1652
1653 int _bt_register_event(int event_type, void *event_cb, void *user_data)
1654 {
1655         DBusError dbus_error;
1656         char *match;
1657         DBusConnection *connection_type;
1658         DBusHandleMessageFunction event_func;
1659         bt_event_info_t *cb_data;
1660
1661         if (is_initialized == FALSE)
1662                 _bt_init_event_handler();
1663
1664         if (__bt_event_is_registered(event_type) == TRUE) {
1665                 BT_ERR("The event is already registed");
1666                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
1667         }
1668
1669         switch (event_type) {
1670         case BT_ADAPTER_EVENT:
1671                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1672                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1673
1674                 event_func = __bt_adapter_event_filter;
1675                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1676                                         BT_ADAPTER_PATH);
1677                 break;
1678         case BT_DEVICE_EVENT:
1679                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1680                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1681
1682                 event_func = __bt_device_event_filter;
1683                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1684                                         BT_DEVICE_PATH);
1685                 break;
1686         case BT_HID_EVENT:
1687                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1688                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1689
1690                 event_func = __bt_hid_event_filter;
1691                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1692                                         BT_HID_PATH);
1693                 break;
1694         case BT_HEADSET_EVENT:
1695                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1696                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1697
1698                 event_func = __bt_headset_event_filter;
1699                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1700                                         BT_HEADSET_PATH);
1701                 break;
1702         case BT_NETWORK_EVENT:
1703                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1704                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1705
1706                 event_func = __bt_network_event_filter;
1707                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1708                                         BT_NETWORK_PATH);
1709                 break;
1710         case BT_AVRCP_EVENT:
1711                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1712                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1713
1714                 event_func = __bt_avrcp_event_filter;
1715                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1716                                         BT_AVRCP_PATH);
1717                 break;
1718         case BT_OPP_CLIENT_EVENT:
1719                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1720                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1721
1722                 event_func = __bt_opp_client_event_filter;
1723                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1724                                         BT_OPP_CLIENT_PATH);
1725                 break;
1726         case BT_OPP_SERVER_EVENT:
1727                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1728                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1729
1730                 event_func = __bt_opp_server_event_filter;
1731                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1732                                         BT_OPP_SERVER_PATH);
1733                 break;
1734         case BT_RFCOMM_CLIENT_EVENT:
1735                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1736                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1737
1738                 event_func = __bt_rfcomm_client_event_filter;
1739                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1740                                         BT_RFCOMM_CLIENT_PATH);
1741                 break;
1742         case BT_RFCOMM_SERVER_EVENT:
1743                 connection_type = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1744                 retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1745
1746                 event_func = __bt_rfcomm_server_event_filter;
1747                 match = g_strdup_printf(EVENT_MATCH_RULE, BT_EVENT_SERVICE,
1748                                         BT_RFCOMM_SERVER_PATH);
1749                 break;
1750         default:
1751                 BT_ERR("Unknown event");
1752                 return BLUETOOTH_ERROR_INTERNAL;
1753         }
1754
1755         cb_data = g_new0(bt_event_info_t, 1);
1756
1757         cb_data->event_type = event_type;
1758         cb_data->conn = connection_type;
1759         cb_data->func = event_func;
1760         cb_data->cb = event_cb;
1761         cb_data->user_data = user_data;
1762
1763         if (!dbus_connection_add_filter(connection_type, event_func,
1764                                 (void *)cb_data, __bt_event_data_free)) {
1765                 BT_ERR("Fail to add filter");
1766                 goto fail;
1767         }
1768
1769         dbus_error_init(&dbus_error);
1770
1771         if (match)
1772                 dbus_bus_add_match(connection_type, match, &dbus_error);
1773
1774         if (dbus_error_is_set(&dbus_error)) {
1775                 BT_ERR("Fail to add match: %s\n", dbus_error.message);
1776                 dbus_error_free(&dbus_error);
1777                 goto fail;
1778         }
1779
1780         g_free(match);
1781
1782         event_list = g_slist_append(event_list, cb_data);
1783
1784         return BLUETOOTH_ERROR_NONE;
1785 fail:
1786         if (connection_type)
1787                 dbus_connection_unref(connection_type);
1788
1789         g_free(cb_data);
1790         g_free(match);
1791         return BLUETOOTH_ERROR_INTERNAL;
1792 }
1793
1794 int _bt_unregister_event(int event_type)
1795 {
1796         DBusConnection *connection_type;
1797         DBusHandleMessageFunction event_func;
1798         bt_event_info_t *cb_data;
1799
1800         if (is_initialized == FALSE) {
1801                 BT_ERR("Event is not registered");
1802                 return BLUETOOTH_ERROR_INTERNAL;
1803         }
1804
1805         if (__bt_event_is_registered(event_type) == FALSE) {
1806                 BT_ERR("Not registered event");
1807                 return BLUETOOTH_ERROR_INTERNAL;
1808         }
1809
1810         cb_data = __bt_event_get_cb_data(event_type);
1811
1812         if (cb_data == NULL) {
1813                 BT_ERR("No matched event data");
1814                 return BLUETOOTH_ERROR_INTERNAL;
1815         }
1816
1817         connection_type = cb_data->conn;
1818         event_func = cb_data->func;
1819
1820         event_list = g_slist_remove(event_list, (void *)cb_data);
1821
1822         retv_if(connection_type == NULL, BLUETOOTH_ERROR_INTERNAL);
1823         retv_if(event_func == NULL, BLUETOOTH_ERROR_INTERNAL);
1824
1825         dbus_connection_remove_filter(connection_type, event_func,
1826                                         (void *)cb_data);
1827
1828         return BLUETOOTH_ERROR_NONE;
1829 }