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