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 #if (SDP_SERVER_ENABLED == TRUE)
110 /* Continuation information for the SDP server response */
111 typedef struct {
112   uint16_t next_attr_index;    /* attr index for next continuation response */
113   uint16_t next_attr_start_id; /* attr id to start with for the attr index in
114                                   next cont. response */
115   tSDP_RECORD* prev_sdp_rec; /* last sdp record that was completely sent in the
116                                 response */
117   bool last_attr_seq_desc_sent; /* whether attr seq length has been sent
118                                    previously */
119   uint16_t attr_offset; /* offset within the attr to keep trak of partial
120                            attributes in the responses */
121 } tSDP_CONT_INFO;
122 #endif /* SDP_SERVER_ENABLED == TRUE */
123 
124 /* Define the SDP Connection Control Block */
125 typedef struct {
126 #define SDP_STATE_IDLE 0
127 #define SDP_STATE_CONN_SETUP 1
128 #define SDP_STATE_CFG_SETUP 2
129 #define SDP_STATE_CONNECTED 3
130   uint8_t con_state;
131 
132 #define SDP_FLAGS_IS_ORIG 0x01
133 #define SDP_FLAGS_HIS_CFG_DONE 0x02
134 #define SDP_FLAGS_MY_CFG_DONE 0x04
135   uint8_t con_flags;
136 
137   RawAddress device_address;
138   alarm_t* sdp_conn_timer;
139   uint16_t rem_mtu_size;
140   uint16_t connection_id;
141   uint16_t list_len; /* length of the response in the GKI buffer */
142   uint8_t* rsp_list; /* pointer to GKI buffer holding response */
143 
144   tSDP_DISCOVERY_DB* p_db; /* Database to save info into   */
145   tSDP_DISC_CMPL_CB* p_cb; /* Callback for discovery done  */
146   tSDP_DISC_CMPL_CB2*
147       p_cb2; /* Callback for discovery done piggy back with the user data */
148   void* user_data; /* piggy back user data */
149   uint32_t
150       handles[SDP_MAX_DISC_SERVER_RECS]; /* Discovered server record handles */
151   uint16_t num_handles;                  /* Number of server handles     */
152   uint16_t cur_handle;                   /* Current handle being processed */
153   uint16_t transaction_id;
154   uint16_t disconnect_reason; /* Disconnect reason            */
155 #if (SDP_BROWSE_PLUS == TRUE)
156   uint16_t cur_uuid_idx;
157 #endif
158 
159 #define SDP_DISC_WAIT_CONN 0
160 #define SDP_DISC_WAIT_HANDLES 1
161 #define SDP_DISC_WAIT_ATTR 2
162 #define SDP_DISC_WAIT_SEARCH_ATTR 3
163 #define SDP_DISC_WAIT_CANCEL 5
164 
165   uint8_t disc_state;
166   uint8_t is_attr_search;
167 
168 #if (SDP_SERVER_ENABLED == TRUE)
169   uint16_t cont_offset;     /* Continuation state data in the server response */
170   tSDP_CONT_INFO cont_info; /* structure to hold continuation information for
171                                the server response */
172 #endif                      /* SDP_SERVER_ENABLED == TRUE */
173 
174 } tCONN_CB;
175 
176 /*  The main SDP control block */
177 typedef struct {
178   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
179   tCONN_CB ccb[SDP_MAX_CONNECTIONS];
180 #if (SDP_SERVER_ENABLED == TRUE)
181   tSDP_DB server_db;
182 #endif
183   tL2CAP_APPL_INFO reg_info;    /* L2CAP Registration info */
184   uint16_t max_attr_list_size;  /* Max attribute list size to use   */
185   uint16_t max_recs_per_search; /* Max records we want per seaarch  */
186   uint8_t trace_level;
187 } tSDP_CB;
188 
189 /* Global SDP data */
190 extern tSDP_CB sdp_cb;
191 
192 /* Functions provided by sdp_main.cc */
193 extern void sdp_init(void);
194 extern void sdp_free(void);
195 extern void sdp_disconnect(tCONN_CB* p_ccb, uint16_t reason);
196 
197 extern void sdp_conn_timer_timeout(void* data);
198 
199 extern tCONN_CB* sdp_conn_originate(const RawAddress& p_bd_addr);
200 
201 /* Functions provided by sdp_utils.cc
202  */
203 extern void sdpu_log_attribute_metrics(const RawAddress& bda,
204                                        tSDP_DISCOVERY_DB* p_db);
205 extern tCONN_CB* sdpu_find_ccb_by_cid(uint16_t cid);
206 extern tCONN_CB* sdpu_find_ccb_by_db(tSDP_DISCOVERY_DB* p_db);
207 extern tCONN_CB* sdpu_allocate_ccb(void);
208 extern void sdpu_release_ccb(tCONN_CB* p_ccb);
209 
210 extern uint8_t* sdpu_build_attrib_seq(uint8_t* p_out, uint16_t* p_attr,
211                                       uint16_t num_attrs);
212 extern uint8_t* sdpu_build_attrib_entry(uint8_t* p_out, tSDP_ATTRIBUTE* p_attr);
213 extern void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num,
214                                     uint16_t error_code, char* p_error_text);
215 
216 extern uint8_t* sdpu_extract_attr_seq(uint8_t* p, uint16_t param_len,
217                                       tSDP_ATTR_SEQ* p_seq);
218 extern uint8_t* sdpu_extract_uid_seq(uint8_t* p, uint16_t param_len,
219                                      tSDP_UUID_SEQ* p_seq);
220 
221 extern uint8_t* sdpu_get_len_from_type(uint8_t* p, uint8_t* p_end, uint8_t type,
222                                        uint32_t* p_len);
223 extern bool sdpu_is_base_uuid(uint8_t* p_uuid);
224 extern bool sdpu_compare_uuid_arrays(uint8_t* p_uuid1, uint32_t len1,
225                                      uint8_t* p_uuid2, uint16_t len2);
226 extern bool sdpu_compare_uuid_with_attr(const bluetooth::Uuid& uuid,
227                                         tSDP_DISC_ATTR* p_attr);
228 
229 extern void sdpu_sort_attr_list(uint16_t num_attr, tSDP_DISCOVERY_DB* p_db);
230 extern uint16_t sdpu_get_list_len(tSDP_UUID_SEQ* uid_seq,
231                                   tSDP_ATTR_SEQ* attr_seq);
232 extern uint16_t sdpu_get_attrib_seq_len(tSDP_RECORD* p_rec,
233                                         tSDP_ATTR_SEQ* attr_seq);
234 extern uint16_t sdpu_get_attrib_entry_len(tSDP_ATTRIBUTE* p_attr);
235 extern uint8_t* sdpu_build_partial_attrib_entry(uint8_t* p_out,
236                                                 tSDP_ATTRIBUTE* p_attr,
237                                                 uint16_t len, uint16_t* offset);
238 
239 /* Functions provided by sdp_db.cc
240  */
241 extern tSDP_RECORD* sdp_db_service_search(tSDP_RECORD* p_rec,
242                                           tSDP_UUID_SEQ* p_seq);
243 extern tSDP_RECORD* sdp_db_find_record(uint32_t handle);
244 extern tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(tSDP_RECORD* p_rec,
245                                                uint16_t start_attr,
246                                                uint16_t end_attr);
247 
248 /* Functions provided by sdp_server.cc
249  */
250 #if (SDP_SERVER_ENABLED == TRUE)
251 extern void sdp_server_handle_client_req(tCONN_CB* p_ccb, BT_HDR* p_msg);
252 #else
253 #define sdp_server_handle_client_req(p_ccb, p_msg)
254 #endif
255 
256 /* Functions provided by sdp_discovery.cc
257  */
258 extern void sdp_disc_connected(tCONN_CB* p_ccb);
259 extern void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg);
260 
261 #endif
262