2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2019 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);
52 BT_ERR("event is NULL");
56 memcpy(event->address.addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
58 event->status = OAL_STATUS_SUCCESS;
61 case BTHD_CONN_STATE_CONNECTED:
62 event_type = OAL_EVENT_HID_DEVICE_CONNECTED;
64 case BTHD_CONN_STATE_DISCONNECTED:
65 event_type = OAL_EVENT_HID_DEVICE_DISCONNECTED;
68 BT_ERR("Unhandled Connection state %d", state);
73 send_event_bda_trace(event_type, event, sizeof(event_hid_conn_t), (bt_address_t*)bd_addr);
76 static bthd_callbacks_t sBluetoothHidDeviceCallbacks = {
77 .size = sizeof(sBluetoothHidDeviceCallbacks),
78 .connection_state_cb = connection_state_callback,
81 oal_status_t hid_device_enable(void)
83 const bt_interface_t *blued_api;
87 blued_api = adapter_get_stack_interface();
89 if (blued_api == NULL) {
90 BT_ERR("Stack is not initialized");
91 return OAL_STATUS_NOT_READY;
93 if (hid_device_api != NULL) {
94 BT_WARN("HID Device Interface is already initialized...");
95 return OAL_STATUS_ALREADY_DONE;
98 hid_device_api = (const bthd_interface_t *)blued_api->get_profile_interface(BT_PROFILE_HIDDEVICE_ID);
99 if (hid_device_api == NULL) {
100 BT_ERR("HID Device interface failed");
101 return OAL_STATUS_INTERNAL_ERROR;
104 if ((ret = hid_device_api->init(&sBluetoothHidDeviceCallbacks)) != BT_STATUS_SUCCESS) {
105 BT_ERR("HID Device Init failed: %s", status2string(ret));
106 hid_device_api->cleanup();
107 hid_device_api = NULL;
108 return convert_to_oal_status(ret);
110 return OAL_STATUS_SUCCESS;
113 oal_status_t hid_device_disable(void)
117 CHECK_OAL_HID_DEVICE_ENABLED();
119 hid_device_api->cleanup();
121 hid_device_api = NULL;
122 return OAL_STATUS_SUCCESS;