Merge "Invoke HAL_DISCOVERY_STATE_STOPPED event once" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hdp.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Atul Kumar Rai <a.rai@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <dlog.h>
27
28 #include "bt-hal.h"
29 #include "bt-hal-log.h"
30 #include "bt-hal-msg.h"
31 #include "bt-hal-utils.h"
32
33 #include "bt-hal-event-receiver.h"
34 #include "bt-hal-hdp-dbus-handler.h"
35
36 static const bthl_callbacks_t *bt_hal_hdp_cbacks;
37
38 static bool interface_ready(void)
39 {
40         return bt_hal_hdp_cbacks != NULL;
41 }
42
43 static void __bt_hal_handle_app_reg_state(void *buf, uint16_t len)
44 {
45         struct hal_ev_hdp_app_reg_state *ev = buf;
46
47         DBG("+");
48
49         if (!ev) {
50                 ERR("Event data is NULL, return");
51                 return;
52         }
53
54         if (bt_hal_hdp_cbacks->app_reg_state_cb)
55                 bt_hal_hdp_cbacks->app_reg_state_cb(ev->app_id, ev->state);
56
57         DBG("-");
58 }
59
60 static void __bt_hal_handle_hdp_conn_state(void *buf, uint16_t len)
61 {
62         struct hal_ev_hdp_conn_state *ev = buf;
63
64         DBG("+");
65
66         if (!ev) {
67                 ERR("Event data is NULL, return");
68                 return;
69         }
70
71         if (bt_hal_hdp_cbacks->channel_state_cb)
72                 bt_hal_hdp_cbacks->channel_state_cb(ev->app_id, (bt_bdaddr_t *)(ev->bdaddr),
73                         ev->mdep_index, ev->channel_id, ev->channel_state, ev->data_fd);
74
75         DBG("-");
76 }
77
78 static void __bt_hal_handle_hdp_events(int message, void *buf, uint16_t len)
79 {
80         DBG("+");
81
82         if (!interface_ready())
83                 return;
84
85         switch (message) {
86         case HAL_EV_HDP_APP_REG_STATE:
87                 DBG("Event: HAL_EV_HDP_APP_REG_STATE");
88                 __bt_hal_handle_app_reg_state(buf, len);
89                 break;
90         case HAL_EV_HDP_CONN_STATE:
91                 DBG("Event: HAL_EV_HDP_CONN_STATE");
92                 __bt_hal_handle_hdp_conn_state(buf, len);
93                 break;
94         default:
95                 DBG("Event Currently not handled!!");
96                 break;
97         }
98
99         DBG("-");
100 }
101
102 /** Register HL application */
103 static bt_status_t hdp_register_application(bthl_reg_param_t *p_reg_param, int *app_id)
104 {
105         int ret = BT_STATUS_SUCCESS;
106         int role;
107         int data_type;
108         int channel_type;
109         const char *desc;
110
111         if (!p_reg_param)
112                 return BT_STATUS_PARM_INVALID;
113
114         if (!p_reg_param->application_name)
115                 return BT_STATUS_PARM_INVALID;
116
117         if (!app_id)
118                 return BT_STATUS_PARM_INVALID;
119
120         /* Currently only 1 dep config is supported per app id */
121         if (0 > p_reg_param->number_of_mdeps || 1 < p_reg_param->number_of_mdeps) {
122                 ERR("Currently only 1 dep config is supported per app");
123                 return BT_STATUS_PARM_INVALID;
124         }
125
126         role = p_reg_param->mdep_cfg[0].mdep_role;
127         data_type = p_reg_param->mdep_cfg[0].data_type;
128         channel_type = p_reg_param->mdep_cfg[0].channel_type;
129         desc = p_reg_param->mdep_cfg[0].mdep_description;
130
131         ret = _bt_hal_dbus_handler_hdp_register_application(
132                         role, data_type, channel_type, desc, app_id);
133         if (BT_STATUS_SUCCESS != ret) {
134                 ERR("_bt_hal_dbus_handler_hdp_register_application failed");
135                 *app_id = -1;
136         } else {
137                 DBG("app_id: %d", *app_id);
138         }
139
140         return ret;
141 }
142
143 /** Unregister HL application */
144 static bt_status_t hdp_unregister_application(int app_id)
145 {
146         int ret;
147
148         DBG("+");
149
150         ret = _bt_hal_dbus_handler_hdp_unregister_application(app_id);
151         if (BT_STATUS_SUCCESS != ret)
152                 ERR("_bt_hal_dbus_handler_hdp_unregister_application failed");
153         DBG("-");
154
155         return ret;
156 }
157
158 /** connect channel */
159 static bt_status_t hdp_connect_channel(int app_id, bt_bdaddr_t *bd_addr, int mdep_cfg_index, int *channel_id)
160 {
161         int ret;
162
163         DBG("+");
164
165         *channel_id = -1;
166         ret = _bt_hal_dbus_handler_hdp_connect_channel(app_id, bd_addr, channel_id);
167         if (BT_STATUS_SUCCESS != ret)
168                 ERR("_bt_hal_dbus_handler_hdp_unregister_application failed");
169
170         DBG("-");
171         return ret;
172 }
173
174 /** destroy channel */
175 static bt_status_t hdp_destroy_channel(int channel_id)
176 {
177         int ret;
178
179         DBG("+");
180
181         ret = _bt_hal_dbus_handler_hdp_destroy_channel(channel_id);
182         if (BT_STATUS_SUCCESS != ret)
183                 ERR("_bt_hal_dbus_handler_hdp_unregister_application failed");
184
185         DBG("-");
186         return ret;
187 }
188
189 static bt_status_t init(bthl_callbacks_t *callbacks)
190 {
191         DBG("");
192
193         if (interface_ready())
194                 return BT_STATUS_DONE;
195
196         bt_hal_hdp_cbacks = callbacks;
197         DBG("Register HDP events callback function");
198         _bt_hal_register_hdp_dbus_handler_cb(__bt_hal_handle_hdp_events);
199
200         return BT_STATUS_SUCCESS;
201 }
202
203 static void cleanup(void)
204 {
205         DBG("");
206
207         if (!interface_ready())
208                 return;
209
210         DBG("Un-register HDP events callback function");
211         _bt_hal_unregister_hdp_dbus_handler_cb();
212
213         bt_hal_hdp_cbacks = NULL;
214 }
215
216 static bthl_interface_t hl_if = {
217         .size = sizeof(hl_if),
218         .init = init,
219         .register_application = hdp_register_application,
220         .unregister_application = hdp_unregister_application,
221         .connect_channel = hdp_connect_channel,
222         .destroy_channel = hdp_destroy_channel,
223         .cleanup = cleanup
224 };
225
226 bthl_interface_t *bt_get_hl_interface(void)
227 {
228         return &hl_if;
229 }