bta_jv_api.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /******************************************************************************
  2. *
  3. * Copyright 2006-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 is the implementation of the JAVA API for Bluetooth Wireless
  21. * Technology (JABWT) as specified by the JSR82 specificiation
  22. *
  23. ******************************************************************************/
  24. #include <base/bind.h>
  25. #include <base/bind_helpers.h>
  26. #include <base/logging.h>
  27. #include <string.h>
  28. #include "bt_common.h"
  29. #include "bta_api.h"
  30. #include "bta_jv_api.h"
  31. #include "bta_jv_int.h"
  32. #include "bta_sys.h"
  33. #include "gap_api.h"
  34. #include "port_api.h"
  35. #include "sdp_api.h"
  36. #include "stack/include/btu.h"
  37. #include "utl.h"
  38. using base::Bind;
  39. using bluetooth::Uuid;
  40. namespace {
  41. bool bta_jv_enabled = false;
  42. }
  43. /*******************************************************************************
  44. *
  45. * Function BTA_JvEnable
  46. *
  47. * Description Enable the Java I/F service. When the enable
  48. * operation is complete the callback function will be
  49. * called with a BTA_JV_ENABLE_EVT. This function must
  50. * be called before other function in the JV API are
  51. * called.
  52. *
  53. * Returns BTA_JV_SUCCESS if successful.
  54. * BTA_JV_FAIL if internal failure.
  55. *
  56. ******************************************************************************/
  57. tBTA_JV_STATUS BTA_JvEnable(tBTA_JV_DM_CBACK* p_cback) {
  58. if (!p_cback || bta_jv_enabled) {
  59. LOG(ERROR) << __func__ << ": failure";
  60. return BTA_JV_FAILURE;
  61. }
  62. VLOG(2) << __func__;
  63. memset(&bta_jv_cb, 0, sizeof(tBTA_JV_CB));
  64. /* set handle to invalid value by default */
  65. for (int i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
  66. bta_jv_cb.pm_cb[i].handle = BTA_JV_PM_HANDLE_CLEAR;
  67. }
  68. bta_jv_enabled = true;
  69. do_in_main_thread(FROM_HERE, Bind(&bta_jv_enable, p_cback));
  70. return BTA_JV_SUCCESS;
  71. }
  72. /** Disable the Java I/F */
  73. void BTA_JvDisable(void) {
  74. VLOG(2) << __func__;
  75. bta_jv_enabled = false;
  76. do_in_main_thread(FROM_HERE, Bind(&bta_jv_disable));
  77. }
  78. /*******************************************************************************
  79. *
  80. * Function BTA_JvIsEncrypted
  81. *
  82. * Description This function checks if the link to peer device is encrypted
  83. *
  84. * Returns true if encrypted.
  85. * false if not.
  86. *
  87. ******************************************************************************/
  88. bool BTA_JvIsEncrypted(const RawAddress& bd_addr) {
  89. bool is_encrypted = false;
  90. uint8_t sec_flags, le_flags;
  91. if (BTM_GetSecurityFlags(bd_addr, &sec_flags) &&
  92. BTM_GetSecurityFlagsByTransport(bd_addr, &le_flags, BT_TRANSPORT_LE)) {
  93. if (sec_flags & BTM_SEC_FLAG_ENCRYPTED || le_flags & BTM_SEC_FLAG_ENCRYPTED)
  94. is_encrypted = true;
  95. }
  96. return is_encrypted;
  97. }
  98. /*******************************************************************************
  99. *
  100. * Function BTA_JvGetChannelId
  101. *
  102. * Description This function reserves a SCN (server channel number) for
  103. * applications running over RFCOMM, L2CAP of L2CAP_LE.
  104. * It is primarily called by server profiles/applications to
  105. * register their SCN into the SDP database. The SCN is
  106. * reported by the tBTA_JV_DM_CBACK callback with a
  107. * BTA_JV_GET_SCN_EVT for RFCOMM channels and
  108. * BTA_JV_GET_PSM_EVT for L2CAP and LE.
  109. * If the SCN/PSM reported is 0, that means all resources are
  110. * exhausted.
  111. * Parameters
  112. * conn_type one of BTA_JV_CONN_TYPE_
  113. * user_data Any uservalue - will be returned in the resulting event.
  114. * channel Only used for RFCOMM - to try to allocate a specific RFCOMM
  115. * channel.
  116. *
  117. * Returns void
  118. *
  119. ******************************************************************************/
  120. void BTA_JvGetChannelId(int conn_type, uint32_t id, int32_t channel) {
  121. VLOG(2) << __func__ << ": conn_type=" << conn_type;
  122. if (conn_type != BTA_JV_CONN_TYPE_RFCOMM &&
  123. conn_type != BTA_JV_CONN_TYPE_L2CAP &&
  124. conn_type != BTA_JV_CONN_TYPE_L2CAP_LE) {
  125. CHECK(false) << "Invalid conn_type=" << conn_type;
  126. }
  127. do_in_main_thread(FROM_HERE,
  128. Bind(&bta_jv_get_channel_id, conn_type, channel, id, id));
  129. }
  130. /*******************************************************************************
  131. *
  132. * Function BTA_JvFreeChannel
  133. *
  134. * Description This function frees a server channel number that was used
  135. * by an application running over RFCOMM.
  136. * Parameters
  137. * channel The channel to free
  138. * conn_type one of BTA_JV_CONN_TYPE_
  139. *
  140. * Returns BTA_JV_SUCCESS, if the request is being processed.
  141. * BTA_JV_FAILURE, otherwise.
  142. *
  143. ******************************************************************************/
  144. tBTA_JV_STATUS BTA_JvFreeChannel(uint16_t channel, int conn_type) {
  145. VLOG(2) << __func__;
  146. do_in_main_thread(FROM_HERE, Bind(&bta_jv_free_scn, conn_type, channel));
  147. return BTA_JV_SUCCESS;
  148. }
  149. /*******************************************************************************
  150. *
  151. * Function BTA_JvStartDiscovery
  152. *
  153. * Description This function performs service discovery for the services
  154. * provided by the given peer device. When the operation is
  155. * complete the tBTA_JV_DM_CBACK callback function will be
  156. * called with a BTA_JV_DISCOVERY_COMP_EVT.
  157. *
  158. * Returns BTA_JV_SUCCESS, if the request is being processed.
  159. * BTA_JV_FAILURE, otherwise.
  160. *
  161. ******************************************************************************/
  162. tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr,
  163. uint16_t num_uuid, const Uuid* p_uuid_list,
  164. uint32_t rfcomm_slot_id) {
  165. VLOG(2) << __func__;
  166. Uuid* uuid_list_copy = new Uuid[num_uuid];
  167. memcpy(uuid_list_copy, p_uuid_list, num_uuid * sizeof(Uuid));
  168. do_in_main_thread(FROM_HERE,
  169. Bind(&bta_jv_start_discovery, bd_addr, num_uuid,
  170. base::Owned(uuid_list_copy), rfcomm_slot_id));
  171. return BTA_JV_SUCCESS;
  172. }
  173. /*******************************************************************************
  174. *
  175. * Function BTA_JvCreateRecord
  176. *
  177. * Description Create a service record in the local SDP database.
  178. * When the operation is complete the tBTA_JV_DM_CBACK callback
  179. * function will be called with a BTA_JV_CREATE_RECORD_EVT.
  180. *
  181. * Returns BTA_JV_SUCCESS, if the request is being processed.
  182. * BTA_JV_FAILURE, otherwise.
  183. *
  184. ******************************************************************************/
  185. tBTA_JV_STATUS BTA_JvCreateRecordByUser(uint32_t rfcomm_slot_id) {
  186. VLOG(2) << __func__;
  187. do_in_main_thread(FROM_HERE, Bind(&bta_jv_create_record, rfcomm_slot_id));
  188. return BTA_JV_SUCCESS;
  189. }
  190. /*******************************************************************************
  191. *
  192. * Function BTA_JvDeleteRecord
  193. *
  194. * Description Delete a service record in the local SDP database.
  195. *
  196. * Returns BTA_JV_SUCCESS, if the request is being processed.
  197. * BTA_JV_FAILURE, otherwise.
  198. *
  199. ******************************************************************************/
  200. tBTA_JV_STATUS BTA_JvDeleteRecord(uint32_t handle) {
  201. VLOG(2) << __func__;
  202. do_in_main_thread(FROM_HERE, Bind(&bta_jv_delete_record, handle));
  203. return BTA_JV_SUCCESS;
  204. }
  205. /*******************************************************************************
  206. *
  207. * Function BTA_JvL2capConnectLE
  208. *
  209. * Description Initiate an LE connection as a L2CAP client to the given BD
  210. * Address.
  211. * When the connection is initiated or failed to initiate,
  212. * tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
  213. * When the connection is established or failed,
  214. * tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
  215. *
  216. ******************************************************************************/
  217. void BTA_JvL2capConnectLE(uint16_t remote_chan, const RawAddress& peer_bd_addr,
  218. tBTA_JV_L2CAP_CBACK* p_cback,
  219. uint32_t l2cap_socket_id) {
  220. VLOG(2) << __func__;
  221. CHECK(p_cback);
  222. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_connect_le, remote_chan,
  223. peer_bd_addr, p_cback, l2cap_socket_id));
  224. }
  225. /*******************************************************************************
  226. *
  227. * Function BTA_JvL2capConnect
  228. *
  229. * Description Initiate a connection as a L2CAP client to the given BD
  230. * Address.
  231. * When the connection is initiated or failed to initiate,
  232. * tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
  233. * When the connection is established or failed,
  234. * tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
  235. *
  236. ******************************************************************************/
  237. void BTA_JvL2capConnect(int conn_type, tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  238. std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
  239. uint16_t remote_psm, uint16_t rx_mtu,
  240. std::unique_ptr<tL2CAP_CFG_INFO> cfg,
  241. const RawAddress& peer_bd_addr,
  242. tBTA_JV_L2CAP_CBACK* p_cback,
  243. uint32_t l2cap_socket_id) {
  244. VLOG(2) << __func__;
  245. CHECK(p_cback);
  246. do_in_main_thread(FROM_HERE,
  247. Bind(&bta_jv_l2cap_connect, conn_type, sec_mask, role,
  248. remote_psm, rx_mtu, peer_bd_addr, base::Passed(&cfg),
  249. base::Passed(&ertm_info), p_cback, l2cap_socket_id));
  250. }
  251. /*******************************************************************************
  252. *
  253. * Function BTA_JvL2capClose
  254. *
  255. * Description This function closes an L2CAP client connection
  256. *
  257. * Returns BTA_JV_SUCCESS, if the request is being processed.
  258. * BTA_JV_FAILURE, otherwise.
  259. *
  260. ******************************************************************************/
  261. tBTA_JV_STATUS BTA_JvL2capClose(uint32_t handle) {
  262. VLOG(2) << __func__;
  263. if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback)
  264. return BTA_JV_FAILURE;
  265. do_in_main_thread(
  266. FROM_HERE, Bind(&bta_jv_l2cap_close, handle, &bta_jv_cb.l2c_cb[handle]));
  267. return BTA_JV_SUCCESS;
  268. }
  269. /*******************************************************************************
  270. *
  271. * Function BTA_JvL2capCloseLE
  272. *
  273. * Description This function closes an L2CAP client connection for Fixed
  274. * Channels Function is idempotent and no callbacks are called!
  275. *
  276. * Returns BTA_JV_SUCCESS, if the request is being processed.
  277. * BTA_JV_FAILURE, otherwise.
  278. *
  279. ******************************************************************************/
  280. tBTA_JV_STATUS BTA_JvL2capCloseLE(uint32_t handle) {
  281. VLOG(2) << __func__;
  282. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_close_fixed, handle));
  283. return BTA_JV_SUCCESS;
  284. }
  285. /*******************************************************************************
  286. *
  287. * Function BTA_JvL2capStartServer
  288. *
  289. * Description This function starts an L2CAP server and listens for an
  290. * L2CAP connection from a remote Bluetooth device. When the
  291. * server is started successfully, tBTA_JV_L2CAP_CBACK is
  292. * called with BTA_JV_L2CAP_START_EVT. When the connection is
  293. * established tBTA_JV_L2CAP_CBACK is called with
  294. * BTA_JV_L2CAP_OPEN_EVT.
  295. *
  296. * Returns void
  297. *
  298. ******************************************************************************/
  299. void BTA_JvL2capStartServer(int conn_type, tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  300. std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
  301. uint16_t local_psm, uint16_t rx_mtu,
  302. std::unique_ptr<tL2CAP_CFG_INFO> cfg,
  303. tBTA_JV_L2CAP_CBACK* p_cback,
  304. uint32_t l2cap_socket_id) {
  305. VLOG(2) << __func__;
  306. CHECK(p_cback);
  307. do_in_main_thread(FROM_HERE,
  308. Bind(&bta_jv_l2cap_start_server, conn_type, sec_mask, role,
  309. local_psm, rx_mtu, base::Passed(&cfg),
  310. base::Passed(&ertm_info), p_cback, l2cap_socket_id));
  311. }
  312. /*******************************************************************************
  313. *
  314. * Function BTA_JvL2capStartServerLE
  315. *
  316. * Description This function starts an LE L2CAP server and listens for an
  317. * L2CAP connection from a remote Bluetooth device. When the
  318. * server is started successfully, tBTA_JV_L2CAP_CBACK is
  319. * called with BTA_JV_L2CAP_START_EVT. When the connection is
  320. * established, tBTA_JV_L2CAP_CBACK is called with
  321. * BTA_JV_L2CAP_OPEN_EVT.
  322. *
  323. * Returns void
  324. *
  325. ******************************************************************************/
  326. void BTA_JvL2capStartServerLE(uint16_t local_chan, tBTA_JV_L2CAP_CBACK* p_cback,
  327. uint32_t l2cap_socket_id) {
  328. VLOG(2) << __func__;
  329. CHECK(p_cback);
  330. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_start_server_le, local_chan,
  331. p_cback, l2cap_socket_id));
  332. }
  333. /*******************************************************************************
  334. *
  335. * Function BTA_JvL2capStopServer
  336. *
  337. * Description This function stops the L2CAP server. If the server has an
  338. * active connection, it would be closed.
  339. *
  340. * Returns BTA_JV_SUCCESS, if the request is being processed.
  341. * BTA_JV_FAILURE, otherwise.
  342. *
  343. ******************************************************************************/
  344. tBTA_JV_STATUS BTA_JvL2capStopServer(uint16_t local_psm,
  345. uint32_t l2cap_socket_id) {
  346. VLOG(2) << __func__;
  347. do_in_main_thread(
  348. FROM_HERE, Bind(&bta_jv_l2cap_stop_server, local_psm, l2cap_socket_id));
  349. return BTA_JV_SUCCESS;
  350. }
  351. /*******************************************************************************
  352. *
  353. * Function BTA_JvL2capStopServerLE
  354. *
  355. * Description This function stops the LE L2CAP server. If the server has
  356. * an active connection, it would be closed.
  357. *
  358. * Returns BTA_JV_SUCCESS, if the request is being processed.
  359. * BTA_JV_FAILURE, otherwise.
  360. *
  361. ******************************************************************************/
  362. tBTA_JV_STATUS BTA_JvL2capStopServerLE(uint16_t local_chan,
  363. uint32_t l2cap_socket_id) {
  364. VLOG(2) << __func__;
  365. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_stop_server_le, local_chan));
  366. return BTA_JV_SUCCESS;
  367. }
  368. /*******************************************************************************
  369. *
  370. * Function BTA_JvL2capRead
  371. *
  372. * Description This function reads data from an L2CAP connection
  373. * When the operation is complete, tBTA_JV_L2CAP_CBACK is
  374. * called with BTA_JV_L2CAP_READ_EVT.
  375. *
  376. * Returns BTA_JV_SUCCESS, if the request is being processed.
  377. * BTA_JV_FAILURE, otherwise.
  378. *
  379. ******************************************************************************/
  380. tBTA_JV_STATUS BTA_JvL2capRead(uint32_t handle, uint32_t req_id,
  381. uint8_t* p_data, uint16_t len) {
  382. VLOG(2) << __func__;
  383. if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback)
  384. return BTA_JV_FAILURE;
  385. tBTA_JV_L2CAP_READ evt_data;
  386. evt_data.status = BTA_JV_FAILURE;
  387. evt_data.handle = handle;
  388. evt_data.req_id = req_id;
  389. evt_data.p_data = p_data;
  390. evt_data.len = 0;
  391. if (BT_PASS ==
  392. GAP_ConnReadData((uint16_t)handle, p_data, len, &evt_data.len)) {
  393. evt_data.status = BTA_JV_SUCCESS;
  394. }
  395. bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_READ_EVT, (tBTA_JV*)&evt_data,
  396. bta_jv_cb.l2c_cb[handle].l2cap_socket_id);
  397. return BTA_JV_SUCCESS;
  398. }
  399. /*******************************************************************************
  400. *
  401. * Function BTA_JvL2capReady
  402. *
  403. * Description This function determined if there is data to read from
  404. * an L2CAP connection
  405. *
  406. * Returns BTA_JV_SUCCESS, if data queue size is in *p_data_size.
  407. * BTA_JV_FAILURE, if error.
  408. *
  409. ******************************************************************************/
  410. tBTA_JV_STATUS BTA_JvL2capReady(uint32_t handle, uint32_t* p_data_size) {
  411. tBTA_JV_STATUS status = BTA_JV_FAILURE;
  412. VLOG(2) << __func__ << ": handle=" << handle;
  413. if (p_data_size && handle < BTA_JV_MAX_L2C_CONN &&
  414. bta_jv_cb.l2c_cb[handle].p_cback) {
  415. *p_data_size = 0;
  416. if (BT_PASS == GAP_GetRxQueueCnt((uint16_t)handle, p_data_size)) {
  417. status = BTA_JV_SUCCESS;
  418. }
  419. }
  420. return (status);
  421. }
  422. /*******************************************************************************
  423. *
  424. * Function BTA_JvL2capWrite
  425. *
  426. * Description This function writes data to an L2CAP connection
  427. * When the operation is complete, tBTA_JV_L2CAP_CBACK is
  428. * called with BTA_JV_L2CAP_WRITE_EVT. Works for
  429. * PSM-based connections. This function takes ownership of
  430. * p_data, and will osi_free it. Data length must be smaller
  431. * than remote maximum SDU size.
  432. *
  433. * Returns BTA_JV_SUCCESS, if the request is being processed.
  434. * BTA_JV_FAILURE, otherwise.
  435. *
  436. ******************************************************************************/
  437. tBTA_JV_STATUS BTA_JvL2capWrite(uint32_t handle, uint32_t req_id, BT_HDR* msg,
  438. uint32_t user_id) {
  439. VLOG(2) << __func__;
  440. if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback) {
  441. osi_free(msg);
  442. return BTA_JV_FAILURE;
  443. }
  444. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_write, handle, req_id, msg,
  445. user_id, &bta_jv_cb.l2c_cb[handle]));
  446. return BTA_JV_SUCCESS;
  447. }
  448. /*******************************************************************************
  449. *
  450. * Function BTA_JvL2capWriteFixed
  451. *
  452. * Description This function writes data to an L2CAP connection
  453. * When the operation is complete, tBTA_JV_L2CAP_CBACK is
  454. * called with BTA_JV_L2CAP_WRITE_EVT. Works for
  455. * fixed-channel connections. This function takes ownership of
  456. * p_data, and will osi_free it.
  457. *
  458. ******************************************************************************/
  459. void BTA_JvL2capWriteFixed(uint16_t channel, const RawAddress& addr,
  460. uint32_t req_id, tBTA_JV_L2CAP_CBACK* p_cback,
  461. BT_HDR* msg, uint32_t user_id) {
  462. VLOG(2) << __func__;
  463. do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_write_fixed, channel, addr,
  464. req_id, msg, user_id, p_cback));
  465. }
  466. /*******************************************************************************
  467. *
  468. * Function BTA_JvRfcommConnect
  469. *
  470. * Description This function makes an RFCOMM conection to a remote BD
  471. * Address.
  472. * When the connection is initiated or failed to initiate,
  473. * tBTA_JV_RFCOMM_CBACK is called with
  474. * BTA_JV_RFCOMM_CL_INIT_EVT
  475. * When the connection is established or failed,
  476. * tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_OPEN_EVT
  477. *
  478. * Returns BTA_JV_SUCCESS, if the request is being processed.
  479. * BTA_JV_FAILURE, otherwise.
  480. *
  481. ******************************************************************************/
  482. tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  483. uint8_t remote_scn,
  484. const RawAddress& peer_bd_addr,
  485. tBTA_JV_RFCOMM_CBACK* p_cback,
  486. uint32_t rfcomm_slot_id) {
  487. VLOG(2) << __func__;
  488. if (!p_cback) return BTA_JV_FAILURE; /* Nothing to do */
  489. do_in_main_thread(FROM_HERE,
  490. Bind(&bta_jv_rfcomm_connect, sec_mask, role, remote_scn,
  491. peer_bd_addr, p_cback, rfcomm_slot_id));
  492. return BTA_JV_SUCCESS;
  493. }
  494. /*******************************************************************************
  495. *
  496. * Function BTA_JvRfcommClose
  497. *
  498. * Description This function closes an RFCOMM connection
  499. *
  500. * Returns BTA_JV_SUCCESS, if the request is being processed.
  501. * BTA_JV_FAILURE, otherwise.
  502. *
  503. ******************************************************************************/
  504. tBTA_JV_STATUS BTA_JvRfcommClose(uint32_t handle, uint32_t rfcomm_slot_id) {
  505. uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
  506. uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
  507. VLOG(2) << __func__;
  508. if (hi >= BTA_JV_MAX_RFC_CONN || !bta_jv_cb.rfc_cb[hi].p_cback ||
  509. si >= BTA_JV_MAX_RFC_SR_SESSION || !bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
  510. return BTA_JV_FAILURE;
  511. do_in_main_thread(FROM_HERE,
  512. Bind(&bta_jv_rfcomm_close, handle, rfcomm_slot_id));
  513. return BTA_JV_SUCCESS;
  514. }
  515. /*******************************************************************************
  516. *
  517. * Function BTA_JvRfcommStartServer
  518. *
  519. * Description This function starts listening for an RFCOMM connection
  520. * request from a remote Bluetooth device. When the server is
  521. * started successfully, tBTA_JV_RFCOMM_CBACK is called
  522. * with BTA_JV_RFCOMM_START_EVT.
  523. * When the connection is established, tBTA_JV_RFCOMM_CBACK
  524. * is called with BTA_JV_RFCOMM_OPEN_EVT.
  525. *
  526. * Returns BTA_JV_SUCCESS, if the request is being processed.
  527. * BTA_JV_FAILURE, otherwise.
  528. *
  529. ******************************************************************************/
  530. tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  531. uint8_t local_scn, uint8_t max_session,
  532. tBTA_JV_RFCOMM_CBACK* p_cback,
  533. uint32_t rfcomm_slot_id) {
  534. VLOG(2) << __func__;
  535. if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
  536. if (max_session == 0) max_session = 1;
  537. if (max_session > BTA_JV_MAX_RFC_SR_SESSION) {
  538. LOG(INFO) << __func__ << "max_session is too big. use max "
  539. << BTA_JV_MAX_RFC_SR_SESSION;
  540. max_session = BTA_JV_MAX_RFC_SR_SESSION;
  541. }
  542. do_in_main_thread(FROM_HERE,
  543. Bind(&bta_jv_rfcomm_start_server, sec_mask, role, local_scn,
  544. max_session, p_cback, rfcomm_slot_id));
  545. return BTA_JV_SUCCESS;
  546. }
  547. /*******************************************************************************
  548. *
  549. * Function BTA_JvRfcommStopServer
  550. *
  551. * Description This function stops the RFCOMM server. If the server has an
  552. * active connection, it would be closed.
  553. *
  554. * Returns BTA_JV_SUCCESS, if the request is being processed.
  555. * BTA_JV_FAILURE, otherwise.
  556. *
  557. ******************************************************************************/
  558. tBTA_JV_STATUS BTA_JvRfcommStopServer(uint32_t handle,
  559. uint32_t rfcomm_slot_id) {
  560. VLOG(2) << __func__;
  561. do_in_main_thread(FROM_HERE,
  562. Bind(&bta_jv_rfcomm_stop_server, handle, rfcomm_slot_id));
  563. return BTA_JV_SUCCESS;
  564. }
  565. /*******************************************************************************
  566. *
  567. * Function BTA_JvRfcommGetPortHdl
  568. *
  569. * Description This function fetches the rfcomm port handle
  570. *
  571. * Returns BTA_JV_SUCCESS, if the request is being processed.
  572. * BTA_JV_FAILURE, otherwise.
  573. *
  574. ******************************************************************************/
  575. uint16_t BTA_JvRfcommGetPortHdl(uint32_t handle) {
  576. uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
  577. uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
  578. if (hi < BTA_JV_MAX_RFC_CONN && si < BTA_JV_MAX_RFC_SR_SESSION &&
  579. bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
  580. return bta_jv_cb.port_cb[bta_jv_cb.rfc_cb[hi].rfc_hdl[si] - 1].port_handle;
  581. else
  582. return 0xffff;
  583. }
  584. /*******************************************************************************
  585. *
  586. * Function BTA_JvRfcommWrite
  587. *
  588. * Description This function writes data to an RFCOMM connection
  589. *
  590. * Returns BTA_JV_SUCCESS, if the request is being processed.
  591. * BTA_JV_FAILURE, otherwise.
  592. *
  593. ******************************************************************************/
  594. tBTA_JV_STATUS BTA_JvRfcommWrite(uint32_t handle, uint32_t req_id) {
  595. uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
  596. uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
  597. VLOG(2) << __func__;
  598. VLOG(2) << __func__ << "handle=" << loghex(handle) << ", hi=" << hi
  599. << ", si=" << si;
  600. if (hi >= BTA_JV_MAX_RFC_CONN || !bta_jv_cb.rfc_cb[hi].p_cback ||
  601. si >= BTA_JV_MAX_RFC_SR_SESSION || !bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
  602. return BTA_JV_FAILURE;
  603. }
  604. VLOG(2) << "write ok";
  605. tBTA_JV_RFC_CB* p_cb = &bta_jv_cb.rfc_cb[hi];
  606. do_in_main_thread(FROM_HERE, Bind(&bta_jv_rfcomm_write, handle, req_id, p_cb,
  607. &bta_jv_cb.port_cb[p_cb->rfc_hdl[si] - 1]));
  608. return BTA_JV_SUCCESS;
  609. }
  610. /*******************************************************************************
  611. *
  612. * Function BTA_JVSetPmProfile
  613. *
  614. * Description: This function set or free power mode profile for different JV
  615. * application.
  616. *
  617. * Parameters: handle, JV handle from RFCOMM or L2CAP
  618. * app_id: app specific pm ID, can be BTA_JV_PM_ALL, see
  619. * bta_dm_cfg.c for details
  620. * BTA_JV_PM_ID_CLEAR: removes pm management on the handle. init_st
  621. * is ignored and BTA_JV_CONN_CLOSE is called implicitly
  622. * init_st: state after calling this API. typically it should be
  623. * BTA_JV_CONN_OPEN
  624. *
  625. * Returns BTA_JV_SUCCESS, if the request is being processed.
  626. * BTA_JV_FAILURE, otherwise.
  627. *
  628. * NOTE: BTA_JV_PM_ID_CLEAR: In general no need to be called as jv pm
  629. * calls automatically
  630. * BTA_JV_CONN_CLOSE to remove in case of connection close!
  631. *
  632. ******************************************************************************/
  633. tBTA_JV_STATUS BTA_JvSetPmProfile(uint32_t handle, tBTA_JV_PM_ID app_id,
  634. tBTA_JV_CONN_STATE init_st) {
  635. VLOG(2) << __func__ << " handle=" << loghex(handle) << ", app_id:" << app_id;
  636. do_in_main_thread(FROM_HERE,
  637. Bind(&bta_jv_set_pm_profile, handle, app_id, init_st));
  638. return BTA_JV_SUCCESS;
  639. }