cde8fa294a0b8d01bc9f107c4f101b540f9a96d2
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-hid.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <dbus/dbus-glib.h>
25 #include <dbus/dbus.h>
26 #include <glib.h>
27 #include <dlog.h>
28 #include <string.h>
29 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
30 #include <syspopup_caller.h>
31 #endif
32 #include "bluetooth-api.h"
33
34 #include "bt-service-common.h"
35 #include "bt-service-device.h"
36 #include "bt-service-hid.h"
37 #include "bt-service-event.h"
38 #include "bt-service-util.h"
39
40 static void __bt_hid_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
41                                     gpointer user_data)
42 {
43         GError *g_error = NULL;
44         GArray *out_param1 = NULL;
45         GArray *out_param2 = NULL;
46         bluetooth_device_address_t device_addr = { {0} };
47         int result = BLUETOOTH_ERROR_NONE;
48         bt_function_data_t *func_data;
49         request_info_t *req_info;
50
51         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
52
53         g_object_unref(proxy);
54
55         func_data = user_data;
56
57         if (func_data == NULL) {
58                 /* Send reply */
59                 BT_ERR("func_data == NULL");
60                 goto done;
61         }
62
63         req_info = _bt_get_request_info(func_data->req_id);
64         if (req_info == NULL) {
65                 BT_ERR("req_info == NULL");
66                 goto done;
67         }
68
69         if (g_error != NULL) {
70                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
71                 result = BLUETOOTH_ERROR_INTERNAL;
72         }
73
74         if (req_info->context == NULL)
75                 goto done;
76
77         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
78         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
79
80         _bt_convert_addr_string_to_type(device_addr.addr,
81                                         func_data->address);
82
83         g_array_append_vals(out_param1, &device_addr,
84                                 sizeof(bluetooth_device_address_t));
85         g_array_append_vals(out_param2, &result, sizeof(int));
86
87         dbus_g_method_return(req_info->context, out_param1, out_param2);
88
89         g_array_free(out_param1, TRUE);
90         g_array_free(out_param2, TRUE);
91
92         _bt_delete_request_list(req_info->req_id);
93         BT_DBG("HID Connected..");
94 done:
95         if (g_error)
96                 g_error_free(g_error);
97
98         if (func_data) {
99                 g_free(func_data->address);
100                 g_free(func_data);
101         }
102 }
103
104 static void __bt_hid_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
105                                     gpointer user_data)
106 {
107         GError *g_error = NULL;
108         GArray *out_param1 = NULL;
109         GArray *out_param2 = NULL;
110         bluetooth_device_address_t device_addr = { {0} };
111         int result = BLUETOOTH_ERROR_NONE;
112         bt_function_data_t *func_data;
113         request_info_t *req_info;
114
115         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
116
117         g_object_unref(proxy);
118
119         func_data = user_data;
120
121         if (func_data == NULL) {
122                 /* Send reply */
123                 BT_ERR("func_data == NULL");
124                 goto done;
125         }
126
127         req_info = _bt_get_request_info(func_data->req_id);
128         if (req_info == NULL) {
129                 BT_ERR("req_info == NULL");
130                 goto done;
131         }
132
133         if (g_error != NULL) {
134                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
135                 result = BLUETOOTH_ERROR_INTERNAL;
136         }
137
138         if (req_info->context == NULL)
139                 goto done;
140
141         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
142         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
143
144         _bt_convert_addr_string_to_type(device_addr.addr,
145                                         func_data->address);
146
147         g_array_append_vals(out_param1, &device_addr,
148                                 sizeof(bluetooth_device_address_t));
149         g_array_append_vals(out_param2, &result, sizeof(int));
150
151         dbus_g_method_return(req_info->context, out_param1, out_param2);
152
153         g_array_free(out_param1, TRUE);
154         g_array_free(out_param2, TRUE);
155
156         _bt_delete_request_list(req_info->req_id);
157 done:
158         if (g_error)
159                 g_error_free(g_error);
160
161         if (func_data) {
162                 g_free(func_data->address);
163                 g_free(func_data);
164         }
165 }
166
167
168 /**********************************************************************
169 *                               HID APIs                              *
170 ***********************************************************************/
171
172 int _bt_hid_connect(int request_id,
173                 bluetooth_device_address_t *device_address)
174 {
175         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
176         bt_function_data_t *func_data;
177         DBusGProxy *adapter_proxy;
178         DBusGConnection *conn;
179
180         int ret;
181         char *uuid;
182
183         BT_CHECK_PARAMETER(device_address, return);
184
185         adapter_proxy = _bt_get_adapter_proxy();
186         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
187
188         conn = _bt_get_system_gconn();
189         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
190
191         _bt_convert_addr_type_to_string(address, device_address->addr);
192
193         func_data = g_malloc0(sizeof(bt_function_data_t));
194
195         func_data->address = g_strdup(address);
196         func_data->req_id = request_id;
197         uuid = HID_UUID;
198
199         ret = _bt_connect_profile(address, uuid,
200                         __bt_hid_connect_cb, func_data);
201
202         if (ret != BLUETOOTH_ERROR_NONE) {
203                 BT_ERR("_bt_connect_profile Error");
204                 return ret;
205         }
206         return BLUETOOTH_ERROR_NONE;
207 }
208
209 int _bt_hid_disconnect(int request_id,
210                 bluetooth_device_address_t *device_address)
211 {
212         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
213         bt_function_data_t *func_data;
214         DBusGProxy *adapter_proxy;
215         DBusGConnection *conn;
216
217         int ret;
218
219         BT_CHECK_PARAMETER(device_address, return);
220
221         adapter_proxy = _bt_get_adapter_proxy();
222         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
223
224         conn = _bt_get_system_gconn();
225         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
226
227         _bt_convert_addr_type_to_string(address, device_address->addr);
228
229         func_data = g_malloc0(sizeof(bt_function_data_t));
230
231         func_data->address = g_strdup(address);
232         func_data->req_id = request_id;
233
234         ret = _bt_disconnect_profile(address, HID_UUID,
235                         __bt_hid_disconnect_cb, func_data);
236
237         if (ret != BLUETOOTH_ERROR_NONE) {
238                 BT_ERR("_bt_disconnect_profile Error");
239                 return ret;
240         }
241
242         return BLUETOOTH_ERROR_NONE;
243 }