HID: Add API to send customized event
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hid-device.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 #include <gio/gio.h>
18 #include <gio/gunixfdlist.h>
19 #include <sys/socket.h>
20
21 #include "bluetooth-api.h"
22 #include "bt-internal-types.h"
23 #include "bluetooth-hid-api.h"
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
27
28 #define HID_UUID                "00001124-0000-1000-8000-00805f9b34fb"
29 #define HID_DEVICE_UUID "00001124-0000-1000-8000-00805f9b43bf"
30 #define REPORTID_MOUSE  1
31 #define BT_HID_BUFFER_LEN 100
32
33 /* HIDP header masks */
34 #define BT_HID_HEADER_TRANS_MASK                        0xf0
35 #define BT_HID_HEADER_PARAM_MASK                        0x0f
36
37 /* HIDP transaction types */
38 #define BT_HID_TRANS_HANDSHAKE                  0x00
39 #define BT_HID_TRANS_HID_CONTROL                        0x10
40 #define BT_HID_TRANS_GET_REPORT                 0x40
41 #define BT_HID_TRANS_SET_REPORT                 0x50
42 #define BT_HID_TRANS_GET_PROTOCOL                       0x60
43 #define BT_HID_TRANS_SET_PROTOCOL                       0x70
44 #define BT_HID_TRANS_GET_IDLE                   0x80
45 #define BT_HID_TRANS_SET_IDLE                   0x90
46 #define BT_HID_TRANS_DATA                               0xa0
47 #define BT_HID_TRANS_DATC                               0xb0
48
49 #define BT_HID_DATA_RTYPE_INPUT                 0x01
50 #define BT_HID_DATA_RTYPE_OUTPUT                        0x02
51
52 #define BT_HID_HSHK_SUCCESSFUL  0x00
53 #define BT_HID_HSHK_NOT_READY   0x01
54 #define BT_HID_HSHK_ERR_INVALID_REPORT_ID       0x02
55 #define BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST     0x03
56 #define BT_HID_HSHK_ERR_INVALID_PARAMETER       0x04
57 #define BT_HID_HSHK_ERR_UNKNOWN 0x0E
58 #define BT_HID_HSHK_ERR_FATAL   0x0F
59
60 typedef struct {
61         guint object_id;
62         gchar *path;
63         int id;
64         char *uuid;
65         GSList *device_list;
66 } hid_info_t;
67
68 typedef struct {
69         int ctrl_fd;
70         int intr_fd;
71         GIOChannel *ctrl_data_io;
72         GIOChannel *intr_data_io;
73         guint ctrl_data_id;
74         guint intr_data_id;
75         char *address;
76         guint disconnect_idle_id;
77 } hid_connected_device_info_t;
78
79 struct reports {
80         guint8 type;
81         guint8 rep_data[20];
82 } __attribute__((__packed__));
83
84 static hid_info_t *hid_info = NULL;
85
86 /* Variable for privilege, only for write API,
87   before we should reduce time to bt-service dbus calling
88   -1 : Don't have a permission to access API
89   0 : Initial value, not yet check
90   1 : Have a permission to access API
91 */
92 static int privilege_token_send_mouse = 0;
93 static int privilege_token_send_key = 0;
94 static int privilege_token_reply = 0;
95
96 static gboolean __hid_disconnect(hid_connected_device_info_t *info);
97
98 int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr)
99 {
100
101         int ret = BLUETOOTH_ERROR_NONE;
102         char *adapter_path;
103         GVariant *result = NULL;
104         GError *err = NULL;
105         GDBusConnection *conn;
106         GDBusProxy *server_proxy;
107         int index1 = 0;
108         int index2 = 0;
109         GUnixFDList *out_fd_list = NULL;
110         conn = _bt_gdbus_get_system_gconn();
111         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
112
113         adapter_path = _bt_get_device_object_path((char *)address);
114         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
115         BT_INFO_C("Device : %s", adapter_path);
116         server_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
117                         NULL, BT_BLUEZ_NAME,
118                         adapter_path, "org.bluez.Input1",  NULL, NULL);
119         g_free(adapter_path);
120
121         if (server_proxy == NULL) {
122                 BT_ERR("Failed to get the network server proxy\n");
123                 return BLUETOOTH_ERROR_INTERNAL;
124         }
125
126         result = g_dbus_proxy_call_with_unix_fd_list_sync(server_proxy, "GetFD",
127                                  NULL,
128                                  G_DBUS_CALL_FLAGS_NONE,
129                                  -1,
130                                  NULL,
131                                  &out_fd_list,
132                                  NULL,
133                                  &err);
134         if (result == NULL) {
135                 if (err != NULL) {
136                         g_dbus_error_strip_remote_error(err);
137                         BT_ERR("INPUT server register Error: %s\n", err->message);
138                         if (g_strcmp0(err->message, "Already Exists") == 0) {
139                                 ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
140                         } else {
141                                 ret = BLUETOOTH_ERROR_INTERNAL;
142                         }
143                         g_error_free(err);
144                 }
145         } else {
146                 g_variant_get (result, "(hh)", &index1, &index2);
147                 int fd1 = g_unix_fd_list_get(out_fd_list, index1, NULL);
148                 int fd2 = g_unix_fd_list_get(out_fd_list, index2, NULL);
149
150                 *ctrl = fd1;
151                 *intr = fd2;
152                 g_object_unref(out_fd_list);
153                 g_variant_unref(result);
154         }
155         g_object_unref(server_proxy);
156         return ret;
157 }
158
159 static hid_connected_device_info_t *__find_hid_info_with_address(const char *remote_addr)
160 {
161         GSList *l;
162
163         for (l = hid_info->device_list; l != NULL; l = l->next) {
164                 hid_connected_device_info_t *info = l->data;
165                 if (g_strcmp0((const char *)info->address, (const char *)remote_addr) == 0)
166                         return info;
167         }
168         return NULL;
169 }
170
171 static void __hid_connected_cb(hid_connected_device_info_t *info,
172                         int result)
173 {
174         bluetooth_hid_request_t conn_info;
175         bt_event_info_t *event_info = NULL;
176
177         event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
178         if (event_info == NULL)
179                 return;
180
181         memset(&conn_info, 0x00, sizeof(bluetooth_hid_request_t));
182         if (info->intr_fd != -1 && info->ctrl_fd == -1)
183                 conn_info.socket_fd = info->intr_fd;
184         else
185                 conn_info.socket_fd = info->ctrl_fd;
186         _bt_convert_addr_string_to_type (conn_info.device_addr.addr , info->address);
187
188         if (result == BLUETOOTH_ERROR_NONE)
189                 BT_INFO_C("Connected [HID Device]");
190
191         _bt_common_event_cb(BLUETOOTH_HID_DEVICE_CONNECTED,
192                         result, &conn_info,
193                         event_info->cb, event_info->user_data);
194 }
195
196 static void __hid_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
197                         gpointer user_data)
198
199 {
200         int result;
201         GError *error = NULL;
202         GVariant *ret = NULL;
203         hid_connected_device_info_t info;
204         const char *path;
205
206         BT_DBG("+");
207
208         ret = g_dbus_proxy_call_finish(proxy, res, &error);
209         if (ret == NULL) {
210                 g_dbus_error_strip_remote_error(error);
211                 BT_ERR("Error : %s \n", error->message);
212
213                 if (g_strcmp0(error->message, "In Progress") == 0)
214                         result = BLUETOOTH_ERROR_DEVICE_BUSY;
215                 else
216                         result = BLUETOOTH_ERROR_INTERNAL;
217
218                 info.ctrl_fd = -1;
219                 info.intr_fd = -1;
220
221                 info.address = g_malloc0(BT_ADDRESS_STRING_SIZE);
222
223                 path = g_dbus_proxy_get_object_path(proxy);
224                 _bt_convert_device_path_to_address(path, info.address);
225
226                 __hid_connected_cb(&info, result);
227
228                 g_free(info.address);
229
230                 g_error_free(error);
231         } else {
232                 g_variant_unref(ret);
233         }
234
235         if (proxy)
236                 g_object_unref(proxy);
237
238         BT_DBG("-");
239 }
240
241 static gboolean __hid_disconnect(hid_connected_device_info_t *info)
242 {
243         bluetooth_hid_request_t disconn_info;
244         int fd = info->ctrl_fd;
245         bt_event_info_t *event_info;
246
247         BT_INFO_C("Disconnected [HID Device]");
248         hid_info->device_list = g_slist_remove(hid_info->device_list, info);
249         if (info->ctrl_data_id > 0) {
250                 g_source_remove(info->ctrl_data_id);
251                 info->ctrl_data_id = 0;
252         }
253         if (info->intr_data_id > 0) {
254                 g_source_remove(info->intr_data_id);
255                 info->intr_data_id = 0;
256         }
257
258         if (info->ctrl_data_io) {
259                 g_io_channel_shutdown(info->ctrl_data_io, TRUE, NULL);
260                 g_io_channel_unref(info->ctrl_data_io);
261                 info->ctrl_data_io = NULL;
262         }
263         if (info->intr_data_io) {
264                 g_io_channel_shutdown(info->intr_data_io, TRUE, NULL);
265                 g_io_channel_unref(info->intr_data_io);
266                 info->intr_data_io = NULL;
267         }
268         if (info->intr_fd >= 0) {
269                 close(info->ctrl_fd);
270                 close(info->intr_fd);
271                 info->intr_fd = -1;
272                 info->ctrl_fd = -1;
273         }
274         info->disconnect_idle_id = 0;
275         event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
276         if (event_info == NULL)
277                 return FALSE;
278
279         memset(&disconn_info, 0x00, sizeof(bluetooth_hid_request_t));
280         disconn_info.socket_fd = fd;
281         _bt_convert_addr_string_to_type(disconn_info.device_addr.addr , info->address);
282         _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DISCONNECTED,
283                         BLUETOOTH_ERROR_NONE, &disconn_info,
284                         event_info->cb, event_info->user_data);
285         if (info->address)
286                 g_free(info->address);
287         g_free(info);
288         info = NULL;
289         BT_DBG("-");
290         return FALSE;
291 }
292
293 void __free_hid_info(hid_info_t *info)
294 {
295         BT_DBG("");
296
297         _bt_unregister_gdbus(info->object_id);
298
299         while (info->device_list) {
300                 hid_connected_device_info_t *dev_info = NULL;
301                 dev_info = (hid_connected_device_info_t *)info->device_list->data;
302
303                 if (dev_info->disconnect_idle_id > 0) {
304                         BT_INFO("Disconnect idle still not process remove source");
305                         g_source_remove(dev_info->disconnect_idle_id);
306                         dev_info->disconnect_idle_id = 0;
307                 }
308                 __hid_disconnect(dev_info);
309         }
310
311         g_free(info->path);
312         g_free(info->uuid);
313         g_free(info);
314 }
315
316 static gboolean __received_cb(GIOChannel *chan, GIOCondition cond,
317                                                                 gpointer data)
318 {
319         hid_connected_device_info_t *info = data;
320         GIOStatus status = G_IO_STATUS_NORMAL;
321         char buffer[20];
322         gsize len = 0;
323         GError *err = NULL;
324         guint8  header, type, param;
325         bt_event_info_t *event_info;
326         retv_if(info == NULL, FALSE);
327
328         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
329                 BT_ERR_C("HID  disconnected: %d", info->ctrl_fd);
330                if (info->disconnect_idle_id > 0) {
331                         BT_INFO("Disconnect idle still not process remove source");
332                         g_source_remove(info->disconnect_idle_id);
333                         info->disconnect_idle_id = 0;
334                }
335                 __hid_disconnect(info);
336                 return FALSE;
337         }
338         status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
339                         &len, &err);
340         if (status == G_IO_STATUS_NORMAL) {
341                 BT_INFO("Parsing Data");
342                 bluetooth_hid_received_data_t data = {0};
343                 header = buffer[0];
344                 type = header & BT_HID_HEADER_TRANS_MASK;
345                 param = header & BT_HID_HEADER_PARAM_MASK;
346                 BT_INFO("type %d , param %d", type, param);
347                 BT_INFO("Data Reveived from %s" , info->address);
348
349                 event_info = _bt_event_get_cb_data(BT_HID_DEVICE_EVENT);
350                 if (event_info == NULL)
351                         return FALSE;
352
353                 data.address = g_strdup(info->address);
354                 switch (type) {
355                         case BT_HID_TRANS_HANDSHAKE:
356                                 BT_INFO("TRANS HANDSHAKE");
357                                 data.type = HTYPE_TRANS_HANDSHAKE;
358                                 data.buffer_size = len;
359                                 data.buffer = (char *) malloc(sizeof(char) * len);
360                                 if (data.buffer)
361                                         memcpy(data.buffer, buffer, len);
362                         break;
363                         case BT_HID_TRANS_HID_CONTROL:
364                                 BT_INFO("HID CONTROL");
365                                 data.type = HTYPE_TRANS_HID_CONTROL;
366                                 data.buffer_size = len;
367                                 data.buffer = (char *) malloc(sizeof(char) * len);
368                                 if (data.buffer)
369                                         memcpy(data.buffer, buffer, len);
370                         break;
371                         case BT_HID_TRANS_DATA:
372                                 BT_INFO("TRANS DATA");
373                                 data.type = HTYPE_TRANS_DATA;
374                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
375                                         BT_INFO("Input Report");
376                                         data.param = PTYPE_DATA_RTYPE_INPUT;
377                                         data.buffer_size = len;
378                                         data.buffer = (char *) malloc(sizeof(char) * len);
379                                         if (data.buffer)
380                                                 memcpy(data.buffer, buffer, len);
381                                 } else {
382                                         BT_INFO("Out Report");
383                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
384                                         data.buffer_size = len;
385                                         data.buffer = (char *) malloc(sizeof(char) * len);
386                                         if (data.buffer)
387                                                 memcpy(data.buffer, buffer, len);
388                                 }
389                         break;
390                         case BT_HID_TRANS_GET_REPORT: {
391                                 BT_INFO("Get Report");
392                                 data.type = HTYPE_TRANS_GET_REPORT;
393                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
394                                         BT_INFO("Input Report");
395                                         data.param = PTYPE_DATA_RTYPE_INPUT;
396                                 } else {
397                                         BT_INFO("Output Report");
398                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
399                                 }
400                                 data.buffer_size = len;
401                                 data.buffer = (char *) malloc(sizeof(char) * len);
402                                 if (data.buffer)
403                                         memcpy(data.buffer, buffer, len);
404                                 break;
405                         }
406                         case BT_HID_TRANS_SET_REPORT: {
407                                 BT_INFO("Set Report");
408                                 data.type = HTYPE_TRANS_SET_REPORT;
409                                 if (param & BT_HID_DATA_RTYPE_INPUT) {
410                                         BT_INFO("Input Report");
411                                         data.param = PTYPE_DATA_RTYPE_INPUT;
412                                 } else {
413                                         BT_INFO("Output Report");
414                                         data.param = PTYPE_DATA_RTYPE_OUTPUT;
415                                 }
416                                 data.buffer_size = len;
417                                 data.buffer = (char *) malloc(sizeof(char) * len);
418                                 if (data.buffer)
419                                         memcpy(data.buffer, buffer, len);
420                                 break;
421                         }
422                         case BT_HID_TRANS_GET_PROTOCOL:{
423                                 BT_INFO("Get_PROTOCOL");
424                                 data.type = HTYPE_TRANS_GET_PROTOCOL;
425                                 data.param = PTYPE_DATA_RTYPE_INPUT;
426                                 data.buffer_size = len;
427                                 data.buffer = (char *) malloc(sizeof(char) * len);
428                                 if (data.buffer)
429                                         memcpy(data.buffer, buffer, len);
430                                 break;
431                         }
432                         case BT_HID_TRANS_SET_PROTOCOL:{
433                                 BT_INFO("Set_PROTOCOL");
434                                 data.type = HTYPE_TRANS_SET_PROTOCOL;
435                                 data.param = PTYPE_DATA_RTYPE_INPUT;
436                                 data.buffer_size = len;
437                                 data.buffer = (char *) malloc(sizeof(char) * len);
438                                 if (data.buffer)
439                                         memcpy(data.buffer, buffer, len);
440                                 break;
441                         }
442                         default: {
443                                 BT_INFO("unsupported HIDP control message");
444                                 BT_ERR("Send Handshake Message");
445                                 guint8 type = BT_HID_TRANS_HANDSHAKE |
446                                         BT_HID_HSHK_ERR_UNSUPPORTED_REQUEST;
447                                 data.type = HTYPE_TRANS_UNKNOWN;
448                                 int fd = g_io_channel_unix_get_fd(chan);
449                                 int bytes = write(fd,  &type, sizeof(type));
450                                 BT_INFO("Bytes Written %d", bytes);
451                                 break;
452                         }
453                 }
454
455                 _bt_common_event_cb(BLUETOOTH_HID_DEVICE_DATA_RECEIVED,
456                                 BLUETOOTH_ERROR_NONE, &data,
457                                 event_info->cb, event_info->user_data);
458                 if (data.buffer)
459                         g_free(data.buffer);
460                 if (data.address)
461                         g_free((char *)data.address);
462         } else {
463                 BT_INFO("Error while reading data");
464         }
465         return TRUE;
466 }
467
468 int new_hid_connection(const char *path, int fd, bluetooth_device_address_t *addr)
469 {
470         hid_info_t *info = NULL;
471         hid_connected_device_info_t *dev_info = NULL;
472         char address[18];
473         info = hid_info;
474
475         if (info == NULL)
476                 return -1;
477         _bt_convert_addr_type_to_string((char *)address, addr->addr);
478         BT_INFO("Address [%s]", address);
479         dev_info = __find_hid_info_with_address(address);
480         if (dev_info == NULL) {
481                 dev_info = (hid_connected_device_info_t *)
482                         g_malloc0(sizeof(hid_connected_device_info_t));
483                 if (dev_info == NULL) {
484                         BT_ERR("Fail to allocation memory");
485                         return -1;
486                 }
487
488                 dev_info->intr_fd = -1;
489                 dev_info->ctrl_fd = -1;
490                 dev_info->intr_fd = fd;
491                 dev_info->address = g_strdup(address);
492                 dev_info->intr_data_io = g_io_channel_unix_new(dev_info->intr_fd);
493                 g_io_channel_set_encoding(dev_info->intr_data_io, NULL, NULL);
494                 g_io_channel_set_flags(dev_info->intr_data_io, G_IO_FLAG_NONBLOCK, NULL);
495                 g_io_channel_set_close_on_unref(dev_info->intr_data_io, TRUE);
496                 dev_info->intr_data_id = g_io_add_watch(dev_info->intr_data_io,
497                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
498                                 __received_cb, dev_info);
499                 hid_info->device_list = g_slist_append(hid_info->device_list, dev_info);
500         } else {
501                 dev_info->ctrl_fd = fd;
502                 dev_info->ctrl_data_io = g_io_channel_unix_new(dev_info->ctrl_fd);
503                 g_io_channel_set_encoding(dev_info->ctrl_data_io, NULL, NULL);
504                 g_io_channel_set_flags(dev_info->ctrl_data_io, G_IO_FLAG_NONBLOCK, NULL);
505                 g_io_channel_set_close_on_unref(dev_info->ctrl_data_io, TRUE);
506                 dev_info->ctrl_data_id = g_io_add_watch(dev_info->ctrl_data_io,
507                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
508                                 __received_cb, dev_info);
509         }
510         if (dev_info->ctrl_fd != -1 && dev_info->intr_fd != -1) {
511                 __hid_connected_cb(dev_info, BLUETOOTH_ERROR_NONE);
512         }
513
514         return 0;
515 }
516 static hid_info_t *__register_method()
517 {
518         int object_id;
519         hid_info_t *info = NULL;
520         char *path = NULL;
521         path = g_strdup_printf("/org/socket/server/%d", getpid());
522
523         object_id = _bt_register_new_conn(path, new_hid_connection);
524         if (object_id < 0) {
525                 return NULL;
526         }
527         info = g_new(hid_info_t, 1);
528         info->object_id = (guint)object_id;
529         info->path = path;
530         info->id = 0;
531         info->device_list = NULL;
532
533         return info;
534 }
535
536 BT_EXPORT_API int bluetooth_hid_device_init(hid_cb_func_ptr callback_ptr, void *user_data)
537 {
538         int ret;
539
540         /* Register HID Device events */
541         BT_INFO("BT_HID_DEVICE_EVENT");
542         ret = _bt_register_event(BT_HID_DEVICE_EVENT , (void *)callback_ptr, user_data);
543
544         if (ret != BLUETOOTH_ERROR_NONE &&
545              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
546                 BT_ERR("Fail to init the event handler");
547                 return ret;
548         }
549
550         _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
551
552         return BLUETOOTH_ERROR_NONE;
553 }
554
555 BT_EXPORT_API int bluetooth_hid_device_deinit(void)
556 {
557         int ret;
558
559         ret = _bt_unregister_event(BT_HID_DEVICE_EVENT);
560
561         if (ret != BLUETOOTH_ERROR_NONE) {
562                 BT_ERR("Fail to deinit the event handler");
563                 return ret;
564         }
565
566         _bt_set_user_data(BT_HID, NULL, NULL);
567
568         return BLUETOOTH_ERROR_NONE;
569 }
570
571 BT_EXPORT_API int bluetooth_hid_device_activate(void)
572 {
573         bt_register_profile_info_t profile_info;
574         int result = BLUETOOTH_ERROR_NONE;
575
576         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_ACTIVATE)
577                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
578                 BT_ERR("Don't have a privilege to use this API");
579                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
580         }
581
582         if (hid_info != NULL)
583                 return BLUETOOTH_ERROR_IN_PROGRESS;
584
585         hid_info = __register_method();
586         if (hid_info == NULL)
587                 return BLUETOOTH_ERROR_INTERNAL;
588
589         hid_info->uuid = g_strdup(HID_DEVICE_UUID);
590
591         profile_info.authentication = TRUE;
592         profile_info.authorization = TRUE;
593         profile_info.obj_path = hid_info->path;
594         profile_info.role = g_strdup("Hid");
595         profile_info.service = hid_info->uuid;
596         profile_info.uuid = hid_info->uuid;
597
598         BT_INFO("uuid %s", profile_info.uuid);
599         result = _bt_register_profile_platform(&profile_info, FALSE);
600
601         return result;
602 }
603
604 BT_EXPORT_API int bluetooth_hid_device_deactivate(void)
605 {
606         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DEACTIVATE)
607                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
608                 BT_ERR("Don't have a privilege to use this API");
609                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
610         }
611
612         if (hid_info == NULL)
613                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
614
615         _bt_unregister_profile(hid_info->path);
616
617         __free_hid_info(hid_info);
618         hid_info = NULL;
619         return BLUETOOTH_ERROR_NONE;
620 }
621
622 BT_EXPORT_API int bluetooth_hid_device_connect(const char *remote_addr)
623 {
624         char device_address[BT_ADDRESS_STRING_SIZE] = {0};
625         hid_connected_device_info_t *info = NULL;
626         int ret;
627         BT_DBG("+");
628         BT_CHECK_PARAMETER(remote_addr, return);
629
630         info = __find_hid_info_with_address(remote_addr);
631         if (info) {
632                 BT_ERR("Connection Already Exists");
633                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
634         }
635         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_CONNECT)
636                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
637                 BT_ERR("Don't have a privilege to use this API");
638                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
639         }
640
641         memcpy(device_address, remote_addr, BT_ADDRESS_STRING_SIZE);
642         ret = _bt_connect_profile(device_address, HID_DEVICE_UUID,
643                                 __hid_connect_response_cb, NULL);
644
645         return ret;
646 }
647 BT_EXPORT_API int bluetooth_hid_device_disconnect(const char *remote_addr)
648 {
649         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_DISCONNECT)
650                  == BLUETOOTH_ERROR_PERMISSION_DEINED) {
651                 BT_ERR("Don't have a privilege to use this API");
652                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
653         }
654
655         hid_connected_device_info_t *info = NULL;
656
657         info = __find_hid_info_with_address(remote_addr);
658         if (info == NULL)
659                 return BLUETOOTH_ERROR_INVALID_PARAM;
660
661         _bt_disconnect_profile((char *)remote_addr, HID_DEVICE_UUID, NULL, NULL);
662
663         info->disconnect_idle_id = g_idle_add((GSourceFunc)__hid_disconnect, info);
664
665         BT_DBG("-");
666         return BLUETOOTH_ERROR_NONE;
667 }
668 BT_EXPORT_API int bluetooth_hid_device_send_mouse_event(const char *remote_addr,
669                                         hid_send_mouse_event_t send_event)
670 {
671         int result;
672         int written = 0;
673         hid_connected_device_info_t *info = NULL;
674
675         switch (privilege_token_send_mouse) {
676         case 0:
677                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_MOUSE_EVENT);
678
679                 if (result == BLUETOOTH_ERROR_NONE) {
680                         privilege_token_send_mouse = 1; /* Have a permission */
681                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
682                         BT_ERR("Don't have a privilege to use this API");
683                         privilege_token_send_mouse = -1; /* Don't have a permission */
684                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
685                 } else {
686                         /* Just break - It is not related with permission error */
687                 }
688                 break;
689         case 1:
690                 /* Already have a privilege */
691                 break;
692         case -1:
693                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
694         default:
695                 /* Invalid privilge token value */
696                 return BLUETOOTH_ERROR_INTERNAL;
697         }
698         info = __find_hid_info_with_address(remote_addr);
699         if (info == NULL) {
700                 BT_ERR("Connection Information not found");
701                 return BLUETOOTH_ERROR_INVALID_PARAM;
702         }
703         int socket_fd;
704
705         if (info->intr_fd != -1 && info->ctrl_fd == -1)
706                 socket_fd = info->intr_fd;
707         else
708                 socket_fd = info->ctrl_fd;
709
710         written = write(socket_fd, &send_event, sizeof(send_event));
711
712         return written;
713 }
714
715 BT_EXPORT_API int bluetooth_hid_device_send_key_event(const char *remote_addr,
716                                         hid_send_key_event_t send_event)
717 {
718         int result;
719         int written = 0;
720         hid_connected_device_info_t *info = NULL;
721
722         switch (privilege_token_send_key) {
723         case 0:
724                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_KEY_EVENT);
725
726                 if (result == BLUETOOTH_ERROR_NONE) {
727                         privilege_token_send_key = 1; /* Have a permission */
728                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
729                         BT_ERR("Don't have a privilege to use this API");
730                         privilege_token_send_key = -1; /* Don't have a permission */
731                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
732                 } else {
733                         /* Just break - It is not related with permission error */
734                 }
735                 break;
736         case 1:
737                 /* Already have a privilege */
738                 break;
739         case -1:
740                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
741         default:
742                 /* Invalid privilge token value */
743                 return BLUETOOTH_ERROR_INTERNAL;
744         }
745
746         info = __find_hid_info_with_address(remote_addr);
747         if (info == NULL) {
748                 BT_ERR("Connection Information not found");
749                 return BLUETOOTH_ERROR_INVALID_PARAM;
750         }
751
752         int socket_fd;
753
754         if (info->intr_fd != -1 && info->ctrl_fd == -1)
755                 socket_fd = info->intr_fd;
756         else
757                 socket_fd = info->ctrl_fd;
758
759         written = write(socket_fd, &send_event, sizeof(send_event));
760         return written;
761 }
762
763 BT_EXPORT_API int bluetooth_hid_device_send_custom_event(const char *remote_addr,
764                         unsigned char btcode, unsigned char report_id,
765                         const char *data, unsigned int data_len)
766 {
767         int result;
768         int written = 0;
769         int socket_fd;
770         hid_connected_device_info_t *info = NULL;
771         char *send_event = NULL;
772
773         BT_CHECK_PARAMETER(remote_addr, return);
774         BT_CHECK_PARAMETER(data, return);
775
776         switch (privilege_token_send_key) {
777         case 0:
778                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_CUSTOM_EVENT);
779
780                 if (result == BLUETOOTH_ERROR_NONE) {
781                         privilege_token_send_key = 1; /* Have a permission */
782                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
783                         BT_ERR("Don't have a privilege to use this API");
784                         privilege_token_send_key = -1; /* Don't have a permission */
785                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
786                 } else {
787                         /* Just break - It is not related with permission error */
788                 }
789                 break;
790         case 1:
791                 /* Already have a privilege */
792                 break;
793         case -1:
794                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
795         default:
796                 /* Invalid privilge token value */
797                 return BLUETOOTH_ERROR_INTERNAL;
798         }
799
800         info = __find_hid_info_with_address(remote_addr);
801         if (info == NULL) {
802                 BT_ERR("Connection Information not found");
803                 return BLUETOOTH_ERROR_INVALID_PARAM;
804         }
805
806         if (info->intr_fd != -1 && info->ctrl_fd == -1)
807                 socket_fd = info->intr_fd;
808         else
809                 socket_fd = info->ctrl_fd;
810
811         send_event = (char*)g_malloc0(data_len + 2);
812         if (send_event == NULL)
813                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
814
815         send_event[0] = (char)btcode;
816         send_event[1] = (char)report_id;
817         memcpy(send_event + 2, data, data_len);
818
819         written = write(socket_fd, send_event, data_len + 2);
820
821         g_free(send_event);
822
823         return written;
824 }
825
826 BT_EXPORT_API int bluetooth_hid_device_reply_to_report(const char *remote_addr,
827                                 bt_hid_header_type_t htype,
828                                 bt_hid_param_type_t ptype,
829                                 const char *data,
830                                 unsigned int data_len)
831 {
832         int result;
833         struct reports output_report = { 0 };
834         int bytes = 0;
835         hid_connected_device_info_t *info = NULL;
836         info = __find_hid_info_with_address(remote_addr);
837         if (info == NULL) {
838                 BT_ERR("Connection Information not found");
839                 return BLUETOOTH_ERROR_INVALID_PARAM;
840         }
841
842         switch (privilege_token_reply) {
843         case 0:
844                 result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DEVICE_SEND_REPLY_TO_REPORT);
845
846                 if (result == BLUETOOTH_ERROR_NONE) {
847                         privilege_token_reply = 1; /* Have a permission */
848                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
849                         BT_ERR("Don't have a privilege to use this API");
850                         privilege_token_reply = -1; /* Don't have a permission */
851                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
852                 } else {
853                         /* Just break - It is not related with permission error */
854                 }
855                 break;
856         case 1:
857                 /* Already have a privilege */
858                 break;
859         case -1:
860                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
861         default:
862                 /* Invalid privilge token value */
863                 return BLUETOOTH_ERROR_INTERNAL;
864         }
865
866         BT_INFO("htype %d ptype %d", htype, ptype);
867         switch(htype) {
868                 case HTYPE_TRANS_GET_REPORT: {
869                         switch(ptype) {
870                                 case PTYPE_DATA_RTYPE_INPUT: {
871                                         output_report.type = BT_HID_TRANS_DATA |
872                                                         BT_HID_DATA_RTYPE_INPUT;
873                                         memcpy(output_report.rep_data, data, data_len);
874                                         bytes = write(info->intr_fd, &output_report,
875                                                                 sizeof(output_report));
876                                         BT_DBG("Bytes Written %d", bytes);
877                                         break;
878                                 }
879                                 default:
880                                         BT_INFO("Not Supported");
881                                         break;
882                         }
883                         break;
884                 case HTYPE_TRANS_GET_PROTOCOL: {
885                         BT_DBG("Replying to Get_PROTOCOL");
886                         output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_OUTPUT;
887                         output_report.rep_data[0] = data[0];
888                         bytes = write(info->intr_fd, &output_report, 2);
889                         BT_DBG("Bytes Written %d", bytes);
890                         break;
891                 }
892                 case HTYPE_TRANS_SET_PROTOCOL: {
893                         BT_DBG("Reply to Set_Protocol");
894                         output_report.type = BT_HID_TRANS_DATA | BT_HID_DATA_RTYPE_INPUT;
895                         memcpy(output_report.rep_data, data, data_len);
896                         bytes = write(info->ctrl_fd, &output_report,
897                                         sizeof(output_report));
898                         BT_DBG("Bytes Written %d", bytes);
899                         break;
900                 }
901                 case HTYPE_TRANS_HANDSHAKE: {
902                         BT_DBG("Replying Handshake");
903                         output_report.type = BT_HID_TRANS_HANDSHAKE | data[0];
904                         memset(output_report.rep_data, 0, sizeof(output_report.rep_data));
905                         bytes = write(info->intr_fd,  &output_report.type,
906                                         sizeof(output_report.type));
907                         BT_DBG("Bytes Written %d", bytes);
908                         break;
909                 }
910                         default:
911                                 break;
912                 }
913         }
914         return bytes;
915 }