Modify build feature names as Tizen 3.0 build option naming rule
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-avrcp.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 <string.h>
19
20 #include "bluetooth-api.h"
21 #include "bluetooth-media-control.h"
22 #include "bt-internal-types.h"
23
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
27 #ifdef TIZEN_FEATURE_BT_DPM
28 #include "bt-dpm.h"
29 #endif
30
31 BT_EXPORT_API int bluetooth_media_player_init(media_cb_func_ptr callback_ptr,
32                                                 void *user_data)
33 {
34         int ret;
35
36         /* Register AVRCP events */
37         ret = _bt_register_event(BT_AVRCP_EVENT , (void *)callback_ptr, user_data);
38
39         if (ret != BLUETOOTH_ERROR_NONE &&
40              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
41                 BT_ERR("Fail to init the event handler");
42                 return ret;
43         }
44
45         _bt_set_user_data(BT_AVRCP, (void *)callback_ptr, user_data);
46
47         return BLUETOOTH_ERROR_NONE;
48 }
49
50 BT_EXPORT_API int bluetooth_media_player_deinit(void)
51 {
52         int ret;
53
54         ret = _bt_unregister_event(BT_AVRCP_EVENT);
55
56         if (ret != BLUETOOTH_ERROR_NONE) {
57                 BT_ERR("Fail to deinit the event handler");
58                 return ret;
59         }
60
61         _bt_set_user_data(BT_AVRCP, NULL, NULL);
62
63         return BLUETOOTH_ERROR_NONE;
64 }
65
66 BT_EXPORT_API int bluetooth_media_player_change_property(
67                         media_player_property_type type,
68                         unsigned int value)
69 {
70         int result;
71
72         BT_CHECK_ENABLED(return);
73
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;
78         }
79 #endif
80
81         BT_INIT_PARAMS();
82         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
83
84         g_array_append_vals(in_param1, &type, sizeof(int));
85         g_array_append_vals(in_param2, &value, sizeof(unsigned int));
86
87         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_PROPERTY,
88                 in_param1, in_param2, in_param3, in_param4, &out_param);
89
90         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
91
92         return result;
93 }
94
95 BT_EXPORT_API int bluetooth_media_player_set_properties(
96                         media_player_settings_t *setting)
97 {
98         int result;
99
100         BT_CHECK_PARAMETER(setting, return);
101         BT_CHECK_ENABLED(return);
102
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;
107         }
108 #endif
109
110         BT_INIT_PARAMS();
111         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
112
113         g_array_append_vals(in_param1, setting, sizeof(media_player_settings_t));
114
115         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_PROPERTIES,
116                 in_param1, in_param2, in_param3, in_param4, &out_param);
117
118         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
119
120         return result;
121 }
122
123 BT_EXPORT_API int bluetooth_media_player_change_track(
124                 media_metadata_attributes_t *metadata)
125 {
126         int result;
127         media_metadata_t meta_data;
128
129         BT_CHECK_PARAMETER(metadata, return);
130         BT_CHECK_ENABLED(return);
131
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;
136         }
137 #endif
138
139         BT_INIT_PARAMS();
140         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141
142         memset(&meta_data, 0x00, sizeof(media_metadata_t));
143
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");
152
153         if (_bt_utf8_validate(meta_data.title) == FALSE)
154                 meta_data.title[0] = '\0';
155
156         if (_bt_utf8_validate(meta_data.artist) == FALSE)
157                 meta_data.artist[0] = '\0';
158
159         if (_bt_utf8_validate(meta_data.album) == FALSE)
160                 meta_data.album[0] = '\0';
161
162         if (_bt_utf8_validate(meta_data.genre) == FALSE)
163                 meta_data.genre[0] = '\0';
164
165         meta_data.total_tracks = metadata->total_tracks;
166         meta_data.number = metadata->number;
167         meta_data.duration = metadata->duration;
168
169         g_array_append_vals(in_param1, &meta_data, sizeof(media_metadata_t));
170
171         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_SET_TRACK_INFO,
172                 in_param1, in_param2, in_param3, in_param4, &out_param);
173
174         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
175
176         return result;
177 }
178
179 BT_EXPORT_API int bluetooth_media_control_init(media_cb_func_ptr callback_ptr,
180                                                 void *user_data)
181 {
182         int ret;
183
184         /* Register AVRCP events */
185         ret = _bt_register_event(BT_AVRCP_CONTROL_EVENT,
186                                         (void *)callback_ptr, user_data);
187
188         if (ret != BLUETOOTH_ERROR_NONE &&
189                         ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
190                 BT_ERR("Fail to init the event handler");
191                 return ret;
192         }
193
194         _bt_set_user_data(BT_AVRCP, (void *)callback_ptr, user_data);
195
196         return BLUETOOTH_ERROR_NONE;
197 }
198
199 BT_EXPORT_API int bluetooth_media_control_deinit(void)
200 {
201         int ret;
202
203         ret = _bt_unregister_event(BT_AVRCP_CONTROL_EVENT);
204
205         if (ret != BLUETOOTH_ERROR_NONE) {
206                 BT_ERR("Fail to deinit the event handler");
207                 return ret;
208         }
209
210         _bt_set_user_data(BT_AVRCP, NULL, NULL);
211
212         return BLUETOOTH_ERROR_NONE;
213 }
214
215 BT_EXPORT_API int bluetooth_media_control_connect(
216                         bluetooth_device_address_t *remote_address)
217 {
218         int result;
219         bt_user_info_t *user_info;
220
221         BT_CHECK_PARAMETER(remote_address, return);
222         BT_CHECK_ENABLED(return);
223
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;
228         }
229
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;
234         }
235
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;
239         }
240 #endif
241
242         user_info = _bt_get_user_data(BT_AVRCP);
243         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
244
245         BT_INIT_PARAMS();
246         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
247
248         g_array_append_vals(in_param1, remote_address,
249                                         sizeof(bluetooth_device_address_t));
250
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);
255
256         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
257
258         return result;
259 }
260
261 BT_EXPORT_API int bluetooth_media_control_disconnect(
262                         bluetooth_device_address_t *remote_address)
263 {
264         int result;
265         bt_user_info_t *user_info;
266
267         BT_CHECK_PARAMETER(remote_address, return);
268         BT_CHECK_ENABLED(return);
269
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;
274         }
275
276         user_info = _bt_get_user_data(BT_AVRCP);
277         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
278
279         BT_INIT_PARAMS();
280         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
281
282         g_array_append_vals(in_param1, remote_address,
283                                         sizeof(bluetooth_device_address_t));
284
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);
289
290         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
291
292         return result;
293 }
294
295 BT_EXPORT_API int bluetooth_media_target_connect(
296                         bluetooth_device_address_t *remote_address)
297 {
298         int result;
299         bt_user_info_t *user_info;
300
301         BT_CHECK_PARAMETER(remote_address, return);
302         BT_CHECK_ENABLED(return);
303
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;
308         }
309
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;
314         }
315 #endif
316         user_info = _bt_get_user_data(BT_AVRCP);
317         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
318
319         BT_INIT_PARAMS();
320         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
321
322         g_array_append_vals(in_param1, remote_address,
323                                         sizeof(bluetooth_device_address_t));
324
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);
329
330         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
331
332         return result;
333 }
334
335 BT_EXPORT_API int bluetooth_media_target_disconnect(
336                         bluetooth_device_address_t *remote_address)
337 {
338         int result;
339         bt_user_info_t *user_info;
340
341         BT_CHECK_PARAMETER(remote_address, return);
342         BT_CHECK_ENABLED(return);
343
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;
348         }
349
350         user_info = _bt_get_user_data(BT_AVRCP);
351         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
352
353         BT_INIT_PARAMS();
354         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
355
356         g_array_append_vals(in_param1, remote_address,
357                                         sizeof(bluetooth_device_address_t));
358
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);
363
364         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
365
366         return result;
367 }
368
369 BT_EXPORT_API int bluetooth_media_control_command(
370                                                 media_player_control_cmd type)
371 {
372         int result;
373
374         BT_CHECK_ENABLED(return);
375
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;
380         }
381 #endif
382         BT_INIT_PARAMS();
383         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
384
385         g_array_append_vals(in_param1, &type, sizeof(int));
386
387         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_HANDLE_CONTROL,
388                 in_param1, in_param2, in_param3, in_param4, &out_param);
389
390         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
391
392         return result;
393 }
394
395 BT_EXPORT_API int bluetooth_media_control_set_property(
396                                                 media_player_property_type type,
397                                                 unsigned int value)
398 {
399         int result;
400
401         BT_CHECK_ENABLED(return);
402
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;
407         }
408 #endif
409
410         BT_INIT_PARAMS();
411         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
412
413         g_array_append_vals(in_param1, &type, sizeof(int));
414         g_array_append_vals(in_param2, &value, sizeof(unsigned int));
415
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);
420
421         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
422
423         return result;
424 }
425
426 BT_EXPORT_API int bluetooth_media_control_get_property(
427                                         media_player_property_type type,
428                                         unsigned int *value)
429 {
430         int result;
431
432         BT_CHECK_PARAMETER(value, return);
433         BT_CHECK_ENABLED(return);
434
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;
439         }
440 #endif
441
442         BT_INIT_PARAMS();
443         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
444         g_array_append_vals(in_param1, &type, sizeof(int));
445
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);
450
451         if (result == BLUETOOTH_ERROR_NONE)
452                 *value = g_array_index(out_param, int, 0);
453
454         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
455
456         return result;
457 }
458
459 BT_EXPORT_API int bluetooth_media_control_get_track_info(
460                 media_metadata_attributes_t *metadata)
461 {
462         int result;
463         media_metadata_t meta_data;
464
465         BT_CHECK_PARAMETER(metadata, return);
466         BT_CHECK_ENABLED(return);
467
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;
472         }
473 #endif
474
475         BT_INIT_PARAMS();
476         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
477
478         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_AVRCP_GET_TRACK_INFO,
479                 in_param1, in_param2, in_param3, in_param4, &out_param);
480
481         memset(&meta_data, 0x00, sizeof(media_metadata_t));
482
483         meta_data = g_array_index(out_param, media_metadata_t, 0);
484
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;
492
493         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
494
495         return result;
496 }