sdp_utils.c 30 KB

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