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 void __bt_hal_handle_avrcp_ctrl_conn_state(void *buf, uint16_t len)
77 struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
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);
84 bt_hal_avrcp_ctrl_cbacks->connection_state_cb(
85 FALSE, FALSE, (bt_bdaddr_t *) ev->bdaddr);
89 btrc_play_status_t __get_play_status(int status)
91 btrc_play_status_t play_status;
92 play_status = BTRC_PLAYSTATE_ERROR;
95 case BTRC_PLAYSTATE_STOPPED:
96 play_status = BTRC_PLAYSTATE_STOPPED;
98 case BTRC_PLAYSTATE_PLAYING:
99 play_status = BTRC_PLAYSTATE_PLAYING;
101 case BTRC_PLAYSTATE_PAUSED:
102 play_status = BTRC_PLAYSTATE_PAUSED;
104 case BTRC_PLAYSTATE_FWD_SEEK:
105 play_status = BTRC_PLAYSTATE_FWD_SEEK;
107 case BTRC_PLAYSTATE_REV_SEEK:
108 play_status = BTRC_PLAYSTATE_REV_SEEK;
111 DBG("Wrong play status received:");
117 static void __bt_hal_handle_avrcp_ctrl_playstatus_changed_event(void *buf, uint16_t len)
119 struct hal_ev_play_status_changed *ev = buf;
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);
125 static void __bt_hal_handle_avrcp_ctrl_playposition_changed_event(void *buf, uint16_t len)
127 struct hal_ev_play_position *ev = buf;
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);
134 static void __bt_hal_handle_avrcp_ctrl_player_setting_changed(void *buf, uint16_t len)
136 struct hal_ev_player_setting *ev = buf;
137 btrc_player_settings_t player_setting;
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));
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);
149 static void __bt_hal_handle_avrcp_ctrl_pass_cmd_res_event(void *buf, uint16_t len)
151 struct hal_ev_pass_cmd_rsp *ev = (struct hal_ev_pass_cmd_rsp *)buf;
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);
158 static void __bt_hal_handle_avrcp_ctrl_set_app_setting_res_event(void *buf, uint16_t len)
160 struct hal_ev_set_player_app_setting_rsp *ev = buf;
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);
166 static void __bt_hal_handle_avrcp_ctrl_track_changed_event(void *buf, uint16_t len)
168 struct hal_ev_track_changed *ev = buf;
169 btrc_element_attr_val_t attr[HAL_MAX_ATTR_NUM];
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));
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);
183 static void __bt_hal_handle_avrcp_ctrl_event(int message, void *buf, uint16_t len)
186 if (!interface_ready())
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);
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);
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);
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);
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);
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);
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);
218 DBG("Event Currently not handled!!");
225 static bt_status_t init(btrc_ctrl_callbacks_t* callbacks)
229 if (interface_ready())
230 return BT_STATUS_DONE;
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;
239 static void cleanup(void)
243 if (!interface_ready())
246 _bt_hal_unregister_event_handler_cb(HAL_AVRCP_CTRL);
247 bt_hal_avrcp_ctrl_cbacks = NULL;
250 static btrc_ctrl_interface_t avrcp_ctrl_if = {
251 .size = sizeof(avrcp_ctrl_if),
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,
260 btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)
262 DBG("Get AVRCP Controller Profile Interface");
263 return &avrcp_ctrl_if;