port_int.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /******************************************************************************
  2. *
  3. * Copyright 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /*****************************************************************************
  19. *
  20. * This file contains definitions internal to the PORT unit
  21. *
  22. *****************************************************************************/
  23. #ifndef PORT_INT_H
  24. #define PORT_INT_H
  25. #include "bt_common.h"
  26. #include "bt_target.h"
  27. #include "osi/include/alarm.h"
  28. #include "osi/include/fixed_queue.h"
  29. #include "port_api.h"
  30. #include "rfcdefs.h"
  31. /* Local events passed when application event is sent from the api to PORT */
  32. /* ???*/
  33. #define PORT_EVENT_OPEN (1 | BT_EVT_TO_BTU_SP_EVT)
  34. #define PORT_EVENT_CONTROL (2 | BT_EVT_TO_BTU_SP_EVT)
  35. #define PORT_EVENT_SET_STATE (3 | BT_EVT_TO_BTU_SP_EVT)
  36. #define PORT_EVENT_SET_CALLBACK (5 | BT_EVT_TO_BTU_SP_EVT)
  37. #define PORT_EVENT_WRITE (6 | BT_EVT_TO_BTU_SP_EVT)
  38. #define PORT_EVENT_PURGE (7 | BT_EVT_TO_BTU_SP_EVT)
  39. #define PORT_EVENT_SEND_ERROR (8 | BT_EVT_TO_BTU_SP_EVT)
  40. #define PORT_EVENT_FLOW_CONTROL (9 | BT_EVT_TO_BTU_SP_EVT)
  41. /*
  42. * Flow control configuration values for the mux
  43. */
  44. #define PORT_FC_UNDEFINED 0 /* mux flow control mechanism not defined yet */
  45. #define PORT_FC_TS710 1 /* use TS 07.10 flow control */
  46. #define PORT_FC_CREDIT 2 /* use RFCOMM credit based flow control */
  47. /*
  48. * Define Port Data Transfere control block
  49. */
  50. typedef struct {
  51. fixed_queue_t* queue; /* Queue of buffers waiting to be sent */
  52. bool peer_fc; /* true if flow control is set based on peer's request */
  53. bool user_fc; /* true if flow control is set based on user's request */
  54. uint32_t queue_size; /* Number of data bytes in the queue */
  55. tPORT_CALLBACK* p_callback; /* Address of the callback function */
  56. } tPORT_DATA;
  57. /*
  58. * Port control structure used to pass modem info
  59. */
  60. typedef struct {
  61. #define MODEM_SIGNAL_DTRDSR 0x01
  62. #define MODEM_SIGNAL_RTSCTS 0x02
  63. #define MODEM_SIGNAL_RI 0x04
  64. #define MODEM_SIGNAL_DCD 0x08
  65. uint8_t modem_signal; /* [DTR/DSR | RTS/CTS | RI | DCD ] */
  66. uint8_t break_signal; /* 0-3 s in steps of 200 ms */
  67. uint8_t discard_buffers; /* 0 - do not discard, 1 - discard */
  68. #define RFCOMM_CTRL_BREAK_ASAP 0
  69. #define RFCOMM_CTRL_BREAK_IN_SEQ 1
  70. uint8_t break_signal_seq; /* as soon as possible | in sequence (default) */
  71. bool fc; /* true when the device is unable to accept frames */
  72. } tPORT_CTRL;
  73. /*
  74. * RFCOMM multiplexer Control Block
  75. */
  76. typedef struct {
  77. alarm_t* mcb_timer; /* MCB timer */
  78. fixed_queue_t* cmd_q; /* Queue for command messages on this mux */
  79. uint8_t port_handles[RFCOMM_MAX_DLCI + 1]; /* Array for quick access to */
  80. /* port handles based on dlci */
  81. RawAddress bd_addr; /* BD ADDR of the peer if initiator */
  82. uint16_t lcid; /* Local cid used for this channel */
  83. uint16_t peer_l2cap_mtu; /* Max frame that can be sent to peer L2CAP */
  84. uint8_t state; /* Current multiplexer channel state */
  85. uint8_t is_initiator; /* true if this side sends SABME (dlci=0) */
  86. bool local_cfg_sent;
  87. bool peer_cfg_rcvd;
  88. bool restart_required; /* true if has to restart channel after disc */
  89. bool peer_ready; /* True if other side can accept frames */
  90. uint8_t flow; /* flow control mechanism for this mux */
  91. bool l2cap_congested; /* true if L2CAP is congested */
  92. bool is_disc_initiator; /* true if initiated disc of port */
  93. uint16_t
  94. pending_lcid; /* store LCID for incoming connection while connecting */
  95. uint8_t
  96. pending_id; /* store l2cap ID for incoming connection while connecting */
  97. } tRFC_MCB;
  98. /*
  99. * RFCOMM Port Connection Control Block
  100. */
  101. typedef struct {
  102. #define RFC_PORT_STATE_IDLE 0
  103. #define RFC_PORT_STATE_WAIT_START 1
  104. #define RFC_PORT_STATE_OPENING 2
  105. #define RFC_PORT_STATE_OPENED 3
  106. #define RFC_PORT_STATE_CLOSING 4
  107. uint8_t state; /* Current state of the connection */
  108. #define RFC_RSP_PN 0x01
  109. #define RFC_RSP_RPN_REPLY 0x02
  110. #define RFC_RSP_RPN 0x04
  111. #define RFC_RSP_MSC 0x08
  112. #define RFC_RSP_RLS 0x10
  113. uint8_t expected_rsp;
  114. tRFC_MCB* p_mcb;
  115. alarm_t* port_timer;
  116. } tRFC_PORT;
  117. /*
  118. * Define control block containing information about PORT connection
  119. */
  120. typedef struct {
  121. uint8_t handle; // Starting from 1, unique for this object
  122. bool in_use; /* True when structure is allocated */
  123. #define PORT_STATE_CLOSED 0
  124. #define PORT_STATE_OPENING 1
  125. #define PORT_STATE_OPENED 2
  126. #define PORT_STATE_CLOSING 3
  127. uint8_t state; /* State of the application */
  128. uint8_t scn; /* Service channel number */
  129. uint16_t uuid; /* Service UUID */
  130. RawAddress bd_addr; /* BD ADDR of the device for the multiplexer channel */
  131. bool is_server; /* true if the server application */
  132. uint8_t dlci; /* DLCI of the connection */
  133. uint8_t error; /* Last error detected */
  134. uint8_t line_status; /* Line status as reported by peer */
  135. uint8_t default_signal_state; /* Initial signal state depending on uuid */
  136. uint16_t mtu; /* Max MTU that port can receive */
  137. uint16_t peer_mtu; /* Max MTU that port can send */
  138. tPORT_DATA tx; /* Control block for data from app to peer */
  139. tPORT_DATA rx; /* Control block for data from peer to app */
  140. tPORT_STATE user_port_pars; /* Port parameters for user connection */
  141. tPORT_STATE peer_port_pars; /* Port parameters for user connection */
  142. tPORT_CTRL local_ctrl;
  143. tPORT_CTRL peer_ctrl;
  144. #define PORT_CTRL_REQ_SENT 0x01
  145. #define PORT_CTRL_REQ_CONFIRMED 0x02
  146. #define PORT_CTRL_IND_RECEIVED 0x04
  147. #define PORT_CTRL_IND_RESPONDED 0x08
  148. uint8_t port_ctrl; /* Modem Status Command */
  149. bool rx_flag_ev_pending; /* RXFLAG Character is received */
  150. tRFC_PORT rfc; /* RFCOMM port control block */
  151. uint32_t ev_mask; /* Event mask for the callback */
  152. tPORT_CALLBACK* p_callback; /* Pointer to users callback function */
  153. tPORT_CALLBACK*
  154. p_mgmt_callback; /* Callback function to receive connection up/down */
  155. tPORT_DATA_CALLBACK*
  156. p_data_callback; /* Callback function to receive data indications */
  157. tPORT_DATA_CO_CALLBACK*
  158. p_data_co_callback; /* Callback function with callouts and flowctrl */
  159. uint16_t credit_tx; /* Flow control credits for tx path */
  160. uint16_t credit_rx; /* Flow control credits for rx path, this is */
  161. /* number of buffers peer is allowed to sent */
  162. uint16_t
  163. credit_rx_max; /* Max number of credits we will allow this guy to sent */
  164. uint16_t credit_rx_low; /* Number of credits when we send credit update */
  165. uint16_t rx_buf_critical; /* port receive queue critical watermark level */
  166. bool keep_port_handle; /* true if port is not deallocated when closing */
  167. /* it is set to true for server when allocating port */
  168. uint16_t keep_mtu; /* Max MTU that port can receive by server */
  169. } tPORT;
  170. /* Define the PORT/RFCOMM control structure
  171. */
  172. typedef struct {
  173. tPORT port[MAX_RFC_PORTS]; /* Port info pool */
  174. tRFC_MCB rfc_mcb[MAX_BD_CONNECTIONS]; /* RFCOMM bd_connections pool */
  175. } tPORT_CB;
  176. /*
  177. * Functions provided by the port_utils.cc
  178. */
  179. extern tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr);
  180. extern void port_set_defaults(tPORT* p_port);
  181. extern void port_select_mtu(tPORT* p_port);
  182. extern void port_release_port(tPORT* p_port);
  183. extern tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci);
  184. extern tRFC_MCB* port_find_mcb(const RawAddress& bd_addr);
  185. extern tPORT* port_find_dlci_port(uint8_t dlci);
  186. extern tPORT* port_find_port(uint8_t dlci, const RawAddress& bd_addr);
  187. extern uint32_t port_get_signal_changes(tPORT* p_port, uint8_t old_signals,
  188. uint8_t signal);
  189. extern uint32_t port_flow_control_user(tPORT* p_port);
  190. extern void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count);
  191. /*
  192. * Functions provided by the port_rfc.cc
  193. */
  194. extern int port_open_continue(tPORT* p_port);
  195. extern void port_start_port_open(tPORT* p_port);
  196. extern void port_start_par_neg(tPORT* p_port);
  197. extern void port_start_control(tPORT* p_port);
  198. extern void port_start_close(tPORT* p_port);
  199. extern void port_rfc_closed(tPORT* p_port, uint8_t res);
  200. #endif