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 |
| 79 | std::map<uint8_t, std::pair<SegmentationAndReassembly, CopyablePacketBuilder>> unacked_list_; |
| 80 | std::queue<std::pair<SegmentationAndReassembly, std::unique_ptr<packet::BasePacketBuilder>>> pending_frames_; |
| 81 | int retry_count_ = 0; |
| 82 | std::map<uint8_t /* tx_seq, */, int /* count */> retry_i_frames_; |
| 83 | bool rnr_sent_ = false; |
| 84 | bool rej_actioned_ = false; |
| 85 | bool srej_actioned_ = false; |
| 86 | uint16_t srej_save_req_seq_ = 0; |
| 87 | bool send_rej_ = false; |
| 88 | int buffer_seq_srej_ = 0; |
| 89 | int frames_sent_ = 0; |
| 90 | os::Alarm retrans_timer_; |
| 91 | os::Alarm monitor_timer_; |
| 92 | |
| 93 | // Events (@see 8.6.5.4) |
| 94 | |
| 95 | void data_request(SegmentationAndReassembly sar, std::unique_ptr<packet::BasePacketBuilder> pdu) { |
| 96 | if (tx_state_ == TxState::XMIT && !remote_busy() && rem_window_not_full()) { |
| 97 | send_data(sar, std::move(pdu)); |
| 98 | } else if (tx_state_ == TxState::XMIT && (remote_busy() || rem_window_full())) { |
| 99 | pend_data(sar, std::move(pdu)); |
| 100 | } else if (tx_state_ == TxState::WAIT_F) { |
| 101 | pend_data(sar, std::move(pdu)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void local_busy_detected() { |
| 106 | local_busy_ = true; |
| 107 | } |
| 108 | |
| 109 | void local_busy_clear() { |
| 110 | if (tx_state_ == TxState::XMIT && rnr_sent()) { |
| 111 | local_busy_ = false; |
| 112 | rnr_sent_ = false; |
| 113 | send_rr(Poll::POLL); |
| 114 | retry_count_ = 1; |
| 115 | stop_retrans_timer(); |
| 116 | start_monitor_timer(); |
| 117 | } else if (tx_state_ == TxState::XMIT) { |
| 118 | local_busy_ = false; |
| 119 | rnr_sent_ = false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void recv_req_seq_and_f_bit(uint8_t req_seq, Final f) { |
| 124 | if (tx_state_ == TxState::XMIT) { |
| 125 | process_req_seq(req_seq); |
| 126 | } else if (f == Final::POLL_RESPONSE) { |
| 127 | process_req_seq(req_seq); |
| 128 | stop_monitor_timer(); |
| 129 | if (unacked_frames_ > 0) { |
| 130 | start_retrans_timer(); |
| 131 | } |
| 132 | tx_state_ = TxState::XMIT; |
| 133 | } else { |
| 134 | process_req_seq(req_seq); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void recv_f_bit(Final f) { |
| 139 | if (tx_state_ == TxState::WAIT_F && f == Final::POLL_RESPONSE) { |
| 140 | stop_monitor_timer(); |
| 141 | if (unacked_frames_ > 0) { |
| 142 | start_retrans_timer(); |
| 143 | } |
| 144 | tx_state_ = TxState::XMIT; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void retrans_timer_expires() { |
| 149 | if (tx_state_ == TxState::XMIT) { |
| 150 | send_rr_or_rnr(Poll::POLL); |
| 151 | // send rr or rnr(p=1) |
| 152 | retry_count_ = 1; |
| 153 | start_retrans_timer(); |
| 154 | tx_state_ = TxState::WAIT_F; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void monitor_timer_expires() { |
| 159 | if (tx_state_ == TxState::WAIT_F && retry_count_less_than_max_transmit()) { |
| 160 | retry_count_++; |
| 161 | send_rr_or_rnr(Poll::POLL); |
| 162 | start_monitor_timer(); |
| 163 | } else if (tx_state_ == TxState::WAIT_F) { |
| 164 | CloseChannel(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void recv_i_frame(Final f, uint8_t tx_seq, uint8_t req_seq, SegmentationAndReassembly sar, |
| 169 | const packet::PacketView<true>& payload) { |
| 170 | if (rx_state_ == RxState::RECV) { |
| 171 | if (f == Final::NOT_SET && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && |
| 172 | !local_busy()) { |
| 173 | increment_expected_tx_seq(); |
| 174 | pass_to_tx(req_seq, f); |
| 175 | data_indication(sar, payload); |
| 176 | send_ack(Final::NOT_SET); |
| 177 | } else if (f == Final::POLL_RESPONSE && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && |
| 178 | with_valid_f_bit(f) && !local_busy()) { |
| 179 | increment_expected_tx_seq(); |
| 180 | pass_to_tx(req_seq, f); |
| 181 | data_indication(sar, payload); |
| 182 | if (!rej_actioned_) { |
| 183 | retransmit_i_frames(req_seq); |
| 184 | send_pending_i_frames(); |
| 185 | } else { |
| 186 | rej_actioned_ = false; |
| 187 | } |
| 188 | send_ack(Final::NOT_SET); |
| 189 | } else if (with_duplicate_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && !local_busy()) { |
| 190 | pass_to_tx(req_seq, f); |
| 191 | } else if (with_unexpected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && |
| 192 | !local_busy()) { |
| 193 | if constexpr (kSendSrej) { |
| 194 | // We don't support sending SREJ |
| 195 | } else { |
| 196 | pass_to_tx(req_seq, f); |
| 197 | send_rej(); |
| 198 | rx_state_ = RxState::REJ_SENT; |
| 199 | } |
| 200 | } else if (with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f) && local_busy()) { |
| 201 | pass_to_tx(req_seq, f); |
| 202 | store_or_ignore(); |
| 203 | } else if (with_valid_req_seq(req_seq) && not_with_expected_tx_seq(tx_seq) && with_valid_f_bit(f) && |
| 204 | local_busy()) { |
| 205 | pass_to_tx(req_seq, f); |
| 206 | } else if ((with_invalid_tx_seq(tx_seq) && controller_->local_tx_window_ > kMaxTxWin / 2) || |
| 207 | with_invalid_req_seq(req_seq)) { |
| 208 | CloseChannel(); |
| 209 | } else if (with_invalid_tx_seq(tx_seq) && controller_->local_tx_window_ <= kMaxTxWin / 2) { |
| 210 | // We decided to ignore |
| 211 | } |
| 212 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 213 | if (f == Final::NOT_SET && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 214 | increment_expected_tx_seq(); |
| 215 | pass_to_tx(req_seq, f); |
| 216 | data_indication(sar, payload); |
| 217 | send_ack(Final::NOT_SET); |
| 218 | rx_state_ = RxState::RECV; |
| 219 | } else if (f == Final::POLL_RESPONSE && with_expected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && |
| 220 | with_valid_f_bit(f)) { |
| 221 | increment_expected_tx_seq(); |
| 222 | pass_to_tx(req_seq, f); |
| 223 | data_indication(sar, payload); |
| 224 | if (!rej_actioned_) { |
| 225 | retransmit_i_frames(req_seq); |
| 226 | send_pending_i_frames(); |
| 227 | } else { |
| 228 | rej_actioned_ = false; |
| 229 | } |
| 230 | send_ack(Final::NOT_SET); |
| 231 | rx_state_ = RxState::RECV; |
| 232 | } else if (with_unexpected_tx_seq(tx_seq) && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 233 | pass_to_tx(req_seq, f); |
| 234 | } |
| 235 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 236 | // SREJ NOT SUPPORTED |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void recv_rr(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 241 | if (rx_state_ == RxState::RECV) { |
| 242 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 243 | pass_to_tx(req_seq, f); |
| 244 | if (remote_busy() && unacked_frames_ > 0) { |
| 245 | start_retrans_timer(); |
| 246 | } |
| 247 | remote_busy_ = false; |
| 248 | send_pending_i_frames(); |
| 249 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 250 | remote_busy_ = false; |
| 251 | pass_to_tx(req_seq, f); |
| 252 | if (!rej_actioned_) { |
| 253 | retransmit_i_frames(req_seq, p); |
| 254 | } else { |
| 255 | rej_actioned_ = false; |
| 256 | } |
| 257 | send_pending_i_frames(); |
| 258 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 259 | pass_to_tx(req_seq, f); |
| 260 | send_i_or_rr_or_rnr(Final::POLL_RESPONSE); |
| 261 | } else if (with_invalid_req_seq(req_seq)) { |
| 262 | CloseChannel(); |
| 263 | } |
| 264 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 265 | if (f == Final::POLL_RESPONSE && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 266 | remote_busy_ = false; |
| 267 | pass_to_tx(req_seq, f); |
| 268 | if (!rej_actioned_) { |
| 269 | retransmit_i_frames(req_seq, p); |
| 270 | } else { |
| 271 | rej_actioned_ = false; |
| 272 | } |
| 273 | send_pending_i_frames(); |
| 274 | } else if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 275 | pass_to_tx(req_seq, f); |
| 276 | if (remote_busy() and unacked_frames_ > 0) { |
| 277 | start_retrans_timer(); |
| 278 | } |
| 279 | remote_busy_ = false; |
| 280 | send_ack(Final::NOT_SET); |
| 281 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 282 | pass_to_tx(req_seq, f); |
| 283 | if (remote_busy() and unacked_frames_ > 0) { |
| 284 | start_retrans_timer(); |
| 285 | } |
| 286 | remote_busy_ = false; |
| 287 | send_rr(Final::POLL_RESPONSE); |
| 288 | } else if (with_invalid_req_seq(req_seq)) { |
| 289 | CloseChannel(); |
| 290 | } |
| 291 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 292 | // SREJ NOT SUPPORTED |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void recv_rej(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 297 | if (rx_state_ == RxState::RECV) { |
| 298 | if (f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 299 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 300 | remote_busy_ = false; |
| 301 | pass_to_tx(req_seq, f); |
| 302 | retransmit_i_frames(req_seq, p); |
| 303 | send_pending_i_frames(); |
| 304 | if (p_bit_outstanding()) { |
| 305 | rej_actioned_ = true; |
| 306 | } |
| 307 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 308 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 309 | remote_busy_ = false; |
| 310 | pass_to_tx(req_seq, f); |
| 311 | if (!rej_actioned_) { |
| 312 | retransmit_i_frames(req_seq, p); |
| 313 | } else { |
| 314 | rej_actioned_ = false; |
| 315 | } |
| 316 | send_pending_i_frames(); |
| 317 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 318 | CloseChannel(); |
| 319 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 320 | CloseChannel(); |
| 321 | } |
| 322 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 323 | if (f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 324 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 325 | remote_busy_ = false; |
| 326 | pass_to_tx(req_seq, f); |
| 327 | retransmit_i_frames(req_seq, p); |
| 328 | send_pending_i_frames(); |
| 329 | if (p_bit_outstanding()) { |
| 330 | rej_actioned_ = true; |
| 331 | } |
| 332 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 333 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 334 | remote_busy_ = false; |
| 335 | pass_to_tx(req_seq, f); |
| 336 | if (!rej_actioned_) { |
| 337 | retransmit_i_frames(req_seq, p); |
| 338 | } else { |
| 339 | rej_actioned_ = false; |
| 340 | } |
| 341 | send_pending_i_frames(); |
| 342 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 343 | CloseChannel(); |
| 344 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 345 | CloseChannel(); |
| 346 | } |
| 347 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 348 | // SREJ NOT SUPPORTED |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void recv_rnr(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 353 | if (rx_state_ == RxState::RECV) { |
| 354 | if (p == Poll::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 355 | remote_busy_ = true; |
| 356 | pass_to_tx(req_seq, f); |
| 357 | stop_retrans_timer(); |
| 358 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 359 | remote_busy_ = true; |
| 360 | pass_to_tx(req_seq, f); |
| 361 | stop_retrans_timer(); |
| 362 | send_rr_or_rnr(Poll::NOT_SET, Final::POLL_RESPONSE); |
| 363 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 364 | CloseChannel(); |
| 365 | } |
| 366 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 367 | if (p == Poll::NOT_SET && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 368 | remote_busy_ = true; |
| 369 | pass_to_tx(req_seq, f); |
| 370 | send_rr(Final::POLL_RESPONSE); |
| 371 | } else if (p == Poll::POLL && with_valid_req_seq(req_seq) && with_valid_f_bit(f)) { |
| 372 | remote_busy_ = true; |
| 373 | pass_to_tx(req_seq, f); |
| 374 | send_rr(Final::NOT_SET); |
| 375 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 376 | CloseChannel(); |
| 377 | } |
| 378 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 379 | // SREJ NOT SUPPORTED |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void recv_srej(uint8_t req_seq, Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 384 | if (rx_state_ == RxState::RECV) { |
| 385 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 386 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 387 | remote_busy_ = false; |
| 388 | pass_to_tx_f_bit(f); |
| 389 | retransmit_requested_i_frame(req_seq, p); |
| 390 | if (p_bit_outstanding()) { |
| 391 | srej_actioned_ = true; |
| 392 | srej_save_req_seq_ = req_seq; |
| 393 | } |
| 394 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 395 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 396 | remote_busy_ = false; |
| 397 | pass_to_tx_f_bit(f); |
| 398 | if (srej_actioned_ && srej_save_req_seq_ == req_seq) { |
| 399 | srej_actioned_ = false; |
| 400 | } else { |
| 401 | retransmit_requested_i_frame(req_seq, p); |
| 402 | } |
| 403 | } else if (p == Poll::POLL && with_valid_req_seq_retrans(req_seq) && |
| 404 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 405 | remote_busy_ = false; |
| 406 | pass_to_tx(req_seq, f); |
| 407 | retransmit_requested_i_frame(req_seq, p); |
| 408 | if (p_bit_outstanding()) { |
| 409 | srej_actioned_ = true; |
| 410 | srej_save_req_seq_ = req_seq; |
| 411 | } |
| 412 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 413 | CloseChannel(); |
| 414 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 415 | CloseChannel(); |
| 416 | } |
| 417 | } else if (rx_state_ == RxState::REJ_SENT) { |
| 418 | if (p == Poll::NOT_SET && f == Final::NOT_SET && with_valid_req_seq_retrans(req_seq) && |
| 419 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 420 | remote_busy_ = false; |
| 421 | pass_to_tx_f_bit(f); |
| 422 | retransmit_requested_i_frame(req_seq, p); |
| 423 | if (p_bit_outstanding()) { |
| 424 | srej_actioned_ = true; |
| 425 | srej_save_req_seq_ = req_seq; |
| 426 | } |
| 427 | } else if (f == Final::POLL_RESPONSE && with_valid_req_seq_retrans(req_seq) && |
| 428 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 429 | remote_busy_ = false; |
| 430 | pass_to_tx_f_bit(f); |
| 431 | if (srej_actioned_ && srej_save_req_seq_ == req_seq) { |
| 432 | srej_actioned_ = false; |
| 433 | } else { |
| 434 | retransmit_requested_i_frame(req_seq, p); |
| 435 | } |
| 436 | } else if (p == Poll::POLL && with_valid_req_seq_retrans(req_seq) && |
| 437 | retry_i_frames_less_than_max_transmit(req_seq) && with_valid_f_bit(f)) { |
| 438 | remote_busy_ = false; |
| 439 | pass_to_tx(req_seq, f); |
| 440 | retransmit_requested_i_frame(req_seq, p); |
| 441 | send_pending_i_frames(); |
| 442 | if (p_bit_outstanding()) { |
| 443 | srej_actioned_ = true; |
| 444 | srej_save_req_seq_ = req_seq; |
| 445 | } |
| 446 | } else if (with_valid_req_seq_retrans(req_seq) && !retry_i_frames_less_than_max_transmit(req_seq)) { |
| 447 | CloseChannel(); |
| 448 | } else if (with_invalid_req_seq_retrans(req_seq)) { |
| 449 | CloseChannel(); |
| 450 | } |
| 451 | } else if (rx_state_ == RxState::SREJ_SENT) { |
| 452 | // SREJ NOT SUPPORTED |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // Conditions (@see 8.6.5.5) |
| 457 | bool remote_busy() { |
| 458 | return remote_busy_; |
| 459 | } |
| 460 | |
| 461 | bool local_busy() { |
| 462 | return local_busy_; |
| 463 | } |
| 464 | |
| 465 | bool rem_window_not_full() { |
| 466 | return unacked_frames_ < controller_->remote_tx_window_; |
| 467 | } |
| 468 | |
| 469 | bool rem_window_full() { |
| 470 | return unacked_frames_ == controller_->remote_tx_window_; |
| 471 | } |
| 472 | |
| 473 | bool rnr_sent() { |
| 474 | return rnr_sent_; |
| 475 | } |
| 476 | |
| 477 | bool retry_i_frames_less_than_max_transmit(uint8_t req_seq) { |
| 478 | return retry_i_frames_[req_seq] < controller_->local_max_transmit_; |
| 479 | } |
| 480 | |
| 481 | bool retry_count_less_than_max_transmit() { |
| 482 | return retry_count_ < controller_->local_max_transmit_; |
| 483 | } |
| 484 | |
| 485 | bool with_expected_tx_seq(uint8_t tx_seq) { |
| 486 | return tx_seq == expected_tx_seq_; |
| 487 | } |
| 488 | |
| 489 | bool with_valid_req_seq(uint8_t req_seq) { |
| 490 | return expected_ack_seq_ <= req_seq && req_seq < next_tx_seq_; |
| 491 | } |
| 492 | |
| 493 | bool with_valid_req_seq_retrans(uint8_t req_seq) { |
| 494 | return expected_ack_seq_ <= req_seq && req_seq < next_tx_seq_; |
| 495 | } |
| 496 | |
| 497 | bool with_valid_f_bit(Final f) { |
| 498 | return f == Final::NOT_SET || tx_state_ == TxState::WAIT_F; |
| 499 | } |
| 500 | |
| 501 | bool with_unexpected_tx_seq(uint8_t tx_seq) { |
| 502 | return tx_seq > expected_tx_seq_ && tx_seq <= expected_tx_seq_ + controller_->local_tx_window_; |
| 503 | } |
| 504 | |
| 505 | bool with_duplicate_tx_seq(uint8_t tx_seq) { |
| 506 | return tx_seq < expected_tx_seq_ && tx_seq >= expected_tx_seq_ - controller_->local_tx_window_; |
| 507 | } |
| 508 | |
| 509 | bool with_invalid_tx_seq(uint8_t tx_seq) { |
| 510 | return tx_seq < expected_tx_seq_ - controller_->local_tx_window_ || |
| 511 | tx_seq > expected_tx_seq_ + controller_->local_tx_window_; |
| 512 | } |
| 513 | |
| 514 | bool with_invalid_req_seq(uint8_t req_seq) { |
| 515 | return req_seq < expected_ack_seq_ || req_seq >= next_tx_seq_; |
| 516 | } |
| 517 | |
| 518 | bool with_invalid_req_seq_retrans(uint8_t req_seq) { |
| 519 | return req_seq < expected_ack_seq_ || req_seq >= next_tx_seq_; |
| 520 | } |
| 521 | |
| 522 | bool not_with_expected_tx_seq(uint8_t tx_seq) { |
| 523 | return !with_invalid_tx_seq(tx_seq) && !with_expected_tx_seq(tx_seq); |
| 524 | } |
| 525 | |
| 526 | bool with_expected_tx_seq_srej() { |
| 527 | // We don't support sending SREJ |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | bool send_req_is_true() { |
| 532 | // We don't support sending SREJ |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | bool srej_list_is_one() { |
| 537 | // We don't support sending SREJ |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | bool with_unexpected_tx_seq_srej() { |
| 542 | // We don't support sending SREJ |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool with_duplicate_tx_seq_srej() { |
| 547 | // We don't support sending SREJ |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | // Actions (@see 8.6.5.6) |
| 552 | |
| 553 | void _send_i_frame(SegmentationAndReassembly sar, std::unique_ptr<packet::BasePacketBuilder> segment, uint8_t req_seq, |
| 554 | uint8_t tx_seq, Final f = Final::NOT_SET) { |
| 555 | auto builder = |
| 556 | ExtendedInformationFrameBuilder::Create(controller_->remote_cid_, f, req_seq, sar, tx_seq, std::move(segment)); |
| 557 | controller_->send_pdu(std::move(builder)); |
| 558 | } |
| 559 | |
| 560 | void send_data(SegmentationAndReassembly sar, std::unique_ptr<packet::BasePacketBuilder> segment, |
| 561 | Final f = Final::NOT_SET) { |
| 562 | unacked_list_.emplace(std::piecewise_construct, std::forward_as_tuple(next_tx_seq_), |
| 563 | std::forward_as_tuple(sar, std::move(segment))); |
| 564 | _send_i_frame(sar, unacked_list_.find(next_tx_seq_)->second.second.Create(), buffer_seq_, next_tx_seq_, f); |
| 565 | // TODO hsz fix me |
| 566 | unacked_frames_++; |
| 567 | frames_sent_++; |
| 568 | retry_i_frames_[next_tx_seq_] = 1; |
| 569 | next_tx_seq_ = (next_tx_seq_ + 1) % kMaxTxWin; |
| 570 | start_retrans_timer(); |
| 571 | } |
| 572 | |
| 573 | void pend_data(SegmentationAndReassembly sar, std::unique_ptr<packet::BasePacketBuilder> data) { |
| 574 | pending_frames_.emplace(std::make_pair(sar, std::move(data))); |
| 575 | } |
| 576 | |
| 577 | void process_req_seq(uint8_t req_seq) { |
| 578 | for (int i = expected_ack_seq_; i < req_seq; i++) { |
| 579 | unacked_list_.erase(i); |
| 580 | retry_i_frames_[i] = 0; |
| 581 | } |
| 582 | unacked_frames_ -= ((req_seq - expected_ack_seq_) + kMaxTxWin) % kMaxTxWin; |
| 583 | if (unacked_frames_ == 0) { |
| 584 | stop_retrans_timer(); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void _send_s_frame(SupervisoryFunction s, uint8_t req_seq, Poll p, Final f) { |
| 589 | auto builder = EnhancedSupervisoryFrameBuilder::Create(controller_->remote_cid_, s, p, f, req_seq); |
| 590 | controller_->send_pdu(std::move(builder)); |
| 591 | } |
| 592 | |
| 593 | void send_rr(Poll p) { |
| 594 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, expected_tx_seq_, p, Final::NOT_SET); |
| 595 | } |
| 596 | |
| 597 | void send_rr(Final f) { |
| 598 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, expected_tx_seq_, Poll::NOT_SET, f); |
| 599 | } |
| 600 | |
| 601 | void send_rnr(Poll p) { |
| 602 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, expected_tx_seq_, p, Final::NOT_SET); |
| 603 | } |
| 604 | |
| 605 | void send_rnr(Final f) { |
| 606 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, expected_tx_seq_, Poll::NOT_SET, f); |
| 607 | } |
| 608 | |
| 609 | void send_rej(Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 610 | _send_s_frame(SupervisoryFunction::REJECT, expected_tx_seq_, p, f); |
| 611 | } |
| 612 | |
| 613 | void send_rr_or_rnr(Poll p = Poll::NOT_SET, Final f = Final::NOT_SET) { |
| 614 | if (local_busy()) { |
| 615 | _send_s_frame(SupervisoryFunction::RECEIVER_NOT_READY, buffer_seq_, p, f); |
| 616 | } else { |
| 617 | _send_s_frame(SupervisoryFunction::RECEIVER_READY, buffer_seq_, p, f); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void send_i_or_rr_or_rnr(Final f = Final::POLL_RESPONSE) { |
| 622 | auto frames_sent = 0; |
| 623 | if (local_busy()) { |
| 624 | send_rnr(Final::POLL_RESPONSE); |
| 625 | } |
| 626 | if (remote_busy() && unacked_frames_ > 0) { |
| 627 | start_retrans_timer(); |
| 628 | } |
| 629 | remote_busy_ = false; |
| 630 | send_pending_i_frames(f); // TODO: Only first has f = 1, other f = 0. Also increase frames_sent |
| 631 | if (!local_busy() && frames_sent == 0) { |
| 632 | send_rr(Final::POLL_RESPONSE); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void send_srej() { |
| 637 | // Sending SREJ is not supported |
| 638 | } |
| 639 | |
| 640 | void start_retrans_timer() { |
| 641 | retrans_timer_.Schedule(common::BindOnce(&impl::retrans_timer_expires, common::Unretained(this)), |
| 642 | std::chrono::milliseconds(controller_->local_retransmit_timeout_ms_)); |
| 643 | } |
| 644 | |
| 645 | void start_monitor_timer() { |
| 646 | monitor_timer_.Schedule(common::BindOnce(&impl::monitor_timer_expires, common::Unretained(this)), |
| 647 | std::chrono::milliseconds(controller_->local_monitor_timeout_ms_)); |
| 648 | } |
| 649 | |
| 650 | void pass_to_tx(uint8_t req_seq, Final f) { |
| 651 | recv_req_seq_and_f_bit(req_seq, f); |
| 652 | } |
| 653 | |
| 654 | void pass_to_tx_f_bit(Final f) { |
| 655 | recv_f_bit(f); |
| 656 | } |
| 657 | |
| 658 | void data_indication(SegmentationAndReassembly sar, const packet::PacketView<true>& segment) { |
| 659 | controller_->stage_for_reassembly(sar, segment); |
| 660 | buffer_seq_ = (buffer_seq_ + 1) % kMaxTxWin; |
| 661 | } |
| 662 | |
| 663 | void increment_expected_tx_seq() { |
| 664 | expected_tx_seq_ = (expected_tx_seq_ + 1) % kMaxTxWin; |
| 665 | } |
| 666 | |
| 667 | void stop_retrans_timer() { |
| 668 | retrans_timer_.Cancel(); |
| 669 | } |
| 670 | |
| 671 | void stop_monitor_timer() { |
| 672 | monitor_timer_.Cancel(); |
| 673 | } |
| 674 | |
| 675 | void send_ack(Final f = Final::NOT_SET) { |
| 676 | if (local_busy()) { |
| 677 | send_rnr(f); |
| 678 | } else if (!remote_busy() && /* pending i frames exist */ rem_window_not_full()) { |
| 679 | send_pending_i_frames(f); |
| 680 | } else { |
| 681 | send_rr(f); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void init_srej() { |
| 686 | // We don't support sending SREJ |
| 687 | } |
| 688 | |
| 689 | void save_i_frame_srej() { |
| 690 | // We don't support sending SREJ |
| 691 | } |
| 692 | |
| 693 | void store_or_ignore() { |
| 694 | // We choose to ignore. We don't support local busy so far. |
| 695 | } |
| 696 | |
| 697 | bool p_bit_outstanding() { |
| 698 | return tx_state_ == TxState::WAIT_F; |
| 699 | } |
| 700 | |
| 701 | void retransmit_i_frames(uint8_t req_seq, Poll p = Poll::NOT_SET) { |
| 702 | uint8_t i = req_seq; |
| 703 | Final f = (p == Poll::NOT_SET ? Final::NOT_SET : Final::POLL_RESPONSE); |
| 704 | while (unacked_list_.find(i) == unacked_list_.end()) { |
| 705 | _send_i_frame(unacked_list_.find(i)->second.first, unacked_list_.find(i)->second.second.Create(), buffer_seq_, i, |
| 706 | f); |
| 707 | retry_i_frames_[i]++; |
| 708 | if (retry_i_frames_[i] == controller_->local_max_transmit_) { |
| 709 | CloseChannel(); |
| 710 | } |
| 711 | frames_sent_++; |
| 712 | f = Final::NOT_SET; |
| 713 | } |
| 714 | start_retrans_timer(); |
| 715 | } |
| 716 | |
| 717 | void retransmit_requested_i_frame(uint8_t req_seq, Poll p) { |
| 718 | Final f = p == Poll::POLL ? Final::POLL_RESPONSE : Final::NOT_SET; |
| 719 | if (unacked_list_.find(req_seq) == unacked_list_.end()) { |
| 720 | LOG_ERROR("Received invalid SREJ"); |
| 721 | return; |
| 722 | } |
| 723 | _send_i_frame(unacked_list_.find(req_seq)->second.first, unacked_list_.find(req_seq)->second.second.Create(), |
| 724 | buffer_seq_, req_seq, f); |
| 725 | retry_i_frames_[req_seq]++; |
| 726 | start_retrans_timer(); |
| 727 | } |
| 728 | |
| 729 | void send_pending_i_frames(Final f = Final::NOT_SET) { |
| 730 | if (p_bit_outstanding()) { |
| 731 | return; |
| 732 | } |
| 733 | while (rem_window_not_full() && !pending_frames_.empty()) { |
| 734 | auto& frame = pending_frames_.front(); |
| 735 | send_data(frame.first, std::move(frame.second), f); |
| 736 | pending_frames_.pop(); |
| 737 | f = Final::NOT_SET; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void CloseChannel() { |
| 742 | // TODO: Needs a reference to signaller |
| 743 | } |
| 744 | |
| 745 | void pop_srej_list() { |
| 746 | // We don't support sending SREJ |
| 747 | } |
| 748 | |
| 749 | void data_indication_srej() { |
| 750 | // We don't support sending SREJ |
| 751 | } |
| 752 | }; |
| 753 | |
| 754 | // Segmentation is handled here |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 755 | void ErtmController::OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) { |
| 756 | LOG_ERROR("Not implemented"); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame^] | 757 | // TODO: Optimize the calculation. We don't need to count for SDU length in CONTINUATION or END packets. We don't need |
| 758 | // to FCS when disabled. |
| 759 | auto size_each_packet = |
| 760 | (remote_mps_ - 4 /* basic L2CAP header */ - 2 /* SDU length */ - 2 /* Extended control */ - 2 /* FCS */); |
| 761 | std::vector<std::unique_ptr<packet::RawBuilder>> segments; |
| 762 | packet::FragmentingInserter fragmenting_inserter(size_each_packet, std::back_insert_iterator(segments)); |
| 763 | sdu->Serialize(fragmenting_inserter); |
| 764 | if (segments.size() == 1) { |
| 765 | pimpl_->data_request(SegmentationAndReassembly::UNSEGMENTED, std::move(sdu)); |
| 766 | return; |
| 767 | } |
| 768 | auto sar = SegmentationAndReassembly::START; |
| 769 | for (auto i = 0; i < segments.size() - 1; i++) { |
| 770 | pimpl_->data_request(sar, std::move(segments[i])); |
| 771 | sar = SegmentationAndReassembly::CONTINUATION; |
| 772 | } |
| 773 | sar = SegmentationAndReassembly::END; |
| 774 | pimpl_->data_request(sar, std::move(segments.back())); |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void ErtmController::OnPdu(BasicFrameView pdu) { |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame^] | 778 | auto standard_frame_view = StandardFrameView::Create(pdu); |
| 779 | if (!standard_frame_view.IsValid()) { |
| 780 | LOG_WARN("Received invalid frame"); |
| 781 | return; |
| 782 | } |
| 783 | auto type = standard_frame_view.GetFrameType(); |
| 784 | if (type == FrameType::I_FRAME) { |
| 785 | auto i_frame_view = EnhancedInformationFrameView::Create(standard_frame_view); |
| 786 | if (!i_frame_view.IsValid()) { |
| 787 | LOG_WARN("Received invalid frame"); |
| 788 | return; |
| 789 | } |
| 790 | pimpl_->recv_i_frame(i_frame_view.GetF(), i_frame_view.GetTxSeq(), i_frame_view.GetReqSeq(), i_frame_view.GetSar(), |
| 791 | i_frame_view.GetPayload()); |
| 792 | } else if (type == FrameType::S_FRAME) { |
| 793 | auto s_frame_view = EnhancedSupervisoryFrameView::Create(standard_frame_view); |
| 794 | if (!s_frame_view.IsValid()) { |
| 795 | LOG_WARN("Received invalid frame"); |
| 796 | return; |
| 797 | } |
| 798 | auto req_seq = s_frame_view.GetReqSeq(); |
| 799 | auto f = s_frame_view.GetF(); |
| 800 | auto p = s_frame_view.GetP(); |
| 801 | switch (s_frame_view.GetS()) { |
| 802 | case SupervisoryFunction::RECEIVER_READY: |
| 803 | pimpl_->recv_rr(req_seq, p, f); |
| 804 | break; |
| 805 | case SupervisoryFunction::RECEIVER_NOT_READY: |
| 806 | pimpl_->recv_rnr(req_seq, p, f); |
| 807 | break; |
| 808 | case SupervisoryFunction::REJECT: |
| 809 | pimpl_->recv_rej(req_seq, p, f); |
| 810 | break; |
| 811 | case SupervisoryFunction::SELECT_REJECT: |
| 812 | pimpl_->recv_srej(req_seq, p, f); |
| 813 | break; |
| 814 | } |
| 815 | } else { |
| 816 | LOG_WARN("Received invalid frame"); |
| 817 | } |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | std::unique_ptr<BasicFrameBuilder> ErtmController::GetNextPacket() { |
| 821 | auto next = std::move(pdu_queue_.front()); |
| 822 | pdu_queue_.pop(); |
| 823 | return next; |
| 824 | } |
| 825 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame^] | 826 | void ErtmController::stage_for_reassembly(SegmentationAndReassembly sar, |
| 827 | const packet::PacketView<kLittleEndian>& payload) { |
| 828 | switch (sar) { |
| 829 | case SegmentationAndReassembly::UNSEGMENTED: |
| 830 | enqueue_buffer_.Enqueue(std::make_unique<packet::PacketView<kLittleEndian>>(payload), handler_); |
| 831 | break; |
| 832 | case SegmentationAndReassembly::START: |
| 833 | if (sar_state_ != SegmentationAndReassembly::END) { |
| 834 | LOG_WARN("Received invalid SAR"); |
| 835 | close_channel(); |
| 836 | return; |
| 837 | } |
| 838 | sar_state_ = SegmentationAndReassembly::START; |
| 839 | reassembly_stage_ = payload; |
| 840 | break; |
| 841 | case SegmentationAndReassembly::CONTINUATION: |
| 842 | if (sar_state_ == SegmentationAndReassembly::END) { |
| 843 | LOG_WARN("Received invalid SAR"); |
| 844 | close_channel(); |
| 845 | return; |
| 846 | } |
| 847 | reassembly_stage_.AppendPacketView(payload); |
| 848 | break; |
| 849 | case SegmentationAndReassembly::END: |
| 850 | if (sar_state_ == SegmentationAndReassembly::END) { |
| 851 | LOG_WARN("Received invalid SAR"); |
| 852 | close_channel(); |
| 853 | return; |
| 854 | } |
| 855 | reassembly_stage_.AppendPacketView(payload); |
| 856 | enqueue_buffer_.Enqueue(std::make_unique<packet::PacketView<kLittleEndian>>(reassembly_stage_), handler_); |
| 857 | sar_state_ = SegmentationAndReassembly::END; |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | void ErtmController::send_pdu(std::unique_ptr<BasicFrameBuilder> pdu) { |
| 863 | pdu_queue_.emplace(std::move(pdu)); |
| 864 | scheduler_->OnPacketsReady(cid_, 1); |
| 865 | } |
| 866 | |
| 867 | void ErtmController::close_channel() { |
| 868 | // TODO: Get a reference to signalling manager |
| 869 | } |
| 870 | |
| 871 | size_t ErtmController::CopyablePacketBuilder::size() const { |
| 872 | return builder_->size(); |
| 873 | } |
| 874 | |
| 875 | void ErtmController::CopyablePacketBuilder::Serialize(BitInserter& it) const { |
| 876 | builder_->Serialize(it); |
| 877 | } |
| 878 | |
| 879 | std::unique_ptr<BasePacketBuilder> ErtmController::CopyablePacketBuilder::Create() { |
| 880 | return std::unique_ptr<packet::BasePacketBuilder>(builder_.get()); |
| 881 | } |
| 882 | |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 883 | } // namespace internal |
| 884 | } // namespace l2cap |
| 885 | } // namespace bluetooth |