port_api.c 57 KB

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