1 /******************************************************************************
2  *
3  *  Copyright 1999-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 file contains internally used SDP definitions
22  *
23  ******************************************************************************/
24 
25 #ifndef SDP_INT_H
26 #define SDP_INT_H
27 
28 #include "bluetooth/uuid.h"
29 #include "bt_target.h"
30 #include "l2c_api.h"
31 #include "osi/include/alarm.h"
32 #include "sdp_api.h"
33 
34 /* Continuation length - we use a 2-byte offset */
35 #define SDP_CONTINUATION_LEN 2
36 #define SDP_MAX_CONTINUATION_LEN 16 /* As per the spec */
37 
38 /* Timeout definitions. */
39 #define SDP_INACT_TIMEOUT_MS (30 * 1000) /* Inactivity timeout (in ms) */
40 
41 /* Define the Protocol Data Unit (PDU) types.
42  */
43 #define SDP_PDU_ERROR_RESPONSE 0x01
44 #define SDP_PDU_SERVICE_SEARCH_REQ 0x02
45 #define SDP_PDU_SERVICE_SEARCH_RSP 0x03
46 #define SDP_PDU_SERVICE_ATTR_REQ 0x04
47 #define SDP_PDU_SERVICE_ATTR_RSP 0x05
48 #define SDP_PDU_SERVICE_SEARCH_ATTR_REQ 0x06
49 #define SDP_PDU_SERVICE_SEARCH_ATTR_RSP 0x07
50 
51 /* Max UUIDs and attributes we support per sequence */
52 #define MAX_UUIDS_PER_SEQ 16
53 #define MAX_ATTR_PER_SEQ 16
54 
55 /* Max length we support for any attribute */
56 #ifdef SDP_MAX_ATTR_LEN
57 #define MAX_ATTR_LEN SDP_MAX_ATTR_LEN
58 #else
59 #define MAX_ATTR_LEN 256
60 #endif
61 
62 /* Internal UUID sequence representation */
63 typedef struct {
64   uint16_t len;
65   uint8_t value[bluetooth::Uuid::kNumBytes128];
66 } tUID_ENT;
67 
68 typedef struct {
69   uint16_t num_uids;
70   tUID_ENT uuid_entry[MAX_UUIDS_PER_SEQ];
71 } tSDP_UUID_SEQ;
72 
73 /* Internal attribute sequence definitions */
74 typedef struct {
75   uint16_t start;
76   uint16_t end;
77 } tATT_ENT;
78 
79 typedef struct {
80   uint16_t num_attr;
81   tATT_ENT attr_entry[MAX_ATTR_PER_SEQ];
82 } tSDP_ATTR_SEQ;
83 
84 /* Define the attribute element of the SDP database record */
85 typedef struct {
86   uint32_t len;       /* Number of bytes in the entry */
87   uint8_t* value_ptr; /* Points to attr_pad */
88   uint16_t id;
89   uint8_t type;
90 } tSDP_ATTRIBUTE;
91 
92 /* An SDP record consists of a handle, and 1 or more attributes */
93 typedef struct {
94   uint32_t record_handle;
95   uint32_t free_pad_ptr;
96   uint16_t num_attributes;
97   tSDP_ATTRIBUTE attribute[SDP_MAX_REC_ATTR];
98   uint8_t attr_pad[SDP_MAX_PAD_LEN];
99 } tSDP_RECORD;
100 
101 /* Define the SDP database */
102 typedef struct {
103   uint32_t
104       di_primary_handle; /* Device ID Primary record or NULL if nonexistent */
105   uint16_t num_records;
106   tSDP_RECORD record[SDP_MAX_RECORDS];
107 } tSDP_DB;
108 
109 /* Continuation information for the SDP server response */
110 typedef struct {
111   uint16_t next_attr_index;    /* attr index for next continuation response */
112   uint16_t next_attr_start_id; /* attr id to start with for the attr index in
113                                   next cont. response */
114   tSDP_RECORD* prev_sdp_rec; /* last sdp record that was completely sent in the
115                                 response */
116   bool last_attr_seq_desc_sent; /* whether attr seq length has been sent
117                                    previously */
118   uint16_t attr_offset; /* offset within the attr to keep trak of partial
119                            attributes in the responses */
120 } tSDP_CONT_INFO;
121 
122 /* Define the SDP Connection Control Block */
123 typedef struct {
124 #define SDP_STATE_IDLE 0
125 #define SDP_STATE_CONN_SETUP 1
126 #define SDP_STATE_CFG_SETUP 2
127 #define SDP_STATE_CONNECTED 3
128   uint8_t con_state;
129 
130 #define SDP_FLAGS_IS_ORIG 0x01
131 #define SDP_FLAGS_HIS_CFG_DONE 0x02
132 #define SDP_FLAGS_MY_CFG_DONE 0x04
133   uint8_t con_flags;
134 
135   RawAddress device_address;
136   alarm_t* sdp_conn_timer;
137   uint16_t rem_mtu_size;
138   uint16_t connection_id;
139   uint16_t list_len; /* length of the response in the GKI buffer */
140   uint8_t* rsp_list; /* pointer to GKI buffer holding response */
141 
142   tSDP_DISCOVERY_DB* p_db; /* Database to save info into   */
143   tSDP_DISC_CMPL_CB* p_cb; /* Callback for discovery done  */
144   tSDP_DISC_CMPL_CB2*
145       p_cb2; /* Callback for discovery done piggy back with the user data */
146   void* user_data; /* piggy back user data */
147   uint32_t
148       handles[SDP_MAX_DISC_SERVER_RECS]; /* Discovered server record handles */
149   uint16_t num_handles;                  /* Number of server handles     */
150   uint16_t cur_handle;                   /* Current handle being processed */
151   uint16_t transaction_id;
152   uint16_t disconnect_reason; /* Disconnect reason            */
153 
154 #define SDP_DISC_WAIT_CONN 0
155 #define SDP_DISC_WAIT_HANDLES 1
156 #define SDP_DISC_WAIT_ATTR 2
157 #define SDP_DISC_WAIT_SEARCH_ATTR 3
158 #define SDP_DISC_WAIT_CANCEL 5
159 
160   uint8_t disc_state;
161   uint8_t is_attr_search;
162 
163   uint16_t cont_offset;     /* Continuation state data in the server response */
164   tSDP_CONT_INFO cont_info; /* structure to hold continuation information for
165                                the server response */
166 
167 } tCONN_CB;
168 
169 /*  The main SDP control block */
170 typedef struct {
171   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
172   tCONN_CB ccb[SDP_MAX_CONNECTIONS];
173   tSDP_DB server_db;
174   tL2CAP_APPL_INFO reg_info;    /* L2CAP Registration info */
175   uint16_t max_attr_list_size;  /* Max attribute list size to use   */
176   uint16_t max_recs_per_search; /* Max records we want per seaarch  */
177   uint8_t trace_level;
178 } tSDP_CB;
179 
180 /* Global SDP data */
181 extern tSDP_CB sdp_cb;
182 
183 /* Functions provided by sdp_main.cc */
184 extern void sdp_init(void);
185 extern void sdp_free(void);
186 extern void sdp_disconnect(tCONN_CB* p_ccb, tSDP_REASON reason);
187 
188 extern void sdp_conn_timer_timeout(void* data);
189 
190 extern tCONN_CB* sdp_conn_originate(const RawAddress& p_bd_addr);
191 
192 /* Functions provided by sdp_utils.cc
193  */
194 extern void sdpu_log_attribute_metrics(const RawAddress& bda,
195                                        tSDP_DISCOVERY_DB* p_db);
196 extern tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid);
197 extern tCONN_CB* sdpu_find_ccb_by_db(tSDP_DISCOVERY_DB* p_db);
198 extern tCONN_CB* sdpu_allocate_ccb(void);
199 extern void sdpu_release_ccb(tCONN_CB* p_ccb);
200 
201 extern uint8_t* sdpu_build_attrib_seq(uint8_t* p_out, uint16_t* p_attr,
202                                       uint16_t num_attrs);
203 extern uint8_t* sdpu_build_attrib_entry(uint8_t* p_out, tSDP_ATTRIBUTE* p_attr);
204 extern void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num,
205                                     uint16_t error_code, char* p_error_text);
206 
207 extern uint8_t* sdpu_extract_attr_seq(uint8_t* p, uint16_t param_len,
208                                       tSDP_ATTR_SEQ* p_seq);
209 extern uint8_t* sdpu_extract_uid_seq(uint8_t* p, uint16_t param_len,
210                                      tSDP_UUID_SEQ* p_seq);
211 
212 extern uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t* p_end, uint8_t type,
213                                        uint32_t* p_len);
214 extern bool sdpu_is_base_uuid(uint8_t* p_uuid);
215 extern bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1,
216                                      uint8_t* p_uuid2, uint16_t len2);
217 extern bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid,
218                                         tSDP_DISC_ATTR* p_attr);
219 
220 extern void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db);
221 extern uint16_t sdpu_get_list_len(tSDP_UUID_SEQ* uid_seq,
222                                   tSDP_ATTR_SEQ* attr_seq);
223 extern uint16_t sdpu_get_attrib_seq_len(tSDP_RECORD* p_rec,
224                                         tSDP_ATTR_SEQ* attr_seq);
225 extern uint16_t sdpu_get_attrib_entry_len(tSDP_ATTRIBUTE* p_attr);
226 extern uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out,
227                                                 tSDP_ATTRIBUTE* p_attr,
228                                                 uint16_t len, uint16_t* offset);
229 extern uint16_t sdpu_is_avrcp_profile_description_list(tSDP_ATTRIBUTE* p_attr);
230 
231 /* Functions provided by sdp_db.cc
232  */
233 extern tSDP_RECORD* sdp_db_service_search(tSDP_RECORD* p_rec,
234                                           tSDP_UUID_SEQ* p_seq);
235 extern tSDP_RECORD* sdp_db_find_record(uint32_t handle);
236 extern tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(tSDP_RECORD* p_rec,
237                                                uint16_t start_attr,
238                                                uint16_t end_attr);
239 
240 /* Functions provided by sdp_server.cc
241  */
242 extern void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg);
243 
244 /* Functions provided by sdp_discovery.cc
245  */
246 extern void sdp_disc_connected(tCONN_CB* p_ccb);
247 extern void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg);
248 
249 #endif
250