xref: /btstack/src/classic/avrcp_target.c (revision c823631b5a0ff4a81ba4e8da529d5334f8dd60c7)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avrcp_target.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "classic/avrcp.h"
47 
48 static avrcp_context_t avrcp_target_context;
49 
50 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name){
51     avrcp_create_sdp_record(0, service, service_record_handle, browsing, supported_features, service_name, service_provider_name);
52 }
53 
54 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
55     UNUSED(connection);
56     UNUSED(packet);
57     UNUSED(size);
58 }
59 
60 static void avrcp_target_handle_can_send_now(avrcp_connection_t * connection){
61     switch (connection->state){
62         default:
63             return;
64     }
65 }
66 
67 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
68     avrcp_connection_t * connection;
69     switch (packet_type) {
70         case L2CAP_DATA_PACKET:
71             connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
72             if (!connection) break;
73             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
74             break;
75         case HCI_EVENT_PACKET:
76             switch (hci_event_packet_get_type(packet)){
77                 case L2CAP_EVENT_CAN_SEND_NOW:
78                     connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
79                     if (!connection) break;
80                     avrcp_target_handle_can_send_now(connection);
81                     break;
82             default:
83                 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
84                 break;
85         }
86         default:
87             break;
88     }
89 }
90 
91 void avrcp_target_init(void){
92     avrcp_target_context.role = AVRCP_TARGET;
93     avrcp_target_context.connections = NULL;
94     avrcp_target_context.packet_handler = avrcp_controller_packet_handler;
95     l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
96 }
97 
98 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
99     if (callback == NULL){
100         log_error("avrcp_register_packet_handler called with NULL callback");
101         return;
102     }
103     avrcp_target_context.avrcp_callback = callback;
104 }
105 
106 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
107     return avrcp_connect(bd_addr, &avrcp_target_context, avrcp_cid);
108 }
109 
110 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
111     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
112     if (!connection){
113         log_error("avrcp_get_capabilities: could not find a connection.");
114         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
115     }
116     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
117     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
118     return ERROR_CODE_SUCCESS;
119 }
120 
121