f0d967a9cbf8681bc41f235d307a5dcc35aa7bc8
[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 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
23 #include <syspopup_caller.h>
24 #endif
25 #include "bluetooth-api.h"
26
27 #include "bt-service-common.h"
28 #include "bt-service-device.h"
29 #include "bt-service-hid.h"
30 #include "bt-service-event.h"
31 #include "bt-service-util.h"
32
33 static void __bt_hid_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
34                                         gpointer user_data)
35 {
36         GError *g_error = NULL;
37         GVariant *out_param1 = NULL;
38         GVariant *reply = NULL;
39         bluetooth_device_address_t device_addr = { {0} };
40         int result = BLUETOOTH_ERROR_NONE;
41         bt_function_data_t *func_data;
42         request_info_t *req_info;
43
44         BT_DBG("+");
45         func_data = user_data;
46
47         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
48         g_object_unref(proxy);
49         if (reply == NULL) {
50                 BT_ERR("Hid Connect Dbus Call Error");
51                 if (g_error) {
52                         BT_ERR("Error: %s\n", g_error->message);
53                         g_clear_error(&g_error);
54                 }
55                 result = BLUETOOTH_ERROR_INTERNAL;
56         }
57         g_variant_unref(reply);
58
59
60         if (func_data == NULL) {
61                 /* Send reply */
62                 BT_ERR("func_data == NULL");
63                 goto done;
64         }
65
66         req_info = _bt_get_request_info(func_data->req_id);
67         if (req_info == NULL) {
68                 BT_ERR("req_info == NULL");
69                 goto done;
70         }
71
72         if (req_info->context == NULL)
73                 goto done;
74         BT_DBG("Address: %s", func_data->address);
75         _bt_convert_addr_string_to_type(device_addr.addr,
76                                         func_data->address);
77
78         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
79                         &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
80
81         g_dbus_method_invocation_return_value(req_info->context,
82                         g_variant_new("iv", result, out_param1));
83
84         _bt_delete_request_list(req_info->req_id);
85         BT_DBG("HID Connected..");
86
87 done:
88         if (func_data) {
89                 g_free(func_data->address);
90                 g_free(func_data);
91         }
92 }
93
94 static void __bt_hid_disconnect_cb(GDBusProxy *proxy,GAsyncResult *res,
95                                         gpointer user_data)
96 {
97         GError *g_error = NULL;
98         GVariant *out_param1 = NULL;
99         GVariant *reply;
100         bluetooth_device_address_t device_addr = { {0} };
101         int result = BLUETOOTH_ERROR_NONE;
102         bt_function_data_t *func_data;
103         request_info_t *req_info;
104
105         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
106         g_object_unref(proxy);
107
108         if (reply == NULL) {
109                 BT_ERR("Hid Disconnect Dbus Call Error");
110                 if (g_error) {
111                         BT_ERR("Error: %s\n", g_error->message);
112                         g_clear_error(&g_error);
113                 }
114                 result = BLUETOOTH_ERROR_INTERNAL;
115         }
116         g_variant_unref(reply);
117
118         func_data = user_data;
119         if (func_data == NULL) {
120                 /* Send reply */
121                 BT_ERR("func_data == NULL");
122                 goto done;
123         }
124
125         req_info = _bt_get_request_info(func_data->req_id);
126         if (req_info == NULL) {
127                 BT_ERR("req_info == NULL");
128                 goto done;
129         }
130
131         if (req_info->context == NULL)
132                 goto done;
133         BT_DBG("Address: %s", func_data->address);
134         _bt_convert_addr_string_to_type(device_addr.addr,
135                                         func_data->address);
136
137         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
138                                                         &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
139
140         g_dbus_method_invocation_return_value(req_info->context,
141                         g_variant_new("iv", result, out_param1));
142
143         _bt_delete_request_list(req_info->req_id);
144         BT_DBG("HID Disconnected..");
145
146 done:
147         if (func_data) {
148                 g_free(func_data->address);
149                 g_free(func_data);
150         }
151 }
152
153
154 /**********************************************************************
155 *                               HID APIs                              *
156 ***********************************************************************/
157
158 int _bt_hid_connect(int request_id,
159                 bluetooth_device_address_t *device_address)
160 {
161         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
162         bt_function_data_t *func_data;
163         /* GDBusProxy *adapter_proxy; */
164         GDBusConnection *conn;
165
166         int ret;
167         char *uuid;
168
169         BT_CHECK_PARAMETER(device_address, return);
170
171         /* Unused adapter proxy
172         adapter_proxy = _bt_get_adapter_proxy();
173         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
174         */
175         conn = _bt_get_system_gconn();
176         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
177
178         _bt_convert_addr_type_to_string(address, device_address->addr);
179
180         func_data = g_malloc0(sizeof(bt_function_data_t));
181         /* Fix : NULL_RETURNS */
182         retv_if(func_data == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
183
184         func_data->address = g_strdup(address);
185         func_data->req_id = request_id;
186         uuid = HID_UUID;
187
188         ret = _bt_connect_profile(address, uuid,
189                         __bt_hid_connect_cb, func_data);
190
191         if (ret != BLUETOOTH_ERROR_NONE) {
192                 BT_ERR("_bt_connect_profile Error");
193                 return ret;
194         }
195         return BLUETOOTH_ERROR_NONE;
196 }
197
198 int _bt_hid_disconnect(int request_id,
199                 bluetooth_device_address_t *device_address)
200 {
201         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
202         bt_function_data_t *func_data;
203         /* GDBusProxy *adapter_proxy; */
204         GDBusConnection *conn;
205
206         int ret;
207
208         BT_CHECK_PARAMETER(device_address, return);
209
210         /* Unused adapter proxy
211         adapter_proxy = _bt_get_adapter_proxy();
212         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
213         */
214         conn = _bt_get_system_gconn();
215         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
216
217         _bt_convert_addr_type_to_string(address, device_address->addr);
218
219         func_data = g_malloc0(sizeof(bt_function_data_t));
220         /* Fix : NULL_RETURNS */
221         retv_if(func_data == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
222
223         func_data->address = g_strdup(address);
224         func_data->req_id = request_id;
225
226         ret = _bt_disconnect_profile(address, HID_UUID,
227                         __bt_hid_disconnect_cb, func_data);
228
229         if (ret != BLUETOOTH_ERROR_NONE) {
230                 BT_ERR("_bt_disconnect_profile Error");
231                 return ret;
232         }
233
234         return BLUETOOTH_ERROR_NONE;
235 }