sdp_main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 the main SDP functions
  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/l2c_api.h"
  32. #include "stack/l2cdefs.h"
  33. #include "stack/btu.h"
  34. #include "stack/btm_api.h"
  35. #include "stack/sdp_api.h"
  36. #include "sdpint.h"
  37. #include "osi/list.h"
  38. #if (SDP_INCLUDED == TRUE)
  39. /********************************************************************************/
  40. /* G L O B A L S D P D A T A */
  41. /********************************************************************************/
  42. #if SDP_DYNAMIC_MEMORY == FALSE
  43. tSDP_CB sdp_cb;
  44. #else
  45. tSDP_CB *sdp_cb_ptr;
  46. #endif
  47. /********************************************************************************/
  48. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  49. /********************************************************************************/
  50. static void sdp_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm,
  51. UINT8 l2cap_id);
  52. static void sdp_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
  53. static void sdp_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
  54. static void sdp_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed);
  55. static void sdp_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg);
  56. #if SDP_CLIENT_ENABLED == TRUE
  57. static void sdp_connect_cfm (UINT16 l2cap_cid, UINT16 result);
  58. static void sdp_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
  59. #else
  60. #define sdp_connect_cfm NULL
  61. #define sdp_disconnect_cfm NULL
  62. #endif
  63. #if BT_SDP_BQB_INCLUDED
  64. static BOOLEAN s_sdp_bqb_disable_flag = FALSE;
  65. static BOOLEAN s_sdp_bqb_inact_timeout_flag = FALSE;
  66. BOOLEAN l2cap_bqb_ertm_mode_included_flag = FALSE;
  67. #endif /* BT_SDP_BQB_INCLUDED */
  68. /*******************************************************************************
  69. **
  70. ** Function sdp_bqb_disable_ctrl
  71. **
  72. ** Description Control the disable of bqb for SDP BQB test
  73. **
  74. ** Returns void
  75. **
  76. *******************************************************************************/
  77. #if BT_SDP_BQB_INCLUDED
  78. void sdp_bqb_disable_ctrl(BOOLEAN enable)
  79. {
  80. s_sdp_bqb_disable_flag = enable;
  81. }
  82. #endif /* BT_SDP_BQB_INCLUDED */
  83. /*******************************************************************************
  84. **
  85. ** Function sdp_bqb_inact_timeout_ctrl
  86. **
  87. ** Description Control the inactivity timeout for SDP BQB test
  88. **
  89. ** Returns void
  90. **
  91. *******************************************************************************/
  92. #if BT_SDP_BQB_INCLUDED
  93. void sdp_bqb_inact_timeout_ctrl(BOOLEAN enable)
  94. {
  95. s_sdp_bqb_inact_timeout_flag = enable;
  96. }
  97. #endif /* BT_SDP_BQB_INCLUDED */
  98. /*******************************************************************************
  99. **
  100. ** Function l2cap_bqb_ertm_mode_included_ctrl
  101. **
  102. ** Description Control the L2CAP flow control and retransmissions mode for SDP BQB test
  103. **
  104. ** Returns void
  105. **
  106. *******************************************************************************/
  107. #if BT_SDP_BQB_INCLUDED
  108. void l2cap_bqb_ertm_mode_included_ctrl(BOOLEAN enable)
  109. {
  110. l2cap_bqb_ertm_mode_included_flag = enable;
  111. }
  112. #endif /* BT_SDP_BQB_INCLUDED */
  113. /*******************************************************************************
  114. **
  115. ** Function sdp_init
  116. **
  117. ** Description This function initializes the SDP unit.
  118. **
  119. ** Returns void
  120. **
  121. *******************************************************************************/
  122. void sdp_init (void)
  123. {
  124. #if SDP_DYNAMIC_MEMORY
  125. sdp_cb_ptr = (tSDP_CB *)osi_malloc(sizeof(tSDP_CB));
  126. #endif /* #if SDP_DYNAMIC_MEMORY */
  127. /* Clears all structures and local SDP database (if Server is enabled) */
  128. memset (&sdp_cb, 0, sizeof (tSDP_CB));
  129. sdp_cb.server_db.p_record_list = list_new(osi_free_func);
  130. /* Initialize the L2CAP configuration. We only care about MTU and flush */
  131. sdp_cb.l2cap_my_cfg.mtu_present = TRUE;
  132. sdp_cb.l2cap_my_cfg.mtu = SDP_MTU_SIZE;
  133. sdp_cb.l2cap_my_cfg.flush_to_present = TRUE;
  134. sdp_cb.l2cap_my_cfg.flush_to = SDP_FLUSH_TO;
  135. #if BT_SDP_BQB_INCLUDED
  136. if (l2cap_bqb_ertm_mode_included_flag) {
  137. sdp_cb.l2cap_my_cfg.fcr_present = TRUE;
  138. sdp_cb.l2cap_my_cfg.fcr.mode = L2CAP_FCR_ERTM_MODE;
  139. sdp_cb.l2cap_my_cfg.fcr.tx_win_sz = 8;
  140. sdp_cb.l2cap_my_cfg.fcr.max_transmit = 0xff;
  141. sdp_cb.l2cap_my_cfg.fcr.rtrans_tout = 2000;
  142. sdp_cb.l2cap_my_cfg.fcr.mon_tout = 12000;
  143. sdp_cb.l2cap_my_cfg.fcr.mps = 672;
  144. }
  145. #endif /* BT_SDP_BQB_INCLUDED */
  146. sdp_cb.max_attr_list_size = SDP_MTU_SIZE - 16;
  147. sdp_cb.max_recs_per_search = SDP_MAX_DISC_SERVER_RECS;
  148. #if SDP_SERVER_ENABLED == TRUE
  149. /* Register with Security Manager for the specific security level */
  150. if (!BTM_SetSecurityLevel (FALSE, SDP_SERVICE_NAME, BTM_SEC_SERVICE_SDP_SERVER,
  151. SDP_SECURITY_LEVEL, SDP_PSM, 0, 0)) {
  152. SDP_TRACE_ERROR ("Security Registration Server failed\n");
  153. return;
  154. }
  155. #endif
  156. #if SDP_CLIENT_ENABLED == TRUE
  157. /* Register with Security Manager for the specific security level */
  158. if (!BTM_SetSecurityLevel (TRUE, SDP_SERVICE_NAME, BTM_SEC_SERVICE_SDP_SERVER,
  159. SDP_SECURITY_LEVEL, SDP_PSM, 0, 0)) {
  160. SDP_TRACE_ERROR ("Security Registration for Client failed\n");
  161. return;
  162. }
  163. #endif
  164. #if defined(SDP_INITIAL_TRACE_LEVEL)
  165. sdp_cb.trace_level = SDP_INITIAL_TRACE_LEVEL;
  166. #else
  167. sdp_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
  168. #endif
  169. sdp_cb.reg_info.pL2CA_ConnectInd_Cb = sdp_connect_ind;
  170. sdp_cb.reg_info.pL2CA_ConnectCfm_Cb = sdp_connect_cfm;
  171. sdp_cb.reg_info.pL2CA_ConnectPnd_Cb = NULL;
  172. sdp_cb.reg_info.pL2CA_ConfigInd_Cb = sdp_config_ind;
  173. sdp_cb.reg_info.pL2CA_ConfigCfm_Cb = sdp_config_cfm;
  174. sdp_cb.reg_info.pL2CA_DisconnectInd_Cb = sdp_disconnect_ind;
  175. sdp_cb.reg_info.pL2CA_DisconnectCfm_Cb = sdp_disconnect_cfm;
  176. sdp_cb.reg_info.pL2CA_QoSViolationInd_Cb = NULL;
  177. sdp_cb.reg_info.pL2CA_DataInd_Cb = sdp_data_ind;
  178. sdp_cb.reg_info.pL2CA_CongestionStatus_Cb = NULL;
  179. sdp_cb.reg_info.pL2CA_TxComplete_Cb = NULL;
  180. /* Now, register with L2CAP */
  181. if (!L2CA_Register (SDP_PSM, &sdp_cb.reg_info)) {
  182. SDP_TRACE_ERROR ("SDP Registration failed\n");
  183. }
  184. }
  185. void sdp_deinit (void)
  186. {
  187. list_free(sdp_cb.server_db.p_record_list);
  188. #if SDP_DYNAMIC_MEMORY
  189. osi_free(sdp_cb_ptr);
  190. sdp_cb_ptr = NULL;
  191. #endif /* #if SDP_DYNAMIC_MEMORY */
  192. }
  193. #if (defined(SDP_DEBUG) && SDP_DEBUG == TRUE)
  194. /*******************************************************************************
  195. **
  196. ** Function sdp_set_max_attr_list_size
  197. **
  198. ** Description This function sets the max attribute list size to use
  199. **
  200. ** Returns void
  201. **
  202. *******************************************************************************/
  203. UINT16 sdp_set_max_attr_list_size (UINT16 max_size)
  204. {
  205. if (max_size > (sdp_cb.l2cap_my_cfg.mtu - 16) ) {
  206. max_size = sdp_cb.l2cap_my_cfg.mtu - 16;
  207. }
  208. sdp_cb.max_attr_list_size = max_size;
  209. return sdp_cb.max_attr_list_size;
  210. }
  211. #endif
  212. /*******************************************************************************
  213. **
  214. ** Function sdp_connect_ind
  215. **
  216. ** Description This function handles an inbound connection indication
  217. ** from L2CAP. This is the case where we are acting as a
  218. ** server.
  219. **
  220. ** Returns void
  221. **
  222. *******************************************************************************/
  223. static void sdp_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id)
  224. {
  225. UNUSED(psm);
  226. #if SDP_SERVER_ENABLED == TRUE
  227. tCONN_CB *p_ccb;
  228. /* Allocate a new CCB. Return if none available. */
  229. if ((p_ccb = sdpu_allocate_ccb()) == NULL) {
  230. return;
  231. }
  232. /* Transition to the next appropriate state, waiting for config setup. */
  233. p_ccb->con_state = SDP_STATE_CFG_SETUP;
  234. /* Save the BD Address and Channel ID. */
  235. memcpy (&p_ccb->device_address[0], bd_addr, sizeof (BD_ADDR));
  236. p_ccb->connection_id = l2cap_cid;
  237. /* Send response to the L2CAP layer. */
  238. L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
  239. {
  240. tL2CAP_CFG_INFO cfg = sdp_cb.l2cap_my_cfg;
  241. if (cfg.fcr_present) {
  242. SDP_TRACE_DEBUG("sdp_connect_ind: mode %u, txwinsz %u, max_trans %u, rtrans_tout %u, mon_tout %u, mps %u\n",
  243. cfg.fcr.mode, cfg.fcr.tx_win_sz, cfg.fcr.max_transmit,
  244. cfg.fcr.rtrans_tout, cfg.fcr.mon_tout, cfg.fcr.mps);
  245. }
  246. if ((!L2CA_ConfigReq (l2cap_cid, &cfg)) && cfg.fcr_present
  247. && cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  248. /* FCR not desired; try again in basic mode */
  249. cfg.fcr.mode = L2CAP_FCR_BASIC_MODE;
  250. cfg.fcr_present = FALSE;
  251. L2CA_ConfigReq (l2cap_cid, &cfg);
  252. }
  253. }
  254. SDP_TRACE_EVENT ("SDP - Rcvd L2CAP conn ind, sent config req, CID 0x%x\n", p_ccb->connection_id);
  255. #else /* No server */
  256. /* Reject the connection */
  257. L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_PSM, 0);
  258. #endif
  259. }
  260. #if SDP_CLIENT_ENABLED == TRUE
  261. /*******************************************************************************
  262. **
  263. ** Function sdp_connect_cfm
  264. **
  265. ** Description This function handles the connect confirm events
  266. ** from L2CAP. This is the case when we are acting as a
  267. ** client and have sent a connect request.
  268. **
  269. ** Returns void
  270. **
  271. *******************************************************************************/
  272. static void sdp_connect_cfm (UINT16 l2cap_cid, UINT16 result)
  273. {
  274. tCONN_CB *p_ccb;
  275. tL2CAP_CFG_INFO cfg;
  276. /* Find CCB based on CID */
  277. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) == NULL) {
  278. SDP_TRACE_WARNING ("SDP - Rcvd conn cnf for unknown CID 0x%x\n", l2cap_cid);
  279. return;
  280. }
  281. /* If the connection response contains success status, then */
  282. /* Transition to the next state and startup the timer. */
  283. if ((result == L2CAP_CONN_OK) && (p_ccb->con_state == SDP_STATE_CONN_SETUP)) {
  284. p_ccb->con_state = SDP_STATE_CFG_SETUP;
  285. cfg = sdp_cb.l2cap_my_cfg;
  286. if (cfg.fcr_present) {
  287. SDP_TRACE_DEBUG("sdp_connect_cfm: mode %u, txwinsz %u, max_trans %u, rtrans_tout %u, mon_tout %u, mps %u\n",
  288. cfg.fcr.mode, cfg.fcr.tx_win_sz, cfg.fcr.max_transmit,
  289. cfg.fcr.rtrans_tout, cfg.fcr.mon_tout, cfg.fcr.mps);
  290. }
  291. if ((!L2CA_ConfigReq (l2cap_cid, &cfg)) && cfg.fcr_present
  292. && cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  293. /* FCR not desired; try again in basic mode */
  294. cfg.fcr_present = FALSE;
  295. cfg.fcr.mode = L2CAP_FCR_BASIC_MODE;
  296. L2CA_ConfigReq (l2cap_cid, &cfg);
  297. }
  298. SDP_TRACE_EVENT ("SDP - got conn cnf, sent cfg req, CID: 0x%x\n", p_ccb->connection_id);
  299. } else {
  300. SDP_TRACE_WARNING ("SDP - Rcvd conn cnf with error: 0x%x CID 0x%x\n", result, p_ccb->connection_id);
  301. /* Tell the user if he has a callback */
  302. if (p_ccb->p_cb || p_ccb->p_cb2) {
  303. UINT16 err = -1;
  304. if ((result == HCI_ERR_HOST_REJECT_SECURITY)
  305. || (result == HCI_ERR_AUTH_FAILURE)
  306. || (result == HCI_ERR_PAIRING_NOT_ALLOWED)
  307. || (result == HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED)
  308. || (result == HCI_ERR_KEY_MISSING)) {
  309. err = SDP_SECURITY_ERR;
  310. } else if (result == HCI_ERR_HOST_REJECT_DEVICE) {
  311. err = SDP_CONN_REJECTED;
  312. } else {
  313. err = SDP_CONN_FAILED;
  314. }
  315. if (p_ccb->p_cb) {
  316. (*p_ccb->p_cb)(err);
  317. } else if (p_ccb->p_cb2) {
  318. (*p_ccb->p_cb2)(err, p_ccb->user_data);
  319. }
  320. }
  321. sdpu_release_ccb (p_ccb);
  322. }
  323. }
  324. #endif /* SDP_CLIENT_ENABLED == TRUE */
  325. /*******************************************************************************
  326. **
  327. ** Function sdp_config_ind
  328. **
  329. ** Description This function processes the L2CAP configuration indication
  330. ** event.
  331. **
  332. ** Returns void
  333. **
  334. *******************************************************************************/
  335. static void sdp_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
  336. {
  337. tCONN_CB *p_ccb;
  338. /* Find CCB based on CID */
  339. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) == NULL) {
  340. SDP_TRACE_WARNING ("SDP - Rcvd L2CAP cfg ind, unknown CID: 0x%x\n", l2cap_cid);
  341. return;
  342. }
  343. /* Remember the remote MTU size */
  344. if (!p_cfg->mtu_present) {
  345. /* use min(L2CAP_DEFAULT_MTU,SDP_MTU_SIZE) for GKI buffer size reasons */
  346. p_ccb->rem_mtu_size = (L2CAP_DEFAULT_MTU > SDP_MTU_SIZE) ? SDP_MTU_SIZE : L2CAP_DEFAULT_MTU;
  347. } else {
  348. if (p_cfg->mtu > SDP_MTU_SIZE) {
  349. p_ccb->rem_mtu_size = SDP_MTU_SIZE;
  350. } else {
  351. p_ccb->rem_mtu_size = p_cfg->mtu;
  352. }
  353. }
  354. /* For now, always accept configuration from the other side */
  355. p_cfg->flush_to_present = FALSE;
  356. p_cfg->mtu_present = FALSE;
  357. p_cfg->result = L2CAP_CFG_OK;
  358. /* Check peer config request against our rfcomm configuration */
  359. if (p_cfg->fcr_present) {
  360. /* Reject the window size if it is bigger than we want it to be */
  361. if (p_cfg->fcr.mode != L2CAP_FCR_BASIC_MODE) {
  362. if (sdp_cb.l2cap_my_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE
  363. && p_cfg->fcr.tx_win_sz > sdp_cb.l2cap_my_cfg.fcr.tx_win_sz) {
  364. p_cfg->fcr.tx_win_sz = sdp_cb.l2cap_my_cfg.fcr.tx_win_sz;
  365. p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  366. SDP_TRACE_DEBUG("sdp_config_ind(CONFIG) -> Please try again with SMALLER TX WINDOW\n");
  367. }
  368. /* Reject if locally we want basic and they don't */
  369. if (sdp_cb.l2cap_my_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
  370. /* Ask for a new setup */
  371. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  372. p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  373. SDP_TRACE_DEBUG("sdp_config_ind(CONFIG) -> Please try again with BASIC mode\n");
  374. }
  375. /* Remain in configure state and give the peer our desired configuration */
  376. if (p_cfg->result != L2CAP_CFG_OK) {
  377. SDP_TRACE_WARNING ("SDP - Rcvd cfg ind, Unacceptable Parameters sent cfg cfm, CID: 0x%x\n", l2cap_cid);
  378. L2CA_ConfigRsp (l2cap_cid, p_cfg);
  379. return;
  380. }
  381. } else { /* We agree with peer's request */
  382. p_cfg->fcr_present = FALSE;
  383. }
  384. }
  385. L2CA_ConfigRsp (l2cap_cid, p_cfg);
  386. SDP_TRACE_EVENT ("SDP - Rcvd cfg ind, sent cfg cfm, CID: 0x%x\n", l2cap_cid);
  387. p_ccb->con_flags |= SDP_FLAGS_HIS_CFG_DONE;
  388. if (p_ccb->con_flags & SDP_FLAGS_MY_CFG_DONE) {
  389. p_ccb->con_state = SDP_STATE_CONNECTED;
  390. if (p_ccb->con_flags & SDP_FLAGS_IS_ORIG) {
  391. sdp_disc_connected (p_ccb);
  392. } else
  393. /* Start inactivity timer */
  394. {
  395. btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
  396. }
  397. }
  398. }
  399. /*******************************************************************************
  400. **
  401. ** Function sdp_config_cfm
  402. **
  403. ** Description This function processes the L2CAP configuration confirmation
  404. ** event.
  405. **
  406. ** Returns void
  407. **
  408. *******************************************************************************/
  409. static void sdp_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
  410. {
  411. tCONN_CB *p_ccb;
  412. SDP_TRACE_EVENT ("SDP - Rcvd cfg cfm, CID: 0x%x Result: %d\n", l2cap_cid, p_cfg->result);
  413. /* Find CCB based on CID */
  414. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) == NULL) {
  415. SDP_TRACE_WARNING ("SDP - Rcvd L2CAP cfg ind, unknown CID: 0x%x\n", l2cap_cid);
  416. return;
  417. }
  418. /* For now, always accept configuration from the other side */
  419. if (p_cfg->result == L2CAP_CFG_OK) {
  420. p_ccb->con_flags |= SDP_FLAGS_MY_CFG_DONE;
  421. if (p_ccb->con_flags & SDP_FLAGS_HIS_CFG_DONE) {
  422. p_ccb->con_state = SDP_STATE_CONNECTED;
  423. if (p_ccb->con_flags & SDP_FLAGS_IS_ORIG) {
  424. sdp_disc_connected (p_ccb);
  425. } else
  426. /* Start inactivity timer */
  427. {
  428. #if BT_SDP_BQB_INCLUDED
  429. /* Change the timeout from 30s to 90s for BQB test */
  430. if (s_sdp_bqb_inact_timeout_flag) {
  431. btu_start_timer(&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_BQB_INACT_TIMEOUT);
  432. } else
  433. #endif /* BT_SDP_BQB_INCLUDED */
  434. {
  435. btu_start_timer(&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
  436. }
  437. }
  438. }
  439. } else {
  440. /* If peer has rejected FCR and suggested basic then try basic */
  441. if (p_cfg->fcr_present) {
  442. tL2CAP_CFG_INFO cfg = sdp_cb.l2cap_my_cfg;
  443. cfg.fcr_present = FALSE;
  444. L2CA_ConfigReq (l2cap_cid, &cfg);
  445. /* Remain in configure state */
  446. return;
  447. }
  448. #if SDP_CLIENT_ENABLED == TRUE
  449. sdp_disconnect(p_ccb, SDP_CFG_FAILED);
  450. #endif
  451. }
  452. }
  453. /*******************************************************************************
  454. **
  455. ** Function sdp_disconnect_ind
  456. **
  457. ** Description This function handles a disconnect event from L2CAP. If
  458. ** requested to, we ack the disconnect before dropping the CCB
  459. **
  460. ** Returns void
  461. **
  462. *******************************************************************************/
  463. static void sdp_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed)
  464. {
  465. tCONN_CB *p_ccb;
  466. /* Find CCB based on CID */
  467. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) == NULL) {
  468. SDP_TRACE_WARNING ("SDP - Rcvd L2CAP disc, unknown CID: 0x%x\n", l2cap_cid);
  469. return;
  470. }
  471. if (ack_needed) {
  472. L2CA_DisconnectRsp (l2cap_cid);
  473. }
  474. SDP_TRACE_EVENT ("SDP - Rcvd L2CAP disc, CID: 0x%x\n", l2cap_cid);
  475. #if SDP_CLIENT_ENABLED == TRUE
  476. /* Tell the user if he has a callback */
  477. if (p_ccb->p_cb) {
  478. (*p_ccb->p_cb) ((UINT16) ((p_ccb->con_state == SDP_STATE_CONNECTED) ?
  479. SDP_SUCCESS : SDP_CONN_FAILED));
  480. } else if (p_ccb->p_cb2) {
  481. (*p_ccb->p_cb2) ((UINT16) ((p_ccb->con_state == SDP_STATE_CONNECTED) ?
  482. SDP_SUCCESS : SDP_CONN_FAILED), p_ccb->user_data);
  483. }
  484. #endif
  485. sdpu_release_ccb (p_ccb);
  486. }
  487. /*******************************************************************************
  488. **
  489. ** Function sdp_data_ind
  490. **
  491. ** Description This function is called when data is received from L2CAP.
  492. ** if we are the originator of the connection, we are the SDP
  493. ** client, and the received message is queued up for the client.
  494. **
  495. ** If we are the destination of the connection, we are the SDP
  496. ** server, so the message is passed to the server processing
  497. ** function.
  498. **
  499. ** Returns void
  500. **
  501. *******************************************************************************/
  502. static void sdp_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg)
  503. {
  504. tCONN_CB *p_ccb;
  505. /* Find CCB based on CID */
  506. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) != NULL) {
  507. if (p_ccb->con_state == SDP_STATE_CONNECTED) {
  508. #if BT_SDP_BQB_INCLUDED
  509. /* Skip the following code in BQB test when the flag is true, since the PDU is reserved and
  510. function sdp_server_handle_client_req will return error (sdpu_build_n_send_error) */
  511. if (!s_sdp_bqb_disable_flag)
  512. #endif /* BT_SDP_BQB_INCLUDED */
  513. {
  514. if (p_ccb->con_flags & SDP_FLAGS_IS_ORIG) {
  515. sdp_disc_server_rsp(p_ccb, p_msg);
  516. } else {
  517. sdp_server_handle_client_req(p_ccb, p_msg);
  518. }
  519. }
  520. } else {
  521. SDP_TRACE_WARNING ("SDP - Ignored L2CAP data while in state: %d, CID: 0x%x\n",
  522. p_ccb->con_state, l2cap_cid);
  523. }
  524. } else {
  525. SDP_TRACE_WARNING ("SDP - Rcvd L2CAP data, unknown CID: 0x%x\n", l2cap_cid);
  526. }
  527. osi_free (p_msg);
  528. }
  529. #if SDP_CLIENT_ENABLED == TRUE
  530. /*******************************************************************************
  531. **
  532. ** Function sdp_conn_originate
  533. **
  534. ** Description This function is called from the API to originate a
  535. ** connection.
  536. **
  537. ** Returns void
  538. **
  539. *******************************************************************************/
  540. tCONN_CB *sdp_conn_originate (UINT8 *p_bd_addr)
  541. {
  542. tCONN_CB *p_ccb;
  543. UINT16 cid;
  544. /* Allocate a new CCB. Return if none available. */
  545. if ((p_ccb = sdpu_allocate_ccb()) == NULL) {
  546. SDP_TRACE_WARNING ("SDP - no spare CCB for orig\n");
  547. return (NULL);
  548. }
  549. SDP_TRACE_EVENT ("SDP - Originate started\n");
  550. /* We are the originator of this connection */
  551. p_ccb->con_flags |= SDP_FLAGS_IS_ORIG;
  552. /* Save the BD Address and Channel ID. */
  553. memcpy (&p_ccb->device_address[0], p_bd_addr, sizeof (BD_ADDR));
  554. /* Transition to the next appropriate state, waiting for connection confirm. */
  555. p_ccb->con_state = SDP_STATE_CONN_SETUP;
  556. cid = L2CA_ConnectReq (SDP_PSM, p_bd_addr);
  557. /* Check if L2CAP started the connection process */
  558. if (cid != 0) {
  559. p_ccb->connection_id = cid;
  560. return (p_ccb);
  561. } else {
  562. SDP_TRACE_WARNING ("SDP - Originate failed\n");
  563. sdpu_release_ccb (p_ccb);
  564. return (NULL);
  565. }
  566. }
  567. /*******************************************************************************
  568. **
  569. ** Function sdp_disconnect
  570. **
  571. ** Description This function disconnects a connection.
  572. **
  573. ** Returns void
  574. **
  575. *******************************************************************************/
  576. void sdp_disconnect (tCONN_CB *p_ccb, UINT16 reason)
  577. {
  578. #if (defined(SDP_BROWSE_PLUS) && SDP_BROWSE_PLUS == TRUE)
  579. /* If we are browsing for multiple UUIDs ... */
  580. if ((p_ccb->con_state == SDP_STATE_CONNECTED)
  581. && (p_ccb->con_flags & SDP_FLAGS_IS_ORIG)
  582. && ((reason == SDP_SUCCESS) || (reason == SDP_NO_RECS_MATCH))) {
  583. /* If the browse found something, do no more searching */
  584. if ((p_ccb->cur_uuid_idx == 0) && (p_ccb->p_db->p_first_rec)) {
  585. p_ccb->cur_uuid_idx = p_ccb->p_db->num_uuid_filters;
  586. }
  587. while (++p_ccb->cur_uuid_idx < p_ccb->p_db->num_uuid_filters) {
  588. /* Check we have not already found the UUID (maybe through browse) */
  589. if ((p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx].len == 2)
  590. && (SDP_FindServiceInDb (p_ccb->p_db,
  591. p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx].uu.uuid16,
  592. NULL))) {
  593. continue;
  594. }
  595. if ((p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx].len > 2)
  596. && (SDP_FindServiceUUIDInDb (p_ccb->p_db,
  597. &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx], NULL))) {
  598. continue;
  599. }
  600. p_ccb->cur_handle = 0;
  601. SDP_TRACE_EVENT ("SDP - looking for for more, CID: 0x%x\n",
  602. p_ccb->connection_id);
  603. sdp_disc_connected (p_ccb);
  604. return;
  605. }
  606. }
  607. if ((reason == SDP_NO_RECS_MATCH) && (p_ccb->p_db->p_first_rec)) {
  608. reason = SDP_SUCCESS;
  609. }
  610. #endif
  611. SDP_TRACE_EVENT ("SDP - disconnect CID: 0x%x\n", p_ccb->connection_id);
  612. /* Check if we have a connection ID */
  613. if (p_ccb->connection_id != 0) {
  614. L2CA_DisconnectReq (p_ccb->connection_id);
  615. p_ccb->disconnect_reason = reason;
  616. }
  617. /* If at setup state, we may not get callback ind from L2CAP */
  618. /* Call user callback immediately */
  619. if (p_ccb->con_state == SDP_STATE_CONN_SETUP) {
  620. /* Tell the user if he has a callback */
  621. if (p_ccb->p_cb) {
  622. (*p_ccb->p_cb) (reason);
  623. } else if (p_ccb->p_cb2) {
  624. (*p_ccb->p_cb2) (reason, p_ccb->user_data);
  625. }
  626. sdpu_release_ccb (p_ccb);
  627. }
  628. }
  629. /*******************************************************************************
  630. **
  631. ** Function sdp_disconnect_cfm
  632. **
  633. ** Description This function handles a disconnect confirm event from L2CAP.
  634. **
  635. ** Returns void
  636. **
  637. *******************************************************************************/
  638. static void sdp_disconnect_cfm (UINT16 l2cap_cid, UINT16 result)
  639. {
  640. tCONN_CB *p_ccb;
  641. UNUSED(result);
  642. /* Find CCB based on CID */
  643. if ((p_ccb = sdpu_find_ccb_by_cid (l2cap_cid)) == NULL) {
  644. SDP_TRACE_WARNING ("SDP - Rcvd L2CAP disc cfm, unknown CID: 0x%x\n", l2cap_cid);
  645. return;
  646. }
  647. SDP_TRACE_EVENT ("SDP - Rcvd L2CAP disc cfm, CID: 0x%x, rsn %d\n", l2cap_cid, p_ccb->disconnect_reason);
  648. /* Tell the user if he has a callback */
  649. if (p_ccb->p_cb) {
  650. (*p_ccb->p_cb) (p_ccb->disconnect_reason);
  651. } else if (p_ccb->p_cb2) {
  652. (*p_ccb->p_cb2) (p_ccb->disconnect_reason, p_ccb->user_data);
  653. }
  654. sdpu_release_ccb (p_ccb);
  655. }
  656. #endif /* SDP_CLIENT_ENABLED == TRUE */
  657. /*******************************************************************************
  658. **
  659. ** Function sdp_conn_timeout
  660. **
  661. ** Description This function processes a timeout. Currently, we simply send
  662. ** a disconnect request to L2CAP.
  663. **
  664. ** Returns void
  665. **
  666. *******************************************************************************/
  667. void sdp_conn_timeout (tCONN_CB *p_ccb)
  668. {
  669. SDP_TRACE_EVENT ("SDP - CCB timeout in state: %d CID: 0x%x\n",
  670. p_ccb->con_state, p_ccb->connection_id);
  671. L2CA_DisconnectReq (p_ccb->connection_id);
  672. #if SDP_CLIENT_ENABLED == TRUE
  673. /* Tell the user if he has a callback */
  674. if (p_ccb->p_cb) {
  675. (*p_ccb->p_cb) (SDP_CONN_FAILED);
  676. } else if (p_ccb->p_cb2) {
  677. (*p_ccb->p_cb2) (SDP_CONN_FAILED, p_ccb->user_data);
  678. }
  679. #endif
  680. sdpu_release_ccb (p_ccb);
  681. }
  682. #endif ///SDP_INCLUDED == TRUE