sdp_utils.c 30 KB

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