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