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 * 21 * This file contains definitions internal to the RFC unit 22 * 23 *****************************************************************************/ 24 25 #ifndef RFC_INT_H 26 #define RFC_INT_H 27 28 #include <cstdint> 29 #include <cstring> 30 #include <unordered_map> 31 32 #include "stack/include/bt_hdr.h" 33 #include "stack/include/btm_status.h" 34 #include "stack/include/l2c_api.h" 35 #include "stack/rfcomm/port_int.h" 36 #include "stack/rfcomm/rfc_event.h" 37 #include "stack/rfcomm/rfc_state.h" 38 #include "types/raw_address.h" 39 40 /* 41 * Define RFCOMM result codes 42 */ 43 #define RFCOMM_SUCCESS 0 44 #define RFCOMM_ERROR 1 45 #define RFCOMM_SECURITY_ERR 112 46 47 /* 48 * Define max and min RFCOMM MTU (N1) 49 */ 50 #define RFCOMM_MIN_MTU 23 51 #define RFCOMM_MAX_MTU 32767 52 53 void RFCOMM_StartReq(tRFC_MCB* p_mcb); 54 void RFCOMM_StartRsp(tRFC_MCB* p_mcb, uint16_t result); 55 56 void RFCOMM_DlcEstablishReq(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu); 57 void RFCOMM_DlcEstablishRsp(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, 58 uint16_t result); 59 60 void RFCOMM_DataReq(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf); 61 62 void RFCOMM_DlcReleaseReq(tRFC_MCB* p_mcb, uint8_t dlci); 63 64 void RFCOMM_ParameterNegotiationRequest(tRFC_MCB* p_mcb, uint8_t dlci, 65 uint16_t mtu); 66 void RFCOMM_ParameterNegotiationResponse(tRFC_MCB* p_mcb, uint8_t dlci, 67 uint16_t mtu, uint8_t cl, uint8_t k); 68 69 void RFCOMM_FlowReq(tRFC_MCB* p_mcb, uint8_t dlci, bool state); 70 71 void RFCOMM_PortParameterNegotiationRequest(tRFC_MCB* p_mcb, uint8_t dlci, 72 tPORT_STATE* p_pars); 73 void RFCOMM_PortParameterNegotiationResponse(tRFC_MCB* p_mcb, uint8_t dlci, 74 tPORT_STATE* p_pars, 75 uint16_t param_mask); 76 77 void RFCOMM_ControlReq(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 78 void RFCOMM_ControlRsp(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 79 80 void RFCOMM_LineStatusReq(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status); 81 /* 82 * Define logical struct used for sending and decoding MX frames 83 */ 84 typedef struct { 85 uint8_t dlci; 86 uint8_t type; 87 uint8_t cr; 88 uint8_t ea; 89 uint8_t pf; 90 uint8_t credit; 91 92 union { 93 struct { 94 uint8_t dlci; 95 uint8_t frame_type; 96 uint8_t conv_layer; 97 uint8_t priority; 98 uint8_t t1; 99 uint16_t mtu; 100 uint8_t n2; 101 uint8_t k; 102 } pn; 103 struct { 104 uint8_t* p_data; 105 uint16_t data_len; 106 } test; 107 struct { 108 uint8_t dlci; 109 uint8_t signals; 110 uint8_t break_present; 111 uint8_t break_duration; 112 } msc; 113 struct { 114 uint8_t ea; 115 uint8_t cr; 116 uint8_t type; 117 } nsc; 118 struct { 119 uint8_t dlci; 120 uint8_t is_request; 121 uint8_t baud_rate; 122 uint8_t byte_size; 123 uint8_t stop_bits; 124 uint8_t parity; 125 uint8_t parity_type; 126 uint8_t fc_type; 127 uint8_t xon_char; 128 uint8_t xoff_char; 129 uint16_t param_mask; 130 } rpn; 131 struct { 132 uint8_t dlci; 133 uint8_t line_status; 134 } rls; 135 } u; 136 } MX_FRAME; 137 138 #define LINE_STATUS_NO_ERROR 0x00 139 #define LINE_STATUS_OVERRUN 0x02 /* Receive Overrun Error */ 140 #define LINE_STATUS_RXPARITY 0x04 /* Receive Parity Error */ 141 #define LINE_STATUS_FRAME 0x08 /* Receive Framing error */ 142 #define LINE_STATUS_FAILED 0x10 /* Connection Failed */ 143 144 /* seconds to wait for reply with Poll bit */ 145 #define RFC_T1_TIMEOUT 20 146 /* seconds to wait for reply with Poll bit other than MX */ 147 #define RFC_PORT_T1_TIMEOUT 60 148 /* timeout to wait for Mx UIH */ 149 #define RFC_T2_TIMEOUT 20 150 /* If something goes wrong and we send DISC we should not wait for min */ 151 #define RFC_DISC_TIMEOUT 3 152 #define RFC_CLOSE_TIMEOUT 10 153 /* first connection to be established on Mx */ 154 #define RFCOMM_CONN_TIMEOUT 120 155 156 /* Define RFComm control block 157 */ 158 typedef struct { 159 MX_FRAME rx_frame; 160 tL2CAP_APPL_INFO reg_info; /* L2CAP Registration info */ 161 162 bool peer_rx_disabled; /* If true peer sent FCOFF */ 163 uint8_t last_mux; /* Last mux allocated */ 164 uint8_t last_port_index; // Index of last port allocated in rfc_cb.port 165 } tRFCOMM_CB; 166 167 /* Main Control Block for the RFCOMM Layer (PORT and RFC) */ 168 typedef struct { 169 tRFCOMM_CB rfc; 170 tPORT_CB port; 171 uint8_t trace_level; 172 } tRFC_CB; 173 174 extern tRFC_CB rfc_cb; 175 176 /* MCB based on the L2CAP's lcid */ 177 extern std::unordered_map<uint16_t /* cid */, tRFC_MCB*> rfc_lcid_mcb; 178 179 /* Timer running on the multiplexor channel while no DLCI connection is open */ 180 #define RFC_MCB_INIT_INACT_TIMER 60 /* in seconds */ 181 182 /* Timer running on the multiplexor channel after last DLCI is released */ 183 #define RFC_MCB_RELEASE_INACT_TIMER 20 /* in seconds */ 184 185 #ifdef RFCOMM_PRECALC_FCS 186 187 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_sabme_fcs[cr][dlci] 188 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_ua_fcs[cr][dlci] 189 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_dm_fcs[cr][dlci] 190 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_disc_fcs[cr][dlci] 191 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_uih_fcs[dlci] 192 193 #else 194 195 uint8_t rfc_calc_fcs(uint16_t len, uint8_t* p); 196 197 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 198 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 199 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 200 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 201 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_calc_fcs(2, p_data) 202 203 #endif 204 205 void rfc_mx_sm_execute(tRFC_MCB* p_mcb, tRFC_MX_EVENT event, void* p_data); 206 207 /* 208 * Functions provided by the rfc_port_fsm.cc 209 */ 210 void rfc_port_sm_execute(tPORT* p_port, tRFC_PORT_EVENT event, void* p_data); 211 212 void rfc_process_pn(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 213 void rfc_process_msc(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 214 void rfc_process_rpn(tRFC_MCB* p_rfc_mcb, bool is_command, bool is_request, 215 MX_FRAME* p_frame); 216 void rfc_process_rls(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 217 void rfc_process_nsc(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame); 218 void rfc_process_test_rsp(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf); 219 void rfc_process_fcon(tRFC_MCB* p_rfc_mcb, bool is_command); 220 void rfc_process_fcoff(tRFC_MCB* p_rfc_mcb, bool is_command); 221 void rfc_process_l2cap_congestion(tRFC_MCB* p_mcb, bool is_congested); 222 223 void rfc_on_l2cap_error(uint16_t lcid, uint16_t result); 224 225 /* 226 * Functions provided by the rfc_utils.cc 227 */ 228 tRFC_MCB* rfc_alloc_multiplexer_channel(const RawAddress& bd_addr, 229 bool is_initiator); 230 void rfc_release_multiplexer_channel(tRFC_MCB* p_rfc_mcb); 231 void rfc_timer_start(tRFC_MCB* p_rfc_mcb, uint16_t timeout); 232 void rfc_timer_stop(tRFC_MCB* p_rfc_mcb); 233 void rfc_port_timer_start(tPORT* p_port, uint16_t tout); 234 void rfc_port_timer_stop(tPORT* p_port); 235 236 bool rfc_check_fcs(uint16_t len, uint8_t* p, uint8_t received_fcs); 237 tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid); 238 void rfc_save_lcid_mcb(tRFC_MCB* p_rfc_mcb, uint16_t lcid); 239 void rfc_check_mcb_active(tRFC_MCB* p_mcb); 240 void rfc_port_closed(tPORT* p_port); 241 void rfc_sec_check_complete(const RawAddress* bd_addr, tBT_TRANSPORT transport, 242 void* p_ref_data, tBTM_STATUS res); 243 void rfc_inc_credit(tPORT* p_port, uint8_t credit); 244 void rfc_dec_credit(tPORT* p_port); 245 void rfc_check_send_cmd(tRFC_MCB* p_mcb, BT_HDR* p_buf); 246 247 /* 248 * Functions provided by the rfc_ts_frames.cc 249 */ 250 void rfc_send_sabme(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 251 void rfc_send_ua(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 252 void rfc_send_dm(tRFC_MCB* p_rfc_mcb, uint8_t dlci, bool pf); 253 void rfc_send_disc(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 254 void rfc_send_pn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, uint16_t mtu, 255 uint8_t cl, uint8_t k); 256 void rfc_send_test(tRFC_MCB* p_rfc_mcb, bool is_command, BT_HDR* p_buf); 257 void rfc_send_msc(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 258 tPORT_CTRL* p_pars); 259 void rfc_send_rls(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 260 uint8_t status); 261 void rfc_send_rpn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 262 tPORT_STATE* p_pars, uint16_t mask); 263 void rfc_send_fcon(tRFC_MCB* p_mcb, bool is_command); 264 void rfc_send_fcoff(tRFC_MCB* p_mcb, bool is_command); 265 void rfc_send_buf_uih(tRFC_MCB* p_rfc_mcb, uint8_t dlci, BT_HDR* p_buf); 266 void rfc_send_credit(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t credit); 267 void rfc_process_mx_message(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf); 268 tRFC_EVENT rfc_parse_data(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame, 269 BT_HDR* p_buf); 270 271 /* Call back functions from RFCOMM */ 272 void rfcomm_l2cap_if_init(void); 273 274 void PORT_StartInd(tRFC_MCB* p_mcb); 275 void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result); 276 277 void PORT_CloseInd(tRFC_MCB* p_mcb); 278 void Port_TimeOutCloseMux(tRFC_MCB* p_mcb); 279 280 void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu); 281 void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, 282 uint16_t result); 283 284 void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf); 285 286 void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci); 287 288 void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl, 289 uint8_t k); 290 void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl, 291 uint8_t k); 292 293 void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool fc); 294 295 void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars, 296 uint16_t param_mask); 297 void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars, 298 uint16_t result); 299 300 void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 301 void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 302 303 void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status); 304 305 #endif 306