bta_jv_act.cc 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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 file contains action functions for BTA JV APIs.
  21. *
  22. ******************************************************************************/
  23. #include <arpa/inet.h>
  24. #include <bluetooth/uuid.h>
  25. #include <hardware/bluetooth.h>
  26. #include <pthread.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "avct_api.h"
  30. #include "avdt_api.h"
  31. #include "bt_common.h"
  32. #include "bt_types.h"
  33. #include "bta_api.h"
  34. #include "bta_jv_api.h"
  35. #include "bta_jv_co.h"
  36. #include "bta_jv_int.h"
  37. #include "bta_sys.h"
  38. #include "btm_api.h"
  39. #include "btm_int.h"
  40. #include "device/include/controller.h"
  41. #include "gap_api.h"
  42. #include "l2c_api.h"
  43. #include "osi/include/allocator.h"
  44. #include "port_api.h"
  45. #include "rfcdefs.h"
  46. #include "sdp_api.h"
  47. #include "stack/l2cap/l2c_int.h"
  48. #include "utl.h"
  49. #include "osi/include/osi.h"
  50. using bluetooth::Uuid;
  51. tBTA_JV_CB bta_jv_cb;
  52. /* one of these exists for each client */
  53. struct fc_client {
  54. struct fc_client* next_all_list;
  55. struct fc_client* next_chan_list;
  56. RawAddress remote_addr;
  57. uint32_t id;
  58. tBTA_JV_L2CAP_CBACK* p_cback;
  59. uint32_t l2cap_socket_id;
  60. uint16_t handle;
  61. uint16_t chan;
  62. uint8_t sec_id;
  63. unsigned server : 1;
  64. unsigned init_called : 1;
  65. };
  66. /* one of these exists for each channel we're dealing with */
  67. struct fc_channel {
  68. struct fc_channel* next;
  69. struct fc_client* clients;
  70. uint8_t has_server : 1;
  71. uint16_t chan;
  72. };
  73. static struct fc_client* fc_clients;
  74. static struct fc_channel* fc_channels;
  75. static uint32_t fc_next_id;
  76. static void fcchan_conn_chng_cbk(uint16_t chan, const RawAddress& bd_addr,
  77. bool connected, uint16_t reason,
  78. tBT_TRANSPORT);
  79. static void fcchan_data_cbk(uint16_t chan, const RawAddress& bd_addr,
  80. BT_HDR* p_buf);
  81. static tBTA_JV_PCB* bta_jv_add_rfc_port(tBTA_JV_RFC_CB* p_cb,
  82. tBTA_JV_PCB* p_pcb_open);
  83. static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(uint32_t jv_handle);
  84. static void bta_jv_pm_conn_busy(tBTA_JV_PM_CB* p_cb);
  85. static void bta_jv_pm_conn_idle(tBTA_JV_PM_CB* p_cb);
  86. static void bta_jv_pm_state_change(tBTA_JV_PM_CB* p_cb,
  87. const tBTA_JV_CONN_STATE state);
  88. /*******************************************************************************
  89. *
  90. * Function bta_jv_alloc_sec_id
  91. *
  92. * Description allocate a security id
  93. *
  94. * Returns
  95. *
  96. ******************************************************************************/
  97. uint8_t bta_jv_alloc_sec_id(void) {
  98. uint8_t ret = 0;
  99. int i;
  100. for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
  101. if (0 == bta_jv_cb.sec_id[i]) {
  102. bta_jv_cb.sec_id[i] = BTA_JV_FIRST_SERVICE_ID + i;
  103. ret = bta_jv_cb.sec_id[i];
  104. break;
  105. }
  106. }
  107. return ret;
  108. }
  109. static int get_sec_id_used(void) {
  110. int i;
  111. int used = 0;
  112. for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
  113. if (bta_jv_cb.sec_id[i]) used++;
  114. }
  115. if (used == BTA_JV_NUM_SERVICE_ID)
  116. LOG(ERROR) << __func__
  117. << ": sec id exceeds the limit=" << BTA_JV_NUM_SERVICE_ID;
  118. return used;
  119. }
  120. static int get_rfc_cb_used(void) {
  121. int i;
  122. int used = 0;
  123. for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
  124. if (bta_jv_cb.rfc_cb[i].handle) used++;
  125. }
  126. if (used == BTA_JV_MAX_RFC_CONN)
  127. LOG(ERROR) << __func__
  128. << ": rfc ctrl block exceeds the limit=" << BTA_JV_MAX_RFC_CONN;
  129. return used;
  130. }
  131. /*******************************************************************************
  132. *
  133. * Function bta_jv_free_sec_id
  134. *
  135. * Description free the given security id
  136. *
  137. * Returns
  138. *
  139. ******************************************************************************/
  140. static void bta_jv_free_sec_id(uint8_t* p_sec_id) {
  141. uint8_t sec_id = *p_sec_id;
  142. *p_sec_id = 0;
  143. if (sec_id >= BTA_JV_FIRST_SERVICE_ID && sec_id <= BTA_JV_LAST_SERVICE_ID) {
  144. BTM_SecClrService(sec_id);
  145. bta_jv_cb.sec_id[sec_id - BTA_JV_FIRST_SERVICE_ID] = 0;
  146. }
  147. }
  148. /*******************************************************************************
  149. *
  150. * Function bta_jv_alloc_rfc_cb
  151. *
  152. * Description allocate a control block for the given port handle
  153. *
  154. * Returns
  155. *
  156. ******************************************************************************/
  157. tBTA_JV_RFC_CB* bta_jv_alloc_rfc_cb(uint16_t port_handle,
  158. tBTA_JV_PCB** pp_pcb) {
  159. tBTA_JV_RFC_CB* p_cb = NULL;
  160. tBTA_JV_PCB* p_pcb;
  161. int i, j;
  162. for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
  163. if (0 == bta_jv_cb.rfc_cb[i].handle) {
  164. p_cb = &bta_jv_cb.rfc_cb[i];
  165. /* mask handle to distinguish it with L2CAP handle */
  166. p_cb->handle = (i + 1) | BTA_JV_RFCOMM_MASK;
  167. p_cb->max_sess = 1;
  168. p_cb->curr_sess = 1;
  169. for (j = 0; j < BTA_JV_MAX_RFC_SR_SESSION; j++) p_cb->rfc_hdl[j] = 0;
  170. p_cb->rfc_hdl[0] = port_handle;
  171. VLOG(2) << __func__ << "port_handle=" << +port_handle
  172. << ", handle=" << loghex(p_cb->handle);
  173. p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
  174. p_pcb->handle = p_cb->handle;
  175. p_pcb->port_handle = port_handle;
  176. p_pcb->p_pm_cb = NULL;
  177. *pp_pcb = p_pcb;
  178. break;
  179. }
  180. }
  181. if (p_cb == NULL) {
  182. LOG(ERROR) << __func__ << "port_handle=" << port_handle << " ctrl block exceeds limit:" << BTA_JV_MAX_RFC_CONN;
  183. }
  184. return p_cb;
  185. }
  186. /*******************************************************************************
  187. *
  188. * Function bta_jv_rfc_port_to_pcb
  189. *
  190. * Description find the port control block associated with the given port
  191. * handle
  192. *
  193. * Returns
  194. *
  195. ******************************************************************************/
  196. tBTA_JV_PCB* bta_jv_rfc_port_to_pcb(uint16_t port_handle) {
  197. tBTA_JV_PCB* p_pcb = NULL;
  198. if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS) &&
  199. bta_jv_cb.port_cb[port_handle - 1].handle) {
  200. p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
  201. }
  202. return p_pcb;
  203. }
  204. /*******************************************************************************
  205. *
  206. * Function bta_jv_rfc_port_to_cb
  207. *
  208. * Description find the RFCOMM control block associated with the given port
  209. * handle
  210. *
  211. * Returns
  212. *
  213. ******************************************************************************/
  214. tBTA_JV_RFC_CB* bta_jv_rfc_port_to_cb(uint16_t port_handle) {
  215. tBTA_JV_RFC_CB* p_cb = NULL;
  216. uint32_t handle;
  217. if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS) &&
  218. bta_jv_cb.port_cb[port_handle - 1].handle) {
  219. handle = bta_jv_cb.port_cb[port_handle - 1].handle;
  220. handle &= BTA_JV_RFC_HDL_MASK;
  221. handle &= ~BTA_JV_RFCOMM_MASK;
  222. if (handle) p_cb = &bta_jv_cb.rfc_cb[handle - 1];
  223. } else {
  224. LOG(WARNING) << __func__
  225. << ": jv handle not found port_handle:" << port_handle;
  226. }
  227. return p_cb;
  228. }
  229. static tBTA_JV_STATUS bta_jv_free_rfc_cb(tBTA_JV_RFC_CB* p_cb,
  230. tBTA_JV_PCB* p_pcb) {
  231. tBTA_JV_STATUS status = BTA_JV_SUCCESS;
  232. bool remove_server = false;
  233. int close_pending = 0;
  234. if (!p_cb || !p_pcb) {
  235. LOG(ERROR) << __func__ << " p_cb or p_pcb cannot be null";
  236. return BTA_JV_FAILURE;
  237. }
  238. VLOG(2) << __func__ << ": max_sess=" << p_cb->max_sess
  239. << ", curr_sess=" << p_cb->curr_sess << ", p_pcb=" << p_pcb
  240. << ", user=" << p_pcb->rfcomm_slot_id << ", state=" << p_pcb->state
  241. << ", jv handle=" << loghex(p_pcb->handle);
  242. if (p_cb->curr_sess <= 0) return BTA_JV_SUCCESS;
  243. switch (p_pcb->state) {
  244. case BTA_JV_ST_CL_CLOSING:
  245. case BTA_JV_ST_SR_CLOSING:
  246. LOG(WARNING) << __func__
  247. << ": return on closing, port state=" << p_pcb->state
  248. << ", scn=" << p_cb->scn << ", p_pcb=" << p_pcb
  249. << ", user_data=" << p_pcb->rfcomm_slot_id;
  250. status = BTA_JV_FAILURE;
  251. return status;
  252. case BTA_JV_ST_CL_OPEN:
  253. case BTA_JV_ST_CL_OPENING:
  254. VLOG(2) << __func__ << ": state=" << p_pcb->state << ", scn=" << p_cb->scn
  255. << ", user_data=" << p_pcb->rfcomm_slot_id;
  256. p_pcb->state = BTA_JV_ST_CL_CLOSING;
  257. break;
  258. case BTA_JV_ST_SR_LISTEN:
  259. p_pcb->state = BTA_JV_ST_SR_CLOSING;
  260. remove_server = true;
  261. VLOG(2) << __func__ << ": state: BTA_JV_ST_SR_LISTEN, scn=" << p_cb->scn
  262. << ", user_data=" << p_pcb->rfcomm_slot_id;
  263. break;
  264. case BTA_JV_ST_SR_OPEN:
  265. p_pcb->state = BTA_JV_ST_SR_CLOSING;
  266. VLOG(2) << ": state: BTA_JV_ST_SR_OPEN, scn=" << p_cb->scn
  267. << " user_data=" << p_pcb->rfcomm_slot_id;
  268. break;
  269. default:
  270. LOG(WARNING) << __func__ << ":failed, ignore port state= " << p_pcb->state
  271. << ", scn=" << p_cb->scn << ", p_pcb= " << p_pcb
  272. << ", jv handle=" << loghex(p_pcb->handle)
  273. << ", port_handle=" << p_pcb->port_handle
  274. << ", user_data=" << p_pcb->rfcomm_slot_id;
  275. status = BTA_JV_FAILURE;
  276. break;
  277. }
  278. if (BTA_JV_SUCCESS == status) {
  279. int port_status;
  280. if (!remove_server)
  281. port_status = RFCOMM_RemoveConnection(p_pcb->port_handle);
  282. else
  283. port_status = RFCOMM_RemoveServer(p_pcb->port_handle);
  284. if (port_status != PORT_SUCCESS) {
  285. status = BTA_JV_FAILURE;
  286. LOG(WARNING) << __func__ << ": Remove jv handle=" << loghex(p_pcb->handle)
  287. << ", state=" << p_pcb->state
  288. << ", port_status=" << port_status
  289. << ", port_handle=" << p_pcb->port_handle
  290. << ", close_pending=" << close_pending;
  291. }
  292. }
  293. if (!close_pending) {
  294. p_pcb->port_handle = 0;
  295. p_pcb->state = BTA_JV_ST_NONE;
  296. bta_jv_free_set_pm_profile_cb(p_pcb->handle);
  297. // Initialize congestion flags
  298. p_pcb->cong = false;
  299. p_pcb->rfcomm_slot_id = 0;
  300. int si = BTA_JV_RFC_HDL_TO_SIDX(p_pcb->handle);
  301. if (0 <= si && si < BTA_JV_MAX_RFC_SR_SESSION) p_cb->rfc_hdl[si] = 0;
  302. p_pcb->handle = 0;
  303. p_cb->curr_sess--;
  304. if (p_cb->curr_sess == 0) {
  305. p_cb->scn = 0;
  306. bta_jv_free_sec_id(&p_cb->sec_id);
  307. p_cb->p_cback = NULL;
  308. p_cb->handle = 0;
  309. p_cb->curr_sess = -1;
  310. }
  311. if (remove_server) {
  312. bta_jv_free_sec_id(&p_cb->sec_id);
  313. }
  314. }
  315. return status;
  316. }
  317. /*******************************************************************************
  318. *
  319. * Function bta_jv_free_l2c_cb
  320. *
  321. * Description free the given L2CAP control block
  322. *
  323. * Returns
  324. *
  325. ******************************************************************************/
  326. tBTA_JV_STATUS bta_jv_free_l2c_cb(tBTA_JV_L2C_CB* p_cb) {
  327. tBTA_JV_STATUS status = BTA_JV_SUCCESS;
  328. if (BTA_JV_ST_NONE != p_cb->state) {
  329. bta_jv_free_set_pm_profile_cb((uint32_t)p_cb->handle);
  330. if (GAP_ConnClose(p_cb->handle) != BT_PASS) status = BTA_JV_FAILURE;
  331. }
  332. p_cb->psm = 0;
  333. p_cb->state = BTA_JV_ST_NONE;
  334. p_cb->cong = false;
  335. bta_jv_free_sec_id(&p_cb->sec_id);
  336. p_cb->p_cback = NULL;
  337. p_cb->handle = 0;
  338. p_cb->l2cap_socket_id = 0;
  339. return status;
  340. }
  341. /*******************************************************************************
  342. *
  343. *
  344. * Function bta_jv_clear_pm_cb
  345. *
  346. * Description clears jv pm control block and optionally calls
  347. * bta_sys_conn_close()
  348. * In general close_conn should be set to true to remove registering
  349. * with dm pm!
  350. *
  351. * WARNING: Make sure to clear pointer form port or l2c to this control block
  352. * too!
  353. *
  354. ******************************************************************************/
  355. static void bta_jv_clear_pm_cb(tBTA_JV_PM_CB* p_pm_cb, bool close_conn) {
  356. /* needs to be called if registered with bta pm, otherwise we may run out of
  357. * dm pm slots! */
  358. if (close_conn)
  359. bta_sys_conn_close(BTA_ID_JV, p_pm_cb->app_id, p_pm_cb->peer_bd_addr);
  360. p_pm_cb->state = BTA_JV_PM_FREE_ST;
  361. p_pm_cb->app_id = BTA_JV_PM_ALL;
  362. p_pm_cb->handle = BTA_JV_PM_HANDLE_CLEAR;
  363. p_pm_cb->peer_bd_addr = RawAddress::kEmpty;
  364. }
  365. /*******************************************************************************
  366. *
  367. * Function bta_jv_free_set_pm_profile_cb
  368. *
  369. * Description free pm profile control block
  370. *
  371. * Returns BTA_JV_SUCCESS if cb has been freed correctly,
  372. * BTA_JV_FAILURE in case of no profile has been registered or
  373. * already freed
  374. *
  375. ******************************************************************************/
  376. static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(uint32_t jv_handle) {
  377. tBTA_JV_STATUS status = BTA_JV_FAILURE;
  378. tBTA_JV_PM_CB** p_cb;
  379. int i, j, bd_counter = 0, appid_counter = 0;
  380. for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
  381. p_cb = NULL;
  382. if ((bta_jv_cb.pm_cb[i].state != BTA_JV_PM_FREE_ST) &&
  383. (jv_handle == bta_jv_cb.pm_cb[i].handle)) {
  384. for (j = 0; j < BTA_JV_PM_MAX_NUM; j++) {
  385. if (bta_jv_cb.pm_cb[j].peer_bd_addr == bta_jv_cb.pm_cb[i].peer_bd_addr)
  386. bd_counter++;
  387. if (bta_jv_cb.pm_cb[j].app_id == bta_jv_cb.pm_cb[i].app_id)
  388. appid_counter++;
  389. }
  390. VLOG(2) << __func__ << ": jv_handle=" << loghex(jv_handle)
  391. << ", idx=" << i << "app_id=" << bta_jv_cb.pm_cb[i].app_id
  392. << ", bd_counter=" << bd_counter
  393. << ", appid_counter=" << appid_counter;
  394. if (bd_counter > 1) {
  395. bta_jv_pm_conn_idle(&bta_jv_cb.pm_cb[i]);
  396. }
  397. if (bd_counter <= 1 || (appid_counter <= 1)) {
  398. bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], true);
  399. } else {
  400. bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], false);
  401. }
  402. if (BTA_JV_RFCOMM_MASK & jv_handle) {
  403. uint32_t hi =
  404. ((jv_handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
  405. uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(jv_handle);
  406. if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
  407. si < BTA_JV_MAX_RFC_SR_SESSION &&
  408. bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
  409. tBTA_JV_PCB* p_pcb =
  410. bta_jv_rfc_port_to_pcb(bta_jv_cb.rfc_cb[hi].rfc_hdl[si]);
  411. if (p_pcb) {
  412. if (NULL == p_pcb->p_pm_cb)
  413. LOG(WARNING) << __func__ << ": jv_handle=" << loghex(jv_handle)
  414. << ", port_handle=" << p_pcb->port_handle
  415. << ", i=" << i << ", no link to pm_cb?";
  416. p_cb = &p_pcb->p_pm_cb;
  417. }
  418. }
  419. } else {
  420. if (jv_handle < BTA_JV_MAX_L2C_CONN) {
  421. tBTA_JV_L2C_CB* p_l2c_cb = &bta_jv_cb.l2c_cb[jv_handle];
  422. if (NULL == p_l2c_cb->p_pm_cb)
  423. LOG(WARNING) << __func__ << ": jv_handle=" << loghex(jv_handle)
  424. << ", i=" << i << " no link to pm_cb?";
  425. p_cb = &p_l2c_cb->p_pm_cb;
  426. }
  427. }
  428. if (p_cb) {
  429. *p_cb = NULL;
  430. status = BTA_JV_SUCCESS;
  431. }
  432. }
  433. }
  434. return status;
  435. }
  436. /*******************************************************************************
  437. *
  438. * Function bta_jv_alloc_set_pm_profile_cb
  439. *
  440. * Description set PM profile control block
  441. *
  442. * Returns pointer to allocated cb or NULL in case of failure
  443. *
  444. ******************************************************************************/
  445. static tBTA_JV_PM_CB* bta_jv_alloc_set_pm_profile_cb(uint32_t jv_handle,
  446. tBTA_JV_PM_ID app_id) {
  447. bool bRfcHandle = (jv_handle & BTA_JV_RFCOMM_MASK) != 0;
  448. RawAddress peer_bd_addr = RawAddress::kEmpty;
  449. int i, j;
  450. tBTA_JV_PM_CB** pp_cb;
  451. for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
  452. pp_cb = NULL;
  453. if (bta_jv_cb.pm_cb[i].state == BTA_JV_PM_FREE_ST) {
  454. /* rfc handle bd addr retrieval requires core stack handle */
  455. if (bRfcHandle) {
  456. for (j = 0; j < BTA_JV_MAX_RFC_CONN; j++) {
  457. if (jv_handle == bta_jv_cb.port_cb[j].handle) {
  458. pp_cb = &bta_jv_cb.port_cb[j].p_pm_cb;
  459. if (PORT_SUCCESS !=
  460. PORT_CheckConnection(bta_jv_cb.port_cb[j].port_handle,
  461. &peer_bd_addr, NULL)) {
  462. i = BTA_JV_PM_MAX_NUM;
  463. }
  464. break;
  465. }
  466. }
  467. } else {
  468. /* use jv handle for l2cap bd address retrieval */
  469. for (j = 0; j < BTA_JV_MAX_L2C_CONN; j++) {
  470. if (jv_handle == bta_jv_cb.l2c_cb[j].handle) {
  471. pp_cb = &bta_jv_cb.l2c_cb[j].p_pm_cb;
  472. const RawAddress* p_bd_addr =
  473. GAP_ConnGetRemoteAddr((uint16_t)jv_handle);
  474. if (p_bd_addr)
  475. peer_bd_addr = *p_bd_addr;
  476. else
  477. i = BTA_JV_PM_MAX_NUM;
  478. break;
  479. }
  480. }
  481. }
  482. VLOG(2) << __func__ << ": handle=" << loghex(jv_handle)
  483. << ", app_id=" << app_id << ", idx=" << i
  484. << ", BTA_JV_PM_MAX_NUM=" << BTA_JV_PM_MAX_NUM
  485. << ", pp_cb=" << pp_cb;
  486. break;
  487. }
  488. }
  489. if ((i != BTA_JV_PM_MAX_NUM) && (NULL != pp_cb)) {
  490. *pp_cb = &bta_jv_cb.pm_cb[i];
  491. bta_jv_cb.pm_cb[i].handle = jv_handle;
  492. bta_jv_cb.pm_cb[i].app_id = app_id;
  493. bta_jv_cb.pm_cb[i].peer_bd_addr = peer_bd_addr;
  494. bta_jv_cb.pm_cb[i].state = BTA_JV_PM_IDLE_ST;
  495. return &bta_jv_cb.pm_cb[i];
  496. }
  497. LOG(WARNING) << __func__ << ": handle=" << loghex(jv_handle)
  498. << ", app_id=" << app_id << ", return NULL";
  499. return NULL;
  500. }
  501. /*******************************************************************************
  502. *
  503. * Function bta_jv_check_psm
  504. *
  505. * Description for now use only the legal PSM per JSR82 spec
  506. *
  507. * Returns true, if allowed
  508. *
  509. ******************************************************************************/
  510. bool bta_jv_check_psm(uint16_t psm) {
  511. bool ret = false;
  512. if (L2C_IS_VALID_PSM(psm)) {
  513. if (psm < 0x1001) {
  514. /* see if this is defined by spec */
  515. switch (psm) {
  516. case SDP_PSM: /* 1 */
  517. case BT_PSM_RFCOMM: /* 3 */
  518. /* do not allow java app to use these 2 PSMs */
  519. break;
  520. case TCS_PSM_INTERCOM: /* 5 */
  521. case TCS_PSM_CORDLESS: /* 7 */
  522. if (!bta_sys_is_register(BTA_ID_CT) &&
  523. !bta_sys_is_register(BTA_ID_CG))
  524. ret = true;
  525. break;
  526. case BT_PSM_BNEP: /* F */
  527. if (!bta_sys_is_register(BTA_ID_PAN)) ret = true;
  528. break;
  529. case HID_PSM_CONTROL: /* 0x11 */
  530. case HID_PSM_INTERRUPT: /* 0x13 */
  531. // FIX: allow HID Device and HID Host to coexist
  532. if (!bta_sys_is_register(BTA_ID_HD) ||
  533. !bta_sys_is_register(BTA_ID_HH))
  534. ret = true;
  535. break;
  536. case AVCT_PSM: /* 0x17 */
  537. case AVDT_PSM: /* 0x19 */
  538. if ((!bta_sys_is_register(BTA_ID_AV)) &&
  539. (!bta_sys_is_register(BTA_ID_AVK)))
  540. ret = true;
  541. break;
  542. default:
  543. ret = true;
  544. break;
  545. }
  546. } else {
  547. ret = true;
  548. }
  549. }
  550. return ret;
  551. }
  552. /* Initialises the JAVA I/F */
  553. void bta_jv_enable(tBTA_JV_DM_CBACK* p_cback) {
  554. tBTA_JV_STATUS status = BTA_JV_SUCCESS;
  555. bta_jv_cb.p_dm_cback = p_cback;
  556. tBTA_JV bta_jv;
  557. bta_jv.status = status;
  558. bta_jv_cb.p_dm_cback(BTA_JV_ENABLE_EVT, &bta_jv, 0);
  559. memset(bta_jv_cb.free_psm_list, 0, sizeof(bta_jv_cb.free_psm_list));
  560. }
  561. /** Disables the BT device manager free the resources used by java */
  562. void bta_jv_disable() { LOG(INFO) << __func__; }
  563. /**
  564. * We keep a list of PSM's that have been freed from JAVA, for reuse.
  565. * This function will return a free PSM, and delete it from the free
  566. * list.
  567. * If no free PSMs exist, 0 will be returned.
  568. */
  569. static uint16_t bta_jv_get_free_psm() {
  570. const int cnt =
  571. sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
  572. for (int i = 0; i < cnt; i++) {
  573. uint16_t psm = bta_jv_cb.free_psm_list[i];
  574. if (psm != 0) {
  575. VLOG(2) << __func__ << ": Reusing PSM=" << loghex(psm);
  576. bta_jv_cb.free_psm_list[i] = 0;
  577. return psm;
  578. }
  579. }
  580. return 0;
  581. }
  582. static void bta_jv_set_free_psm(uint16_t psm) {
  583. int free_index = -1;
  584. const int cnt =
  585. sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
  586. for (int i = 0; i < cnt; i++) {
  587. if (bta_jv_cb.free_psm_list[i] == 0) {
  588. free_index = i;
  589. } else if (psm == bta_jv_cb.free_psm_list[i]) {
  590. return; // PSM already freed?
  591. }
  592. }
  593. if (free_index != -1) {
  594. bta_jv_cb.free_psm_list[free_index] = psm;
  595. VLOG(2) << __func__ << ": Recycling PSM=" << loghex(psm);
  596. } else {
  597. LOG(ERROR) << __func__ << ": unable to free psm=" << loghex(psm)
  598. << " no more free slots";
  599. }
  600. }
  601. /** Obtain a free SCN (Server Channel Number) (RFCOMM channel or L2CAP PSM) */
  602. void bta_jv_get_channel_id(
  603. int32_t type /* One of BTA_JV_CONN_TYPE_ */,
  604. int32_t channel /* optionally request a specific channel */,
  605. uint32_t l2cap_socket_id, uint32_t rfcomm_slot_id) {
  606. uint16_t psm = 0;
  607. switch (type) {
  608. case BTA_JV_CONN_TYPE_RFCOMM: {
  609. uint8_t scn = 0;
  610. if (channel > 0) {
  611. if (!BTM_TryAllocateSCN(channel)) {
  612. LOG(ERROR) << "rfc channel=" << channel
  613. << " already in use or invalid";
  614. channel = 0;
  615. }
  616. } else {
  617. channel = BTM_AllocateSCN();
  618. if (channel == 0) {
  619. LOG(ERROR) << "run out of rfc channels";
  620. channel = 0;
  621. }
  622. }
  623. if (channel != 0) {
  624. bta_jv_cb.scn[channel - 1] = true;
  625. scn = (uint8_t)channel;
  626. }
  627. if (bta_jv_cb.p_dm_cback) {
  628. tBTA_JV bta_jv;
  629. bta_jv.scn = scn;
  630. bta_jv_cb.p_dm_cback(BTA_JV_GET_SCN_EVT, &bta_jv, rfcomm_slot_id);
  631. }
  632. return;
  633. }
  634. case BTA_JV_CONN_TYPE_L2CAP:
  635. psm = bta_jv_get_free_psm();
  636. if (psm == 0) {
  637. psm = L2CA_AllocatePSM();
  638. VLOG(2) << __func__ << ": returned PSM=" << loghex(psm);
  639. }
  640. break;
  641. case BTA_JV_CONN_TYPE_L2CAP_LE:
  642. psm = L2CA_AllocateLePSM();
  643. if (psm == 0) {
  644. LOG(ERROR) << __func__ << ": Error: No free LE PSM available";
  645. }
  646. break;
  647. default:
  648. break;
  649. }
  650. if (bta_jv_cb.p_dm_cback) {
  651. tBTA_JV bta_jv;
  652. bta_jv.psm = psm;
  653. bta_jv_cb.p_dm_cback(BTA_JV_GET_PSM_EVT, &bta_jv, l2cap_socket_id);
  654. }
  655. }
  656. /** free a SCN */
  657. void bta_jv_free_scn(int32_t type /* One of BTA_JV_CONN_TYPE_ */,
  658. uint16_t scn) {
  659. switch (type) {
  660. case BTA_JV_CONN_TYPE_RFCOMM: {
  661. if (scn > 0 && scn <= BTA_JV_MAX_SCN && bta_jv_cb.scn[scn - 1]) {
  662. /* this scn is used by JV */
  663. bta_jv_cb.scn[scn - 1] = false;
  664. BTM_FreeSCN(scn);
  665. }
  666. break;
  667. }
  668. case BTA_JV_CONN_TYPE_L2CAP:
  669. bta_jv_set_free_psm(scn);
  670. break;
  671. case BTA_JV_CONN_TYPE_L2CAP_LE:
  672. VLOG(2) << __func__ << ": type=BTA_JV_CONN_TYPE_L2CAP_LE. psm=" << scn;
  673. L2CA_FreeLePSM(scn);
  674. break;
  675. default:
  676. break;
  677. }
  678. }
  679. /*******************************************************************************
  680. *
  681. * Function bta_jv_start_discovery_cback
  682. *
  683. * Description Callback for Start Discovery
  684. *
  685. * Returns void
  686. *
  687. ******************************************************************************/
  688. static void bta_jv_start_discovery_cback(uint16_t result, void* user_data) {
  689. tBTA_JV_STATUS status;
  690. uint32_t* p_rfcomm_slot_id = static_cast<uint32_t*>(user_data);
  691. VLOG(2) << __func__ << ": res=" << loghex(result);
  692. bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_NONE;
  693. if (bta_jv_cb.p_dm_cback) {
  694. tBTA_JV_DISCOVERY_COMP dcomp;
  695. dcomp.scn = 0;
  696. status = BTA_JV_FAILURE;
  697. if (result == SDP_SUCCESS || result == SDP_DB_FULL) {
  698. tSDP_DISC_REC* p_sdp_rec = NULL;
  699. tSDP_PROTOCOL_ELEM pe;
  700. VLOG(2) << __func__ << ": bta_jv_cb.uuid=" << bta_jv_cb.uuid;
  701. p_sdp_rec = SDP_FindServiceUUIDInDb(p_bta_jv_cfg->p_sdp_db,
  702. bta_jv_cb.uuid, p_sdp_rec);
  703. VLOG(2) << __func__ << ": p_sdp_rec=" << p_sdp_rec;
  704. if (p_sdp_rec &&
  705. SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
  706. dcomp.scn = (uint8_t)pe.params[0];
  707. status = BTA_JV_SUCCESS;
  708. }
  709. }
  710. dcomp.status = status;
  711. tBTA_JV bta_jv;
  712. bta_jv.disc_comp = dcomp;
  713. bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, *p_rfcomm_slot_id);
  714. osi_free(p_rfcomm_slot_id);
  715. }
  716. }
  717. /* Discovers services on a remote device */
  718. void bta_jv_start_discovery(const RawAddress& bd_addr, uint16_t num_uuid,
  719. bluetooth::Uuid* uuid_list,
  720. uint32_t rfcomm_slot_id) {
  721. tBTA_JV_STATUS status = BTA_JV_FAILURE;
  722. VLOG(2) << __func__ << ": in, sdp_active=" << bta_jv_cb.sdp_active;
  723. if (bta_jv_cb.sdp_active != BTA_JV_SDP_ACT_NONE) {
  724. /* SDP is still in progress */
  725. status = BTA_JV_BUSY;
  726. if (bta_jv_cb.p_dm_cback) {
  727. tBTA_JV bta_jv;
  728. bta_jv.status = status;
  729. bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, rfcomm_slot_id);
  730. }
  731. return;
  732. }
  733. /* init the database/set up the filter */
  734. VLOG(2) << __func__ << ": call SDP_InitDiscoveryDb, num_uuid=" << num_uuid;
  735. SDP_InitDiscoveryDb(p_bta_jv_cfg->p_sdp_db, p_bta_jv_cfg->sdp_db_size,
  736. num_uuid, uuid_list, 0, NULL);
  737. /* tell SDP to keep the raw data */
  738. p_bta_jv_cfg->p_sdp_db->raw_data = p_bta_jv_cfg->p_sdp_raw_data;
  739. p_bta_jv_cfg->p_sdp_db->raw_size = p_bta_jv_cfg->sdp_raw_size;
  740. bta_jv_cb.p_sel_raw_data = 0;
  741. bta_jv_cb.uuid = uuid_list[0];
  742. bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_YES;
  743. uint32_t* rfcomm_slot_id_copy = (uint32_t*)osi_malloc(sizeof(uint32_t));
  744. *rfcomm_slot_id_copy = rfcomm_slot_id;
  745. if (!SDP_ServiceSearchAttributeRequest2(bd_addr, p_bta_jv_cfg->p_sdp_db,
  746. bta_jv_start_discovery_cback,
  747. (void*)rfcomm_slot_id_copy)) {
  748. bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_NONE;
  749. /* failed to start SDP. report the failure right away */
  750. if (bta_jv_cb.p_dm_cback) {
  751. tBTA_JV bta_jv;
  752. bta_jv.status = status;
  753. bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, rfcomm_slot_id);
  754. }
  755. }
  756. /*
  757. else report the result when the cback is called
  758. */
  759. }
  760. /* Create an SDP record with the given attributes */
  761. void bta_jv_create_record(uint32_t rfcomm_slot_id) {
  762. tBTA_JV_CREATE_RECORD evt_data;
  763. evt_data.status = BTA_JV_SUCCESS;
  764. if (bta_jv_cb.p_dm_cback) {
  765. // callback immediately to create the sdp record in stack thread context
  766. tBTA_JV bta_jv;
  767. bta_jv.create_rec = evt_data;
  768. bta_jv_cb.p_dm_cback(BTA_JV_CREATE_RECORD_EVT, &bta_jv, rfcomm_slot_id);
  769. }
  770. }
  771. /* Delete an SDP record */
  772. void bta_jv_delete_record(uint32_t handle) {
  773. if (handle) {
  774. /* this is a record created by btif layer*/
  775. SDP_DeleteRecord(handle);
  776. }
  777. }
  778. /*******************************************************************************
  779. *
  780. * Function bta_jv_l2cap_client_cback
  781. *
  782. * Description handles the l2cap client events
  783. *
  784. * Returns void
  785. *
  786. ******************************************************************************/
  787. static void bta_jv_l2cap_client_cback(uint16_t gap_handle, uint16_t event,
  788. tGAP_CB_DATA* data) {
  789. tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[gap_handle];
  790. tBTA_JV evt_data;
  791. if (gap_handle >= BTA_JV_MAX_L2C_CONN && !p_cb->p_cback) return;
  792. VLOG(2) << __func__ << ": gap_handle=" << gap_handle
  793. << ", evt=" << loghex(event);
  794. evt_data.l2c_open.status = BTA_JV_SUCCESS;
  795. evt_data.l2c_open.handle = gap_handle;
  796. switch (event) {
  797. case GAP_EVT_CONN_OPENED:
  798. evt_data.l2c_open.rem_bda = *GAP_ConnGetRemoteAddr(gap_handle);
  799. evt_data.l2c_open.tx_mtu = GAP_ConnGetRemMtuSize(gap_handle);
  800. p_cb->state = BTA_JV_ST_CL_OPEN;
  801. p_cb->p_cback(BTA_JV_L2CAP_OPEN_EVT, &evt_data, p_cb->l2cap_socket_id);
  802. break;
  803. case GAP_EVT_CONN_CLOSED:
  804. p_cb->state = BTA_JV_ST_NONE;
  805. bta_jv_free_sec_id(&p_cb->sec_id);
  806. evt_data.l2c_close.async = true;
  807. p_cb->p_cback(BTA_JV_L2CAP_CLOSE_EVT, &evt_data, p_cb->l2cap_socket_id);
  808. p_cb->p_cback = NULL;
  809. break;
  810. case GAP_EVT_CONN_DATA_AVAIL:
  811. evt_data.data_ind.handle = gap_handle;
  812. /* Reset idle timer to avoid requesting sniff mode while receiving data */
  813. bta_jv_pm_conn_busy(p_cb->p_pm_cb);
  814. p_cb->p_cback(BTA_JV_L2CAP_DATA_IND_EVT, &evt_data,
  815. p_cb->l2cap_socket_id);
  816. bta_jv_pm_conn_idle(p_cb->p_pm_cb);
  817. break;
  818. case GAP_EVT_TX_EMPTY:
  819. bta_jv_pm_conn_idle(p_cb->p_pm_cb);
  820. break;
  821. case GAP_EVT_CONN_CONGESTED:
  822. case GAP_EVT_CONN_UNCONGESTED:
  823. p_cb->cong = (event == GAP_EVT_CONN_CONGESTED) ? true : false;
  824. evt_data.l2c_cong.cong = p_cb->cong;
  825. p_cb->p_cback(BTA_JV_L2CAP_CONG_EVT, &evt_data, p_cb->l2cap_socket_id);
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831. /* makes an l2cap client connection */
  832. void bta_jv_l2cap_connect(int32_t type, tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  833. uint16_t remote_psm, uint16_t rx_mtu,
  834. const RawAddress& peer_bd_addr,
  835. std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,
  836. std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
  837. tBTA_JV_L2CAP_CBACK* p_cback,
  838. uint32_t l2cap_socket_id) {
  839. uint16_t handle = GAP_INVALID_HANDLE;
  840. uint8_t chan_mode_mask = GAP_FCR_CHAN_OPT_BASIC;
  841. tL2CAP_CFG_INFO cfg;
  842. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  843. if (cfg_param) {
  844. cfg = *cfg_param;
  845. if (cfg.fcr_present && cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  846. chan_mode_mask = GAP_FCR_CHAN_OPT_ERTM;
  847. }
  848. }
  849. /* We need to use this value for MTU to be able to handle cases where cfg is
  850. * not set in req. */
  851. cfg.mtu_present = true;
  852. cfg.mtu = rx_mtu;
  853. /* TODO: DM role manager
  854. L2CA_SetDesireRole(role);
  855. */
  856. uint8_t sec_id = bta_jv_alloc_sec_id();
  857. tBTA_JV_L2CAP_CL_INIT evt_data;
  858. evt_data.sec_id = sec_id;
  859. evt_data.status = BTA_JV_FAILURE;
  860. if (sec_id) {
  861. /* PSM checking is not required for LE COC */
  862. if ((type != BTA_JV_CONN_TYPE_L2CAP) ||
  863. (bta_jv_check_psm(remote_psm))) /* allowed */
  864. {
  865. uint16_t max_mps = 0xffff; // Let GAP_ConnOpen set the max_mps.
  866. handle = GAP_ConnOpen("", sec_id, 0, &peer_bd_addr, remote_psm, max_mps,
  867. &cfg, ertm_info.get(), sec_mask, chan_mode_mask,
  868. bta_jv_l2cap_client_cback, type);
  869. if (handle != GAP_INVALID_HANDLE) {
  870. evt_data.status = BTA_JV_SUCCESS;
  871. }
  872. }
  873. }
  874. if (evt_data.status == BTA_JV_SUCCESS) {
  875. tBTA_JV_L2C_CB* p_cb;
  876. p_cb = &bta_jv_cb.l2c_cb[handle];
  877. p_cb->handle = handle;
  878. p_cb->p_cback = p_cback;
  879. p_cb->l2cap_socket_id = l2cap_socket_id;
  880. p_cb->psm = 0; /* not a server */
  881. p_cb->sec_id = sec_id;
  882. p_cb->state = BTA_JV_ST_CL_OPENING;
  883. } else {
  884. bta_jv_free_sec_id(&sec_id);
  885. }
  886. evt_data.handle = handle;
  887. if (p_cback) {
  888. tBTA_JV bta_jv;
  889. bta_jv.l2c_cl_init = evt_data;
  890. p_cback(BTA_JV_L2CAP_CL_INIT_EVT, &bta_jv, l2cap_socket_id);
  891. }
  892. }
  893. /** Close an L2CAP client connection */
  894. void bta_jv_l2cap_close(uint32_t handle, tBTA_JV_L2C_CB* p_cb) {
  895. tBTA_JV_L2CAP_CLOSE evt_data;
  896. tBTA_JV_L2CAP_CBACK* p_cback = p_cb->p_cback;
  897. uint32_t l2cap_socket_id = p_cb->l2cap_socket_id;
  898. evt_data.handle = handle;
  899. evt_data.status = bta_jv_free_l2c_cb(p_cb);
  900. evt_data.async = false;
  901. if (p_cback) {
  902. tBTA_JV bta_jv;
  903. bta_jv.l2c_close = evt_data;
  904. p_cback(BTA_JV_L2CAP_CLOSE_EVT, &bta_jv, l2cap_socket_id);
  905. }
  906. }
  907. /*******************************************************************************
  908. *
  909. * Function bta_jv_l2cap_server_cback
  910. *
  911. * Description handles the l2cap server callback
  912. *
  913. * Returns void
  914. *
  915. ******************************************************************************/
  916. static void bta_jv_l2cap_server_cback(uint16_t gap_handle, uint16_t event,
  917. tGAP_CB_DATA* data) {
  918. tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[gap_handle];
  919. tBTA_JV evt_data;
  920. tBTA_JV_L2CAP_CBACK* p_cback;
  921. uint32_t socket_id;
  922. if (gap_handle >= BTA_JV_MAX_L2C_CONN && !p_cb->p_cback) return;
  923. VLOG(2) << __func__ << ": gap_handle=" << gap_handle
  924. << ", evt=" << loghex(event);
  925. evt_data.l2c_open.status = BTA_JV_SUCCESS;
  926. evt_data.l2c_open.handle = gap_handle;
  927. switch (event) {
  928. case GAP_EVT_CONN_OPENED:
  929. evt_data.l2c_open.rem_bda = *GAP_ConnGetRemoteAddr(gap_handle);
  930. evt_data.l2c_open.tx_mtu = GAP_ConnGetRemMtuSize(gap_handle);
  931. p_cb->state = BTA_JV_ST_SR_OPEN;
  932. p_cb->p_cback(BTA_JV_L2CAP_OPEN_EVT, &evt_data, p_cb->l2cap_socket_id);
  933. break;
  934. case GAP_EVT_CONN_CLOSED:
  935. evt_data.l2c_close.async = true;
  936. evt_data.l2c_close.handle = p_cb->handle;
  937. p_cback = p_cb->p_cback;
  938. socket_id = p_cb->l2cap_socket_id;
  939. evt_data.l2c_close.status = bta_jv_free_l2c_cb(p_cb);
  940. p_cback(BTA_JV_L2CAP_CLOSE_EVT, &evt_data, socket_id);
  941. break;
  942. case GAP_EVT_CONN_DATA_AVAIL:
  943. evt_data.data_ind.handle = gap_handle;
  944. /* Reset idle timer to avoid requesting sniff mode while receiving data */
  945. bta_jv_pm_conn_busy(p_cb->p_pm_cb);
  946. p_cb->p_cback(BTA_JV_L2CAP_DATA_IND_EVT, &evt_data,
  947. p_cb->l2cap_socket_id);
  948. bta_jv_pm_conn_idle(p_cb->p_pm_cb);
  949. break;
  950. case GAP_EVT_TX_EMPTY:
  951. bta_jv_pm_conn_idle(p_cb->p_pm_cb);
  952. break;
  953. case GAP_EVT_CONN_CONGESTED:
  954. case GAP_EVT_CONN_UNCONGESTED:
  955. p_cb->cong = (event == GAP_EVT_CONN_CONGESTED) ? true : false;
  956. evt_data.l2c_cong.cong = p_cb->cong;
  957. p_cb->p_cback(BTA_JV_L2CAP_CONG_EVT, &evt_data, p_cb->l2cap_socket_id);
  958. break;
  959. default:
  960. break;
  961. }
  962. }
  963. /** starts an L2CAP server */
  964. void bta_jv_l2cap_start_server(int32_t type, tBTA_SEC sec_mask,
  965. tBTA_JV_ROLE role, uint16_t local_psm,
  966. uint16_t rx_mtu,
  967. std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,
  968. std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
  969. tBTA_JV_L2CAP_CBACK* p_cback,
  970. uint32_t l2cap_socket_id) {
  971. uint16_t handle;
  972. tBTA_JV_L2CAP_START evt_data;
  973. uint8_t chan_mode_mask = GAP_FCR_CHAN_OPT_BASIC;
  974. tL2CAP_CFG_INFO cfg;
  975. memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  976. if (cfg_param) {
  977. cfg = *cfg_param;
  978. if (cfg.fcr_present && cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  979. chan_mode_mask = GAP_FCR_CHAN_OPT_ERTM;
  980. }
  981. }
  982. // FIX: MTU=0 means not present
  983. if (rx_mtu > 0) {
  984. cfg.mtu_present = true;
  985. cfg.mtu = rx_mtu;
  986. } else {
  987. cfg.mtu_present = false;
  988. cfg.mtu = 0;
  989. }
  990. /* TODO DM role manager
  991. L2CA_SetDesireRole(role);
  992. */
  993. uint8_t sec_id = bta_jv_alloc_sec_id();
  994. uint16_t max_mps = 0xffff; // Let GAP_ConnOpen set the max_mps.
  995. /* PSM checking is not required for LE COC */
  996. if (0 == sec_id ||
  997. ((type == BTA_JV_CONN_TYPE_L2CAP) && (!bta_jv_check_psm(local_psm))) ||
  998. (handle = GAP_ConnOpen("JV L2CAP", sec_id, 1, nullptr, local_psm, max_mps,
  999. &cfg, ertm_info.get(), sec_mask, chan_mode_mask,
  1000. bta_jv_l2cap_server_cback, type)) ==
  1001. GAP_INVALID_HANDLE) {
  1002. bta_jv_free_sec_id(&sec_id);
  1003. evt_data.status = BTA_JV_FAILURE;
  1004. } else {
  1005. tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[handle];
  1006. evt_data.status = BTA_JV_SUCCESS;
  1007. evt_data.handle = handle;
  1008. evt_data.sec_id = sec_id;
  1009. p_cb->p_cback = p_cback;
  1010. p_cb->l2cap_socket_id = l2cap_socket_id;
  1011. p_cb->handle = handle;
  1012. p_cb->sec_id = sec_id;
  1013. p_cb->state = BTA_JV_ST_SR_LISTEN;
  1014. p_cb->psm = local_psm;
  1015. }
  1016. if (p_cback) {
  1017. tBTA_JV bta_jv;
  1018. bta_jv.l2c_start = evt_data;
  1019. p_cback(BTA_JV_L2CAP_START_EVT, &bta_jv, l2cap_socket_id);
  1020. }
  1021. }
  1022. /* stops an L2CAP server */
  1023. void bta_jv_l2cap_stop_server(uint16_t local_psm, uint32_t l2cap_socket_id) {
  1024. for (int i = 0; i < BTA_JV_MAX_L2C_CONN; i++) {
  1025. if (bta_jv_cb.l2c_cb[i].l2cap_socket_id == l2cap_socket_id) {
  1026. tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[i];
  1027. tBTA_JV_L2CAP_CBACK* p_cback = p_cb->p_cback;
  1028. tBTA_JV_L2CAP_CLOSE evt_data;
  1029. evt_data.handle = p_cb->handle;
  1030. evt_data.status = bta_jv_free_l2c_cb(p_cb);
  1031. evt_data.async = false;
  1032. if (p_cback) {
  1033. tBTA_JV bta_jv;
  1034. bta_jv.l2c_close = evt_data;
  1035. p_cback(BTA_JV_L2CAP_CLOSE_EVT, &bta_jv, l2cap_socket_id);
  1036. }
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. /* Write data to an L2CAP connection */
  1042. void bta_jv_l2cap_write(uint32_t handle, uint32_t req_id, BT_HDR* msg,
  1043. uint32_t user_id, tBTA_JV_L2C_CB* p_cb) {
  1044. /* As we check this callback exists before the tBTA_JV_API_L2CAP_WRITE can be
  1045. * send through the API this check should not be needed. But the API is not
  1046. * designed to be used (safely at least) in a multi-threaded scheduler, hence
  1047. * if the peer device disconnects the l2cap link after the API is called, but
  1048. * before this message is handled, the ->p_cback will be cleared at this
  1049. * point. At first glanch this seems highly unlikely, but for all
  1050. * obex-profiles with two channels connected - e.g. MAP, this happens around 1
  1051. * of 4 disconnects, as a disconnect on the server channel causes a disconnect
  1052. * to be send on the client (notification) channel, but at the peer typically
  1053. * disconnects both the OBEX disconnect request crosses the incoming l2cap
  1054. * disconnect. If p_cback is cleared, we simply discard the data. RISK: The
  1055. * caller must handle any cleanup based on another signal than
  1056. * BTA_JV_L2CAP_WRITE_EVT, which is typically not possible, as the pointer to
  1057. * the allocated buffer is stored in this message, and can therefore not be
  1058. * freed, hence we have a mem-leak-by-design.*/
  1059. if (!p_cb->p_cback) {
  1060. /* As this pointer is checked in the API function, this occurs only when the
  1061. * channel is disconnected after the API function is called, but before the
  1062. * message is handled. */
  1063. LOG(ERROR) << __func__ << ": p_cb->p_cback == NULL";
  1064. osi_free(msg);
  1065. return;
  1066. }
  1067. tBTA_JV_L2CAP_WRITE evt_data;
  1068. evt_data.status = BTA_JV_FAILURE;
  1069. evt_data.handle = handle;
  1070. evt_data.req_id = req_id;
  1071. evt_data.cong = p_cb->cong;
  1072. evt_data.len = msg->len;
  1073. bta_jv_pm_conn_busy(p_cb->p_pm_cb);
  1074. // TODO: this was set only for non-fixed channel packets. Is that needed ?
  1075. msg->event = BT_EVT_TO_BTU_SP_DATA;
  1076. if (evt_data.cong) {
  1077. osi_free(msg);
  1078. } else {
  1079. if (GAP_ConnWriteData(handle, msg) == BT_PASS)
  1080. evt_data.status = BTA_JV_SUCCESS;
  1081. }
  1082. tBTA_JV bta_jv;
  1083. bta_jv.l2c_write = evt_data;
  1084. p_cb->p_cback(BTA_JV_L2CAP_WRITE_EVT, &bta_jv, user_id);
  1085. }
  1086. /* Write data to an L2CAP connection using Fixed channels */
  1087. void bta_jv_l2cap_write_fixed(uint16_t channel, const RawAddress& addr,
  1088. uint32_t req_id, BT_HDR* msg, uint32_t user_id,
  1089. tBTA_JV_L2CAP_CBACK* p_cback) {
  1090. tBTA_JV_L2CAP_WRITE_FIXED evt_data;
  1091. evt_data.status = BTA_JV_FAILURE;
  1092. evt_data.channel = channel;
  1093. evt_data.addr = addr;
  1094. evt_data.req_id = req_id;
  1095. evt_data.len = 0;
  1096. L2CA_SendFixedChnlData(channel, addr, msg);
  1097. tBTA_JV bta_jv;
  1098. bta_jv.l2c_write_fixed = evt_data;
  1099. p_cback(BTA_JV_L2CAP_WRITE_FIXED_EVT, &bta_jv, user_id);
  1100. }
  1101. /*******************************************************************************
  1102. *
  1103. * Function bta_jv_port_data_co_cback
  1104. *
  1105. * Description port data callback function of rfcomm
  1106. * connections
  1107. *
  1108. * Returns void
  1109. *
  1110. ******************************************************************************/
  1111. static int bta_jv_port_data_co_cback(uint16_t port_handle, uint8_t* buf,
  1112. uint16_t len, int type) {
  1113. tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
  1114. tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
  1115. VLOG(2) << __func__ << ": p_cb=" << p_cb << ", p_pcb=" << p_pcb
  1116. << ", len=" << len << ", type=" << type;
  1117. if (p_pcb != NULL) {
  1118. switch (type) {
  1119. case DATA_CO_CALLBACK_TYPE_INCOMING:
  1120. return bta_co_rfc_data_incoming(p_pcb->rfcomm_slot_id, (BT_HDR*)buf);
  1121. case DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE:
  1122. return bta_co_rfc_data_outgoing_size(p_pcb->rfcomm_slot_id, (int*)buf);
  1123. case DATA_CO_CALLBACK_TYPE_OUTGOING:
  1124. return bta_co_rfc_data_outgoing(p_pcb->rfcomm_slot_id, buf, len);
  1125. default:
  1126. LOG(ERROR) << __func__ << ": unknown callout type=" << type;
  1127. break;
  1128. }
  1129. }
  1130. return 0;
  1131. }
  1132. /*******************************************************************************
  1133. *
  1134. * Function bta_jv_port_mgmt_cl_cback
  1135. *
  1136. * Description callback for port mamangement function of rfcomm
  1137. * client connections
  1138. *
  1139. * Returns void
  1140. *
  1141. ******************************************************************************/
  1142. static void bta_jv_port_mgmt_cl_cback(uint32_t code, uint16_t port_handle) {
  1143. tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
  1144. tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
  1145. tBTA_JV evt_data;
  1146. RawAddress rem_bda = RawAddress::kEmpty;
  1147. uint16_t lcid;
  1148. tBTA_JV_RFCOMM_CBACK* p_cback; /* the callback function */
  1149. VLOG(2) << __func__ << ": code=" << code << ", port_handle=" << port_handle;
  1150. if (NULL == p_cb || NULL == p_cb->p_cback) return;
  1151. VLOG(2) << __func__ << ": code=" << code << ", port_handle=" << port_handle
  1152. << ", handle=" << p_cb->handle;
  1153. PORT_CheckConnection(port_handle, &rem_bda, &lcid);
  1154. if (code == PORT_SUCCESS) {
  1155. evt_data.rfc_open.handle = p_cb->handle;
  1156. evt_data.rfc_open.status = BTA_JV_SUCCESS;
  1157. evt_data.rfc_open.rem_bda = rem_bda;
  1158. p_pcb->state = BTA_JV_ST_CL_OPEN;
  1159. p_cb->p_cback(BTA_JV_RFCOMM_OPEN_EVT, &evt_data, p_pcb->rfcomm_slot_id);
  1160. } else {
  1161. evt_data.rfc_close.handle = p_cb->handle;
  1162. evt_data.rfc_close.status = BTA_JV_FAILURE;
  1163. evt_data.rfc_close.port_status = code;
  1164. evt_data.rfc_close.async = true;
  1165. if (p_pcb->state == BTA_JV_ST_CL_CLOSING) {
  1166. evt_data.rfc_close.async = false;
  1167. }
  1168. // p_pcb->state = BTA_JV_ST_NONE;
  1169. // p_pcb->cong = false;
  1170. p_cback = p_cb->p_cback;
  1171. p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, p_pcb->rfcomm_slot_id);
  1172. // bta_jv_free_rfc_cb(p_cb, p_pcb);
  1173. }
  1174. }
  1175. /*******************************************************************************
  1176. *
  1177. * Function bta_jv_port_event_cl_cback
  1178. *
  1179. * Description Callback for RFCOMM client port events
  1180. *
  1181. * Returns void
  1182. *
  1183. ******************************************************************************/
  1184. static void bta_jv_port_event_cl_cback(uint32_t code, uint16_t port_handle) {
  1185. tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
  1186. tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
  1187. tBTA_JV evt_data;
  1188. VLOG(2) << __func__ << ": port_handle=" << port_handle;
  1189. if (NULL == p_cb || NULL == p_cb->p_cback) return;
  1190. VLOG(2) << __func__ << ": code=" << loghex(code)
  1191. << ", port_handle=" << port_handle << ", handle=" << p_cb->handle;
  1192. if (code & PORT_EV_RXCHAR) {
  1193. evt_data.data_ind.handle = p_cb->handle;
  1194. p_cb->p_cback(BTA_JV_RFCOMM_DATA_IND_EVT, &evt_data, p_pcb->rfcomm_slot_id);
  1195. }
  1196. if (code & PORT_EV_FC) {
  1197. p_pcb->cong = (code & PORT_EV_FCS) ? false : true;
  1198. evt_data.rfc_cong.cong = p_pcb->cong;
  1199. evt_data.rfc_cong.handle = p_cb->handle;
  1200. evt_data.rfc_cong.status = BTA_JV_SUCCESS;
  1201. p_cb->p_cback(BTA_JV_RFCOMM_CONG_EVT, &evt_data, p_pcb->rfcomm_slot_id);
  1202. }
  1203. if (code & PORT_EV_TXEMPTY) {
  1204. bta_jv_pm_conn_idle(p_pcb->p_pm_cb);
  1205. }
  1206. }
  1207. /* Client initiates an RFCOMM connection */
  1208. void bta_jv_rfcomm_connect(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  1209. uint8_t remote_scn, const RawAddress& peer_bd_addr,
  1210. tBTA_JV_RFCOMM_CBACK* p_cback,
  1211. uint32_t rfcomm_slot_id) {
  1212. uint16_t handle = 0;
  1213. uint32_t event_mask = BTA_JV_RFC_EV_MASK;
  1214. tPORT_STATE port_state;
  1215. /* TODO DM role manager
  1216. L2CA_SetDesireRole(role);
  1217. */
  1218. uint8_t sec_id = bta_jv_alloc_sec_id();
  1219. tBTA_JV_RFCOMM_CL_INIT evt_data;
  1220. memset(&evt_data, 0, sizeof(evt_data));
  1221. evt_data.sec_id = sec_id;
  1222. evt_data.status = BTA_JV_SUCCESS;
  1223. if (0 == sec_id ||
  1224. !BTM_SetSecurityLevel(true, "", sec_id, sec_mask, BT_PSM_RFCOMM,
  1225. BTM_SEC_PROTO_RFCOMM, remote_scn)) {
  1226. evt_data.status = BTA_JV_FAILURE;
  1227. LOG(ERROR) << __func__ << ": sec_id=" << +sec_id
  1228. << " is zero or BTM_SetSecurityLevel failed, remote_scn:"
  1229. << +remote_scn;
  1230. }
  1231. if (evt_data.status == BTA_JV_SUCCESS &&
  1232. RFCOMM_CreateConnection(UUID_SERVCLASS_SERIAL_PORT, remote_scn, false,
  1233. BTA_JV_DEF_RFC_MTU, peer_bd_addr, &handle,
  1234. bta_jv_port_mgmt_cl_cback) != PORT_SUCCESS) {
  1235. LOG(ERROR) << __func__ << ": RFCOMM_CreateConnection failed";
  1236. evt_data.status = BTA_JV_FAILURE;
  1237. }
  1238. if (evt_data.status == BTA_JV_SUCCESS) {
  1239. tBTA_JV_PCB* p_pcb;
  1240. tBTA_JV_RFC_CB* p_cb = bta_jv_alloc_rfc_cb(handle, &p_pcb);
  1241. if (p_cb) {
  1242. p_cb->p_cback = p_cback;
  1243. p_cb->sec_id = sec_id;
  1244. p_cb->scn = 0;
  1245. p_pcb->state = BTA_JV_ST_CL_OPENING;
  1246. p_pcb->rfcomm_slot_id = rfcomm_slot_id;
  1247. evt_data.use_co = true;
  1248. PORT_SetEventCallback(handle, bta_jv_port_event_cl_cback);
  1249. PORT_SetEventMask(handle, event_mask);
  1250. PORT_SetDataCOCallback(handle, bta_jv_port_data_co_cback);
  1251. PORT_GetState(handle, &port_state);
  1252. port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
  1253. PORT_SetState(handle, &port_state);
  1254. evt_data.handle = p_cb->handle;
  1255. } else {
  1256. evt_data.status = BTA_JV_FAILURE;
  1257. LOG(ERROR) << __func__ << ": run out of rfc control block";
  1258. }
  1259. }
  1260. tBTA_JV bta_jv;
  1261. bta_jv.rfc_cl_init = evt_data;
  1262. p_cback(BTA_JV_RFCOMM_CL_INIT_EVT, &bta_jv, rfcomm_slot_id);
  1263. if (bta_jv.rfc_cl_init.status == BTA_JV_FAILURE) {
  1264. if (sec_id) bta_jv_free_sec_id(&sec_id);
  1265. if (handle) RFCOMM_RemoveConnection(handle);
  1266. }
  1267. }
  1268. static int find_rfc_pcb(uint32_t rfcomm_slot_id, tBTA_JV_RFC_CB** cb,
  1269. tBTA_JV_PCB** pcb) {
  1270. *cb = NULL;
  1271. *pcb = NULL;
  1272. int i;
  1273. for (i = 0; i < MAX_RFC_PORTS; i++) {
  1274. uint32_t rfc_handle = bta_jv_cb.port_cb[i].handle & BTA_JV_RFC_HDL_MASK;
  1275. rfc_handle &= ~BTA_JV_RFCOMM_MASK;
  1276. if (rfc_handle && bta_jv_cb.port_cb[i].rfcomm_slot_id == rfcomm_slot_id) {
  1277. *pcb = &bta_jv_cb.port_cb[i];
  1278. *cb = &bta_jv_cb.rfc_cb[rfc_handle - 1];
  1279. VLOG(2) << __func__ << ": FOUND rfc_cb_handle=" << loghex(rfc_handle)
  1280. << ", port.jv_handle=" << loghex((*pcb)->handle)
  1281. << ", state=" << (*pcb)->state
  1282. << ", rfc_cb->handle=" << loghex((*cb)->handle);
  1283. return 1;
  1284. }
  1285. }
  1286. VLOG(2) << __func__
  1287. << ": cannot find rfc_cb from user data:" << rfcomm_slot_id;
  1288. return 0;
  1289. }
  1290. /* Close an RFCOMM connection */
  1291. void bta_jv_rfcomm_close(uint32_t handle, uint32_t rfcomm_slot_id) {
  1292. if (!handle) {
  1293. LOG(ERROR) << __func__ << ": rfc handle is null";
  1294. return;
  1295. }
  1296. VLOG(2) << __func__ << ": rfc handle=" << handle;
  1297. tBTA_JV_RFC_CB* p_cb = NULL;
  1298. tBTA_JV_PCB* p_pcb = NULL;
  1299. if (!find_rfc_pcb(rfcomm_slot_id, &p_cb, &p_pcb)) return;
  1300. bta_jv_free_rfc_cb(p_cb, p_pcb);
  1301. VLOG(2) << __func__ << ": sec id in use=" << get_sec_id_used()
  1302. << ", rfc_cb in use=" << get_rfc_cb_used();
  1303. }
  1304. /*******************************************************************************
  1305. *
  1306. * Function bta_jv_port_mgmt_sr_cback
  1307. *
  1308. * Description callback for port mamangement function of rfcomm
  1309. * server connections
  1310. *
  1311. * Returns void
  1312. *
  1313. ******************************************************************************/
  1314. static void bta_jv_port_mgmt_sr_cback(uint32_t code, uint16_t port_handle) {
  1315. tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
  1316. tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
  1317. tBTA_JV evt_data;
  1318. RawAddress rem_bda = RawAddress::kEmpty;
  1319. uint16_t lcid;
  1320. VLOG(2) << __func__ << ": code=" << code << ", port_handle=" << port_handle;
  1321. if (NULL == p_cb || NULL == p_cb->p_cback) {
  1322. LOG(ERROR) << __func__ << ": p_cb=" << p_cb
  1323. << ", p_cb->p_cback=" << (p_cb ? p_cb->p_cback : 0);
  1324. return;
  1325. }
  1326. uint32_t rfcomm_slot_id = p_pcb->rfcomm_slot_id;
  1327. VLOG(2) << __func__ << ": code=" << code
  1328. << ", port_handle=" << loghex(port_handle)
  1329. << ", handle=" << loghex(p_cb->handle) << ", p_pcb" << p_pcb
  1330. << ", user=" << p_pcb->rfcomm_slot_id;
  1331. int status = PORT_CheckConnection(port_handle, &rem_bda, &lcid);
  1332. int failed = true;
  1333. if (code == PORT_SUCCESS) {
  1334. if (status != PORT_SUCCESS) {
  1335. LOG(ERROR) << __func__ << ": PORT_CheckConnection returned " << status
  1336. << ", although port is supposed to be connected";
  1337. }
  1338. evt_data.rfc_srv_open.handle = p_pcb->handle;
  1339. evt_data.rfc_srv_open.status = BTA_JV_SUCCESS;
  1340. evt_data.rfc_srv_open.rem_bda = rem_bda;
  1341. tBTA_JV_PCB* p_pcb_new_listen = bta_jv_add_rfc_port(p_cb, p_pcb);
  1342. if (p_pcb_new_listen) {
  1343. evt_data.rfc_srv_open.new_listen_handle = p_pcb_new_listen->handle;
  1344. p_pcb_new_listen->rfcomm_slot_id =
  1345. p_cb->p_cback(BTA_JV_RFCOMM_SRV_OPEN_EVT, &evt_data, rfcomm_slot_id);
  1346. VLOG(2) << __func__ << ": curr_sess=" << p_cb->curr_sess
  1347. << ", max_sess=" << p_cb->max_sess;
  1348. failed = false;
  1349. } else
  1350. LOG(ERROR) << __func__ << ": failed to create new listen port";
  1351. }
  1352. if (failed) {
  1353. evt_data.rfc_close.handle = p_cb->handle;
  1354. evt_data.rfc_close.status = BTA_JV_FAILURE;
  1355. evt_data.rfc_close.async = true;
  1356. evt_data.rfc_close.port_status = code;
  1357. p_pcb->cong = false;
  1358. tBTA_JV_RFCOMM_CBACK* p_cback = p_cb->p_cback;
  1359. VLOG(2) << __func__
  1360. << ": PORT_CLOSED before BTA_JV_RFCOMM_CLOSE_EVT: curr_sess="
  1361. << p_cb->curr_sess << ", max_sess=" << p_cb->max_sess;
  1362. if (BTA_JV_ST_SR_CLOSING == p_pcb->state) {
  1363. evt_data.rfc_close.async = false;
  1364. evt_data.rfc_close.status = BTA_JV_SUCCESS;
  1365. }
  1366. // p_pcb->state = BTA_JV_ST_NONE;
  1367. p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, rfcomm_slot_id);
  1368. // bta_jv_free_rfc_cb(p_cb, p_pcb);
  1369. VLOG(2) << __func__
  1370. << ": PORT_CLOSED after BTA_JV_RFCOMM_CLOSE_EVT: curr_sess="
  1371. << p_cb->curr_sess << ", max_sess=" << p_cb->max_sess;
  1372. }
  1373. }
  1374. /*******************************************************************************
  1375. *
  1376. * Function bta_jv_port_event_sr_cback
  1377. *
  1378. * Description Callback for RFCOMM server port events
  1379. *
  1380. * Returns void
  1381. *
  1382. ******************************************************************************/
  1383. static void bta_jv_port_event_sr_cback(uint32_t code, uint16_t port_handle) {
  1384. tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
  1385. tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
  1386. tBTA_JV evt_data;
  1387. if (NULL == p_cb || NULL == p_cb->p_cback) {
  1388. LOG(ERROR) << __func__ << ": p_cb=" << p_cb
  1389. << ", p_cb->p_cback=" << (p_cb ? p_cb->p_cback : 0);
  1390. return;
  1391. }
  1392. VLOG(2) << __func__ << ": code=" << loghex(code)
  1393. << ", port_handle=" << port_handle << ", handle=" << p_cb->handle;
  1394. uint32_t user_data = p_pcb->rfcomm_slot_id;
  1395. if (code & PORT_EV_RXCHAR) {
  1396. evt_data.data_ind.handle = p_cb->handle;
  1397. p_cb->p_cback(BTA_JV_RFCOMM_DATA_IND_EVT, &evt_data, user_data);
  1398. }
  1399. if (code & PORT_EV_FC) {
  1400. p_pcb->cong = (code & PORT_EV_FCS) ? false : true;
  1401. evt_data.rfc_cong.cong = p_pcb->cong;
  1402. evt_data.rfc_cong.handle = p_cb->handle;
  1403. evt_data.rfc_cong.status = BTA_JV_SUCCESS;
  1404. p_cb->p_cback(BTA_JV_RFCOMM_CONG_EVT, &evt_data, user_data);
  1405. }
  1406. if (code & PORT_EV_TXEMPTY) {
  1407. bta_jv_pm_conn_idle(p_pcb->p_pm_cb);
  1408. }
  1409. }
  1410. /*******************************************************************************
  1411. *
  1412. * Function bta_jv_add_rfc_port
  1413. *
  1414. * Description add a port for server when the existing posts is open
  1415. *
  1416. * Returns return a pointer to tBTA_JV_PCB just added
  1417. *
  1418. ******************************************************************************/
  1419. static tBTA_JV_PCB* bta_jv_add_rfc_port(tBTA_JV_RFC_CB* p_cb,
  1420. tBTA_JV_PCB* p_pcb_open) {
  1421. uint8_t used = 0, i, listen = 0;
  1422. uint32_t si = 0;
  1423. tPORT_STATE port_state;
  1424. uint32_t event_mask = BTA_JV_RFC_EV_MASK;
  1425. tBTA_JV_PCB* p_pcb = NULL;
  1426. if (p_cb->max_sess > 1) {
  1427. for (i = 0; i < p_cb->max_sess; i++) {
  1428. if (p_cb->rfc_hdl[i] != 0) {
  1429. p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[i] - 1];
  1430. if (p_pcb->state == BTA_JV_ST_SR_LISTEN) {
  1431. listen++;
  1432. if (p_pcb_open == p_pcb) {
  1433. VLOG(2) << __func__ << ": port_handle=" << p_pcb->port_handle
  1434. << ", change the listen port to open state";
  1435. p_pcb->state = BTA_JV_ST_SR_OPEN;
  1436. } else {
  1437. LOG(ERROR) << __func__
  1438. << ": open pcb not matching listen one, count=" << listen
  1439. << ", listen pcb handle=" << p_pcb->port_handle
  1440. << ", open pcb=" << p_pcb_open->handle;
  1441. return NULL;
  1442. }
  1443. }
  1444. used++;
  1445. } else if (si == 0) {
  1446. si = i + 1;
  1447. }
  1448. }
  1449. VLOG(2) << __func__ << ": max_sess=" << p_cb->max_sess << ", used=" << used
  1450. << ", curr_sess=" << p_cb->curr_sess << ", listen=" << listen
  1451. << ", si=" << si;
  1452. if (used < p_cb->max_sess && listen == 1 && si) {
  1453. si--;
  1454. if (RFCOMM_CreateConnection(p_cb->sec_id, p_cb->scn, true,
  1455. BTA_JV_DEF_RFC_MTU, RawAddress::kAny,
  1456. &(p_cb->rfc_hdl[si]),
  1457. bta_jv_port_mgmt_sr_cback) == PORT_SUCCESS) {
  1458. p_cb->curr_sess++;
  1459. p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[si] - 1];
  1460. p_pcb->state = BTA_JV_ST_SR_LISTEN;
  1461. p_pcb->port_handle = p_cb->rfc_hdl[si];
  1462. p_pcb->rfcomm_slot_id = p_pcb_open->rfcomm_slot_id;
  1463. PORT_ClearKeepHandleFlag(p_pcb->port_handle);
  1464. PORT_SetEventCallback(p_pcb->port_handle, bta_jv_port_event_sr_cback);
  1465. PORT_SetDataCOCallback(p_pcb->port_handle, bta_jv_port_data_co_cback);
  1466. PORT_SetEventMask(p_pcb->port_handle, event_mask);
  1467. PORT_GetState(p_pcb->port_handle, &port_state);
  1468. port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
  1469. PORT_SetState(p_pcb->port_handle, &port_state);
  1470. p_pcb->handle = BTA_JV_RFC_H_S_TO_HDL(p_cb->handle, si);
  1471. VLOG(2) << __func__ << ": p_pcb->handle=" << loghex(p_pcb->handle)
  1472. << ", curr_sess=" << p_cb->curr_sess;
  1473. }
  1474. } else {
  1475. LOG(ERROR) << __func__ << ": cannot create new rfc listen port";
  1476. return NULL;
  1477. }
  1478. }
  1479. VLOG(2) << __func__ << ": sec id in use=" << get_sec_id_used()
  1480. << ", rfc_cb in use=" << get_rfc_cb_used();
  1481. return p_pcb;
  1482. }
  1483. /* waits for an RFCOMM client to connect */
  1484. void bta_jv_rfcomm_start_server(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
  1485. uint8_t local_scn, uint8_t max_session,
  1486. tBTA_JV_RFCOMM_CBACK* p_cback,
  1487. uint32_t rfcomm_slot_id) {
  1488. uint16_t handle = 0;
  1489. uint32_t event_mask = BTA_JV_RFC_EV_MASK;
  1490. tPORT_STATE port_state;
  1491. uint8_t sec_id = 0;
  1492. tBTA_JV_RFC_CB* p_cb = NULL;
  1493. tBTA_JV_PCB* p_pcb;
  1494. tBTA_JV_RFCOMM_START evt_data;
  1495. /* TODO DM role manager
  1496. L2CA_SetDesireRole(role);
  1497. */
  1498. memset(&evt_data, 0, sizeof(evt_data));
  1499. evt_data.status = BTA_JV_FAILURE;
  1500. VLOG(2) << __func__ << ": sec id in use=" << get_sec_id_used()
  1501. << ", rfc_cb in use=" << get_rfc_cb_used();
  1502. do {
  1503. sec_id = bta_jv_alloc_sec_id();
  1504. if (0 == sec_id ||
  1505. !BTM_SetSecurityLevel(false, "JV PORT", sec_id, sec_mask, BT_PSM_RFCOMM,
  1506. BTM_SEC_PROTO_RFCOMM, local_scn)) {
  1507. LOG(ERROR) << __func__ << ": run out of sec_id";
  1508. break;
  1509. }
  1510. if (RFCOMM_CreateConnection(sec_id, local_scn, true, BTA_JV_DEF_RFC_MTU,
  1511. RawAddress::kAny, &handle,
  1512. bta_jv_port_mgmt_sr_cback) != PORT_SUCCESS) {
  1513. LOG(ERROR) << __func__ << ": RFCOMM_CreateConnection failed";
  1514. break;
  1515. }
  1516. p_cb = bta_jv_alloc_rfc_cb(handle, &p_pcb);
  1517. if (!p_cb) {
  1518. LOG(ERROR) << __func__ << ": run out of rfc control block";
  1519. break;
  1520. }
  1521. p_cb->max_sess = max_session;
  1522. p_cb->p_cback = p_cback;
  1523. p_cb->sec_id = sec_id;
  1524. p_cb->scn = local_scn;
  1525. p_pcb->state = BTA_JV_ST_SR_LISTEN;
  1526. p_pcb->rfcomm_slot_id = rfcomm_slot_id;
  1527. evt_data.status = BTA_JV_SUCCESS;
  1528. evt_data.handle = p_cb->handle;
  1529. evt_data.sec_id = sec_id;
  1530. evt_data.use_co = true;
  1531. PORT_ClearKeepHandleFlag(handle);
  1532. PORT_SetEventCallback(handle, bta_jv_port_event_sr_cback);
  1533. PORT_SetEventMask(handle, event_mask);
  1534. PORT_GetState(handle, &port_state);
  1535. port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
  1536. PORT_SetState(handle, &port_state);
  1537. } while (0);
  1538. tBTA_JV bta_jv;
  1539. bta_jv.rfc_start = evt_data;
  1540. p_cback(BTA_JV_RFCOMM_START_EVT, &bta_jv, rfcomm_slot_id);
  1541. if (bta_jv.rfc_start.status == BTA_JV_SUCCESS) {
  1542. PORT_SetDataCOCallback(handle, bta_jv_port_data_co_cback);
  1543. } else {
  1544. if (sec_id) bta_jv_free_sec_id(&sec_id);
  1545. if (handle) RFCOMM_RemoveConnection(handle);
  1546. }
  1547. }
  1548. /* stops an RFCOMM server */
  1549. void bta_jv_rfcomm_stop_server(uint32_t handle, uint32_t rfcomm_slot_id) {
  1550. if (!handle) {
  1551. LOG(ERROR) << __func__ << ": jv handle is null";
  1552. return;
  1553. }
  1554. VLOG(2) << __func__;
  1555. tBTA_JV_RFC_CB* p_cb = NULL;
  1556. tBTA_JV_PCB* p_pcb = NULL;
  1557. if (!find_rfc_pcb(rfcomm_slot_id, &p_cb, &p_pcb)) return;
  1558. VLOG(2) << __func__ << ": p_pcb=" << p_pcb
  1559. << ", p_pcb->port_handle=" << p_pcb->port_handle;
  1560. bta_jv_free_rfc_cb(p_cb, p_pcb);
  1561. VLOG(2) << __func__ << ": sec id in use=" << get_sec_id_used()
  1562. << ", rfc_cb in use=" << get_rfc_cb_used();
  1563. }
  1564. /* write data to an RFCOMM connection */
  1565. void bta_jv_rfcomm_write(uint32_t handle, uint32_t req_id, tBTA_JV_RFC_CB* p_cb,
  1566. tBTA_JV_PCB* p_pcb) {
  1567. if (p_pcb->state == BTA_JV_ST_NONE) {
  1568. LOG(ERROR) << __func__ << ": in state BTA_JV_ST_NONE - cannot write";
  1569. return;
  1570. }
  1571. tBTA_JV_RFCOMM_WRITE evt_data;
  1572. evt_data.status = BTA_JV_FAILURE;
  1573. evt_data.handle = handle;
  1574. evt_data.req_id = req_id;
  1575. evt_data.cong = p_pcb->cong;
  1576. evt_data.len = 0;
  1577. bta_jv_pm_conn_busy(p_pcb->p_pm_cb);
  1578. if (!evt_data.cong &&
  1579. PORT_WriteDataCO(p_pcb->port_handle, &evt_data.len) == PORT_SUCCESS) {
  1580. evt_data.status = BTA_JV_SUCCESS;
  1581. }
  1582. // Update congestion flag
  1583. evt_data.cong = p_pcb->cong;
  1584. if (!p_cb->p_cback) {
  1585. LOG(ERROR) << __func__ << ": No JV callback set";
  1586. return;
  1587. }
  1588. tBTA_JV bta_jv;
  1589. bta_jv.rfc_write = evt_data;
  1590. p_cb->p_cback(BTA_JV_RFCOMM_WRITE_EVT, &bta_jv, p_pcb->rfcomm_slot_id);
  1591. }
  1592. /* Set or free power mode profile for a JV application */
  1593. void bta_jv_set_pm_profile(uint32_t handle, tBTA_JV_PM_ID app_id,
  1594. tBTA_JV_CONN_STATE init_st) {
  1595. tBTA_JV_STATUS status;
  1596. tBTA_JV_PM_CB* p_cb;
  1597. VLOG(2) << __func__ << " handle=" << loghex(handle) << ", app_id=" << app_id
  1598. << ", init_st=" << +init_st;
  1599. /* clear PM control block */
  1600. if (app_id == BTA_JV_PM_ID_CLEAR) {
  1601. status = bta_jv_free_set_pm_profile_cb(handle);
  1602. if (status != BTA_JV_SUCCESS) {
  1603. LOG(WARNING) << __func__ << ": free pm cb failed: reason=" << +status;
  1604. }
  1605. } else /* set PM control block */
  1606. {
  1607. p_cb = bta_jv_alloc_set_pm_profile_cb(handle, app_id);
  1608. if (NULL != p_cb)
  1609. bta_jv_pm_state_change(p_cb, init_st);
  1610. else
  1611. LOG(WARNING) << __func__ << ": failed";
  1612. }
  1613. }
  1614. /*******************************************************************************
  1615. *
  1616. * Function bta_jv_pm_conn_busy
  1617. *
  1618. * Description set pm connection busy state (input param safe)
  1619. *
  1620. * Params p_cb: pm control block of jv connection
  1621. *
  1622. * Returns void
  1623. *
  1624. ******************************************************************************/
  1625. static void bta_jv_pm_conn_busy(tBTA_JV_PM_CB* p_cb) {
  1626. if ((NULL != p_cb) && (BTA_JV_PM_IDLE_ST == p_cb->state))
  1627. bta_jv_pm_state_change(p_cb, BTA_JV_CONN_BUSY);
  1628. }
  1629. /*******************************************************************************
  1630. *
  1631. * Function bta_jv_pm_conn_busy
  1632. *
  1633. * Description set pm connection busy state (input param safe)
  1634. *
  1635. * Params p_cb: pm control block of jv connection
  1636. *
  1637. * Returns void
  1638. *
  1639. ******************************************************************************/
  1640. static void bta_jv_pm_conn_idle(tBTA_JV_PM_CB* p_cb) {
  1641. if ((NULL != p_cb) && (BTA_JV_PM_IDLE_ST != p_cb->state))
  1642. bta_jv_pm_state_change(p_cb, BTA_JV_CONN_IDLE);
  1643. }
  1644. /*******************************************************************************
  1645. *
  1646. * Function bta_jv_pm_state_change
  1647. *
  1648. * Description Notify power manager there is state change
  1649. *
  1650. * Params p_cb: must be NONE NULL
  1651. *
  1652. * Returns void
  1653. *
  1654. ******************************************************************************/
  1655. static void bta_jv_pm_state_change(tBTA_JV_PM_CB* p_cb,
  1656. const tBTA_JV_CONN_STATE state) {
  1657. VLOG(2) << __func__ << ": p_cb=" << p_cb
  1658. << ", handle=" << loghex(p_cb->handle)
  1659. << ", busy/idle_state=" << p_cb->state << ", app_id=" << p_cb->app_id
  1660. << ", conn_state=" << state;
  1661. switch (state) {
  1662. case BTA_JV_CONN_OPEN:
  1663. bta_sys_conn_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1664. break;
  1665. case BTA_JV_CONN_CLOSE:
  1666. bta_sys_conn_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1667. break;
  1668. case BTA_JV_APP_OPEN:
  1669. bta_sys_app_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1670. break;
  1671. case BTA_JV_APP_CLOSE:
  1672. bta_sys_app_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1673. break;
  1674. case BTA_JV_SCO_OPEN:
  1675. bta_sys_sco_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1676. break;
  1677. case BTA_JV_SCO_CLOSE:
  1678. bta_sys_sco_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1679. break;
  1680. case BTA_JV_CONN_IDLE:
  1681. p_cb->state = BTA_JV_PM_IDLE_ST;
  1682. bta_sys_idle(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1683. break;
  1684. case BTA_JV_CONN_BUSY:
  1685. p_cb->state = BTA_JV_PM_BUSY_ST;
  1686. bta_sys_busy(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
  1687. break;
  1688. default:
  1689. LOG(WARNING) << __func__ << ": Invalid state=" << +state;
  1690. break;
  1691. }
  1692. }
  1693. /******************************************************************************/
  1694. static struct fc_channel* fcchan_get(uint16_t chan, char create) {
  1695. struct fc_channel* t = fc_channels;
  1696. static tL2CAP_FIXED_CHNL_REG fcr = {
  1697. .pL2CA_FixedConn_Cb = fcchan_conn_chng_cbk,
  1698. .pL2CA_FixedData_Cb = fcchan_data_cbk,
  1699. .default_idle_tout = 0xffff,
  1700. .fixed_chnl_opts =
  1701. {
  1702. .mode = L2CAP_FCR_BASIC_MODE,
  1703. .max_transmit = 0xFF,
  1704. .rtrans_tout = 2000,
  1705. .mon_tout = 12000,
  1706. .mps = 670,
  1707. .tx_win_sz = 1,
  1708. },
  1709. };
  1710. while (t && t->chan != chan) t = t->next;
  1711. if (t)
  1712. return t;
  1713. else if (!create)
  1714. return NULL; /* we cannot alloc a struct if not asked to */
  1715. t = static_cast<struct fc_channel*>(osi_calloc(sizeof(*t)));
  1716. t->chan = chan;
  1717. if (!L2CA_RegisterFixedChannel(chan, &fcr)) {
  1718. osi_free(t);
  1719. return NULL;
  1720. }
  1721. // link it in
  1722. t->next = fc_channels;
  1723. fc_channels = t;
  1724. return t;
  1725. }
  1726. /* pass NULL to find servers */
  1727. static struct fc_client* fcclient_find_by_addr(struct fc_client* start,
  1728. const RawAddress* addr) {
  1729. struct fc_client* t = start;
  1730. while (t) {
  1731. /* match client if have addr */
  1732. if (addr && addr == &t->remote_addr) break;
  1733. /* match server if do not have addr */
  1734. if (!addr && t->server) break;
  1735. t = t->next_all_list;
  1736. }
  1737. return t;
  1738. }
  1739. static struct fc_client* fcclient_find_by_id(uint32_t id) {
  1740. struct fc_client* t = fc_clients;
  1741. while (t && t->id != id) t = t->next_all_list;
  1742. return t;
  1743. }
  1744. static struct fc_client* fcclient_alloc(uint16_t chan, char server,
  1745. const uint8_t* sec_id_to_use) {
  1746. struct fc_channel* fc = fcchan_get(chan, true);
  1747. struct fc_client* t;
  1748. uint8_t sec_id;
  1749. if (!fc) return NULL;
  1750. if (fc->has_server && server)
  1751. return NULL; /* no way to have multiple servers on same channel */
  1752. if (sec_id_to_use)
  1753. sec_id = *sec_id_to_use;
  1754. else
  1755. sec_id = bta_jv_alloc_sec_id();
  1756. t = static_cast<fc_client*>(osi_calloc(sizeof(*t)));
  1757. // Allocate it a unique ID
  1758. do {
  1759. t->id = ++fc_next_id;
  1760. } while (!t->id || fcclient_find_by_id(t->id));
  1761. // Populate some params
  1762. t->chan = chan;
  1763. t->server = server;
  1764. // Get a security id
  1765. t->sec_id = sec_id;
  1766. // Link it in to global list
  1767. t->next_all_list = fc_clients;
  1768. fc_clients = t;
  1769. // Link it in to channel list
  1770. t->next_chan_list = fc->clients;
  1771. fc->clients = t;
  1772. // Update channel if needed
  1773. if (server) fc->has_server = true;
  1774. return t;
  1775. }
  1776. static void fcclient_free(struct fc_client* fc) {
  1777. struct fc_client* t = fc_clients;
  1778. struct fc_channel* tc = fcchan_get(fc->chan, false);
  1779. // remove from global list
  1780. while (t && t->next_all_list != fc) t = t->next_all_list;
  1781. if (!t && fc != fc_clients) return; /* prevent double-free */
  1782. if (t)
  1783. t->next_all_list = fc->next_all_list;
  1784. else
  1785. fc_clients = fc->next_all_list;
  1786. // remove from channel list
  1787. if (tc) {
  1788. t = tc->clients;
  1789. while (t && t->next_chan_list != fc) t = t->next_chan_list;
  1790. if (t)
  1791. t->next_chan_list = fc->next_chan_list;
  1792. else
  1793. tc->clients = fc->next_chan_list;
  1794. // if was server then channel no longer has a server
  1795. if (fc->server) tc->has_server = false;
  1796. }
  1797. // free security id
  1798. bta_jv_free_sec_id(&fc->sec_id);
  1799. osi_free(fc);
  1800. }
  1801. static void fcchan_conn_chng_cbk(uint16_t chan, const RawAddress& bd_addr,
  1802. bool connected, uint16_t reason,
  1803. tBT_TRANSPORT transport) {
  1804. tBTA_JV init_evt;
  1805. tBTA_JV open_evt;
  1806. struct fc_channel* tc;
  1807. struct fc_client *t = NULL, *new_conn;
  1808. tBTA_JV_L2CAP_CBACK* p_cback = NULL;
  1809. char call_init = false;
  1810. uint32_t l2cap_socket_id;
  1811. tc = fcchan_get(chan, false);
  1812. if (tc) {
  1813. t = fcclient_find_by_addr(
  1814. tc->clients,
  1815. &bd_addr); // try to find an open socked for that addr
  1816. if (t) {
  1817. p_cback = t->p_cback;
  1818. l2cap_socket_id = t->l2cap_socket_id;
  1819. } else {
  1820. t = fcclient_find_by_addr(
  1821. tc->clients,
  1822. NULL); // try to find a listening socked for that channel
  1823. if (t) {
  1824. // found: create a normal connection socket and assign the connection to
  1825. // it
  1826. new_conn = fcclient_alloc(chan, false, &t->sec_id);
  1827. if (new_conn) {
  1828. new_conn->remote_addr = bd_addr;
  1829. new_conn->p_cback = NULL; // for now
  1830. new_conn->init_called = true; /*nop need to do it again */
  1831. p_cback = t->p_cback;
  1832. l2cap_socket_id = t->l2cap_socket_id;
  1833. t = new_conn;
  1834. }
  1835. } else {
  1836. // drop it
  1837. return;
  1838. }
  1839. }
  1840. }
  1841. if (t) {
  1842. if (!t->init_called) {
  1843. call_init = true;
  1844. t->init_called = true;
  1845. init_evt.l2c_cl_init.handle = t->id;
  1846. init_evt.l2c_cl_init.status = BTA_JV_SUCCESS;
  1847. init_evt.l2c_cl_init.sec_id = t->sec_id;
  1848. }
  1849. open_evt.l2c_open.handle = t->id;
  1850. open_evt.l2c_open.tx_mtu = 23; /* 23, why not ?*/
  1851. memcpy(&open_evt.l2c_le_open.rem_bda, &t->remote_addr,
  1852. sizeof(open_evt.l2c_le_open.rem_bda));
  1853. // TODO: (apanicke) Change the way these functions work so that casting
  1854. // isn't needed
  1855. open_evt.l2c_le_open.p_p_cback = (void**)&t->p_cback;
  1856. open_evt.l2c_le_open.p_user_data = (void**)&t->l2cap_socket_id;
  1857. open_evt.l2c_le_open.status = BTA_JV_SUCCESS;
  1858. if (connected) {
  1859. open_evt.l2c_open.status = BTA_JV_SUCCESS;
  1860. } else {
  1861. fcclient_free(t);
  1862. open_evt.l2c_open.status = BTA_JV_FAILURE;
  1863. }
  1864. }
  1865. if (call_init) p_cback(BTA_JV_L2CAP_CL_INIT_EVT, &init_evt, l2cap_socket_id);
  1866. // call this with lock taken so socket does not disappear from under us */
  1867. if (p_cback) {
  1868. p_cback(BTA_JV_L2CAP_OPEN_EVT, &open_evt, l2cap_socket_id);
  1869. if (!t->p_cback) /* no callback set, means they do not want this one... */
  1870. fcclient_free(t);
  1871. }
  1872. }
  1873. static void fcchan_data_cbk(uint16_t chan, const RawAddress& bd_addr,
  1874. BT_HDR* p_buf) {
  1875. tBTA_JV evt_data;
  1876. struct fc_channel* tc;
  1877. struct fc_client* t = NULL;
  1878. tBTA_JV_L2CAP_CBACK* sock_cback = NULL;
  1879. uint32_t sock_id;
  1880. tc = fcchan_get(chan, false);
  1881. if (tc) {
  1882. // try to find an open socked for that addr and channel
  1883. t = fcclient_find_by_addr(tc->clients, &bd_addr);
  1884. }
  1885. if (!t) {
  1886. // no socket -> drop it
  1887. return;
  1888. }
  1889. sock_cback = t->p_cback;
  1890. sock_id = t->l2cap_socket_id;
  1891. evt_data.le_data_ind.handle = t->id;
  1892. evt_data.le_data_ind.p_buf = p_buf;
  1893. if (sock_cback) sock_cback(BTA_JV_L2CAP_DATA_IND_EVT, &evt_data, sock_id);
  1894. }
  1895. /** makes an le l2cap client connection */
  1896. void bta_jv_l2cap_connect_le(uint16_t remote_chan,
  1897. const RawAddress& peer_bd_addr,
  1898. tBTA_JV_L2CAP_CBACK* p_cback,
  1899. uint32_t l2cap_socket_id) {
  1900. tBTA_JV evt;
  1901. uint32_t id;
  1902. char call_init_f = true;
  1903. struct fc_client* t;
  1904. evt.l2c_cl_init.handle = GAP_INVALID_HANDLE;
  1905. evt.l2c_cl_init.status = BTA_JV_FAILURE;
  1906. t = fcclient_alloc(remote_chan, false, NULL);
  1907. if (!t) {
  1908. p_cback(BTA_JV_L2CAP_CL_INIT_EVT, &evt, l2cap_socket_id);
  1909. return;
  1910. }
  1911. t->p_cback = p_cback;
  1912. t->l2cap_socket_id = l2cap_socket_id;
  1913. t->remote_addr = peer_bd_addr;
  1914. id = t->id;
  1915. t->init_called = false;
  1916. if (L2CA_ConnectFixedChnl(t->chan, t->remote_addr)) {
  1917. evt.l2c_cl_init.status = BTA_JV_SUCCESS;
  1918. evt.l2c_cl_init.handle = id;
  1919. }
  1920. // it could have been deleted/moved from under us, so re-find it */
  1921. t = fcclient_find_by_id(id);
  1922. if (t) {
  1923. if (evt.l2c_cl_init.status == BTA_JV_SUCCESS) {
  1924. call_init_f = !t->init_called;
  1925. } else {
  1926. fcclient_free(t);
  1927. t = NULL;
  1928. }
  1929. }
  1930. if (call_init_f) p_cback(BTA_JV_L2CAP_CL_INIT_EVT, &evt, l2cap_socket_id);
  1931. if (t) {
  1932. t->init_called = true;
  1933. }
  1934. }
  1935. /* stops an LE L2CAP server */
  1936. void bta_jv_l2cap_stop_server_le(uint16_t local_chan) {
  1937. struct fc_client* fcclient;
  1938. tBTA_JV evt;
  1939. evt.l2c_close.status = BTA_JV_FAILURE;
  1940. evt.l2c_close.async = false;
  1941. evt.l2c_close.handle = GAP_INVALID_HANDLE;
  1942. struct fc_channel* fcchan = fcchan_get(local_chan, false);
  1943. if (fcchan) {
  1944. while ((fcclient = fcchan->clients)) {
  1945. tBTA_JV_L2CAP_CBACK* p_cback = fcclient->p_cback;
  1946. uint32_t l2cap_socket_id = fcclient->l2cap_socket_id;
  1947. evt.l2c_close.handle = fcclient->id;
  1948. evt.l2c_close.status = BTA_JV_SUCCESS;
  1949. evt.l2c_close.async = false;
  1950. fcclient_free(fcclient);
  1951. if (p_cback) p_cback(BTA_JV_L2CAP_CLOSE_EVT, &evt, l2cap_socket_id);
  1952. }
  1953. }
  1954. }
  1955. /** starts an LE L2CAP server */
  1956. void bta_jv_l2cap_start_server_le(uint16_t local_chan,
  1957. tBTA_JV_L2CAP_CBACK* p_cback,
  1958. uint32_t l2cap_socket_id) {
  1959. tBTA_JV_L2CAP_START evt_data;
  1960. evt_data.handle = GAP_INVALID_HANDLE;
  1961. evt_data.status = BTA_JV_FAILURE;
  1962. struct fc_client* t = fcclient_alloc(local_chan, true, NULL);
  1963. if (!t) goto out;
  1964. t->p_cback = p_cback;
  1965. t->l2cap_socket_id = l2cap_socket_id;
  1966. // if we got here, we're registered...
  1967. evt_data.status = BTA_JV_SUCCESS;
  1968. evt_data.handle = t->id;
  1969. evt_data.sec_id = t->sec_id;
  1970. out:
  1971. tBTA_JV bta_jv;
  1972. bta_jv.l2c_start = evt_data;
  1973. p_cback(BTA_JV_L2CAP_START_EVT, &bta_jv, l2cap_socket_id);
  1974. }
  1975. /* close a fixed channel connection. calls no callbacks. idempotent */
  1976. extern void bta_jv_l2cap_close_fixed(uint32_t handle) {
  1977. struct fc_client* t = fcclient_find_by_id(handle);
  1978. if (t) fcclient_free(t);
  1979. }