BLE Gatt Server Socket notify implementation.
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-a2dp-sink.c
1 /*
2  * Bluetooth HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Nilesh Trimbake <t.shripati@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 #include "bt-hal-a2dp-sink-dbus-handler.h"
33
34 static const btav_callbacks_t *bt_hal_a2dp_sink_cbacks;
35
36 static bool interface_ready(void)
37 {
38         return bt_hal_a2dp_sink_cbacks != NULL;
39 }
40
41 static bt_status_t a2dp_sink_connect(bt_bdaddr_t *bd_addr)
42 {
43         DBG("");
44         return _bt_hal_dbus_handler_a2dp_sink_connect(bd_addr);
45 }
46
47 static bt_status_t a2dp_sink_disconnect(bt_bdaddr_t *bd_addr)
48 {
49         DBG("");
50         return _bt_hal_dbus_handler_a2dp_sink_disconnect(bd_addr);
51 }
52
53 static void __bt_hal_handle_a2dp_sink_conn_state(void *buf, uint16_t len)
54 {
55         struct hal_ev_a2dp_conn_state *ev = buf;
56
57         if (bt_hal_a2dp_sink_cbacks->connection_state_cb)
58                 bt_hal_a2dp_sink_cbacks->connection_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
59 }
60
61 static void __bt_hal_handle_a2dp_sink_audio_conn_state(void *buf, uint16_t len)
62 {
63         struct hal_ev_a2dp_audio_state *ev = buf;
64
65         if (bt_hal_a2dp_sink_cbacks->audio_state_cb)
66                 bt_hal_a2dp_sink_cbacks->audio_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
67 }
68
69 static void __bt_hal_handle_a2dp_sink_events(int message, void *buf, uint16_t len)
70 {
71         DBG("+");
72         if (!interface_ready())
73                 return;
74         switch (message) {
75         case HAL_EV_A2DP_SOURCE_CONN_STATE:
76                 DBG("Event: HAL_EV_A2DP_SOURCE_CONN_STATE");
77                 __bt_hal_handle_a2dp_sink_conn_state(buf, len);
78                 break;
79         case HAL_EV_A2DP_AUDIO_STATE:
80                 DBG("Event: HAL_EV_A2DP_AUDIO_STATE");
81                 __bt_hal_handle_a2dp_sink_audio_conn_state(buf, len);
82                 break;
83         default:
84                 DBG("Event Currently not handled!!");
85                 break;
86         }
87         DBG("-");
88 }
89
90 static bt_status_t init(btav_callbacks_t* callbacks)
91 {
92         DBG("");
93
94         if (interface_ready())
95                 return BT_STATUS_DONE;
96
97         if (BT_STATUS_SUCCESS != _bt_hal_dbus_handler_enable_a2dp_sink()) {
98                 ERR("_bt_hal_dbus_handler_enable_a2dp_sink failed");
99                 return BT_STATUS_FAIL;
100         }
101
102         bt_hal_a2dp_sink_cbacks = callbacks;
103         DBG("Register A2DP Sink events callback function");
104         _bt_hal_register_a2dp_sink_dbus_handler_cb(__bt_hal_handle_a2dp_sink_events);
105         _bt_hal_register_event_handler_cb(HAL_A2DP_SNK, __bt_hal_handle_a2dp_sink_events);
106         return BT_STATUS_SUCCESS;
107 }
108
109 static void cleanup(void)
110 {
111         DBG("");
112
113         if (!interface_ready())
114                 return;
115
116         bt_hal_a2dp_sink_cbacks = NULL;
117         _bt_hal_unregister_event_handler_cb(HAL_A2DP_SNK);
118 }
119
120 static btav_interface_t a2dp_sink_if = {
121         .size = sizeof(a2dp_sink_if),
122         .init = init,
123         .connect = a2dp_sink_connect,
124         .disconnect = a2dp_sink_disconnect,
125         .cleanup = cleanup
126 };
127
128 btav_interface_t *bt_get_a2dp_sink_interface(void)
129 {
130         DBG("Get A2DP Sink Profile Interface");
131         return &a2dp_sink_if;
132 }