Rollback changes to submit TIZEN:COMMON project
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-network.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 <dbus/dbus-glib.h>
21 #include <dbus/dbus.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
26 #include <syspopup_caller.h>
27 #endif
28 #include "bluetooth-api.h"
29 #include "bt-service-network.h"
30 #include "bt-service-common.h"
31 #include "bt-service-event.h"
32 #include "bt-service-util.h"
33
34 static void __bt_network_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
35                                     gpointer user_data)
36 {
37         GError *g_error = NULL;
38         char *device = NULL;
39         GArray *out_param1 = NULL;
40         GArray *out_param2 = NULL;
41         bluetooth_device_address_t device_addr = { {0} };
42         int result = BLUETOOTH_ERROR_NONE;
43         bt_function_data_t *func_data;
44         request_info_t *req_info;
45
46         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_STRING, &device, G_TYPE_INVALID);
47
48         g_object_unref(proxy);
49
50         func_data = user_data;
51
52         if (func_data == NULL) {
53                 /* Send reply */
54                 BT_ERR("func_data == NULL");
55                 goto done;
56         }
57
58         req_info = _bt_get_request_info(func_data->req_id);
59         if (req_info == NULL) {
60                 BT_ERR("req_info == NULL");
61                 goto done;
62         }
63
64         if (g_error != NULL) {
65                 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
66                 result = BLUETOOTH_ERROR_INTERNAL;
67                 goto dbus_return;
68         }
69
70 dbus_return:
71         if (req_info->context == NULL)
72                 goto done;
73
74         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
75         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
76
77         _bt_convert_addr_string_to_type(device_addr.addr,
78                                         func_data->address);
79
80         g_array_append_vals(out_param1, &device_addr,
81                                 sizeof(bluetooth_device_address_t));
82         g_array_append_vals(out_param2, &result, sizeof(int));
83
84         dbus_g_method_return(req_info->context, out_param1, out_param2);
85
86         g_array_free(out_param1, TRUE);
87         g_array_free(out_param2, TRUE);
88
89         _bt_delete_request_list(req_info->req_id);
90 done:
91         if (g_error)
92                 g_error_free(g_error);
93
94         if (func_data) {
95                 g_free(func_data->address);
96                 g_free(func_data);
97         }
98 }
99
100 static void __bt_network_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
101                                     gpointer user_data)
102 {
103         GError *g_error = NULL;
104         GArray *out_param1 = NULL;
105         GArray *out_param2 = NULL;
106         bluetooth_device_address_t device_addr = { {0} };
107         int result = BLUETOOTH_ERROR_NONE;
108         bt_function_data_t *func_data;
109         request_info_t *req_info;
110
111         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
112
113         g_object_unref(proxy);
114
115         func_data = user_data;
116
117         if (func_data == NULL) {
118                 /* Send reply */
119                 BT_ERR("func_data == NULL");
120                 goto done;
121         }
122
123         req_info = _bt_get_request_info(func_data->req_id);
124         if (req_info == NULL) {
125                 BT_ERR("req_info == NULL");
126                 goto done;
127         }
128
129         if (g_error != NULL) {
130                 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
131                 result = BLUETOOTH_ERROR_INTERNAL;
132                 goto dbus_return;
133         }
134
135 dbus_return:
136         if (req_info->context == NULL)
137                 goto done;
138
139         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
140         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
141
142         _bt_convert_addr_string_to_type(device_addr.addr,
143                                         func_data->address);
144
145         g_array_append_vals(out_param1, &device_addr,
146                                 sizeof(bluetooth_device_address_t));
147         g_array_append_vals(out_param2, &result, sizeof(int));
148
149         dbus_g_method_return(req_info->context, out_param1, out_param2);
150
151         g_array_free(out_param1, TRUE);
152         g_array_free(out_param2, TRUE);
153
154         _bt_delete_request_list(req_info->req_id);
155 done:
156         if (g_error)
157                 g_error_free(g_error);
158
159         if (func_data) {
160                 g_free(func_data->address);
161                 g_free(func_data);
162         }
163 }
164
165 int _bt_network_activate(void)
166 {
167         int ret = BLUETOOTH_ERROR_NONE;
168         char *adapter_path;
169         GError *err = NULL;
170         DBusGConnection *conn;
171         DBusGProxy *server_proxy;
172
173         conn = _bt_get_system_gconn();
174         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
175
176         adapter_path = _bt_get_adapter_path();
177         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
178
179         server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
180                              adapter_path, BT_NETWORK_SERVER_INTERFACE);
181
182         g_free(adapter_path);
183
184         if (server_proxy == NULL) {
185                 BT_ERR("Failed to get the network server proxy\n");
186                 return BLUETOOTH_ERROR_INTERNAL;
187         }
188
189         if (!dbus_g_proxy_call(server_proxy, "Register", &err,
190                         G_TYPE_STRING, NAP_UUID_NAME,
191                         G_TYPE_STRING, NET_BRIDGE_INTERFACE,
192                         G_TYPE_INVALID, G_TYPE_INVALID)) {
193                 if (err != NULL) {
194                         BT_ERR("Network server register Error: %s\n", err->message);
195                         if (g_strcmp0(err->message, "Already Exists") == 0) {
196                                 ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
197                         } else {
198                                 ret = BLUETOOTH_ERROR_INTERNAL;
199                         }
200                         g_error_free(err);
201                 }
202         }
203
204         g_object_unref(server_proxy);
205
206         return ret;
207 }
208
209 int _bt_network_deactivate(void)
210 {
211         char *adapter_path;
212         GError *err = NULL;
213         DBusGConnection *conn;
214         DBusGProxy *server_proxy;
215
216         conn = _bt_get_system_gconn();
217         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
218
219         adapter_path = _bt_get_adapter_path();
220         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
221
222         server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
223                              adapter_path, BT_NETWORK_SERVER_INTERFACE);
224
225         g_free(adapter_path);
226
227         if (server_proxy == NULL) {
228                 BT_ERR("Failed to get the network server proxy\n");
229                 return BLUETOOTH_ERROR_INTERNAL;
230         }
231
232         if (!dbus_g_proxy_call(server_proxy, "Unregister", &err,
233                         G_TYPE_STRING, NAP_UUID_NAME,
234                         G_TYPE_INVALID, G_TYPE_INVALID)) {
235                 if (err != NULL) {
236                         BT_ERR("Network server unregister Error: %s\n", err->message);
237                         g_error_free(err);
238                 }
239                 g_object_unref(server_proxy);
240                 return BLUETOOTH_ERROR_INTERNAL;
241         }
242
243         g_object_unref(server_proxy);
244
245         return BLUETOOTH_ERROR_NONE;
246 }
247
248 int _bt_network_connect(int request_id, int role,
249                 bluetooth_device_address_t *device_address)
250 {
251         gchar *device_path = NULL;
252         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
253         char remote_role[BLUETOOTH_UUID_STRING_MAX] = { 0 };
254         bt_function_data_t *func_data;
255         DBusGProxy *adapter_proxy;
256         DBusGProxy *profile_proxy;
257         DBusGConnection *conn;
258
259         BT_CHECK_PARAMETER(device_address, return);
260
261         switch (role) {
262         case BLUETOOTH_NETWORK_PANU_ROLE:
263                 g_strlcpy(remote_role, PANU_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
264                 break;
265
266         case BLUETOOTH_NETWORK_NAP_ROLE:
267                 g_strlcpy(remote_role, NAP_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
268                 break;
269
270         case BLUETOOTH_NETWORK_GN_ROLE:
271                 g_strlcpy(remote_role, GN_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
272                 break;
273         default:
274                 BT_ERR("Unknown role");
275                 return BLUETOOTH_ERROR_INTERNAL;
276         }
277
278         adapter_proxy = _bt_get_adapter_proxy();
279         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
280
281         conn = _bt_get_system_gconn();
282         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
283
284         _bt_convert_addr_type_to_string(address, device_address->addr);
285
286         dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
287                           G_TYPE_STRING, address, G_TYPE_INVALID,
288                           DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
289
290         if (device_path == NULL) {
291                 BT_ERR("No paired device");
292                 return BLUETOOTH_ERROR_NOT_PAIRED;
293         }
294
295         profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
296                                       device_path, BT_NETWORK_CLIENT_INTERFACE);
297         g_free(device_path);
298         retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
299         func_data = g_malloc0(sizeof(bt_function_data_t));
300
301         func_data->address = g_strdup(address);
302         func_data->req_id = request_id;
303
304         if (!dbus_g_proxy_begin_call(profile_proxy, "Connect",
305                         (DBusGProxyCallNotify)__bt_network_connect_cb,
306                         func_data, NULL,
307                         G_TYPE_STRING, remote_role,
308                         G_TYPE_INVALID)) {
309                 BT_ERR("network connect Dbus Call Error");
310                 g_object_unref(profile_proxy);
311                 return BLUETOOTH_ERROR_INTERNAL;
312         }
313
314         return BLUETOOTH_ERROR_NONE;
315 }
316
317 int _bt_network_disconnect(int request_id,
318                 bluetooth_device_address_t *device_address)
319 {
320         gchar *device_path = NULL;
321         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
322         bt_function_data_t *func_data;
323         DBusGProxy *adapter_proxy;
324         DBusGProxy *profile_proxy;
325         DBusGConnection *conn;
326
327         BT_CHECK_PARAMETER(device_address, return);
328
329         adapter_proxy = _bt_get_adapter_proxy();
330         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
331
332         conn = _bt_get_system_gconn();
333         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
334
335         _bt_convert_addr_type_to_string(address, device_address->addr);
336
337         dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
338                           G_TYPE_STRING, address, G_TYPE_INVALID,
339                           DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
340
341         if (device_path == NULL) {
342                 BT_ERR("No paired device");
343                 return BLUETOOTH_ERROR_NOT_PAIRED;
344         }
345
346         profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
347                                       device_path, BT_NETWORK_CLIENT_INTERFACE);
348         g_free(device_path);
349         retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
350         func_data = g_malloc0(sizeof(bt_function_data_t));
351
352         func_data->address = g_strdup(address);
353         func_data->req_id = request_id;
354
355         if (!dbus_g_proxy_begin_call(profile_proxy, "Disconnect",
356                         (DBusGProxyCallNotify)__bt_network_disconnect_cb,
357                         func_data, NULL, G_TYPE_INVALID)) {
358                 BT_ERR("network disconnect Dbus Call Error");
359                 g_object_unref(profile_proxy);
360                 return BLUETOOTH_ERROR_INTERNAL;
361         }
362
363         return BLUETOOTH_ERROR_NONE;
364 }