1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * this file contains the Serial Port API code
23 ******************************************************************************/
26 #include "bt_target.h"
38 /* duration of break in 200ms units */
39 #define PORT_BREAK_DURATION 1
41 #include <cutils/log.h>
42 #define info(fmt, ...) ALOGI ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
43 #define debug(fmt, ...) ALOGD ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
44 #define error(fmt, ...) ALOGE ("## ERROR : %s: " fmt "##",__FUNCTION__, ## __VA_ARGS__)
45 #define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
47 /* Mapping from PORT_* result codes to human readable strings. */
48 static const char *result_code_strings[] = {
62 "Parameter negotiation failed",
63 "Port negotiation failed",
65 "Peer connection failed",
78 /*******************************************************************************
80 ** Function RFCOMM_CreateConnection
82 ** Description RFCOMM_CreateConnection function is used from the application
83 ** to establish serial port connection to the peer device,
84 ** or allow RFCOMM to accept a connection from the peer
87 ** Parameters: scn - Service Channel Number as registered with
88 ** the SDP (server) or obtained using SDP from
89 ** the peer device (client).
90 ** is_server - TRUE if requesting application is a server
91 ** mtu - Maximum frame size the application can accept
92 ** bd_addr - BD_ADDR of the peer (client)
93 ** mask - specifies events to be enabled. A value
94 ** of zero disables all events.
95 ** p_handle - OUT pointer to the handle.
96 ** p_mgmt_cb - pointer to callback function to receive
97 ** connection up/down events.
100 ** Server can call this function with the same scn parameter multiple times if
101 ** it is ready to accept multiple simulteneous connections.
103 ** DLCI for the connection is (scn * 2 + 1) if client originates connection on
104 ** existing none initiator multiplexer channel. Otherwise it is (scn * 2).
105 ** For the server DLCI can be changed later if client will be calling it using
106 ** (scn * 2 + 1) dlci.
108 *******************************************************************************/
109 int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
110 UINT16 mtu, BD_ADDR bd_addr, UINT16 *p_handle,
111 tPORT_CALLBACK *p_mgmt_cb)
116 tRFC_MCB *p_mcb = port_find_mcb (bd_addr);
119 counter_add("rfcomm.conn.created", 1);
121 RFCOMM_TRACE_API ("RFCOMM_CreateConnection() BDA: %02x-%02x-%02x-%02x-%02x-%02x",
122 bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
126 if (( scn == 0 )||(scn >= PORT_MAX_RFC_PORTS ))
128 /* Server Channel Number(SCN) should be in range 1...30 */
129 RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - invalid SCN");
130 return (PORT_INVALID_SCN);
133 /* For client that originate connection on the existing none initiator */
134 /* multiplexer channel DLCI should be odd */
135 if (p_mcb && !p_mcb->is_initiator && !is_server)
136 dlci = (scn << 1) + 1;
139 RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p",
140 scn, dlci, is_server, mtu, p_mcb);
142 /* For the server side always allocate a new port. On the client side */
143 /* do not allow the same (dlci, bd_addr) to be opened twice by application */
144 if (!is_server && ((p_port = port_find_port (dlci, bd_addr)) != NULL))
146 /* if existing port is also a client port */
147 if (p_port->is_server == FALSE)
149 RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, MCB state:%d",
150 p_port->state, p_port->rfc.state, p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0);
151 return (PORT_ALREADY_OPENED);
155 if ((p_port = port_allocate_port (dlci, bd_addr)) == NULL)
157 RFCOMM_TRACE_WARNING ("RFCOMM_CreateConnection - no resources");
158 return (PORT_NO_RESOURCES);
160 RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p, p_port:%p",
161 scn, dlci, is_server, mtu, p_mcb, p_port);
163 p_port->default_signal_state = (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
167 case UUID_PROTOCOL_OBEX:
168 p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE;
170 case UUID_SERVCLASS_SERIAL_PORT:
171 p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE;
173 case UUID_SERVCLASS_LAN_ACCESS_USING_PPP:
174 p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE;
176 case UUID_SERVCLASS_DIALUP_NETWORKING:
177 case UUID_SERVCLASS_FAX:
178 p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE;
182 RFCOMM_TRACE_EVENT ("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, p_port->default_signal_state);
184 *p_handle = p_port->inx;
186 p_port->state = PORT_STATE_OPENING;
188 p_port->is_server = is_server;
192 /* If the MTU is not specified (0), keep MTU decision until the
193 * PN frame has to be send
194 * at that time connection should be established and we
195 * will know for sure our prefered MTU
198 rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
201 p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu;
203 p_port->mtu = rfcomm_mtu;
205 /* server doesn't need to release port when closing */
208 p_port->keep_port_handle = TRUE;
210 /* keep mtu that user asked, p_port->mtu could be updated during param negotiation */
211 p_port->keep_mtu = p_port->mtu;
214 p_port->local_ctrl.modem_signal = p_port->default_signal_state;
215 p_port->local_ctrl.fc = FALSE;
217 p_port->p_mgmt_callback = p_mgmt_cb;
219 for (i = 0; i < BD_ADDR_LEN; i++)
220 p_port->bd_addr[i] = bd_addr[i];
222 /* If this is not initiator of the connection need to just wait */
223 if (p_port->is_server)
225 return (PORT_SUCCESS);
228 /* Open will be continued after security checks are passed */
229 return port_open_continue (p_port);
233 /*******************************************************************************
235 ** Function RFCOMM_RemoveConnection
237 ** Description This function is called to close the specified connection.
239 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
241 *******************************************************************************/
242 int RFCOMM_RemoveConnection (UINT16 handle)
246 counter_add("rfcomm.conn.destroyed", 1);
248 RFCOMM_TRACE_API ("RFCOMM_RemoveConnection() handle:%d", handle);
250 /* Check if handle is valid to avoid crashing */
251 if ((handle == 0) || (handle > MAX_RFC_PORTS))
253 RFCOMM_TRACE_ERROR ("RFCOMM_RemoveConnection() BAD handle:%d", handle);
254 return (PORT_BAD_HANDLE);
256 p_port = &rfc_cb.port.port[handle - 1];
258 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
260 RFCOMM_TRACE_EVENT ("RFCOMM_RemoveConnection() Not opened:%d", handle);
261 return (PORT_SUCCESS);
264 p_port->state = PORT_STATE_CLOSING;
266 port_start_close (p_port);
268 return (PORT_SUCCESS);
271 /*******************************************************************************
273 ** Function RFCOMM_RemoveServer
275 ** Description This function is called to close the server port.
277 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
279 *******************************************************************************/
280 int RFCOMM_RemoveServer (UINT16 handle)
284 RFCOMM_TRACE_API ("RFCOMM_RemoveServer() handle:%d", handle);
286 /* Check if handle is valid to avoid crashing */
287 if ((handle == 0) || (handle > MAX_RFC_PORTS))
289 RFCOMM_TRACE_ERROR ("RFCOMM_RemoveServer() BAD handle:%d", handle);
290 return (PORT_BAD_HANDLE);
292 p_port = &rfc_cb.port.port[handle - 1];
294 /* Do not report any events to the client any more. */
295 p_port->p_mgmt_callback = NULL;
297 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
299 RFCOMM_TRACE_EVENT ("RFCOMM_RemoveServer() Not opened:%d", handle);
300 return (PORT_SUCCESS);
303 /* this port will be deallocated after closing */
304 p_port->keep_port_handle = FALSE;
305 p_port->state = PORT_STATE_CLOSING;
307 port_start_close (p_port);
309 return (PORT_SUCCESS);
312 /*******************************************************************************
314 ** Function PORT_SetEventCallback
316 ** Description This function is called to provide an address of the
317 ** function which will be called when one of the events
318 ** specified in the mask occures.
320 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
321 ** p_callback - address of the callback function which should
322 ** be called from the RFCOMM when an event
323 ** specified in the mask occures.
326 *******************************************************************************/
327 int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb)
331 /* Check if handle is valid to avoid crashing */
332 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
334 return (PORT_BAD_HANDLE);
337 p_port = &rfc_cb.port.port[port_handle - 1];
339 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
341 return (PORT_NOT_OPENED);
344 RFCOMM_TRACE_API ("PORT_SetEventCallback() handle:%d", port_handle);
346 p_port->p_callback = p_port_cb;
348 return (PORT_SUCCESS);
350 /*******************************************************************************
352 ** Function PORT_ClearKeepHandleFlag
354 ** Description This function is called to clear the keep handle flag
355 ** which will cause not to keep the port handle open when closed
356 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
358 *******************************************************************************/
360 int PORT_ClearKeepHandleFlag (UINT16 port_handle)
364 /* Check if handle is valid to avoid crashing */
365 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
367 return (PORT_BAD_HANDLE);
370 p_port = &rfc_cb.port.port[port_handle - 1];
371 p_port->keep_port_handle = 0;
372 return (PORT_SUCCESS);
375 /*******************************************************************************
377 ** Function PORT_SetDataCallback
379 ** Description This function is when a data packet is received
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
387 *******************************************************************************/
388 int PORT_SetDataCallback (UINT16 port_handle, tPORT_DATA_CALLBACK *p_port_cb)
392 RFCOMM_TRACE_API ("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle, p_port_cb);
394 /* Check if handle is valid to avoid crashing */
395 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
397 return (PORT_BAD_HANDLE);
400 p_port = &rfc_cb.port.port[port_handle - 1];
402 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
404 return (PORT_NOT_OPENED);
407 p_port->p_data_callback = p_port_cb;
409 return (PORT_SUCCESS);
411 /*******************************************************************************
413 ** Function PORT_SetCODataCallback
415 ** Description This function is when a data packet is received
417 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
418 ** p_callback - address of the callback function which should
419 ** be called from the RFCOMM when data packet
423 *******************************************************************************/
424 int PORT_SetDataCOCallback (UINT16 port_handle, tPORT_DATA_CO_CALLBACK *p_port_cb)
428 RFCOMM_TRACE_API ("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle, p_port_cb);
430 /* Check if handle is valid to avoid crashing */
431 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
433 return (PORT_BAD_HANDLE);
436 p_port = &rfc_cb.port.port[port_handle - 1];
438 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
440 return (PORT_NOT_OPENED);
443 p_port->p_data_co_callback = p_port_cb;
445 return (PORT_SUCCESS);
450 /*******************************************************************************
452 ** Function PORT_SetEventMask
454 ** Description This function is called to close the specified connection.
456 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
457 ** mask - Bitmask of the events the host is interested in
459 *******************************************************************************/
460 int PORT_SetEventMask (UINT16 port_handle, UINT32 mask)
464 RFCOMM_TRACE_API ("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask);
466 /* Check if handle is valid to avoid crashing */
467 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
469 return (PORT_BAD_HANDLE);
472 p_port = &rfc_cb.port.port[port_handle - 1];
474 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
476 return (PORT_NOT_OPENED);
479 p_port->ev_mask = mask;
481 return (PORT_SUCCESS);
485 /*******************************************************************************
487 ** Function PORT_CheckConnection
489 ** Description This function returns PORT_SUCCESS if connection referenced
490 ** by handle is up and running
492 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
493 ** bd_addr - OUT bd_addr of the peer
494 ** p_lcid - OUT L2CAP's LCID
496 *******************************************************************************/
497 int PORT_CheckConnection (UINT16 handle, BD_ADDR bd_addr, UINT16 *p_lcid)
501 RFCOMM_TRACE_API ("PORT_CheckConnection() handle:%d", handle);
503 /* Check if handle is valid to avoid crashing */
504 if ((handle == 0) || (handle > MAX_RFC_PORTS))
506 return (PORT_BAD_HANDLE);
509 p_port = &rfc_cb.port.port[handle - 1];
511 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
513 return (PORT_NOT_OPENED);
516 if (!p_port->rfc.p_mcb
517 || !p_port->rfc.p_mcb->peer_ready
518 || (p_port->rfc.state != RFC_STATE_OPENED))
520 return (PORT_LINE_ERR);
523 memcpy (bd_addr, p_port->rfc.p_mcb->bd_addr, BD_ADDR_LEN);
525 *p_lcid = p_port->rfc.p_mcb->lcid;
527 return (PORT_SUCCESS);
530 /*******************************************************************************
532 ** Function PORT_IsOpening
534 ** Description This function returns TRUE if there is any RFCOMM connection
535 ** opening in process.
537 ** Parameters: TRUE if any connection opening is found
538 ** bd_addr - bd_addr of the peer
540 *******************************************************************************/
541 BOOLEAN PORT_IsOpening (BD_ADDR bd_addr)
544 tRFC_MCB *p_mcb = NULL;
548 /* Check for any rfc_mcb which is in the middle of opening. */
549 for (xx = 0; xx < MAX_BD_CONNECTIONS; xx++)
551 if ((rfc_cb.port.rfc_mcb[xx].state > RFC_MX_STATE_IDLE) &&
552 (rfc_cb.port.rfc_mcb[xx].state < RFC_MX_STATE_CONNECTED))
554 memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
558 if (rfc_cb.port.rfc_mcb[xx].state == RFC_MX_STATE_CONNECTED)
561 p_mcb = &rfc_cb.port.rfc_mcb[xx];
562 p_port = &rfc_cb.port.port[0];
564 for (yy = 0; yy < MAX_RFC_PORTS; yy++, p_port++)
566 if (p_port->rfc.p_mcb == p_mcb)
574 (found_port && (p_port->rfc.state < RFC_STATE_OPENED)))
576 /* Port is not established yet. */
577 memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
586 /*******************************************************************************
588 ** Function PORT_SetState
590 ** Description This function configures connection according to the
591 ** specifications in the tPORT_STATE structure.
593 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
594 ** p_settings - Pointer to a tPORT_STATE structure containing
595 ** configuration information for the connection.
598 *******************************************************************************/
599 int PORT_SetState (UINT16 handle, tPORT_STATE *p_settings)
604 RFCOMM_TRACE_API ("PORT_SetState() handle:%d", handle);
606 /* Check if handle is valid to avoid crashing */
607 if ((handle == 0) || (handle > MAX_RFC_PORTS))
609 return (PORT_BAD_HANDLE);
612 p_port = &rfc_cb.port.port[handle - 1];
614 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
616 return (PORT_NOT_OPENED);
619 if (p_port->line_status)
621 return (PORT_LINE_ERR);
624 RFCOMM_TRACE_API ("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
625 p_settings->fc_type);
627 baud_rate = p_port->user_port_pars.baud_rate;
628 p_port->user_port_pars = *p_settings;
630 /* for now we've been asked to pass only baud rate */
631 if (baud_rate != p_settings->baud_rate)
633 port_start_par_neg (p_port);
635 return (PORT_SUCCESS);
638 /*******************************************************************************
640 ** Function PORT_GetRxQueueCnt
642 ** Description This function return number of buffers on the rx queue.
644 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
645 ** p_rx_queue_count - Pointer to return queue count in.
647 *******************************************************************************/
648 int PORT_GetRxQueueCnt (UINT16 handle, UINT16 *p_rx_queue_count)
652 RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() handle:%d", handle);
654 /* Check if handle is valid to avoid crashing */
655 if ((handle == 0) || (handle > MAX_RFC_PORTS))
657 return (PORT_BAD_HANDLE);
660 p_port = &rfc_cb.port.port[handle - 1];
662 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
664 return (PORT_NOT_OPENED);
667 if (p_port->line_status)
669 return (PORT_LINE_ERR);
672 *p_rx_queue_count = p_port->rx.queue_size;
674 RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d",
675 *p_rx_queue_count, p_port->rx.queue_size);
677 return (PORT_SUCCESS);
680 /*******************************************************************************
682 ** Function PORT_GetState
684 ** Description This function is called to fill tPORT_STATE structure
685 ** with the curremt control settings for the port
687 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
688 ** p_settings - Pointer to a tPORT_STATE structure in which
689 ** configuration information is returned.
691 *******************************************************************************/
692 int PORT_GetState (UINT16 handle, tPORT_STATE *p_settings)
696 RFCOMM_TRACE_API ("PORT_GetState() handle:%d", handle);
698 /* Check if handle is valid to avoid crashing */
699 if ((handle == 0) || (handle > MAX_RFC_PORTS))
701 return (PORT_BAD_HANDLE);
704 p_port = &rfc_cb.port.port[handle - 1];
706 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
708 return (PORT_NOT_OPENED);
711 if (p_port->line_status)
713 return (PORT_LINE_ERR);
716 *p_settings = p_port->user_port_pars;
717 return (PORT_SUCCESS);
721 /*******************************************************************************
723 ** Function PORT_Control
725 ** Description This function directs a specified connection to pass control
726 ** control information to the peer device.
728 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
729 ** signal = specify the function to be passed
731 *******************************************************************************/
732 int PORT_Control (UINT16 handle, UINT8 signal)
735 UINT8 old_modem_signal;
737 RFCOMM_TRACE_API ("PORT_Control() handle:%d signal:0x%x", handle, signal);
739 /* Check if handle is valid to avoid crashing */
740 if ((handle == 0) || (handle > MAX_RFC_PORTS))
742 return (PORT_BAD_HANDLE);
745 p_port = &rfc_cb.port.port[handle - 1];
747 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
749 return (PORT_NOT_OPENED);
752 old_modem_signal = p_port->local_ctrl.modem_signal;
753 p_port->local_ctrl.break_signal = 0;
757 case PORT_SET_CTSRTS:
758 p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON;
761 case PORT_CLR_CTSRTS:
762 p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON;
765 case PORT_SET_DTRDSR:
766 p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON;
769 case PORT_CLR_DTRDSR:
770 p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON;
774 p_port->local_ctrl.modem_signal |= PORT_RING_ON;
778 p_port->local_ctrl.modem_signal &= ~PORT_RING_ON;
782 p_port->local_ctrl.modem_signal |= PORT_DCD_ON;
786 p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON;
790 if (signal == PORT_BREAK)
791 p_port->local_ctrl.break_signal = PORT_BREAK_DURATION;
792 else if (p_port->local_ctrl.modem_signal == old_modem_signal)
793 return (PORT_SUCCESS);
795 port_start_control (p_port);
797 RFCOMM_TRACE_EVENT ("PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
798 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
799 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
800 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
801 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
803 return (PORT_SUCCESS);
807 /*******************************************************************************
809 ** Function PORT_FlowControl
811 ** Description This function directs a specified connection to pass
812 ** flow control message to the peer device. Enable flag passed
813 ** shows if port can accept more data.
815 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
816 ** enable - enables data flow
818 *******************************************************************************/
819 int PORT_FlowControl (UINT16 handle, BOOLEAN enable)
825 RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
827 /* Check if handle is valid to avoid crashing */
828 if ((handle == 0) || (handle > MAX_RFC_PORTS))
830 return (PORT_BAD_HANDLE);
833 p_port = &rfc_cb.port.port[handle - 1];
835 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
837 return (PORT_NOT_OPENED);
840 if (!p_port->rfc.p_mcb)
842 return (PORT_NOT_OPENED);
845 p_port->rx.user_fc = !enable;
847 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
849 if (!p_port->rx.user_fc)
851 port_flow_control_peer(p_port, TRUE, 0);
856 old_fc = p_port->local_ctrl.fc;
858 /* FC is set if user is set or peer is set */
859 p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
861 if (p_port->local_ctrl.fc != old_fc)
862 port_start_control (p_port);
865 /* Need to take care of the case when we could not deliver events */
866 /* to the application because we were flow controlled */
867 if (enable && (p_port->rx.queue_size != 0))
869 events = PORT_EV_RXCHAR;
870 if (p_port->rx_flag_ev_pending)
872 p_port->rx_flag_ev_pending = FALSE;
873 events |= PORT_EV_RXFLAG;
876 events &= p_port->ev_mask;
877 if (p_port->p_callback && events)
879 p_port->p_callback (events, p_port->inx);
882 return (PORT_SUCCESS);
884 /*******************************************************************************
886 ** Function PORT_FlowControl_MaxCredit
888 ** Description This function directs a specified connection to pass
889 ** flow control message to the peer device. Enable flag passed
890 ** shows if port can accept more data. It also sends max credit
891 ** when data flow enabled
893 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
894 ** enable - enables data flow
896 *******************************************************************************/
898 int PORT_FlowControl_MaxCredit (UINT16 handle, BOOLEAN enable)
904 RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
906 /* Check if handle is valid to avoid crashing */
907 if ((handle == 0) || (handle > MAX_RFC_PORTS))
909 return (PORT_BAD_HANDLE);
912 p_port = &rfc_cb.port.port[handle - 1];
914 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
916 return (PORT_NOT_OPENED);
919 if (!p_port->rfc.p_mcb)
921 return (PORT_NOT_OPENED);
924 p_port->rx.user_fc = !enable;
926 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
928 if (!p_port->rx.user_fc)
930 port_flow_control_peer(p_port, TRUE, p_port->credit_rx);
935 old_fc = p_port->local_ctrl.fc;
937 /* FC is set if user is set or peer is set */
938 p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
940 if (p_port->local_ctrl.fc != old_fc)
941 port_start_control (p_port);
944 /* Need to take care of the case when we could not deliver events */
945 /* to the application because we were flow controlled */
946 if (enable && (p_port->rx.queue_size != 0))
948 events = PORT_EV_RXCHAR;
949 if (p_port->rx_flag_ev_pending)
951 p_port->rx_flag_ev_pending = FALSE;
952 events |= PORT_EV_RXFLAG;
955 events &= p_port->ev_mask;
956 if (p_port->p_callback && events)
958 p_port->p_callback (events, p_port->inx);
961 return (PORT_SUCCESS);
965 /*******************************************************************************
967 ** Function PORT_GetModemStatus
969 ** Description This function retrieves modem control signals. Normally
970 ** application will call this function after a callback
971 ** function is called with notification that one of signals
974 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
975 ** p_signal - specify the pointer to control signals info
977 *******************************************************************************/
978 int PORT_GetModemStatus (UINT16 handle, UINT8 *p_signal)
982 if ((handle == 0) || (handle > MAX_RFC_PORTS))
984 return (PORT_BAD_HANDLE);
987 p_port = &rfc_cb.port.port[handle - 1];
989 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
991 return (PORT_NOT_OPENED);
994 *p_signal = p_port->peer_ctrl.modem_signal;
996 RFCOMM_TRACE_API ("PORT_GetModemStatus() handle:%d signal:%x", handle, *p_signal);
998 return (PORT_SUCCESS);
1002 /*******************************************************************************
1004 ** Function PORT_ClearError
1006 ** Description This function retreives information about a communications
1007 ** error and reports current status of a connection. The
1008 ** function should be called when an error occures to clear
1009 ** the connection error flag and to enable additional read
1010 ** and write operations.
1012 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1013 ** p_errors - pointer of the variable to receive error codes
1014 ** p_status - pointer to the tPORT_STATUS structur to receive
1015 ** connection status
1017 *******************************************************************************/
1018 int PORT_ClearError (UINT16 handle, UINT16 *p_errors, tPORT_STATUS *p_status)
1022 RFCOMM_TRACE_API ("PORT_ClearError() handle:%d", handle);
1024 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1026 return (PORT_BAD_HANDLE);
1029 p_port = &rfc_cb.port.port[handle - 1];
1031 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1033 return (PORT_NOT_OPENED);
1036 *p_errors = p_port->line_status;
1038 /* This is the only call to clear error status. We can not clear */
1039 /* connection failed status. To clean it port should be closed and reopened */
1040 p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED);
1042 PORT_GetQueueStatus (handle, p_status);
1043 return (PORT_SUCCESS);
1047 /*******************************************************************************
1049 ** Function PORT_SendError
1051 ** Description This function send a communications error to the peer device
1053 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1054 ** errors - receive error codes
1056 *******************************************************************************/
1057 int PORT_SendError (UINT16 handle, UINT8 errors)
1061 RFCOMM_TRACE_API ("PORT_SendError() handle:%d errors:0x%x", handle, errors);
1063 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1065 return (PORT_BAD_HANDLE);
1068 p_port = &rfc_cb.port.port[handle - 1];
1070 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1072 return (PORT_NOT_OPENED);
1075 if (!p_port->rfc.p_mcb)
1077 return (PORT_NOT_OPENED);
1080 RFCOMM_LineStatusReq (p_port->rfc.p_mcb, p_port->dlci, errors);
1081 return (PORT_SUCCESS);
1085 /*******************************************************************************
1087 ** Function PORT_GetQueueStatus
1089 ** Description This function reports current status of a connection.
1091 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1092 ** p_status - pointer to the tPORT_STATUS structur to receive
1093 ** connection status
1095 *******************************************************************************/
1096 int PORT_GetQueueStatus (UINT16 handle, tPORT_STATUS *p_status)
1100 /* RFCOMM_TRACE_API ("PORT_GetQueueStatus() handle:%d", handle); */
1102 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1104 return (PORT_BAD_HANDLE);
1107 p_port = &rfc_cb.port.port[handle - 1];
1109 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1111 return (PORT_NOT_OPENED);
1114 p_status->in_queue_size = (UINT16) p_port->rx.queue_size;
1115 p_status->out_queue_size = (UINT16) p_port->tx.queue_size;
1117 p_status->mtu_size = (UINT16) p_port->peer_mtu;
1119 p_status->flags = 0;
1121 if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON))
1122 p_status->flags |= PORT_FLAG_CTS_HOLD;
1124 if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON))
1125 p_status->flags |= PORT_FLAG_DSR_HOLD;
1127 if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON))
1128 p_status->flags |= PORT_FLAG_RLSD_HOLD;
1130 return (PORT_SUCCESS);
1134 /*******************************************************************************
1136 ** Function PORT_Purge
1138 ** Description This function discards all the data from the output or
1139 ** input queues of the specified connection.
1141 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1142 ** purge_flags - specify the action to take.
1144 *******************************************************************************/
1145 int PORT_Purge (UINT16 handle, UINT8 purge_flags)
1152 RFCOMM_TRACE_API ("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags);
1154 /* Check if handle is valid to avoid crashing */
1155 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1157 return (PORT_BAD_HANDLE);
1160 p_port = &rfc_cb.port.port[handle - 1];
1162 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1164 return (PORT_NOT_OPENED);
1167 if (purge_flags & PORT_PURGE_RXCLEAR)
1169 PORT_SCHEDULE_LOCK; /* to prevent missing credit */
1171 count = GKI_queue_length(&p_port->rx.queue);
1173 while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue)) != NULL)
1174 GKI_freebuf (p_buf);
1176 p_port->rx.queue_size = 0;
1178 PORT_SCHEDULE_UNLOCK;
1180 /* If we flowed controlled peer based on rx_queue size enable data again */
1182 port_flow_control_peer (p_port, TRUE, count);
1185 if (purge_flags & PORT_PURGE_TXCLEAR)
1187 PORT_SCHEDULE_LOCK; /* to prevent tx.queue_size from being negative */
1189 while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->tx.queue)) != NULL)
1190 GKI_freebuf (p_buf);
1192 p_port->tx.queue_size = 0;
1194 PORT_SCHEDULE_UNLOCK;
1196 events = PORT_EV_TXEMPTY;
1198 events |= port_flow_control_user (p_port);
1200 events &= p_port->ev_mask;
1202 if ((p_port->p_callback != NULL) && events)
1203 (p_port->p_callback)(events, p_port->inx);
1206 return (PORT_SUCCESS);
1210 /*******************************************************************************
1212 ** Function PORT_ReadData
1214 ** Description Normally not GKI aware application will call this function
1215 ** after receiving PORT_EV_RXCHAR event.
1217 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1218 ** p_data - Data area
1219 ** max_len - Byte count requested
1220 ** p_len - Byte count received
1222 *******************************************************************************/
1223 int PORT_ReadData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
1229 RFCOMM_TRACE_API ("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
1231 /* Initialize this in case of an error */
1234 /* Check if handle is valid to avoid crashing */
1235 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1237 return (PORT_BAD_HANDLE);
1240 p_port = &rfc_cb.port.port[handle - 1];
1242 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1244 return (PORT_NOT_OPENED);
1247 if (p_port->line_status)
1249 return (PORT_LINE_ERR);
1252 p_buf = (BT_HDR *)GKI_getfirst (&p_port->rx.queue);
1254 return (PORT_SUCCESS);
1258 while (max_len && p_buf)
1260 if (p_buf->len > max_len)
1262 memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, max_len);
1263 p_buf->offset += max_len;
1264 p_buf->len -= max_len;
1270 p_port->rx.queue_size -= max_len;
1272 PORT_SCHEDULE_UNLOCK;
1278 memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
1280 *p_len += p_buf->len;
1281 max_len -= p_buf->len;
1285 p_port->rx.queue_size -= p_buf->len;
1289 p_data += p_buf->len;
1290 p_buf = (BT_HDR *)GKI_getnext (p_buf);
1293 GKI_freebuf (GKI_dequeue (&p_port->rx.queue));
1295 PORT_SCHEDULE_UNLOCK;
1303 RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size, *p_len, (p_data[0]));
1307 RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size, *p_len);
1310 /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
1311 /* check if it can be resumed now */
1312 port_flow_control_peer (p_port, TRUE, count);
1314 return (PORT_SUCCESS);
1318 /*******************************************************************************
1320 ** Function PORT_Read
1322 ** Description Normally application will call this function after receiving
1323 ** PORT_EV_RXCHAR event.
1325 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1326 ** pp_buf - pointer to address of buffer with data,
1328 *******************************************************************************/
1329 int PORT_Read (UINT16 handle, BT_HDR **pp_buf)
1334 RFCOMM_TRACE_API ("PORT_Read() handle:%d", handle);
1336 /* Check if handle is valid to avoid crashing */
1337 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1339 return (PORT_BAD_HANDLE);
1341 p_port = &rfc_cb.port.port[handle - 1];
1343 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1345 return (PORT_NOT_OPENED);
1348 if (p_port->line_status)
1350 return (PORT_LINE_ERR);
1355 p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue);
1358 p_port->rx.queue_size -= p_buf->len;
1360 PORT_SCHEDULE_UNLOCK;
1362 /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
1363 /* check if it can be resumed now */
1364 port_flow_control_peer (p_port, TRUE, 1);
1368 PORT_SCHEDULE_UNLOCK;
1372 return (PORT_SUCCESS);
1376 /*******************************************************************************
1378 ** Function port_write
1380 ** Description This function when a data packet is received from the apper
1383 ** Parameters: p_port - pointer to address of port control block
1384 ** p_buf - pointer to address of buffer with data,
1386 *******************************************************************************/
1387 static int port_write (tPORT *p_port, BT_HDR *p_buf)
1389 /* We should not allow to write data in to server port when connection is not opened */
1390 if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED))
1392 GKI_freebuf (p_buf);
1393 return (PORT_CLOSED);
1396 /* Keep the data in pending queue if peer does not allow data, or */
1397 /* Peer is not ready or Port is not yet opened or initial port control */
1398 /* command has not been sent */
1399 if (p_port->tx.peer_fc
1400 || !p_port->rfc.p_mcb
1401 || !p_port->rfc.p_mcb->peer_ready
1402 || (p_port->rfc.state != RFC_STATE_OPENED)
1403 || ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) !=
1404 (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)))
1406 if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM)
1407 || (GKI_queue_length(&p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM))
1409 RFCOMM_TRACE_WARNING ("PORT_Write: Queue size: %d",
1410 p_port->tx.queue_size);
1412 GKI_freebuf (p_buf);
1414 if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR))
1415 p_port->p_callback (PORT_EV_ERR, p_port->inx);
1417 return (PORT_TX_FULL);
1420 RFCOMM_TRACE_EVENT ("PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d ctrl_state %x",
1422 (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready),
1426 GKI_enqueue (&p_port->tx.queue, p_buf);
1427 p_port->tx.queue_size += p_buf->len;
1429 return (PORT_CMD_PENDING);
1433 RFCOMM_TRACE_EVENT ("PORT_Write : Data is being sent");
1435 RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
1436 return (PORT_SUCCESS);
1440 /*******************************************************************************
1442 ** Function PORT_Write
1444 ** Description This function when a data packet is received from the apper
1447 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1448 ** pp_buf - pointer to address of buffer with data,
1450 *******************************************************************************/
1451 int PORT_Write (UINT16 handle, BT_HDR *p_buf)
1457 RFCOMM_TRACE_API ("PORT_Write() handle:%d", handle);
1459 /* Check if handle is valid to avoid crashing */
1460 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1462 GKI_freebuf (p_buf);
1463 return (PORT_BAD_HANDLE);
1466 p_port = &rfc_cb.port.port[handle - 1];
1468 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1470 GKI_freebuf (p_buf);
1471 return (PORT_NOT_OPENED);
1474 if (p_port->line_status)
1476 RFCOMM_TRACE_WARNING ("PORT_Write: Data dropped line_status:0x%x",
1477 p_port->line_status);
1478 GKI_freebuf (p_buf);
1479 return (PORT_LINE_ERR);
1482 rc = port_write (p_port, p_buf);
1483 event |= port_flow_control_user (p_port);
1488 event |= PORT_EV_ERR;
1492 event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY);
1495 /* Mask out all events that are not of interest to user */
1496 event &= p_port->ev_mask;
1498 /* Send event to the application */
1499 if (p_port->p_callback && event)
1500 (p_port->p_callback)(event, p_port->inx);
1502 return (PORT_SUCCESS);
1504 /*******************************************************************************
1506 ** Function PORT_WriteDataCO
1508 ** Description Normally not GKI aware application will call this function
1509 ** to send data to the port by callout functions
1511 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1513 ** p_len - Byte count returned
1515 *******************************************************************************/
1516 int PORT_WriteDataCO (UINT16 handle, int* p_len)
1525 RFCOMM_TRACE_API ("PORT_WriteDataCO() handle:%d", handle);
1529 /* Check if handle is valid to avoid crashing */
1530 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1532 return (PORT_BAD_HANDLE);
1534 p_port = &rfc_cb.port.port[handle - 1];
1536 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1538 RFCOMM_TRACE_WARNING ("PORT_WriteDataByFd() no port state:%d", p_port->state);
1539 return (PORT_NOT_OPENED);
1542 if (!p_port->peer_mtu)
1544 RFCOMM_TRACE_ERROR ("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
1545 return (PORT_UNKNOWN_ERROR);
1548 //if(ioctl(fd, FIONREAD, &available) < 0)
1549 if(p_port->p_data_co_callback(handle, (UINT8*)&available, sizeof(available),
1550 DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE) == FALSE)
1552 RFCOMM_TRACE_ERROR("p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, available:%d", available);
1553 return (PORT_UNKNOWN_ERROR);
1556 return PORT_SUCCESS;
1557 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
1558 length = RFCOMM_DATA_POOL_BUF_SIZE -
1559 (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
1561 /* If there are buffers scheduled for transmission check if requested */
1562 /* data fits into the end of the queue */
1565 if (((p_buf = (BT_HDR *)GKI_getlast(&p_port->tx.queue)) != NULL)
1566 && (((int)p_buf->len + available) <= (int)p_port->peer_mtu)
1567 && (((int)p_buf->len + available) <= (int)length))
1569 //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, available, 0) != available)
1570 if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len,
1571 available, DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)
1574 error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, available:%d", available);
1575 PORT_SCHEDULE_UNLOCK;
1576 return (PORT_UNKNOWN_ERROR);
1578 //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
1579 p_port->tx.queue_size += (UINT16)available;
1582 p_buf->len += (UINT16)available;
1584 PORT_SCHEDULE_UNLOCK;
1586 return (PORT_SUCCESS);
1589 PORT_SCHEDULE_UNLOCK;
1591 //int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu;
1593 //max_read = available < max_read ? available : max_read;
1597 /* if we're over buffer high water mark, we're done */
1598 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM)
1599 || (GKI_queue_length(&p_port->tx.queue) > PORT_TX_BUF_HIGH_WM))
1601 port_flow_control_user(p_port);
1602 event |= PORT_EV_FC;
1603 debug("tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d",
1604 p_port->tx.queue_size, GKI_queue_length(&p_port->tx.queue), available);
1608 /* continue with rfcomm data write */
1609 p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
1613 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
1614 p_buf->layer_specific = handle;
1616 if (p_port->peer_mtu < length)
1617 length = p_port->peer_mtu;
1618 if (available < (int)length)
1619 length = (UINT16)available;
1620 p_buf->len = length;
1621 p_buf->event = BT_EVT_TO_BTU_SP_DATA;
1623 //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
1624 //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset, (int)length, 0) != (int)length)
1625 if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset, length,
1626 DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)
1628 error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d", length);
1629 return (PORT_UNKNOWN_ERROR);
1633 RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
1635 rc = port_write (p_port, p_buf);
1637 /* If queue went below the threashold need to send flow control */
1638 event |= port_flow_control_user (p_port);
1640 if (rc == PORT_SUCCESS)
1641 event |= PORT_EV_TXCHAR;
1643 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING))
1647 available -= (int)length;
1649 if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
1650 event |= PORT_EV_TXEMPTY;
1652 /* Mask out all events that are not of interest to user */
1653 event &= p_port->ev_mask;
1655 /* Send event to the application */
1656 if (p_port->p_callback && event)
1657 (p_port->p_callback)(event, p_port->inx);
1659 return (PORT_SUCCESS);
1664 /*******************************************************************************
1666 ** Function PORT_WriteData
1668 ** Description Normally not GKI aware application will call this function
1669 ** to send data to the port.
1671 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1672 ** p_data - Data area
1673 ** max_len - Byte count requested
1674 ** p_len - Byte count received
1676 *******************************************************************************/
1677 int PORT_WriteData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
1685 RFCOMM_TRACE_API ("PORT_WriteData() max_len:%d", max_len);
1689 /* Check if handle is valid to avoid crashing */
1690 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1692 return (PORT_BAD_HANDLE);
1694 p_port = &rfc_cb.port.port[handle - 1];
1696 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1698 RFCOMM_TRACE_WARNING ("PORT_WriteData() no port state:%d", p_port->state);
1699 return (PORT_NOT_OPENED);
1702 if (!max_len || !p_port->peer_mtu)
1704 RFCOMM_TRACE_ERROR ("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
1705 return (PORT_UNKNOWN_ERROR);
1708 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
1709 length = RFCOMM_DATA_POOL_BUF_SIZE -
1710 (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
1712 /* If there are buffers scheduled for transmission check if requested */
1713 /* data fits into the end of the queue */
1716 if (((p_buf = (BT_HDR *)GKI_getlast(&p_port->tx.queue)) != NULL)
1717 && ((p_buf->len + max_len) <= p_port->peer_mtu)
1718 && ((p_buf->len + max_len) <= length))
1720 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
1721 p_port->tx.queue_size += max_len;
1724 p_buf->len += max_len;
1726 PORT_SCHEDULE_UNLOCK;
1728 return (PORT_SUCCESS);
1731 PORT_SCHEDULE_UNLOCK;
1735 /* if we're over buffer high water mark, we're done */
1736 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM)
1737 || (GKI_queue_length(&p_port->tx.queue) > PORT_TX_BUF_HIGH_WM))
1740 /* continue with rfcomm data write */
1741 p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
1745 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
1746 p_buf->layer_specific = handle;
1748 if (p_port->peer_mtu < length)
1749 length = p_port->peer_mtu;
1750 if (max_len < length)
1752 p_buf->len = length;
1753 p_buf->event = BT_EVT_TO_BTU_SP_DATA;
1755 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
1757 RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
1759 rc = port_write (p_port, p_buf);
1761 /* If queue went below the threashold need to send flow control */
1762 event |= port_flow_control_user (p_port);
1764 if (rc == PORT_SUCCESS)
1765 event |= PORT_EV_TXCHAR;
1767 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING))
1775 if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
1776 event |= PORT_EV_TXEMPTY;
1778 /* Mask out all events that are not of interest to user */
1779 event &= p_port->ev_mask;
1781 /* Send event to the application */
1782 if (p_port->p_callback && event)
1783 (p_port->p_callback)(event, p_port->inx);
1785 return (PORT_SUCCESS);
1789 /*******************************************************************************
1791 ** Function PORT_Test
1793 ** Description Application can call this function to send RFCOMM Test frame
1795 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection
1796 ** p_data - Data area
1797 ** max_len - Byte count requested
1799 *******************************************************************************/
1800 int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len)
1805 RFCOMM_TRACE_API ("PORT_Test() len:%d", len);
1807 if ((handle == 0) || (handle > MAX_RFC_PORTS))
1809 return (PORT_BAD_HANDLE);
1811 p_port = &rfc_cb.port.port[handle - 1];
1813 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
1815 return (PORT_NOT_OPENED);
1818 if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu))
1820 return (PORT_UNKNOWN_ERROR);
1823 if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) != NULL)
1826 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
1829 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len);
1831 rfc_send_test (p_port->rfc.p_mcb, TRUE, p_buf);
1832 return (PORT_SUCCESS);
1836 return (PORT_NO_MEM);
1840 /*******************************************************************************
1842 ** Function RFCOMM_Init
1844 ** Description This function is called to initialize RFCOMM layer
1846 *******************************************************************************/
1847 void RFCOMM_Init (void)
1849 memset (&rfc_cb, 0, sizeof (tRFC_CB)); /* Init RFCOMM control block */
1851 rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
1853 #if defined(RFCOMM_INITIAL_TRACE_LEVEL)
1854 rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL;
1856 rfc_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
1859 rfcomm_l2cap_if_init ();
1862 /*******************************************************************************
1864 ** Function PORT_SetTraceLevel
1866 ** Description This function sets the trace level for RFCOMM. If called with
1867 ** a value of 0xFF, it simply reads the current trace level.
1869 ** Returns the new (current) trace level
1871 *******************************************************************************/
1872 UINT8 PORT_SetTraceLevel (UINT8 new_level)
1874 if (new_level != 0xFF)
1875 rfc_cb.trace_level = new_level;
1877 return (rfc_cb.trace_level);
1880 /*******************************************************************************
1882 ** Function PORT_GetResultString
1884 ** Description This function returns the human-readable string for a given
1887 ** Returns a pointer to the human-readable string for the given result.
1889 *******************************************************************************/
1890 const char *PORT_GetResultString (const uint8_t result_code) {
1891 if (result_code > PORT_ERR_MAX) {
1892 return result_code_strings[PORT_ERR_MAX];
1895 return result_code_strings[result_code];