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