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-common.h"
30 #include "bt-service-hid.h"
31 #include "bt-service-event.h"
32 #include "bt-service-util.h"
34 static void __bt_hid_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
37 GError *g_error = NULL;
38 GArray *out_param1 = NULL;
39 GArray *out_param2 = NULL;
40 bluetooth_device_address_t device_addr = { {0} };
41 int result = BLUETOOTH_ERROR_NONE;
42 bt_function_data_t *func_data;
43 request_info_t *req_info;
45 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
47 g_object_unref(proxy);
49 func_data = user_data;
51 if (func_data == NULL) {
53 BT_ERR("func_data == NULL");
57 req_info = _bt_get_request_info(func_data->req_id);
58 if (req_info == NULL) {
59 BT_ERR("req_info == NULL");
63 if (g_error != NULL) {
64 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
65 result = BLUETOOTH_ERROR_INTERNAL;
70 if (req_info->context == NULL)
73 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
74 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
76 _bt_convert_addr_string_to_type(device_addr.addr,
79 g_array_append_vals(out_param1, &device_addr,
80 sizeof(bluetooth_device_address_t));
81 g_array_append_vals(out_param2, &result, sizeof(int));
83 dbus_g_method_return(req_info->context, out_param1, out_param2);
85 g_array_free(out_param1, TRUE);
86 g_array_free(out_param2, TRUE);
88 _bt_delete_request_list(req_info->req_id);
91 g_error_free(g_error);
94 g_free(func_data->address);
99 static void __bt_hid_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
102 GError *g_error = NULL;
103 GArray *out_param1 = NULL;
104 GArray *out_param2 = NULL;
105 bluetooth_device_address_t device_addr = { {0} };
106 int result = BLUETOOTH_ERROR_NONE;
107 bt_function_data_t *func_data;
108 request_info_t *req_info;
110 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
112 g_object_unref(proxy);
114 func_data = user_data;
116 if (func_data == NULL) {
118 BT_ERR("func_data == NULL");
122 req_info = _bt_get_request_info(func_data->req_id);
123 if (req_info == NULL) {
124 BT_ERR("req_info == NULL");
128 if (g_error != NULL) {
129 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
130 result = BLUETOOTH_ERROR_INTERNAL;
135 if (req_info->context == NULL)
138 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
139 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
141 _bt_convert_addr_string_to_type(device_addr.addr,
144 g_array_append_vals(out_param1, &device_addr,
145 sizeof(bluetooth_device_address_t));
146 g_array_append_vals(out_param2, &result, sizeof(int));
148 dbus_g_method_return(req_info->context, out_param1, out_param2);
150 g_array_free(out_param1, TRUE);
151 g_array_free(out_param2, TRUE);
153 _bt_delete_request_list(req_info->req_id);
156 g_error_free(g_error);
159 g_free(func_data->address);
165 /**********************************************************************
167 ***********************************************************************/
169 int _bt_hid_connect(int request_id,
170 bluetooth_device_address_t *device_address)
172 gchar *device_path = NULL;
173 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
174 bt_function_data_t *func_data;
175 DBusGProxy *adapter_proxy;
176 DBusGProxy *hid_proxy;
177 DBusGConnection *conn;
179 BT_CHECK_PARAMETER(device_address, return);
181 _bt_convert_addr_type_to_string(address, device_address->addr);
183 adapter_proxy = _bt_get_adapter_proxy();
184 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
186 device_path = _bt_get_device_object_path(address);
187 if (device_path == NULL) {
188 BT_ERR("No paired device");
189 return BLUETOOTH_ERROR_NOT_PAIRED;
192 conn = _bt_get_system_gconn();
193 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
195 hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
196 device_path, BT_DEVICE_INTERFACE);
199 retv_if(hid_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
200 func_data = g_malloc0(sizeof(bt_function_data_t));
202 func_data->address = g_strdup(address);
203 func_data->req_id = request_id;
205 if (!dbus_g_proxy_begin_call(hid_proxy, "Connect",
206 (DBusGProxyCallNotify)__bt_hid_connect_cb,
209 BT_ERR("Hidh connect Dbus Call Error");
210 g_object_unref(hid_proxy);
212 g_free(func_data->address);
214 return BLUETOOTH_ERROR_INTERNAL;
217 return BLUETOOTH_ERROR_NONE;
220 int _bt_hid_disconnect(int request_id,
221 bluetooth_device_address_t *device_address)
223 gchar *device_path = NULL;
224 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
225 bt_function_data_t *func_data;
226 DBusGProxy *adapter_proxy;
227 DBusGProxy *hid_proxy;
228 DBusGConnection *conn;
230 BT_CHECK_PARAMETER(device_address, return);
232 _bt_convert_addr_type_to_string(address, device_address->addr);
234 adapter_proxy = _bt_get_adapter_proxy();
235 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
237 device_path = _bt_get_device_object_path(address);
238 if (device_path == NULL) {
239 BT_ERR("No paired device");
240 return BLUETOOTH_ERROR_NOT_PAIRED;
243 conn = _bt_get_system_gconn();
244 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
246 hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
247 device_path, BT_DEVICE_INTERFACE);
250 retv_if(hid_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
251 func_data = g_malloc0(sizeof(bt_function_data_t));
253 func_data->address = g_strdup(address);
254 func_data->req_id = request_id;
256 if (!dbus_g_proxy_begin_call(hid_proxy, "Disconnect",
257 (DBusGProxyCallNotify)__bt_hid_disconnect_cb,
260 BT_ERR("Hidh disconnect Dbus Call Error");
261 g_object_unref(hid_proxy);
263 g_free(func_data->address);
265 return BLUETOOTH_ERROR_INTERNAL;
268 return BLUETOOTH_ERROR_NONE;