Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[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 #include <syspopup_caller.h>
30 #include "bluetooth-api.h"
31
32 #include "bt-service-common.h"
33 #include "bt-service-device.h"
34 #include "bt-service-hid.h"
35 #include "bt-service-event.h"
36 #include "bt-service-util.h"
37
38 static void __bt_hid_connect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
39                                     gpointer user_data)
40 {
41         GError *g_error = NULL;
42         GArray *out_param1 = NULL;
43         GArray *out_param2 = NULL;
44         bluetooth_device_address_t device_addr = { {0} };
45         int result = BLUETOOTH_ERROR_NONE;
46         bt_function_data_t *func_data;
47         request_info_t *req_info;
48
49         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
50
51         g_object_unref(proxy);
52
53         func_data = user_data;
54
55         if (func_data == NULL) {
56                 /* Send reply */
57                 BT_ERR("func_data == NULL");
58                 goto done;
59         }
60
61         req_info = _bt_get_request_info(func_data->req_id);
62         if (req_info == NULL) {
63                 BT_ERR("req_info == NULL");
64                 goto done;
65         }
66
67         if (g_error != NULL) {
68                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
69                 result = BLUETOOTH_ERROR_INTERNAL;
70         }
71
72         if (req_info->context == NULL)
73                 goto done;
74
75         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
76         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
77
78         _bt_convert_addr_string_to_type(device_addr.addr,
79                                         func_data->address);
80
81         g_array_append_vals(out_param1, &device_addr,
82                                 sizeof(bluetooth_device_address_t));
83         g_array_append_vals(out_param2, &result, sizeof(int));
84
85         dbus_g_method_return(req_info->context, out_param1, out_param2);
86
87         g_array_free(out_param1, TRUE);
88         g_array_free(out_param2, TRUE);
89
90         _bt_delete_request_list(req_info->req_id);
91         BT_DBG("HID Connected..");
92 done:
93         if (g_error)
94                 g_error_free(g_error);
95
96         if (func_data) {
97                 g_free(func_data->address);
98                 g_free(func_data);
99         }
100 }
101
102 static void __bt_hid_disconnect_cb(DBusGProxy *proxy, DBusGProxyCall *call,
103                                     gpointer user_data)
104 {
105         GError *g_error = NULL;
106         GArray *out_param1 = NULL;
107         GArray *out_param2 = NULL;
108         bluetooth_device_address_t device_addr = { {0} };
109         int result = BLUETOOTH_ERROR_NONE;
110         bt_function_data_t *func_data;
111         request_info_t *req_info;
112
113         dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
114
115         g_object_unref(proxy);
116
117         func_data = user_data;
118
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 (g_error != NULL) {
132                 BT_ERR("Hidh Connect Dbus Call Error: %s\n", g_error->message);
133                 result = BLUETOOTH_ERROR_INTERNAL;
134         }
135
136         if (req_info->context == NULL)
137                 goto done;
138
139         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
140         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
141
142         _bt_convert_addr_string_to_type(device_addr.addr,
143                                         func_data->address);
144
145         g_array_append_vals(out_param1, &device_addr,
146                                 sizeof(bluetooth_device_address_t));
147         g_array_append_vals(out_param2, &result, sizeof(int));
148
149         dbus_g_method_return(req_info->context, out_param1, out_param2);
150
151         g_array_free(out_param1, TRUE);
152         g_array_free(out_param2, TRUE);
153
154         _bt_delete_request_list(req_info->req_id);
155 done:
156         if (g_error)
157                 g_error_free(g_error);
158
159         if (func_data) {
160                 g_free(func_data->address);
161                 g_free(func_data);
162         }
163 }
164
165
166 /**********************************************************************
167 *                               HID APIs                              *
168 ***********************************************************************/
169
170 int _bt_hid_connect(int request_id,
171                 bluetooth_device_address_t *device_address)
172 {
173         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
174         bt_function_data_t *func_data;
175         DBusGProxy *adapter_proxy;
176         DBusGConnection *conn;
177
178         int ret;
179         char *uuid;
180
181         BT_CHECK_PARAMETER(device_address, return);
182
183         adapter_proxy = _bt_get_adapter_proxy();
184         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
185
186         conn = _bt_get_system_gconn();
187         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
188
189         _bt_convert_addr_type_to_string(address, device_address->addr);
190
191         func_data = g_malloc0(sizeof(bt_function_data_t));
192
193         func_data->address = g_strdup(address);
194         func_data->req_id = request_id;
195         uuid = HID_UUID;
196
197         ret = _bt_connect_profile(address, uuid,
198                         __bt_hid_connect_cb, func_data);
199
200         if (ret != BLUETOOTH_ERROR_NONE) {
201                 BT_ERR("_bt_connect_profile Error");
202                 return ret;
203         }
204         return BLUETOOTH_ERROR_NONE;
205 }
206
207 int _bt_hid_disconnect(int request_id,
208                 bluetooth_device_address_t *device_address)
209 {
210         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
211         bt_function_data_t *func_data;
212         DBusGProxy *adapter_proxy;
213         DBusGConnection *conn;
214
215         int ret;
216
217         BT_CHECK_PARAMETER(device_address, return);
218
219         adapter_proxy = _bt_get_adapter_proxy();
220         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
221
222         conn = _bt_get_system_gconn();
223         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
224
225         _bt_convert_addr_type_to_string(address, device_address->addr);
226
227         func_data = g_malloc0(sizeof(bt_function_data_t));
228
229         func_data->address = g_strdup(address);
230         func_data->req_id = request_id;
231
232         ret = _bt_disconnect_profile(address, HID_UUID,
233                         __bt_hid_disconnect_cb, func_data);
234
235         if (ret != BLUETOOTH_ERROR_NONE) {
236                 BT_ERR("_bt_disconnect_profile Error");
237                 return ret;
238         }
239
240         return BLUETOOTH_ERROR_NONE;
241 }