• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "ringbuffer.h"
18 
19 #include <android-base/logging.h>
20 
21 namespace aidl {
22 namespace android {
23 namespace hardware {
24 namespace wifi {
25 
Ringbuffer(size_t maxSize)26 Ringbuffer::Ringbuffer(size_t maxSize) : size_(0), maxSize_(maxSize) {}
27 
append(const std::vector<uint8_t> & input)28 enum Ringbuffer::AppendStatus Ringbuffer::append(const std::vector<uint8_t>& input) {
29     if (input.size() == 0) {
30         return AppendStatus::FAIL_IP_BUFFER_ZERO;
31     }
32     if (input.size() > maxSize_) {
33         LOG(INFO) << "Oversized message of " << input.size() << " bytes is dropped";
34         return AppendStatus::FAIL_IP_BUFFER_EXCEEDED_MAXSIZE;
35     }
36     data_.push_back(input);
37     size_ += input.size() * sizeof(input[0]);
38     while (size_ > maxSize_) {
39         if (data_.front().size() <= 0 || data_.front().size() > maxSize_) {
40             LOG(ERROR) << "First buffer in the ring buffer is Invalid. Size: "
41                        << data_.front().size();
42             return AppendStatus::FAIL_RING_BUFFER_CORRUPTED;
43         }
44         size_ -= data_.front().size() * sizeof(data_.front()[0]);
45         data_.pop_front();
46     }
47     return AppendStatus::SUCCESS;
48 }
49 
getData() const50 const std::list<std::vector<uint8_t>>& Ringbuffer::getData() const {
51     return data_;
52 }
53 
clear()54 void Ringbuffer::clear() {
55     data_.clear();
56     size_ = 0;
57 }
58 
59 }  // namespace wifi
60 }  // namespace hardware
61 }  // namespace android
62 }  // namespace aidl
63