sdp_db.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains functions that handle the database
  21. *
  22. ******************************************************************************/
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include "bt_target.h"
  27. #include "gki.h"
  28. #include "l2cdefs.h"
  29. #include "hcidefs.h"
  30. #include "hcimsgs.h"
  31. #include "sdp_api.h"
  32. #include "sdpint.h"
  33. #if SDP_SERVER_ENABLED == TRUE
  34. /********************************************************************************/
  35. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  36. /********************************************************************************/
  37. static BOOLEAN find_uuid_in_seq (UINT8 *p , UINT32 seq_len, UINT8 *p_his_uuid,
  38. UINT16 his_len, int nest_level);
  39. /*******************************************************************************
  40. **
  41. ** Function sdp_db_service_search
  42. **
  43. ** Description This function searches for a record that contains the
  44. ** specified UIDs. It is passed either NULL to start at the
  45. ** beginning, or the previous record found.
  46. **
  47. ** Returns Pointer to the record, or NULL if not found.
  48. **
  49. *******************************************************************************/
  50. tSDP_RECORD *sdp_db_service_search (tSDP_RECORD *p_rec, tSDP_UUID_SEQ *p_seq)
  51. {
  52. UINT16 xx, yy;
  53. tSDP_ATTRIBUTE *p_attr;
  54. tSDP_RECORD *p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
  55. /* If NULL, start at the beginning, else start at the first specified record */
  56. if (!p_rec) {
  57. p_rec = &sdp_cb.server_db.record[0];
  58. } else {
  59. p_rec++;
  60. }
  61. /* Look through the records. The spec says that a match occurs if */
  62. /* the record contains all the passed UUIDs in it. */
  63. for ( ; p_rec < p_end; p_rec++) {
  64. for (yy = 0; yy < p_seq->num_uids; yy++) {
  65. p_attr = &p_rec->attribute[0];
  66. for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
  67. if (p_attr->type == UUID_DESC_TYPE) {
  68. if (sdpu_compare_uuid_arrays (p_attr->value_ptr, p_attr->len,
  69. &p_seq->uuid_entry[yy].value[0],
  70. p_seq->uuid_entry[yy].len)) {
  71. break;
  72. }
  73. } else if (p_attr->type == DATA_ELE_SEQ_DESC_TYPE) {
  74. if (find_uuid_in_seq (p_attr->value_ptr, p_attr->len,
  75. &p_seq->uuid_entry[yy].value[0],
  76. p_seq->uuid_entry[yy].len, 0)) {
  77. break;
  78. }
  79. }
  80. }
  81. /* If any UUID was not found, on to the next record */
  82. if (xx == p_rec->num_attributes) {
  83. break;
  84. }
  85. }
  86. /* If every UUID was found in the record, return the record */
  87. if (yy == p_seq->num_uids) {
  88. return (p_rec);
  89. }
  90. }
  91. /* If here, no more records found */
  92. return (NULL);
  93. }
  94. /*******************************************************************************
  95. **
  96. ** Function find_uuid_in_seq
  97. **
  98. ** Description This function searches a data element sequenct for a UUID.
  99. **
  100. ** Returns TRUE if found, else FALSE
  101. **
  102. *******************************************************************************/
  103. static BOOLEAN find_uuid_in_seq (UINT8 *p , UINT32 seq_len, UINT8 *p_uuid,
  104. UINT16 uuid_len, int nest_level)
  105. {
  106. UINT8 *p_end = p + seq_len;
  107. UINT8 type;
  108. UINT32 len;
  109. /* A little safety check to avoid excessive recursion */
  110. if (nest_level > 3) {
  111. return (FALSE);
  112. }
  113. while (p < p_end) {
  114. type = *p++;
  115. p = sdpu_get_len_from_type (p, type, &len);
  116. type = type >> 3;
  117. if (type == UUID_DESC_TYPE) {
  118. if (sdpu_compare_uuid_arrays (p, len, p_uuid, uuid_len)) {
  119. return (TRUE);
  120. }
  121. } else if (type == DATA_ELE_SEQ_DESC_TYPE) {
  122. if (find_uuid_in_seq (p, len, p_uuid, uuid_len, nest_level + 1)) {
  123. return (TRUE);
  124. }
  125. }
  126. p = p + len;
  127. }
  128. /* If here, failed to match */
  129. return (FALSE);
  130. }
  131. /*******************************************************************************
  132. **
  133. ** Function sdp_db_find_record
  134. **
  135. ** Description This function searches for a record with a specific handle
  136. ** It is passed the handle of the record.
  137. **
  138. ** Returns Pointer to the record, or NULL if not found.
  139. **
  140. *******************************************************************************/
  141. tSDP_RECORD *sdp_db_find_record (UINT32 handle)
  142. {
  143. tSDP_RECORD *p_rec;
  144. tSDP_RECORD *p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
  145. /* Look through the records for the caller's handle */
  146. for (p_rec = &sdp_cb.server_db.record[0]; p_rec < p_end; p_rec++) {
  147. if (p_rec->record_handle == handle) {
  148. return (p_rec);
  149. }
  150. }
  151. /* Record with that handle not found. */
  152. return (NULL);
  153. }
  154. /*******************************************************************************
  155. **
  156. ** Function sdp_db_find_attr_in_rec
  157. **
  158. ** Description This function searches a record for specific attributes.
  159. ** It is passed a pointer to the record. If the record contains
  160. ** the specified attribute, (the caller may specify be a range
  161. ** of attributes), the attribute is returned.
  162. **
  163. ** Returns Pointer to the attribute, or NULL if not found.
  164. **
  165. *******************************************************************************/
  166. tSDP_ATTRIBUTE *sdp_db_find_attr_in_rec (tSDP_RECORD *p_rec, UINT16 start_attr,
  167. UINT16 end_attr)
  168. {
  169. tSDP_ATTRIBUTE *p_at;
  170. UINT16 xx;
  171. /* Note that the attributes in a record are assumed to be in sorted order */
  172. for (xx = 0, p_at = &p_rec->attribute[0]; xx < p_rec->num_attributes;
  173. xx++, p_at++) {
  174. if ((p_at->id >= start_attr) && (p_at->id <= end_attr)) {
  175. return (p_at);
  176. }
  177. }
  178. /* No matching attribute found */
  179. return (NULL);
  180. }
  181. /*******************************************************************************
  182. **
  183. ** Function sdp_compose_proto_list
  184. **
  185. ** Description This function is called to compose a data sequence from
  186. ** protocol element list struct pointer
  187. **
  188. ** Returns the length of the data sequence
  189. **
  190. *******************************************************************************/
  191. static int sdp_compose_proto_list( UINT8 *p, UINT16 num_elem,
  192. tSDP_PROTOCOL_ELEM *p_elem_list)
  193. {
  194. UINT16 xx, yy, len;
  195. BOOLEAN is_rfcomm_scn;
  196. UINT8 *p_head = p;
  197. UINT8 *p_len;
  198. /* First, build the protocol list. This consists of a set of data element
  199. ** sequences, one for each layer. Each layer sequence consists of layer's
  200. ** UUID and optional parameters
  201. */
  202. for (xx = 0; xx < num_elem; xx++, p_elem_list++) {
  203. len = 3 + (p_elem_list->num_params * 3);
  204. UINT8_TO_BE_STREAM (p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  205. p_len = p;
  206. *p++ = (UINT8) len;
  207. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  208. UINT16_TO_BE_STREAM (p, p_elem_list->protocol_uuid);
  209. if (p_elem_list->protocol_uuid == UUID_PROTOCOL_RFCOMM) {
  210. is_rfcomm_scn = TRUE;
  211. } else {
  212. is_rfcomm_scn = FALSE;
  213. }
  214. for (yy = 0; yy < p_elem_list->num_params; yy++) {
  215. if (is_rfcomm_scn) {
  216. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_ONE_BYTE);
  217. UINT8_TO_BE_STREAM (p, p_elem_list->params[yy]);
  218. *p_len -= 1;
  219. } else {
  220. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  221. UINT16_TO_BE_STREAM (p, p_elem_list->params[yy]);
  222. }
  223. }
  224. }
  225. return (p - p_head);
  226. }
  227. #endif /* SDP_SERVER_ENABLED == TRUE */
  228. /*******************************************************************************
  229. **
  230. ** Function SDP_CreateRecord
  231. **
  232. ** Description This function is called to create a record in the database.
  233. ** This would be through the SDP database maintenance API. The
  234. ** record is created empty, teh application should then call
  235. ** "add_attribute" to add the record's attributes.
  236. **
  237. ** Returns Record handle if OK, else 0.
  238. **
  239. *******************************************************************************/
  240. UINT32 SDP_CreateRecord (void)
  241. {
  242. #if SDP_SERVER_ENABLED == TRUE
  243. UINT32 handle;
  244. UINT8 buf[4];
  245. tSDP_DB *p_db = &sdp_cb.server_db;
  246. /* First, check if there is a free record */
  247. if (p_db->num_records < SDP_MAX_RECORDS) {
  248. memset (&p_db->record[p_db->num_records], 0,
  249. sizeof (tSDP_RECORD));
  250. /* We will use a handle of the first unreserved handle plus last record
  251. ** number + 1 */
  252. if (p_db->num_records) {
  253. handle = p_db->record[p_db->num_records - 1].record_handle + 1;
  254. } else {
  255. handle = 0x10000;
  256. }
  257. p_db->record[p_db->num_records].record_handle = handle;
  258. p_db->num_records++;
  259. SDP_TRACE_DEBUG("SDP_CreateRecord ok, num_records:%d\n", p_db->num_records);
  260. /* Add the first attribute (the handle) automatically */
  261. UINT32_TO_BE_FIELD (buf, handle);
  262. SDP_AddAttribute (handle, ATTR_ID_SERVICE_RECORD_HDL, UINT_DESC_TYPE,
  263. 4, buf);
  264. return (p_db->record[p_db->num_records - 1].record_handle);
  265. } else {
  266. SDP_TRACE_ERROR("SDP_CreateRecord fail, exceed maximum records:%d\n", SDP_MAX_RECORDS);
  267. }
  268. #endif
  269. return (0);
  270. }
  271. /*******************************************************************************
  272. **
  273. ** Function SDP_DeleteRecord
  274. **
  275. ** Description This function is called to add a record (or all records)
  276. ** from the database. This would be through the SDP database
  277. ** maintenance API.
  278. **
  279. ** If a record handle of 0 is passed, all records are deleted.
  280. **
  281. ** Returns TRUE if succeeded, else FALSE
  282. **
  283. *******************************************************************************/
  284. BOOLEAN SDP_DeleteRecord (UINT32 handle)
  285. {
  286. #if SDP_SERVER_ENABLED == TRUE
  287. UINT16 xx, yy, zz;
  288. tSDP_RECORD *p_rec = &sdp_cb.server_db.record[0];
  289. if (handle == 0 || sdp_cb.server_db.num_records == 0) {
  290. /* Delete all records in the database */
  291. sdp_cb.server_db.num_records = 0;
  292. /* require new DI record to be created in SDP_SetLocalDiRecord */
  293. sdp_cb.server_db.di_primary_handle = 0;
  294. return (TRUE);
  295. } else {
  296. /* Find the record in the database */
  297. for (xx = 0; xx < sdp_cb.server_db.num_records; xx++, p_rec++) {
  298. if (p_rec->record_handle == handle) {
  299. /* Found it. Shift everything up one */
  300. for (yy = xx; yy < sdp_cb.server_db.num_records; yy++, p_rec++) {
  301. *p_rec = *(p_rec + 1);
  302. /* Adjust the attribute value pointer for each attribute */
  303. for (zz = 0; zz < p_rec->num_attributes; zz++) {
  304. p_rec->attribute[zz].value_ptr -= sizeof(tSDP_RECORD);
  305. }
  306. }
  307. sdp_cb.server_db.num_records--;
  308. SDP_TRACE_DEBUG("SDP_DeleteRecord ok, num_records:%d\n", sdp_cb.server_db.num_records);
  309. /* if we're deleting the primary DI record, clear the */
  310. /* value in the control block */
  311. if ( sdp_cb.server_db.di_primary_handle == handle ) {
  312. sdp_cb.server_db.di_primary_handle = 0;
  313. }
  314. return (TRUE);
  315. }
  316. }
  317. }
  318. #endif
  319. return (FALSE);
  320. }
  321. /*******************************************************************************
  322. **
  323. ** Function SDP_AddAttribute
  324. **
  325. ** Description This function is called to add an attribute to a record.
  326. ** This would be through the SDP database maintenance API.
  327. ** If the attribute already exists in the record, it is replaced
  328. ** with the new value.
  329. **
  330. ** NOTE Attribute values must be passed as a Big Endian stream.
  331. **
  332. ** Returns TRUE if added OK, else FALSE
  333. **
  334. *******************************************************************************/
  335. BOOLEAN SDP_AddAttribute (UINT32 handle, UINT16 attr_id, UINT8 attr_type,
  336. UINT32 attr_len, UINT8 *p_val)
  337. {
  338. #if SDP_SERVER_ENABLED == TRUE
  339. UINT16 xx, yy, zz;
  340. tSDP_RECORD *p_rec = &sdp_cb.server_db.record[0];
  341. #if (BT_TRACE_VERBOSE == TRUE)
  342. if (sdp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) {
  343. if ((attr_type == UINT_DESC_TYPE) ||
  344. (attr_type == TWO_COMP_INT_DESC_TYPE) ||
  345. (attr_type == UUID_DESC_TYPE) ||
  346. (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
  347. (attr_type == DATA_ELE_ALT_DESC_TYPE)) {
  348. UINT8 num_array[400];
  349. UINT32 i;
  350. UINT32 len = (attr_len > 200) ? 200 : attr_len;
  351. num_array[0] = '\0';
  352. for (i = 0; i < len; i++) {
  353. sprintf((char *)&num_array[i * 2], "%02X", (UINT8)(p_val[i]));
  354. }
  355. SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%s\n",
  356. handle, attr_id, attr_type, attr_len, p_val, num_array);
  357. } else if (attr_type == BOOLEAN_DESC_TYPE) {
  358. SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%d\n",
  359. handle, attr_id, attr_type, attr_len, p_val, *p_val);
  360. } else {
  361. SDP_TRACE_DEBUG("SDP_AddAttribute: handle:%X, id:%04X, type:%d, len:%d, p_val:%p, *p_val:%s\n",
  362. handle, attr_id, attr_type, attr_len, p_val, p_val);
  363. }
  364. }
  365. #endif
  366. /* Find the record in the database */
  367. for (zz = 0; zz < sdp_cb.server_db.num_records; zz++, p_rec++) {
  368. if (p_rec->record_handle == handle) {
  369. tSDP_ATTRIBUTE *p_attr = &p_rec->attribute[0];
  370. /* Found the record. Now, see if the attribute already exists */
  371. for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
  372. /* The attribute exists. replace it */
  373. if (p_attr->id == attr_id) {
  374. SDP_DeleteAttribute (handle, attr_id);
  375. break;
  376. }
  377. if (p_attr->id > attr_id) {
  378. break;
  379. }
  380. }
  381. if (p_rec->num_attributes == SDP_MAX_REC_ATTR) {
  382. return (FALSE);
  383. }
  384. /* If not found, see if we can allocate a new entry */
  385. if (xx == p_rec->num_attributes) {
  386. p_attr = &p_rec->attribute[p_rec->num_attributes];
  387. } else {
  388. /* Since the attributes are kept in sorted order, insert ours here */
  389. for (yy = p_rec->num_attributes; yy > xx; yy--) {
  390. p_rec->attribute[yy] = p_rec->attribute[yy - 1];
  391. }
  392. }
  393. p_attr->id = attr_id;
  394. p_attr->type = attr_type;
  395. p_attr->len = attr_len;
  396. if (p_rec->free_pad_ptr + attr_len >= SDP_MAX_PAD_LEN) {
  397. /* do truncate only for text string type descriptor */
  398. if (attr_type == TEXT_STR_DESC_TYPE) {
  399. SDP_TRACE_WARNING("SDP_AddAttribute: attr_len:%d too long. truncate to (%d)\n",
  400. attr_len, SDP_MAX_PAD_LEN - p_rec->free_pad_ptr );
  401. attr_len = SDP_MAX_PAD_LEN - p_rec->free_pad_ptr;
  402. p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr] = '\0';
  403. p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr + 1] = '\0';
  404. } else {
  405. attr_len = 0;
  406. }
  407. }
  408. if ((attr_len > 0) && (p_val != 0)) {
  409. p_attr->len = attr_len;
  410. memcpy (&p_rec->attr_pad[p_rec->free_pad_ptr], p_val, (size_t)attr_len);
  411. p_attr->value_ptr = &p_rec->attr_pad[p_rec->free_pad_ptr];
  412. p_rec->free_pad_ptr += attr_len;
  413. } else if ((attr_len == 0 && p_attr->len != 0) || /* if truncate to 0 length, simply don't add */
  414. p_val == 0) {
  415. SDP_TRACE_ERROR("SDP_AddAttribute fail, length exceed maximum: ID %d: attr_len:%d \n",
  416. attr_id, attr_len );
  417. p_attr->id = p_attr->type = p_attr->len = 0;
  418. return (FALSE);
  419. }
  420. p_rec->num_attributes++;
  421. return (TRUE);
  422. }
  423. }
  424. #endif
  425. return (FALSE);
  426. }
  427. /*******************************************************************************
  428. **
  429. ** Function SDP_AddSequence
  430. **
  431. ** Description This function is called to add a sequence to a record.
  432. ** This would be through the SDP database maintenance API.
  433. ** If the sequence already exists in the record, it is replaced
  434. ** with the new sequence.
  435. **
  436. ** NOTE Element values must be passed as a Big Endian stream.
  437. **
  438. ** Returns TRUE if added OK, else FALSE
  439. **
  440. *******************************************************************************/
  441. BOOLEAN SDP_AddSequence (UINT32 handle, UINT16 attr_id, UINT16 num_elem,
  442. UINT8 type[], UINT8 len[], UINT8 *p_val[])
  443. {
  444. #if SDP_SERVER_ENABLED == TRUE
  445. UINT16 xx;
  446. UINT8 *p_buff;
  447. UINT8 *p;
  448. UINT8 *p_head;
  449. BOOLEAN result;
  450. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
  451. SDP_TRACE_ERROR("SDP_AddSequence cannot get a buffer!\n");
  452. return (FALSE);
  453. }
  454. p = p_buff;
  455. /* First, build the sequence */
  456. for (xx = 0; xx < num_elem; xx++) {
  457. p_head = p;
  458. switch (len[xx]) {
  459. case 1:
  460. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_ONE_BYTE);
  461. break;
  462. case 2:
  463. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_TWO_BYTES);
  464. break;
  465. case 4:
  466. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_FOUR_BYTES);
  467. break;
  468. case 8:
  469. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_EIGHT_BYTES);
  470. break;
  471. case 16:
  472. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_SIXTEEN_BYTES);
  473. break;
  474. default:
  475. UINT8_TO_BE_STREAM (p, (type[xx] << 3) | SIZE_IN_NEXT_BYTE);
  476. UINT8_TO_BE_STREAM (p, len[xx]);
  477. break;
  478. }
  479. ARRAY_TO_BE_STREAM (p, p_val[xx], len[xx]);
  480. if (p - p_buff > SDP_MAX_ATTR_LEN) {
  481. /* go back to before we add this element */
  482. p = p_head;
  483. if (p_head == p_buff) {
  484. /* the first element exceed the max length */
  485. SDP_TRACE_ERROR ("SDP_AddSequence - too long(attribute is not added)!!\n");
  486. GKI_freebuf(p_buff);
  487. return FALSE;
  488. } else {
  489. SDP_TRACE_ERROR ("SDP_AddSequence - too long, add %d elements of %d\n", xx, num_elem);
  490. }
  491. break;
  492. }
  493. }
  494. result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
  495. GKI_freebuf(p_buff);
  496. return result;
  497. #else /* SDP_SERVER_ENABLED == FALSE */
  498. return (FALSE);
  499. #endif
  500. }
  501. /*******************************************************************************
  502. **
  503. ** Function SDP_AddUuidSequence
  504. **
  505. ** Description This function is called to add a UUID sequence to a record.
  506. ** This would be through the SDP database maintenance API.
  507. ** If the sequence already exists in the record, it is replaced
  508. ** with the new sequence.
  509. **
  510. ** Returns TRUE if added OK, else FALSE
  511. **
  512. *******************************************************************************/
  513. BOOLEAN SDP_AddUuidSequence (UINT32 handle, UINT16 attr_id, UINT16 num_uuids,
  514. UINT16 *p_uuids)
  515. {
  516. #if SDP_SERVER_ENABLED == TRUE
  517. UINT16 xx;
  518. UINT8 *p_buff;
  519. UINT8 *p;
  520. INT32 max_len = SDP_MAX_ATTR_LEN - 3;
  521. BOOLEAN result;
  522. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
  523. SDP_TRACE_ERROR("SDP_AddUuidSequence cannot get a buffer!\n");
  524. return (FALSE);
  525. }
  526. p = p_buff;
  527. /* First, build the sequence */
  528. for (xx = 0; xx < num_uuids ; xx++, p_uuids++) {
  529. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  530. UINT16_TO_BE_STREAM (p, *p_uuids);
  531. if ((p - p_buff) > max_len) {
  532. SDP_TRACE_WARNING ("SDP_AddUuidSequence - too long, add %d uuids of %d\n", xx, num_uuids);
  533. break;
  534. }
  535. }
  536. result = SDP_AddAttribute (handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
  537. GKI_freebuf(p_buff);
  538. return result;
  539. #else /* SDP_SERVER_ENABLED == FALSE */
  540. return (FALSE);
  541. #endif
  542. }
  543. /*******************************************************************************
  544. **
  545. ** Function SDP_AddProtocolList
  546. **
  547. ** Description This function is called to add a protocol descriptor list to
  548. ** a record. This would be through the SDP database maintenance API.
  549. ** If the protocol list already exists in the record, it is replaced
  550. ** with the new list.
  551. **
  552. ** Returns TRUE if added OK, else FALSE
  553. **
  554. *******************************************************************************/
  555. BOOLEAN SDP_AddProtocolList (UINT32 handle, UINT16 num_elem,
  556. tSDP_PROTOCOL_ELEM *p_elem_list)
  557. {
  558. #if SDP_SERVER_ENABLED == TRUE
  559. UINT8 *p_buff;
  560. int offset;
  561. BOOLEAN result;
  562. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
  563. SDP_TRACE_ERROR("SDP_AddProtocolList cannot get a buffer!\n");
  564. return (FALSE);
  565. }
  566. offset = sdp_compose_proto_list(p_buff, num_elem, p_elem_list);
  567. result = SDP_AddAttribute (handle, ATTR_ID_PROTOCOL_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE, (UINT32) offset, p_buff);
  568. GKI_freebuf(p_buff);
  569. return result;
  570. #else /* SDP_SERVER_ENABLED == FALSE */
  571. return (FALSE);
  572. #endif
  573. }
  574. /*******************************************************************************
  575. **
  576. ** Function SDP_AddAdditionProtoLists
  577. **
  578. ** Description This function is called to add a protocol descriptor list to
  579. ** a record. This would be through the SDP database maintenance API.
  580. ** If the protocol list already exists in the record, it is replaced
  581. ** with the new list.
  582. **
  583. ** Returns TRUE if added OK, else FALSE
  584. **
  585. *******************************************************************************/
  586. BOOLEAN SDP_AddAdditionProtoLists (UINT32 handle, UINT16 num_elem,
  587. tSDP_PROTO_LIST_ELEM *p_proto_list)
  588. {
  589. #if SDP_SERVER_ENABLED == TRUE
  590. UINT16 xx;
  591. UINT8 *p_buff;
  592. UINT8 *p;
  593. UINT8 *p_len;
  594. int offset;
  595. BOOLEAN result;
  596. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
  597. SDP_TRACE_ERROR("SDP_AddAdditionProtoLists cannot get a buffer!\n");
  598. return (FALSE);
  599. }
  600. p = p_buff;
  601. /* for each ProtocolDescriptorList */
  602. for (xx = 0; xx < num_elem; xx++, p_proto_list++) {
  603. UINT8_TO_BE_STREAM (p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  604. p_len = p++;
  605. offset = sdp_compose_proto_list(p, p_proto_list->num_elems,
  606. p_proto_list->list_elem);
  607. p += offset;
  608. *p_len = (UINT8)(p - p_len - 1);
  609. }
  610. result = SDP_AddAttribute (handle, ATTR_ID_ADDITION_PROTO_DESC_LISTS, DATA_ELE_SEQ_DESC_TYPE,
  611. (UINT32) (p - p_buff), p_buff);
  612. GKI_freebuf(p_buff);
  613. return result;
  614. #else /* SDP_SERVER_ENABLED == FALSE */
  615. return (FALSE);
  616. #endif
  617. }
  618. /*******************************************************************************
  619. **
  620. ** Function SDP_AddProfileDescriptorList
  621. **
  622. ** Description This function is called to add a profile descriptor list to
  623. ** a record. This would be through the SDP database maintenance API.
  624. ** If the version already exists in the record, it is replaced
  625. ** with the new one.
  626. **
  627. ** Returns TRUE if added OK, else FALSE
  628. **
  629. *******************************************************************************/
  630. BOOLEAN SDP_AddProfileDescriptorList (UINT32 handle, UINT16 profile_uuid,
  631. UINT16 version)
  632. {
  633. #if SDP_SERVER_ENABLED == TRUE
  634. UINT8 *p_buff;
  635. UINT8 *p;
  636. BOOLEAN result;
  637. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN)) == NULL) {
  638. SDP_TRACE_ERROR("SDP_AddProfileDescriptorList cannot get a buffer!\n");
  639. return (FALSE);
  640. }
  641. p = p_buff + 2;
  642. /* First, build the profile descriptor list. This consists of a data element sequence. */
  643. /* The sequence consists of profile's UUID and version number */
  644. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  645. UINT16_TO_BE_STREAM (p, profile_uuid);
  646. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  647. UINT16_TO_BE_STREAM (p, version);
  648. /* Add in type and length fields */
  649. *p_buff = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  650. *(p_buff + 1) = (UINT8) (p - (p_buff + 2));
  651. result = SDP_AddAttribute (handle, ATTR_ID_BT_PROFILE_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE, (UINT32) (p - p_buff), p_buff);
  652. GKI_freebuf(p_buff);
  653. return result;
  654. #else /* SDP_SERVER_ENABLED == FALSE */
  655. return (FALSE);
  656. #endif
  657. }
  658. /*******************************************************************************
  659. **
  660. ** Function SDP_AddLanguageBaseAttrIDList
  661. **
  662. ** Description This function is called to add a language base attr list to
  663. ** a record. This would be through the SDP database maintenance API.
  664. ** If the version already exists in the record, it is replaced
  665. ** with the new one.
  666. **
  667. ** Returns TRUE if added OK, else FALSE
  668. **
  669. *******************************************************************************/
  670. BOOLEAN SDP_AddLanguageBaseAttrIDList (UINT32 handle, UINT16 lang,
  671. UINT16 char_enc, UINT16 base_id)
  672. {
  673. #if SDP_SERVER_ENABLED == TRUE
  674. UINT8 *p_buff;
  675. UINT8 *p;
  676. BOOLEAN result;
  677. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN)) == NULL) {
  678. SDP_TRACE_ERROR("SDP_AddLanguageBaseAttrIDList cannot get a buffer!\n");
  679. return (FALSE);
  680. }
  681. p = p_buff;
  682. /* First, build the language base descriptor list. This consists of a data */
  683. /* element sequence. The sequence consists of 9 bytes (3 UINt16 fields) */
  684. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  685. UINT16_TO_BE_STREAM (p, lang);
  686. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  687. UINT16_TO_BE_STREAM (p, char_enc);
  688. UINT8_TO_BE_STREAM (p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  689. UINT16_TO_BE_STREAM (p, base_id);
  690. result = SDP_AddAttribute (handle, ATTR_ID_LANGUAGE_BASE_ATTR_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  691. (UINT32) (p - p_buff), p_buff);
  692. GKI_freebuf(p_buff);
  693. return result;
  694. #else /* SDP_SERVER_ENABLED == FALSE */
  695. return (FALSE);
  696. #endif
  697. }
  698. /*******************************************************************************
  699. **
  700. ** Function SDP_AddServiceClassIdList
  701. **
  702. ** Description This function is called to add a service list to a record.
  703. ** This would be through the SDP database maintenance API.
  704. ** If the service list already exists in the record, it is replaced
  705. ** with the new list.
  706. **
  707. ** Returns TRUE if added OK, else FALSE
  708. **
  709. *******************************************************************************/
  710. BOOLEAN SDP_AddServiceClassIdList (UINT32 handle, UINT16 num_services,
  711. UINT16 *p_service_uuids)
  712. {
  713. #if SDP_SERVER_ENABLED == TRUE
  714. UINT16 xx;
  715. UINT8 *p_buff;
  716. UINT8 *p;
  717. BOOLEAN result;
  718. if ((p_buff = (UINT8 *) GKI_getbuf(sizeof(UINT8) * SDP_MAX_ATTR_LEN * 2)) == NULL) {
  719. SDP_TRACE_ERROR("SDP_AddServiceClassIdList cannot get a buffer!\n");
  720. return (FALSE);
  721. }
  722. p = p_buff;
  723. for (xx = 0; xx < num_services; xx++, p_service_uuids++) {
  724. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
  725. UINT16_TO_BE_STREAM (p, *p_service_uuids);
  726. }
  727. result = SDP_AddAttribute (handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  728. (UINT32) (p - p_buff), p_buff);
  729. GKI_freebuf(p_buff);
  730. return result;
  731. #else /* SDP_SERVER_ENABLED == FALSE */
  732. return (FALSE);
  733. #endif
  734. }
  735. /*******************************************************************************
  736. **
  737. ** Function SDP_DeleteAttribute
  738. **
  739. ** Description This function is called to delete an attribute from a record.
  740. ** This would be through the SDP database maintenance API.
  741. **
  742. ** Returns TRUE if deleted OK, else FALSE if not found
  743. **
  744. *******************************************************************************/
  745. BOOLEAN SDP_DeleteAttribute (UINT32 handle, UINT16 attr_id)
  746. {
  747. #if SDP_SERVER_ENABLED == TRUE
  748. UINT16 xx, yy;
  749. tSDP_RECORD *p_rec = &sdp_cb.server_db.record[0];
  750. UINT8 *pad_ptr;
  751. UINT32 len; /* Number of bytes in the entry */
  752. /* Find the record in the database */
  753. for (xx = 0; xx < sdp_cb.server_db.num_records; xx++, p_rec++) {
  754. if (p_rec->record_handle == handle) {
  755. tSDP_ATTRIBUTE *p_attr = &p_rec->attribute[0];
  756. SDP_TRACE_API("Deleting attr_id 0x%04x for handle 0x%x\n", attr_id, handle);
  757. /* Found it. Now, find the attribute */
  758. for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
  759. if (p_attr->id == attr_id) {
  760. pad_ptr = p_attr->value_ptr;
  761. len = p_attr->len;
  762. if (len) {
  763. for (yy = 0; yy < p_rec->num_attributes; yy++) {
  764. if ( p_rec->attribute[yy].value_ptr > pad_ptr ) {
  765. p_rec->attribute[yy].value_ptr -= len;
  766. }
  767. }
  768. }
  769. /* Found it. Shift everything up one */
  770. p_rec->num_attributes--;
  771. for (yy = xx; yy < p_rec->num_attributes; yy++, p_attr++) {
  772. *p_attr = *(p_attr + 1);
  773. }
  774. /* adjust attribute values if needed */
  775. if (len) {
  776. xx = (p_rec->free_pad_ptr - ((pad_ptr + len) -
  777. &p_rec->attr_pad[0]));
  778. for ( yy = 0; yy < xx; yy++, pad_ptr++) {
  779. *pad_ptr = *(pad_ptr + len);
  780. }
  781. p_rec->free_pad_ptr -= len;
  782. }
  783. return (TRUE);
  784. }
  785. }
  786. }
  787. }
  788. #endif
  789. /* If here, not found */
  790. return (FALSE);
  791. }
  792. /*******************************************************************************
  793. **
  794. ** Function SDP_ReadRecord
  795. **
  796. ** Description This function is called to get the raw data of the record
  797. ** with the given handle from the database.
  798. **
  799. ** Returns -1, if the record is not found.
  800. ** Otherwise, the offset (0 or 1) to start of data in p_data.
  801. **
  802. ** The size of data copied into p_data is in *p_data_len.
  803. **
  804. *******************************************************************************/
  805. #if (SDP_RAW_DATA_INCLUDED == TRUE)
  806. INT32 SDP_ReadRecord(UINT32 handle, UINT8 *p_data, INT32 *p_data_len)
  807. {
  808. INT32 offset = -1; /* default to not found */
  809. #if SDP_SERVER_ENABLED == TRUE
  810. INT32 len = 0; /* Number of bytes in the entry */
  811. tSDP_RECORD *p_rec;
  812. UINT16 start = 0;
  813. UINT16 end = 0xffff;
  814. tSDP_ATTRIBUTE *p_attr;
  815. UINT16 rem_len;
  816. UINT8 *p_rsp;
  817. /* Find the record in the database */
  818. p_rec = sdp_db_find_record(handle);
  819. if (p_rec && p_data && p_data_len) {
  820. p_rsp = &p_data[3];
  821. while ( (p_attr = sdp_db_find_attr_in_rec (p_rec, start, end)) != NULL) {
  822. /* Check if attribute fits. Assume 3-byte value type/length */
  823. rem_len = *p_data_len - (UINT16) (p_rsp - p_data);
  824. if (p_attr->len > (UINT32)(rem_len - 6)) {
  825. break;
  826. }
  827. p_rsp = sdpu_build_attrib_entry (p_rsp, p_attr);
  828. /* next attr id */
  829. start = p_attr->id + 1;
  830. }
  831. len = (INT32) (p_rsp - p_data);
  832. /* Put in the sequence header (2 or 3 bytes) */
  833. if (len > 255) {
  834. offset = 0;
  835. p_data[0] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
  836. p_data[1] = (UINT8) ((len - 3) >> 8);
  837. p_data[2] = (UINT8) (len - 3);
  838. } else {
  839. offset = 1;
  840. p_data[1] = (UINT8) ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
  841. p_data[2] = (UINT8) (len - 3);
  842. len--;
  843. }
  844. *p_data_len = len;
  845. }
  846. #endif
  847. /* If here, not found */
  848. return (offset);
  849. }
  850. #endif