sdp_main.c 24 KB

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