Fix the memory leak
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-tds.c
1 /*
2  * Copyright (c) 2020 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 <dlog.h>
18 #include <bluetooth.h>
19 #include "bt_tds.h"
20 #include "oal-event.h"
21 #include "oal-internal.h"
22 #include "oal-manager.h"
23 #include "oal-utils.h"
24 #include "oal-common.h"
25 #include "oal-tds.h"
26
27 #define CHECK_OAL_TDS_ENABLED() \
28         do { \
29                 if (tds_api == NULL) { \
30                         BT_ERR("TDS Not Enabled"); \
31                         return OAL_STATUS_NOT_READY; \
32                 } \
33         } while (0)
34
35 static const bttds_interface_t *tds_api;
36
37 static void tds_activation_requested_cb(char *address, unsigned char org_id, unsigned char *data, unsigned int data_len)
38 {
39         event_tds_activation_requested_t *event = g_malloc0(sizeof(event_tds_activation_requested_t));
40         g_strlcpy(event->address, address, BT_ADDRESS_STR_LEN);
41         event->org_id = org_id;
42         memcpy(event->tds_data, data, sizeof(event->tds_data));
43         event->data_len = data_len;
44
45         send_event(OAL_EVENT_TDS_ACTIVATION_REQUESTED, event, sizeof(event_tds_activation_requested_t));
46 }
47
48 static bttds_callbacks_t tds_callbacks = {
49         .activation_requested_cb = tds_activation_requested_cb,
50 };
51
52 oal_status_t tds_enable(void)
53 {
54         const bt_interface_t * blued_api;
55         int ret;
56
57         /* Get stack interface */
58         blued_api = (const bt_interface_t *)adapter_get_stack_interface();
59
60         if (blued_api == NULL) {
61                 BT_ERR("Stack is not initialized");
62                 return OAL_STATUS_NOT_READY;
63         }
64
65         if (tds_api) {
66                 BT_WARN("TDS Interface is already initialized...");
67                 return OAL_STATUS_ALREADY_DONE;
68         }
69
70         tds_api = (const bttds_interface_t *)blued_api->get_profile_interface(BT_PROFILE_TDS_ID);
71         if (tds_api == NULL) {
72                 BT_ERR("TDS interface failed");
73                 return OAL_STATUS_INTERNAL_ERROR;
74         }
75
76         if ((ret = tds_api->init(&tds_callbacks)) != BT_STATUS_SUCCESS) {
77                 BT_ERR("Error: Unable to initialise TDS :%s", status2string(ret));
78                 tds_api->cleanup();
79                 tds_api = NULL;
80                 return convert_to_oal_status(ret);
81         }
82
83         BT_INFO("TDS successfully initialized");
84         return OAL_STATUS_SUCCESS;
85 }
86
87 oal_status_t tds_disable(void)
88 {
89         if (tds_api) {
90                 tds_api->cleanup();
91                 tds_api = NULL;
92         }
93
94         return OAL_STATUS_SUCCESS;
95 }
96
97 oal_status_t tds_provider_send_activation_response(unsigned char response,
98                 unsigned char *data, unsigned int data_len)
99 {
100         int ret;
101         API_TRACE();
102         CHECK_OAL_TDS_ENABLED();
103
104         ret = tds_api->send_activation_response(response, data, data_len);
105         if (ret != BT_STATUS_SUCCESS) {
106                 BT_ERR("send_activation_response() failed: %s", status2string(ret));
107                 return convert_to_oal_status(ret);
108         }
109         return OAL_STATUS_SUCCESS;
110 }
111
112 oal_status_t tds_provider_set_block_data(unsigned char *data, unsigned int data_len)
113 {
114         int ret;
115         API_TRACE();
116         CHECK_OAL_TDS_ENABLED();
117
118         ret = tds_api->set_block_data(data, data_len);
119         if (ret != BT_STATUS_SUCCESS) {
120                 BT_ERR("set_block_data() failed: %s", status2string(ret));
121                 return convert_to_oal_status(ret);
122         }
123         return OAL_STATUS_SUCCESS;
124 }
125
126 oal_status_t tds_provider_unregister()
127 {
128         int ret;
129         API_TRACE();
130         CHECK_OAL_TDS_ENABLED();
131
132         ret = tds_api->unregister_provider();
133         if (ret != BT_STATUS_SUCCESS) {
134                 BT_ERR("unregister_provider() failed: %s", status2string(ret));
135                 return convert_to_oal_status(ret);
136         }
137         return OAL_STATUS_SUCCESS;
138 }
139
140 oal_status_t tds_provider_register()
141 {
142         int ret;
143         API_TRACE();
144         CHECK_OAL_TDS_ENABLED();
145
146         ret = tds_api->register_provider();
147         if (ret != BT_STATUS_SUCCESS) {
148                 BT_ERR("register_provider() failed: %s", status2string(ret));
149                 return convert_to_oal_status(ret);
150         }
151         return OAL_STATUS_SUCCESS;
152 }