3d6078b816f9c9397036d78fa4cc05be1bcf3260
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / pan_nap / bt-service-pan-nap-event-receiver.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <glib.h>
19 #include <string.h>
20 #include <dlog.h>
21 #include <vconf.h>
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"
26
27 guint nap_connected_device_count = 0;
28 static GDBusConnection *manager_conn;
29
30 void bt_set_device_values(gboolean connected, int state)
31 {
32         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
33
34         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
35                 BT_ERR("vconf_get_int failed");
36
37         if (connected == TRUE)
38                 bt_device_state |= state;
39         else if (bt_device_state & state)
40                 bt_device_state ^= state;
41
42         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
43                 BT_ERR("vconf_set_int failed");
44 }
45
46 void _bt_handle_network_server_event(GVariant *msg, const char *member)
47 {
48         int result = BLUETOOTH_ERROR_NONE;
49         char *address = NULL;
50         char *device = NULL;
51         GVariant *param = NULL;
52         ret_if(member == NULL);
53
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);
57
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,
62                         param);
63                 g_free(device);
64                 g_free(address);
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,
74                         param);
75                 g_free(device);
76                 g_free(address);
77         }
78 }
79
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,
85                                         GVariant *parameters,
86                                         gpointer user_data)
87 {
88         if (signal_name == NULL)
89                 return;
90
91         if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0)
92                 _bt_handle_network_server_event(parameters, signal_name);
93         return;
94 }
95
96 int _bt_register_network_subscribe_signal(GDBusConnection *conn,
97                 int subscribe)
98 {
99         if (conn == NULL)
100                 return -1;
101
102         static int subs_serv_id = -1;
103         static int subs_client_id = -1;
104
105         if (subscribe) {
106                 if (subs_serv_id == -1) {
107                         subs_serv_id = g_dbus_connection_signal_subscribe(conn,
108                                 NULL, BT_NETWORK_SERVER_INTERFACE,
109                                 NULL, NULL, NULL, 0,
110                                 __bt_manager_event_filter,
111                                 NULL, NULL);
112                 }
113                 if (subs_client_id == -1) {
114                         subs_client_id = g_dbus_connection_signal_subscribe(conn,
115                                 NULL, BT_NETWORK_CLIENT_INTERFACE,
116                                 NULL, NULL, NULL, 0,
117                                 __bt_manager_event_filter,
118                                 NULL, NULL);
119                 }
120         } else {
121                 if (subs_serv_id != -1) {
122                         g_dbus_connection_signal_unsubscribe(conn,
123                                         subs_serv_id);
124                         subs_serv_id = -1;
125                 }
126                 if (subs_client_id != -1) {
127                         g_dbus_connection_signal_unsubscribe(conn,
128                                         subs_client_id);
129                         subs_client_id = -1;
130                 }
131         }
132         return 0;
133 }
134
135 static int __bt_init_manager_receiver(void)
136 {
137         BT_DBG("+");
138
139         GError *error = NULL;
140
141         if (manager_conn == NULL) {
142                 manager_conn =  g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
143                 if (error != NULL) {
144                         BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
145                         g_clear_error(&error);
146                 }
147
148                 retv_if(manager_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
149         }
150
151         if (_bt_register_network_subscribe_signal(manager_conn, TRUE) == -1) {
152                 BT_DBG("_bt_register_network_subscribe_signal Failed..");
153                 goto fail;
154         }
155
156
157         return BLUETOOTH_ERROR_NONE;
158 fail:
159         if (manager_conn) {
160                 g_object_unref(manager_conn);
161                 manager_conn = NULL;
162         }
163
164         BT_DBG("-");
165
166         return BLUETOOTH_ERROR_INTERNAL;
167 }
168
169 /* To receive the event from bluez */
170 int _bt_init_pan_nap_event_receiver(void)
171 {
172         BT_DBG("+");
173
174         int result;
175
176         result = __bt_init_manager_receiver();
177         retv_if(result != BLUETOOTH_ERROR_NONE, result);
178
179         BT_DBG("-");
180
181         return BLUETOOTH_ERROR_NONE;
182 }
183
184 void _bt_deinit_pan_nap_event_receiver(void)
185 {
186         BT_DBG("+");
187
188         _bt_register_network_subscribe_signal(manager_conn, FALSE);
189
190         if (manager_conn) {
191                 g_object_unref(manager_conn);
192                 manager_conn = NULL;
193         }
194
195         BT_DBG("-");
196 }
197