sdp_db.c 35 KB

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