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 "bt-service-common.h"
23 #include <vconf-internal-bt-keys.h>
24 #include "bt-service-event.h"
25 //#include "bt-service-device.h"
27 guint nap_connected_device_count = 0;
28 static GDBusConnection *manager_conn;
30 void bt_set_device_values(gboolean connected, int state)
32 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
34 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
35 BT_ERR("vconf_get_int failed");
37 if (connected == TRUE)
38 bt_device_state |= state;
39 else if (bt_device_state & state)
40 bt_device_state ^= state;
42 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
43 BT_ERR("vconf_set_int failed");
46 void _bt_handle_network_server_event(GVariant *msg, const char *member)
48 int result = BLUETOOTH_ERROR_NONE;
51 GVariant *param = NULL;
52 ret_if(member == NULL);
54 BT_DBG("_bt_handle_network_server_event Event handler called >>>>>>>>>>>>>");
55 if (strcasecmp(member, "PeerConnected") == 0) {
56 g_variant_get(msg, "(ss)", &device, &address);
58 bt_set_device_values(TRUE,
59 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
60 param = g_variant_new("(iss)", result, device, address);
61 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_CONNECTED,
65 nap_connected_device_count++;
66 } else if (strcasecmp(member, "PeerDisconnected") == 0) {
67 g_variant_get(msg, "(ss)", &device, &address);
68 nap_connected_device_count--;
69 if (nap_connected_device_count == 0)
70 bt_set_device_values(FALSE,
71 VCONFKEY_BT_DEVICE_PAN_CONNECTED);
72 param = g_variant_new("(iss)", result, device, address);
73 _bt_send_event(BT_NETWORK_EVENT, BLUETOOTH_EVENT_NETWORK_SERVER_DISCONNECTED,
80 static void __bt_manager_event_filter(GDBusConnection *connection,
81 const gchar *sender_name,
82 const gchar *object_path,
83 const gchar *interface_name,
84 const gchar *signal_name,
88 if (signal_name == NULL)
91 if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0)
92 _bt_handle_network_server_event(parameters, signal_name);
96 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
102 static int subs_serv_id = -1;
103 static int subs_client_id = -1;
106 if (subs_serv_id == -1) {
107 subs_serv_id = g_dbus_connection_signal_subscribe(conn,
108 NULL, BT_NETWORK_SERVER_INTERFACE,
110 __bt_manager_event_filter,
113 if (subs_client_id == -1) {
114 subs_client_id = g_dbus_connection_signal_subscribe(conn,
115 NULL, BT_NETWORK_CLIENT_INTERFACE,
117 __bt_manager_event_filter,
121 if (subs_serv_id != -1) {
122 g_dbus_connection_signal_unsubscribe(conn,
126 if (subs_client_id != -1) {
127 g_dbus_connection_signal_unsubscribe(conn,
135 static int __bt_init_manager_receiver(void)
139 GError *error = NULL;
141 if (manager_conn == NULL) {
142 manager_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
144 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
145 g_clear_error(&error);
148 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
151 if (_bt_register_network_subscribe_signal(manager_conn, TRUE) == -1) {
152 BT_DBG("_bt_register_network_subscribe_signal Failed..");
157 return BLUETOOTH_ERROR_NONE;
160 g_object_unref(manager_conn);
166 return BLUETOOTH_ERROR_INTERNAL;
169 /* To receive the event from bluez */
170 int _bt_init_pan_nap_event_receiver(void)
176 result = __bt_init_manager_receiver();
177 retv_if(result != BLUETOOTH_ERROR_NONE, result);
181 return BLUETOOTH_ERROR_NONE;
184 void _bt_deinit_pan_nap_event_receiver(void)
188 _bt_register_network_subscribe_signal(manager_conn, FALSE);
191 g_object_unref(manager_conn);