blob: 981bf7bce706359340b4635bd2d858e085d67986 [file] [log] [blame]
xshu5899e8e2018-01-09 15:36:03 -08001/*
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
xshu4cb33162018-01-24 15:40:06 -080017#include <android-base/logging.h>
18
xshu5899e8e2018-01-09 15:36:03 -080019#include "ringbuffer.h"
20
21namespace android {
22namespace hardware {
23namespace wifi {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080024namespace V1_6 {
xshu5899e8e2018-01-09 15:36:03 -080025namespace implementation {
26
27Ringbuffer::Ringbuffer(size_t maxSize) : size_(0), maxSize_(maxSize) {}
28
Sunil Ravi07ef1912022-05-17 18:01:06 -070029enum Ringbuffer::AppendStatus Ringbuffer::append(const std::vector<uint8_t>& input) {
xshu5899e8e2018-01-09 15:36:03 -080030 if (input.size() == 0) {
Sunil Ravi07ef1912022-05-17 18:01:06 -070031 return AppendStatus::FAIL_IP_BUFFER_ZERO;
xshu5899e8e2018-01-09 15:36:03 -080032 }
xshu4cb33162018-01-24 15:40:06 -080033 if (input.size() > maxSize_) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080034 LOG(INFO) << "Oversized message of " << input.size() << " bytes is dropped";
Sunil Ravi07ef1912022-05-17 18:01:06 -070035 return AppendStatus::FAIL_IP_BUFFER_EXCEEDED_MAXSIZE;
xshu4cb33162018-01-24 15:40:06 -080036 }
xshu5899e8e2018-01-09 15:36:03 -080037 data_.push_back(input);
38 size_ += input.size() * sizeof(input[0]);
39 while (size_ > maxSize_) {
Sunil Ravi07ef1912022-05-17 18:01:06 -070040 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 }
xshu5899e8e2018-01-09 15:36:03 -080045 size_ -= data_.front().size() * sizeof(data_.front()[0]);
46 data_.pop_front();
47 }
Sunil Ravi07ef1912022-05-17 18:01:06 -070048 return AppendStatus::SUCCESS;
xshu5899e8e2018-01-09 15:36:03 -080049}
50
51const std::list<std::vector<uint8_t>>& Ringbuffer::getData() const {
52 return data_;
53}
54
xshuc905ea62021-07-11 19:57:02 -070055void Ringbuffer::clear() {
56 data_.clear();
57 size_ = 0;
58}
59
xshu5899e8e2018-01-09 15:36:03 -080060} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080061} // namespace V1_6
xshu5899e8e2018-01-09 15:36:03 -080062} // namespace wifi
63} // namespace hardware
64} // namespace android