sdp_utils.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 <netinet/in.h>
  26. //#include <stdio.h>
  27. #include "bt_defs.h"
  28. #include "gki.h"
  29. #include "bt_types.h"
  30. #include "l2cdefs.h"
  31. #include "hcidefs.h"
  32. #include "hcimsgs.h"
  33. #include "sdp_api.h"
  34. #include "sdpint.h"
  35. #include "btu.h"
  36. static const UINT8 sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  37. 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
  38. };
  39. /*******************************************************************************
  40. **
  41. ** Function sdpu_find_ccb_by_cid
  42. **
  43. ** Description This function searches the CCB table for an entry with the
  44. ** passed CID.
  45. **
  46. ** Returns the CCB address, or NULL if not found.
  47. **
  48. *******************************************************************************/
  49. tCONN_CB *sdpu_find_ccb_by_cid (UINT16 cid)
  50. {
  51. UINT16 xx;
  52. tCONN_CB *p_ccb;
  53. /* Look through each connection control block */
  54. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  55. if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->connection_id == cid)) {
  56. return (p_ccb);
  57. }
  58. }
  59. /* If here, not found */
  60. return (NULL);
  61. }
  62. /*******************************************************************************
  63. **
  64. ** Function sdpu_find_ccb_by_db
  65. **
  66. ** Description This function searches the CCB table for an entry with the
  67. ** passed discovery db.
  68. **
  69. ** Returns the CCB address, or NULL if not found.
  70. **
  71. *******************************************************************************/
  72. tCONN_CB *sdpu_find_ccb_by_db (tSDP_DISCOVERY_DB *p_db)
  73. {
  74. #if SDP_CLIENT_ENABLED == TRUE
  75. UINT16 xx;
  76. tCONN_CB *p_ccb;
  77. if (p_db) {
  78. /* Look through each connection control block */
  79. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  80. if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->p_db == p_db)) {
  81. return (p_ccb);
  82. }
  83. }
  84. }
  85. #endif
  86. /* If here, not found */
  87. return (NULL);
  88. }
  89. /*******************************************************************************
  90. **
  91. ** Function sdpu_allocate_ccb
  92. **
  93. ** Description This function allocates a new CCB.
  94. **
  95. ** Returns CCB address, or NULL if none available.
  96. **
  97. *******************************************************************************/
  98. tCONN_CB *sdpu_allocate_ccb (void)
  99. {
  100. UINT16 xx;
  101. tCONN_CB *p_ccb;
  102. /* Look through each connection control block for a free one */
  103. for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
  104. if (p_ccb->con_state == SDP_STATE_IDLE) {
  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 */
  125. btu_stop_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. GKI_freebuf(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 *)GKI_getpoolbuf (SDP_POOL_ID)) == 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 *) GKI_getbuf(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. GKI_freebuf(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. }