xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
xshu | 4cb3316 | 2018-01-24 15:40:06 -0800 | [diff] [blame] | 17 | #include <android-base/logging.h> |
| 18 | |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 19 | #include "ringbuffer.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace wifi { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 24 | namespace V1_6 { |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 25 | namespace implementation { |
| 26 | |
| 27 | Ringbuffer::Ringbuffer(size_t maxSize) : size_(0), maxSize_(maxSize) {} |
| 28 | |
Sunil Ravi | 07ef191 | 2022-05-17 18:01:06 -0700 | [diff] [blame] | 29 | enum Ringbuffer::AppendStatus Ringbuffer::append(const std::vector<uint8_t>& input) { |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 30 | if (input.size() == 0) { |
Sunil Ravi | 07ef191 | 2022-05-17 18:01:06 -0700 | [diff] [blame] | 31 | return AppendStatus::FAIL_IP_BUFFER_ZERO; |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 32 | } |
xshu | 4cb3316 | 2018-01-24 15:40:06 -0800 | [diff] [blame] | 33 | if (input.size() > maxSize_) { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 34 | LOG(INFO) << "Oversized message of " << input.size() << " bytes is dropped"; |
Sunil Ravi | 07ef191 | 2022-05-17 18:01:06 -0700 | [diff] [blame] | 35 | return AppendStatus::FAIL_IP_BUFFER_EXCEEDED_MAXSIZE; |
xshu | 4cb3316 | 2018-01-24 15:40:06 -0800 | [diff] [blame] | 36 | } |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 37 | data_.push_back(input); |
| 38 | size_ += input.size() * sizeof(input[0]); |
| 39 | while (size_ > maxSize_) { |
Sunil Ravi | 07ef191 | 2022-05-17 18:01:06 -0700 | [diff] [blame] | 40 | if (data_.front().size() <= 0 || data_.front().size() > maxSize_) { |
| 41 | LOG(ERROR) << "First buffer in the ring buffer is Invalid. Size: " |
| 42 | << data_.front().size(); |
| 43 | return AppendStatus::FAIL_RING_BUFFER_CORRUPTED; |
| 44 | } |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 45 | size_ -= data_.front().size() * sizeof(data_.front()[0]); |
| 46 | data_.pop_front(); |
| 47 | } |
Sunil Ravi | 07ef191 | 2022-05-17 18:01:06 -0700 | [diff] [blame] | 48 | return AppendStatus::SUCCESS; |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | const std::list<std::vector<uint8_t>>& Ringbuffer::getData() const { |
| 52 | return data_; |
| 53 | } |
| 54 | |
xshu | c905ea6 | 2021-07-11 19:57:02 -0700 | [diff] [blame] | 55 | void Ringbuffer::clear() { |
| 56 | data_.clear(); |
| 57 | size_ = 0; |
| 58 | } |
| 59 | |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 60 | } // namespace implementation |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame] | 61 | } // namespace V1_6 |
xshu | 5899e8e | 2018-01-09 15:36:03 -0800 | [diff] [blame] | 62 | } // namespace wifi |
| 63 | } // namespace hardware |
| 64 | } // namespace android |