Fix the svace issue - DEREF_AFTER_NULL
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-hid-device.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2019 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                 BT_ERR("event is NULL");
53                 return;
54         }
55
56         memcpy(event->address.addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
57
58         event->status = OAL_STATUS_SUCCESS;
59
60         switch (state) {
61         case BTHD_CONN_STATE_CONNECTED:
62                 event_type = OAL_EVENT_HID_DEVICE_CONNECTED;
63                 break;
64         case BTHD_CONN_STATE_DISCONNECTED:
65                 event_type = OAL_EVENT_HID_DEVICE_DISCONNECTED;
66                 break;
67         default:
68                 BT_ERR("Unhandled Connection state %d", state);
69                 g_free(event);
70                 return;
71         }
72
73         send_event_bda_trace(event_type, event, sizeof(event_hid_conn_t), (bt_address_t*)bd_addr);
74 }
75
76 static bthd_callbacks_t sBluetoothHidDeviceCallbacks = {
77         .size = sizeof(sBluetoothHidDeviceCallbacks),
78         .connection_state_cb = connection_state_callback,
79 };
80
81 oal_status_t hid_device_enable(void)
82 {
83         const bt_interface_t *blued_api;
84         int ret;
85
86         API_TRACE();
87         blued_api = adapter_get_stack_interface();
88
89         if (blued_api == NULL) {
90                 BT_ERR("Stack is not initialized");
91                 return OAL_STATUS_NOT_READY;
92         }
93         if (hid_device_api != NULL) {
94                 BT_WARN("HID Device Interface is already initialized...");
95                 return OAL_STATUS_ALREADY_DONE;
96         }
97
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;
102         }
103
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);
109         }
110         return OAL_STATUS_SUCCESS;
111 }
112
113 oal_status_t hid_device_disable(void)
114 {
115         API_TRACE();
116
117         CHECK_OAL_HID_DEVICE_ENABLED();
118
119         hid_device_api->cleanup();
120
121         hid_device_api = NULL;
122         return OAL_STATUS_SUCCESS;
123 }
124