port_api.cc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /******************************************************************************
  2. *
  3. * Copyright 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains the Serial Port API code
  21. *
  22. ******************************************************************************/
  23. #define LOG_TAG "bt_port_api"
  24. #include <base/logging.h>
  25. #include <string.h>
  26. #include "osi/include/log.h"
  27. #include "osi/include/mutex.h"
  28. #include "bt_common.h"
  29. #include "btm_api.h"
  30. #include "btm_int.h"
  31. #include "l2c_api.h"
  32. #include "port_api.h"
  33. #include "port_int.h"
  34. #include "rfc_int.h"
  35. #include "rfcdefs.h"
  36. #include "sdp_api.h"
  37. /* duration of break in 200ms units */
  38. #define PORT_BREAK_DURATION 1
  39. #define info(fmt, ...) LOG_INFO(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__)
  40. #define debug(fmt, ...) LOG_DEBUG(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__)
  41. #define error(fmt, ...) \
  42. LOG_ERROR(LOG_TAG, "## ERROR : %s: " fmt "##", __func__, ##__VA_ARGS__)
  43. #define asrt(s) \
  44. if (!(s)) \
  45. LOG_ERROR(LOG_TAG, "## %s assert %s failed at line:%d ##", __func__, #s, \
  46. __LINE__)
  47. /* Mapping from PORT_* result codes to human readable strings. */
  48. static const char* result_code_strings[] = {"Success",
  49. "Unknown error",
  50. "Already opened",
  51. "Command pending",
  52. "App not registered",
  53. "No memory",
  54. "No resources",
  55. "Bad BD address",
  56. "Unspecified error",
  57. "Bad handle",
  58. "Not opened",
  59. "Line error",
  60. "Start failed",
  61. "Parameter negotiation failed",
  62. "Port negotiation failed",
  63. "Sec failed",
  64. "Peer connection failed",
  65. "Peer failed",
  66. "Peer timeout",
  67. "Closed",
  68. "TX full",
  69. "Local closed",
  70. "Local timeout",
  71. "TX queue disabled",
  72. "Page timeout",
  73. "Invalid SCN",
  74. "Unknown result code"};
  75. /*******************************************************************************
  76. *
  77. * Function RFCOMM_CreateConnection
  78. *
  79. * Description RFCOMM_CreateConnection function is used from the
  80. * application to establish serial port connection to the peer
  81. * device, or allow RFCOMM to accept a connection from the peer
  82. * application.
  83. *
  84. * Parameters: scn - Service Channel Number as registered with
  85. * the SDP (server) or obtained using SDP from
  86. * the peer device (client).
  87. * is_server - true if requesting application is a server
  88. * mtu - Maximum frame size the application can accept
  89. * bd_addr - address of the peer (client)
  90. * mask - specifies events to be enabled. A value
  91. * of zero disables all events.
  92. * p_handle - OUT pointer to the handle.
  93. * p_mgmt_cb - pointer to callback function to receive
  94. * connection up/down events.
  95. * Notes:
  96. *
  97. * Server can call this function with the same scn parameter multiple times if
  98. * it is ready to accept multiple simulteneous connections.
  99. *
  100. * DLCI for the connection is (scn * 2 + 1) if client originates connection on
  101. * existing none initiator multiplexer channel. Otherwise it is (scn * 2).
  102. * For the server DLCI can be changed later if client will be calling it using
  103. * (scn * 2 + 1) dlci.
  104. *
  105. ******************************************************************************/
  106. int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server,
  107. uint16_t mtu, const RawAddress& bd_addr,
  108. uint16_t* p_handle, tPORT_CALLBACK* p_mgmt_cb) {
  109. *p_handle = 0;
  110. if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) {
  111. // Server Channel Number (SCN) should be in range [1, 30]
  112. LOG(ERROR) << __func__ << ": Invalid SCN, bd_addr=" << bd_addr
  113. << ", scn=" << static_cast<int>(scn)
  114. << ", is_server=" << is_server
  115. << ", mtu=" << static_cast<int>(mtu)
  116. << ", uuid=" << loghex(uuid);
  117. return (PORT_INVALID_SCN);
  118. }
  119. // For client that originates connection on the existing none initiator
  120. // multiplexer channel, DLCI should be odd.
  121. uint8_t dlci;
  122. tRFC_MCB* p_mcb = port_find_mcb(bd_addr);
  123. if (p_mcb && !p_mcb->is_initiator && !is_server) {
  124. dlci = static_cast<uint8_t>((scn << 1) + 1);
  125. } else {
  126. dlci = (scn << 1);
  127. }
  128. // On the client side, do not allow the same (dlci, bd_addr) to be opened
  129. // twice by application
  130. tPORT* p_port;
  131. if (!is_server) {
  132. p_port = port_find_port(dlci, bd_addr);
  133. if (p_port != nullptr) {
  134. // if existing port is also a client port, error out
  135. if (!p_port->is_server) {
  136. LOG(ERROR) << __func__ << ": already at opened state "
  137. << static_cast<int>(p_port->state)
  138. << ", RFC_state=" << static_cast<int>(p_port->rfc.state)
  139. << ", MCB_state="
  140. << (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0)
  141. << ", bd_addr=" << bd_addr << ", scn=" << std::to_string(scn)
  142. << ", is_server=" << is_server << ", mtu=" << mtu
  143. << ", uuid=" << loghex(uuid) << ", dlci=" << +dlci
  144. << ", p_mcb=" << p_mcb
  145. << ", port=" << std::to_string(p_port->handle);
  146. *p_handle = p_port->handle;
  147. return (PORT_ALREADY_OPENED);
  148. }
  149. }
  150. }
  151. // On the server side, always allocate a new port.
  152. p_port = port_allocate_port(dlci, bd_addr);
  153. if (p_port == nullptr) {
  154. LOG(ERROR) << __func__ << ": no resources, bd_addr=" << bd_addr
  155. << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
  156. << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
  157. << ", dlci=" << +dlci;
  158. return PORT_NO_RESOURCES;
  159. }
  160. *p_handle = p_port->handle;
  161. // Get default signal state
  162. switch (uuid) {
  163. case UUID_PROTOCOL_OBEX:
  164. p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE;
  165. break;
  166. case UUID_SERVCLASS_SERIAL_PORT:
  167. p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE;
  168. break;
  169. case UUID_SERVCLASS_LAN_ACCESS_USING_PPP:
  170. p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE;
  171. break;
  172. case UUID_SERVCLASS_DIALUP_NETWORKING:
  173. case UUID_SERVCLASS_FAX:
  174. p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE;
  175. break;
  176. default:
  177. p_port->default_signal_state =
  178. (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
  179. break;
  180. }
  181. // Assign port specific values
  182. p_port->state = PORT_STATE_OPENING;
  183. p_port->uuid = uuid;
  184. p_port->is_server = is_server;
  185. p_port->scn = scn;
  186. p_port->ev_mask = 0;
  187. // Find MTU
  188. // If the MTU is not specified (0), keep MTU decision until the PN frame has
  189. // to be send at that time connection should be established and we will know
  190. // for sure our prefered MTU
  191. uint16_t rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
  192. if (mtu) {
  193. p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu;
  194. } else {
  195. p_port->mtu = rfcomm_mtu;
  196. }
  197. // Other states
  198. // server doesn't need to release port when closing
  199. if (is_server) {
  200. p_port->keep_port_handle = true;
  201. // keep mtu that user asked, p_port->mtu could be updated during param
  202. // negotiation
  203. p_port->keep_mtu = p_port->mtu;
  204. }
  205. p_port->local_ctrl.modem_signal = p_port->default_signal_state;
  206. p_port->local_ctrl.fc = false;
  207. p_port->p_mgmt_callback = p_mgmt_cb;
  208. p_port->bd_addr = bd_addr;
  209. LOG(INFO) << __func__ << ": bd_addr=" << bd_addr
  210. << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
  211. << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
  212. << ", dlci=" << std::to_string(dlci)
  213. << ", signal_state=" << loghex(p_port->default_signal_state)
  214. << ", p_port=" << p_port;
  215. // If this is not initiator of the connection need to just wait
  216. if (p_port->is_server) {
  217. return (PORT_SUCCESS);
  218. }
  219. // Open will be continued after security checks are passed
  220. return port_open_continue(p_port);
  221. }
  222. /*******************************************************************************
  223. *
  224. * Function RFCOMM_RemoveConnection
  225. *
  226. * Description This function is called to close the specified connection.
  227. *
  228. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  229. *
  230. ******************************************************************************/
  231. int RFCOMM_RemoveConnection(uint16_t handle) {
  232. tPORT* p_port;
  233. RFCOMM_TRACE_API("RFCOMM_RemoveConnection() handle:%d", handle);
  234. /* Check if handle is valid to avoid crashing */
  235. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  236. RFCOMM_TRACE_ERROR("RFCOMM_RemoveConnection() BAD handle:%d", handle);
  237. return (PORT_BAD_HANDLE);
  238. }
  239. p_port = &rfc_cb.port.port[handle - 1];
  240. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  241. RFCOMM_TRACE_EVENT("RFCOMM_RemoveConnection() Not opened:%d", handle);
  242. return (PORT_SUCCESS);
  243. }
  244. p_port->state = PORT_STATE_CLOSING;
  245. port_start_close(p_port);
  246. return (PORT_SUCCESS);
  247. }
  248. /*******************************************************************************
  249. *
  250. * Function RFCOMM_RemoveServer
  251. *
  252. * Description This function is called to close the server port.
  253. *
  254. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  255. *
  256. ******************************************************************************/
  257. int RFCOMM_RemoveServer(uint16_t handle) {
  258. /* Check if handle is valid to avoid crashing */
  259. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  260. LOG(ERROR) << __func__ << ": bad handle " << handle;
  261. return (PORT_BAD_HANDLE);
  262. }
  263. tPORT* p_port = &rfc_cb.port.port[handle - 1];
  264. /* Do not report any events to the client any more. */
  265. p_port->p_mgmt_callback = nullptr;
  266. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  267. VLOG(1) << __func__ << ": handle " << handle << " not opened";
  268. return (PORT_SUCCESS);
  269. }
  270. LOG(INFO) << __func__ << ": handle=" << handle;
  271. /* this port will be deallocated after closing */
  272. p_port->keep_port_handle = false;
  273. p_port->state = PORT_STATE_CLOSING;
  274. port_start_close(p_port);
  275. return (PORT_SUCCESS);
  276. }
  277. /*******************************************************************************
  278. *
  279. * Function PORT_SetEventCallback
  280. *
  281. * Description This function is called to provide an address of the
  282. * function which will be called when one of the events
  283. * specified in the mask occures.
  284. *
  285. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  286. * p_callback - address of the callback function which should
  287. * be called from the RFCOMM when an event
  288. * specified in the mask occures.
  289. *
  290. *
  291. ******************************************************************************/
  292. int PORT_SetEventCallback(uint16_t port_handle, tPORT_CALLBACK* p_port_cb) {
  293. tPORT* p_port;
  294. /* Check if handle is valid to avoid crashing */
  295. if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
  296. return (PORT_BAD_HANDLE);
  297. }
  298. p_port = &rfc_cb.port.port[port_handle - 1];
  299. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  300. return (PORT_NOT_OPENED);
  301. }
  302. RFCOMM_TRACE_API("PORT_SetEventCallback() handle:%d", port_handle);
  303. p_port->p_callback = p_port_cb;
  304. return (PORT_SUCCESS);
  305. }
  306. /*******************************************************************************
  307. *
  308. * Function PORT_ClearKeepHandleFlag
  309. *
  310. * Description Clear the keep handle flag, which will cause not to keep the
  311. * port handle open when closed
  312. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  313. *
  314. ******************************************************************************/
  315. int PORT_ClearKeepHandleFlag(uint16_t port_handle) {
  316. tPORT* p_port;
  317. /* Check if handle is valid to avoid crashing */
  318. if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
  319. return (PORT_BAD_HANDLE);
  320. }
  321. p_port = &rfc_cb.port.port[port_handle - 1];
  322. p_port->keep_port_handle = 0;
  323. return (PORT_SUCCESS);
  324. }
  325. /*******************************************************************************
  326. *
  327. * Function PORT_SetDataCallback
  328. *
  329. * Description This function is when a data packet is received
  330. *
  331. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  332. * p_callback - address of the callback function which should
  333. * be called from the RFCOMM when data packet
  334. * is received.
  335. *
  336. *
  337. ******************************************************************************/
  338. int PORT_SetDataCallback(uint16_t port_handle, tPORT_DATA_CALLBACK* p_port_cb) {
  339. tPORT* p_port;
  340. RFCOMM_TRACE_API("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle,
  341. p_port_cb);
  342. /* Check if handle is valid to avoid crashing */
  343. if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
  344. return (PORT_BAD_HANDLE);
  345. }
  346. p_port = &rfc_cb.port.port[port_handle - 1];
  347. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  348. return (PORT_NOT_OPENED);
  349. }
  350. p_port->p_data_callback = p_port_cb;
  351. return (PORT_SUCCESS);
  352. }
  353. /*******************************************************************************
  354. *
  355. * Function PORT_SetCODataCallback
  356. *
  357. * Description This function is when a data packet is received
  358. *
  359. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  360. * p_callback - address of the callback function which should
  361. * be called from the RFCOMM when data packet
  362. * is received.
  363. *
  364. *
  365. ******************************************************************************/
  366. int PORT_SetDataCOCallback(uint16_t port_handle,
  367. tPORT_DATA_CO_CALLBACK* p_port_cb) {
  368. tPORT* p_port;
  369. RFCOMM_TRACE_API("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle,
  370. p_port_cb);
  371. /* Check if handle is valid to avoid crashing */
  372. if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
  373. return (PORT_BAD_HANDLE);
  374. }
  375. p_port = &rfc_cb.port.port[port_handle - 1];
  376. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  377. return (PORT_NOT_OPENED);
  378. }
  379. p_port->p_data_co_callback = p_port_cb;
  380. return (PORT_SUCCESS);
  381. }
  382. /*******************************************************************************
  383. *
  384. * Function PORT_SetEventMask
  385. *
  386. * Description This function is called to close the specified connection.
  387. *
  388. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  389. * mask - Bitmask of the events the host is interested in
  390. *
  391. ******************************************************************************/
  392. int PORT_SetEventMask(uint16_t port_handle, uint32_t mask) {
  393. tPORT* p_port;
  394. RFCOMM_TRACE_API("PORT_SetEventMask() handle:%d mask:0x%x", port_handle,
  395. mask);
  396. /* Check if handle is valid to avoid crashing */
  397. if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
  398. return (PORT_BAD_HANDLE);
  399. }
  400. p_port = &rfc_cb.port.port[port_handle - 1];
  401. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  402. return (PORT_NOT_OPENED);
  403. }
  404. p_port->ev_mask = mask;
  405. return (PORT_SUCCESS);
  406. }
  407. /*******************************************************************************
  408. *
  409. * Function PORT_CheckConnection
  410. *
  411. * Description This function returns PORT_SUCCESS if connection referenced
  412. * by handle is up and running
  413. *
  414. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  415. * bd_addr - OUT bd_addr of the peer
  416. * p_lcid - OUT L2CAP's LCID
  417. *
  418. ******************************************************************************/
  419. int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr,
  420. uint16_t* p_lcid) {
  421. /* Check if handle is valid to avoid crashing */
  422. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  423. return (PORT_BAD_HANDLE);
  424. }
  425. tPORT* p_port = &rfc_cb.port.port[handle - 1];
  426. RFCOMM_TRACE_DEBUG(
  427. "%s: handle=%d, in_use=%d, port_state=%d, p_mcb=%p, peer_ready=%d, "
  428. "rfc_state=%d",
  429. __func__, handle, p_port->in_use, p_port->state, p_port->rfc.p_mcb,
  430. (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->peer_ready : -1),
  431. p_port->rfc.state);
  432. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  433. return (PORT_NOT_OPENED);
  434. }
  435. if (!p_port->rfc.p_mcb || !p_port->rfc.p_mcb->peer_ready ||
  436. (p_port->rfc.state != RFC_STATE_OPENED)) {
  437. return (PORT_LINE_ERR);
  438. }
  439. *bd_addr = p_port->rfc.p_mcb->bd_addr;
  440. if (p_lcid) *p_lcid = p_port->rfc.p_mcb->lcid;
  441. return (PORT_SUCCESS);
  442. }
  443. /*******************************************************************************
  444. *
  445. * Function PORT_IsOpening
  446. *
  447. * Description This function returns true if there is any RFCOMM connection
  448. * opening in process.
  449. *
  450. * Parameters: true if any connection opening is found
  451. * bd_addr - bd_addr of the peer
  452. *
  453. ******************************************************************************/
  454. bool PORT_IsOpening(RawAddress* bd_addr) {
  455. /* Check for any rfc_mcb which is in the middle of opening. */
  456. for (auto& multiplexer_cb : rfc_cb.port.rfc_mcb) {
  457. if ((multiplexer_cb.state > RFC_MX_STATE_IDLE) &&
  458. (multiplexer_cb.state < RFC_MX_STATE_CONNECTED)) {
  459. *bd_addr = multiplexer_cb.bd_addr;
  460. return true;
  461. }
  462. if (multiplexer_cb.state == RFC_MX_STATE_CONNECTED) {
  463. bool found_port = false;
  464. tPORT* p_port = nullptr;
  465. for (tPORT& port : rfc_cb.port.port) {
  466. if (port.rfc.p_mcb == &multiplexer_cb) {
  467. found_port = true;
  468. p_port = &port;
  469. break;
  470. }
  471. }
  472. if ((!found_port) ||
  473. (found_port && (p_port->rfc.state < RFC_STATE_OPENED))) {
  474. /* Port is not established yet. */
  475. *bd_addr = multiplexer_cb.bd_addr;
  476. return true;
  477. }
  478. }
  479. }
  480. return false;
  481. }
  482. /*******************************************************************************
  483. *
  484. * Function PORT_SetState
  485. *
  486. * Description This function configures connection according to the
  487. * specifications in the tPORT_STATE structure.
  488. *
  489. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  490. * p_settings - Pointer to a tPORT_STATE structure containing
  491. * configuration information for the connection.
  492. *
  493. *
  494. ******************************************************************************/
  495. int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
  496. tPORT* p_port;
  497. uint8_t baud_rate;
  498. RFCOMM_TRACE_API("PORT_SetState() handle:%d", handle);
  499. /* Check if handle is valid to avoid crashing */
  500. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  501. return (PORT_BAD_HANDLE);
  502. }
  503. p_port = &rfc_cb.port.port[handle - 1];
  504. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  505. return (PORT_NOT_OPENED);
  506. }
  507. if (p_port->line_status) {
  508. return (PORT_LINE_ERR);
  509. }
  510. RFCOMM_TRACE_API("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
  511. p_settings->fc_type);
  512. baud_rate = p_port->user_port_pars.baud_rate;
  513. p_port->user_port_pars = *p_settings;
  514. /* for now we've been asked to pass only baud rate */
  515. if (baud_rate != p_settings->baud_rate) {
  516. port_start_par_neg(p_port);
  517. }
  518. return (PORT_SUCCESS);
  519. }
  520. /*******************************************************************************
  521. *
  522. * Function PORT_GetRxQueueCnt
  523. *
  524. * Description This function return number of buffers on the rx queue.
  525. *
  526. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  527. * p_rx_queue_count - Pointer to return queue count in.
  528. *
  529. ******************************************************************************/
  530. int PORT_GetRxQueueCnt(uint16_t handle, uint16_t* p_rx_queue_count) {
  531. tPORT* p_port;
  532. RFCOMM_TRACE_API("PORT_GetRxQueueCnt() handle:%d", handle);
  533. /* Check if handle is valid to avoid crashing */
  534. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  535. return (PORT_BAD_HANDLE);
  536. }
  537. p_port = &rfc_cb.port.port[handle - 1];
  538. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  539. return (PORT_NOT_OPENED);
  540. }
  541. if (p_port->line_status) {
  542. return (PORT_LINE_ERR);
  543. }
  544. *p_rx_queue_count = p_port->rx.queue_size;
  545. RFCOMM_TRACE_API(
  546. "PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d",
  547. *p_rx_queue_count, p_port->rx.queue_size);
  548. return (PORT_SUCCESS);
  549. }
  550. /*******************************************************************************
  551. *
  552. * Function PORT_GetState
  553. *
  554. * Description This function is called to fill tPORT_STATE structure
  555. * with the curremt control settings for the port
  556. *
  557. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  558. * p_settings - Pointer to a tPORT_STATE structure in which
  559. * configuration information is returned.
  560. *
  561. ******************************************************************************/
  562. int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings) {
  563. tPORT* p_port;
  564. RFCOMM_TRACE_API("PORT_GetState() handle:%d", handle);
  565. /* Check if handle is valid to avoid crashing */
  566. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  567. return (PORT_BAD_HANDLE);
  568. }
  569. p_port = &rfc_cb.port.port[handle - 1];
  570. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  571. return (PORT_NOT_OPENED);
  572. }
  573. if (p_port->line_status) {
  574. return (PORT_LINE_ERR);
  575. }
  576. *p_settings = p_port->user_port_pars;
  577. return (PORT_SUCCESS);
  578. }
  579. /*******************************************************************************
  580. *
  581. * Function PORT_Control
  582. *
  583. * Description This function directs a specified connection to pass control
  584. * control information to the peer device.
  585. *
  586. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  587. * signal = specify the function to be passed
  588. *
  589. ******************************************************************************/
  590. int PORT_Control(uint16_t handle, uint8_t signal) {
  591. tPORT* p_port;
  592. uint8_t old_modem_signal;
  593. RFCOMM_TRACE_API("PORT_Control() handle:%d signal:0x%x", handle, signal);
  594. /* Check if handle is valid to avoid crashing */
  595. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  596. return (PORT_BAD_HANDLE);
  597. }
  598. p_port = &rfc_cb.port.port[handle - 1];
  599. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  600. return (PORT_NOT_OPENED);
  601. }
  602. old_modem_signal = p_port->local_ctrl.modem_signal;
  603. p_port->local_ctrl.break_signal = 0;
  604. switch (signal) {
  605. case PORT_SET_CTSRTS:
  606. p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON;
  607. break;
  608. case PORT_CLR_CTSRTS:
  609. p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON;
  610. break;
  611. case PORT_SET_DTRDSR:
  612. p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON;
  613. break;
  614. case PORT_CLR_DTRDSR:
  615. p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON;
  616. break;
  617. case PORT_SET_RI:
  618. p_port->local_ctrl.modem_signal |= PORT_RING_ON;
  619. break;
  620. case PORT_CLR_RI:
  621. p_port->local_ctrl.modem_signal &= ~PORT_RING_ON;
  622. break;
  623. case PORT_SET_DCD:
  624. p_port->local_ctrl.modem_signal |= PORT_DCD_ON;
  625. break;
  626. case PORT_CLR_DCD:
  627. p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON;
  628. break;
  629. }
  630. if (signal == PORT_BREAK)
  631. p_port->local_ctrl.break_signal = PORT_BREAK_DURATION;
  632. else if (p_port->local_ctrl.modem_signal == old_modem_signal)
  633. return (PORT_SUCCESS);
  634. port_start_control(p_port);
  635. RFCOMM_TRACE_EVENT(
  636. "PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
  637. ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
  638. ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
  639. ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
  640. ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
  641. return (PORT_SUCCESS);
  642. }
  643. /*******************************************************************************
  644. *
  645. * Function PORT_FlowControl
  646. *
  647. * Description This function directs a specified connection to pass
  648. * flow control message to the peer device. Enable flag passed
  649. * shows if port can accept more data.
  650. *
  651. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  652. * enable - enables data flow
  653. *
  654. ******************************************************************************/
  655. int PORT_FlowControl(uint16_t handle, bool enable) {
  656. tPORT* p_port;
  657. bool old_fc;
  658. uint32_t events;
  659. RFCOMM_TRACE_API("PORT_FlowControl() handle:%d enable: %d", handle, enable);
  660. /* Check if handle is valid to avoid crashing */
  661. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  662. return (PORT_BAD_HANDLE);
  663. }
  664. p_port = &rfc_cb.port.port[handle - 1];
  665. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  666. return (PORT_NOT_OPENED);
  667. }
  668. if (!p_port->rfc.p_mcb) {
  669. return (PORT_NOT_OPENED);
  670. }
  671. p_port->rx.user_fc = !enable;
  672. if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
  673. if (!p_port->rx.user_fc) {
  674. port_flow_control_peer(p_port, true, 0);
  675. }
  676. } else {
  677. old_fc = p_port->local_ctrl.fc;
  678. /* FC is set if user is set or peer is set */
  679. p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
  680. if (p_port->local_ctrl.fc != old_fc) port_start_control(p_port);
  681. }
  682. /* Need to take care of the case when we could not deliver events */
  683. /* to the application because we were flow controlled */
  684. if (enable && (p_port->rx.queue_size != 0)) {
  685. events = PORT_EV_RXCHAR;
  686. if (p_port->rx_flag_ev_pending) {
  687. p_port->rx_flag_ev_pending = false;
  688. events |= PORT_EV_RXFLAG;
  689. }
  690. events &= p_port->ev_mask;
  691. if (p_port->p_callback && events) {
  692. p_port->p_callback(events, p_port->handle);
  693. }
  694. }
  695. return (PORT_SUCCESS);
  696. }
  697. /*******************************************************************************
  698. *
  699. * Function PORT_FlowControl_MaxCredit
  700. *
  701. * Description This function directs a specified connection to pass
  702. * flow control message to the peer device. Enable flag passed
  703. * shows if port can accept more data. It also sends max credit
  704. * when data flow enabled
  705. *
  706. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  707. * enable - enables data flow
  708. *
  709. ******************************************************************************/
  710. int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable) {
  711. tPORT* p_port;
  712. bool old_fc;
  713. uint32_t events;
  714. RFCOMM_TRACE_API("PORT_FlowControl() handle:%d enable: %d", handle, enable);
  715. /* Check if handle is valid to avoid crashing */
  716. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  717. return (PORT_BAD_HANDLE);
  718. }
  719. p_port = &rfc_cb.port.port[handle - 1];
  720. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  721. return (PORT_NOT_OPENED);
  722. }
  723. if (!p_port->rfc.p_mcb) {
  724. return (PORT_NOT_OPENED);
  725. }
  726. p_port->rx.user_fc = !enable;
  727. if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
  728. if (!p_port->rx.user_fc) {
  729. port_flow_control_peer(p_port, true, p_port->credit_rx);
  730. }
  731. } else {
  732. old_fc = p_port->local_ctrl.fc;
  733. /* FC is set if user is set or peer is set */
  734. p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
  735. if (p_port->local_ctrl.fc != old_fc) port_start_control(p_port);
  736. }
  737. /* Need to take care of the case when we could not deliver events */
  738. /* to the application because we were flow controlled */
  739. if (enable && (p_port->rx.queue_size != 0)) {
  740. events = PORT_EV_RXCHAR;
  741. if (p_port->rx_flag_ev_pending) {
  742. p_port->rx_flag_ev_pending = false;
  743. events |= PORT_EV_RXFLAG;
  744. }
  745. events &= p_port->ev_mask;
  746. if (p_port->p_callback && events) {
  747. p_port->p_callback(events, p_port->handle);
  748. }
  749. }
  750. return (PORT_SUCCESS);
  751. }
  752. /*******************************************************************************
  753. *
  754. * Function PORT_GetModemStatus
  755. *
  756. * Description This function retrieves modem control signals. Normally
  757. * application will call this function after a callback
  758. * function is called with notification that one of signals
  759. * has been changed.
  760. *
  761. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  762. * p_signal - specify the pointer to control signals info
  763. *
  764. ******************************************************************************/
  765. int PORT_GetModemStatus(uint16_t handle, uint8_t* p_signal) {
  766. tPORT* p_port;
  767. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  768. return (PORT_BAD_HANDLE);
  769. }
  770. p_port = &rfc_cb.port.port[handle - 1];
  771. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  772. return (PORT_NOT_OPENED);
  773. }
  774. *p_signal = p_port->peer_ctrl.modem_signal;
  775. RFCOMM_TRACE_API("PORT_GetModemStatus() handle:%d signal:%x", handle,
  776. *p_signal);
  777. return (PORT_SUCCESS);
  778. }
  779. /*******************************************************************************
  780. *
  781. * Function PORT_ClearError
  782. *
  783. * Description This function retreives information about a communications
  784. * error and reports current status of a connection. The
  785. * function should be called when an error occures to clear
  786. * the connection error flag and to enable additional read
  787. * and write operations.
  788. *
  789. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  790. * p_errors - pointer of the variable to receive error codes
  791. * p_status - pointer to the tPORT_STATUS structur to receive
  792. * connection status
  793. *
  794. ******************************************************************************/
  795. int PORT_ClearError(uint16_t handle, uint16_t* p_errors,
  796. tPORT_STATUS* p_status) {
  797. tPORT* p_port;
  798. RFCOMM_TRACE_API("PORT_ClearError() handle:%d", handle);
  799. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  800. return (PORT_BAD_HANDLE);
  801. }
  802. p_port = &rfc_cb.port.port[handle - 1];
  803. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  804. return (PORT_NOT_OPENED);
  805. }
  806. *p_errors = p_port->line_status;
  807. /* This is the only call to clear error status. We can not clear */
  808. /* connection failed status. To clean it port should be closed and reopened
  809. */
  810. p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED);
  811. PORT_GetQueueStatus(handle, p_status);
  812. return (PORT_SUCCESS);
  813. }
  814. /*******************************************************************************
  815. *
  816. * Function PORT_SendError
  817. *
  818. * Description This function send a communications error to the peer device
  819. *
  820. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  821. * errors - receive error codes
  822. *
  823. ******************************************************************************/
  824. int PORT_SendError(uint16_t handle, uint8_t errors) {
  825. tPORT* p_port;
  826. RFCOMM_TRACE_API("PORT_SendError() handle:%d errors:0x%x", handle, errors);
  827. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  828. return (PORT_BAD_HANDLE);
  829. }
  830. p_port = &rfc_cb.port.port[handle - 1];
  831. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  832. return (PORT_NOT_OPENED);
  833. }
  834. if (!p_port->rfc.p_mcb) {
  835. return (PORT_NOT_OPENED);
  836. }
  837. RFCOMM_LineStatusReq(p_port->rfc.p_mcb, p_port->dlci, errors);
  838. return (PORT_SUCCESS);
  839. }
  840. /*******************************************************************************
  841. *
  842. * Function PORT_GetQueueStatus
  843. *
  844. * Description This function reports current status of a connection.
  845. *
  846. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  847. * p_status - pointer to the tPORT_STATUS structur to receive
  848. * connection status
  849. *
  850. ******************************************************************************/
  851. int PORT_GetQueueStatus(uint16_t handle, tPORT_STATUS* p_status) {
  852. tPORT* p_port;
  853. /* RFCOMM_TRACE_API ("PORT_GetQueueStatus() handle:%d", handle); */
  854. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  855. return (PORT_BAD_HANDLE);
  856. }
  857. p_port = &rfc_cb.port.port[handle - 1];
  858. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  859. return (PORT_NOT_OPENED);
  860. }
  861. p_status->in_queue_size = (uint16_t)p_port->rx.queue_size;
  862. p_status->out_queue_size = (uint16_t)p_port->tx.queue_size;
  863. p_status->mtu_size = (uint16_t)p_port->peer_mtu;
  864. p_status->flags = 0;
  865. if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON))
  866. p_status->flags |= PORT_FLAG_CTS_HOLD;
  867. if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON))
  868. p_status->flags |= PORT_FLAG_DSR_HOLD;
  869. if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON))
  870. p_status->flags |= PORT_FLAG_RLSD_HOLD;
  871. return (PORT_SUCCESS);
  872. }
  873. /*******************************************************************************
  874. *
  875. * Function PORT_Purge
  876. *
  877. * Description This function discards all the data from the output or
  878. * input queues of the specified connection.
  879. *
  880. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  881. * purge_flags - specify the action to take.
  882. *
  883. ******************************************************************************/
  884. int PORT_Purge(uint16_t handle, uint8_t purge_flags) {
  885. tPORT* p_port;
  886. BT_HDR* p_buf;
  887. uint16_t count;
  888. uint32_t events;
  889. RFCOMM_TRACE_API("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags);
  890. /* Check if handle is valid to avoid crashing */
  891. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  892. return (PORT_BAD_HANDLE);
  893. }
  894. p_port = &rfc_cb.port.port[handle - 1];
  895. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  896. return (PORT_NOT_OPENED);
  897. }
  898. if (purge_flags & PORT_PURGE_RXCLEAR) {
  899. mutex_global_lock(); /* to prevent missing credit */
  900. count = fixed_queue_length(p_port->rx.queue);
  901. while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->rx.queue)) != NULL)
  902. osi_free(p_buf);
  903. p_port->rx.queue_size = 0;
  904. mutex_global_unlock();
  905. /* If we flowed controlled peer based on rx_queue size enable data again */
  906. if (count) port_flow_control_peer(p_port, true, count);
  907. }
  908. if (purge_flags & PORT_PURGE_TXCLEAR) {
  909. mutex_global_lock(); /* to prevent tx.queue_size from being negative */
  910. while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue)) != NULL)
  911. osi_free(p_buf);
  912. p_port->tx.queue_size = 0;
  913. mutex_global_unlock();
  914. events = PORT_EV_TXEMPTY;
  915. events |= port_flow_control_user(p_port);
  916. events &= p_port->ev_mask;
  917. if ((p_port->p_callback != NULL) && events)
  918. (p_port->p_callback)(events, p_port->handle);
  919. }
  920. return (PORT_SUCCESS);
  921. }
  922. /*******************************************************************************
  923. *
  924. * Function PORT_ReadData
  925. *
  926. * Description Normally not GKI aware application will call this function
  927. * after receiving PORT_EV_RXCHAR event.
  928. *
  929. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  930. * p_data - Data area
  931. * max_len - Byte count requested
  932. * p_len - Byte count received
  933. *
  934. ******************************************************************************/
  935. int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  936. uint16_t* p_len) {
  937. tPORT* p_port;
  938. BT_HDR* p_buf;
  939. uint16_t count;
  940. RFCOMM_TRACE_API("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
  941. /* Initialize this in case of an error */
  942. *p_len = 0;
  943. /* Check if handle is valid to avoid crashing */
  944. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  945. return (PORT_BAD_HANDLE);
  946. }
  947. p_port = &rfc_cb.port.port[handle - 1];
  948. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  949. return (PORT_NOT_OPENED);
  950. }
  951. if (p_port->line_status) {
  952. return (PORT_LINE_ERR);
  953. }
  954. if (fixed_queue_is_empty(p_port->rx.queue)) return (PORT_SUCCESS);
  955. count = 0;
  956. while (max_len) {
  957. p_buf = (BT_HDR*)fixed_queue_try_peek_first(p_port->rx.queue);
  958. if (p_buf == NULL) break;
  959. if (p_buf->len > max_len) {
  960. memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, max_len);
  961. p_buf->offset += max_len;
  962. p_buf->len -= max_len;
  963. *p_len += max_len;
  964. mutex_global_lock();
  965. p_port->rx.queue_size -= max_len;
  966. mutex_global_unlock();
  967. break;
  968. } else {
  969. memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
  970. *p_len += p_buf->len;
  971. max_len -= p_buf->len;
  972. mutex_global_lock();
  973. p_port->rx.queue_size -= p_buf->len;
  974. if (max_len) {
  975. p_data += p_buf->len;
  976. }
  977. osi_free(fixed_queue_try_dequeue(p_port->rx.queue));
  978. mutex_global_unlock();
  979. count++;
  980. }
  981. }
  982. if (*p_len == 1) {
  983. RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d %x",
  984. p_port->rx.queue_size, *p_len, (p_data[0]));
  985. } else {
  986. RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d",
  987. p_port->rx.queue_size, *p_len);
  988. }
  989. /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
  990. /* check if it can be resumed now */
  991. port_flow_control_peer(p_port, true, count);
  992. return (PORT_SUCCESS);
  993. }
  994. /*******************************************************************************
  995. *
  996. * Function PORT_Read
  997. *
  998. * Description Normally application will call this function after receiving
  999. * PORT_EV_RXCHAR event.
  1000. *
  1001. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  1002. * pp_buf - pointer to address of buffer with data,
  1003. *
  1004. ******************************************************************************/
  1005. int PORT_Read(uint16_t handle, BT_HDR** pp_buf) {
  1006. tPORT* p_port;
  1007. BT_HDR* p_buf;
  1008. RFCOMM_TRACE_API("PORT_Read() handle:%d", handle);
  1009. /* Check if handle is valid to avoid crashing */
  1010. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  1011. return (PORT_BAD_HANDLE);
  1012. }
  1013. p_port = &rfc_cb.port.port[handle - 1];
  1014. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  1015. return (PORT_NOT_OPENED);
  1016. }
  1017. if (p_port->line_status) {
  1018. return (PORT_LINE_ERR);
  1019. }
  1020. mutex_global_lock();
  1021. p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->rx.queue);
  1022. if (p_buf) {
  1023. p_port->rx.queue_size -= p_buf->len;
  1024. mutex_global_unlock();
  1025. /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
  1026. /* check if it can be resumed now */
  1027. port_flow_control_peer(p_port, true, 1);
  1028. } else {
  1029. mutex_global_unlock();
  1030. }
  1031. *pp_buf = p_buf;
  1032. return (PORT_SUCCESS);
  1033. }
  1034. /*******************************************************************************
  1035. *
  1036. * Function port_write
  1037. *
  1038. * Description This function when a data packet is received from the apper
  1039. * layer task.
  1040. *
  1041. * Parameters: p_port - pointer to address of port control block
  1042. * p_buf - pointer to address of buffer with data,
  1043. *
  1044. ******************************************************************************/
  1045. static int port_write(tPORT* p_port, BT_HDR* p_buf) {
  1046. /* We should not allow to write data in to server port when connection is not
  1047. * opened */
  1048. if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED)) {
  1049. osi_free(p_buf);
  1050. return (PORT_CLOSED);
  1051. }
  1052. /* Keep the data in pending queue if peer does not allow data, or */
  1053. /* Peer is not ready or Port is not yet opened or initial port control */
  1054. /* command has not been sent */
  1055. if (p_port->tx.peer_fc || !p_port->rfc.p_mcb ||
  1056. !p_port->rfc.p_mcb->peer_ready ||
  1057. (p_port->rfc.state != RFC_STATE_OPENED) ||
  1058. ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) !=
  1059. (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) {
  1060. if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM) ||
  1061. (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM)) {
  1062. RFCOMM_TRACE_WARNING("PORT_Write: Queue size: %d", p_port->tx.queue_size);
  1063. osi_free(p_buf);
  1064. if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR))
  1065. p_port->p_callback(PORT_EV_ERR, p_port->handle);
  1066. return (PORT_TX_FULL);
  1067. }
  1068. RFCOMM_TRACE_EVENT(
  1069. "PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d "
  1070. "ctrl_state %x",
  1071. p_port->tx.peer_fc,
  1072. (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready), p_port->rfc.state,
  1073. p_port->port_ctrl);
  1074. fixed_queue_enqueue(p_port->tx.queue, p_buf);
  1075. p_port->tx.queue_size += p_buf->len;
  1076. return (PORT_CMD_PENDING);
  1077. } else {
  1078. RFCOMM_TRACE_EVENT("PORT_Write : Data is being sent");
  1079. RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
  1080. return (PORT_SUCCESS);
  1081. }
  1082. }
  1083. /*******************************************************************************
  1084. *
  1085. * Function PORT_Write
  1086. *
  1087. * Description This function when a data packet is received from the apper
  1088. * layer task.
  1089. *
  1090. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  1091. * pp_buf - pointer to address of buffer with data,
  1092. *
  1093. ******************************************************************************/
  1094. int PORT_Write(uint16_t handle, BT_HDR* p_buf) {
  1095. tPORT* p_port;
  1096. uint32_t event = 0;
  1097. int rc;
  1098. RFCOMM_TRACE_API("PORT_Write() handle:%d", handle);
  1099. /* Check if handle is valid to avoid crashing */
  1100. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  1101. osi_free(p_buf);
  1102. return (PORT_BAD_HANDLE);
  1103. }
  1104. p_port = &rfc_cb.port.port[handle - 1];
  1105. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  1106. osi_free(p_buf);
  1107. return (PORT_NOT_OPENED);
  1108. }
  1109. if (p_port->line_status) {
  1110. RFCOMM_TRACE_WARNING("PORT_Write: Data dropped line_status:0x%x",
  1111. p_port->line_status);
  1112. osi_free(p_buf);
  1113. return (PORT_LINE_ERR);
  1114. }
  1115. rc = port_write(p_port, p_buf);
  1116. event |= port_flow_control_user(p_port);
  1117. switch (rc) {
  1118. case PORT_TX_FULL:
  1119. event |= PORT_EV_ERR;
  1120. break;
  1121. case PORT_SUCCESS:
  1122. event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY);
  1123. break;
  1124. }
  1125. /* Mask out all events that are not of interest to user */
  1126. event &= p_port->ev_mask;
  1127. /* Send event to the application */
  1128. if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->handle);
  1129. return (PORT_SUCCESS);
  1130. }
  1131. /*******************************************************************************
  1132. *
  1133. * Function PORT_WriteDataCO
  1134. *
  1135. * Description Normally not GKI aware application will call this function
  1136. * to send data to the port by callout functions
  1137. *
  1138. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  1139. * fd - socket fd
  1140. * p_len - Byte count returned
  1141. *
  1142. ******************************************************************************/
  1143. int PORT_WriteDataCO(uint16_t handle, int* p_len) {
  1144. tPORT* p_port;
  1145. BT_HDR* p_buf;
  1146. uint32_t event = 0;
  1147. int rc = 0;
  1148. uint16_t length;
  1149. RFCOMM_TRACE_API("PORT_WriteDataCO() handle:%d", handle);
  1150. *p_len = 0;
  1151. /* Check if handle is valid to avoid crashing */
  1152. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  1153. return (PORT_BAD_HANDLE);
  1154. }
  1155. p_port = &rfc_cb.port.port[handle - 1];
  1156. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  1157. RFCOMM_TRACE_WARNING("PORT_WriteDataByFd() no port state:%d",
  1158. p_port->state);
  1159. return (PORT_NOT_OPENED);
  1160. }
  1161. if (!p_port->peer_mtu) {
  1162. RFCOMM_TRACE_ERROR("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
  1163. return (PORT_UNKNOWN_ERROR);
  1164. }
  1165. int available = 0;
  1166. // if(ioctl(fd, FIONREAD, &available) < 0)
  1167. if (!p_port->p_data_co_callback(handle, (uint8_t*)&available,
  1168. sizeof(available),
  1169. DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE)) {
  1170. RFCOMM_TRACE_ERROR(
  1171. "p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, "
  1172. "available:%d",
  1173. available);
  1174. return (PORT_UNKNOWN_ERROR);
  1175. }
  1176. if (available == 0) return PORT_SUCCESS;
  1177. /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len
  1178. */
  1179. length = RFCOMM_DATA_BUF_SIZE -
  1180. (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
  1181. /* If there are buffers scheduled for transmission check if requested */
  1182. /* data fits into the end of the queue */
  1183. mutex_global_lock();
  1184. p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue);
  1185. if ((p_buf != NULL) &&
  1186. (((int)p_buf->len + available) <= (int)p_port->peer_mtu) &&
  1187. (((int)p_buf->len + available) <= (int)length)) {
  1188. // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len,
  1189. // available, 0) != available)
  1190. if (!p_port->p_data_co_callback(
  1191. handle, (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len,
  1192. available, DATA_CO_CALLBACK_TYPE_OUTGOING))
  1193. {
  1194. error(
  1195. "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, "
  1196. "available:%d",
  1197. available);
  1198. mutex_global_unlock();
  1199. return (PORT_UNKNOWN_ERROR);
  1200. }
  1201. // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data,
  1202. // max_len);
  1203. p_port->tx.queue_size += (uint16_t)available;
  1204. *p_len = available;
  1205. p_buf->len += (uint16_t)available;
  1206. mutex_global_unlock();
  1207. return (PORT_SUCCESS);
  1208. }
  1209. mutex_global_unlock();
  1210. // int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu;
  1211. // max_read = available < max_read ? available : max_read;
  1212. while (available) {
  1213. /* if we're over buffer high water mark, we're done */
  1214. if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) ||
  1215. (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) {
  1216. port_flow_control_user(p_port);
  1217. event |= PORT_EV_FC;
  1218. RFCOMM_TRACE_EVENT(
  1219. "tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d",
  1220. p_port->tx.queue_size, fixed_queue_length(p_port->tx.queue),
  1221. available);
  1222. break;
  1223. }
  1224. /* continue with rfcomm data write */
  1225. p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE);
  1226. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
  1227. p_buf->layer_specific = handle;
  1228. if (p_port->peer_mtu < length) length = p_port->peer_mtu;
  1229. if (available < (int)length) length = (uint16_t)available;
  1230. p_buf->len = length;
  1231. p_buf->event = BT_EVT_TO_BTU_SP_DATA;
  1232. // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset, p_data, length);
  1233. // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset, (int)length, 0) !=
  1234. // (int)length)
  1235. if (!p_port->p_data_co_callback(handle,
  1236. (uint8_t*)(p_buf + 1) + p_buf->offset,
  1237. length, DATA_CO_CALLBACK_TYPE_OUTGOING)) {
  1238. error(
  1239. "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d",
  1240. length);
  1241. return (PORT_UNKNOWN_ERROR);
  1242. }
  1243. RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length);
  1244. rc = port_write(p_port, p_buf);
  1245. /* If queue went below the threashold need to send flow control */
  1246. event |= port_flow_control_user(p_port);
  1247. if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR;
  1248. if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break;
  1249. *p_len += length;
  1250. available -= (int)length;
  1251. }
  1252. if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
  1253. event |= PORT_EV_TXEMPTY;
  1254. /* Mask out all events that are not of interest to user */
  1255. event &= p_port->ev_mask;
  1256. /* Send event to the application */
  1257. if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->handle);
  1258. return (PORT_SUCCESS);
  1259. }
  1260. /*******************************************************************************
  1261. *
  1262. * Function PORT_WriteData
  1263. *
  1264. * Description Normally not GKI aware application will call this function
  1265. * to send data to the port.
  1266. *
  1267. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  1268. * p_data - Data area
  1269. * max_len - Byte count requested
  1270. * p_len - Byte count received
  1271. *
  1272. ******************************************************************************/
  1273. int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
  1274. uint16_t* p_len) {
  1275. tPORT* p_port;
  1276. BT_HDR* p_buf;
  1277. uint32_t event = 0;
  1278. int rc = 0;
  1279. uint16_t length;
  1280. RFCOMM_TRACE_API("PORT_WriteData() max_len:%d", max_len);
  1281. *p_len = 0;
  1282. /* Check if handle is valid to avoid crashing */
  1283. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  1284. return (PORT_BAD_HANDLE);
  1285. }
  1286. p_port = &rfc_cb.port.port[handle - 1];
  1287. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  1288. RFCOMM_TRACE_WARNING("PORT_WriteData() no port state:%d", p_port->state);
  1289. return (PORT_NOT_OPENED);
  1290. }
  1291. if (!max_len || !p_port->peer_mtu) {
  1292. RFCOMM_TRACE_ERROR("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
  1293. return (PORT_UNKNOWN_ERROR);
  1294. }
  1295. /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len
  1296. */
  1297. length = RFCOMM_DATA_BUF_SIZE -
  1298. (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
  1299. /* If there are buffers scheduled for transmission check if requested */
  1300. /* data fits into the end of the queue */
  1301. mutex_global_lock();
  1302. p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue);
  1303. if ((p_buf != NULL) && ((p_buf->len + max_len) <= p_port->peer_mtu) &&
  1304. ((p_buf->len + max_len) <= length)) {
  1305. memcpy((uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
  1306. p_port->tx.queue_size += max_len;
  1307. *p_len = max_len;
  1308. p_buf->len += max_len;
  1309. mutex_global_unlock();
  1310. return (PORT_SUCCESS);
  1311. }
  1312. mutex_global_unlock();
  1313. while (max_len) {
  1314. /* if we're over buffer high water mark, we're done */
  1315. if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) ||
  1316. (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM))
  1317. break;
  1318. /* continue with rfcomm data write */
  1319. p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE);
  1320. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
  1321. p_buf->layer_specific = handle;
  1322. if (p_port->peer_mtu < length) length = p_port->peer_mtu;
  1323. if (max_len < length) length = max_len;
  1324. p_buf->len = length;
  1325. p_buf->event = BT_EVT_TO_BTU_SP_DATA;
  1326. memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, length);
  1327. RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length);
  1328. rc = port_write(p_port, p_buf);
  1329. /* If queue went below the threashold need to send flow control */
  1330. event |= port_flow_control_user(p_port);
  1331. if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR;
  1332. if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break;
  1333. *p_len += length;
  1334. max_len -= length;
  1335. p_data += length;
  1336. }
  1337. if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
  1338. event |= PORT_EV_TXEMPTY;
  1339. /* Mask out all events that are not of interest to user */
  1340. event &= p_port->ev_mask;
  1341. /* Send event to the application */
  1342. if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->handle);
  1343. return (PORT_SUCCESS);
  1344. }
  1345. /*******************************************************************************
  1346. *
  1347. * Function PORT_Test
  1348. *
  1349. * Description Application can call this function to send RFCOMM Test frame
  1350. *
  1351. * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
  1352. * p_data - Data area
  1353. * max_len - Byte count requested
  1354. *
  1355. ******************************************************************************/
  1356. int PORT_Test(uint16_t handle, uint8_t* p_data, uint16_t len) {
  1357. tPORT* p_port;
  1358. RFCOMM_TRACE_API("PORT_Test() len:%d", len);
  1359. if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  1360. return (PORT_BAD_HANDLE);
  1361. }
  1362. p_port = &rfc_cb.port.port[handle - 1];
  1363. if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
  1364. return (PORT_NOT_OPENED);
  1365. }
  1366. if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu)) {
  1367. return (PORT_UNKNOWN_ERROR);
  1368. }
  1369. BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
  1370. p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
  1371. p_buf->len = len;
  1372. memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, p_buf->len);
  1373. rfc_send_test(p_port->rfc.p_mcb, true, p_buf);
  1374. return (PORT_SUCCESS);
  1375. }
  1376. /*******************************************************************************
  1377. *
  1378. * Function RFCOMM_Init
  1379. *
  1380. * Description This function is called to initialize RFCOMM layer
  1381. *
  1382. ******************************************************************************/
  1383. void RFCOMM_Init(void) {
  1384. memset(&rfc_cb, 0, sizeof(tRFC_CB)); /* Init RFCOMM control block */
  1385. rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
  1386. #if defined(RFCOMM_INITIAL_TRACE_LEVEL)
  1387. rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL;
  1388. #else
  1389. rfc_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
  1390. #endif
  1391. rfcomm_l2cap_if_init();
  1392. }
  1393. /*******************************************************************************
  1394. *
  1395. * Function PORT_SetTraceLevel
  1396. *
  1397. * Description Set the trace level for RFCOMM. If called with 0xFF, it
  1398. * simply reads the current trace level.
  1399. *
  1400. * Returns the new (current) trace level
  1401. *
  1402. ******************************************************************************/
  1403. uint8_t PORT_SetTraceLevel(uint8_t new_level) {
  1404. if (new_level != 0xFF) rfc_cb.trace_level = new_level;
  1405. return (rfc_cb.trace_level);
  1406. }
  1407. /*******************************************************************************
  1408. *
  1409. * Function PORT_GetResultString
  1410. *
  1411. * Description This function returns the human-readable string for a given
  1412. * result code.
  1413. *
  1414. * Returns a pointer to the human-readable string for the given result.
  1415. *
  1416. ******************************************************************************/
  1417. const char* PORT_GetResultString(const uint8_t result_code) {
  1418. if (result_code > PORT_ERR_MAX) {
  1419. return result_code_strings[PORT_ERR_MAX];
  1420. }
  1421. return result_code_strings[result_code];
  1422. }