4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dbus/dbus-glib.h>
21 #include <dbus/dbus.h>
25 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
26 #include <syspopup_caller.h>
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"
34 static void __bt_network_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
37 GError *g_error = 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;
46 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_STRING, &device, G_TYPE_INVALID);
48 g_object_unref(proxy);
50 func_data = user_data;
52 if (func_data == NULL) {
54 BT_ERR("func_data == NULL");
58 req_info = _bt_get_request_info(func_data->req_id);
59 if (req_info == NULL) {
60 BT_ERR("req_info == NULL");
64 if (g_error != NULL) {
65 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
66 result = BLUETOOTH_ERROR_INTERNAL;
71 if (req_info->context == NULL)
74 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
75 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
77 _bt_convert_addr_string_to_type(device_addr.addr,
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));
84 dbus_g_method_return(req_info->context, out_param1, out_param2);
86 g_array_free(out_param1, TRUE);
87 g_array_free(out_param2, TRUE);
89 _bt_delete_request_list(req_info->req_id);
92 g_error_free(g_error);
95 g_free(func_data->address);
100 static void __bt_network_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
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;
111 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
113 g_object_unref(proxy);
115 func_data = user_data;
117 if (func_data == NULL) {
119 BT_ERR("func_data == NULL");
123 req_info = _bt_get_request_info(func_data->req_id);
124 if (req_info == NULL) {
125 BT_ERR("req_info == NULL");
129 if (g_error != NULL) {
130 BT_ERR("Network Connect Dbus Call Error: %s\n", g_error->message);
131 result = BLUETOOTH_ERROR_INTERNAL;
136 if (req_info->context == NULL)
139 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
140 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
142 _bt_convert_addr_string_to_type(device_addr.addr,
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));
149 dbus_g_method_return(req_info->context, out_param1, out_param2);
151 g_array_free(out_param1, TRUE);
152 g_array_free(out_param2, TRUE);
154 _bt_delete_request_list(req_info->req_id);
157 g_error_free(g_error);
160 g_free(func_data->address);
165 int _bt_network_activate(void)
167 int ret = BLUETOOTH_ERROR_NONE;
170 DBusGConnection *conn;
171 DBusGProxy *server_proxy;
173 conn = _bt_get_system_gconn();
174 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
176 adapter_path = _bt_get_adapter_path();
177 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
179 server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
180 adapter_path, BT_NETWORK_SERVER_INTERFACE);
182 g_free(adapter_path);
184 if (server_proxy == NULL) {
185 BT_ERR("Failed to get the network server proxy\n");
186 return BLUETOOTH_ERROR_INTERNAL;
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)) {
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;
198 ret = BLUETOOTH_ERROR_INTERNAL;
204 g_object_unref(server_proxy);
209 int _bt_network_deactivate(void)
213 DBusGConnection *conn;
214 DBusGProxy *server_proxy;
216 conn = _bt_get_system_gconn();
217 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
219 adapter_path = _bt_get_adapter_path();
220 retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
222 server_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
223 adapter_path, BT_NETWORK_SERVER_INTERFACE);
225 g_free(adapter_path);
227 if (server_proxy == NULL) {
228 BT_ERR("Failed to get the network server proxy\n");
229 return BLUETOOTH_ERROR_INTERNAL;
232 if (!dbus_g_proxy_call(server_proxy, "Unregister", &err,
233 G_TYPE_STRING, NAP_UUID_NAME,
234 G_TYPE_INVALID, G_TYPE_INVALID)) {
236 BT_ERR("Network server unregister Error: %s\n", err->message);
239 g_object_unref(server_proxy);
240 return BLUETOOTH_ERROR_INTERNAL;
243 g_object_unref(server_proxy);
245 return BLUETOOTH_ERROR_NONE;
248 int _bt_network_connect(int request_id, int role,
249 bluetooth_device_address_t *device_address)
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;
259 BT_CHECK_PARAMETER(device_address, return);
262 case BLUETOOTH_NETWORK_PANU_ROLE:
263 g_strlcpy(remote_role, PANU_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
266 case BLUETOOTH_NETWORK_NAP_ROLE:
267 g_strlcpy(remote_role, NAP_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
270 case BLUETOOTH_NETWORK_GN_ROLE:
271 g_strlcpy(remote_role, GN_UUID_NAME, BLUETOOTH_UUID_STRING_MAX);
274 BT_ERR("Unknown role");
275 return BLUETOOTH_ERROR_INTERNAL;
278 adapter_proxy = _bt_get_adapter_proxy();
279 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
281 conn = _bt_get_system_gconn();
282 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
284 _bt_convert_addr_type_to_string(address, device_address->addr);
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);
290 if (device_path == NULL) {
291 BT_ERR("No paired device");
292 return BLUETOOTH_ERROR_NOT_PAIRED;
295 profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
296 device_path, BT_NETWORK_CLIENT_INTERFACE);
298 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
299 func_data = g_malloc0(sizeof(bt_function_data_t));
301 func_data->address = g_strdup(address);
302 func_data->req_id = request_id;
304 if (!dbus_g_proxy_begin_call(profile_proxy, "Connect",
305 (DBusGProxyCallNotify)__bt_network_connect_cb,
307 G_TYPE_STRING, remote_role,
309 BT_ERR("network connect Dbus Call Error");
310 g_object_unref(profile_proxy);
311 return BLUETOOTH_ERROR_INTERNAL;
314 return BLUETOOTH_ERROR_NONE;
317 int _bt_network_disconnect(int request_id,
318 bluetooth_device_address_t *device_address)
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;
327 BT_CHECK_PARAMETER(device_address, return);
329 adapter_proxy = _bt_get_adapter_proxy();
330 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
332 conn = _bt_get_system_gconn();
333 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
335 _bt_convert_addr_type_to_string(address, device_address->addr);
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);
341 if (device_path == NULL) {
342 BT_ERR("No paired device");
343 return BLUETOOTH_ERROR_NOT_PAIRED;
346 profile_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
347 device_path, BT_NETWORK_CLIENT_INTERFACE);
349 retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
350 func_data = g_malloc0(sizeof(bt_function_data_t));
352 func_data->address = g_strdup(address);
353 func_data->req_id = request_id;
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;
363 return BLUETOOTH_ERROR_NONE;