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 } tRFC_CB; 172 173 extern tRFC_CB rfc_cb; 174 175 /* MCB based on the L2CAP's lcid */ 176 extern std::unordered_map<uint16_t /* cid */, tRFC_MCB*> rfc_lcid_mcb; 177 178 /* Timer running on the multiplexor channel while no DLCI connection is open */ 179 #define RFC_MCB_INIT_INACT_TIMER 60 /* in seconds */ 180 181 /* Timer running on the multiplexor channel after last DLCI is released */ 182 #define RFC_MCB_RELEASE_INACT_TIMER 20 /* in seconds */ 183 184 #ifdef RFCOMM_PRECALC_FCS 185 186 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_sabme_fcs[cr][dlci] 187 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_ua_fcs[cr][dlci] 188 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_dm_fcs[cr][dlci] 189 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_disc_fcs[cr][dlci] 190 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_uih_fcs[dlci] 191 192 #else 193 194 uint8_t rfc_calc_fcs(uint16_t len, uint8_t* p); 195 196 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 197 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 198 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 199 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data) 200 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_calc_fcs(2, p_data) 201 202 #endif 203 204 void rfc_mx_sm_execute(tRFC_MCB* p_mcb, tRFC_MX_EVENT event, void* p_data); 205 206 /* 207 * Functions provided by the rfc_port_fsm.cc 208 */ 209 void rfc_port_sm_execute(tPORT* p_port, tRFC_PORT_EVENT event, void* p_data); 210 211 void rfc_process_pn(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 212 void rfc_process_msc(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 213 void rfc_process_rpn(tRFC_MCB* p_rfc_mcb, bool is_command, bool is_request, 214 MX_FRAME* p_frame); 215 void rfc_process_rls(tRFC_MCB* p_rfc_mcb, bool is_command, MX_FRAME* p_frame); 216 void rfc_process_nsc(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame); 217 void rfc_process_test_rsp(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf); 218 void rfc_process_fcon(tRFC_MCB* p_rfc_mcb, bool is_command); 219 void rfc_process_fcoff(tRFC_MCB* p_rfc_mcb, bool is_command); 220 void rfc_process_l2cap_congestion(tRFC_MCB* p_mcb, bool is_congested); 221 222 void rfc_on_l2cap_error(uint16_t lcid, uint16_t result); 223 224 /* 225 * Functions provided by the rfc_utils.cc 226 */ 227 tRFC_MCB* rfc_alloc_multiplexer_channel(const RawAddress& bd_addr, 228 bool is_initiator); 229 void rfc_release_multiplexer_channel(tRFC_MCB* p_rfc_mcb); 230 void rfc_timer_start(tRFC_MCB* p_rfc_mcb, uint16_t timeout); 231 void rfc_timer_stop(tRFC_MCB* p_rfc_mcb); 232 void rfc_port_timer_start(tPORT* p_port, uint16_t tout); 233 void rfc_port_timer_stop(tPORT* p_port); 234 235 bool rfc_check_fcs(uint16_t len, uint8_t* p, uint8_t received_fcs); 236 tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid); 237 void rfc_save_lcid_mcb(tRFC_MCB* p_rfc_mcb, uint16_t lcid); 238 void rfc_check_mcb_active(tRFC_MCB* p_mcb); 239 void rfc_port_closed(tPORT* p_port); 240 void rfc_sec_check_complete(RawAddress bd_addr, tBT_TRANSPORT transport, 241 void* p_ref_data, tBTM_STATUS res); 242 void rfc_inc_credit(tPORT* p_port, uint8_t credit); 243 void rfc_dec_credit(tPORT* p_port); 244 void rfc_check_send_cmd(tRFC_MCB* p_mcb, BT_HDR* p_buf); 245 246 /* 247 * Functions provided by the rfc_ts_frames.cc 248 */ 249 void rfc_send_sabme(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 250 void rfc_send_ua(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 251 void rfc_send_dm(tRFC_MCB* p_rfc_mcb, uint8_t dlci, bool pf); 252 void rfc_send_disc(tRFC_MCB* p_rfc_mcb, uint8_t dlci); 253 void rfc_send_pn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, uint16_t mtu, 254 uint8_t cl, uint8_t k); 255 void rfc_send_test(tRFC_MCB* p_rfc_mcb, bool is_command, BT_HDR* p_buf); 256 void rfc_send_msc(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 257 tPORT_CTRL* p_pars); 258 void rfc_send_rls(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 259 uint8_t status); 260 void rfc_send_rpn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, 261 tPORT_STATE* p_pars, uint16_t mask); 262 void rfc_send_fcon(tRFC_MCB* p_mcb, bool is_command); 263 void rfc_send_fcoff(tRFC_MCB* p_mcb, bool is_command); 264 void rfc_send_buf_uih(tRFC_MCB* p_rfc_mcb, uint8_t dlci, BT_HDR* p_buf); 265 void rfc_send_credit(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t credit); 266 void rfc_process_mx_message(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf); 267 tRFC_EVENT rfc_parse_data(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame, 268 BT_HDR* p_buf); 269 270 /* Call back functions from RFCOMM */ 271 void rfcomm_l2cap_if_init(void); 272 273 void PORT_StartInd(tRFC_MCB* p_mcb); 274 void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result); 275 276 void PORT_CloseInd(tRFC_MCB* p_mcb); 277 void PORT_TimeOutCloseMux(tRFC_MCB* p_mcb); 278 279 void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu); 280 void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, 281 uint16_t result); 282 283 void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf); 284 285 void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci); 286 287 void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl, 288 uint8_t k); 289 void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl, 290 uint8_t k); 291 292 void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool fc); 293 294 void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars, 295 uint16_t param_mask); 296 void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars, 297 uint16_t result); 298 299 void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 300 void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars); 301 302 void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status); 303 304 #endif 305