tizen 2.3.1 release
[framework/connectivity/bluez.git] / android / hal-a2dp.c
1 /*
2  * Copyright (C) 2013 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 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
54         struct hal_ev_a2dp_audio_config *ev = buf;
55
56         if (cbs->audio_config_cb)
57                 cbs->audio_config_cb((bt_bdaddr_t *)(ev->bdaddr),
58                                         ev->sample_rate, ev->channel_count);
59 #endif
60 }
61
62 /*
63  * handlers will be called from notification thread context,
64  * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
65  */
66 static const struct hal_ipc_handler ev_handlers[] = {
67         /* HAL_EV_A2DP_CONN_STATE */
68         { handle_conn_state, false, sizeof(struct hal_ev_a2dp_conn_state) },
69         /* HAL_EV_A2DP_AUDIO_STATE */
70         { handle_audio_state, false, sizeof(struct hal_ev_a2dp_audio_state) },
71         /* HAL_EV_A2DP_AUDIO_CONFIG */
72         { handle_audio_config, false, sizeof(struct hal_ev_a2dp_audio_config) },
73 };
74
75 static bt_status_t a2dp_connect(bt_bdaddr_t *bd_addr)
76 {
77         struct hal_cmd_a2dp_connect cmd;
78
79         DBG("");
80
81         if (!interface_ready())
82                 return BT_STATUS_NOT_READY;
83
84         memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
85
86         return hal_ipc_cmd(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_CONNECT,
87                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
88 }
89
90 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
91 {
92         struct hal_cmd_a2dp_disconnect cmd;
93
94         DBG("");
95
96         if (!interface_ready())
97                 return BT_STATUS_NOT_READY;
98
99         memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
100
101         return hal_ipc_cmd(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_DISCONNECT,
102                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
103 }
104
105 static bt_status_t init(btav_callbacks_t *callbacks)
106 {
107         struct hal_cmd_register_module cmd;
108         int ret;
109
110         DBG("");
111
112         if (interface_ready())
113                 return BT_STATUS_DONE;
114
115         cbs = callbacks;
116
117         hal_ipc_register(HAL_SERVICE_ID_A2DP, ev_handlers,
118                                 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
119
120         cmd.service_id = HAL_SERVICE_ID_A2DP;
121         cmd.mode = HAL_MODE_DEFAULT;
122         cmd.max_clients = 1;
123
124         ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
125                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
126
127         if (ret != BT_STATUS_SUCCESS) {
128                 cbs = NULL;
129                 hal_ipc_unregister(HAL_SERVICE_ID_A2DP);
130         }
131
132         return ret;
133 }
134
135 static void cleanup(void)
136 {
137         struct hal_cmd_unregister_module cmd;
138
139         DBG("");
140
141         if (!interface_ready())
142                 return;
143
144         cmd.service_id = HAL_SERVICE_ID_A2DP;
145
146         hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
147                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
148
149         hal_ipc_unregister(HAL_SERVICE_ID_A2DP);
150
151         cbs = NULL;
152 }
153
154 static btav_interface_t iface = {
155         .size = sizeof(iface),
156         .init = init,
157         .connect = a2dp_connect,
158         .disconnect = disconnect,
159         .cleanup = cleanup
160 };
161
162 btav_interface_t *bt_get_a2dp_interface(void)
163 {
164         return &iface;
165 }