Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "l2cap/internal/enhanced_retransmission_mode_channel_data_controller.h" |
| 18 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <queue> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "common/bind.h" |
| 24 | #include "os/alarm.h" |
| 25 | #include "packet/fragmenting_inserter.h" |
| 26 | #include "packet/raw_builder.h" |
| 27 | |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 28 | namespace bluetooth { |
| 29 | namespace l2cap { |
| 30 | namespace internal { |
| 31 | ErtmController::ErtmController(Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end, os::Handler* handler, |
| 32 | Scheduler* scheduler) |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 33 | : cid_(cid), remote_cid_(remote_cid), enqueue_buffer_(channel_queue_end), handler_(handler), scheduler_(scheduler), |
| 34 | pimpl_(std::make_unique<impl>(this, handler)) {} |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 35 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 36 | ErtmController::~ErtmController() = default; |
| 37 | |
| 38 | struct ErtmController::impl { |
| 39 | impl(ErtmController* controller, os::Handler* handler) |
| 40 | : controller_(controller), handler_(handler), retrans_timer_(handler), monitor_timer_(handler) {} |
| 41 | |
| 42 | ErtmController* controller_; |
| 43 | os::Handler* handler_; |
| 44 | |
| 45 | // We don't support extended window |
| 46 | static constexpr uint8_t kMaxTxWin = 64; |
| 47 | |
| 48 | // We don't support sending SREJ |
| 49 | static constexpr bool kSendSrej = false; |
| 50 | |
| 51 | // States (@see 8.6.5.2): Transmitter state and receiver state |
| 52 | |
| 53 | enum class TxState { |
| 54 | XMIT, |
| 55 | WAIT_F, |
| 56 | }; |
| 57 | TxState tx_state_ = TxState::XMIT; |
| 58 | |
| 59 | enum class RxState { |
| 60 | RECV, |
| 61 | REJ_SENT, |
| 62 | SREJ_SENT, |
| 63 | }; |
| 64 | RxState rx_state_ = RxState::RECV; |
| 65 | |
| 66 | // Variables and Timers (@see 8.6.5.3) |
| 67 | |
| 68 | uint8_t tx_seq_ = 0; |
| 69 | uint8_t next_tx_seq_ = 0; |
| 70 | uint8_t expected_ack_seq_ = 0; |
| 71 | uint8_t req_seq_ = 0; |
| 72 | uint8_t expected_tx_seq_ = 0; |
| 73 | uint8_t buffer_seq_ = 0; |
| 74 | |
| 75 | bool remote_busy_ = false; |
| 76 | bool local_busy_ = false; |
| 77 | int unacked_frames_ = 0; |
| 78 | // TODO: Instead of having a map, we may consider about a better data structure |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 79 | // Map from TxSeq to (SAR, SDU size for START packet, information payload) |
| 80 | std::map<uint8_t, std::tuple<SegmentationAndReassembly, uint16_t, std::shared_ptr<packet::RawBuilder>>> unacked_list_; |
| 81 | // Stores (SAR, SDU size for START packet, information payload) |
| 82 | std::queue<std::tuple<SegmentationAndReassembly, uint16_t, std::unique_ptr<packet::RawBuilder>>> pending_frames_; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 83 | int retry_count_ = 0; |
| 84 | std::map<uint8_t /* tx_seq, */, int /* count */> retry_i_frames_; |
| 85 | bool rnr_sent_ = false; |
| 86 | bool rej_actioned_ = false; |
| 87 | bool srej_actioned_ = false; |
| 88 | uint16_t srej_save_req_seq_ = 0; |
| 89 | bool send_rej_ = false; |
| 90 | int buffer_seq_srej_ = 0; |
| 91 | int frames_sent_ = 0; |
| 92 | os::Alarm retrans_timer_; |
| 93 | os::Alarm monitor_timer_; |
| 94 | |
| 95 | // Events (@see 8.6.5.4) |
| 96 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 97 | void data_request(SegmentationAndReassembly sar, std::unique_ptr<packet::RawBuilder> pdu, uint16_t sdu_size = 0) { |
| 98 | // Note: sdu_size only applies to START packet |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 99 | if (tx_state_ == TxState::XMIT && !remote_busy() && rem_window_not_full()) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 100 | send_data(sar, sdu_size, std::move(pdu)); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 101 | } else if (tx_state_ == TxState::XMIT && (remote_busy() || rem_window_full())) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 102 | pend_data(sar, sdu_size, std::move(pdu)); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 103 | } else if (tx_state_ == TxState::WAIT_F) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 104 | pend_data(sar, sdu_size, std::move(pdu)); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | void local_busy_detected() { |
| 109 | local_busy_ = true; |
| 110 | } |
| 111 | |
| 112 | void local_busy_clear() { |
| 113 | if (tx_state_ == TxState::XMIT && rnr_sent()) { |
| 114 | local_busy_ = false; |
| 115 | rnr_sent_ = false; |
| 116 | send_rr(Poll::POLL); |
| 117 | retry_count_ = 1; |
| 118 | stop_retrans_timer(); |
| 119 | start_monitor_timer(); |
| 120 | } else if (tx_state_ == TxState::XMIT) { |
| 121 | local_busy_ = false; |
| 122 | rnr_sent_ = false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void recv_req_seq_and_f_bit(uint8_t req_seq, Final f) { |
| 127 | if (tx_state_ == TxState::XMIT) { |
| 128 | process_req_seq(req_seq); |
| 129 | } else if (f == Final::POLL_RESPONSE) { |
| 130 | process_req_seq(req_seq); |
| 131 | stop_monitor_timer(); |
| 132 | if (unacked_frames_ > 0) { |
| 133 | start_retrans_timer(); |
| 134 | } |
| 135 | tx_state_ = TxState::XMIT; |
| 136 | } else { |
| 137 | process_req_seq(req_seq); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void recv_f_bit(Final f) { |
| 142 | if (tx_state_ == TxState::WAIT_F && f == Final::POLL_RESPONSE) { |
| 143 | stop_monitor_timer(); |
| 144 | if (unacked_frames_ > 0) { |
| 145 | start_retrans_timer(); |
| 146 | } |
| 147 | tx_state_ = TxState::XMIT; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void retrans_timer_expires() { |
| 152 | if (tx_state_ == TxState::XMIT) { |
| 153 | send_rr_or_rnr(Poll::POLL); |
| 154 | // send rr or rnr(p=1) |
| 155 | retry_count_ = 1; |
| 156 | start_retrans_timer(); |
| 157 | tx_state_ = TxState::WAIT_F; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void monitor_timer_expires() { |
| 162 | if (tx_state_ == TxState::WAIT_F && retry_count_less_than_max_transmit()) { |
| 163 | retry_count_++; |
| 164 | send_rr_or_rnr(Poll::POLL); |
| 165 | start_monitor_timer(); |
| 166 | } else if (tx_state_ == TxState::WAIT_F) { |
| 167 | CloseChannel(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void recv_i_frame(Final f, uint8_t tx_seq, uint8_t req_seq, SegmentationAndReassembly sar, |
| 172 | const packet::PacketView<true>& payload) { |
| 173 | if (rx_state_ == RxState::RECV) { |
| 174 | if (f == Final::NOT_SET && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && |
| 175 | !local_busy()) { |
| 176 | increment_expected_tx_seq(); |
| 177 | pass_to_tx(req_seq, f); |
| 178 | data_indication(sar, payload); |
| 179 | send_ack(Final::NOT_SET); |
| 180 | } else if (f == Final::POLL_RESPONSE && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && |
| 181 | with_valid_f_bit(f) && !local_busy()) { |
| 182 | increment_expected_tx_seq(); |
| 183 | pass_to_tx(req_seq, f); |
| 184 | data_indication(sar, payload); |
| 185 | if (!rej_actioned_) { |
| 186 | retransmit_i_frames(req_seq); |
| 187 | send_pending_i_frames(); |
| 188 | } else { |
| 189 | rej_actioned_ = false; |
| 190 | } |
| 191 | send_ack(Final::NOT_SET); |
| 192 | } else if (with_duplicate_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && !local_busy()) { |
| 193 | pass_to_tx(req_seq, f); |
| 194 | } else if (with_unexpected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && |
| 195 | !local_busy()) { |
| 196 | if constexpr (kSendSrej) { |
| 197 | // We don't support sending SREJ |
| 198 | } else { |
| 199 | pass_to_tx(req_seq, f); |
| 200 | send_rej(); |
| 201 | rx_state_ = RxState::REJ_SENT; |
| 202 | } |
| 203 | } else if (with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && local_busy()) { |
| 204 | pass_to_tx(req_seq, f); |
| 205 | store_or_ignore(); |
| 206 | } else if (with_valid_req_seq(req_seq) && not_with_expected_tx_seq(tx_seq) && with_valid_f_bit(f) && |
| 207 | local_busy()) { |
| 208 | pass_to_tx(req_seq, f); |
| 209 | } else if ((with_invalid_tx_seq(tx_seq) && controller_->local_tx_window_ > kMaxTxWin / 2) || |
| 210 | with_invalid_req_seq(req_seq)) { |
| 211 | CloseChannel(); |
| 212 | } else if (with_invalid_tx_seq(tx_seq) && controller_->local_tx_window_ <= kMaxTxWin / 2) { |
| 213 | // We decided to ignore |
| 214 | } |
| 215 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 216 | if (f == Final::NOT_SET && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 217 | increment_expected_tx_seq(); |
| 218 | pass_to_tx(req_seq, f); |
| 219 | data_indication(sar, payload); |
| 220 | send_ack(Final::NOT_SET); |
| 221 | rx_state_ = RxState::RECV; |
| 222 | } else if (f == Final::POLL_RESPONSE && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && |
| 223 | with_valid_f_bit(f)) { |
| 224 | increment_expected_tx_seq(); |
| 225 | pass_to_tx(req_seq, f); |
| 226 | data_indication(sar, payload); |
| 227 | if (!rej_actioned_) { |
| 228 | retransmit_i_frames(req_seq); |
| 229 | send_pending_i_frames(); |
| 230 | } else { |
| 231 | rej_actioned_ = false; |
| 232 | } |
| 233 | send_ack(Final::NOT_SET); |
| 234 | rx_state_ = RxState::RECV; |
| 235 | } else if (with_unexpected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 236 | pass_to_tx(req_seq, f); |
| 237 | } |
| 238 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 239 | // SREJ NOT SUPPORTED |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void recv_rr(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 244 | if (rx_state_ == RxState::RECV) { |
| 245 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 246 | pass_to_tx(req_seq, f); |
| 247 | if (remote_busy() && unacked_frames_ > 0) { |
| 248 | start_retrans_timer(); |
| 249 | } |
| 250 | remote_busy_ = false; |
| 251 | send_pending_i_frames(); |
| 252 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 253 | remote_busy_ = false; |
| 254 | pass_to_tx(req_seq, f); |
| 255 | if (!rej_actioned_) { |
| 256 | retransmit_i_frames(req_seq, p); |
| 257 | } else { |
| 258 | rej_actioned_ = false; |
| 259 | } |
| 260 | send_pending_i_frames(); |
| 261 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 262 | pass_to_tx(req_seq, f); |
| 263 | send_i_or_rr_or_rnr(Final::POLL_RESPONSE); |
| 264 | } else if (with_invalid_req_seq(req_seq)) { |
| 265 | CloseChannel(); |
| 266 | } |
| 267 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 268 | if (f == Final::POLL_RESPONSE && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 269 | remote_busy_ = false; |
| 270 | pass_to_tx(req_seq, f); |
| 271 | if (!rej_actioned_) { |
| 272 | retransmit_i_frames(req_seq, p); |
| 273 | } else { |
| 274 | rej_actioned_ = false; |
| 275 | } |
| 276 | send_pending_i_frames(); |
| 277 | } else if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 278 | pass_to_tx(req_seq, f); |
| 279 | if (remote_busy() and unacked_frames_ > 0) { |
| 280 | start_retrans_timer(); |
| 281 | } |
| 282 | remote_busy_ = false; |
| 283 | send_ack(Final::NOT_SET); |
| 284 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 285 | pass_to_tx(req_seq, f); |
| 286 | if (remote_busy() and unacked_frames_ > 0) { |
| 287 | start_retrans_timer(); |
| 288 | } |
| 289 | remote_busy_ = false; |
| 290 | send_rr(Final::POLL_RESPONSE); |
| 291 | } else if (with_invalid_req_seq(req_seq)) { |
| 292 | CloseChannel(); |
| 293 | } |
| 294 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 295 | // SREJ NOT SUPPORTED |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void recv_rej(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 300 | if (rx_state_ == RxState::RECV) { |
| 301 | if (f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 302 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 303 | remote_busy_ = false; |
| 304 | pass_to_tx(req_seq, f); |
| 305 | retransmit_i_frames(req_seq, p); |
| 306 | send_pending_i_frames(); |
| 307 | if (p_bit_outstanding()) { |
| 308 | rej_actioned_ = true; |
| 309 | } |
| 310 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 311 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 312 | remote_busy_ = false; |
| 313 | pass_to_tx(req_seq, f); |
| 314 | if (!rej_actioned_) { |
| 315 | retransmit_i_frames(req_seq, p); |
| 316 | } else { |
| 317 | rej_actioned_ = false; |
| 318 | } |
| 319 | send_pending_i_frames(); |
| 320 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 321 | CloseChannel(); |
| 322 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 323 | CloseChannel(); |
| 324 | } |
| 325 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 326 | if (f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 327 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 328 | remote_busy_ = false; |
| 329 | pass_to_tx(req_seq, f); |
| 330 | retransmit_i_frames(req_seq, p); |
| 331 | send_pending_i_frames(); |
| 332 | if (p_bit_outstanding()) { |
| 333 | rej_actioned_ = true; |
| 334 | } |
| 335 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 336 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 337 | remote_busy_ = false; |
| 338 | pass_to_tx(req_seq, f); |
| 339 | if (!rej_actioned_) { |
| 340 | retransmit_i_frames(req_seq, p); |
| 341 | } else { |
| 342 | rej_actioned_ = false; |
| 343 | } |
| 344 | send_pending_i_frames(); |
| 345 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 346 | CloseChannel(); |
| 347 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 348 | CloseChannel(); |
| 349 | } |
| 350 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 351 | // SREJ NOT SUPPORTED |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void recv_rnr(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 356 | if (rx_state_ == RxState::RECV) { |
| 357 | if (p == Poll::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 358 | remote_busy_ = true; |
| 359 | pass_to_tx(req_seq, f); |
| 360 | stop_retrans_timer(); |
| 361 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 362 | remote_busy_ = true; |
| 363 | pass_to_tx(req_seq, f); |
| 364 | stop_retrans_timer(); |
| 365 | send_rr_or_rnr(Poll::NOT_SET, Final::POLL_RESPONSE); |
| 366 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 367 | CloseChannel(); |
| 368 | } |
| 369 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 370 | if (p == Poll::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 371 | remote_busy_ = true; |
| 372 | pass_to_tx(req_seq, f); |
| 373 | send_rr(Final::POLL_RESPONSE); |
| 374 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 375 | remote_busy_ = true; |
| 376 | pass_to_tx(req_seq, f); |
| 377 | send_rr(Final::NOT_SET); |
| 378 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 379 | CloseChannel(); |
| 380 | } |
| 381 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 382 | // SREJ NOT SUPPORTED |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void recv_srej(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 387 | if (rx_state_ == RxState::RECV) { |
| 388 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 389 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 390 | remote_busy_ = false; |
| 391 | pass_to_tx_f_bit(f); |
| 392 | retransmit_requested_i_frame(req_seq, p); |
| 393 | if (p_bit_outstanding()) { |
| 394 | srej_actioned_ = true; |
| 395 | srej_save_req_seq_ = req_seq; |
| 396 | } |
| 397 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 398 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 399 | remote_busy_ = false; |
| 400 | pass_to_tx_f_bit(f); |
| 401 | if (srej_actioned_ && srej_save_req_seq_ == req_seq) { |
| 402 | srej_actioned_ = false; |
| 403 | } else { |
| 404 | retransmit_requested_i_frame(req_seq, p); |
| 405 | } |
| 406 | } else if (p == Poll::POLL && with_valid_req_seq_retrans(req_seq) && |
| 407 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 408 | remote_busy_ = false; |
| 409 | pass_to_tx(req_seq, f); |
| 410 | retransmit_requested_i_frame(req_seq, p); |
| 411 | if (p_bit_outstanding()) { |
| 412 | srej_actioned_ = true; |
| 413 | srej_save_req_seq_ = req_seq; |
| 414 | } |
| 415 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 416 | CloseChannel(); |
| 417 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 418 | CloseChannel(); |
| 419 | } |
| 420 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 421 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 422 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 423 | remote_busy_ = false; |
| 424 | pass_to_tx_f_bit(f); |
| 425 | retransmit_requested_i_frame(req_seq, p); |
| 426 | if (p_bit_outstanding()) { |
| 427 | srej_actioned_ = true; |
| 428 | srej_save_req_seq_ = req_seq; |
| 429 | } |
| 430 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 431 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 432 | remote_busy_ = false; |
| 433 | pass_to_tx_f_bit(f); |
| 434 | if (srej_actioned_ && srej_save_req_seq_ == req_seq) { |
| 435 | srej_actioned_ = false; |
| 436 | } else { |
| 437 | retransmit_requested_i_frame(req_seq, p); |
| 438 | } |
| 439 | } else if (p == Poll::POLL && with_valid_req_seq_retrans(req_seq) && |
| 440 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 441 | remote_busy_ = false; |
| 442 | pass_to_tx(req_seq, f); |
| 443 | retransmit_requested_i_frame(req_seq, p); |
| 444 | send_pending_i_frames(); |
| 445 | if (p_bit_outstanding()) { |
| 446 | srej_actioned_ = true; |
| 447 | srej_save_req_seq_ = req_seq; |
| 448 | } |
| 449 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 450 | CloseChannel(); |
| 451 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 452 | CloseChannel(); |
| 453 | } |
| 454 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 455 | // SREJ NOT SUPPORTED |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Conditions (@see 8.6.5.5) |
| 460 | bool remote_busy() { |
| 461 | return remote_busy_; |
| 462 | } |
| 463 | |
| 464 | bool local_busy() { |
| 465 | return local_busy_; |
| 466 | } |
| 467 | |
| 468 | bool rem_window_not_full() { |
| 469 | return unacked_frames_ < controller_->remote_tx_window_; |
| 470 | } |
| 471 | |
| 472 | bool rem_window_full() { |
| 473 | return unacked_frames_ == controller_->remote_tx_window_; |
| 474 | } |
| 475 | |
| 476 | bool rnr_sent() { |
| 477 | return rnr_sent_; |
| 478 | } |
| 479 | |
| 480 | bool retry_i_frames_less_than_max_transmit(uint8_t req_seq) { |
| 481 | return retry_i_frames_[req_seq] < controller_->local_max_transmit_; |
| 482 | } |
| 483 | |
| 484 | bool retry_count_less_than_max_transmit() { |
| 485 | return retry_count_ < controller_->local_max_transmit_; |
| 486 | } |
| 487 | |
| 488 | bool with_expected_tx_seq(uint8_t tx_seq) { |
| 489 | return tx_seq == expected_tx_seq_; |
| 490 | } |
| 491 | |
| 492 | bool with_valid_req_seq(uint8_t req_seq) { |
Hansong Zhang | ba61624 | 2019-11-20 21:30:39 -0800 | [diff] [blame] | 493 | return expected_ack_seq_ <= req_seq && req_seq <= next_tx_seq_; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | bool with_valid_req_seq_retrans(uint8_t req_seq) { |
Hansong Zhang | ba61624 | 2019-11-20 21:30:39 -0800 | [diff] [blame] | 497 | return expected_ack_seq_ <= req_seq && req_seq <= next_tx_seq_; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | bool with_valid_f_bit(Final f) { |
| 501 | return f == Final::NOT_SET || tx_state_ == TxState::WAIT_F; |
| 502 | } |
| 503 | |
| 504 | bool with_unexpected_tx_seq(uint8_t tx_seq) { |
| 505 | return tx_seq > expected_tx_seq_ && tx_seq <= expected_tx_seq_ + controller_->local_tx_window_; |
| 506 | } |
| 507 | |
| 508 | bool with_duplicate_tx_seq(uint8_t tx_seq) { |
| 509 | return tx_seq < expected_tx_seq_ && tx_seq >= expected_tx_seq_ - controller_->local_tx_window_; |
| 510 | } |
| 511 | |
| 512 | bool with_invalid_tx_seq(uint8_t tx_seq) { |
| 513 | return tx_seq < expected_tx_seq_ - controller_->local_tx_window_ || |
| 514 | tx_seq > expected_tx_seq_ + controller_->local_tx_window_; |
| 515 | } |
| 516 | |
| 517 | bool with_invalid_req_seq(uint8_t req_seq) { |
| 518 | return req_seq < expected_ack_seq_ || req_seq >= next_tx_seq_; |
| 519 | } |
| 520 | |
| 521 | bool with_invalid_req_seq_retrans(uint8_t req_seq) { |
| 522 | return req_seq < expected_ack_seq_ || req_seq >= next_tx_seq_; |
| 523 | } |
| 524 | |
| 525 | bool not_with_expected_tx_seq(uint8_t tx_seq) { |
| 526 | return !with_invalid_tx_seq(tx_seq) && !with_expected_tx_seq(tx_seq); |
| 527 | } |
| 528 | |
| 529 | bool with_expected_tx_seq_srej() { |
| 530 | // We don't support sending SREJ |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | bool send_req_is_true() { |
| 535 | // We don't support sending SREJ |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | bool srej_list_is_one() { |
| 540 | // We don't support sending SREJ |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | bool with_unexpected_tx_seq_srej() { |
| 545 | // We don't support sending SREJ |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | bool with_duplicate_tx_seq_srej() { |
| 550 | // We don't support sending SREJ |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | // Actions (@see 8.6.5.6) |
| 555 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 556 | void _send_i_frame(SegmentationAndReassembly sar, std::unique_ptr<CopyablePacketBuilder> segment, uint8_t req_seq, |
| 557 | uint8_t tx_seq, uint16_t sdu_size = 0, Final f = Final::NOT_SET) { |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 558 | std::unique_ptr<packet::BasePacketBuilder> builder; |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 559 | if (sar == SegmentationAndReassembly::START) { |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 560 | if (controller_->fcs_enabled_) { |
| 561 | builder = EnhancedInformationStartFrameWithFcsBuilder::Create(controller_->remote_cid_, tx_seq, f, req_seq, |
| 562 | sdu_size, std::move(segment)); |
| 563 | } else { |
| 564 | builder = EnhancedInformationStartFrameBuilder::Create(controller_->remote_cid_, tx_seq, f, req_seq, sdu_size, |
| 565 | std::move(segment)); |
| 566 | } |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 567 | } else { |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 568 | if (controller_->fcs_enabled_) { |
| 569 | builder = EnhancedInformationFrameWithFcsBuilder::Create(controller_->remote_cid_, tx_seq, f, req_seq, sar, |
| 570 | std::move(segment)); |
| 571 | } else { |
| 572 | builder = EnhancedInformationFrameBuilder::Create(controller_->remote_cid_, tx_seq, f, req_seq, sar, |
| 573 | std::move(segment)); |
| 574 | } |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 575 | } |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 576 | controller_->send_pdu(std::move(builder)); |
| 577 | } |
| 578 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 579 | void send_data(SegmentationAndReassembly sar, uint16_t sdu_size, std::unique_ptr<packet::RawBuilder> segment, |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 580 | Final f = Final::NOT_SET) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 581 | std::shared_ptr<packet::RawBuilder> shared_segment(segment.release()); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 582 | unacked_list_.emplace(std::piecewise_construct, std::forward_as_tuple(next_tx_seq_), |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 583 | std::forward_as_tuple(sar, sdu_size, shared_segment)); |
| 584 | |
| 585 | std::unique_ptr<CopyablePacketBuilder> copyable_packet_builder = |
| 586 | std::make_unique<CopyablePacketBuilder>(std::get<2>(unacked_list_.find(next_tx_seq_)->second)); |
| 587 | _send_i_frame(sar, std::move(copyable_packet_builder), buffer_seq_, next_tx_seq_, sdu_size, f); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 588 | unacked_frames_++; |
| 589 | frames_sent_++; |
| 590 | retry_i_frames_[next_tx_seq_] = 1; |
| 591 | next_tx_seq_ = (next_tx_seq_ + 1) % kMaxTxWin; |
| 592 | start_retrans_timer(); |
| 593 | } |
| 594 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 595 | void pend_data(SegmentationAndReassembly sar, uint16_t sdu_size, std::unique_ptr<packet::RawBuilder> data) { |
| 596 | pending_frames_.emplace(std::make_tuple(sar, sdu_size, std::move(data))); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void process_req_seq(uint8_t req_seq) { |
| 600 | for (int i = expected_ack_seq_; i < req_seq; i++) { |
| 601 | unacked_list_.erase(i); |
| 602 | retry_i_frames_[i] = 0; |
| 603 | } |
| 604 | unacked_frames_ -= ((req_seq - expected_ack_seq_) + kMaxTxWin) % kMaxTxWin; |
| 605 | if (unacked_frames_ == 0) { |
| 606 | stop_retrans_timer(); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | void _send_s_frame(SupervisoryFunction s, uint8_t req_seq, Poll p, Final f) { |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 611 | std::unique_ptr<packet::BasePacketBuilder> builder; |
| 612 | if (controller_->fcs_enabled_) { |
| 613 | builder = EnhancedSupervisoryFrameWithFcsBuilder::Create(controller_->remote_cid_, s, p, f, req_seq); |
| 614 | } else { |
| 615 | builder = EnhancedSupervisoryFrameBuilder::Create(controller_->remote_cid_, s, p, f, req_seq); |
| 616 | } |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 617 | controller_->send_pdu(std::move(builder)); |
| 618 | } |
| 619 | |
| 620 | void send_rr(Poll p) { |
| 621 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, expected_tx_seq_, p, Final::NOT_SET); |
| 622 | } |
| 623 | |
| 624 | void send_rr(Final f) { |
| 625 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, expected_tx_seq_, Poll::NOT_SET, f); |
| 626 | } |
| 627 | |
| 628 | void send_rnr(Poll p) { |
| 629 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, expected_tx_seq_, p, Final::NOT_SET); |
| 630 | } |
| 631 | |
| 632 | void send_rnr(Final f) { |
| 633 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, expected_tx_seq_, Poll::NOT_SET, f); |
| 634 | } |
| 635 | |
| 636 | void send_rej(Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 637 | _send_s_frame(SupervisoryFunction::REJECT, expected_tx_seq_, p, f); |
| 638 | } |
| 639 | |
| 640 | void send_rr_or_rnr(Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 641 | if (local_busy()) { |
| 642 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, buffer_seq_, p, f); |
| 643 | } else { |
| 644 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, buffer_seq_, p, f); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | void send_i_or_rr_or_rnr(Final f = Final::POLL_RESPONSE) { |
| 649 | auto frames_sent = 0; |
| 650 | if (local_busy()) { |
| 651 | send_rnr(Final::POLL_RESPONSE); |
| 652 | } |
| 653 | if (remote_busy() && unacked_frames_ > 0) { |
| 654 | start_retrans_timer(); |
| 655 | } |
| 656 | remote_busy_ = false; |
| 657 | send_pending_i_frames(f); // TODO: Only first has f = 1, other f = 0. Also increase frames_sent |
| 658 | if (!local_busy() && frames_sent == 0) { |
| 659 | send_rr(Final::POLL_RESPONSE); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | void send_srej() { |
| 664 | // Sending SREJ is not supported |
| 665 | } |
| 666 | |
| 667 | void start_retrans_timer() { |
| 668 | retrans_timer_.Schedule(common::BindOnce(&impl::retrans_timer_expires, common::Unretained(this)), |
| 669 | std::chrono::milliseconds(controller_->local_retransmit_timeout_ms_)); |
| 670 | } |
| 671 | |
| 672 | void start_monitor_timer() { |
| 673 | monitor_timer_.Schedule(common::BindOnce(&impl::monitor_timer_expires, common::Unretained(this)), |
| 674 | std::chrono::milliseconds(controller_->local_monitor_timeout_ms_)); |
| 675 | } |
| 676 | |
| 677 | void pass_to_tx(uint8_t req_seq, Final f) { |
| 678 | recv_req_seq_and_f_bit(req_seq, f); |
| 679 | } |
| 680 | |
| 681 | void pass_to_tx_f_bit(Final f) { |
| 682 | recv_f_bit(f); |
| 683 | } |
| 684 | |
| 685 | void data_indication(SegmentationAndReassembly sar, const packet::PacketView<true>& segment) { |
| 686 | controller_->stage_for_reassembly(sar, segment); |
| 687 | buffer_seq_ = (buffer_seq_ + 1) % kMaxTxWin; |
| 688 | } |
| 689 | |
| 690 | void increment_expected_tx_seq() { |
| 691 | expected_tx_seq_ = (expected_tx_seq_ + 1) % kMaxTxWin; |
| 692 | } |
| 693 | |
| 694 | void stop_retrans_timer() { |
| 695 | retrans_timer_.Cancel(); |
| 696 | } |
| 697 | |
| 698 | void stop_monitor_timer() { |
| 699 | monitor_timer_.Cancel(); |
| 700 | } |
| 701 | |
| 702 | void send_ack(Final f = Final::NOT_SET) { |
| 703 | if (local_busy()) { |
| 704 | send_rnr(f); |
| 705 | } else if (!remote_busy() && /* pending i frames exist */ rem_window_not_full()) { |
| 706 | send_pending_i_frames(f); |
| 707 | } else { |
| 708 | send_rr(f); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | void init_srej() { |
| 713 | // We don't support sending SREJ |
| 714 | } |
| 715 | |
| 716 | void save_i_frame_srej() { |
| 717 | // We don't support sending SREJ |
| 718 | } |
| 719 | |
| 720 | void store_or_ignore() { |
| 721 | // We choose to ignore. We don't support local busy so far. |
| 722 | } |
| 723 | |
| 724 | bool p_bit_outstanding() { |
| 725 | return tx_state_ == TxState::WAIT_F; |
| 726 | } |
| 727 | |
| 728 | void retransmit_i_frames(uint8_t req_seq, Poll p = Poll::NOT_SET) { |
| 729 | uint8_t i = req_seq; |
| 730 | Final f = (p == Poll::NOT_SET ? Final::NOT_SET : Final::POLL_RESPONSE); |
| 731 | while (unacked_list_.find(i) == unacked_list_.end()) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 732 | std::unique_ptr<CopyablePacketBuilder> copyable_packet_builder = |
| 733 | std::make_unique<CopyablePacketBuilder>(std::get<2>(unacked_list_.find(i)->second)); |
| 734 | _send_i_frame(std::get<0>(unacked_list_.find(i)->second), std::move(copyable_packet_builder), buffer_seq_, i, |
| 735 | std::get<1>(unacked_list_.find(i)->second), f); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 736 | retry_i_frames_[i]++; |
| 737 | if (retry_i_frames_[i] == controller_->local_max_transmit_) { |
| 738 | CloseChannel(); |
| 739 | } |
| 740 | frames_sent_++; |
| 741 | f = Final::NOT_SET; |
| 742 | } |
| 743 | start_retrans_timer(); |
| 744 | } |
| 745 | |
| 746 | void retransmit_requested_i_frame(uint8_t req_seq, Poll p) { |
| 747 | Final f = p == Poll::POLL ? Final::POLL_RESPONSE : Final::NOT_SET; |
| 748 | if (unacked_list_.find(req_seq) == unacked_list_.end()) { |
| 749 | LOG_ERROR("Received invalid SREJ"); |
| 750 | return; |
| 751 | } |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 752 | std::unique_ptr<CopyablePacketBuilder> copyable_packet_builder = |
| 753 | std::make_unique<CopyablePacketBuilder>(std::get<2>(unacked_list_.find(req_seq)->second)); |
| 754 | _send_i_frame(std::get<0>(unacked_list_.find(req_seq)->second), std::move(copyable_packet_builder), buffer_seq_, |
| 755 | req_seq, std::get<1>(unacked_list_.find(req_seq)->second), f); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 756 | retry_i_frames_[req_seq]++; |
| 757 | start_retrans_timer(); |
| 758 | } |
| 759 | |
| 760 | void send_pending_i_frames(Final f = Final::NOT_SET) { |
| 761 | if (p_bit_outstanding()) { |
| 762 | return; |
| 763 | } |
| 764 | while (rem_window_not_full() && !pending_frames_.empty()) { |
| 765 | auto& frame = pending_frames_.front(); |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 766 | send_data(std::get<0>(frame), std::get<1>(frame), std::move(std::get<2>(frame)), f); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 767 | pending_frames_.pop(); |
| 768 | f = Final::NOT_SET; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | void CloseChannel() { |
| 773 | // TODO: Needs a reference to signaller |
| 774 | } |
| 775 | |
| 776 | void pop_srej_list() { |
| 777 | // We don't support sending SREJ |
| 778 | } |
| 779 | |
| 780 | void data_indication_srej() { |
| 781 | // We don't support sending SREJ |
| 782 | } |
| 783 | }; |
| 784 | |
| 785 | // Segmentation is handled here |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 786 | void ErtmController::OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 787 | auto sdu_size = sdu->size(); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 788 | std::vector<std::unique_ptr<packet::RawBuilder>> segments; |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 789 | packet::FragmentingInserter fragmenting_inserter(size_each_packet_, std::back_insert_iterator(segments)); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 790 | sdu->Serialize(fragmenting_inserter); |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 791 | fragmenting_inserter.finalize(); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 792 | if (segments.size() == 1) { |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 793 | pimpl_->data_request(SegmentationAndReassembly::UNSEGMENTED, std::move(segments[0])); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 794 | return; |
| 795 | } |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 796 | pimpl_->data_request(SegmentationAndReassembly::START, std::move(segments[0]), sdu_size); |
| 797 | for (auto i = 1; i < segments.size() - 1; i++) { |
| 798 | pimpl_->data_request(SegmentationAndReassembly::CONTINUATION, std::move(segments[i])); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 799 | } |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 800 | pimpl_->data_request(SegmentationAndReassembly::END, std::move(segments.back())); |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 803 | void ErtmController::OnPdu(packet::PacketView<true> pdu) { |
| 804 | if (fcs_enabled_) { |
| 805 | on_pdu_fcs(pdu); |
| 806 | } else { |
| 807 | on_pdu_no_fcs(pdu); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | void ErtmController::on_pdu_no_fcs(const packet::PacketView<true>& pdu) { |
| 812 | auto basic_frame_view = BasicFrameView::Create(pdu); |
| 813 | if (!basic_frame_view.IsValid()) { |
| 814 | return; |
| 815 | } |
| 816 | auto standard_frame_view = StandardFrameView::Create(basic_frame_view); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 817 | if (!standard_frame_view.IsValid()) { |
| 818 | LOG_WARN("Received invalid frame"); |
| 819 | return; |
| 820 | } |
| 821 | auto type = standard_frame_view.GetFrameType(); |
| 822 | if (type == FrameType::I_FRAME) { |
| 823 | auto i_frame_view = EnhancedInformationFrameView::Create(standard_frame_view); |
| 824 | if (!i_frame_view.IsValid()) { |
| 825 | LOG_WARN("Received invalid frame"); |
| 826 | return; |
| 827 | } |
| 828 | pimpl_->recv_i_frame(i_frame_view.GetF(), i_frame_view.GetTxSeq(), i_frame_view.GetReqSeq(), i_frame_view.GetSar(), |
| 829 | i_frame_view.GetPayload()); |
| 830 | } else if (type == FrameType::S_FRAME) { |
| 831 | auto s_frame_view = EnhancedSupervisoryFrameView::Create(standard_frame_view); |
| 832 | if (!s_frame_view.IsValid()) { |
| 833 | LOG_WARN("Received invalid frame"); |
| 834 | return; |
| 835 | } |
| 836 | auto req_seq = s_frame_view.GetReqSeq(); |
| 837 | auto f = s_frame_view.GetF(); |
| 838 | auto p = s_frame_view.GetP(); |
| 839 | switch (s_frame_view.GetS()) { |
| 840 | case SupervisoryFunction::RECEIVER_READY: |
| 841 | pimpl_->recv_rr(req_seq, p, f); |
| 842 | break; |
| 843 | case SupervisoryFunction::RECEIVER_NOT_READY: |
| 844 | pimpl_->recv_rnr(req_seq, p, f); |
| 845 | break; |
| 846 | case SupervisoryFunction::REJECT: |
| 847 | pimpl_->recv_rej(req_seq, p, f); |
| 848 | break; |
| 849 | case SupervisoryFunction::SELECT_REJECT: |
| 850 | pimpl_->recv_srej(req_seq, p, f); |
| 851 | break; |
| 852 | } |
| 853 | } else { |
| 854 | LOG_WARN("Received invalid frame"); |
| 855 | } |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 858 | void ErtmController::on_pdu_fcs(const packet::PacketView<true>& pdu) { |
| 859 | auto basic_frame_view = BasicFrameWithFcsView::Create(pdu); |
| 860 | if (!basic_frame_view.IsValid()) { |
| 861 | return; |
| 862 | } |
| 863 | auto standard_frame_view = StandardFrameWithFcsView::Create(basic_frame_view); |
| 864 | if (!standard_frame_view.IsValid()) { |
| 865 | LOG_WARN("Received invalid frame"); |
| 866 | return; |
| 867 | } |
| 868 | auto type = standard_frame_view.GetFrameType(); |
| 869 | if (type == FrameType::I_FRAME) { |
| 870 | auto i_frame_view = EnhancedInformationFrameWithFcsView::Create(standard_frame_view); |
| 871 | if (!i_frame_view.IsValid()) { |
| 872 | LOG_WARN("Received invalid frame"); |
| 873 | return; |
| 874 | } |
| 875 | pimpl_->recv_i_frame(i_frame_view.GetF(), i_frame_view.GetTxSeq(), i_frame_view.GetReqSeq(), i_frame_view.GetSar(), |
| 876 | i_frame_view.GetPayload()); |
| 877 | } else if (type == FrameType::S_FRAME) { |
| 878 | auto s_frame_view = EnhancedSupervisoryFrameWithFcsView::Create(standard_frame_view); |
| 879 | if (!s_frame_view.IsValid()) { |
| 880 | LOG_WARN("Received invalid frame"); |
| 881 | return; |
| 882 | } |
| 883 | auto req_seq = s_frame_view.GetReqSeq(); |
| 884 | auto f = s_frame_view.GetF(); |
| 885 | auto p = s_frame_view.GetP(); |
| 886 | switch (s_frame_view.GetS()) { |
| 887 | case SupervisoryFunction::RECEIVER_READY: |
| 888 | pimpl_->recv_rr(req_seq, p, f); |
| 889 | break; |
| 890 | case SupervisoryFunction::RECEIVER_NOT_READY: |
| 891 | pimpl_->recv_rnr(req_seq, p, f); |
| 892 | break; |
| 893 | case SupervisoryFunction::REJECT: |
| 894 | pimpl_->recv_rej(req_seq, p, f); |
| 895 | break; |
| 896 | case SupervisoryFunction::SELECT_REJECT: |
| 897 | pimpl_->recv_srej(req_seq, p, f); |
| 898 | break; |
| 899 | } |
| 900 | } else { |
| 901 | LOG_WARN("Received invalid frame"); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | std::unique_ptr<packet::BasePacketBuilder> ErtmController::GetNextPacket() { |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 906 | auto next = std::move(pdu_queue_.front()); |
| 907 | pdu_queue_.pop(); |
| 908 | return next; |
| 909 | } |
| 910 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 911 | void ErtmController::stage_for_reassembly(SegmentationAndReassembly sar, |
| 912 | const packet::PacketView<kLittleEndian>& payload) { |
| 913 | switch (sar) { |
| 914 | case SegmentationAndReassembly::UNSEGMENTED: |
| 915 | enqueue_buffer_.Enqueue(std::make_unique<packet::PacketView<kLittleEndian>>(payload), handler_); |
| 916 | break; |
| 917 | case SegmentationAndReassembly::START: |
| 918 | if (sar_state_ != SegmentationAndReassembly::END) { |
| 919 | LOG_WARN("Received invalid SAR"); |
| 920 | close_channel(); |
| 921 | return; |
| 922 | } |
| 923 | sar_state_ = SegmentationAndReassembly::START; |
| 924 | reassembly_stage_ = payload; |
| 925 | break; |
| 926 | case SegmentationAndReassembly::CONTINUATION: |
| 927 | if (sar_state_ == SegmentationAndReassembly::END) { |
| 928 | LOG_WARN("Received invalid SAR"); |
| 929 | close_channel(); |
| 930 | return; |
| 931 | } |
| 932 | reassembly_stage_.AppendPacketView(payload); |
| 933 | break; |
| 934 | case SegmentationAndReassembly::END: |
| 935 | if (sar_state_ == SegmentationAndReassembly::END) { |
| 936 | LOG_WARN("Received invalid SAR"); |
| 937 | close_channel(); |
| 938 | return; |
| 939 | } |
| 940 | reassembly_stage_.AppendPacketView(payload); |
| 941 | enqueue_buffer_.Enqueue(std::make_unique<packet::PacketView<kLittleEndian>>(reassembly_stage_), handler_); |
| 942 | sar_state_ = SegmentationAndReassembly::END; |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | |
Hansong Zhang | 7394ae7 | 2019-11-21 10:57:04 -0800 | [diff] [blame] | 947 | void ErtmController::EnableFcs(bool enabled) { |
| 948 | fcs_enabled_ = enabled; |
| 949 | } |
| 950 | |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 951 | void ErtmController::send_pdu(std::unique_ptr<packet::BasePacketBuilder> pdu) { |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 952 | pdu_queue_.emplace(std::move(pdu)); |
| 953 | scheduler_->OnPacketsReady(cid_, 1); |
| 954 | } |
| 955 | |
Hansong Zhang | 7394ae7 | 2019-11-21 10:57:04 -0800 | [diff] [blame] | 956 | void ErtmController::SetRetransmissionAndFlowControlOptions( |
| 957 | const RetransmissionAndFlowControlConfigurationOption& option) { |
| 958 | local_tx_window_ = option.tx_window_size_; |
| 959 | local_max_transmit_ = option.max_transmit_; |
| 960 | local_retransmit_timeout_ms_ = option.retransmission_time_out_; |
| 961 | local_monitor_timeout_ms_ = option.monitor_time_out_; |
| 962 | } |
| 963 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 964 | void ErtmController::close_channel() { |
| 965 | // TODO: Get a reference to signalling manager |
| 966 | } |
| 967 | |
| 968 | size_t ErtmController::CopyablePacketBuilder::size() const { |
| 969 | return builder_->size(); |
| 970 | } |
| 971 | |
| 972 | void ErtmController::CopyablePacketBuilder::Serialize(BitInserter& it) const { |
| 973 | builder_->Serialize(it); |
| 974 | } |
| 975 | |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 976 | } // namespace internal |
| 977 | } // namespace l2cap |
| 978 | } // namespace bluetooth |