2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "bluetooth-api.h"
21 #include "bluetooth-media-control.h"
22 #include "bt-internal-types.h"
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
27 #ifdef TIZEN_FEATURE_BT_DPM
31 BT_EXPORT_API int bluetooth_media_player_init(media_cb_func_ptr callback_ptr,
36 /* Register AVRCP events */
37 ret = _bt_register_event(BT_AVRCP_EVENT , (void *)callback_ptr, user_data);
39 if (ret != BLUETOOTH_ERROR_NONE &&
40 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
41 BT_ERR("Fail to init the event handler");
45 _bt_set_user_data(BT_AVRCP, (void *)callback_ptr, user_data);
47 return BLUETOOTH_ERROR_NONE;
50 BT_EXPORT_API int bluetooth_media_player_deinit(void)
54 ret = _bt_unregister_event(BT_AVRCP_EVENT);
56 if (ret != BLUETOOTH_ERROR_NONE) {
57 BT_ERR("Fail to deinit the event handler");
61 _bt_set_user_data(BT_AVRCP, NULL, NULL);
63 return BLUETOOTH_ERROR_NONE;
66 BT_EXPORT_API int bluetooth_media_player_change_property(
67 media_player_property_type type,
72 BT_CHECK_ENABLED(return);
74 #ifdef TIZEN_FEATURE_BT_DPM
75 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
76 BT_ERR("Not allow to use AVRCP profile");
77 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
82 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
84 g_array_append_vals(in_param1, &type, sizeof(int));
85 g_array_append_vals(in_param2, &value, sizeof(unsigned int));
87 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_PROPERTY,
88 in_param1, in_param2, in_param3, in_param4, &out_param);
90 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
95 BT_EXPORT_API int bluetooth_media_player_set_properties(
96 media_player_settings_t *setting)
100 BT_CHECK_PARAMETER(setting, return);
101 BT_CHECK_ENABLED(return);
103 #ifdef TIZEN_FEATURE_BT_DPM
104 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
105 BT_ERR("Not allow to use AVRCP profile");
106 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
111 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
113 g_array_append_vals(in_param1, setting, sizeof(media_player_settings_t));
115 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_PROPERTIES,
116 in_param1, in_param2, in_param3, in_param4, &out_param);
118 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
123 BT_EXPORT_API int bluetooth_media_player_change_track(
124 media_metadata_attributes_t *metadata)
127 media_metadata_t meta_data;
129 BT_CHECK_PARAMETER(metadata, return);
130 BT_CHECK_ENABLED(return);
132 #ifdef TIZEN_FEATURE_BT_DPM
133 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
134 BT_ERR("Not allow to use AVRCP profile");
135 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
140 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
142 memset(&meta_data, 0x00, sizeof(media_metadata_t));
144 if (_bt_copy_utf8_string(meta_data.title, metadata->title, BT_META_DATA_MAX_LEN))
145 BT_ERR("Error in copying Title\n");
146 if (_bt_copy_utf8_string(meta_data.artist, metadata->artist, BT_META_DATA_MAX_LEN))
147 BT_ERR("Error in copying Artist\n");
148 if (_bt_copy_utf8_string(meta_data.album, metadata->album, BT_META_DATA_MAX_LEN))
149 BT_ERR("Error in copying Album\n");
150 if (_bt_copy_utf8_string(meta_data.genre, metadata->genre, BT_META_DATA_MAX_LEN))
151 BT_ERR("Error in copying Genre\n");
153 if (_bt_utf8_validate(meta_data.title) == FALSE)
154 meta_data.title[0] = '\0';
156 if (_bt_utf8_validate(meta_data.artist) == FALSE)
157 meta_data.artist[0] = '\0';
159 if (_bt_utf8_validate(meta_data.album) == FALSE)
160 meta_data.album[0] = '\0';
162 if (_bt_utf8_validate(meta_data.genre) == FALSE)
163 meta_data.genre[0] = '\0';
165 meta_data.total_tracks = metadata->total_tracks;
166 meta_data.number = metadata->number;
167 meta_data.duration = metadata->duration;
169 g_array_append_vals(in_param1, &meta_data, sizeof(media_metadata_t));
171 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_TRACK_INFO,
172 in_param1, in_param2, in_param3, in_param4, &out_param);
174 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
179 BT_EXPORT_API int bluetooth_media_control_init(media_cb_func_ptr callback_ptr,
184 /* Register AVRCP events */
185 ret = _bt_register_event(BT_AVRCP_CONTROL_EVENT,
186 (void *)callback_ptr, user_data);
188 if (ret != BLUETOOTH_ERROR_NONE &&
189 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
190 BT_ERR("Fail to init the event handler");
194 _bt_set_user_data(BT_AVRCP, (void *)callback_ptr, user_data);
196 return BLUETOOTH_ERROR_NONE;
199 BT_EXPORT_API int bluetooth_media_control_deinit(void)
203 ret = _bt_unregister_event(BT_AVRCP_CONTROL_EVENT);
205 if (ret != BLUETOOTH_ERROR_NONE) {
206 BT_ERR("Fail to deinit the event handler");
210 _bt_set_user_data(BT_AVRCP, NULL, NULL);
212 return BLUETOOTH_ERROR_NONE;
215 BT_EXPORT_API int bluetooth_media_control_connect(
216 bluetooth_device_address_t *remote_address)
219 bt_user_info_t *user_info;
221 BT_CHECK_PARAMETER(remote_address, return);
222 BT_CHECK_ENABLED(return);
224 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_AVRCP_CONTROL_CONNECT)
225 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
226 BT_ERR("Don't have a privilege to use this API");
227 return BLUETOOTH_ERROR_PERMISSION_DEINED;
230 #ifdef TIZEN_FEATURE_BT_DPM
231 if (_bt_check_dpm(BT_DPM_ADDRESS, remote_address) == BT_DPM_RESTRICTED) {
232 BT_ERR("Blacklist device");
233 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
236 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
237 BT_ERR("Not allow to use AVRCP profile");
238 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
242 user_info = _bt_get_user_data(BT_AVRCP);
243 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
246 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
248 g_array_append_vals(in_param1, remote_address,
249 sizeof(bluetooth_device_address_t));
251 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
252 BT_AVRCP_CONTROL_CONNECT, in_param1,
253 in_param2, in_param3, in_param4,
254 user_info->cb, user_info->user_data);
256 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
261 BT_EXPORT_API int bluetooth_media_control_disconnect(
262 bluetooth_device_address_t *remote_address)
265 bt_user_info_t *user_info;
267 BT_CHECK_PARAMETER(remote_address, return);
268 BT_CHECK_ENABLED(return);
270 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_AVRCP_CONTROL_DISCONNECT)
271 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
272 BT_ERR("Don't have a privilege to use this API");
273 return BLUETOOTH_ERROR_PERMISSION_DEINED;
276 user_info = _bt_get_user_data(BT_AVRCP);
277 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
280 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
282 g_array_append_vals(in_param1, remote_address,
283 sizeof(bluetooth_device_address_t));
285 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
286 BT_AVRCP_CONTROL_DISCONNECT, in_param1,
287 in_param2, in_param3, in_param4,
288 user_info->cb, user_info->user_data);
290 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
295 BT_EXPORT_API int bluetooth_media_target_connect(
296 bluetooth_device_address_t *remote_address)
299 bt_user_info_t *user_info;
301 BT_CHECK_PARAMETER(remote_address, return);
302 BT_CHECK_ENABLED(return);
304 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_AVRCP_TARGET_CONNECT)
305 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
306 BT_ERR("Don't have a privilege to use this API");
307 return BLUETOOTH_ERROR_PERMISSION_DEINED;
310 #ifdef TIZEN_FEATURE_BT_DPM
311 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
312 BT_ERR("Not allow to use AVRCP profile");
313 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
316 user_info = _bt_get_user_data(BT_AVRCP);
317 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
320 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
322 g_array_append_vals(in_param1, remote_address,
323 sizeof(bluetooth_device_address_t));
325 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
326 BT_AVRCP_TARGET_CONNECT, in_param1,
327 in_param2, in_param3, in_param4,
328 user_info->cb, user_info->user_data);
330 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
335 BT_EXPORT_API int bluetooth_media_target_disconnect(
336 bluetooth_device_address_t *remote_address)
339 bt_user_info_t *user_info;
341 BT_CHECK_PARAMETER(remote_address, return);
342 BT_CHECK_ENABLED(return);
344 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_AVRCP_TARGET_DISCONNECT)
345 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
346 BT_ERR("Don't have a privilege to use this API");
347 return BLUETOOTH_ERROR_PERMISSION_DEINED;
350 user_info = _bt_get_user_data(BT_AVRCP);
351 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
354 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
356 g_array_append_vals(in_param1, remote_address,
357 sizeof(bluetooth_device_address_t));
359 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
360 BT_AVRCP_TARGET_DISCONNECT, in_param1,
361 in_param2, in_param3, in_param4,
362 user_info->cb, user_info->user_data);
364 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
369 BT_EXPORT_API int bluetooth_media_control_command(
370 media_player_control_cmd type)
374 BT_CHECK_ENABLED(return);
376 #ifdef TIZEN_FEATURE_BT_DPM
377 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
378 BT_ERR("Not allow to use AVRCP profile");
379 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
383 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
385 g_array_append_vals(in_param1, &type, sizeof(int));
387 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_HANDLE_CONTROL,
388 in_param1, in_param2, in_param3, in_param4, &out_param);
390 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
395 BT_EXPORT_API int bluetooth_media_control_set_property(
396 media_player_property_type type,
401 BT_CHECK_ENABLED(return);
403 #ifdef TIZEN_FEATURE_BT_DPM
404 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
405 BT_ERR("Not allow to use AVRCP profile");
406 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
411 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
413 g_array_append_vals(in_param1, &type, sizeof(int));
414 g_array_append_vals(in_param2, &value, sizeof(unsigned int));
416 result = _bt_send_request(BT_BLUEZ_SERVICE,
417 BT_AVRCP_CONTROL_SET_PROPERTY,
418 in_param1, in_param2, in_param3,
419 in_param4, &out_param);
421 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
426 BT_EXPORT_API int bluetooth_media_control_get_property(
427 media_player_property_type type,
432 BT_CHECK_PARAMETER(value, return);
433 BT_CHECK_ENABLED(return);
435 #ifdef TIZEN_FEATURE_BT_DPM
436 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
437 BT_ERR("Not allow to use AVRCP profile");
438 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
443 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
444 g_array_append_vals(in_param1, &type, sizeof(int));
446 result = _bt_send_request(BT_BLUEZ_SERVICE,
447 BT_AVRCP_CONTROL_GET_PROPERTY,
448 in_param1, in_param2, in_param3,
449 in_param4, &out_param);
451 if (result == BLUETOOTH_ERROR_NONE)
452 *value = g_array_index(out_param, int, 0);
454 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
459 BT_EXPORT_API int bluetooth_media_control_get_track_info(
460 media_metadata_attributes_t *metadata)
463 media_metadata_t meta_data;
465 BT_CHECK_PARAMETER(metadata, return);
466 BT_CHECK_ENABLED(return);
468 #ifdef TIZEN_FEATURE_BT_DPM
469 if (_bt_check_dpm(BT_DPM_AVRCP, NULL) == BT_DPM_RESTRICTED) {
470 BT_ERR("Not allow to use AVRCP profile");
471 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
476 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
478 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_GET_TRACK_INFO,
479 in_param1, in_param2, in_param3, in_param4, &out_param);
481 memset(&meta_data, 0x00, sizeof(media_metadata_t));
483 meta_data = g_array_index(out_param, media_metadata_t, 0);
485 metadata->title = g_strdup(meta_data.title);
486 metadata->artist = g_strdup(meta_data.artist);
487 metadata->album = g_strdup(meta_data.album);
488 metadata->genre = g_strdup(meta_data.genre);
489 metadata->total_tracks = meta_data.total_tracks;
490 metadata->number = meta_data.number;
491 metadata->duration = (int64_t) meta_data.duration;
493 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);