871b7e20be0e3e2ede14b6b4784baaefbbea1615
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-av.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@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-av-dbus-handler.h"
34
35 static const btav_callbacks_t *bt_hal_av_cbacks;
36
37 static bool interface_ready(void)
38 {
39         return bt_hal_av_cbacks != NULL;
40 }
41
42 static bt_status_t av_connect(bt_bdaddr_t *bd_addr)
43 {
44         DBG("");
45         return _bt_hal_dbus_handler_av_connect(bd_addr);
46 }
47
48 static bt_status_t av_disconnect(bt_bdaddr_t *bd_addr)
49 {
50         DBG("");
51         return _bt_hal_dbus_handler_av_disconnect(bd_addr);
52 }
53
54 static void __bt_hal_handle_av_conn_state(void *buf, uint16_t len)
55 {
56         struct hal_ev_a2dp_conn_state *ev = buf;
57
58         if (bt_hal_av_cbacks->connection_state_cb)
59                 bt_hal_av_cbacks->connection_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
60 }
61
62 static void __bt_hal_handle_av_audio_conn_state(void *buf, uint16_t len)
63 {
64         struct hal_ev_a2dp_audio_state *ev = buf;
65
66         if (bt_hal_av_cbacks->audio_state_cb)
67                 bt_hal_av_cbacks->audio_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
68 }
69
70 static void __bt_hal_handle_av_events(int message, void *buf, uint16_t len)
71 {
72         DBG("+");
73         if (!interface_ready())
74                 return;
75         switch(message) {
76         case HAL_EV_A2DP_CONN_STATE:
77                 DBG("Event: HAL_EV_A2DP_CONN_STATE");
78                 __bt_hal_handle_av_conn_state(buf, len);
79                 break;
80         case HAL_EV_A2DP_AUDIO_STATE:
81                 DBG("Event: HAL_EV_A2DP_AUDIO_STATE");
82                 __bt_hal_handle_av_audio_conn_state(buf, len);
83                 break;
84         default:
85                 DBG("Event Currently not handled!!");
86                 break;
87         }
88         DBG("-");
89 }
90
91 static bt_status_t init(btav_callbacks_t* callbacks)
92 {
93         DBG("");
94
95         if (interface_ready())
96                 return BT_STATUS_DONE;
97
98         if (BT_STATUS_SUCCESS != _bt_hal_dbus_handler_enable_a2dp_source()) {
99                 ERR("_bt_hal_dbus_handler_enable_a2dp_source failed");
100                 return BT_STATUS_FAIL;
101         }
102
103         bt_hal_av_cbacks = callbacks;
104         DBG("Register A2DP Src events callback function");
105         _bt_hal_register_av_dbus_handler_cb(__bt_hal_handle_av_events);
106         _bt_hal_register_event_handler_cb(HAL_A2DP_SRC, __bt_hal_handle_av_events);
107         return BT_STATUS_SUCCESS;
108 }
109
110 static void cleanup(void)
111 {
112         DBG("");
113
114         if (!interface_ready())
115                 return;
116
117         _bt_hal_unregister_event_handler_cb(HAL_A2DP_SRC);
118         bt_hal_av_cbacks = NULL;
119 }
120
121 static btav_interface_t av_if = {
122         .size = sizeof(av_if),
123         .init = init,
124         .connect = av_connect,
125         .disconnect = av_disconnect,
126         .cleanup = cleanup
127 };
128
129 btav_interface_t *bt_get_av_interface(void)
130 {
131         DBG("Get A2DP Src Profile Interface");
132         return &av_if;
133 }