sdp_main.c 24 KB

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