4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
29 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
30 #include <syspopup_caller.h>
32 #include <net_connection.h>
34 #include "bluetooth-api.h"
35 #include "bt-service-network.h"
36 #include "bt-service-common.h"
37 #include "bt-service-event.h"
38 #include "bt-service-util.h"
39 #include "bt-internal-types.h"
41 void _bt_util_addr_type_to_addr_net_string(char *address,
44 ret_if(address == NULL);
47 snprintf(address, BT_ADDRESS_STR_LEN, "%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X", addr[0],
48 addr[1], addr[2], addr[3], addr[4], addr[5]);
51 static connection_profile_h __bt_get_net_profile(void *connection,
52 connection_iterator_type_e type,
53 unsigned char *address)
57 char net_address[BT_ADDRESS_STR_LEN + 1] = { 0 };
58 char *profile_name = NULL;
59 connection_profile_iterator_h profile_iter;
60 connection_profile_h profile_h;
61 connection_profile_type_e profile_type;
63 retv_if(connection == NULL, NULL);
64 retv_if(address == NULL, NULL);
66 BT_DBG("net_conn: %x", connection);
68 _bt_util_addr_type_to_addr_net_string(net_address, address);
70 result = connection_get_profile_iterator(connection,
73 if (result != CONNECTION_ERROR_NONE) {
74 BT_ERR("Fail to get profile iterator [%d]", result);
78 while (connection_profile_iterator_has_next(profile_iter)) {
83 if (connection_profile_iterator_next(profile_iter,
84 &profile_h) != CONNECTION_ERROR_NONE) {
85 BT_ERR("Fail to get profile handle");
89 if (connection_profile_get_type(profile_h,
90 &profile_type) != CONNECTION_ERROR_NONE) {
91 BT_ERR("Fail to get profile type");
95 if (profile_type != CONNECTION_PROFILE_TYPE_BT)
98 if (connection_profile_get_name(profile_h,
99 &profile_name) != CONNECTION_ERROR_NONE) {
100 BT_ERR("Fail to get profile name");
104 split_string = g_strsplit(profile_name, "_", 3);
106 g_free(profile_name);
108 if (g_strv_length(split_string) < 3)
111 if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
112 BT_DBG("matched profile");
113 g_strfreev(split_string);
117 g_strfreev(split_string);
123 int _bt_is_network_connected(void *connection, unsigned char *address,
124 gboolean *is_connected)
127 handle = __bt_get_net_profile(connection,
128 CONNECTION_ITERATOR_TYPE_CONNECTED,
131 *is_connected = TRUE;
133 *is_connected = FALSE;
135 return BLUETOOTH_ERROR_NONE;
138 static void __bt_network_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
141 GError *g_error = NULL;
142 GVariant *out_param1 = NULL;
143 GVariant *reply = NULL;
144 bluetooth_device_address_t device_addr = { {0} };
145 int result = BLUETOOTH_ERROR_NONE;
146 bt_function_data_t *func_data;
147 request_info_t *req_info;
149 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
150 g_object_unref(proxy);
153 BT_ERR("Network Connect Dbus Call Error");
155 BT_ERR("Error: %s\n", g_error->message);
156 g_clear_error(&g_error);
158 result = BLUETOOTH_ERROR_INTERNAL;
160 g_variant_unref(reply);
162 func_data = user_data;
163 if (func_data == NULL) {
165 BT_ERR("func_data == NULL");
169 BT_ERR("func_data->req_id: %d", func_data->req_id);
170 req_info = _bt_get_request_info(func_data->req_id);
171 if (req_info == NULL) {
172 BT_ERR("req_info == NULL");
176 if (req_info->context == NULL)
179 _bt_convert_addr_string_to_type(device_addr.addr,
182 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
183 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
185 g_dbus_method_invocation_return_value(req_info->context,
186 g_variant_new("iv", result, out_param1));
188 _bt_delete_request_list(req_info->req_id);
192 g_free(func_data->address);
197 static void __bt_network_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
200 GError *g_error = NULL;
201 GVariant *out_param1 = NULL;
202 GVariant *reply = NULL;
203 bluetooth_device_address_t device_addr = { {0} };
204 int result = BLUETOOTH_ERROR_NONE;
205 bt_function_data_t *func_data;
206 request_info_t *req_info;
208 g_dbus_proxy_call_finish(proxy, res, &g_error);
209 g_object_unref(proxy);
212 BT_ERR("Network Disconnect Dbus Call Error");
214 BT_ERR("Error: %s\n", g_error->message);
215 g_clear_error(&g_error);
217 result = BLUETOOTH_ERROR_INTERNAL;
219 g_variant_unref(reply);
221 func_data = user_data;
222 if (func_data == NULL) {
224 BT_ERR("func_data == NULL");
227 BT_ERR("func_data->req_id: %d", func_data->req_id);
228 req_info = _bt_get_request_info(func_data->req_id);
229 if (req_info == NULL) {
230 BT_ERR("req_info == NULL");
234 if (g_error != NULL) {
235 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
236 result = BLUETOOTH_ERROR_INTERNAL;
239 if (req_info->context == NULL)
242 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
243 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
245 g_dbus_method_invocation_return_value(req_info->context,
246 g_variant_new("iv", result, out_param1));
248 _bt_delete_request_list(req_info->req_id);
252 g_free(func_data->address);
257 int _bt_network_activate(void)
259 int ret = BLUETOOTH_ERROR_NONE;
262 GDBusConnection *conn;
263 GDBusProxy *server_proxy;
265 conn = _bt_get_system_gconn();
266 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
268 adapter_path = _bt_get_adapter_path();
269 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
271 server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
273 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
274 g_free(adapter_path);
276 if (server_proxy == NULL) {
277 BT_ERR("Failed to get the network server proxy\n");
278 return BLUETOOTH_ERROR_INTERNAL;
281 g_dbus_proxy_call_sync(server_proxy, "Register",
282 g_variant_new("(ss)", NAP_UUID_NAME, NET_BRIDGE_INTERFACE),
283 G_DBUS_CALL_FLAGS_NONE,
288 BT_ERR("Network server register Error: %s\n", err->message);
289 if (g_strcmp0(err->message, "Already Exists") == 0) {
290 ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
292 ret = BLUETOOTH_ERROR_INTERNAL;
296 g_object_unref(server_proxy);
301 int _bt_network_deactivate(void)
305 GDBusConnection *conn;
306 GDBusProxy *server_proxy;
307 int ret = BLUETOOTH_ERROR_NONE;
309 conn = _bt_get_system_gconn();
310 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
312 adapter_path = _bt_get_adapter_path();
313 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
315 server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
317 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
318 g_free(adapter_path);
320 if (server_proxy == NULL) {
321 BT_ERR("Failed to get the network server proxy\n");
322 return BLUETOOTH_ERROR_INTERNAL;
325 g_dbus_proxy_call_sync(server_proxy, "Unregister",
326 g_variant_new("(s)", NAP_UUID_NAME),
327 G_DBUS_CALL_FLAGS_NONE,
332 BT_ERR("Network server unregister Error: %s\n", err->message);
333 if (g_strcmp0(err->message,
334 "Operation currently not available") == 0) {
335 ret = BLUETOOTH_ERROR_ALREADY_DEACTIVATED;
337 ret = BLUETOOTH_ERROR_INTERNAL;
341 g_object_unref(server_proxy);
346 int _bt_network_connect(int request_id, int role,
347 bluetooth_device_address_t *device_address)
349 const gchar *device_path = NULL;
350 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
351 char remote_role[BLUETOOTH_UUID_STRING_MAX] = { 0 };
352 bt_function_data_t *func_data;
353 GDBusProxy *adapter_proxy;
354 GDBusProxy *profile_proxy;
355 GDBusConnection *conn;
356 GVariant *result = NULL;
359 BT_CHECK_PARAMETER(device_address, return);
362 case BLUETOOTH_NETWORK_PANU_ROLE:
363 g_strlcpy(remote_role, PANU_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
366 case BLUETOOTH_NETWORK_NAP_ROLE:
367 g_strlcpy(remote_role, NAP_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
370 case BLUETOOTH_NETWORK_GN_ROLE:
371 g_strlcpy(remote_role, GN_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
374 BT_ERR("Unknown role");
375 return BLUETOOTH_ERROR_INTERNAL;
378 adapter_proxy = _bt_get_adapter_proxy();
379 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
381 conn = _bt_get_system_gconn();
382 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
384 _bt_convert_addr_type_to_string(address, device_address->addr);
386 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
387 g_variant_new("(s)", address),
388 G_DBUS_CALL_FLAGS_NONE,
390 if (result == NULL) {
391 BT_ERR("Error occurred in call to FindDevice");
393 BT_ERR("Error: %s", err->message);
396 return BLUETOOTH_ERROR_INTERNAL;
399 device_path = g_variant_get_string(result, NULL);
400 if (device_path == NULL) {
401 BT_ERR("No paired device");
402 g_variant_unref(result);
403 return BLUETOOTH_ERROR_NOT_PAIRED;
406 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
408 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
410 g_variant_unref(result);
411 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
412 func_data = g_malloc0(sizeof(bt_function_data_t));
414 func_data->address = g_strdup(address);
415 func_data->req_id = request_id;
417 g_dbus_proxy_call(profile_proxy, "Connect",
418 g_variant_new("(s)", remote_role),
419 G_DBUS_CALL_FLAGS_NONE,
422 (GAsyncReadyCallback)__bt_network_connect_cb,
425 return BLUETOOTH_ERROR_NONE;
428 int _bt_network_disconnect(int request_id,
429 bluetooth_device_address_t *device_address)
431 const gchar *device_path = NULL;
432 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
433 bt_function_data_t *func_data;
434 GDBusProxy *adapter_proxy;
435 GDBusProxy *profile_proxy;
436 GDBusConnection *conn;
437 GVariant *result = NULL;
440 BT_CHECK_PARAMETER(device_address, return);
442 adapter_proxy = _bt_get_adapter_proxy();
443 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
445 conn = _bt_get_system_gconn();
446 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
448 _bt_convert_addr_type_to_string(address, device_address->addr);
450 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
451 g_variant_new("(s)", address),
452 G_DBUS_CALL_FLAGS_NONE,
454 if (result == NULL) {
455 BT_ERR("Error occurred in call to FindDevice");
457 BT_ERR("Error: %s", err->message);
460 return BLUETOOTH_ERROR_INTERNAL;
463 device_path = g_variant_get_string(result, NULL);
464 if (device_path == NULL) {
465 BT_ERR("No paired device");
466 return BLUETOOTH_ERROR_NOT_PAIRED;
469 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
471 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
473 g_variant_unref(result);
474 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
475 func_data = g_malloc0(sizeof(bt_function_data_t));
477 func_data->address = g_strdup(address);
478 func_data->req_id = request_id;
480 g_dbus_proxy_call(profile_proxy, "Disconnect",
482 G_DBUS_CALL_FLAGS_NONE,
485 (GAsyncReadyCallback)__bt_network_disconnect_cb,
488 return BLUETOOTH_ERROR_NONE;
491 int _bt_network_server_disconnect(int request_id,
492 bluetooth_device_address_t *device_address)
494 gchar *adapter_path = NULL;
495 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
496 bt_function_data_t *func_data;
497 GDBusProxy *profile_proxy;
498 GDBusConnection *conn;
500 BT_CHECK_PARAMETER(device_address, return);
502 conn = _bt_get_system_gconn();
503 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
505 adapter_path = _bt_get_adapter_path();
506 if (adapter_path == NULL) {
507 BT_ERR("No adapter found");
508 return BLUETOOTH_ERROR_INTERNAL;
511 _bt_convert_addr_type_to_string(address, device_address->addr);
513 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
515 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
516 g_free(adapter_path);
517 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
518 func_data = g_malloc0(sizeof(bt_function_data_t));
520 func_data->address = g_strdup(address);
521 func_data->req_id = request_id;
523 g_dbus_proxy_call(profile_proxy, "Disconnect",
524 g_variant_new("(s)", address),
525 G_DBUS_CALL_FLAGS_NONE,
528 (GAsyncReadyCallback)__bt_network_disconnect_cb,
531 g_free(func_data->address);
534 return BLUETOOTH_ERROR_NONE;