port_api.c 57 KB

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