sdp_server.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 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. * This file contains functions that handle the SDP server functions.
  21. * This is mainly dealing with client requests
  22. *
  23. ******************************************************************************/
  24. //#include <stdlib.h>
  25. #include <string.h>
  26. //#include <stdio.h>
  27. #include "stack/bt_types.h"
  28. #include "osi/allocator.h"
  29. #include "stack/btu.h"
  30. #include "common/bt_defs.h"
  31. #include "stack/l2cdefs.h"
  32. #include "stack/hcidefs.h"
  33. #include "stack/hcimsgs.h"
  34. #include "stack/sdp_api.h"
  35. #include "sdpint.h"
  36. #if SDP_SERVER_ENABLED == TRUE
  37. /* Maximum number of bytes to reserve out of SDP MTU for response data */
  38. #define SDP_MAX_SERVICE_RSPHDR_LEN 12
  39. #define SDP_MAX_SERVATTR_RSPHDR_LEN 10
  40. #define SDP_MAX_ATTR_RSPHDR_LEN 10
  41. /********************************************************************************/
  42. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  43. /********************************************************************************/
  44. static void process_service_search (tCONN_CB *p_ccb, UINT16 trans_num,
  45. UINT16 param_len, UINT8 *p_req,
  46. UINT8 *p_req_end);
  47. static void process_service_attr_req (tCONN_CB *p_ccb, UINT16 trans_num,
  48. UINT16 param_len, UINT8 *p_req,
  49. UINT8 *p_req_end);
  50. static void process_service_search_attr_req (tCONN_CB *p_ccb, UINT16 trans_num,
  51. UINT16 param_len, UINT8 *p_req,
  52. UINT8 *p_req_end);
  53. /********************************************************************************/
  54. /* E R R O R T E X T S T R I N G S */
  55. /* */
  56. /* The default is to have no text string, but we allow the strings to be */
  57. /* configured in target.h if people want them. */
  58. /********************************************************************************/
  59. #ifndef SDP_TEXT_BAD_HEADER
  60. #define SDP_TEXT_BAD_HEADER NULL
  61. #endif
  62. #ifndef SDP_TEXT_BAD_PDU
  63. #define SDP_TEXT_BAD_PDU NULL
  64. #endif
  65. #ifndef SDP_TEXT_BAD_UUID_LIST
  66. #define SDP_TEXT_BAD_UUID_LIST NULL
  67. #endif
  68. #ifndef SDP_TEXT_BAD_HANDLE
  69. #define SDP_TEXT_BAD_HANDLE NULL
  70. #endif
  71. #ifndef SDP_TEXT_BAD_ATTR_LIST
  72. #define SDP_TEXT_BAD_ATTR_LIST NULL
  73. #endif
  74. #ifndef SDP_TEXT_BAD_CONT_LEN
  75. #define SDP_TEXT_BAD_CONT_LEN NULL
  76. #endif
  77. #ifndef SDP_TEXT_BAD_CONT_INX
  78. #define SDP_TEXT_BAD_CONT_INX NULL
  79. #endif
  80. #ifndef SDP_TEXT_BAD_MAX_RECORDS_LIST
  81. #define SDP_TEXT_BAD_MAX_RECORDS_LIST NULL
  82. #endif
  83. /*******************************************************************************
  84. **
  85. ** Function sdp_server_handle_client_req
  86. **
  87. ** Description This is the main dispatcher of the SDP server. It is called
  88. ** when any data is received from L2CAP, and dispatches the
  89. ** request to the appropriate handler.
  90. **
  91. ** Returns void
  92. **
  93. *******************************************************************************/
  94. void sdp_server_handle_client_req (tCONN_CB *p_ccb, BT_HDR *p_msg)
  95. {
  96. UINT8 *p_req = (UINT8 *) (p_msg + 1) + p_msg->offset;
  97. UINT8 *p_req_end = p_req + p_msg->len;
  98. UINT8 pdu_id;
  99. UINT16 trans_num, param_len;
  100. /* Start inactivity timer */
  101. btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
  102. /* The first byte in the message is the pdu type */
  103. pdu_id = *p_req++;
  104. /* Extract the transaction number and parameter length */
  105. BE_STREAM_TO_UINT16 (trans_num, p_req);
  106. BE_STREAM_TO_UINT16 (param_len, p_req);
  107. if ((p_req + param_len) != p_req_end) {
  108. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_PDU_SIZE, SDP_TEXT_BAD_HEADER);
  109. return;
  110. }
  111. switch (pdu_id) {
  112. case SDP_PDU_SERVICE_SEARCH_REQ:
  113. process_service_search (p_ccb, trans_num, param_len, p_req, p_req_end);
  114. break;
  115. case SDP_PDU_SERVICE_ATTR_REQ:
  116. process_service_attr_req (p_ccb, trans_num, param_len, p_req, p_req_end);
  117. break;
  118. case SDP_PDU_SERVICE_SEARCH_ATTR_REQ:
  119. process_service_search_attr_req (p_ccb, trans_num, param_len, p_req, p_req_end);
  120. break;
  121. default:
  122. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_PDU);
  123. SDP_TRACE_WARNING ("SDP - server got unknown PDU: 0x%x\n", pdu_id);
  124. break;
  125. }
  126. }
  127. /*******************************************************************************
  128. **
  129. ** Function process_service_search
  130. **
  131. ** Description This function handles a service search request from the
  132. ** client. It builds a reply message with info from the database,
  133. ** and sends the reply back to the client.
  134. **
  135. ** Returns void
  136. **
  137. *******************************************************************************/
  138. static void process_service_search (tCONN_CB *p_ccb, UINT16 trans_num,
  139. UINT16 param_len, UINT8 *p_req,
  140. UINT8 *p_req_end)
  141. {
  142. UINT16 max_replies, cur_handles, rem_handles, cont_offset;
  143. tSDP_UUID_SEQ uid_seq;
  144. UINT8 *p_rsp, *p_rsp_start, *p_rsp_param_len;
  145. UINT16 rsp_param_len, num_rsp_handles, xx;
  146. UINT32 rsp_handles[SDP_MAX_RECORDS] = {0};
  147. tSDP_RECORD *p_rec = NULL;
  148. BT_HDR *p_buf;
  149. BOOLEAN is_cont = FALSE;
  150. UNUSED(p_req_end);
  151. p_req = sdpu_extract_uid_seq (p_req, param_len, &uid_seq);
  152. if ((!p_req) || (!uid_seq.num_uids)) {
  153. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_UUID_LIST);
  154. return;
  155. }
  156. /* Get the max replies we can send. Cap it at our max anyways. */
  157. BE_STREAM_TO_UINT16 (max_replies, p_req);
  158. if (max_replies > SDP_MAX_RECORDS) {
  159. max_replies = SDP_MAX_RECORDS;
  160. }
  161. if ((!p_req) || (p_req > p_req_end)) {
  162. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_MAX_RECORDS_LIST);
  163. return;
  164. }
  165. /* Get a list of handles that match the UUIDs given to us */
  166. for (num_rsp_handles = 0; num_rsp_handles < max_replies; ) {
  167. p_rec = sdp_db_service_search (p_rec, &uid_seq);
  168. if (p_rec) {
  169. rsp_handles[num_rsp_handles++] = p_rec->record_handle;
  170. } else {
  171. break;
  172. }
  173. }
  174. /* Check if this is a continuation request */
  175. if (*p_req) {
  176. if (*p_req++ != SDP_CONTINUATION_LEN || (p_req >= p_req_end)) {
  177. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE,
  178. SDP_TEXT_BAD_CONT_LEN);
  179. return;
  180. }
  181. BE_STREAM_TO_UINT16 (cont_offset, p_req);
  182. if (cont_offset != p_ccb->cont_offset || num_rsp_handles < cont_offset) {
  183. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE,
  184. SDP_TEXT_BAD_CONT_INX);
  185. return;
  186. }
  187. rem_handles = num_rsp_handles - cont_offset; /* extract the remaining handles */
  188. } else {
  189. rem_handles = num_rsp_handles;
  190. cont_offset = 0;
  191. p_ccb->cont_offset = 0;
  192. }
  193. /* Calculate how many handles will fit in one PDU */
  194. cur_handles = (UINT16)((p_ccb->rem_mtu_size - SDP_MAX_SERVICE_RSPHDR_LEN) / 4);
  195. if (rem_handles <= cur_handles) {
  196. cur_handles = rem_handles;
  197. } else { /* Continuation is set */
  198. p_ccb->cont_offset += cur_handles;
  199. is_cont = TRUE;
  200. }
  201. /* Get a buffer to use to build the response */
  202. if ((p_buf = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
  203. SDP_TRACE_ERROR ("SDP - no buf for search rsp\n");
  204. return;
  205. }
  206. p_buf->offset = L2CAP_MIN_OFFSET;
  207. p_rsp = p_rsp_start = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  208. /* Start building a rsponse */
  209. UINT8_TO_BE_STREAM (p_rsp, SDP_PDU_SERVICE_SEARCH_RSP);
  210. UINT16_TO_BE_STREAM (p_rsp, trans_num);
  211. /* Skip the length, we need to add it at the end */
  212. p_rsp_param_len = p_rsp;
  213. p_rsp += 2;
  214. /* Put in total and current number of handles, and handles themselves */
  215. UINT16_TO_BE_STREAM (p_rsp, num_rsp_handles);
  216. UINT16_TO_BE_STREAM (p_rsp, cur_handles);
  217. /* SDP_TRACE_DEBUG("SDP Service Rsp: tothdl %d, curhdlr %d, start %d, end %d, cont %d",
  218. num_rsp_handles, cur_handles, cont_offset,
  219. cont_offset + cur_handles-1, is_cont); */
  220. for (xx = cont_offset; xx < cont_offset + cur_handles; xx++) {
  221. UINT32_TO_BE_STREAM (p_rsp, rsp_handles[xx]);
  222. }
  223. if (is_cont) {
  224. UINT8_TO_BE_STREAM (p_rsp, SDP_CONTINUATION_LEN);
  225. UINT16_TO_BE_STREAM (p_rsp, p_ccb->cont_offset);
  226. } else {
  227. UINT8_TO_BE_STREAM (p_rsp, 0);
  228. }
  229. /* Go back and put the parameter length into the buffer */
  230. rsp_param_len = p_rsp - p_rsp_param_len - 2;
  231. UINT16_TO_BE_STREAM (p_rsp_param_len, rsp_param_len);
  232. /* Set the length of the SDP data in the buffer */
  233. p_buf->len = p_rsp - p_rsp_start;
  234. /* Send the buffer through L2CAP */
  235. L2CA_DataWrite (p_ccb->connection_id, p_buf);
  236. }
  237. /*******************************************************************************
  238. **
  239. ** Function process_service_attr_req
  240. **
  241. ** Description This function handles an attribute request from the client.
  242. ** It builds a reply message with info from the database,
  243. ** and sends the reply back to the client.
  244. **
  245. ** Returns void
  246. **
  247. *******************************************************************************/
  248. static void process_service_attr_req (tCONN_CB *p_ccb, UINT16 trans_num,
  249. UINT16 param_len, UINT8 *p_req,
  250. UINT8 *p_req_end)
  251. {
  252. UINT16 max_list_len, len_to_send, cont_offset;
  253. INT16 rem_len;
  254. tSDP_ATTR_SEQ attr_seq, attr_seq_sav;
  255. UINT8 *p_rsp, *p_rsp_start, *p_rsp_param_len;
  256. UINT16 rsp_param_len, xx;
  257. UINT32 rec_handle;
  258. tSDP_RECORD *p_rec;
  259. tSDP_ATTRIBUTE *p_attr;
  260. BT_HDR *p_buf;
  261. BOOLEAN is_cont = FALSE;
  262. UINT16 attr_len;
  263. /* Extract the record handle */
  264. BE_STREAM_TO_UINT32 (rec_handle, p_req);
  265. if (p_req > p_req_end) {
  266. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_SERV_REC_HDL, SDP_TEXT_BAD_HANDLE);
  267. return;
  268. }
  269. /* Get the max list length we can send. Cap it at MTU size minus overhead */
  270. BE_STREAM_TO_UINT16 (max_list_len, p_req);
  271. if (max_list_len > (p_ccb->rem_mtu_size - SDP_MAX_ATTR_RSPHDR_LEN)) {
  272. max_list_len = p_ccb->rem_mtu_size - SDP_MAX_ATTR_RSPHDR_LEN;
  273. }
  274. p_req = sdpu_extract_attr_seq (p_req, param_len, &attr_seq);
  275. if ((!p_req) || (!attr_seq.num_attr) || (p_req > p_req_end)) {
  276. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_ATTR_LIST);
  277. return;
  278. }
  279. memcpy(&attr_seq_sav, &attr_seq, sizeof(tSDP_ATTR_SEQ)) ;
  280. /* Find a record with the record handle */
  281. p_rec = sdp_db_find_record (rec_handle);
  282. if (!p_rec) {
  283. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_SERV_REC_HDL, SDP_TEXT_BAD_HANDLE);
  284. return;
  285. }
  286. /* Check if this is a continuation request */
  287. if (*p_req) {
  288. /* Free and reallocate buffer */
  289. if (p_ccb->rsp_list) {
  290. osi_free(p_ccb->rsp_list);
  291. }
  292. p_ccb->rsp_list = (UINT8 *)osi_malloc(max_list_len);
  293. if (p_ccb->rsp_list == NULL) {
  294. SDP_TRACE_ERROR("%s No scratch buf for attr rsp\n", __func__);
  295. return;
  296. }
  297. if (*p_req++ != SDP_CONTINUATION_LEN) {
  298. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE, SDP_TEXT_BAD_CONT_LEN);
  299. return;
  300. }
  301. BE_STREAM_TO_UINT16 (cont_offset, p_req);
  302. if (cont_offset != p_ccb->cont_offset) {
  303. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE, SDP_TEXT_BAD_CONT_INX);
  304. return;
  305. }
  306. if (!p_ccb->rsp_list) {
  307. sdpu_build_n_send_error (p_ccb, trans_num, SDP_NO_RESOURCES, NULL);
  308. return;
  309. }
  310. is_cont = TRUE;
  311. /* Initialise for continuation response */
  312. p_rsp = &p_ccb->rsp_list[0];
  313. attr_seq.attr_entry[p_ccb->cont_info.next_attr_index].start = p_ccb->cont_info.next_attr_start_id;
  314. } else {
  315. if (p_ccb->rsp_list) {
  316. osi_free (p_ccb->rsp_list);
  317. }
  318. p_ccb->rsp_list = (UINT8 *)osi_malloc (max_list_len);
  319. if (p_ccb->rsp_list == NULL) {
  320. SDP_TRACE_ERROR ("SDP - no scratch buf for search rsp\n");
  321. return;
  322. }
  323. p_ccb->cont_offset = 0;
  324. p_rsp = &p_ccb->rsp_list[3]; /* Leave space for data elem descr */
  325. /* Reset continuation parameters in p_ccb */
  326. p_ccb->cont_info.prev_sdp_rec = NULL;
  327. p_ccb->cont_info.next_attr_index = 0;
  328. p_ccb->cont_info.attr_offset = 0;
  329. }
  330. /* Search for attributes that match the list given to us */
  331. for (xx = p_ccb->cont_info.next_attr_index; xx < attr_seq.num_attr; xx++) {
  332. p_attr = sdp_db_find_attr_in_rec (p_rec, attr_seq.attr_entry[xx].start, attr_seq.attr_entry[xx].end);
  333. if (p_attr) {
  334. /* Check if attribute fits. Assume 3-byte value type/length */
  335. rem_len = max_list_len - (INT16) (p_rsp - &p_ccb->rsp_list[0]);
  336. /* just in case */
  337. if (rem_len <= 0) {
  338. p_ccb->cont_info.next_attr_index = xx;
  339. p_ccb->cont_info.next_attr_start_id = p_attr->id;
  340. break;
  341. }
  342. attr_len = sdpu_get_attrib_entry_len(p_attr);
  343. /* if there is a partial attribute pending to be sent */
  344. if (p_ccb->cont_info.attr_offset) {
  345. p_rsp = sdpu_build_partial_attrib_entry (p_rsp, p_attr, rem_len,
  346. &p_ccb->cont_info.attr_offset);
  347. /* If the partial attrib could not been fully added yet */
  348. if (p_ccb->cont_info.attr_offset != attr_len) {
  349. break;
  350. } else { /* If the partial attrib has been added in full by now */
  351. p_ccb->cont_info.attr_offset = 0; /* reset attr_offset */
  352. }
  353. } else if (rem_len < attr_len) { /* Not enough space for attr... so add partially */
  354. if (attr_len >= SDP_MAX_ATTR_LEN) {
  355. SDP_TRACE_ERROR("SDP attr too big: max_list_len=%d,attr_len=%d\n", max_list_len, attr_len);
  356. sdpu_build_n_send_error (p_ccb, trans_num, SDP_NO_RESOURCES, NULL);
  357. return;
  358. }
  359. /* add the partial attribute if possible */
  360. p_rsp = sdpu_build_partial_attrib_entry (p_rsp, p_attr, (UINT16)rem_len,
  361. &p_ccb->cont_info.attr_offset);
  362. p_ccb->cont_info.next_attr_index = xx;
  363. p_ccb->cont_info.next_attr_start_id = p_attr->id;
  364. break;
  365. } else { /* build the whole attribute */
  366. p_rsp = sdpu_build_attrib_entry (p_rsp, p_attr);
  367. }
  368. /* If doing a range, stick with this one till no more attributes found */
  369. if (attr_seq.attr_entry[xx].start != attr_seq.attr_entry[xx].end) {
  370. /* Update for next time through */
  371. attr_seq.attr_entry[xx].start = p_attr->id + 1;
  372. xx--;
  373. }
  374. }
  375. }
  376. /* If all the attributes have been accomodated in p_rsp,
  377. reset next_attr_index */
  378. if (xx == attr_seq.num_attr) {
  379. p_ccb->cont_info.next_attr_index = 0;
  380. }
  381. len_to_send = (UINT16) (p_rsp - &p_ccb->rsp_list[0]);
  382. cont_offset = 0;
  383. if (!is_cont) {
  384. p_ccb->list_len = sdpu_get_attrib_seq_len(p_rec, &attr_seq_sav) + 3;
  385. /* Put in the sequence header (2 or 3 bytes) */
  386. if (p_ccb->list_len > 255) {
  387. p_ccb->rsp_list[0] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
  388. p_ccb->rsp_list[1] = (UINT8) ((p_ccb->list_len - 3) >> 8);
  389. p_ccb->rsp_list[2] = (UINT8) (p_ccb->list_len - 3);
  390. } else {
  391. cont_offset = 1;
  392. p_ccb->rsp_list[1] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  393. p_ccb->rsp_list[2] = (UINT8) (p_ccb->list_len - 3);
  394. p_ccb->list_len--;
  395. len_to_send--;
  396. }
  397. }
  398. /* Get a buffer to use to build the response */
  399. if ((p_buf = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
  400. SDP_TRACE_ERROR ("SDP - no buf for search rsp\n");
  401. return;
  402. }
  403. p_buf->offset = L2CAP_MIN_OFFSET;
  404. p_rsp = p_rsp_start = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  405. /* Start building a rsponse */
  406. UINT8_TO_BE_STREAM (p_rsp, SDP_PDU_SERVICE_ATTR_RSP);
  407. UINT16_TO_BE_STREAM (p_rsp, trans_num);
  408. /* Skip the parameter length, add it when we know the length */
  409. p_rsp_param_len = p_rsp;
  410. p_rsp += 2;
  411. UINT16_TO_BE_STREAM (p_rsp, len_to_send);
  412. memcpy (p_rsp, &p_ccb->rsp_list[cont_offset], len_to_send);
  413. p_rsp += len_to_send;
  414. p_ccb->cont_offset += len_to_send;
  415. /* If anything left to send, continuation needed */
  416. if (p_ccb->cont_offset < p_ccb->list_len) {
  417. is_cont = TRUE;
  418. UINT8_TO_BE_STREAM (p_rsp, SDP_CONTINUATION_LEN);
  419. UINT16_TO_BE_STREAM (p_rsp, p_ccb->cont_offset);
  420. } else {
  421. UINT8_TO_BE_STREAM (p_rsp, 0);
  422. }
  423. /* Go back and put the parameter length into the buffer */
  424. rsp_param_len = p_rsp - p_rsp_param_len - 2;
  425. UINT16_TO_BE_STREAM (p_rsp_param_len, rsp_param_len);
  426. /* Set the length of the SDP data in the buffer */
  427. p_buf->len = p_rsp - p_rsp_start;
  428. /* Send the buffer through L2CAP */
  429. L2CA_DataWrite (p_ccb->connection_id, p_buf);
  430. }
  431. /*******************************************************************************
  432. **
  433. ** Function process_service_search_attr_req
  434. **
  435. ** Description This function handles a combined service search and attribute
  436. ** read request from the client. It builds a reply message with
  437. ** info from the database, and sends the reply back to the client.
  438. **
  439. ** Returns void
  440. **
  441. *******************************************************************************/
  442. static void process_service_search_attr_req (tCONN_CB *p_ccb, UINT16 trans_num,
  443. UINT16 param_len, UINT8 *p_req,
  444. UINT8 *p_req_end)
  445. {
  446. UINT16 max_list_len;
  447. INT16 rem_len;
  448. UINT16 len_to_send, cont_offset;
  449. tSDP_UUID_SEQ uid_seq;
  450. UINT8 *p_rsp, *p_rsp_start, *p_rsp_param_len;
  451. UINT16 rsp_param_len, xx;
  452. tSDP_RECORD *p_rec;
  453. tSDP_ATTR_SEQ attr_seq, attr_seq_sav;
  454. tSDP_ATTRIBUTE *p_attr;
  455. BT_HDR *p_buf;
  456. BOOLEAN maxxed_out = FALSE, is_cont = FALSE;
  457. UINT8 *p_seq_start;
  458. UINT16 seq_len, attr_len;
  459. UNUSED(p_req_end);
  460. /* Extract the UUID sequence to search for */
  461. p_req = sdpu_extract_uid_seq (p_req, param_len, &uid_seq);
  462. if ((!p_req) || (!uid_seq.num_uids)) {
  463. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_UUID_LIST);
  464. return;
  465. }
  466. /* Get the max list length we can send. Cap it at our max list length. */
  467. BE_STREAM_TO_UINT16 (max_list_len, p_req);
  468. if (max_list_len > (p_ccb->rem_mtu_size - SDP_MAX_SERVATTR_RSPHDR_LEN)) {
  469. max_list_len = p_ccb->rem_mtu_size - SDP_MAX_SERVATTR_RSPHDR_LEN;
  470. }
  471. p_req = sdpu_extract_attr_seq (p_req, param_len, &attr_seq);
  472. if ((!p_req) || (!attr_seq.num_attr)) {
  473. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_REQ_SYNTAX, SDP_TEXT_BAD_ATTR_LIST);
  474. return;
  475. }
  476. memcpy(&attr_seq_sav, &attr_seq, sizeof(tSDP_ATTR_SEQ)) ;
  477. /* Check if this is a continuation request */
  478. if (*p_req) {
  479. /* Free and reallocate buffer */
  480. if (p_ccb->rsp_list) {
  481. osi_free (p_ccb->rsp_list);
  482. }
  483. p_ccb->rsp_list = (UINT8 *)osi_malloc (max_list_len);
  484. if (p_ccb->rsp_list == NULL) {
  485. SDP_TRACE_ERROR ("SDP - no scratch buf for search rsp\n");
  486. return;
  487. }
  488. if (*p_req++ != SDP_CONTINUATION_LEN) {
  489. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE, SDP_TEXT_BAD_CONT_LEN);
  490. return;
  491. }
  492. BE_STREAM_TO_UINT16 (cont_offset, p_req);
  493. if (cont_offset != p_ccb->cont_offset) {
  494. sdpu_build_n_send_error (p_ccb, trans_num, SDP_INVALID_CONT_STATE, SDP_TEXT_BAD_CONT_INX);
  495. return;
  496. }
  497. if (!p_ccb->rsp_list) {
  498. sdpu_build_n_send_error (p_ccb, trans_num, SDP_NO_RESOURCES, NULL);
  499. return;
  500. }
  501. is_cont = TRUE;
  502. /* Initialise for continuation response */
  503. p_rsp = &p_ccb->rsp_list[0];
  504. attr_seq.attr_entry[p_ccb->cont_info.next_attr_index].start = p_ccb->cont_info.next_attr_start_id;
  505. } else {
  506. /* Get a scratch buffer to store response */
  507. /* Free and reallocate if the earlier allocated buffer is small */
  508. if (p_ccb->rsp_list) {
  509. osi_free (p_ccb->rsp_list);
  510. }
  511. p_ccb->rsp_list = (UINT8 *)osi_malloc (max_list_len);
  512. if (p_ccb->rsp_list == NULL) {
  513. SDP_TRACE_ERROR ("SDP - no scratch buf for search rsp\n");
  514. return;
  515. }
  516. p_ccb->cont_offset = 0;
  517. p_rsp = &p_ccb->rsp_list[3]; /* Leave space for data elem descr */
  518. /* Reset continuation parameters in p_ccb */
  519. p_ccb->cont_info.prev_sdp_rec = NULL;
  520. p_ccb->cont_info.next_attr_index = 0;
  521. p_ccb->cont_info.last_attr_seq_desc_sent = FALSE;
  522. p_ccb->cont_info.attr_offset = 0;
  523. }
  524. /* Get a list of handles that match the UUIDs given to us */
  525. for (p_rec = sdp_db_service_search (p_ccb->cont_info.prev_sdp_rec, &uid_seq); p_rec; p_rec = sdp_db_service_search (p_rec, &uid_seq)) {
  526. /* Allow space for attribute sequence type and length */
  527. p_seq_start = p_rsp;
  528. if (p_ccb->cont_info.last_attr_seq_desc_sent == FALSE) {
  529. /* See if there is enough room to include a new service in the current response */
  530. rem_len = max_list_len - (INT16) (p_rsp - &p_ccb->rsp_list[0]);
  531. if (rem_len < 3) {
  532. /* Not enough room. Update continuation info for next response */
  533. p_ccb->cont_info.next_attr_index = 0;
  534. p_ccb->cont_info.next_attr_start_id = attr_seq.attr_entry[0].start;
  535. break;
  536. }
  537. p_rsp += 3;
  538. }
  539. /* Get a list of handles that match the UUIDs given to us */
  540. for (xx = p_ccb->cont_info.next_attr_index; xx < attr_seq.num_attr; xx++) {
  541. p_attr = sdp_db_find_attr_in_rec (p_rec, attr_seq.attr_entry[xx].start, attr_seq.attr_entry[xx].end);
  542. if (p_attr) {
  543. /* Check if attribute fits. Assume 3-byte value type/length */
  544. rem_len = max_list_len - (INT16) (p_rsp - &p_ccb->rsp_list[0]);
  545. /* just in case */
  546. if (rem_len <= 0) {
  547. p_ccb->cont_info.next_attr_index = xx;
  548. p_ccb->cont_info.next_attr_start_id = p_attr->id;
  549. maxxed_out = TRUE;
  550. break;
  551. }
  552. attr_len = sdpu_get_attrib_entry_len(p_attr);
  553. /* if there is a partial attribute pending to be sent */
  554. if (p_ccb->cont_info.attr_offset) {
  555. p_rsp = sdpu_build_partial_attrib_entry (p_rsp, p_attr, rem_len,
  556. &p_ccb->cont_info.attr_offset);
  557. /* If the partial attrib could not been fully added yet */
  558. if (p_ccb->cont_info.attr_offset != attr_len) {
  559. maxxed_out = TRUE;
  560. break;
  561. } else { /* If the partial attrib has been added in full by now */
  562. p_ccb->cont_info.attr_offset = 0; /* reset attr_offset */
  563. }
  564. } else if (rem_len < attr_len) { /* Not enough space for attr... so add partially */
  565. if (attr_len >= SDP_MAX_ATTR_LEN) {
  566. SDP_TRACE_ERROR("SDP attr too big: max_list_len=%d,attr_len=%d\n", max_list_len, attr_len);
  567. sdpu_build_n_send_error (p_ccb, trans_num, SDP_NO_RESOURCES, NULL);
  568. return;
  569. }
  570. /* add the partial attribute if possible */
  571. p_rsp = sdpu_build_partial_attrib_entry (p_rsp, p_attr, (UINT16)rem_len,
  572. &p_ccb->cont_info.attr_offset);
  573. p_ccb->cont_info.next_attr_index = xx;
  574. p_ccb->cont_info.next_attr_start_id = p_attr->id;
  575. maxxed_out = TRUE;
  576. break;
  577. } else { /* build the whole attribute */
  578. p_rsp = sdpu_build_attrib_entry (p_rsp, p_attr);
  579. }
  580. /* If doing a range, stick with this one till no more attributes found */
  581. if (attr_seq.attr_entry[xx].start != attr_seq.attr_entry[xx].end) {
  582. /* Update for next time through */
  583. attr_seq.attr_entry[xx].start = p_attr->id + 1;
  584. xx--;
  585. }
  586. }
  587. }
  588. /* Go back and put the type and length into the buffer */
  589. if (p_ccb->cont_info.last_attr_seq_desc_sent == FALSE) {
  590. seq_len = sdpu_get_attrib_seq_len(p_rec, &attr_seq_sav);
  591. if (seq_len != 0) {
  592. UINT8_TO_BE_STREAM (p_seq_start, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
  593. UINT16_TO_BE_STREAM (p_seq_start, seq_len);
  594. if (maxxed_out) {
  595. p_ccb->cont_info.last_attr_seq_desc_sent = TRUE;
  596. }
  597. } else {
  598. p_rsp = p_seq_start;
  599. }
  600. }
  601. if (maxxed_out) {
  602. break;
  603. }
  604. /* Restore the attr_seq to look for in the next sdp record */
  605. memcpy(&attr_seq, &attr_seq_sav, sizeof(tSDP_ATTR_SEQ)) ;
  606. /* Reset the next attr index */
  607. p_ccb->cont_info.next_attr_index = 0;
  608. p_ccb->cont_info.prev_sdp_rec = p_rec;
  609. p_ccb->cont_info.last_attr_seq_desc_sent = FALSE;
  610. }
  611. /* response length */
  612. len_to_send = (UINT16) (p_rsp - &p_ccb->rsp_list[0]);
  613. cont_offset = 0;
  614. // The current SDP server design has a critical flaw where it can run into an infinite
  615. // request/response loop with the client. Here's the scenario:
  616. // - client makes SDP request
  617. // - server returns the first fragment of the response with a continuation token
  618. // - an SDP record is deleted from the server
  619. // - client issues another request with previous continuation token
  620. // - server has nothing to send back because the record is unavailable but in the
  621. // first fragment, it had specified more response bytes than are now available
  622. // - server sends back no additional response bytes and returns the same continuation token
  623. // - client issues another request with the continuation token, and the process repeats
  624. //
  625. // We work around this design flaw here by checking if we will make forward progress
  626. // (i.e. we will send > 0 response bytes) on a continued request. If not, we must have
  627. // run into the above situation and we tell the peer an error occurred.
  628. //
  629. // TODO(sharvil): rewrite SDP server.
  630. if (is_cont && len_to_send == 0) {
  631. sdpu_build_n_send_error(p_ccb, trans_num, SDP_INVALID_CONT_STATE, NULL);
  632. return;
  633. }
  634. /* If first response, insert sequence header */
  635. if (!is_cont) {
  636. /* Get the total list length for requested uid and attribute sequence */
  637. p_ccb->list_len = sdpu_get_list_len(&uid_seq, &attr_seq_sav) + 3;
  638. /* Put in the sequence header (2 or 3 bytes) */
  639. if (p_ccb->list_len > 255) {
  640. p_ccb->rsp_list[0] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
  641. p_ccb->rsp_list[1] = (UINT8) ((p_ccb->list_len - 3) >> 8);
  642. p_ccb->rsp_list[2] = (UINT8) (p_ccb->list_len - 3);
  643. } else {
  644. cont_offset = 1;
  645. p_ccb->rsp_list[1] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  646. p_ccb->rsp_list[2] = (UINT8) (p_ccb->list_len - 3);
  647. p_ccb->list_len--;
  648. len_to_send--;
  649. }
  650. }
  651. /* Get a buffer to use to build the response */
  652. if ((p_buf = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
  653. SDP_TRACE_ERROR ("SDP - no buf for search rsp\n");
  654. return;
  655. }
  656. p_buf->offset = L2CAP_MIN_OFFSET;
  657. p_rsp = p_rsp_start = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  658. /* Start building a rsponse */
  659. UINT8_TO_BE_STREAM (p_rsp, SDP_PDU_SERVICE_SEARCH_ATTR_RSP);
  660. UINT16_TO_BE_STREAM (p_rsp, trans_num);
  661. /* Skip the parameter length, add it when we know the length */
  662. p_rsp_param_len = p_rsp;
  663. p_rsp += 2;
  664. /* Stream the list length to send */
  665. UINT16_TO_BE_STREAM (p_rsp, len_to_send);
  666. /* copy from rsp_list to the actual buffer to be sent */
  667. memcpy (p_rsp, &p_ccb->rsp_list[cont_offset], len_to_send);
  668. p_rsp += len_to_send;
  669. p_ccb->cont_offset += len_to_send;
  670. /* If anything left to send, continuation needed */
  671. if (p_ccb->cont_offset < p_ccb->list_len) {
  672. is_cont = TRUE;
  673. UINT8_TO_BE_STREAM (p_rsp, SDP_CONTINUATION_LEN);
  674. UINT16_TO_BE_STREAM (p_rsp, p_ccb->cont_offset);
  675. } else {
  676. UINT8_TO_BE_STREAM (p_rsp, 0);
  677. }
  678. /* Go back and put the parameter length into the buffer */
  679. rsp_param_len = p_rsp - p_rsp_param_len - 2;
  680. UINT16_TO_BE_STREAM (p_rsp_param_len, rsp_param_len);
  681. /* Set the length of the SDP data in the buffer */
  682. p_buf->len = p_rsp - p_rsp_start;
  683. /* Send the buffer through L2CAP */
  684. L2CA_DataWrite (p_ccb->connection_id, p_buf);
  685. }
  686. #endif /* SDP_SERVER_ENABLED == TRUE */