2b96f38a0df8f03eb203804e999fb6a6d06c5d9f
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-hid.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <dbus/dbus-glib.h>
21 #include <dbus/dbus.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
26 #include <syspopup_caller.h>
27 #endif
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"
33
34 static void __bt_hid_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
35                                     gpointer user_data)
36 {
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;
44
45         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
46
47         g_object_unref(proxy);
48
49         func_data = user_data;
50
51         if (func_data == NULL) {
52                 /* Send reply */
53                 BT_ERR("func_data == NULL");
54                 goto done;
55         }
56
57         req_info = _bt_get_request_info(func_data->req_id);
58         if (req_info == NULL) {
59                 BT_ERR("req_info == NULL");
60                 goto done;
61         }
62
63         if (g_error != NULL) {
64                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
65                 result = BLUETOOTH_ERROR_INTERNAL;
66                 goto dbus_return;
67         }
68
69 dbus_return:
70         if (req_info->context == NULL)
71                 goto done;
72
73         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
74         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
75
76         _bt_convert_addr_string_to_type(device_addr.addr,
77                                         func_data->address);
78
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));
82
83         dbus_g_method_return(req_info->context, out_param1, out_param2);
84
85         g_array_free(out_param1, TRUE);
86         g_array_free(out_param2, TRUE);
87
88         _bt_delete_request_list(req_info->req_id);
89 done:
90         if (g_error)
91                 g_error_free(g_error);
92
93         if (func_data) {
94                 g_free(func_data->address);
95                 g_free(func_data);
96         }
97 }
98
99 static void __bt_hid_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
100                                     gpointer user_data)
101 {
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;
109
110         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
111
112         g_object_unref(proxy);
113
114         func_data = user_data;
115
116         if (func_data == NULL) {
117                 /* Send reply */
118                 BT_ERR("func_data == NULL");
119                 goto done;
120         }
121
122         req_info = _bt_get_request_info(func_data->req_id);
123         if (req_info == NULL) {
124                 BT_ERR("req_info == NULL");
125                 goto done;
126         }
127
128         if (g_error != NULL) {
129                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
130                 result = BLUETOOTH_ERROR_INTERNAL;
131                 goto dbus_return;
132         }
133
134 dbus_return:
135         if (req_info->context == NULL)
136                 goto done;
137
138         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
139         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
140
141         _bt_convert_addr_string_to_type(device_addr.addr,
142                                         func_data->address);
143
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));
147
148         dbus_g_method_return(req_info->context, out_param1, out_param2);
149
150         g_array_free(out_param1, TRUE);
151         g_array_free(out_param2, TRUE);
152
153         _bt_delete_request_list(req_info->req_id);
154 done:
155         if (g_error)
156                 g_error_free(g_error);
157
158         if (func_data) {
159                 g_free(func_data->address);
160                 g_free(func_data);
161         }
162 }
163
164
165 /**********************************************************************
166 *                               HID APIs                              *
167 ***********************************************************************/
168
169 int _bt_hid_connect(int request_id,
170                 bluetooth_device_address_t *device_address)
171 {
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;
178
179         BT_CHECK_PARAMETER(device_address, return);
180
181         _bt_convert_addr_type_to_string(address, device_address->addr);
182
183         adapter_proxy = _bt_get_adapter_proxy();
184         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
185
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;
190         }
191
192         conn = _bt_get_system_gconn();
193         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
194
195         hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
196                                         device_path, BT_DEVICE_INTERFACE);
197
198         g_free(device_path);
199         retv_if(hid_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
200         func_data = g_malloc0(sizeof(bt_function_data_t));
201
202         func_data->address = g_strdup(address);
203         func_data->req_id = request_id;
204
205         if (!dbus_g_proxy_begin_call(hid_proxy, "Connect",
206                         (DBusGProxyCallNotify)__bt_hid_connect_cb,
207                         func_data, NULL,
208                         G_TYPE_INVALID)) {
209                                 BT_ERR("Hidh connect Dbus Call Error");
210                                 g_object_unref(hid_proxy);
211
212                                 g_free(func_data->address);
213                                 g_free(func_data);
214                                 return BLUETOOTH_ERROR_INTERNAL;
215         }
216
217         return BLUETOOTH_ERROR_NONE;
218 }
219
220 int _bt_hid_disconnect(int request_id,
221                 bluetooth_device_address_t *device_address)
222 {
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;
229
230         BT_CHECK_PARAMETER(device_address, return);
231
232         _bt_convert_addr_type_to_string(address, device_address->addr);
233
234         adapter_proxy = _bt_get_adapter_proxy();
235         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
236
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;
241         }
242
243         conn = _bt_get_system_gconn();
244         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
245
246         hid_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
247                                         device_path, BT_DEVICE_INTERFACE);
248
249         g_free(device_path);
250         retv_if(hid_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
251         func_data = g_malloc0(sizeof(bt_function_data_t));
252
253         func_data->address = g_strdup(address);
254         func_data->req_id = request_id;
255
256         if (!dbus_g_proxy_begin_call(hid_proxy, "Disconnect",
257                         (DBusGProxyCallNotify)__bt_hid_disconnect_cb,
258                         func_data, NULL,
259                         G_TYPE_INVALID)) {
260                                 BT_ERR("Hidh disconnect Dbus Call Error");
261                                 g_object_unref(hid_proxy);
262
263                                 g_free(func_data->address);
264                                 g_free(func_data);
265                                 return BLUETOOTH_ERROR_INTERNAL;
266         }
267
268         return BLUETOOTH_ERROR_NONE;
269 }