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