2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <bluetooth.h>
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"
34 #define CHECK_OAL_HID_DEVICE_ENABLED() \
36 if (hid_device_api == NULL) { \
37 BT_ERR("HID Device Not Enabled"); \
38 return OAL_STATUS_NOT_READY; \
42 static const bthd_interface_t *hid_device_api;
44 static void connection_state_callback(bt_bdaddr_t *bd_addr, bthd_connection_state_t state)
46 event_hid_conn_t *event = g_new0(event_hid_conn_t, 1);
53 memcpy(event->address.addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
55 event->status = OAL_STATUS_SUCCESS;
58 case BTHD_CONN_STATE_CONNECTED:
59 event_type = OAL_EVENT_HID_DEVICE_CONNECTED;
61 case BTHD_CONN_STATE_DISCONNECTED:
62 event_type = OAL_EVENT_HID_DEVICE_DISCONNECTED;
65 BT_ERR("Unhandled Connection state %d", state);
70 send_event_bda_trace(event_type, event, sizeof(event_hid_conn_t), (bt_address_t*)bd_addr);
73 static bthd_callbacks_t sBluetoothHidDeviceCallbacks = {
74 .size = sizeof(sBluetoothHidDeviceCallbacks),
75 .connection_state_cb = connection_state_callback,
78 oal_status_t hid_device_enable(void)
80 const bt_interface_t *blued_api;
84 blued_api = adapter_get_stack_interface();
86 if (blued_api == NULL) {
87 BT_ERR("Stack is not initialized");
88 return OAL_STATUS_NOT_READY;
90 if (hid_device_api != NULL) {
91 BT_WARN("HID Device Interface is already initialized...");
92 return OAL_STATUS_ALREADY_DONE;
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;
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);
107 return OAL_STATUS_SUCCESS;
110 oal_status_t hid_device_disable(void)
114 CHECK_OAL_HID_DEVICE_ENABLED();
116 hid_device_api->cleanup();
118 hid_device_api = NULL;
119 return OAL_STATUS_SUCCESS;