tizen 2.3.1 release
[framework/connectivity/bluez.git] / android / hal-a2dp-sink.c
1 /*
2  * Copyright (C) 2014 Intel Corporation
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
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <string.h>
21
22 #include "hal-log.h"
23 #include "hal.h"
24 #include "hal-msg.h"
25 #include "hal-ipc.h"
26
27 static const btav_callbacks_t *cbs = NULL;
28
29 static bool interface_ready(void)
30 {
31         return cbs != NULL;
32 }
33
34 static void handle_conn_state(void *buf, uint16_t len, int fd)
35 {
36         struct hal_ev_a2dp_conn_state *ev = buf;
37
38         if (cbs->connection_state_cb)
39                 cbs->connection_state_cb(ev->state,
40                                                 (bt_bdaddr_t *) (ev->bdaddr));
41 }
42
43 static void handle_audio_state(void *buf, uint16_t len, int fd)
44 {
45         struct hal_ev_a2dp_audio_state *ev = buf;
46
47         if (cbs->audio_state_cb)
48                 cbs->audio_state_cb(ev->state, (bt_bdaddr_t *)(ev->bdaddr));
49 }
50
51 static void handle_audio_config(void *buf, uint16_t len, int fd)
52 {
53         struct hal_ev_a2dp_audio_config *ev = buf;
54
55         if (cbs->audio_config_cb)
56                 cbs->audio_config_cb((bt_bdaddr_t *)(ev->bdaddr),
57                                         ev->sample_rate, ev->channel_count);
58 }
59
60 /*
61  * handlers will be called from notification thread context,
62  * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
63  */
64 static const struct hal_ipc_handler ev_handlers[] = {
65         /* HAL_EV_A2DP_CONN_STATE */
66         { handle_conn_state, false, sizeof(struct hal_ev_a2dp_conn_state) },
67         /* HAL_EV_A2DP_AUDIO_STATE */
68         { handle_audio_state, false, sizeof(struct hal_ev_a2dp_audio_state) },
69         /* HAL_EV_A2DP_AUDIO_CONFIG */
70         { handle_audio_config, false, sizeof(struct hal_ev_a2dp_audio_config) },
71 };
72
73 static bt_status_t a2dp_connect(bt_bdaddr_t *bd_addr)
74 {
75         struct hal_cmd_a2dp_connect cmd;
76
77         DBG("");
78
79         if (!interface_ready())
80                 return BT_STATUS_NOT_READY;
81
82         memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
83
84         return hal_ipc_cmd(HAL_SERVICE_ID_A2DP_SINK, HAL_OP_A2DP_CONNECT,
85                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
86 }
87
88 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
89 {
90         struct hal_cmd_a2dp_disconnect cmd;
91
92         DBG("");
93
94         if (!interface_ready())
95                 return BT_STATUS_NOT_READY;
96
97         memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
98
99         return hal_ipc_cmd(HAL_SERVICE_ID_A2DP_SINK, HAL_OP_A2DP_DISCONNECT,
100                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
101 }
102
103 static bt_status_t init(btav_callbacks_t *callbacks)
104 {
105         struct hal_cmd_register_module cmd;
106         int ret;
107
108         DBG("");
109
110         if (interface_ready())
111                 return BT_STATUS_DONE;
112
113         cbs = callbacks;
114
115         hal_ipc_register(HAL_SERVICE_ID_A2DP_SINK, ev_handlers,
116                                 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
117
118         cmd.service_id = HAL_SERVICE_ID_A2DP_SINK;
119         cmd.mode = HAL_MODE_DEFAULT;
120         cmd.max_clients = 1;
121
122         ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
123                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
124
125         if (ret != BT_STATUS_SUCCESS) {
126                 cbs = NULL;
127                 hal_ipc_unregister(HAL_SERVICE_ID_A2DP_SINK);
128         }
129
130         return ret;
131 }
132
133 static void cleanup(void)
134 {
135         struct hal_cmd_unregister_module cmd;
136
137         DBG("");
138
139         if (!interface_ready())
140                 return;
141
142         cmd.service_id = HAL_SERVICE_ID_A2DP_SINK;
143
144         hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
145                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
146
147         hal_ipc_unregister(HAL_SERVICE_ID_A2DP_SINK);
148
149         cbs = NULL;
150 }
151
152 static btav_interface_t iface = {
153         .size = sizeof(iface),
154         .init = init,
155         .connect = a2dp_connect,
156         .disconnect = disconnect,
157         .cleanup = cleanup
158 };
159
160 btav_interface_t *bt_get_a2dp_sink_interface(void)
161 {
162         return &iface;
163 }