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 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;
365 BT_CHECK_PARAMETER(device_address, return);
368 case BLUETOOTH_NETWORK_PANU_ROLE:
369 g_strlcpy(remote_role, PANU_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
372 case BLUETOOTH_NETWORK_NAP_ROLE:
373 g_strlcpy(remote_role, NAP_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
376 case BLUETOOTH_NETWORK_GN_ROLE:
377 g_strlcpy(remote_role, GN_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
380 BT_ERR("Unknown role");
381 return BLUETOOTH_ERROR_INTERNAL;
384 adapter_proxy = _bt_get_adapter_proxy();
385 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
387 conn = _bt_gdbus_get_system_gconn();
388 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
390 _bt_convert_addr_type_to_string(address, device_address->addr);
392 device_path = _bt_get_device_object_path(address);
393 if (device_path == NULL) {
394 BT_ERR("No paired device");
395 return BLUETOOTH_ERROR_NOT_PAIRED;
398 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
400 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
403 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
404 func_data = g_malloc0(sizeof(bt_function_data_t));
405 func_data->address = g_strdup(address);
406 func_data->req_id = request_id;
408 g_dbus_proxy_call(profile_proxy, "Connect",
409 g_variant_new("(s)", remote_role),
410 G_DBUS_CALL_FLAGS_NONE,
413 (GAsyncReadyCallback)__bt_network_connect_cb,
416 return BLUETOOTH_ERROR_NONE;
419 int _bt_network_disconnect(int request_id,
420 bluetooth_device_address_t *device_address)
422 gchar *device_path = NULL;
423 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
424 bt_function_data_t *func_data;
425 GDBusProxy *adapter_proxy;
426 GDBusProxy *profile_proxy;
427 GDBusConnection *conn;
429 BT_CHECK_PARAMETER(device_address, return);
431 adapter_proxy = _bt_get_adapter_proxy();
432 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
434 conn = _bt_gdbus_get_system_gconn();
435 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
437 _bt_convert_addr_type_to_string(address, device_address->addr);
439 device_path = _bt_get_device_object_path(address);
440 if (device_path == NULL) {
441 BT_ERR("No paired device");
442 return BLUETOOTH_ERROR_NOT_PAIRED;
445 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
447 device_path, BT_NETWORK_CLIENT_INTERFACE, NULL, NULL);
450 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
451 func_data = g_malloc0(sizeof(bt_function_data_t));
452 func_data->address = g_strdup(address);
453 func_data->req_id = request_id;
455 g_dbus_proxy_call(profile_proxy, "Disconnect",
457 G_DBUS_CALL_FLAGS_NONE,
460 (GAsyncReadyCallback)__bt_network_disconnect_cb,
463 return BLUETOOTH_ERROR_NONE;
466 int _bt_network_server_disconnect(int request_id,
467 bluetooth_device_address_t *device_address)
469 gchar *adapter_path = NULL;
470 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
471 bt_function_data_t *func_data;
472 GDBusProxy *profile_proxy;
473 GDBusConnection *conn;
475 BT_CHECK_PARAMETER(device_address, return);
477 conn = _bt_gdbus_get_system_gconn();
478 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
480 adapter_path = _bt_get_adapter_path();
481 if (adapter_path == NULL) {
482 BT_ERR("No adapter found");
483 return BLUETOOTH_ERROR_INTERNAL;
486 _bt_convert_addr_type_to_string(address, device_address->addr);
488 profile_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
490 adapter_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
491 g_free(adapter_path);
492 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
493 func_data = g_malloc0(sizeof(bt_function_data_t));
495 func_data->address = g_strdup(address);
496 func_data->req_id = request_id;
498 g_dbus_proxy_call(profile_proxy, "Disconnect",
499 g_variant_new("(s)", address),
500 G_DBUS_CALL_FLAGS_NONE,
503 (GAsyncReadyCallback)__bt_network_disconnect_cb,
506 return BLUETOOTH_ERROR_NONE;