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.
23 #include <syspopup_caller.h>
24 #include <net_connection.h>
26 #include "bluetooth-api.h"
27 #include "bt-service-network.h"
28 #include "bt-service-common.h"
29 #include "bt-service-event.h"
30 #include "bt-service-util.h"
31 #include "bt-internal-types.h"
33 void _bt_util_addr_type_to_addr_net_string(char *address,
36 ret_if(address == NULL);
39 snprintf(address, BT_ADDRESS_STR_LEN, "%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X", addr[0],
40 addr[1], addr[2], addr[3], addr[4], addr[5]);
43 static connection_profile_h __bt_get_net_profile(void *connection,
44 connection_iterator_type_e type,
45 unsigned char *address)
49 char net_address[BT_ADDRESS_STR_LEN + 1] = { 0 };
50 char *profile_name = NULL;
51 connection_profile_iterator_h profile_iter;
52 connection_profile_h profile_h;
53 connection_profile_type_e profile_type;
55 retv_if(connection == NULL, NULL);
56 retv_if(address == NULL, NULL);
58 BT_DBG("net_conn: %x", connection);
60 _bt_util_addr_type_to_addr_net_string(net_address, address);
62 result = connection_get_profile_iterator(connection,
65 if (result != CONNECTION_ERROR_NONE) {
66 BT_ERR("Fail to get profile iterator [%d]", result);
70 while (connection_profile_iterator_has_next(profile_iter)) {
75 if (connection_profile_iterator_next(profile_iter,
76 &profile_h) != CONNECTION_ERROR_NONE) {
77 BT_ERR("Fail to get profile handle");
81 if (connection_profile_get_type(profile_h,
82 &profile_type) != CONNECTION_ERROR_NONE) {
83 BT_ERR("Fail to get profile type");
87 if (profile_type != CONNECTION_PROFILE_TYPE_BT)
90 if (connection_profile_get_name(profile_h,
91 &profile_name) != CONNECTION_ERROR_NONE) {
92 BT_ERR("Fail to get profile name");
96 split_string = g_strsplit(profile_name, "_", 3);
100 if (g_strv_length(split_string) < 3)
103 if (g_ascii_strcasecmp(split_string[2], net_address) == 0) {
104 BT_DBG("matched profile");
105 g_strfreev(split_string);
109 g_strfreev(split_string);
115 int _bt_is_network_connected(void *connection, unsigned char *address,
116 gboolean *is_connected)
119 handle = __bt_get_net_profile(connection,
120 CONNECTION_ITERATOR_TYPE_CONNECTED,
123 *is_connected = TRUE;
125 *is_connected = FALSE;
127 return BLUETOOTH_ERROR_NONE;
130 static void __bt_network_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
133 GError *g_error = NULL;
134 GVariant *out_param1 = NULL;
135 GVariant *reply = NULL;
136 bluetooth_device_address_t device_addr = { {0} };
137 int result = BLUETOOTH_ERROR_NONE;
138 bt_function_data_t *func_data;
139 request_info_t *req_info;
141 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
142 g_object_unref(proxy);
145 BT_ERR("Network Connect Dbus Call Error");
147 BT_ERR("Error: %s\n", g_error->message);
148 g_clear_error(&g_error);
150 result = BLUETOOTH_ERROR_INTERNAL;
152 g_variant_unref(reply);
155 func_data = user_data;
156 if (func_data == NULL) {
158 BT_ERR("func_data == NULL");
162 BT_ERR("func_data->req_id: %d", func_data->req_id);
163 req_info = _bt_get_request_info(func_data->req_id);
164 if (req_info == NULL) {
165 BT_ERR("req_info == NULL");
169 if (req_info->context == NULL)
172 _bt_convert_addr_string_to_type(device_addr.addr,
175 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
176 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
178 g_dbus_method_invocation_return_value(req_info->context,
179 g_variant_new("iv", result, out_param1));
181 _bt_delete_request_list(req_info->req_id);
185 g_free(func_data->address);
190 static void __bt_network_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
193 GError *g_error = NULL;
194 GVariant *out_param1 = NULL;
195 GVariant *reply = NULL;
196 bluetooth_device_address_t device_addr = { {0} };
197 int result = BLUETOOTH_ERROR_NONE;
198 bt_function_data_t *func_data;
199 request_info_t *req_info;
201 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
202 g_object_unref(proxy);
205 BT_ERR("Network Disconnect Dbus Call Error");
207 BT_ERR("Error: %s\n", g_error->message);
208 g_clear_error(&g_error);
210 result = BLUETOOTH_ERROR_INTERNAL;
212 g_variant_unref(reply);
215 func_data = user_data;
216 if (func_data == NULL) {
218 BT_ERR("func_data == NULL");
221 BT_ERR("func_data->req_id: %d", func_data->req_id);
222 req_info = _bt_get_request_info(func_data->req_id);
223 if (req_info == NULL) {
224 BT_ERR("req_info == NULL");
228 if (g_error != NULL) {
229 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
230 result = BLUETOOTH_ERROR_INTERNAL;
233 if (req_info->context == NULL)
236 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
237 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
239 g_dbus_method_invocation_return_value(req_info->context,
240 g_variant_new("iv", result, out_param1));
242 _bt_delete_request_list(req_info->req_id);
246 g_free(func_data->address);
251 int _bt_network_activate(void)
253 int ret = BLUETOOTH_ERROR_NONE;
255 GVariant *result = NULL;
257 GDBusConnection *conn;
258 GDBusProxy *server_proxy;
260 conn = _bt_gdbus_get_system_gconn();
261 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
263 adapter_path = _bt_get_adapter_path();
264 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
266 server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
268 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
269 g_free(adapter_path);
271 if (server_proxy == NULL) {
272 BT_ERR("Failed to get the network server proxy\n");
273 return BLUETOOTH_ERROR_INTERNAL;
276 result = g_dbus_proxy_call_sync(server_proxy, "Register",
277 g_variant_new("(ss)", NAP_UUID_NAME, NET_BRIDGE_INTERFACE),
278 G_DBUS_CALL_FLAGS_NONE,
282 if (result == NULL) {
284 g_dbus_error_strip_remote_error(err);
285 BT_ERR("Network server register Error: %s\n", err->message);
286 if (g_strcmp0(err->message, "Already Exists") == 0)
287 ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
289 ret = BLUETOOTH_ERROR_INTERNAL;
294 g_variant_unref(result);
297 g_object_unref(server_proxy);
302 int _bt_network_deactivate(void)
305 GVariant *result = NULL;
307 GDBusConnection *conn;
308 GDBusProxy *server_proxy;
309 int ret = BLUETOOTH_ERROR_NONE;
311 conn = _bt_gdbus_get_system_gconn();
312 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
314 adapter_path = _bt_get_adapter_path();
315 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
317 server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
319 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
320 g_free(adapter_path);
322 if (server_proxy == NULL) {
323 BT_ERR("Failed to get the network server proxy\n");
324 return BLUETOOTH_ERROR_INTERNAL;
327 result = g_dbus_proxy_call_sync(server_proxy, "Unregister",
328 g_variant_new("(s)", NAP_UUID_NAME),
329 G_DBUS_CALL_FLAGS_NONE,
333 if (result == NULL) {
335 g_dbus_error_strip_remote_error(err);
336 BT_ERR("Network server unregister Error: %s\n", err->message);
337 if (g_strcmp0(err->message,
338 "Operation currently not available") == 0) {
339 ret = BLUETOOTH_ERROR_ALREADY_DEACTIVATED;
341 ret = BLUETOOTH_ERROR_INTERNAL;
346 g_variant_unref(result);
349 g_object_unref(server_proxy);
354 int _bt_network_connect(int request_id, int role,
355 bluetooth_device_address_t *device_address)
357 const gchar *device_path = NULL;
358 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
359 char remote_role[BLUETOOTH_UUID_STRING_MAX] = { 0 };
360 bt_function_data_t *func_data;
361 GDBusProxy *adapter_proxy;
362 GDBusProxy *profile_proxy;
363 GDBusConnection *conn;
364 GVariant *result = NULL;
367 BT_CHECK_PARAMETER(device_address, return);
370 case BLUETOOTH_NETWORK_PANU_ROLE:
371 g_strlcpy(remote_role, PANU_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
374 case BLUETOOTH_NETWORK_NAP_ROLE:
375 g_strlcpy(remote_role, NAP_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
378 case BLUETOOTH_NETWORK_GN_ROLE:
379 g_strlcpy(remote_role, GN_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
382 BT_ERR("Unknown role");
383 return BLUETOOTH_ERROR_INTERNAL;
386 adapter_proxy = _bt_get_adapter_proxy();
387 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
389 conn = _bt_gdbus_get_system_gconn();
390 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
392 _bt_convert_addr_type_to_string(address, device_address->addr);
394 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
395 g_variant_new("(s)", address),
396 G_DBUS_CALL_FLAGS_NONE,
398 if (result == NULL) {
399 BT_ERR("Error occurred in call to FindDevice");
401 BT_ERR("Error: %s", err->message);
404 return BLUETOOTH_ERROR_INTERNAL;
407 device_path = g_variant_get_string(result, NULL);
408 if (device_path == NULL) {
409 BT_ERR("No paired device");
410 g_variant_unref(result);
411 return BLUETOOTH_ERROR_NOT_PAIRED;
414 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
416 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
418 g_variant_unref(result);
419 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
420 func_data = g_malloc0(sizeof(bt_function_data_t));
421 func_data->address = g_strdup(address);
422 func_data->req_id = request_id;
424 g_dbus_proxy_call(profile_proxy, "Connect",
425 g_variant_new("(s)", remote_role),
426 G_DBUS_CALL_FLAGS_NONE,
429 (GAsyncReadyCallback)__bt_network_connect_cb,
432 return BLUETOOTH_ERROR_NONE;
435 int _bt_network_disconnect(int request_id,
436 bluetooth_device_address_t *device_address)
438 const gchar *device_path = NULL;
439 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
440 bt_function_data_t *func_data;
441 GDBusProxy *adapter_proxy;
442 GDBusProxy *profile_proxy;
443 GDBusConnection *conn;
444 GVariant *result = NULL;
447 BT_CHECK_PARAMETER(device_address, return);
449 adapter_proxy = _bt_get_adapter_proxy();
450 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
452 conn = _bt_gdbus_get_system_gconn();
453 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
455 _bt_convert_addr_type_to_string(address, device_address->addr);
457 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
458 g_variant_new("(s)", address),
459 G_DBUS_CALL_FLAGS_NONE,
461 if (result == NULL) {
462 BT_ERR("Error occurred in call to FindDevice");
464 BT_ERR("Error: %s", err->message);
467 return BLUETOOTH_ERROR_INTERNAL;
470 device_path = g_variant_get_string(result, NULL);
471 if (device_path == NULL) {
472 BT_ERR("No paired device");
473 g_variant_unref(result);
474 return BLUETOOTH_ERROR_NOT_PAIRED;
477 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
479 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
481 g_variant_unref(result);
482 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
483 func_data = g_malloc0(sizeof(bt_function_data_t));
484 func_data->address = g_strdup(address);
485 func_data->req_id = request_id;
487 g_dbus_proxy_call(profile_proxy, "Disconnect",
489 G_DBUS_CALL_FLAGS_NONE,
492 (GAsyncReadyCallback)__bt_network_disconnect_cb,
495 return BLUETOOTH_ERROR_NONE;
498 int _bt_network_server_disconnect(int request_id,
499 bluetooth_device_address_t *device_address)
501 gchar *adapter_path = NULL;
502 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
503 bt_function_data_t *func_data;
504 GDBusProxy *profile_proxy;
505 GDBusConnection *conn;
507 BT_CHECK_PARAMETER(device_address, return);
509 conn = _bt_gdbus_get_system_gconn();
510 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
512 adapter_path = _bt_get_adapter_path();
513 if (adapter_path == NULL) {
514 BT_ERR("No adapter found");
515 return BLUETOOTH_ERROR_INTERNAL;
518 _bt_convert_addr_type_to_string(address, device_address->addr);
520 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
522 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
523 g_free(adapter_path);
524 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
525 func_data = g_malloc0(sizeof(bt_function_data_t));
527 func_data->address = g_strdup(address);
528 func_data->req_id = request_id;
530 g_dbus_proxy_call(profile_proxy, "Disconnect",
531 g_variant_new("(s)", address),
532 G_DBUS_CALL_FLAGS_NONE,
535 (GAsyncReadyCallback)__bt_network_disconnect_cb,
538 return BLUETOOTH_ERROR_NONE;