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 #include <syspopup_caller.h>
26 #include "bluetooth-api.h"
27 #include "bt-service-common.h"
28 #include "bt-service-hid.h"
29 #include "bt-service-event.h"
30 #include "bt-service-util.h"
32 static void __bt_hid_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
35 GError *g_error = NULL;
36 GArray *out_param1 = NULL;
37 GArray *out_param2 = NULL;
38 bluetooth_device_address_t device_addr = { {0} };
39 int result = BLUETOOTH_ERROR_NONE;
40 bt_function_data_t *func_data;
41 request_info_t *req_info;
43 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
45 g_object_unref(proxy);
47 func_data = user_data;
49 if (func_data == NULL) {
51 BT_ERR("func_data == NULL");
55 req_info = _bt_get_request_info(func_data->req_id);
56 if (req_info == NULL) {
57 BT_ERR("req_info == NULL");
61 if (g_error != NULL) {
62 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
63 result = BLUETOOTH_ERROR_INTERNAL;
68 if (req_info->context == NULL)
71 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
72 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
74 _bt_convert_addr_string_to_type(device_addr.addr,
77 g_array_append_vals(out_param1, &device_addr,
78 sizeof(bluetooth_device_address_t));
79 g_array_append_vals(out_param2, &result, sizeof(int));
81 dbus_g_method_return(req_info->context, out_param1, out_param2);
83 g_array_free(out_param1, TRUE);
84 g_array_free(out_param2, TRUE);
86 _bt_delete_request_list(req_info->req_id);
89 g_error_free(g_error);
92 g_free(func_data->address);
97 static void __bt_hid_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
100 GError *g_error = NULL;
101 GArray *out_param1 = NULL;
102 GArray *out_param2 = NULL;
103 bluetooth_device_address_t device_addr = { {0} };
104 int result = BLUETOOTH_ERROR_NONE;
105 bt_function_data_t *func_data;
106 request_info_t *req_info;
108 dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
110 g_object_unref(proxy);
112 func_data = user_data;
114 if (func_data == NULL) {
116 BT_ERR("func_data == NULL");
120 req_info = _bt_get_request_info(func_data->req_id);
121 if (req_info == NULL) {
122 BT_ERR("req_info == NULL");
126 if (g_error != NULL) {
127 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
128 result = BLUETOOTH_ERROR_INTERNAL;
133 if (req_info->context == NULL)
136 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
137 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
139 _bt_convert_addr_string_to_type(device_addr.addr,
142 g_array_append_vals(out_param1, &device_addr,
143 sizeof(bluetooth_device_address_t));
144 g_array_append_vals(out_param2, &result, sizeof(int));
146 dbus_g_method_return(req_info->context, out_param1, out_param2);
148 g_array_free(out_param1, TRUE);
149 g_array_free(out_param2, TRUE);
151 _bt_delete_request_list(req_info->req_id);
154 g_error_free(g_error);
157 g_free(func_data->address);
163 /**********************************************************************
165 ***********************************************************************/
167 int _bt_hid_connect(int request_id,
168 bluetooth_device_address_t *device_address)
170 gchar *device_path = NULL;
171 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
172 bt_function_data_t *func_data;
173 DBusGProxy *adapter_proxy;
174 DBusGProxy *hid_proxy;
175 DBusGConnection *conn;
177 BT_CHECK_PARAMETER(device_address);
179 adapter_proxy = _bt_get_adapter_proxy();
180 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
182 conn = _bt_get_system_gconn();
183 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
185 _bt_convert_addr_type_to_string(address, device_address->addr);
187 dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
188 G_TYPE_STRING, address, G_TYPE_INVALID,
189 DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
191 if (device_path == NULL) {
192 BT_ERR("No paired device");
193 return BLUETOOTH_ERROR_NOT_PAIRED;
196 hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
197 device_path, BT_INPUT_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,
207 func_data, NULL, G_TYPE_INVALID)) {
208 BT_ERR("Hidh connect Dbus Call Error");
209 g_object_unref(hid_proxy);
210 return BLUETOOTH_ERROR_INTERNAL;
213 return BLUETOOTH_ERROR_NONE;
216 int _bt_hid_disconnect(int request_id,
217 bluetooth_device_address_t *device_address)
219 gchar *device_path = NULL;
220 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
221 bt_function_data_t *func_data;
222 DBusGProxy *adapter_proxy;
223 DBusGProxy *hid_proxy;
224 DBusGConnection *conn;
226 BT_CHECK_PARAMETER(device_address);
228 adapter_proxy = _bt_get_adapter_proxy();
229 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
231 conn = _bt_get_system_gconn();
232 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
234 _bt_convert_addr_type_to_string(address, device_address->addr);
236 dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
237 G_TYPE_STRING, address, G_TYPE_INVALID,
238 DBUS_TYPE_G_OBJECT_PATH, &device_path, G_TYPE_INVALID);
240 if (device_path == NULL) {
241 BT_ERR("No paired device");
242 return BLUETOOTH_ERROR_NOT_PAIRED;
245 hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
246 device_path, BT_INPUT_INTERFACE);
248 retv_if(hid_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
249 func_data = g_malloc0(sizeof(bt_function_data_t));
251 func_data->address = g_strdup(address);
252 func_data->req_id = request_id;
254 if (!dbus_g_proxy_begin_call(hid_proxy, "Disconnect",
255 (DBusGProxyCallNotify)__bt_hid_disconnect_cb,
256 func_data, NULL, G_TYPE_INVALID)) {
257 BT_ERR("Hidh disconnect Dbus Call Error");
258 g_object_unref(hid_proxy);
259 return BLUETOOTH_ERROR_INTERNAL;
262 return BLUETOOTH_ERROR_NONE;