Merge "Implement HID Device role event handling logic" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-hid-device.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <dlog.h>
22 #include <string.h>
23
24 #include <bluetooth.h>
25 #include <bt_hd.h>
26
27 #include "oal-event.h"
28 #include "oal-internal.h"
29 #include "oal-common.h"
30 #include "oal-manager.h"
31 #include "oal-hid-device.h"
32 #include "oal-utils.h"
33
34 #define CHECK_OAL_HID_DEVICE_ENABLED() \
35         do { \
36                 if (hid_device_api == NULL) { \
37                         BT_ERR("HID Device Not Enabled"); \
38                         return OAL_STATUS_NOT_READY; \
39                 } \
40         } while (0)
41
42 static const bthd_interface_t *hid_device_api;
43
44 static void connection_state_callback(bt_bdaddr_t *bd_addr, bthd_connection_state_t state)
45 {
46         event_hid_conn_t *event = g_new0(event_hid_conn_t, 1);
47         int event_type;
48
49         BT_DBG("%d", state);
50
51         if (!event)
52                 return;
53         memcpy(event->address.addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
54
55         event->status = OAL_STATUS_SUCCESS;
56
57         switch (state) {
58         case BTHD_CONN_STATE_CONNECTED:
59                 event_type = OAL_EVENT_HID_DEVICE_CONNECTED;
60                 break;
61         case BTHD_CONN_STATE_DISCONNECTED:
62                 event_type = OAL_EVENT_HID_DEVICE_DISCONNECTED;
63                 break;
64         default:
65                 BT_ERR("Unhandled Connection state %d", state);
66                 g_free(event);
67                 return;
68         }
69
70         send_event_bda_trace(event_type, event, sizeof(event_hid_conn_t), (bt_address_t*)bd_addr);
71 }
72
73 static bthd_callbacks_t sBluetoothHidDeviceCallbacks = {
74         .size = sizeof(sBluetoothHidDeviceCallbacks),
75         .connection_state_cb = connection_state_callback,
76 };
77
78 oal_status_t hid_device_enable(void)
79 {
80         const bt_interface_t *blued_api;
81         int ret;
82
83         API_TRACE();
84         blued_api = adapter_get_stack_interface();
85
86         if (blued_api == NULL) {
87                 BT_ERR("Stack is not initialized");
88                 return OAL_STATUS_NOT_READY;
89         }
90         if (hid_device_api != NULL) {
91                 BT_WARN("HID Device Interface is already initialized...");
92                 return OAL_STATUS_ALREADY_DONE;
93         }
94
95         hid_device_api = (const bthd_interface_t *)blued_api->get_profile_interface(BT_PROFILE_HIDDEVICE_ID);
96         if (hid_device_api == NULL) {
97                 BT_ERR("HID Device interface failed");
98                 return OAL_STATUS_INTERNAL_ERROR;
99         }
100
101         if ((ret = hid_device_api->init(&sBluetoothHidDeviceCallbacks)) != BT_STATUS_SUCCESS) {
102                 BT_ERR("HID Device Init failed: %s", status2string(ret));
103                 hid_device_api->cleanup();
104                 hid_device_api = NULL;
105                 return convert_to_oal_status(ret);
106         }
107         return OAL_STATUS_SUCCESS;
108 }
109
110 oal_status_t hid_device_disable(void)
111 {
112         API_TRACE();
113
114         CHECK_OAL_HID_DEVICE_ENABLED();
115
116         hid_device_api->cleanup();
117
118         hid_device_api = NULL;
119         return OAL_STATUS_SUCCESS;
120 }
121