sdp_utils.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 SDP utility functions
  21. *
  22. ******************************************************************************/
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "allocator.h"
  26. #include "bt_defs.h"
  27. #include "bt_types.h"
  28. #include "l2cdefs.h"
  29. #include "hcidefs.h"
  30. #include "hcimsgs.h"
  31. #include "sdp_api.h"
  32. #include "sdpint.h"
  33. #include "btu.h"
  34. #if (SDP_INCLUDED == TRUE)
  35. static const UINT8 sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  36. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  37. };
  38. /*******************************************************************************
  39. **
  40. ** Function sdpu_find_ccb_by_cid
  41. **
  42. ** Description This function searches the CCB table for an entry with the
  43. ** passed CID.
  44. **
  45. ** Returns the CCB address, or NULL if not found.
  46. **
  47. *******************************************************************************/
  48. tCONN_CB *sdpu_find_ccb_by_cid (UINT16 cid)
  49. {
  50. UINT16 xx;
  51. tCONN_CB *p_ccb;
  52. /* Look through each connection control block */
  53. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  54. if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->connection_id == cid)) {
  55. return (p_ccb);
  56. }
  57. }
  58. /* If here, not found */
  59. return (NULL);
  60. }
  61. /*******************************************************************************
  62. **
  63. ** Function sdpu_find_ccb_by_db
  64. **
  65. ** Description This function searches the CCB table for an entry with the
  66. ** passed discovery db.
  67. **
  68. ** Returns the CCB address, or NULL if not found.
  69. **
  70. *******************************************************************************/
  71. tCONN_CB *sdpu_find_ccb_by_db (tSDP_DISCOVERY_DB *p_db)
  72. {
  73. #if SDP_CLIENT_ENABLED == TRUE
  74. UINT16 xx;
  75. tCONN_CB *p_ccb;
  76. if (p_db) {
  77. /* Look through each connection control block */
  78. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  79. if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->p_db == p_db)) {
  80. return (p_ccb);
  81. }
  82. }
  83. }
  84. #endif
  85. /* If here, not found */
  86. return (NULL);
  87. }
  88. /*******************************************************************************
  89. **
  90. ** Function sdpu_allocate_ccb
  91. **
  92. ** Description This function allocates a new CCB.
  93. **
  94. ** Returns CCB address, or NULL if none available.
  95. **
  96. *******************************************************************************/
  97. tCONN_CB *sdpu_allocate_ccb (void)
  98. {
  99. UINT16 xx;
  100. tCONN_CB *p_ccb;
  101. /* Look through each connection control block for a free one */
  102. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  103. if (p_ccb->con_state == SDP_STATE_IDLE) {
  104. memset (p_ccb, 0, sizeof (tCONN_CB));
  105. p_ccb->timer_entry.param = (UINT32) p_ccb;
  106. return (p_ccb);
  107. }
  108. }
  109. /* If here, no free CCB found */
  110. return (NULL);
  111. }
  112. /*******************************************************************************
  113. **
  114. ** Function sdpu_release_ccb
  115. **
  116. ** Description This function releases a CCB.
  117. **
  118. ** Returns void
  119. **
  120. *******************************************************************************/
  121. void sdpu_release_ccb (tCONN_CB *p_ccb)
  122. {
  123. /* Ensure timer is stopped */
  124. btu_stop_timer (&p_ccb->timer_entry);
  125. /* Drop any response pointer we may be holding */
  126. p_ccb->con_state = SDP_STATE_IDLE;
  127. #if SDP_CLIENT_ENABLED == TRUE
  128. p_ccb->is_attr_search = FALSE;
  129. #endif
  130. /* Free the response buffer */
  131. if (p_ccb->rsp_list) {
  132. SDP_TRACE_DEBUG("releasing SDP rsp_list\n");
  133. osi_free(p_ccb->rsp_list);
  134. p_ccb->rsp_list = NULL;
  135. }
  136. }
  137. /*******************************************************************************
  138. **
  139. ** Function sdpu_build_attrib_seq
  140. **
  141. ** Description This function builds an attribute sequence from the list of
  142. ** passed attributes. It is also passed the address of the output
  143. ** buffer.
  144. **
  145. ** Returns Pointer to next byte in the output buffer.
  146. **
  147. *******************************************************************************/
  148. UINT8 *sdpu_build_attrib_seq (UINT8 *p_out, UINT16 *p_attr, UINT16 num_attrs)
  149. {
  150. UINT16 xx;
  151. /* First thing is the data element header. See if the length fits 1 byte */
  152. /* If no attributes, assume a 4-byte wildcard */
  153. if (!p_attr) {
  154. xx = 5;
  155. } else {
  156. xx = num_attrs * 3;
  157. }
  158. if (xx > 255) {
  159. UINT8_TO_BE_STREAM (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
  160. UINT16_TO_BE_STREAM (p_out, xx);
  161. } else {
  162. UINT8_TO_BE_STREAM (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  163. UINT8_TO_BE_STREAM (p_out, xx);
  164. }
  165. /* If there are no attributes specified, assume caller wants wildcard */
  166. if (!p_attr) {
  167. UINT8_TO_BE_STREAM (p_out, (UINT_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
  168. UINT16_TO_BE_STREAM (p_out, 0);
  169. UINT16_TO_BE_STREAM (p_out, 0xFFFF);
  170. } else {
  171. /* Loop through and put in all the attributes(s) */
  172. for (xx = 0; xx < num_attrs; xx++, p_attr++) {
  173. UINT8_TO_BE_STREAM (p_out, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  174. UINT16_TO_BE_STREAM (p_out, *p_attr);
  175. }
  176. }
  177. return (p_out);
  178. }
  179. /*******************************************************************************
  180. **
  181. ** Function sdpu_build_attrib_entry
  182. **
  183. ** Description This function builds an attribute entry from the passed
  184. ** attribute record. It is also passed the address of the output
  185. ** buffer.
  186. **
  187. ** Returns Pointer to next byte in the output buffer.
  188. **
  189. *******************************************************************************/
  190. UINT8 *sdpu_build_attrib_entry (UINT8 *p_out, tSDP_ATTRIBUTE *p_attr)
  191. {
  192. /* First, store the attribute ID. Goes as a UINT */
  193. UINT8_TO_BE_STREAM (p_out, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  194. UINT16_TO_BE_STREAM (p_out, p_attr->id);
  195. /* the attribute is in the db record.
  196. * assuming the attribute len is less than SDP_MAX_ATTR_LEN */
  197. switch (p_attr->type) {
  198. case TEXT_STR_DESC_TYPE: /* 4 */
  199. case DATA_ELE_SEQ_DESC_TYPE:/* 6 */
  200. case DATA_ELE_ALT_DESC_TYPE:/* 7 */
  201. case URL_DESC_TYPE: /* 8 */
  202. #if (SDP_MAX_ATTR_LEN > 0xFFFF)
  203. if (p_attr->len > 0xFFFF) {
  204. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_LONG);
  205. UINT32_TO_BE_STREAM (p_out, p_attr->len);
  206. } else
  207. #endif /* 0xFFFF - 0xFF */
  208. #if (SDP_MAX_ATTR_LEN > 0xFF)
  209. if (p_attr->len > 0xFF) {
  210. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_WORD);
  211. UINT16_TO_BE_STREAM (p_out, p_attr->len);
  212. } else
  213. #endif /* 0xFF and less*/
  214. {
  215. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_BYTE);
  216. UINT8_TO_BE_STREAM (p_out, p_attr->len);
  217. }
  218. if (p_attr->value_ptr != NULL) {
  219. ARRAY_TO_BE_STREAM (p_out, p_attr->value_ptr, (int)p_attr->len);
  220. }
  221. return (p_out);
  222. }
  223. /* Now, store the attribute value */
  224. switch (p_attr->len) {
  225. case 1:
  226. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_ONE_BYTE);
  227. break;
  228. case 2:
  229. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_TWO_BYTES);
  230. break;
  231. case 4:
  232. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_FOUR_BYTES);
  233. break;
  234. case 8:
  235. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_EIGHT_BYTES);
  236. break;
  237. case 16:
  238. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_SIXTEEN_BYTES);
  239. break;
  240. default:
  241. UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_BYTE);
  242. UINT8_TO_BE_STREAM (p_out, p_attr->len);
  243. break;
  244. }
  245. if (p_attr->value_ptr != NULL) {
  246. ARRAY_TO_BE_STREAM (p_out, p_attr->value_ptr, (int)p_attr->len);
  247. }
  248. return (p_out);
  249. }
  250. /*******************************************************************************
  251. **
  252. ** Function sdpu_build_n_send_error
  253. **
  254. ** Description This function builds and sends an error packet.
  255. **
  256. ** Returns void
  257. **
  258. *******************************************************************************/
  259. void sdpu_build_n_send_error (tCONN_CB *p_ccb, UINT16 trans_num, UINT16 error_code, char *p_error_text)
  260. {
  261. UINT8 *p_rsp, *p_rsp_start, *p_rsp_param_len;
  262. UINT16 rsp_param_len;
  263. BT_HDR *p_buf;
  264. SDP_TRACE_WARNING ("SDP - sdpu_build_n_send_error code: 0x%x CID: 0x%x\n",
  265. error_code, p_ccb->connection_id);
  266. /* Get a buffer to use to build and send the packet to L2CAP */
  267. if ((p_buf = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
  268. SDP_TRACE_ERROR ("SDP - no buf for err msg\n");
  269. return;
  270. }
  271. p_buf->offset = L2CAP_MIN_OFFSET;
  272. p_rsp = p_rsp_start = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  273. UINT8_TO_BE_STREAM (p_rsp, SDP_PDU_ERROR_RESPONSE);
  274. UINT16_TO_BE_STREAM (p_rsp, trans_num);
  275. /* Skip the parameter length, we need to add it at the end */
  276. p_rsp_param_len = p_rsp;
  277. p_rsp += 2;
  278. UINT16_TO_BE_STREAM (p_rsp, error_code);
  279. /* Unplugfest example traces do not have any error text */
  280. if (p_error_text) {
  281. ARRAY_TO_BE_STREAM (p_rsp, p_error_text, (int) strlen (p_error_text));
  282. }
  283. /* Go back and put the parameter length into the buffer */
  284. rsp_param_len = p_rsp - p_rsp_param_len - 2;
  285. UINT16_TO_BE_STREAM (p_rsp_param_len, rsp_param_len);
  286. /* Set the length of the SDP data in the buffer */
  287. p_buf->len = p_rsp - p_rsp_start;
  288. /* Send the buffer through L2CAP */
  289. L2CA_DataWrite (p_ccb->connection_id, p_buf);
  290. }
  291. /*******************************************************************************
  292. **
  293. ** Function sdpu_extract_uid_seq
  294. **
  295. ** Description This function extracts a UUID sequence from the passed input
  296. ** buffer, and puts it into the passed output list.
  297. **
  298. ** Returns Pointer to next byte in the input buffer after the sequence.
  299. **
  300. *******************************************************************************/
  301. UINT8 *sdpu_extract_uid_seq (UINT8 *p, UINT16 param_len, tSDP_UUID_SEQ *p_seq)
  302. {
  303. UINT8 *p_seq_end;
  304. UINT8 descr, type, size;
  305. UINT32 seq_len, uuid_len;
  306. /* Assume none found */
  307. p_seq->num_uids = 0;
  308. /* A UID sequence is composed of a bunch of UIDs. */
  309. BE_STREAM_TO_UINT8 (descr, p);
  310. type = descr >> 3;
  311. size = descr & 7;
  312. if (type != DATA_ELE_SEQ_DESC_TYPE) {
  313. return (NULL);
  314. }
  315. switch (size) {
  316. case SIZE_TWO_BYTES:
  317. seq_len = 2;
  318. break;
  319. case SIZE_FOUR_BYTES:
  320. seq_len = 4;
  321. break;
  322. case SIZE_SIXTEEN_BYTES:
  323. seq_len = 16;
  324. break;
  325. case SIZE_IN_NEXT_BYTE:
  326. BE_STREAM_TO_UINT8 (seq_len, p);
  327. break;
  328. case SIZE_IN_NEXT_WORD:
  329. BE_STREAM_TO_UINT16 (seq_len, p);
  330. break;
  331. case SIZE_IN_NEXT_LONG:
  332. BE_STREAM_TO_UINT32 (seq_len, p);
  333. break;
  334. default:
  335. return (NULL);
  336. }
  337. if (seq_len >= param_len) {
  338. return (NULL);
  339. }
  340. p_seq_end = p + seq_len;
  341. /* Loop through, extracting the UIDs */
  342. for ( ; p < p_seq_end ; ) {
  343. BE_STREAM_TO_UINT8 (descr, p);
  344. type = descr >> 3;
  345. size = descr & 7;
  346. if (type != UUID_DESC_TYPE) {
  347. return (NULL);
  348. }
  349. switch (size) {
  350. case SIZE_TWO_BYTES:
  351. uuid_len = 2;
  352. break;
  353. case SIZE_FOUR_BYTES:
  354. uuid_len = 4;
  355. break;
  356. case SIZE_SIXTEEN_BYTES:
  357. uuid_len = 16;
  358. break;
  359. case SIZE_IN_NEXT_BYTE:
  360. BE_STREAM_TO_UINT8 (uuid_len, p);
  361. break;
  362. case SIZE_IN_NEXT_WORD:
  363. BE_STREAM_TO_UINT16 (uuid_len, p);
  364. break;
  365. case SIZE_IN_NEXT_LONG:
  366. BE_STREAM_TO_UINT32 (uuid_len, p);
  367. break;
  368. default:
  369. return (NULL);
  370. }
  371. /* If UUID length is valid, copy it across */
  372. if ((uuid_len == 2) || (uuid_len == 4) || (uuid_len == 16)) {
  373. p_seq->uuid_entry[p_seq->num_uids].len = (UINT16) uuid_len;
  374. BE_STREAM_TO_ARRAY (p, p_seq->uuid_entry[p_seq->num_uids].value, (int)uuid_len);
  375. p_seq->num_uids++;
  376. } else {
  377. return (NULL);
  378. }
  379. /* We can only do so many */
  380. if (p_seq->num_uids >= MAX_UUIDS_PER_SEQ) {
  381. return (NULL);
  382. }
  383. }
  384. if (p != p_seq_end) {
  385. return (NULL);
  386. }
  387. return (p);
  388. }
  389. /*******************************************************************************
  390. **
  391. ** Function sdpu_extract_attr_seq
  392. **
  393. ** Description This function extracts an attribute sequence from the passed
  394. ** input buffer, and puts it into the passed output list.
  395. **
  396. ** Returns Pointer to next byte in the input buffer after the sequence.
  397. **
  398. *******************************************************************************/
  399. UINT8 *sdpu_extract_attr_seq (UINT8 *p, UINT16 param_len, tSDP_ATTR_SEQ *p_seq)
  400. {
  401. UINT8 *p_end_list;
  402. UINT8 descr, type, size;
  403. UINT32 list_len, attr_len;
  404. /* Assume none found */
  405. p_seq->num_attr = 0;
  406. /* Get attribute sequence info */
  407. BE_STREAM_TO_UINT8 (descr, p);
  408. type = descr >> 3;
  409. size = descr & 7;
  410. if (type != DATA_ELE_SEQ_DESC_TYPE) {
  411. return (p);
  412. }
  413. switch (size) {
  414. case SIZE_IN_NEXT_BYTE:
  415. BE_STREAM_TO_UINT8 (list_len, p);
  416. break;
  417. case SIZE_IN_NEXT_WORD:
  418. BE_STREAM_TO_UINT16 (list_len, p);
  419. break;
  420. case SIZE_IN_NEXT_LONG:
  421. BE_STREAM_TO_UINT32 (list_len, p);
  422. break;
  423. default:
  424. return (p);
  425. }
  426. if (list_len > param_len) {
  427. return (p);
  428. }
  429. p_end_list = p + list_len;
  430. /* Loop through, extracting the attribute IDs */
  431. for ( ; p < p_end_list ; ) {
  432. BE_STREAM_TO_UINT8 (descr, p);
  433. type = descr >> 3;
  434. size = descr & 7;
  435. if (type != UINT_DESC_TYPE) {
  436. return (p);
  437. }
  438. switch (size) {
  439. case SIZE_TWO_BYTES:
  440. attr_len = 2;
  441. break;
  442. case SIZE_FOUR_BYTES:
  443. attr_len = 4;
  444. break;
  445. case SIZE_IN_NEXT_BYTE:
  446. BE_STREAM_TO_UINT8 (attr_len, p);
  447. break;
  448. case SIZE_IN_NEXT_WORD:
  449. BE_STREAM_TO_UINT16 (attr_len, p);
  450. break;
  451. case SIZE_IN_NEXT_LONG:
  452. BE_STREAM_TO_UINT32 (attr_len, p);
  453. break;
  454. default:
  455. return (NULL);
  456. break;
  457. }
  458. /* Attribute length must be 2-bytes or 4-bytes for a paired entry. */
  459. if (attr_len == 2) {
  460. BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].start, p);
  461. p_seq->attr_entry[p_seq->num_attr].end = p_seq->attr_entry[p_seq->num_attr].start;
  462. } else if (attr_len == 4) {
  463. BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].start, p);
  464. BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].end, p);
  465. } else {
  466. return (NULL);
  467. }
  468. /* We can only do so many */
  469. if (++p_seq->num_attr >= MAX_ATTR_PER_SEQ) {
  470. return (NULL);
  471. }
  472. }
  473. return (p);
  474. }
  475. /*******************************************************************************
  476. **
  477. ** Function sdpu_get_len_from_type
  478. **
  479. ** Description This function gets the length
  480. **
  481. ** Returns void
  482. **
  483. *******************************************************************************/
  484. UINT8 *sdpu_get_len_from_type (UINT8 *p, UINT8 type, UINT32 *p_len)
  485. {
  486. UINT8 u8;
  487. UINT16 u16;
  488. UINT32 u32;
  489. switch (type & 7) {
  490. case SIZE_ONE_BYTE:
  491. *p_len = 1;
  492. break;
  493. case SIZE_TWO_BYTES:
  494. *p_len = 2;
  495. break;
  496. case SIZE_FOUR_BYTES:
  497. *p_len = 4;
  498. break;
  499. case SIZE_EIGHT_BYTES:
  500. *p_len = 8;
  501. break;
  502. case SIZE_SIXTEEN_BYTES:
  503. *p_len = 16;
  504. break;
  505. case SIZE_IN_NEXT_BYTE:
  506. BE_STREAM_TO_UINT8 (u8, p);
  507. *p_len = u8;
  508. break;
  509. case SIZE_IN_NEXT_WORD:
  510. BE_STREAM_TO_UINT16 (u16, p);
  511. *p_len = u16;
  512. break;
  513. case SIZE_IN_NEXT_LONG:
  514. BE_STREAM_TO_UINT32 (u32, p);
  515. *p_len = (UINT16) u32;
  516. break;
  517. }
  518. return (p);
  519. }
  520. /*******************************************************************************
  521. **
  522. ** Function sdpu_is_base_uuid
  523. **
  524. ** Description This function checks a 128-bit UUID with the base to see if
  525. ** it matches. Only the last 12 bytes are compared.
  526. **
  527. ** Returns TRUE if matched, else FALSE
  528. **
  529. *******************************************************************************/
  530. BOOLEAN sdpu_is_base_uuid (UINT8 *p_uuid)
  531. {
  532. UINT16 xx;
  533. for (xx = 4; xx < MAX_UUID_SIZE; xx++)
  534. if (p_uuid[xx] != sdp_base_uuid[xx]) {
  535. return (FALSE);
  536. }
  537. /* If here, matched */
  538. return (TRUE);
  539. }
  540. /*******************************************************************************
  541. **
  542. ** Function sdpu_compare_uuid_arrays
  543. **
  544. ** Description This function compares 2 BE UUIDs. If needed, they are expanded
  545. ** to 128-bit UUIDs, then compared.
  546. **
  547. ** NOTE it is assumed that the arrays are in Big Endian format
  548. **
  549. ** Returns TRUE if matched, else FALSE
  550. **
  551. *******************************************************************************/
  552. BOOLEAN sdpu_compare_uuid_arrays (UINT8 *p_uuid1, UINT32 len1, UINT8 *p_uuid2, UINT16 len2)
  553. {
  554. UINT8 nu1[MAX_UUID_SIZE];
  555. UINT8 nu2[MAX_UUID_SIZE];
  556. if ( ((len1 != 2) && (len1 != 4) && (len1 != 16)) ||
  557. ((len2 != 2) && (len2 != 4) && (len2 != 16)) ) {
  558. SDP_TRACE_ERROR("%s: invalid length\n", __func__);
  559. return FALSE;
  560. }
  561. /* If lengths match, do a straight compare */
  562. if (len1 == len2) {
  563. if (len1 == 2) {
  564. return ((p_uuid1[0] == p_uuid2[0]) && (p_uuid1[1] == p_uuid2[1]));
  565. }
  566. if (len1 == 4)
  567. return ( (p_uuid1[0] == p_uuid2[0]) && (p_uuid1[1] == p_uuid2[1])
  568. && (p_uuid1[2] == p_uuid2[2]) && (p_uuid1[3] == p_uuid2[3]) );
  569. else {
  570. return (memcmp (p_uuid1, p_uuid2, (size_t)len1) == 0);
  571. }
  572. } else if (len1 > len2) {
  573. /* If the len1 was 4-byte, (so len2 is 2-byte), compare on the fly */
  574. if (len1 == 4) {
  575. return ( (p_uuid1[0] == 0) && (p_uuid1[1] == 0)
  576. && (p_uuid1[2] == p_uuid2[0]) && (p_uuid1[3] == p_uuid2[1]) );
  577. } else {
  578. /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */
  579. memcpy (nu1, p_uuid1, MAX_UUID_SIZE);
  580. memcpy (nu2, sdp_base_uuid, MAX_UUID_SIZE);
  581. if (len2 == 4) {
  582. memcpy (nu2, p_uuid2, len2);
  583. } else if (len2 == 2) {
  584. memcpy (nu2 + 2, p_uuid2, len2);
  585. }
  586. return (memcmp (nu1, nu2, MAX_UUID_SIZE) == 0);
  587. }
  588. } else {
  589. /* len2 is greater than len1 */
  590. /* If the len2 was 4-byte, (so len1 is 2-byte), compare on the fly */
  591. if (len2 == 4) {
  592. return ( (p_uuid2[0] == 0) && (p_uuid2[1] == 0)
  593. && (p_uuid2[2] == p_uuid1[0]) && (p_uuid2[3] == p_uuid1[1]) );
  594. } else {
  595. /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */
  596. memcpy (nu2, p_uuid2, MAX_UUID_SIZE);
  597. memcpy (nu1, sdp_base_uuid, MAX_UUID_SIZE);
  598. if (len1 == 4) {
  599. memcpy (nu1, p_uuid1, (size_t)len1);
  600. } else if (len1 == 2) {
  601. memcpy (nu1 + 2, p_uuid1, (size_t)len1);
  602. }
  603. return (memcmp (nu1, nu2, MAX_UUID_SIZE) == 0);
  604. }
  605. }
  606. }
  607. /*******************************************************************************
  608. **
  609. ** Function sdpu_compare_bt_uuids
  610. **
  611. ** Description This function compares 2 BT UUID structures.
  612. **
  613. ** NOTE it is assumed that BT UUID structures are compressed to the
  614. ** smallest possible UUIDs (by removing the base SDP UUID)
  615. **
  616. ** Returns TRUE if matched, else FALSE
  617. **
  618. *******************************************************************************/
  619. BOOLEAN sdpu_compare_bt_uuids (tBT_UUID *p_uuid1, tBT_UUID *p_uuid2)
  620. {
  621. /* Lengths must match for BT UUIDs to match */
  622. if (p_uuid1->len == p_uuid2->len) {
  623. if (p_uuid1->len == 2) {
  624. return (p_uuid1->uu.uuid16 == p_uuid2->uu.uuid16);
  625. } else if (p_uuid1->len == 4) {
  626. return (p_uuid1->uu.uuid32 == p_uuid2->uu.uuid32);
  627. } else if (!memcmp (p_uuid1->uu.uuid128, p_uuid2->uu.uuid128, 16)) {
  628. return (TRUE);
  629. }
  630. }
  631. return (FALSE);
  632. }
  633. /*******************************************************************************
  634. **
  635. ** Function sdpu_compare_uuid_with_attr
  636. **
  637. ** Description This function compares a BT UUID structure with the UUID in an
  638. ** SDP attribute record. If needed, they are expanded to 128-bit
  639. ** UUIDs, then compared.
  640. **
  641. ** NOTE - it is assumed that BT UUID structures are compressed to the
  642. ** smallest possible UUIDs (by removing the base SDP UUID).
  643. ** - it is also assumed that the discovery atribute is compressed
  644. ** to the smallest possible
  645. **
  646. ** Returns TRUE if matched, else FALSE
  647. **
  648. *******************************************************************************/
  649. BOOLEAN sdpu_compare_uuid_with_attr (tBT_UUID *p_btuuid, tSDP_DISC_ATTR *p_attr)
  650. {
  651. UINT16 attr_len = SDP_DISC_ATTR_LEN (p_attr->attr_len_type);
  652. /* Since both UUIDs are compressed, lengths must match */
  653. if (p_btuuid->len != attr_len) {
  654. return (FALSE);
  655. }
  656. if (p_btuuid->len == 2) {
  657. return (BOOLEAN)(p_btuuid->uu.uuid16 == p_attr->attr_value.v.u16);
  658. } else if (p_btuuid->len == 4) {
  659. return (BOOLEAN)(p_btuuid->uu.uuid32 == p_attr->attr_value.v.u32);
  660. }
  661. /* coverity[overrun-buffer-arg] */
  662. /*
  663. Event overrun-buffer-arg: Overrun of static array "&p_attr->attr_value.v.array" of size 4 bytes by passing it to a function which indexes it with argument "16U" at byte position 15
  664. FALSE-POSITIVE error from Coverity test tool. Please do NOT remove following comment.
  665. False-positive: SDP uses scratch buffer to hold the attribute value.
  666. The actual size of tSDP_DISC_ATVAL does not matter.
  667. If the array size in tSDP_DISC_ATVAL is increase, we would increase the system RAM usage unnecessarily
  668. */
  669. else if (!memcmp (p_btuuid->uu.uuid128, (void *) p_attr->attr_value.v.array, MAX_UUID_SIZE)) {
  670. return (TRUE);
  671. }
  672. return (FALSE);
  673. }
  674. /*******************************************************************************
  675. **
  676. ** Function sdpu_sort_attr_list
  677. **
  678. ** Description sorts a list of attributes in numeric order from lowest to
  679. ** highest to conform to SDP specification
  680. **
  681. ** Returns void
  682. **
  683. *******************************************************************************/
  684. void sdpu_sort_attr_list( UINT16 num_attr, tSDP_DISCOVERY_DB *p_db )
  685. {
  686. UINT16 i;
  687. UINT16 x;
  688. /* Done if no attributes to sort */
  689. if (num_attr <= 1) {
  690. return;
  691. } else if (num_attr > SDP_MAX_ATTR_FILTERS) {
  692. num_attr = SDP_MAX_ATTR_FILTERS;
  693. }
  694. num_attr--; /* for the for-loop */
  695. for ( i = 0; i < num_attr; ) {
  696. if ( p_db->attr_filters[i] > p_db->attr_filters[i + 1] ) {
  697. /* swap the attribute IDs and start from the beginning */
  698. x = p_db->attr_filters[i];
  699. p_db->attr_filters[i] = p_db->attr_filters[i + 1];
  700. p_db->attr_filters[i + 1] = x;
  701. i = 0;
  702. } else {
  703. i++;
  704. }
  705. }
  706. }
  707. /*******************************************************************************
  708. **
  709. ** Function sdpu_get_list_len
  710. **
  711. ** Description gets the total list length in the sdp database for a given
  712. ** uid sequence and attr sequence
  713. **
  714. ** Returns void
  715. **
  716. *******************************************************************************/
  717. UINT16 sdpu_get_list_len(tSDP_UUID_SEQ *uid_seq, tSDP_ATTR_SEQ *attr_seq)
  718. {
  719. tSDP_RECORD *p_rec;
  720. UINT16 len = 0;
  721. UINT16 len1;
  722. for (p_rec = sdp_db_service_search (NULL, uid_seq); p_rec; p_rec = sdp_db_service_search (p_rec, uid_seq)) {
  723. len += 3;
  724. len1 = sdpu_get_attrib_seq_len(p_rec, attr_seq );
  725. if (len1 != 0) {
  726. len += len1;
  727. } else {
  728. len -= 3;
  729. }
  730. }
  731. return len;
  732. }
  733. /*******************************************************************************
  734. **
  735. ** Function sdpu_get_attrib_seq_len
  736. **
  737. ** Description gets the length of the specific attributes in a given
  738. ** sdp record
  739. **
  740. ** Returns void
  741. **
  742. *******************************************************************************/
  743. UINT16 sdpu_get_attrib_seq_len(tSDP_RECORD *p_rec, tSDP_ATTR_SEQ *attr_seq)
  744. {
  745. tSDP_ATTRIBUTE *p_attr;
  746. UINT16 len1 = 0;
  747. UINT16 xx;
  748. BOOLEAN is_range = FALSE;
  749. UINT16 start_id = 0, end_id = 0;
  750. for (xx = 0; xx < attr_seq->num_attr; xx++) {
  751. if (is_range == FALSE) {
  752. start_id = attr_seq->attr_entry[xx].start;
  753. end_id = attr_seq->attr_entry[xx].end;
  754. }
  755. p_attr = sdp_db_find_attr_in_rec (p_rec,
  756. start_id,
  757. end_id);
  758. if (p_attr) {
  759. len1 += sdpu_get_attrib_entry_len (p_attr);
  760. /* If doing a range, stick with this one till no more attributes found */
  761. if (start_id != end_id) {
  762. /* Update for next time through */
  763. start_id = p_attr->id + 1;
  764. xx--;
  765. is_range = TRUE;
  766. } else {
  767. is_range = FALSE;
  768. }
  769. } else {
  770. is_range = FALSE;
  771. }
  772. }
  773. return len1;
  774. }
  775. /*******************************************************************************
  776. **
  777. ** Function sdpu_get_attrib_entry_len
  778. **
  779. ** Description gets the length of a specific attribute
  780. **
  781. ** Returns void
  782. **
  783. *******************************************************************************/
  784. UINT16 sdpu_get_attrib_entry_len(tSDP_ATTRIBUTE *p_attr)
  785. {
  786. UINT16 len = 3;
  787. /* the attribute is in the db record.
  788. * assuming the attribute len is less than SDP_MAX_ATTR_LEN */
  789. switch (p_attr->type) {
  790. case TEXT_STR_DESC_TYPE: /* 4 */
  791. case DATA_ELE_SEQ_DESC_TYPE:/* 6 */
  792. case DATA_ELE_ALT_DESC_TYPE:/* 7 */
  793. case URL_DESC_TYPE: /* 8 */
  794. #if (SDP_MAX_ATTR_LEN > 0xFFFF)
  795. if (p_attr->len > 0xFFFF) {
  796. len += 5;
  797. } else
  798. #endif/* 0xFFFF - 0xFF */
  799. #if (SDP_MAX_ATTR_LEN > 0xFF)
  800. if (p_attr->len > 0xFF) {
  801. len += 3;
  802. } else
  803. #endif /* 0xFF and less*/
  804. {
  805. len += 2;
  806. }
  807. len += p_attr->len;
  808. return len;
  809. }
  810. /* Now, the attribute value */
  811. switch (p_attr->len) {
  812. case 1:
  813. case 2:
  814. case 4:
  815. case 8:
  816. case 16:
  817. len += 1;
  818. break;
  819. default:
  820. len += 2;
  821. break;
  822. }
  823. len += p_attr->len;
  824. return len;
  825. }
  826. /*******************************************************************************
  827. **
  828. ** Function sdpu_build_partial_attrib_entry
  829. **
  830. ** Description This function fills a buffer with partial attribute. It is
  831. ** assumed that the maximum size of any attribute is 256 bytes.
  832. **
  833. ** p_out: output buffer
  834. ** p_attr: attribute to be copied partially into p_out
  835. ** rem_len: num bytes to copy into p_out
  836. ** offset: current start offset within the attr that needs to be copied
  837. **
  838. ** Returns Pointer to next byte in the output buffer.
  839. ** offset is also updated
  840. **
  841. *******************************************************************************/
  842. UINT8 *sdpu_build_partial_attrib_entry (UINT8 *p_out, tSDP_ATTRIBUTE *p_attr, UINT16 len, UINT16 *offset)
  843. {
  844. UINT8 *p_attr_buff;
  845. UINT8 *p_tmp_attr;
  846. size_t len_to_copy;
  847. UINT16 attr_len;
  848. if ((p_attr_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN )) == NULL) {
  849. SDP_TRACE_ERROR("sdpu_build_partial_attrib_entry cannot get a buffer!\n");
  850. return NULL;
  851. }
  852. p_tmp_attr = p_attr_buff;
  853. sdpu_build_attrib_entry(p_tmp_attr, p_attr);
  854. attr_len = sdpu_get_attrib_entry_len(p_attr);
  855. len_to_copy = ((attr_len - *offset) < len) ? (attr_len - *offset) : len;
  856. memcpy(p_out, &p_attr_buff[*offset], len_to_copy);
  857. p_out = &p_out[len_to_copy];
  858. *offset += len_to_copy;
  859. osi_free(p_attr_buff);
  860. return p_out;
  861. }
  862. /*******************************************************************************
  863. **
  864. ** Function sdpu_uuid16_to_uuid128
  865. **
  866. ** Description This function converts UUID-16 to UUID-128 by including the base UUID
  867. **
  868. ** uuid16: 2-byte UUID
  869. ** p_uuid128: Expanded 128-bit UUID
  870. **
  871. ** Returns None
  872. **
  873. *******************************************************************************/
  874. void sdpu_uuid16_to_uuid128(UINT16 uuid16, UINT8 *p_uuid128)
  875. {
  876. UINT16 uuid16_bo;
  877. memset(p_uuid128, 0, 16);
  878. memcpy(p_uuid128, sdp_base_uuid, MAX_UUID_SIZE);
  879. uuid16_bo = ntohs(uuid16);
  880. memcpy(p_uuid128 + 2, &uuid16_bo, sizeof(uint16_t));
  881. }
  882. #endif ///SDP_INCLUDED == TRUE