1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This interface file contains the interface to the Audio Video Control
22  *  Transport Protocol (AVCTP).
23  *
24  ******************************************************************************/
25 #ifndef AVCT_API_H
26 #define AVCT_API_H
27 
28 #include <cstdint>
29 #include <string>
30 
31 #include "include/macros.h"
32 #include "stack/include/bt_hdr.h"
33 #include "types/raw_address.h"
34 
35 /*****************************************************************************
36  *  Constants
37  ****************************************************************************/
38 
39 /* API function return value result codes. */
40 #define AVCT_SUCCESS 0      /* Function successful */
41 #define AVCT_NO_RESOURCES 1 /* Not enough resources */
42 #define AVCT_BAD_HANDLE 2   /* Bad handle */
43 #define AVCT_PID_IN_USE 3   /* PID already in use */
44 #define AVCT_NOT_OPEN 4     /* Connection not open */
45 
46 /* Protocol revision numbers */
47 #define AVCT_REV_1_0 0x0100
48 #define AVCT_REV_1_2 0x0102
49 #define AVCT_REV_1_3 0x0103
50 #define AVCT_REV_1_4 0x0104
51 
52 /* the layer_specific settings */
53 #define AVCT_DATA_CTRL 0x0001    /* for the control channel */
54 #define AVCT_DATA_BROWSE 0x0002  /* for the browsing channel */
55 #define AVCT_DATA_PARTIAL 0x0100 /* Only have room for a partial message */
56 
57 /* Per the AVRC spec, minimum MTU for the control channel */
58 #define AVCT_MIN_CONTROL_MTU 48
59 /* Per the AVRC spec, minimum MTU for the browsing channel */
60 #define AVCT_MIN_BROWSE_MTU 335
61 
62 /* Message offset.  The number of bytes needed by the protocol stack for the
63  * protocol headers of an AVCTP message packet.
64  */
65 #define AVCT_MSG_OFFSET 15
66 #define AVCT_BROWSE_OFFSET 17 /* the default offset for browsing channel */
67 
68 /* Connection role. */
69 typedef enum {
70   AVCT_ROLE_INITIATOR = 0, /* Initiator connection */
71   AVCT_ROLE_ACCEPTOR = 1,  /* Acceptor connection */
72 } tAVCT_ROLE;
73 
avct_role_text(const tAVCT_ROLE & role)74 inline std::string avct_role_text(const tAVCT_ROLE& role) {
75   switch (role) {
76     CASE_RETURN_TEXT(AVCT_ROLE_INITIATOR);
77     CASE_RETURN_TEXT(AVCT_ROLE_ACCEPTOR);
78   }
79   RETURN_UNKNOWN_TYPE_STRING(tAVCT_ROLE, role);
80 }
81 
82 /* Control role. */
83 #define AVCT_TARGET 1  /* target  */
84 #define AVCT_CONTROL 2 /* controller  */
85 #define AVCT_PASSIVE 4 /* If conflict, allow the other side to succeed  */
86 
87 /* Command/Response indicator. */
88 #define AVCT_CMD 0 /* Command message */
89 #define AVCT_RSP 2 /* Response message */
90 #define AVCT_REJ 3 /* Message rejected */
91 
92 /* Control callback events. */
93 #define AVCT_CONNECT_CFM_EVT 0        /* Connection confirm */
94 #define AVCT_CONNECT_IND_EVT 1        /* Connection indication */
95 #define AVCT_DISCONNECT_CFM_EVT 2     /* Disconnect confirm */
96 #define AVCT_DISCONNECT_IND_EVT 3     /* Disconnect indication */
97 #define AVCT_CONG_IND_EVT 4           /* Congestion indication */
98 #define AVCT_UNCONG_IND_EVT 5         /* Uncongestion indication */
99 #define AVCT_BROWSE_CONN_CFM_EVT 6    /* Browse Connection confirm */
100 #define AVCT_BROWSE_CONN_IND_EVT 7    /* Browse Connection indication */
101 #define AVCT_BROWSE_DISCONN_CFM_EVT 8 /* Browse Disconnect confirm */
102 #define AVCT_BROWSE_DISCONN_IND_EVT 9 /* Browse Disconnect indication */
103 #define AVCT_BROWSE_CONG_IND_EVT 10   /* Congestion indication */
104 #define AVCT_BROWSE_UNCONG_IND_EVT 11 /* Uncongestion indication */
105 
106 /* General purpose failure result code for callback events. */
107 #define AVCT_RESULT_FAIL 5
108 
109 /*****************************************************************************
110  *  Type Definitions
111  ****************************************************************************/
112 
113 /* Control callback function. */
114 typedef void(tAVCT_CTRL_CBACK)(uint8_t handle, uint8_t event, uint16_t result,
115                                const RawAddress* peer_addr);
116 
117 /* Message callback function */
118 /* p_pkt->layer_specific is AVCT_DATA_CTRL or AVCT_DATA_BROWSE */
119 typedef void(tAVCT_MSG_CBACK)(uint8_t handle, uint8_t label, uint8_t cr, BT_HDR* p_pkt);
120 
121 /* Structure used by AVCT_CreateConn. */
122 typedef struct {
123   tAVCT_CTRL_CBACK* p_ctrl_cback; /* Control callback */
124   tAVCT_MSG_CBACK* p_msg_cback;   /* Message callback */
125   uint16_t pid;                   /* Profile ID */
126   tAVCT_ROLE role;                /* Initiator/acceptor role */
127   uint8_t control;                /* Control role (Control/Target) */
128 } tAVCT_CC;
129 
130 /*****************************************************************************
131  *  External Function Declarations
132  ****************************************************************************/
133 
134 /*******************************************************************************
135  *
136  * Function         AVCT_Register
137  *
138  * Description      This is the system level registration function for the
139  *                  AVCTP protocol.  This function initializes AVCTP and
140  *                  prepares the protocol stack for its use.  This function
141  *                  must be called once by the system or platform using AVCTP
142  *                  before the other functions of the API an be used.
143  *
144  *
145  * Returns          void
146  *
147  ******************************************************************************/
148 void AVCT_Register();
149 
150 /*******************************************************************************
151  *
152  * Function         AVCT_Deregister
153  *
154  * Description      This function is called to deregister use AVCTP protocol.
155  *                  It is called when AVCTP is no longer being used by any
156  *                  application in the system.  Before this function can be
157  *                  called, all connections must be removed with
158  *                  AVCT_RemoveConn().
159  *
160  *
161  * Returns          void
162  *
163  ******************************************************************************/
164 void AVCT_Deregister(void);
165 
166 /*******************************************************************************
167  *
168  * Function         AVCT_CreateConn
169  *
170  * Description      Create an AVCTP connection.  There are two types of
171  *                  connections, initiator and acceptor, as determined by
172  *                  the p_cc->role parameter.  When this function is called to
173  *                  create an initiator connection, an AVCTP connection to
174  *                  the peer device is initiated if one does not already exist.
175  *                  If an acceptor connection is created, the connection waits
176  *                  passively for an incoming AVCTP connection from a peer
177  *                  device.
178  *
179  *
180  * Returns          AVCT_SUCCESS if successful, otherwise error.
181  *
182  ******************************************************************************/
183 uint16_t AVCT_CreateConn(uint8_t* p_handle, tAVCT_CC* p_cc, const RawAddress& peer_addr);
184 
185 /*******************************************************************************
186  *
187  * Function         AVCT_RemoveConn
188  *
189  * Description      Remove an AVCTP connection.  This function is called when
190  *                  the application is no longer using a connection.  If this
191  *                  is the last connection to a peer the L2CAP channel for AVCTP
192  *                  will be closed.
193  *
194  *
195  * Returns          AVCT_SUCCESS if successful, otherwise error.
196  *
197  ******************************************************************************/
198 uint16_t AVCT_RemoveConn(uint8_t handle);
199 
200 /*******************************************************************************
201  *
202  * Function         AVCT_CreateBrowse
203  *
204  * Description      Create an AVCTP connection.  There are two types of
205  *                  connections, initiator and acceptor, as determined by
206  *                  the p_cc->role parameter.  When this function is called to
207  *                  create an initiator connection, an AVCTP connection to
208  *                  the peer device is initiated if one does not already exist.
209  *                  If an acceptor connection is created, the connection waits
210  *                  passively for an incoming AVCTP connection from a peer
211  *                  device.
212  *
213  *
214  * Returns          AVCT_SUCCESS if successful, otherwise error.
215  *
216  ******************************************************************************/
217 uint16_t AVCT_CreateBrowse(uint8_t handle, tAVCT_ROLE role);
218 
219 /*******************************************************************************
220  *
221  * Function         AVCT_RemoveBrowse
222  *
223  * Description      Remove an AVCTP connection.  This function is called when
224  *                  the application is no longer using a connection.  If this
225  *                  is the last connection to a peer the L2CAP channel for AVCTP
226  *                  will be closed.
227  *
228  *
229  * Returns          AVCT_SUCCESS if successful, otherwise error.
230  *
231  ******************************************************************************/
232 uint16_t AVCT_RemoveBrowse(uint8_t handle);
233 
234 /*******************************************************************************
235  *
236  * Function         AVCT_GetBrowseMtu
237  *
238  * Description      Get the peer_mtu for the AVCTP Browse channel of the given
239  *                  connection.
240  *
241  * Returns          the peer browsing channel MTU.
242  *
243  ******************************************************************************/
244 uint16_t AVCT_GetBrowseMtu(uint8_t handle);
245 
246 /*******************************************************************************
247  *
248  * Function         AVCT_GetPeerMtu
249  *
250  * Description      Get the peer_mtu for the AVCTP channel of the given
251  *                  connection.
252  *
253  * Returns          the peer MTU size.
254  *
255  ******************************************************************************/
256 uint16_t AVCT_GetPeerMtu(uint8_t handle);
257 
258 /*******************************************************************************
259  *
260  * Function         AVCT_MsgReq
261  *
262  * Description      Send an AVCTP message to a peer device.  In calling
263  *                  AVCT_MsgReq(), the application should keep track of the
264  *                  congestion state of AVCTP as communicated with events
265  *                  AVCT_CONG_IND_EVT and AVCT_UNCONG_IND_EVT.   If the
266  *                  application calls AVCT_MsgReq() when AVCTP is congested
267  *                  the message may be discarded.  The application may make its
268  *                  first call to AVCT_MsgReq() after it receives an
269  *                  AVCT_CONNECT_CFM_EVT or AVCT_CONNECT_IND_EVT on control
270  *                  channel or
271  *                  AVCT_BROWSE_CONN_CFM_EVT or AVCT_BROWSE_CONN_IND_EVT on
272  *                  browsing channel.
273  *
274  *                  p_msg->layer_specific must be set to
275  *                  AVCT_DATA_CTRL for control channel traffic;
276  *                  AVCT_DATA_BROWSE for for browse channel traffic.
277  *
278  * Returns          AVCT_SUCCESS if successful, otherwise error.
279  *
280  ******************************************************************************/
281 uint16_t AVCT_MsgReq(uint8_t handle, uint8_t label, uint8_t cr, BT_HDR* p_msg);
282 
283 /*******************************************************************************
284 **
285 ** Function         AVCT_Dumpsys
286 **
287 ** Description      This function provides dumpsys data during the dumpsys
288 **                  procedure.
289 **
290 ** Parameters:      fd: Descriptor used to write the AVCT internals
291 **
292 ** Returns          void
293 **
294 *******************************************************************************/
295 void AVCT_Dumpsys(int fd);
296 
297 #endif /* AVCT_API_H */
298