2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-core-device.h"
31 #include "bt-service-obex-server.h"
32 #include "bt-service-obex-agent.h"
33 #include "bt-service-pbap.h"
34 #include "bt-service-opp-client.h"
35 #include "bt-service-map-client.h"
36 #include "bt-service-core-adapter.h"
38 #ifdef TIZEN_FEATURE_BT_DPM
39 #include "bt-service-dpm.h"
42 #define DBUS_TIMEOUT 20 * 1000 /* 20 Sec */
43 static GDBusConnection *manager_conn;
44 static GDBusConnection *obexd_conn;
45 static GDBusConnection *opc_obexd_conn;
46 static GDBusConnection *map_obexd_conn;
48 static guint event_id;
50 static guint session_reinit_timer;
53 * obexd connection type
61 OBEX_PCSUITE = (1 << 6),
62 OBEX_SYNCEVOLUTION = (1 << 7),
65 } bluetooth_obex_connection_type_t;
68 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path);
69 void _bt_opc_property_changed_event(GVariant *msg, char *path);
70 void _bt_map_property_changed_event(GVariant *msg, const char *path);
71 int _bt_register_service_event(GDBusConnection *g_conn, int event_type);
72 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type);
73 void _bt_opp_client_event_deinit(void);
74 void _bt_map_client_event_deinit(void);
75 void _bt_map_on_transfer_finished(const char *transfer_object_path, const int error);
77 /* Temp Adapter changes required to make OBEX work for handling device events here ,
78 This code will be removed and moved to OAL Event Handling part .
82 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
84 ret_if(value == NULL);
89 info->uuids = g_variant_dup_strv(value, &uuid_count);
90 info->uuid_count = (unsigned int)uuid_count;
92 BT_DBG("uuid count : %d", uuid_count);
96 void _bt_convert_addr_string_to_secure_string(char *addr,
101 ret_if(address == NULL);
102 ret_if(addr == NULL);
104 len = strlen(address);
105 ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
107 strncpy(addr, address, len);
122 bt_status_t _bt_adapter_get_status_for_Obex(void)
124 int value = VCONFKEY_BT_STATUS_OFF;
126 /* check VCONFKEY_BT_STATUS */
127 if (vconf_get_int(VCONFKEY_BT_STATUS, &value) != 0) {
128 BT_ERR("fail to get vconf key!");
129 return BLUETOOTH_ADAPTER_DISABLED;
135 static int __bt_get_owner_info(GVariant *msg, char **name,
136 char **previous, char **current)
138 g_variant_get(msg, "(&s&s&s)", name, previous, current);
139 return BLUETOOTH_ERROR_NONE;
142 static int __bt_get_agent_signal_info(GVariant *msg, char **address,
143 char **name, char **uuid)
145 g_variant_get(msg, "(sss)", address, name, uuid);
146 return BLUETOOTH_ERROR_NONE;
149 gboolean __bt_handle_is_flight_mode_enabled(void)
151 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
152 int is_flight_mode = 0;
154 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &is_flight_mode);
156 BT_ERR("vconf_get_bool failed");
158 return (is_flight_mode == 0) ? FALSE : TRUE;
164 void _bt_handle_adapter_event(GVariant *msg, const char *member)
167 ret_if(member == NULL);
169 if (strcasecmp(member, "DeviceCreated") == 0) {
170 char *object_path = NULL;
172 ret_if(_bt_is_device_creating() == FALSE);
174 /* Bonding from remote device */
175 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
177 g_variant_get(msg, "(&o)", &object_path);
178 _bt_convert_device_path_to_address((const char*)object_path, address);
181 } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
182 BT_ERR_C("### Hardware error received from BLUEZ");
183 } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
184 BT_ERR_C("### Tx timeout error received from BLUEZ");
189 static void __bt_obex_property_changed_event(GVariant *msg, const char *path)
193 GVariantIter value_iter;
194 GVariant *child = NULL, *val = NULL;
195 char *property = NULL;
196 g_variant_iter_init(&value_iter, msg);
197 while ((child = g_variant_iter_next_value(&value_iter))) {
198 g_variant_get(child, "{sv}", &property, &val);
200 ret_if(property == NULL);
202 BT_DBG("property :%s", property);
204 if (strcasecmp(property, "Status") == 0) {
206 g_variant_get(val, "s", &status);
208 if (strcasecmp(status, "active") == 0) {
209 _bt_obex_transfer_started(path);
210 } else if (strcasecmp(status, "complete") == 0) {
211 _bt_obex_transfer_completed(path, TRUE);
212 _bt_pbap_obex_transfer_completed(path, TRUE);
213 } else if (strcasecmp(status, "error") == 0) {
214 _bt_obex_transfer_completed(path, FALSE);
215 _bt_pbap_obex_transfer_completed(path, FALSE);
218 } else if (strcasecmp(property, "Transferred") == 0) {
219 guint64 transferred = 0;
220 /* As Transferred is expected guint64 so change int to guint64 and
221 * eariler transferred is static because of it can overwrite data
222 * on present on opc_obex_conn or obexd_conn as these are
223 * static memory are in sequential */
224 g_variant_get(val, "t", &transferred);
226 _bt_obex_transfer_progress(path, transferred);
228 /* TODO: MAP, "Complete"? see above */
230 g_variant_unref(val);
231 g_variant_unref(child);
236 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
238 char *interface_name = NULL;
239 GVariant *val = NULL;
241 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &val, NULL);
242 BT_DBG("_bt_handle_property_changed_event");
244 if (strcasecmp(interface_name, BT_ADAPTER_INTERFACE) == 0) {
245 gboolean ret = FALSE;
246 gboolean powered = FALSE;
248 /* Check if Adapter is disabled, cancle queued transfers if any */
249 ret = g_variant_lookup(msg, "Powered", "b", &powered);
251 _bt_cancel_queued_transfers();
252 } else if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
253 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
254 __bt_obex_property_changed_event(val,
257 g_variant_unref(val);
260 void __bt_opc_property_changed_event(GVariant *msg,
263 GVariantIter value_iter;
264 char *property = NULL;
265 GVariant *val = NULL;
266 GVariant *child = NULL;
268 g_variant_iter_init(&value_iter, msg);
269 if ((child = g_variant_iter_next_value(&value_iter))) {
270 g_variant_get(child, "{sv}", &property, &val);
271 ret_if(property == NULL);
273 if (strcasecmp(property, "Status") == 0) {
275 g_variant_get(val, "s", &status);
276 BT_DBG("Status is %s", status);
278 if (strcasecmp(status, "active") == 0)
279 _bt_obex_client_started(path);
280 else if (strcasecmp(status, "complete") == 0)
281 _bt_obex_client_completed(path, TRUE);
282 else if (strcasecmp(status, "error") == 0)
283 _bt_obex_client_completed(path, FALSE);
286 } else if (strcasecmp(property, "Transferred") == 0) {
287 guint64 transferred = 0;
288 g_variant_get(val, "t", &transferred);
290 _bt_obex_client_progress(path, transferred);
292 BT_DBG("property : [%s]", property);
295 g_variant_unref(child);
296 g_variant_unref(val);
300 void _bt_opc_property_changed_event(GVariant *msg, char *path)
302 char *interface_name = NULL;
303 GVariant *value = NULL;
304 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
305 BT_INFO("interface_name = %s", interface_name);
306 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
307 __bt_opc_property_changed_event(value,
310 BT_DBG("interface_name : [%s]", interface_name);
312 g_variant_unref(value);
316 void __bt_map_property_changed_event(GVariant *msg,
320 GVariantIter value_iter;
321 char *property = NULL;
322 GVariant *val = NULL;
323 GVariant *child = NULL;
325 g_variant_iter_init(&value_iter, msg);
326 while ((child = g_variant_iter_next_value(&value_iter))) {
327 g_variant_get(child, "{sv}", &property, &val);
328 ret_if(property == NULL);
330 if (strcasecmp(property, "Status") == 0) {
332 g_variant_get(val, "s", &status);
333 BT_DBG("Status is %s", status);
335 if (strcasecmp(status, "active") == 0) {
336 BT_DBG("EVENT : STARTED");
337 // currently doing nothing
338 } else if (strcasecmp(status, "complete") == 0) {
339 BT_DBG("EVENT : COMPLETED");
340 _bt_map_on_transfer_finished(path, BLUETOOTH_ERROR_NONE);
341 } else if (strcasecmp(status, "error") == 0) {
342 BT_DBG("EVENT : FAILED");
343 _bt_map_on_transfer_finished(path, BLUETOOTH_ERROR_INTERNAL);
346 } else if (strcasecmp(property, "Transferred") == 0) {
347 guint64 transferred = 0;
348 g_variant_get(val, "t", &transferred);
350 BT_DBG("EVENT : PROGRESS CALLBACK");
351 // currently doing nothing - progress callback type is not used
353 BT_DBG("OTHER EVENT : property : [%s]", property);
356 g_variant_unref(child);
357 g_variant_unref(val);
361 void _bt_map_property_changed_event(GVariant *msg, const char *path)
363 BT_DBG("Entered _bt_map_property_changed_event");
364 char *interface_name = NULL;
365 GVariant *value = NULL;
366 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, &value, NULL);
367 BT_INFO("interface_name = %s", interface_name);
368 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
369 __bt_map_property_changed_event(value,
372 BT_DBG("interface_name : [%s]", interface_name);
374 g_variant_unref(value);
378 void _bt_handle_agent_event(GVariant *msg, const char *member)
380 int result = BLUETOOTH_ERROR_NONE;
381 char *address = NULL;
384 GVariant *param = NULL;
385 ret_if(member == NULL);
387 if (strcasecmp(member, "ObexAuthorize") == 0) {
388 __bt_get_agent_signal_info(msg, &address, &name, &uuid);
389 param = g_variant_new("(i&s&s)", result, address, name);
390 _bt_send_event(BT_OPP_SERVER_EVENT,
391 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
393 /* TODO: MAP? see above */
397 static int __bt_get_object_path(GVariant *msg, char **path)
399 g_variant_get(msg, "(o*)", path, NULL);
401 return BLUETOOTH_ERROR_INTERNAL;
403 return BLUETOOTH_ERROR_NONE;
406 char *_bt_get_bonded_device_name(char *address)
408 bt_remote_dev_info_t *dev_info;
410 retv_if(address == NULL, strdup(""));
412 dev_info = _bt_service_get_remote_dev_info(address);
413 retv_if(dev_info == NULL, strdup(""));
414 retv_if(dev_info->name == NULL, strdup(""));
416 return g_strdup(dev_info->name);
418 /* Temp Adapter Util changes to make OBEX work.
421 void _bt_handle_device_event(GVariant *msg, const char *member, const char *path)
424 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
425 ret_if(path == NULL);
427 if (strcasecmp(member, "Disconnected") == 0) {
428 unsigned char disc_reason = 0;
429 unsigned char addr_type = 0;
430 char *dev_name = NULL;
431 gboolean sending = FALSE;
433 g_variant_get(msg, "(yys)", &addr_type, &disc_reason, &dev_name);
435 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
437 _bt_convert_device_path_to_address(path, address);
440 /*Check for any OPP transfer on the device and cancel
443 _bt_obex_check_pending_transfer(address);
444 _bt_opp_client_is_sending(&sending);
446 _bt_opp_client_check_pending_transfer(address);
447 /* TODO: MAP? see above */
451 } else if (strcasecmp(member, "ProfileStateChanged") == 0) {
453 char *profile_uuid = NULL;
454 bluetooth_device_address_t bd_addr;
456 g_variant_get(msg, "(si)", &profile_uuid, &state);
458 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
460 _bt_convert_device_path_to_address(path, address);
461 _bt_convert_addr_string_to_type(bd_addr.addr, address);
463 _bt_convert_addr_string_to_secure_string(secure_address, address);
464 BT_DBG("[%s] [%s] [%d]", secure_address, profile_uuid, state);
469 static void __bt_manager_event_filter(GDBusConnection *connection,
470 const gchar *sender_name,
471 const gchar *object_path,
472 const gchar *interface_name,
473 const gchar *signal_name,
474 GVariant *parameters,
479 char *obj_path = NULL;
481 if (signal_name == NULL)
484 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
485 g_variant_get(parameters, "(&o@a{sa{sv}})", &obj_path, &value);
487 if (strcasecmp(obj_path, BT_BLUEZ_HCI_PATH) == 0)
488 if (!TIZEN_FEATURE_BT_USB_DONGLE) {
489 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
490 BT_ERR("Fail to init obex server");
492 g_variant_unref(value);
493 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
494 if (TIZEN_FEATURE_BT_USB_DONGLE) {
495 if (_bt_unregister_obex_server() != BLUETOOTH_ERROR_NONE)
496 BT_ERR("Fail to unregister obex server");
498 } else if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
501 char *previous = NULL;
502 char *current = NULL;
504 if (g_strcmp0(g_variant_get_type_string(parameters), "(sss)") != 0) {
505 BT_ERR("Invalid variant format");
509 if (__bt_get_owner_info(parameters, &name, &previous, ¤t)) {
510 BT_ERR("Fail to get the owner info");
514 if (*current != '\0')
520 if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
521 BT_INFO_C("### Bluetoothd is terminated");
523 if (_bt_unregister_obex_server() != BLUETOOTH_ERROR_NONE)
524 BT_ERR("Fail to unregister obex server");
528 _bt_obex_server_check_allocation(&value);
531 /* Check if the obex server was terminated abnormally */
532 _bt_obex_server_check_termination(name);
534 } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
535 _bt_handle_agent_event(parameters, signal_name);
536 } else if (g_strcmp0(interface_name, BT_DEVICE_INTERFACE) == 0) {
537 _bt_handle_device_event(parameters, signal_name, object_path);
543 static gboolean __bt_is_obexd_event(GVariant *msg, const char *interface)
546 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
547 char *interface_name = NULL;
549 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
550 retv_if(interface_name == NULL, FALSE);
552 if (strcasecmp(interface_name, BT_OBEX_TRANSFER_INTERFACE) == 0) {
553 BT_DBG("BT_OBEX_TRANSFER_INTERFACE");
561 static void __bt_obexd_event_filter(GDBusConnection *connection,
562 const gchar *sender_name,
563 const gchar *object_path,
564 const gchar *interface_name,
565 const gchar *signal_name,
566 GVariant *parameters,
569 const char *member = signal_name;
570 char *obj_path = NULL;
571 ret_if(member == NULL);
573 if (strcasecmp(member, "InterfacesAdded") == 0) {
574 if (__bt_get_object_path(parameters, &obj_path)) {
575 BT_ERR("Fail to get the path");
579 /*Handle OPP_SERVER_CONNECTED_EVENT here */
580 /* TODO: MAP? see above */
581 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
582 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
587 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
588 BT_DBG("Obex_Server_Session_Transfer connected");
589 _bt_obex_transfer_connected(obj_path);
592 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
593 /*Handle OPP_SERVER_DISCONNECTED_EVENT here */
594 /* TODO: MAP? see above */
595 if (__bt_get_object_path(parameters, &obj_path)) {
596 BT_ERR("Fail to get the path");
600 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
601 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
602 BT_DBG("Call PBAP Disconnected");
603 _bt_obex_pbap_client_disconnect(obj_path);
606 if (strncmp(obj_path, BT_SESSION_BASEPATH_SERVER,
607 strlen(BT_SESSION_BASEPATH_SERVER)) != 0) {
612 if (g_strrstr(obj_path, "session") && g_strrstr(obj_path, "transfer")) {
613 BT_DBG("Obex_Server_Session_Transfer disconnected %s",
616 _bt_obex_transfer_disconnected(obj_path);
619 } else if (__bt_is_obexd_event(parameters, interface_name) == TRUE) {
620 const char *path = object_path;
622 if (strncmp(path, BT_SESSION_BASEPATH_SERVER,
623 strlen(BT_SESSION_BASEPATH_SERVER)) != 0 &&
624 strncmp(path, BT_SESSION_BASEPATH_CLIENT,
625 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
626 BT_DBG("DBUS_HANDLER_RESULT_NOT_YET_HANDLED");
630 _bt_handle_property_changed_event(parameters, path);
635 static gboolean __bt_is_obexd_client_event(GVariant *msg, const char *interface)
639 if (g_strcmp0(interface, BT_PROPERTIES_INTERFACE) == 0) {
640 char *interface_name = NULL;
642 g_variant_get(msg, "(&s@a{sv}@as)", &interface_name, NULL, NULL);
644 retv_if(interface_name == NULL, FALSE);
646 if (strcasecmp(interface_name,
647 BT_OBEX_TRANSFER_INTERFACE) == 0) {
658 static void __bt_opc_event_filter(GDBusConnection *connection,
659 const gchar *sender_name,
660 const gchar *object_path,
661 const gchar *interface_name,
662 const gchar *signal_name,
663 GVariant *parameters,
666 const char *member = signal_name;
667 char *obj_path = NULL;
668 if (strcasecmp(member, "InterfacesAdded") == 0) {
669 BT_DBG("InterfacesAdded");
670 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
672 if (__bt_get_object_path(parameters, &obj_path)) {
673 BT_ERR("Fail to get the path");
677 BT_DBG("object_path = %s", obj_path);
679 if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
680 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0
681 || strstr(obj_path, "transfer") == NULL) {
684 } else if (strncmp(obj_path, BT_SESSION_BASEPATH_CLIENT,
685 strlen(BT_SESSION_BASEPATH_CLIENT)) == 0) {
686 BT_DBG("Going to call opc disconnected");
687 _bt_opc_disconnected(obj_path);
692 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
693 char *path = (char *)object_path;
694 BT_INFO("object_path %s", path);
695 if (strncmp(path, BT_SESSION_BASEPATH_CLIENT,
696 strlen(BT_SESSION_BASEPATH_CLIENT)) != 0) {
697 BT_DBG("NOT BT_SESSION_BASEPATH_CLIENT");
701 _bt_opc_property_changed_event(parameters, path);
707 int _bt_opp_client_event_init(void)
709 GError *error = NULL;
711 if (opc_obexd_conn == NULL) {
712 opc_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
714 if (!opc_obexd_conn) {
716 BT_ERR("Unable to connect to dbus: %s", error->message);
717 g_clear_error(&error);
719 return BLUETOOTH_ERROR_INTERNAL;
723 if (_bt_register_service_event(opc_obexd_conn,
724 BT_OPP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
725 g_object_unref(opc_obexd_conn);
726 opc_obexd_conn = NULL;
727 return BLUETOOTH_ERROR_INTERNAL;
730 return BLUETOOTH_ERROR_NONE;
733 void _bt_opp_client_event_deinit(void)
735 if (opc_obexd_conn) {
736 _bt_unregister_service_event(opc_obexd_conn,
737 BT_OPP_CLIENT_EVENT);
738 g_object_unref(opc_obexd_conn);
739 opc_obexd_conn = NULL;
743 static void __bt_map_event_filter(GDBusConnection *connection,
744 const gchar *sender_name,
745 const gchar *object_path,
746 const gchar *interface_name,
747 const gchar *signal_name,
748 GVariant *parameters,
751 BT_DBG("Entered __bt_map_event_filter");
752 const char *member = signal_name;
754 if (strcasecmp(member, "InterfacesAdded") == 0) {
755 BT_DBG("------------------------------------ADDED------------------------------------");
756 // currently doing nothing
757 } else if (strcasecmp(member, "InterfacesRemoved") == 0) {
758 BT_DBG("------------------------------------REMOVED------------------------------------");
759 // TODO check if something should be called here?
760 //_bt_map_on_transfer_finished(object_path, error);
761 } else if (__bt_is_obexd_client_event(parameters, interface_name) == TRUE) {
762 BT_DBG("------------------------------------CLIENT EVENT------------------------------------");
763 _bt_map_property_changed_event(parameters, object_path);
769 int _bt_map_client_event_init(void)
771 GError *error = NULL;
773 if (map_obexd_conn == NULL) {
774 map_obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
776 if (!map_obexd_conn) {
778 BT_ERR("Unable to connect to dbus: %s", error->message);
779 g_clear_error(&error);
781 return BLUETOOTH_ERROR_INTERNAL;
785 if (_bt_register_service_event(map_obexd_conn,
786 BT_MAP_CLIENT_EVENT) != BLUETOOTH_ERROR_NONE) {
787 g_object_unref(map_obexd_conn);
788 map_obexd_conn = NULL;
789 return BLUETOOTH_ERROR_INTERNAL;
792 return BLUETOOTH_ERROR_NONE;
795 void _bt_map_client_event_deinit(void)
797 if (map_obexd_conn) {
798 _bt_unregister_service_event(map_obexd_conn,
799 BT_MAP_CLIENT_EVENT);
800 g_object_unref(map_obexd_conn);
801 map_obexd_conn = NULL;
805 int _bt_register_manager_subscribe_signal(GDBusConnection *conn,
811 static int subs_interface_added_id = -1;
812 static int subs_interface_removed_id = -1;
813 static int subs_name_owner_changed = -1;
816 if (subs_interface_added_id == -1) {
817 subs_interface_added_id = g_dbus_connection_signal_subscribe(conn,
818 NULL, BT_MANAGER_INTERFACE,
819 BT_INTERFACES_ADDED, NULL, NULL, 0,
820 __bt_manager_event_filter,
823 if (subs_interface_removed_id == -1) {
824 subs_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
825 NULL, BT_MANAGER_INTERFACE,
826 BT_INTERFACES_REMOVED, NULL, NULL, 0,
827 __bt_manager_event_filter,
830 if (subs_name_owner_changed == -1) {
831 subs_name_owner_changed = g_dbus_connection_signal_subscribe(conn,
832 NULL, BT_EVENT_FREEDESKTOP,
833 BT_NAME_OWNER_CHANGED, NULL, NULL, 0,
834 __bt_manager_event_filter, NULL, NULL);
838 if (subs_interface_added_id != -1) {
839 g_dbus_connection_signal_unsubscribe(conn,
840 subs_interface_added_id);
841 subs_interface_added_id = -1;
843 if (subs_interface_removed_id != -1) {
844 g_dbus_connection_signal_unsubscribe(conn,
845 subs_interface_removed_id);
846 subs_interface_removed_id = -1;
848 if (subs_name_owner_changed != -1) {
849 g_dbus_connection_signal_unsubscribe(conn,
850 subs_name_owner_changed);
851 subs_name_owner_changed = -1;
858 int _bt_register_device_subscribe_signal(GDBusConnection *conn,
864 static int subs_device_id = -1;
867 if (subs_device_id == -1) {
868 subs_device_id = g_dbus_connection_signal_subscribe(conn,
869 NULL, BT_DEVICE_INTERFACE,
871 __bt_manager_event_filter,
875 if (subs_device_id != -1) {
876 g_dbus_connection_signal_unsubscribe(conn,
884 int _bt_register_input_subscribe_signal(GDBusConnection *conn,
890 static int subs_input_id = -1;
893 if (subs_input_id == -1) {
894 subs_input_id = g_dbus_connection_signal_subscribe(conn,
895 NULL, BT_INPUT_INTERFACE,
897 __bt_manager_event_filter,
901 if (subs_input_id != -1) {
902 g_dbus_connection_signal_unsubscribe(conn,
912 int _bt_register_opp_server_subscribe_signal(GDBusConnection *conn,
918 static int subs_opp_server_interface_added_id = -1;
919 static int subs_opp_server_interface_removed_id = -1;
920 static int subs_opp_server_property_id = -1;
924 if (subs_opp_server_interface_added_id == -1) {
925 subs_opp_server_interface_added_id = g_dbus_connection_signal_subscribe(conn,
926 NULL, BT_MANAGER_INTERFACE,
927 BT_INTERFACES_ADDED, NULL, NULL, 0,
928 __bt_obexd_event_filter,
931 if (subs_opp_server_interface_removed_id == -1) {
932 subs_opp_server_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
933 NULL, BT_MANAGER_INTERFACE,
934 BT_INTERFACES_REMOVED, NULL, NULL, 0,
935 __bt_obexd_event_filter,
938 if (subs_opp_server_property_id == -1) {
939 subs_opp_server_property_id = g_dbus_connection_signal_subscribe(conn,
940 NULL, BT_PROPERTIES_INTERFACE,
941 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
942 __bt_obexd_event_filter,
946 if (subs_opp_server_interface_added_id != -1) {
947 g_dbus_connection_signal_unsubscribe(conn,
948 subs_opp_server_interface_added_id);
949 subs_opp_server_interface_added_id = -1;
951 if (subs_opp_server_interface_removed_id != -1) {
952 g_dbus_connection_signal_unsubscribe(conn,
953 subs_opp_server_interface_removed_id);
954 subs_opp_server_interface_removed_id = -1;
956 if (subs_opp_server_property_id != -1) {
957 g_dbus_connection_signal_unsubscribe(conn,
958 subs_opp_server_property_id);
959 subs_opp_server_property_id = -1;
965 int _bt_register_opp_client_subscribe_signal(GDBusConnection *conn,
971 static int subs_opp_client_interface_added_id = -1;
972 static int subs_opp_client_interface_removed_id = -1;
973 static int subs_opp_client_property_id = -1;
977 if (subs_opp_client_interface_added_id == -1) {
978 subs_opp_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
979 NULL, BT_MANAGER_INTERFACE,
980 BT_INTERFACES_ADDED, NULL, NULL, 0,
981 __bt_opc_event_filter,
984 if (subs_opp_client_interface_removed_id == -1) {
985 subs_opp_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
986 NULL, BT_MANAGER_INTERFACE,
987 BT_INTERFACES_REMOVED, NULL, NULL, 0,
988 __bt_opc_event_filter,
991 if (subs_opp_client_property_id == -1) {
992 subs_opp_client_property_id = g_dbus_connection_signal_subscribe(conn,
993 NULL, BT_PROPERTIES_INTERFACE,
994 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
995 __bt_opc_event_filter,
999 if (subs_opp_client_interface_added_id != -1) {
1000 g_dbus_connection_signal_unsubscribe(conn,
1001 subs_opp_client_interface_added_id);
1002 subs_opp_client_interface_added_id = -1;
1004 if (subs_opp_client_interface_removed_id != -1) {
1005 g_dbus_connection_signal_unsubscribe(conn,
1006 subs_opp_client_interface_removed_id);
1007 subs_opp_client_interface_removed_id = -1;
1009 if (subs_opp_client_property_id != -1) {
1010 g_dbus_connection_signal_unsubscribe(conn,
1011 subs_opp_client_property_id);
1012 subs_opp_client_property_id = -1;
1018 int _bt_register_map_client_subscribe_signal(GDBusConnection *conn,
1024 static int subs_map_client_interface_added_id = -1;
1025 static int subs_map_client_interface_removed_id = -1;
1026 static int subs_map_client_property_id = -1;
1030 if (subs_map_client_interface_added_id == -1) {
1031 subs_map_client_interface_added_id = g_dbus_connection_signal_subscribe(conn,
1032 NULL, BT_MANAGER_INTERFACE,
1033 BT_INTERFACES_ADDED, NULL, NULL, 0,
1034 __bt_map_event_filter,
1037 if (subs_map_client_interface_removed_id == -1) {
1038 subs_map_client_interface_removed_id = g_dbus_connection_signal_subscribe(conn,
1039 NULL, BT_MANAGER_INTERFACE,
1040 BT_INTERFACES_REMOVED, NULL, NULL, 0,
1041 __bt_map_event_filter,
1044 if (subs_map_client_property_id == -1) {
1045 subs_map_client_property_id = g_dbus_connection_signal_subscribe(conn,
1046 NULL, BT_PROPERTIES_INTERFACE,
1047 BT_PROPERTIES_CHANGED, NULL, NULL, 0,
1048 __bt_map_event_filter,
1052 if (subs_map_client_interface_added_id != -1) {
1053 g_dbus_connection_signal_unsubscribe(conn,
1054 subs_map_client_interface_added_id);
1055 subs_map_client_interface_added_id = -1;
1057 if (subs_map_client_interface_removed_id != -1) {
1058 g_dbus_connection_signal_unsubscribe(conn,
1059 subs_map_client_interface_removed_id);
1060 subs_map_client_interface_removed_id = -1;
1062 if (subs_map_client_property_id != -1) {
1063 g_dbus_connection_signal_unsubscribe(conn,
1064 subs_map_client_property_id);
1065 subs_map_client_property_id = -1;
1072 int _bt_register_service_event(GDBusConnection *g_conn, int event_type)
1074 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1076 switch (event_type) {
1077 case BT_MANAGER_EVENT:
1078 _bt_register_manager_subscribe_signal(g_conn, TRUE);
1080 case BT_DEVICE_EVENT:
1081 _bt_register_device_subscribe_signal(g_conn, TRUE);
1083 case BT_OPP_SERVER_EVENT:
1084 _bt_register_opp_server_subscribe_signal(g_conn, TRUE);
1086 case BT_OPP_CLIENT_EVENT:
1087 _bt_register_opp_client_subscribe_signal(g_conn, TRUE);
1089 case BT_MAP_CLIENT_EVENT:
1090 _bt_register_map_client_subscribe_signal(g_conn, TRUE);
1093 BT_ERR("Unknown event");
1094 return BLUETOOTH_ERROR_INTERNAL;
1097 return BLUETOOTH_ERROR_NONE;
1100 void _bt_unregister_service_event(GDBusConnection *g_conn, int event_type)
1104 ret_if(g_conn == NULL);
1106 switch (event_type) {
1107 case BT_MANAGER_EVENT:
1108 _bt_register_manager_subscribe_signal(g_conn, FALSE);
1109 _bt_register_device_subscribe_signal(g_conn, FALSE);
1110 _bt_register_input_subscribe_signal(g_conn, FALSE);
1112 case BT_OPP_SERVER_EVENT:
1113 _bt_register_opp_server_subscribe_signal(g_conn, FALSE);
1115 case BT_OPP_CLIENT_EVENT:
1116 _bt_register_opp_client_subscribe_signal(g_conn, FALSE);
1118 case BT_MAP_CLIENT_EVENT:
1119 _bt_register_map_client_subscribe_signal(g_conn, FALSE);
1122 BT_ERR("Unknown event");
1129 static int __bt_init_manager_receiver(void)
1133 GError *error = NULL;
1135 if (manager_conn == NULL) {
1136 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1137 if (error != NULL) {
1138 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
1139 g_clear_error(&error);
1141 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1144 if (_bt_register_service_event(manager_conn,
1145 BT_MANAGER_EVENT) != BLUETOOTH_ERROR_NONE)
1148 if (_bt_register_service_event(manager_conn,
1149 BT_DEVICE_EVENT) != BLUETOOTH_ERROR_NONE)
1152 return BLUETOOTH_ERROR_NONE;
1155 g_object_unref(manager_conn);
1156 manager_conn = NULL;
1161 return BLUETOOTH_ERROR_INTERNAL;
1164 static int __bt_init_obexd_receiver(void)
1167 if (!TIZEN_PROFILE_TV) { /* TODO: obexd doesn't work in TV profile. It should be resolved later. */
1168 GError *error = NULL;
1170 if (obexd_conn == NULL) {
1171 obexd_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1172 if (error != NULL) {
1173 BT_ERR("ERROR: Can't get on session bus [%s]", error->message);
1174 g_clear_error(&error);
1176 retv_if(obexd_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1179 if (_bt_register_service_event(obexd_conn,
1180 BT_OPP_SERVER_EVENT) != BLUETOOTH_ERROR_NONE) {
1181 BT_ERR("Error while registering service event");
1182 g_object_unref(obexd_conn);
1184 return BLUETOOTH_ERROR_INTERNAL;
1190 return BLUETOOTH_ERROR_NONE;
1193 gboolean __bt_reinit_obexd_receiver(gpointer user_data)
1195 static int retry_cnt = 0;
1196 int result = BLUETOOTH_ERROR_NONE;
1200 result = __bt_init_obexd_receiver();
1201 if (result != BLUETOOTH_ERROR_NONE) {
1202 /* 20 ms * 50 = 10 seconds. During 10 seconds fail to initialize,
1203 then it is not the timing issue. Just can't use the session bus connection */
1204 if (retry_cnt > 100) {
1205 BT_ERR("Fail to init obexd receiver by 50 times.");
1207 session_reinit_timer = 0;
1211 BT_DBG("Retry to initialize the obexd receiver");
1216 session_reinit_timer = 0;
1223 /* To receive the event from bluez */
1224 int _bt_init_obex_event_receiver(void)
1230 result = __bt_init_manager_receiver();
1231 retv_if(result != BLUETOOTH_ERROR_NONE, result);
1233 result = __bt_init_obexd_receiver();
1234 if (result != BLUETOOTH_ERROR_NONE) {
1235 BT_ERR("Fail to init obexd receiver");
1237 /* Try to re-initialize obexd receiver in the timer */
1238 if (session_reinit_timer > 0)
1239 g_source_remove(session_reinit_timer);
1241 session_reinit_timer = g_timeout_add(200,
1242 (GSourceFunc)__bt_reinit_obexd_receiver, NULL);
1247 return BLUETOOTH_ERROR_NONE;
1250 void _bt_deinit_obex_event_receiver(void)
1254 _bt_unregister_service_event(manager_conn, BT_MANAGER_EVENT);
1256 _bt_unregister_service_event(obexd_conn, BT_OPP_SERVER_EVENT);
1259 g_object_unref(manager_conn);
1260 manager_conn = NULL;
1264 g_object_unref(obexd_conn);
1269 g_source_remove(event_id);