resolved the code rule warnings
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-avrcp-ctrl.c
1 /*
2  * Bluetooth-frwk
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 <stdio.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <dlog.h>
28
29 #include "bt-hal.h"
30 #include "bt-hal-log.h"
31 #include "bt-hal-msg.h"
32 #include "bt-hal-utils.h"
33 #include "bt-hal-avrcp-ctrl-dbus-handler.h"
34
35 #define AVRC_MAX_NUM_MEDIA_ATTR_ID               7
36
37 static const btrc_ctrl_callbacks_t *bt_hal_avrcp_ctrl_cbacks;
38
39 static bool interface_ready(void)
40 {
41         return bt_hal_avrcp_ctrl_cbacks != NULL;
42 }
43
44 static bt_status_t avrcp_ctrl_connect(bt_bdaddr_t *bd_addr)
45 {
46         DBG("");
47         return _bt_hal_dbus_handler_avrcp_ctrl_connect(bd_addr);
48 }
49
50 static bt_status_t avrcp_ctrl_disconnect(bt_bdaddr_t *bd_addr)
51 {
52         DBG("");
53         return _bt_hal_dbus_handler_avrcp_ctrl_disconnect(bd_addr);
54 }
55
56 static bt_status_t avrcp_ctrl_send_pass_through_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, uint8_t key_state)
57 {
58         DBG("");
59         return _bt_hal_dbus_handler_avrcp_ctrl_cmd(bd_addr, key_code, key_state);
60 }
61
62 static bt_status_t avrcp_ctrl_set_player_app_setting_cmd(bt_bdaddr_t *bd_addr,
63                                 uint8_t num_attrib, uint8_t* type, uint8_t* value)
64 {
65         int i = 0;
66         int ret = BT_STATUS_SUCCESS;
67
68         DBG("");
69         for (i = 0; i < num_attrib; i++)
70                 ret = _bt_hal_dbus_handler_avrcp_ctrl_set_property(bd_addr, type[i], value[i]);
71
72         return ret;
73 }
74
75 static void __bt_hal_handle_avrcp_ctrl_conn_state(void *buf, uint16_t len)
76 {
77         struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
78
79         if (bt_hal_avrcp_ctrl_cbacks->connection_state_cb) {
80                 if (ev->state == HAL_AVRCP_CTRL_STATE_CONNECTED)
81                         bt_hal_avrcp_ctrl_cbacks->connection_state_cb(
82                                         TRUE, TRUE, (bt_bdaddr_t *) ev->bdaddr);
83                 else
84                         bt_hal_avrcp_ctrl_cbacks->connection_state_cb(
85                                         FALSE, FALSE, (bt_bdaddr_t *) ev->bdaddr);
86         }
87 }
88
89 btrc_play_status_t __get_play_status(int status)
90 {
91         btrc_play_status_t play_status;
92         play_status = BTRC_PLAYSTATE_ERROR;
93
94         switch (status) {
95         case BTRC_PLAYSTATE_STOPPED:
96                 play_status = BTRC_PLAYSTATE_STOPPED;
97                 break;
98         case BTRC_PLAYSTATE_PLAYING:
99                 play_status = BTRC_PLAYSTATE_PLAYING;
100                 break;
101         case BTRC_PLAYSTATE_PAUSED:
102                 play_status = BTRC_PLAYSTATE_PAUSED;
103                 break;
104         case BTRC_PLAYSTATE_FWD_SEEK:
105                 play_status = BTRC_PLAYSTATE_FWD_SEEK;
106                 break;
107         case BTRC_PLAYSTATE_REV_SEEK:
108                 play_status = BTRC_PLAYSTATE_REV_SEEK;
109                 break;
110         default:
111                 DBG("Wrong play status received:");
112                 break;
113         }
114         return play_status;
115 }
116
117 static void __bt_hal_handle_avrcp_ctrl_playstatus_changed_event(void *buf, uint16_t len)
118 {
119         struct hal_ev_play_status_changed *ev = buf;
120
121         if (bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb)
122                 bt_hal_avrcp_ctrl_cbacks->play_status_changed_cb((bt_bdaddr_t *)ev->bdaddr, ev->status);
123 }
124
125 static void __bt_hal_handle_avrcp_ctrl_playposition_changed_event(void *buf, uint16_t len)
126 {
127         struct hal_ev_play_position *ev = buf;
128
129         if (bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb)
130                 bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb((bt_bdaddr_t *)ev->bdaddr, ev->len, ev->pos);
131
132 }
133
134 static void __bt_hal_handle_avrcp_ctrl_player_setting_changed(void *buf, uint16_t len)
135 {
136         struct hal_ev_player_setting *ev = buf;
137         btrc_player_settings_t player_setting;
138
139         memset(&player_setting, 0, sizeof(player_setting));
140         player_setting.num_attr = ev->num_attr;
141         memcpy(player_setting.attr_ids, ev->attr_ids, sizeof(player_setting.attr_ids));
142         memcpy(player_setting.attr_values, ev->attr_values, sizeof(player_setting.attr_values));
143
144         if (bt_hal_avrcp_ctrl_cbacks->playerapplicationsetting_changed_cb)
145                 bt_hal_avrcp_ctrl_cbacks->playerapplicationsetting_changed_cb(
146                                 (bt_bdaddr_t *)ev->bdaddr, &player_setting);
147 }
148
149 static void __bt_hal_handle_avrcp_ctrl_pass_cmd_res_event(void *buf, uint16_t len)
150 {
151         struct hal_ev_pass_cmd_rsp *ev = (struct hal_ev_pass_cmd_rsp *)buf;
152
153         if (bt_hal_avrcp_ctrl_cbacks->passthrough_rsp_cb)
154                 bt_hal_avrcp_ctrl_cbacks->passthrough_rsp_cb(NULL,
155                         ev->key_code, ev->key_state);
156 }
157
158 static void __bt_hal_handle_avrcp_ctrl_set_app_setting_res_event(void *buf, uint16_t len)
159 {
160         struct hal_ev_set_player_app_setting_rsp *ev = buf;
161
162         if (bt_hal_avrcp_ctrl_cbacks->setplayerappsetting_rsp_cb)
163                 bt_hal_avrcp_ctrl_cbacks->setplayerappsetting_rsp_cb((bt_bdaddr_t *)ev->bdaddr, ev->resp);
164 }
165
166 static void __bt_hal_handle_avrcp_ctrl_track_changed_event(void *buf, uint16_t len)
167 {
168         struct hal_ev_track_changed *ev = buf;
169         btrc_element_attr_val_t attr[HAL_MAX_ATTR_NUM];
170         int i;
171
172         for (i = 0; i < ev->num_attr; i++) {
173                 attr[i].attr_id = ev->attr[i].attr_id;
174                 memcpy(attr[i].text, ev->attr[i].text, sizeof(ev->attr[i].text));
175         }
176
177         DBG("call track_changed_cb");
178         if (bt_hal_avrcp_ctrl_cbacks->track_changed_cb)
179                 bt_hal_avrcp_ctrl_cbacks->track_changed_cb(
180                                 (bt_bdaddr_t *)ev->bdaddr, ev->num_attr, attr);
181 }
182
183 static void __bt_hal_handle_avrcp_ctrl_event(int message, void *buf, uint16_t len)
184 {
185         DBG("+");
186         if (!interface_ready())
187                 return;
188         switch (message) {
189         case HAL_EV_AVRCP_CTRL_CONN_STATE:
190                 DBG("Event: HAL_EV_AVRCP_CTRL_CONN_STATE");
191                 __bt_hal_handle_avrcp_ctrl_conn_state(buf, len);
192                 break;
193         case HAL_EV_AVRCP_CTRL_PASS_THROUGH_RSP:
194                 DBG("Event: HAL_EV_AVRCP_CTRL_PASS_THROUGH_RSP");
195                 __bt_hal_handle_avrcp_ctrl_pass_cmd_res_event(buf, len);
196                 break;
197         case HAL_EV_AVRCP_CTRL_SET_PLAYER_APP_SETTING_RSP:
198                 DBG("Event: HAL_EV_AVRCP_CTRL_SET_PLAYER_APP_SETTING_RSP");
199                 __bt_hal_handle_avrcp_ctrl_set_app_setting_res_event(buf, len);
200                 break;
201         case HAL_EV_AVRCP_CTRL_PLAYER_APP_SETTING_CHANGED:
202                 DBG("Event: HAL_EV_AVRCP_CTRL_PLAYER_APP_SETTING_CHANGED");
203                 __bt_hal_handle_avrcp_ctrl_player_setting_changed(buf, len);
204                 break;
205         case HAL_EV_AVRCP_CTRL_TRACK_CHANGED:
206                 DBG("Event: HAL_EV_AVRCP_CTRL_TRACK_CHANGED");
207                 __bt_hal_handle_avrcp_ctrl_track_changed_event(buf, len);
208                 break;
209         case HAL_EV_AVRCP_CTRL_PLAY_POSITION_CHANGED:
210                 DBG("Event: HAL_EV_AVRCP_CTRL_PLAY_POSITION_CHANGED");
211                 __bt_hal_handle_avrcp_ctrl_playposition_changed_event(buf, len);
212                 break;
213         case HAL_EV_AVRCP_CTRL_PLAY_STATUS_CHANGED:
214                 DBG("Event: HAL_EV_AVRCP_CTRL_PLAY_STATUS_CHANGED");
215                 __bt_hal_handle_avrcp_ctrl_playstatus_changed_event(buf, len);
216                 break;
217         default:
218                 DBG("Event Currently not handled!!");
219                 break;
220         }
221
222         DBG("-");
223 }
224
225 static bt_status_t init(btrc_ctrl_callbacks_t* callbacks)
226 {
227         DBG("");
228
229         if (interface_ready())
230                 return BT_STATUS_DONE;
231
232         bt_hal_avrcp_ctrl_cbacks = callbacks;
233         DBG("Register AVRCP Ctroller events callback function");
234         _bt_hal_register_avrcp_ctrl_dbus_handler_cb(__bt_hal_handle_avrcp_ctrl_event);
235         _bt_hal_register_event_handler_cb(HAL_AVRCP_CTRL, __bt_hal_handle_avrcp_ctrl_event);
236         return BT_STATUS_SUCCESS;
237 }
238
239 static void cleanup(void)
240 {
241         DBG("");
242
243         if (!interface_ready())
244                 return;
245
246         _bt_hal_unregister_event_handler_cb(HAL_AVRCP_CTRL);
247         bt_hal_avrcp_ctrl_cbacks = NULL;
248 }
249
250 static btrc_ctrl_interface_t avrcp_ctrl_if = {
251         .size = sizeof(avrcp_ctrl_if),
252         .init = init,
253         .connect = avrcp_ctrl_connect,
254         .disconnect = avrcp_ctrl_disconnect,
255         .send_pass_through_cmd = avrcp_ctrl_send_pass_through_cmd,
256         .set_player_app_setting_cmd = avrcp_ctrl_set_player_app_setting_cmd,
257         .cleanup = cleanup
258 };
259
260 btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)
261 {
262         DBG("Get AVRCP Controller Profile Interface");
263         return &avrcp_ctrl_if;
264 }