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