Add the sender name in signal subscribe function
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / audio / avrcp / bt-service-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 <sys/socket.h>
23 #include <sys/errno.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <vconf.h>
27
28 #include "oal-hardware.h"
29 #include "oal-device-mgr.h"
30 #include <oal-manager.h>
31 #include <oal-avrcp-ct.h>
32
33 #include <bt-service-avrcp-ctrl.h>
34 #include <bluetooth-media-control.h>
35 #include <bt-service-event.h>
36
37 static int __media_prop_to_oal_type(int type)
38 {
39         int oal_type;
40
41         switch (type) {
42         case EQUALIZER:
43                 oal_type = OAL_EQUALIZER;
44                 break;
45         case SHUFFLE:
46                 oal_type = OAL_SHUFFLE;
47                 break;
48         case REPEAT:
49                 oal_type = OAL_REPEAT;
50                 break;
51         case SCAN:
52                 oal_type = OAL_SCAN;
53                 break;
54         case POSITION:
55                 oal_type = OAL_PLAY_POSITION;
56                 break;
57         case STATUS:
58                 oal_type = OAL_PLAY_STATUS;
59                 break;
60         default:
61                 BT_ERR("Unknown type: %d", type);
62                 oal_type = -1;
63         }
64
65         return oal_type;
66 }
67
68 static int __oal_type_to_media_prop(int oal_type)
69 {
70         int type;
71
72         switch (oal_type) {
73         case OAL_EQUALIZER:
74                 type = EQUALIZER;
75                 break;
76         case OAL_SHUFFLE:
77                 type = SHUFFLE;
78                 break;
79         case OAL_REPEAT:
80                 type = REPEAT;
81                 break;
82         case OAL_SCAN:
83                 type = SCAN;
84                 break;
85         case OAL_PLAY_POSITION:
86                 type = POSITION;
87                 break;
88         case OAL_PLAY_STATUS:
89                 type = STATUS;
90                 break;
91         default:
92                 BT_ERR("Unknown type: %d", oal_type);
93                 type = -1;
94         }
95
96         return type;
97 }
98
99 int _bt_avrcp_connect_remote_target(bluetooth_device_address_t *device_address)
100 {
101         oal_status_t status = OAL_STATUS_SUCCESS;
102         int result = BLUETOOTH_ERROR_NONE;
103         BT_INFO("+");
104
105         status = avrcp_ct_connect((bt_address_t*)device_address);
106         if (status != OAL_STATUS_SUCCESS) {
107                 BT_ERR("Connection could not be established, err: [%d]", status);
108                 result = _bt_convert_oal_status_to_bt_error(status);
109         }
110         return result;
111 }
112
113 int _bt_avrcp_disconnect_remote_target(bluetooth_device_address_t *device_address)
114 {
115         oal_status_t status = OAL_STATUS_SUCCESS;
116         int result = BLUETOOTH_ERROR_NONE;
117         BT_INFO("+");
118
119         status = avrcp_ct_disconnect((bt_address_t*)device_address);
120         if (status != OAL_STATUS_SUCCESS) {
121                 BT_ERR("DisConnection err: [%d]", status);
122                 result = _bt_convert_oal_status_to_bt_error(status);
123         }
124         return result;
125 }
126
127 int _bt_avrcp_control_cmd(int type)
128 {
129         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
130         gboolean connected;
131         bluetooth_device_address_t device_address;
132         oal_status_t status = OAL_STATUS_SUCCESS;
133         int result = BLUETOOTH_ERROR_NONE;
134         BT_INFO("+");
135
136         connected = _bt_is_headset_type_connected(BT_AVRCP, connected_address);
137
138         if (!connected) {
139                 BT_ERR("Device is not connected:");
140                 return BLUETOOTH_ERROR_NOT_CONNECTED;
141         }
142
143         _bt_convert_addr_string_to_type(device_address.addr,
144                         connected_address);
145         switch (type) {
146         case RC_PASS_CMD_PLAY:
147                 status = avrcp_ct_play((bt_address_t*)&device_address);
148                 break;
149         case RC_PASS_CMD_PAUSE:
150                 status = avrcp_ct_pause((bt_address_t*)&device_address);
151                 break;
152         case RC_PASS_CMD_STOP:
153                 status = avrcp_ct_stop((bt_address_t*)&device_address);
154                 break;
155         case RC_PASS_CMD_NEXT:
156                 status = avrcp_ct_next_track((bt_address_t*)&device_address);
157                 break;
158         case RC_PASS_CMD_PREVIOUS:
159                 status = avrcp_ct_prev_track((bt_address_t*)&device_address);
160                 break;
161         case RC_PASS_CMD_PRESS_FAST_FORWARD:
162                 status = avrcp_ct_fforward((bt_address_t*)&device_address, PRESS_STATE);
163                 break;
164         case RC_PASS_CMD_RELEASE_FAST_FORWARD:
165                 status = avrcp_ct_fforward((bt_address_t*)&device_address, RELEASE_STATE);
166                 break;
167         case RC_PASS_CMD_PRESS_REWIND:
168                 status = avrcp_ct_rewind((bt_address_t*)&device_address, PRESS_STATE);
169                 break;
170         case RC_PASS_CMD_RELEASE_REWIND:
171                 status = avrcp_ct_rewind((bt_address_t*)&device_address, RELEASE_STATE);
172                 break;
173         case RC_PASS_CMD_VOLUME_UP:
174                 status = avrcp_ct_volume_up((bt_address_t*)&device_address);
175                 break;
176         case RC_PASS_CMD_VOLUME_DOWN:
177                 status = avrcp_ct_volume_down((bt_address_t*)&device_address);
178                 break;
179         default:
180                 break;
181         }
182
183         if (status != OAL_STATUS_SUCCESS) {
184                 BT_ERR("Send pass through command err: [%d]", status);
185                 result = _bt_convert_oal_status_to_bt_error(status);
186         }
187
188         return result;
189 }
190
191 int _bt_avrcp_control_cmd_to_dest(int type, bluetooth_device_address_t *device_address)
192 {
193         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
194         gboolean connected;
195         oal_status_t status = OAL_STATUS_SUCCESS;
196         int result = BLUETOOTH_ERROR_NONE;
197         BT_INFO("+");
198
199         _bt_convert_addr_type_to_string(connected_address, device_address->addr);
200
201         connected = _bt_is_headset_address_type_connected(BT_AVRCP,
202                                                                 (const char *)connected_address);
203
204         if (!connected) {
205                 BT_ERR("Device is not connected:");
206                 return BLUETOOTH_ERROR_NOT_CONNECTED;
207         }
208
209         switch (type) {
210         case RC_PASS_CMD_PLAY:
211                 status = avrcp_ct_play((bt_address_t*)device_address);
212                 break;
213         case RC_PASS_CMD_PAUSE:
214                 status = avrcp_ct_pause((bt_address_t*)device_address);
215                 break;
216         case RC_PASS_CMD_STOP:
217                 status = avrcp_ct_stop((bt_address_t*)device_address);
218                 break;
219         case RC_PASS_CMD_NEXT:
220                 status = avrcp_ct_next_track((bt_address_t*)device_address);
221                 break;
222         case RC_PASS_CMD_PREVIOUS:
223                 status = avrcp_ct_prev_track((bt_address_t*)device_address);
224                 break;
225         case RC_PASS_CMD_PRESS_FAST_FORWARD:
226                 status = avrcp_ct_fforward((bt_address_t*)device_address, PRESS_STATE);
227                 break;
228         case RC_PASS_CMD_RELEASE_FAST_FORWARD:
229                 status = avrcp_ct_fforward((bt_address_t*)device_address, RELEASE_STATE);
230                 break;
231         case RC_PASS_CMD_PRESS_REWIND:
232                 status = avrcp_ct_rewind((bt_address_t*)device_address, PRESS_STATE);
233                 break;
234         case RC_PASS_CMD_RELEASE_REWIND:
235                 status = avrcp_ct_rewind((bt_address_t*)device_address, RELEASE_STATE);
236                 break;
237         case RC_PASS_CMD_VOLUME_UP:
238                 status = avrcp_ct_volume_up((bt_address_t*)device_address);
239                 break;
240         case RC_PASS_CMD_VOLUME_DOWN:
241                 status = avrcp_ct_volume_down((bt_address_t*)device_address);
242                 break;
243         default:
244                 break;
245         }
246
247         if (status != OAL_STATUS_SUCCESS) {
248                 BT_ERR("Send pass through command err: [%d]", status);
249                 result = _bt_convert_oal_status_to_bt_error(status);
250         }
251
252         return result;
253 }
254
255 int _bt_avrcp_control_set_property(int type, unsigned int value)
256 {
257         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
258         gboolean connected;
259         bluetooth_device_address_t device_address;
260         oal_status_t status = OAL_STATUS_SUCCESS;
261         int result = BLUETOOTH_ERROR_NONE;
262         BT_INFO("+");
263         connected = _bt_is_headset_type_connected(BT_AVRCP, connected_address);
264
265         if (connected) {
266                 int oal_type;
267
268                 oal_type = __media_prop_to_oal_type(type);
269                 retv_if(0 > oal_type, BLUETOOTH_ERROR_INVALID_PARAM);
270
271                 _bt_convert_addr_string_to_type(device_address.addr,
272                                 connected_address);
273                 status = avrcp_ct_set_property((bt_address_t*)&device_address,
274                                 oal_type, value);
275                 if (status != OAL_STATUS_SUCCESS) {
276                         BT_ERR("Set peoperty err: [%d]", status);
277                         result = _bt_convert_oal_status_to_bt_error(status);
278                 }
279         } else {
280                 BT_ERR("Device is not connected:");
281                 return BLUETOOTH_ERROR_NOT_CONNECTED;
282         }
283         return result;
284 }
285
286 int _bt_avrcp_transport_set_property(int type, unsigned int value)
287 {
288         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
289         gboolean connected;
290         bluetooth_device_address_t device_address;
291         oal_status_t status = OAL_STATUS_SUCCESS;
292         int result = BLUETOOTH_ERROR_NONE;
293         BT_INFO("+");
294         connected = _bt_is_headset_type_connected(BT_AVRCP, connected_address);
295
296         if (connected) {
297                 _bt_convert_addr_string_to_type(device_address.addr,
298                                 connected_address);
299                 status = avrcp_transport_set_property((bt_address_t*)&device_address,
300                                 type, value);
301                 if (status != OAL_STATUS_SUCCESS) {
302                         BT_ERR("Set peoperty err: [%d]", status);
303                         result = _bt_convert_oal_status_to_bt_error(status);
304                 }
305         } else {
306                 BT_ERR("Device is not connected:");
307                 return BLUETOOTH_ERROR_NOT_CONNECTED;
308         }
309         return result;
310 }
311
312 int _bt_avrcp_control_get_property(int type)
313 {
314         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
315         gboolean connected;
316         bt_address_t device_address;
317         oal_status_t status = OAL_STATUS_SUCCESS;
318         int result = BLUETOOTH_ERROR_NONE;
319         BT_INFO("+");
320         connected = _bt_is_headset_type_connected(BT_AVRCP, connected_address);
321
322         if (connected) {
323                 avrcp_ct_player_property_type_t oal_type;
324
325                 _bt_convert_addr_string_to_type(
326                                 device_address.addr,
327                                 connected_address);
328                 if (type == STATUS) {
329                         status = avrcp_ct_get_play_status(&device_address);
330                         if (status != OAL_STATUS_SUCCESS) {
331                                 BT_ERR("Get play status err: [%d]", status);
332                                 result = _bt_convert_oal_status_to_bt_error(status);
333                         }
334                 } else {
335                         oal_type = __media_prop_to_oal_type(type);
336                         retv_if(0 > oal_type, BLUETOOTH_ERROR_INVALID_PARAM);
337
338                         status = avrcp_ct_get_property(&device_address, oal_type);
339                         if (status != OAL_STATUS_SUCCESS) {
340                                 BT_ERR("Get peoperty err: [%d]", status);
341                                 result = _bt_convert_oal_status_to_bt_error(status);
342                         }
343                 }
344         } else {
345                 BT_ERR("Device is not connected:");
346                 return BLUETOOTH_ERROR_NOT_CONNECTED;
347         }
348         return result;
349 }
350
351 int _bt_avrcp_control_get_track_info(void)
352 {
353         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
354         gboolean connected;
355         bluetooth_device_address_t device_address;
356         oal_status_t status = OAL_STATUS_SUCCESS;
357         int result = BLUETOOTH_ERROR_NONE;
358         BT_INFO("+");
359         connected = _bt_is_headset_type_connected(BT_AVRCP, connected_address);
360
361         if (connected) {
362                 _bt_convert_addr_string_to_type(
363                                 device_address.addr,
364                                 connected_address);
365
366                 status = avrcp_ct_get_media_attribute((bt_address_t*)&device_address);
367                 if (status != OAL_STATUS_SUCCESS) {
368                         BT_ERR("Get track info err: [%d]", status);
369                         result = _bt_convert_oal_status_to_bt_error(status);
370                 }
371         } else {
372                 BT_ERR("Device is not connected:");
373                 return BLUETOOTH_ERROR_NOT_CONNECTED;
374         }
375         return result;
376 }
377
378 static void __bt_reply_avrcp_ct_connection_pending_request(bluetooth_device_address_t *address)
379 {
380         BT_DBG("+");
381         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
382         int result = BLUETOOTH_ERROR_NONE;
383         bluetooth_device_address_t device_address;
384         GArray *out_param;
385         invocation_info_t *req_info;
386         memcpy(device_address.addr, address->addr, BLUETOOTH_ADDRESS_LENGTH);
387         _bt_convert_addr_type_to_string(addr, address->addr);
388
389         req_info = _bt_get_request_info_data(BT_AVRCP_CONTROL_CONNECT, addr);
390         if (NULL == req_info) {
391                 BT_INFO("AVRCP CT Connect request not found or possibly already replied");
392                 return;
393         } else {
394                 BT_INFO("AVRCP CT Connect request found for [%s]", addr);
395         }
396
397         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
398         g_array_append_vals(out_param, addr, BT_ADDRESS_STRING_SIZE);
399         _bt_service_method_return(req_info->context,
400                         out_param, result);
401         g_array_free(out_param, TRUE);
402         _bt_free_info_from_invocation_list(req_info);
403 }
404
405 static void __bt_handle_avrcp_target_connected_state(bluetooth_device_address_t *address)
406 {
407         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
408         GVariant *param;
409         int result = BLUETOOTH_ERROR_NONE;
410         ret_if(NULL == address);
411         BT_INFO("+");
412
413         _bt_convert_addr_type_to_string(addr, address->addr);
414         BT_INFO("Address of connected device [%s]", addr);
415
416         /* Add data from the connected list */
417         _bt_add_headset_to_list(BT_AVRCP, BT_STATE_CONNECTED, addr);
418
419         /* Replay to avrcp cotroller connect */
420         __bt_reply_avrcp_ct_connection_pending_request(address);
421
422         /* Send AVRCP(TARGET Role) connected event to Application */
423         param = g_variant_new("(is)", result, addr);
424         _bt_send_event(BT_AVRCP_CONTROL_EVENT, BLUETOOTH_EVENT_AVRCP_CONNECTED, param);
425
426         BT_INFO("-");
427 }
428
429 static void __bt_reply_avrcp_ct_disconnection_pending_request(bluetooth_device_address_t *address)
430 {
431         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
432         int result = BLUETOOTH_ERROR_NONE;
433         GArray *out_param;
434         invocation_info_t *req_info;
435
436         BT_DBG("+");
437         _bt_convert_addr_type_to_string(addr, address->addr);
438
439         req_info = _bt_get_request_info_data(BT_AVRCP_CONTROL_DISCONNECT, addr);
440         if (NULL == req_info) {
441                 BT_INFO("AVRCP CT Disconnect request not found or possibly already replied");
442                 return;
443         } else {
444                 BT_INFO("AVRCP CT Disconnect request found for [%s]", addr);
445         }
446
447         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
448         g_array_append_vals(out_param, addr, BT_ADDRESS_STRING_SIZE);
449         _bt_service_method_return(req_info->context,
450                         out_param, result);
451         g_array_free(out_param, TRUE);
452         _bt_free_info_from_invocation_list(req_info);
453 }
454
455 static void __bt_avrcp_ct_reply_pending_requests(void)
456 {
457         BT_INFO("+");
458         int result = BLUETOOTH_ERROR_INTERNAL;
459         GArray *out_param;
460         invocation_info_t *req_info;
461         GSList *l;
462
463         for (l = _bt_get_invocation_list(); l != NULL;) {
464                 req_info = l->data;
465                 l = g_slist_next(l);
466
467                 if (req_info == NULL)
468                         continue;
469
470                 /* Create out param */
471                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
472
473                 BT_INFO("service_function: [0x%X]", req_info->service_function);
474                 switch (req_info->service_function) {
475                 case BT_AVRCP_GET_TRACK_INFO:
476                 case BT_AVRCP_CONTROL_GET_PROPERTY: {
477                         _bt_service_method_return(req_info->context, out_param, result);
478                         _bt_free_info_from_invocation_list(req_info);
479                         g_array_free(out_param, TRUE);
480                         break;
481                 }
482                 default:
483                         break;
484                 }
485         }
486         BT_INFO("-");
487 }
488
489 static void __bt_handle_avrcp_target_disconnected_state(bluetooth_device_address_t *address)
490 {
491         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
492         GVariant *param;
493         int result = BLUETOOTH_ERROR_NONE;
494         ret_if(NULL == address);
495         BT_INFO("+");
496
497         _bt_convert_addr_type_to_string(addr, address->addr);
498         BT_INFO("Address of disconnected device [%s]", addr);
499
500         /* Remove data from the connected list */
501         _bt_remove_headset_from_list(BT_AVRCP, addr);
502
503         /* Reply to avrcp cotroller connect */
504         __bt_reply_avrcp_ct_disconnection_pending_request(address);
505
506         /* Reply AVRCP CT pending requests */
507         __bt_avrcp_ct_reply_pending_requests();
508
509         /* Send AVRCP(TARGET Role) disconnected event to Application */
510         param = g_variant_new("(is)", result, addr);
511         _bt_send_event(BT_AVRCP_CONTROL_EVENT, BLUETOOTH_EVENT_AVRCP_DISCONNECTED, param);
512
513         BT_INFO("-");
514 }
515
516 static int __bt_oal_to_bt_event(int oal_event)
517 {
518         int ret = 0;
519
520         switch (oal_event) {
521         case OAL_EVENT_AVRCP_CT_EQUALIZER_STATUS:
522                 ret = BLUETOOTH_EVENT_AVRCP_CONTROL_EQUALIZER_STATUS;
523                 break;
524         case OAL_EVENT_AVRCP_CT_REPEAT_STATUS:
525                 ret = BLUETOOTH_EVENT_AVRCP_CONTROL_REPEAT_STATUS;
526                 break;
527         case OAL_EVENT_AVRCP_CT_SHUFFLE_STATUS:
528                 ret = BLUETOOTH_EVENT_AVRCP_CONTROL_SHUFFLE_STATUS;
529                 break;
530         case OAL_EVENT_AVRCP_CT_SCAN_STATUS:
531                 ret = BLUETOOTH_EVENT_AVRCP_CONTROL_SCAN_STATUS;
532                 break;
533         case OAL_EVENT_AVRCP_CT_NOTIF_PLAY_POS_CHANGED:
534                 ret = BLUETOOTH_EVENT_AVRCP_SONG_POSITION_STATUS;
535                 break;
536         case OAL_EVENT_AVRCP_CT_NOTIF_PLAY_STATUS_CHANGED:
537                 ret = BLUETOOTH_EVENT_AVRCP_PLAY_STATUS_CHANGED;
538                 break;
539         default:
540                 break;
541         }
542         return ret;
543 }
544
545 static void __bt_handle_avrcp_target_player_property(unsigned int property_value, int oal_event)
546 {
547         GVariant *param;
548         BT_INFO("+");
549         /* Send AVRCP Target player property event to Application */
550         param = g_variant_new("(u)", property_value);
551         _bt_send_event(BT_AVRCP_CONTROL_EVENT,
552                         __bt_oal_to_bt_event(oal_event), param);
553 }
554
555 static void __bt_handle_avrcp_track_info_changed(event_avrcp_ct_media_info_t* metadata)
556 {
557         GVariant *param;
558         unsigned int total_track = 0;
559         unsigned int track_number = 0;
560         unsigned int playing_time = 0;
561         GVariant *title;
562         GVariant *artist;
563         GVariant *album;
564         GVariant *genre;
565
566         BT_INFO("+");
567
568         total_track = (unsigned int)(metadata->total_track);
569         track_number = (unsigned int)(metadata->track_number);
570         playing_time = (unsigned int)(metadata->playing_time);
571
572         title = g_variant_new_from_data((const GVariantType *)"ay",
573                         metadata->title_info, MEDIA_ATTIRBUTE_STRING_LENGTH,
574                         TRUE, NULL, NULL);
575         artist = g_variant_new_from_data((const GVariantType *)"ay",
576                         metadata->artist_info, MEDIA_ATTIRBUTE_STRING_LENGTH,
577                         TRUE, NULL, NULL);
578         album = g_variant_new_from_data((const GVariantType *)"ay",
579                         metadata->album_info, MEDIA_ATTIRBUTE_STRING_LENGTH,
580                         TRUE, NULL, NULL);
581         genre = g_variant_new_from_data((const GVariantType *)"ay",
582                         metadata->genre_info, MEDIA_ATTIRBUTE_STRING_LENGTH,
583                         TRUE, NULL, NULL);
584
585         /* Send AVRCP Target player track info changed event to application*/
586         param = g_variant_new("(@ay@ay@ay@ayuuu)",
587                         title, artist, album, genre,
588                         total_track, track_number, playing_time);
589
590         BT_INFO("Total_track: %u, track_number: %u, playing_time: %u",
591                         total_track, track_number, playing_time);
592         _bt_send_event(BT_AVRCP_CONTROL_EVENT,
593                         BLUETOOTH_EVENT_AVRCP_TRACK_CHANGED, param);
594 }
595
596 static invocation_info_t* __bt_get_request_info(int service_function)
597 {
598         GSList *l;
599         invocation_info_t *req_info = NULL;
600
601         BT_DBG("+");
602         /* Get method invocation context */
603         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
604                 req_info = l->data;
605                 if (req_info == NULL || req_info->service_function != service_function)
606                         continue;
607                 return req_info;
608         }
609         return NULL;
610 }
611
612 static void __bt_handle_avrcp_track_info(event_avrcp_ct_media_info_t* metadata)
613 {
614         media_metadata_t meta_data;
615         invocation_info_t *req_info = NULL;
616         GArray *out_param = NULL;
617         int result = BLUETOOTH_ERROR_NONE;
618
619         req_info = __bt_get_request_info(BT_AVRCP_GET_TRACK_INFO);
620         ret_if(NULL == req_info);
621
622         memset(&meta_data, 0x00, sizeof(media_metadata_t));
623
624         memcpy(&meta_data.title, &metadata->title_info,
625                         strlen((char *)metadata->title_info));
626         meta_data.title[strlen((char *)metadata->title_info)] = '\0';
627
628         memcpy(&meta_data.artist, &metadata->artist_info,
629                         strlen((char *)metadata->artist_info));
630         meta_data.artist[strlen((char *)metadata->artist_info)] = '\0';
631
632         memcpy(&meta_data.album, &metadata->album_info,
633                         strlen((char *)metadata->album_info));
634         meta_data.album[strlen((char *)metadata->album_info)] = '\0';
635
636         memcpy(&meta_data.genre, &metadata->genre_info,
637                         strlen((char *)metadata->genre_info));
638         meta_data.genre[strlen((char *)metadata->genre_info)] = '\0';
639
640         meta_data.total_tracks = metadata->total_track;
641         meta_data.number = metadata->track_number;
642         meta_data.duration = metadata->playing_time;
643
644         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
645         g_array_append_vals(out_param, &meta_data, sizeof(media_metadata_t));
646
647         _bt_service_method_return(req_info->context, out_param, result);
648         g_array_free(out_param, TRUE);
649         _bt_free_info_from_invocation_list(req_info);
650 }
651
652 static void __bt_handle_avrcp_pass_cmd_res(avrcp_ct_pass_cmd_t *pass_cmd)
653 {
654         BT_INFO(" Send Command Response [%d]", pass_cmd->key_code);
655 }
656
657 static void __bt_handle_avrcp_player_setting_res(avrcp_ct_playersetting_t *player_setting_res)
658 {
659         BT_INFO("Set Property Response [%d]", player_setting_res->accepted);
660 }
661
662 static void __bt_handle_avrcp_get_property_res_event(avrcp_ct_player_property_type_t oal_type, int value)
663 {
664         invocation_info_t *req_info = NULL;
665         GArray *out_param = NULL;
666         int result = BLUETOOTH_ERROR_NONE;
667         int prop_type;
668         int type;
669         GSList *l;
670
671         BT_DBG("+");
672
673         prop_type = __oal_type_to_media_prop(oal_type);
674         ret_if(0 > prop_type);
675
676         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
677         g_array_append_vals(out_param, &value, sizeof(value));
678
679         /* Find and reply to all get requests for prop_type */
680         for (l = _bt_get_invocation_list(); l != NULL;) {
681                 req_info = l->data;
682                 l = g_slist_next(l);
683
684                 if (req_info == NULL)
685                         continue;
686
687                 type = *((int *)req_info->user_data);
688                 if (req_info->service_function != BT_AVRCP_CONTROL_GET_PROPERTY
689                                 || type != prop_type)
690                         continue;
691
692                 BT_INFO("Request found");
693                 _bt_service_method_return(req_info->context, out_param, result);
694                 _bt_free_info_from_invocation_list(req_info);
695         }
696
697         g_array_free(out_param, TRUE);
698         BT_DBG("-");
699 }
700
701 void _bt_avrcp_ctrl_event_handler(int oal_event, gpointer event_data)
702 {
703         BT_INFO("+");
704         bluetooth_device_address_t* bd_addr;
705
706         switch (oal_event) {
707         case OAL_EVENT_AVRCP_CT_CONNECTED: {
708                 BT_INFO("AVRCP Controller Profile connected..");
709                 bd_addr = (bluetooth_device_address_t*)event_data;
710                 __bt_handle_avrcp_target_connected_state(bd_addr);
711                 BT_PERMANENT_LOG("Connected AVRCP ct");
712                 break;
713         }
714         case OAL_EVENT_AVRCP_CT_DISCONNECTED: {
715                 BT_INFO("AVRCP Controller Profile dissconnected..");
716                 bd_addr = (bluetooth_device_address_t*)event_data;
717                 __bt_handle_avrcp_target_disconnected_state(bd_addr);
718                 BT_PERMANENT_LOG("Disconnected AVRCP ct");
719                 break;
720         }
721         case OAL_EVENT_AVRCP_CT_EQUALIZER_STATUS:
722         case OAL_EVENT_AVRCP_CT_REPEAT_STATUS:
723         case OAL_EVENT_AVRCP_CT_SHUFFLE_STATUS:
724         case OAL_EVENT_AVRCP_CT_SCAN_STATUS: {
725                 avrcp_ct_property_value_t* property_val;
726                 property_val = (avrcp_ct_property_value_t*)event_data;
727                 __bt_handle_avrcp_target_player_property(property_val->value, oal_event);
728                 break;
729         }
730         case OAL_EVENT_AVRCP_CT_NOTIF_PLAY_STATUS_CHANGED:{
731                 event_notif_avrcp_ct_notif_info_t* play_status_val;
732                 play_status_val = (event_notif_avrcp_ct_notif_info_t*)event_data;
733                 __bt_handle_avrcp_target_player_property(play_status_val->play_status, oal_event);
734                 break;
735         }
736         case OAL_EVENT_AVRCP_CT_NOTIF_PLAY_POS_CHANGED: {
737                 event_notif_avrcp_ct_notif_info_t* play_position;
738                 play_position = (event_notif_avrcp_ct_notif_info_t*)event_data;
739                 __bt_handle_avrcp_target_player_property(play_position->song_pos, oal_event);
740                 break;
741         }
742         case OAL_EVENT_AVRCP_CT_NOTIF_TRACK_CHANGE: {
743                 BT_INFO("AVRCP Controller Track Changed event..");
744                 event_avrcp_ct_media_info_t* metadata = event_data;
745
746                 __bt_handle_avrcp_track_info_changed(metadata);
747                 break;
748         }
749         case OAL_EVENT_AVRCP_CT_MEDIA_INFO: {
750                 BT_INFO("AVRCP Controller Track Info event..");
751                 event_avrcp_ct_media_info_t* metadata;
752
753                 metadata = (event_avrcp_ct_media_info_t*)event_data;
754                 __bt_handle_avrcp_track_info(metadata);
755                 break;
756         }
757         case OAL_EVENT_AVRCP_CT_GET_PROPERTY_RES: {
758                 BT_INFO("AVRCP Controller Get Property response event..");
759                 avrcp_ct_property_value_t *property = (avrcp_ct_property_value_t *)event_data;
760                 __bt_handle_avrcp_get_property_res_event(property->type, property->value);
761                 break;
762         }
763         case OAL_EVENT_AVRCP_CT_PLAY_STATUS: {
764                 BT_INFO("AVRCP Controller Get Play status response event..");
765                 event_avrcp_ct_play_status_t *play_status_info = (event_avrcp_ct_play_status_t *)event_data;
766                 __bt_handle_avrcp_get_property_res_event(OAL_PLAY_STATUS, play_status_info->play_status);
767                 break;
768         }
769         case OAL_EVENT_AVRCP_CT_PASS_CMD_RES: {
770                 BT_INFO("AVRCP Controller Pass Command Res");
771                 avrcp_ct_pass_cmd_t *pass_cmd;
772
773                 pass_cmd = (avrcp_ct_pass_cmd_t *)event_data;
774                 __bt_handle_avrcp_pass_cmd_res(pass_cmd);
775                 break;
776         }
777         case OAL_EVENT_AVRCP_CT_PLAYER_SETTING_RES: {
778                 BT_INFO("AVRCP Player setting response");
779                 avrcp_ct_playersetting_t *player_setting_res;
780
781                 player_setting_res = (avrcp_ct_playersetting_t *)event_data;
782                 __bt_handle_avrcp_player_setting_res(player_setting_res);
783                 break;
784         }
785         default:
786                 BT_INFO("Invalid Event = %d", oal_event);
787                 break;
788         }
789
790         BT_INFO("-");
791 }