• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Out-Flow default values. */
42 #define SDP_OFLOW_QOS_FLAG 0
43 #define SDP_OFLOW_SERV_TYPE 0
44 #define SDP_OFLOW_TOKEN_RATE 0
45 #define SDP_OFLOW_TOKEN_BUCKET_SIZE 0
46 #define SDP_OFLOW_PEAK_BANDWIDTH 0
47 #define SDP_OFLOW_LATENCY 0
48 #define SDP_OFLOW_DELAY_VARIATION 0
49 
50 /* Define the In-Flow default values. */
51 #define SDP_IFLOW_QOS_FLAG 0
52 #define SDP_IFLOW_SERV_TYPE 0
53 #define SDP_IFLOW_TOKEN_RATE 0
54 #define SDP_IFLOW_TOKEN_BUCKET_SIZE 0
55 #define SDP_IFLOW_PEAK_BANDWIDTH 0
56 #define SDP_IFLOW_LATENCY 0
57 #define SDP_IFLOW_DELAY_VARIATION 0
58 
59 #define SDP_LINK_TO 0
60 
61 /* Define the type of device notification. */
62 /* (Inquiry Scan and Page Scan)            */
63 #define SDP_DEVICE_NOTI_LEN \
64   (sizeof(BT_HDR) + HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_PARAM1)
65 
66 #define SDP_DEVICE_NOTI_FLAG 0x03
67 
68 /* Define the Protocol Data Unit (PDU) types.
69  */
70 #define SDP_PDU_ERROR_RESPONSE 0x01
71 #define SDP_PDU_SERVICE_SEARCH_REQ 0x02
72 #define SDP_PDU_SERVICE_SEARCH_RSP 0x03
73 #define SDP_PDU_SERVICE_ATTR_REQ 0x04
74 #define SDP_PDU_SERVICE_ATTR_RSP 0x05
75 #define SDP_PDU_SERVICE_SEARCH_ATTR_REQ 0x06
76 #define SDP_PDU_SERVICE_SEARCH_ATTR_RSP 0x07
77 
78 /* Max UUIDs and attributes we support per sequence */
79 #define MAX_UUIDS_PER_SEQ 16
80 #define MAX_ATTR_PER_SEQ 16
81 
82 /* Max length we support for any attribute */
83 #ifdef SDP_MAX_ATTR_LEN
84 #define MAX_ATTR_LEN SDP_MAX_ATTR_LEN
85 #else
86 #define MAX_ATTR_LEN 256
87 #endif
88 
89 /* Internal UUID sequence representation */
90 typedef struct {
91   uint16_t len;
92   uint8_t value[bluetooth::Uuid::kNumBytes128];
93 } tUID_ENT;
94 
95 typedef struct {
96   uint16_t num_uids;
97   tUID_ENT uuid_entry[MAX_UUIDS_PER_SEQ];
98 } tSDP_UUID_SEQ;
99 
100 /* Internal attribute sequence definitions */
101 typedef struct {
102   uint16_t start;
103   uint16_t end;
104 } tATT_ENT;
105 
106 typedef struct {
107   uint16_t num_attr;
108   tATT_ENT attr_entry[MAX_ATTR_PER_SEQ];
109 } tSDP_ATTR_SEQ;
110 
111 /* Define the attribute element of the SDP database record */
112 typedef struct {
113   uint32_t len;       /* Number of bytes in the entry */
114   uint8_t* value_ptr; /* Points to attr_pad */
115   uint16_t id;
116   uint8_t type;
117 } tSDP_ATTRIBUTE;
118 
119 /* An SDP record consists of a handle, and 1 or more attributes */
120 typedef struct {
121   uint32_t record_handle;
122   uint32_t free_pad_ptr;
123   uint16_t num_attributes;
124   tSDP_ATTRIBUTE attribute[SDP_MAX_REC_ATTR];
125   uint8_t attr_pad[SDP_MAX_PAD_LEN];
126 } tSDP_RECORD;
127 
128 /* Define the SDP database */
129 typedef struct {
130   uint32_t
131       di_primary_handle; /* Device ID Primary record or NULL if nonexistent */
132   uint16_t num_records;
133   tSDP_RECORD record[SDP_MAX_RECORDS];
134 } tSDP_DB;
135 
136 enum {
137   SDP_IS_SEARCH,
138   SDP_IS_ATTR_SEARCH,
139 };
140 
141 #if (SDP_SERVER_ENABLED == TRUE)
142 /* Continuation information for the SDP server response */
143 typedef struct {
144   uint16_t next_attr_index;    /* attr index for next continuation response */
145   uint16_t next_attr_start_id; /* attr id to start with for the attr index in
146                                   next cont. response */
147   tSDP_RECORD* prev_sdp_rec; /* last sdp record that was completely sent in the
148                                 response */
149   bool last_attr_seq_desc_sent; /* whether attr seq length has been sent
150                                    previously */
151   uint16_t attr_offset; /* offset within the attr to keep trak of partial
152                            attributes in the responses */
153 } tSDP_CONT_INFO;
154 #endif /* SDP_SERVER_ENABLED == TRUE */
155 
156 /* Define the SDP Connection Control Block */
157 typedef struct {
158 #define SDP_STATE_IDLE 0
159 #define SDP_STATE_CONN_SETUP 1
160 #define SDP_STATE_CFG_SETUP 2
161 #define SDP_STATE_CONNECTED 3
162   uint8_t con_state;
163 
164 #define SDP_FLAGS_IS_ORIG 0x01
165 #define SDP_FLAGS_HIS_CFG_DONE 0x02
166 #define SDP_FLAGS_MY_CFG_DONE 0x04
167   uint8_t con_flags;
168 
169   RawAddress device_address;
170   alarm_t* sdp_conn_timer;
171   uint16_t rem_mtu_size;
172   uint16_t connection_id;
173   uint16_t list_len; /* length of the response in the GKI buffer */
174   uint8_t* rsp_list; /* pointer to GKI buffer holding response */
175 
176   tSDP_DISCOVERY_DB* p_db; /* Database to save info into   */
177   tSDP_DISC_CMPL_CB* p_cb; /* Callback for discovery done  */
178   tSDP_DISC_CMPL_CB2*
179       p_cb2; /* Callback for discovery done piggy back with the user data */
180   void* user_data; /* piggy back user data */
181   uint32_t
182       handles[SDP_MAX_DISC_SERVER_RECS]; /* Discovered server record handles */
183   uint16_t num_handles;                  /* Number of server handles     */
184   uint16_t cur_handle;                   /* Current handle being processed */
185   uint16_t transaction_id;
186   uint16_t disconnect_reason; /* Disconnect reason            */
187 #if (SDP_BROWSE_PLUS == TRUE)
188   uint16_t cur_uuid_idx;
189 #endif
190 
191 #define SDP_DISC_WAIT_CONN 0
192 #define SDP_DISC_WAIT_HANDLES 1
193 #define SDP_DISC_WAIT_ATTR 2
194 #define SDP_DISC_WAIT_SEARCH_ATTR 3
195 #define SDP_DISC_WAIT_CANCEL 5
196 
197   uint8_t disc_state;
198   uint8_t is_attr_search;
199 
200 #if (SDP_SERVER_ENABLED == TRUE)
201   uint16_t cont_offset;     /* Continuation state data in the server response */
202   tSDP_CONT_INFO cont_info; /* structure to hold continuation information for
203                                the server response */
204 #endif                      /* SDP_SERVER_ENABLED == TRUE */
205 
206 } tCONN_CB;
207 
208 /*  The main SDP control block */
209 typedef struct {
210   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
211   tCONN_CB ccb[SDP_MAX_CONNECTIONS];
212 #if (SDP_SERVER_ENABLED == TRUE)
213   tSDP_DB server_db;
214 #endif
215   tL2CAP_APPL_INFO reg_info;    /* L2CAP Registration info */
216   uint16_t max_attr_list_size;  /* Max attribute list size to use   */
217   uint16_t max_recs_per_search; /* Max records we want per seaarch  */
218   uint8_t trace_level;
219 } tSDP_CB;
220 
221 /* Global SDP data */
222 extern tSDP_CB sdp_cb;
223 
224 /* Functions provided by sdp_main.cc */
225 extern void sdp_init(void);
226 extern void sdp_free(void);
227 extern void sdp_disconnect(tCONN_CB* p_ccb, uint16_t reason);
228 
229 #if (SDP_DEBUG == TRUE)
230 extern uint16_t sdp_set_max_attr_list_size(uint16_t max_size);
231 #endif
232 
233 /* Functions provided by sdp_conn.cc
234  */
235 extern void sdp_conn_rcv_l2e_conn_ind(BT_HDR* p_msg);
236 extern void sdp_conn_rcv_l2e_conn_cfm(BT_HDR* p_msg);
237 extern void sdp_conn_rcv_l2e_disc(BT_HDR* p_msg);
238 extern void sdp_conn_rcv_l2e_config_ind(BT_HDR* p_msg);
239 extern void sdp_conn_rcv_l2e_config_cfm(BT_HDR* p_msg);
240 extern void sdp_conn_rcv_l2e_conn_failed(BT_HDR* p_msg);
241 extern void sdp_conn_rcv_l2e_connected(BT_HDR* p_msg);
242 extern void sdp_conn_rcv_l2e_conn_failed(BT_HDR* p_msg);
243 extern void sdp_conn_rcv_l2e_data(BT_HDR* p_msg);
244 extern void sdp_conn_timer_timeout(void* data);
245 
246 extern tCONN_CB* sdp_conn_originate(const RawAddress& p_bd_addr);
247 
248 /* Functions provided by sdp_utils.cc
249  */
250 extern void sdpu_log_attribute_metrics(const RawAddress& bda,
251                                        tSDP_DISCOVERY_DB* p_db);
252 extern tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid);
253 extern tCONN_CB* sdpu_find_ccb_by_db(tSDP_DISCOVERY_DB* p_db);
254 extern tCONN_CB* sdpu_allocate_ccb(void);
255 extern void sdpu_release_ccb(tCONN_CB* p_ccb);
256 
257 extern uint8_t* sdpu_build_attrib_seq(uint8_t* p_out, uint16_t* p_attr,
258                                       uint16_t num_attrs);
259 extern uint8_t* sdpu_build_attrib_entry(uint8_t* p_out, tSDP_ATTRIBUTE* p_attr);
260 extern void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num,
261                                     uint16_t error_code, char* p_error_text);
262 
263 extern uint8_t* sdpu_extract_attr_seq(uint8_t* p, uint16_t param_len,
264                                       tSDP_ATTR_SEQ* p_seq);
265 extern uint8_t* sdpu_extract_uid_seq(uint8_t* p, uint16_t param_len,
266                                      tSDP_UUID_SEQ* p_seq);
267 
268 extern uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t* p_end, uint8_t type,
269                                        uint32_t* p_len);
270 extern bool sdpu_is_base_uuid(uint8_t* p_uuid);
271 extern bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1,
272                                      uint8_t* p_uuid2, uint16_t len2);
273 extern bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid,
274                                         tSDP_DISC_ATTR* p_attr);
275 
276 extern void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db);
277 extern uint16_t sdpu_get_list_len(tSDP_UUID_SEQ* uid_seq,
278                                   tSDP_ATTR_SEQ* attr_seq);
279 extern uint16_t sdpu_get_attrib_seq_len(tSDP_RECORD* p_rec,
280                                         tSDP_ATTR_SEQ* attr_seq);
281 extern uint16_t sdpu_get_attrib_entry_len(tSDP_ATTRIBUTE* p_attr);
282 extern uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out,
283                                                 tSDP_ATTRIBUTE* p_attr,
284                                                 uint16_t len, uint16_t* offset);
285 
286 /* Functions provided by sdp_db.cc
287  */
288 extern tSDP_RECORD* sdp_db_service_search(tSDP_RECORD* p_rec,
289                                           tSDP_UUID_SEQ* p_seq);
290 extern tSDP_RECORD* sdp_db_find_record(uint32_t handle);
291 extern tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(tSDP_RECORD* p_rec,
292                                                uint16_t start_attr,
293                                                uint16_t end_attr);
294 
295 /* Functions provided by sdp_server.cc
296  */
297 #if (SDP_SERVER_ENABLED == TRUE)
298 extern void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg);
299 #else
300 #define sdp_server_handle_client_req(p_ccb, p_msg)
301 #endif
302 
303 /* Functions provided by sdp_discovery.cc
304  */
305 extern void sdp_disc_connected(tCONN_CB* p_ccb);
306 extern void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg);
307 
308 #endif
309