4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Nilesh Trimbake <t.shripati@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
35 #define AVRC_MAX_NUM_MEDIA_ATTR_ID 7
37 static const btrc_ctrl_callbacks_t *bt_hal_avrcp_ctrl_cbacks;
39 static bool interface_ready(void)
41 return bt_hal_avrcp_ctrl_cbacks != NULL;
44 static bt_status_t avrcp_ctrl_connect(bt_bdaddr_t *bd_addr)
47 return _bt_hal_dbus_handler_avrcp_ctrl_connect(bd_addr);
50 static bt_status_t avrcp_ctrl_disconnect(bt_bdaddr_t *bd_addr)
53 return _bt_hal_dbus_handler_avrcp_ctrl_disconnect(bd_addr);
56 static bt_status_t avrcp_ctrl_send_pass_through_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, uint8_t key_state)
59 return _bt_hal_dbus_handler_avrcp_ctrl_cmd(bd_addr, key_code, key_state);
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)
66 int ret = BT_STATUS_SUCCESS;
69 for (i = 0; i < num_attrib; i++)
70 ret = _bt_hal_dbus_handler_avrcp_ctrl_set_property(bd_addr, type[i], value[i]);
75 static bt_status_t avrcp_transport_setting_cmd(bt_bdaddr_t *bd_addr,
76 uint8_t num_attrib, int *type, unsigned int *value)
79 int ret = BT_STATUS_SUCCESS;
82 for (i = 0; i < num_attrib; i++)
83 ret = _bt_hal_dbus_handler_avrcp_transport_set_property(bd_addr, type[i], value[i]);
88 static void __bt_hal_handle_avrcp_ctrl_conn_state(void *buf, uint16_t len)
90 struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
92 if (bt_hal_avrcp_ctrl_cbacks->connection_state_cb) {
93 if (ev->state == HAL_AVRCP_CTRL_STATE_CONNECTED)
94 bt_hal_avrcp_ctrl_cbacks->connection_state_cb(
95 TRUE, TRUE, (bt_bdaddr_t *) ev->bdaddr);
97 bt_hal_avrcp_ctrl_cbacks->connection_state_cb(
98 FALSE, FALSE, (bt_bdaddr_t *) ev->bdaddr);
102 btrc_play_status_t __get_play_status(int status)
104 btrc_play_status_t play_status;
105 play_status = BTRC_PLAYSTATE_ERROR;
108 case BTRC_PLAYSTATE_STOPPED:
109 play_status = BTRC_PLAYSTATE_STOPPED;
111 case BTRC_PLAYSTATE_PLAYING:
112 play_status = BTRC_PLAYSTATE_PLAYING;
114 case BTRC_PLAYSTATE_PAUSED:
115 play_status = BTRC_PLAYSTATE_PAUSED;
117 case BTRC_PLAYSTATE_FWD_SEEK:
118 play_status = BTRC_PLAYSTATE_FWD_SEEK;
120 case BTRC_PLAYSTATE_REV_SEEK:
121 play_status = BTRC_PLAYSTATE_REV_SEEK;
124 DBG("Wrong play status received:");
130 static void __bt_hal_handle_avrcp_ctrl_playstatus_changed_event(void *buf, uint16_t len)
132 struct hal_ev_play_status_changed *ev = buf;
134 if (bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb)
135 bt_hal_avrcp_ctrl_cbacks->play_status_changed_cb((bt_bdaddr_t *)ev->bdaddr, ev->status);
138 static void __bt_hal_handle_avrcp_ctrl_playposition_changed_event(void *buf, uint16_t len)
140 struct hal_ev_play_position *ev = buf;
142 if (bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb)
143 bt_hal_avrcp_ctrl_cbacks->play_position_changed_cb((bt_bdaddr_t *)ev->bdaddr, ev->len, ev->pos);
147 static void __bt_hal_handle_avrcp_ctrl_player_setting_changed(void *buf, uint16_t len)
149 struct hal_ev_player_setting *ev = buf;
150 btrc_player_settings_t player_setting;
152 memset(&player_setting, 0, sizeof(player_setting));
153 player_setting.num_attr = ev->num_attr;
154 memcpy(player_setting.attr_ids, ev->attr_ids, sizeof(player_setting.attr_ids));
155 memcpy(player_setting.attr_values, ev->attr_values, sizeof(player_setting.attr_values));
157 if (bt_hal_avrcp_ctrl_cbacks->playerapplicationsetting_changed_cb)
158 bt_hal_avrcp_ctrl_cbacks->playerapplicationsetting_changed_cb(
159 (bt_bdaddr_t *)ev->bdaddr, &player_setting);
162 static void __bt_hal_handle_avrcp_ctrl_pass_cmd_res_event(void *buf, uint16_t len)
164 struct hal_ev_pass_cmd_rsp *ev = (struct hal_ev_pass_cmd_rsp *)buf;
166 if (bt_hal_avrcp_ctrl_cbacks->passthrough_rsp_cb)
167 bt_hal_avrcp_ctrl_cbacks->passthrough_rsp_cb(NULL,
168 ev->key_code, ev->key_state);
171 static void __bt_hal_handle_avrcp_ctrl_set_app_setting_res_event(void *buf, uint16_t len)
173 struct hal_ev_set_player_app_setting_rsp *ev = buf;
175 if (bt_hal_avrcp_ctrl_cbacks->setplayerappsetting_rsp_cb)
176 bt_hal_avrcp_ctrl_cbacks->setplayerappsetting_rsp_cb((bt_bdaddr_t *)ev->bdaddr, ev->resp);
179 static void __bt_hal_handle_avrcp_ctrl_track_changed_event(void *buf, uint16_t len)
181 struct hal_ev_track_changed *ev = buf;
182 btrc_element_attr_val_t attr[HAL_MAX_ATTR_NUM];
185 for (i = 0; i < ev->num_attr; i++) {
186 attr[i].attr_id = ev->attr[i].attr_id;
187 memcpy(attr[i].text, ev->attr[i].text, sizeof(ev->attr[i].text));
190 DBG("call track_changed_cb");
191 if (bt_hal_avrcp_ctrl_cbacks->track_changed_cb)
192 bt_hal_avrcp_ctrl_cbacks->track_changed_cb(
193 (bt_bdaddr_t *)ev->bdaddr, ev->num_attr, attr);
196 static void __bt_hal_handle_avrcp_ctrl_event(int message, void *buf, uint16_t len)
199 if (!interface_ready())
202 case HAL_EV_AVRCP_CTRL_CONN_STATE:
203 DBG("Event: HAL_EV_AVRCP_CTRL_CONN_STATE");
204 __bt_hal_handle_avrcp_ctrl_conn_state(buf, len);
206 case HAL_EV_AVRCP_CTRL_PASS_THROUGH_RSP:
207 DBG("Event: HAL_EV_AVRCP_CTRL_PASS_THROUGH_RSP");
208 __bt_hal_handle_avrcp_ctrl_pass_cmd_res_event(buf, len);
210 case HAL_EV_AVRCP_CTRL_SET_PLAYER_APP_SETTING_RSP:
211 DBG("Event: HAL_EV_AVRCP_CTRL_SET_PLAYER_APP_SETTING_RSP");
212 __bt_hal_handle_avrcp_ctrl_set_app_setting_res_event(buf, len);
214 case HAL_EV_AVRCP_CTRL_PLAYER_APP_SETTING_CHANGED:
215 DBG("Event: HAL_EV_AVRCP_CTRL_PLAYER_APP_SETTING_CHANGED");
216 __bt_hal_handle_avrcp_ctrl_player_setting_changed(buf, len);
218 case HAL_EV_AVRCP_CTRL_TRACK_CHANGED:
219 DBG("Event: HAL_EV_AVRCP_CTRL_TRACK_CHANGED");
220 __bt_hal_handle_avrcp_ctrl_track_changed_event(buf, len);
222 case HAL_EV_AVRCP_CTRL_PLAY_POSITION_CHANGED:
223 DBG("Event: HAL_EV_AVRCP_CTRL_PLAY_POSITION_CHANGED");
224 __bt_hal_handle_avrcp_ctrl_playposition_changed_event(buf, len);
226 case HAL_EV_AVRCP_CTRL_PLAY_STATUS_CHANGED:
227 DBG("Event: HAL_EV_AVRCP_CTRL_PLAY_STATUS_CHANGED");
228 __bt_hal_handle_avrcp_ctrl_playstatus_changed_event(buf, len);
231 DBG("Event Currently not handled!!");
238 static bt_status_t init(btrc_ctrl_callbacks_t* callbacks)
242 if (interface_ready())
243 return BT_STATUS_DONE;
245 bt_hal_avrcp_ctrl_cbacks = callbacks;
246 DBG("Register AVRCP Ctroller events callback function");
247 _bt_hal_register_avrcp_ctrl_dbus_handler_cb(__bt_hal_handle_avrcp_ctrl_event);
248 _bt_hal_register_event_handler_cb(HAL_AVRCP_CTRL, __bt_hal_handle_avrcp_ctrl_event);
249 return BT_STATUS_SUCCESS;
252 static void cleanup(void)
256 if (!interface_ready())
259 _bt_hal_unregister_event_handler_cb(HAL_AVRCP_CTRL);
260 bt_hal_avrcp_ctrl_cbacks = NULL;
263 static btrc_ctrl_interface_t avrcp_ctrl_if = {
264 .size = sizeof(avrcp_ctrl_if),
266 .connect = avrcp_ctrl_connect,
267 .disconnect = avrcp_ctrl_disconnect,
268 .send_pass_through_cmd = avrcp_ctrl_send_pass_through_cmd,
269 .set_player_app_setting_cmd = avrcp_ctrl_set_player_app_setting_cmd,
270 .set_transport_setting_cmd = avrcp_transport_setting_cmd,
274 btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)
276 DBG("Get AVRCP Controller Profile Interface");
277 return &avrcp_ctrl_if;