c5b16b429d532ca499d4e308bf52f16ddc220c62
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hid-dbus-handler.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Atul Kumar Rai <a.rai@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <dlog.h>
30 #include <vconf.h>
31
32 #include "bt-hal-hid-dbus-handler.h"
33 #include "bt-hal-dbus-common-utils.h"
34 #include "bt-hal-internal.h"
35
36 static handle_stack_msg event_cb = NULL;
37
38 /* To send stack event to hal-hidhost handler */
39 void _bt_hal_register_hid_dbus_handler_cb(handle_stack_msg cb)
40 {
41         event_cb = cb;
42 }
43
44 /* To send stack event to hal-hidhost handler */
45 void _bt_hal_unregister_hid_dbus_handler_cb()
46 {
47         event_cb = NULL;
48 }
49
50 static void __bt_hid_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
51                 gpointer user_data)
52 {
53         GError *g_error = NULL;
54         struct hal_ev_hidhost_conn_state ev;
55         GVariant *reply = NULL;
56         char *address = user_data;
57         int result = BT_STATUS_SUCCESS;
58
59         DBG("+");
60
61         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
62         g_object_unref(proxy);
63         if (reply == NULL) {
64                 ERR("Hid Connect Dbus Call Error");
65                 if (g_error) {
66                         ERR("Error: %s\n", g_error->message);
67                         g_clear_error(&g_error);
68                 }
69                 result = BT_STATUS_FAIL;
70         }
71         g_variant_unref(reply);
72
73         DBG("Address: %s", address);
74         /*
75          * If result is success, HID connected event will be triggered
76          * automatically from stack, so return from here.
77          */
78         if (result == BT_STATUS_SUCCESS)
79                 goto done;
80
81         /* Prepare to send HID connection state event */
82         memset(&ev, 0, sizeof(ev));
83         _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
84         ev.state = HAL_HIDHOST_STATE_DISCONNECTED;
85         if (!event_cb)
86                 ERR("HID dbus handler callback not registered");
87         else
88                 event_cb(HAL_EV_HIDHOST_CONN_STATE, (void *)&ev, sizeof(ev));
89
90 done:
91         g_free(address);
92 }
93
94 bt_status_t _bt_hal_dbus_handler_hidhost_connect(bt_bdaddr_t *bd_addr)
95 {
96         char *address;
97         struct hal_ev_hidhost_conn_state ev;
98         GDBusConnection *conn;
99
100         int ret;
101         char *uuid;
102
103         if(!bd_addr) {
104                 ERR("bd_addr is NULL, return");
105                 return BT_STATUS_PARM_INVALID;
106         }
107
108         conn = _bt_hal_get_system_gconn();
109         if(!conn) {
110                 ERR("_bt_hal_get_system_gconn returned NULL, return");
111                 return BT_STATUS_FAIL;
112         }
113
114         address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
115         if (!address) {
116                 ERR("Memory allocation failed");
117                 return BT_STATUS_NOMEM;
118         }
119         _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
120         uuid = HID_UUID;
121
122         ret = _bt_hal_connect_profile(address, uuid,
123                         __bt_hid_connect_cb, address);
124
125         if (ret != BT_HAL_ERROR_NONE) {
126                 ERR("_bt_hal_connect_profile Error");
127                 return BT_STATUS_FAIL;
128         }
129
130         /* Prepare to send HID connecting event */
131         memset(&ev, 0, sizeof(ev));
132         ev.state = HAL_HIDHOST_STATE_CONNECTING;
133         memcpy(ev.bdaddr, bd_addr, sizeof(bt_bdaddr_t));
134         if (!event_cb)
135                 ERR("HID dbus handler callback not registered");
136         else
137                 event_cb(HAL_EV_HIDHOST_CONN_STATE, (void *)&ev, sizeof(ev));
138
139         return BT_STATUS_SUCCESS;
140 }
141
142 static void __bt_hid_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
143                 gpointer user_data)
144 {
145         GError *g_error = NULL;
146         GVariant *reply = NULL;
147         char *address = user_data;
148         int result = BT_STATUS_SUCCESS;
149
150         DBG("+");
151
152         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
153         g_object_unref(proxy);
154         if (reply == NULL) {
155                 ERR("Hid Disconnect Dbus Call Error");
156                 if (g_error) {
157                         ERR("Error: %s\n", g_error->message);
158                         g_clear_error(&g_error);
159                 }
160                 result = BT_STATUS_FAIL;
161         }
162         g_variant_unref(reply);
163
164         if (result != BT_STATUS_FAIL)
165                 DBG("HID Disconnect successful for Device: %s", address);
166         else
167                 DBG("HID Disconnect un-successful for Device: %s", address);
168         g_free(address);
169         DBG("-");
170 }
171
172 bt_status_t _bt_hal_dbus_handler_hidhost_disconnect(bt_bdaddr_t *bd_addr)
173 {
174         char *address;
175         struct hal_ev_hidhost_conn_state ev;
176         GDBusConnection *conn;
177
178         int ret;
179         char *uuid;
180
181         if(!bd_addr) {
182                 ERR("bd_addr is NULL, return");
183                 return BT_STATUS_PARM_INVALID;
184         }
185
186         conn = _bt_hal_get_system_gconn();
187         if(!conn) {
188                 ERR("_bt_hal_get_system_gconn returned NULL, return");
189                 return BT_STATUS_FAIL;
190         }
191
192         address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
193         if (!address) {
194                 ERR("Memory allocation failed");
195                 return BT_STATUS_NOMEM;
196         }
197         _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
198         uuid = HID_UUID;
199
200         ret = _bt_hal_disconnect_profile(address, uuid,
201                         __bt_hid_disconnect_cb, address);
202         if (ret != BT_HAL_ERROR_NONE) {
203                 ERR("_bt_hal_connect_profile Error");
204                 return BT_STATUS_FAIL;
205         }
206
207         /* Prepare to send HID connecting event */
208         memset(&ev, 0, sizeof(ev));
209         ev.state = HAL_HIDHOST_STATE_DISCONNECTING;
210         memcpy(ev.bdaddr, bd_addr, sizeof(bt_bdaddr_t));
211         if (!event_cb)
212                 ERR("HID dbus handler callback not registered");
213         else
214                 event_cb(HAL_EV_HIDHOST_CONN_STATE, (void *)&ev, sizeof(ev));
215
216         return BT_STATUS_SUCCESS;
217 }