blob: 9d08a7368eec3df4d25b9fd2fba7015f6e96ab7a [file] [log] [blame]
xshu5899e8e2018-01-09 15:36:03 -08001/*
Gabriel Birene58e2632022-07-15 23:25:39 +00002 * Copyright (C) 2022 The Android Open Source Project
xshu5899e8e2018-01-09 15:36:03 -08003 *
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
Gabriel Birene58e2632022-07-15 23:25:39 +000019#include <android-base/logging.h>
20
21namespace aidl {
xshu5899e8e2018-01-09 15:36:03 -080022namespace android {
23namespace hardware {
24namespace wifi {
xshu5899e8e2018-01-09 15:36:03 -080025
26Ringbuffer::Ringbuffer(size_t maxSize) : size_(0), maxSize_(maxSize) {}
27
Sunil Ravi07ef1912022-05-17 18:01:06 -070028enum Ringbuffer::AppendStatus Ringbuffer::append(const std::vector<uint8_t>& input) {
xshu5899e8e2018-01-09 15:36:03 -080029 if (input.size() == 0) {
Sunil Ravi07ef1912022-05-17 18:01:06 -070030 return AppendStatus::FAIL_IP_BUFFER_ZERO;
xshu5899e8e2018-01-09 15:36:03 -080031 }
xshu4cb33162018-01-24 15:40:06 -080032 if (input.size() > maxSize_) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080033 LOG(INFO) << "Oversized message of " << input.size() << " bytes is dropped";
Sunil Ravi07ef1912022-05-17 18:01:06 -070034 return AppendStatus::FAIL_IP_BUFFER_EXCEEDED_MAXSIZE;
xshu4cb33162018-01-24 15:40:06 -080035 }
xshu5899e8e2018-01-09 15:36:03 -080036 data_.push_back(input);
37 size_ += input.size() * sizeof(input[0]);
38 while (size_ > maxSize_) {
Sunil Ravi07ef1912022-05-17 18:01:06 -070039 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 }
xshu5899e8e2018-01-09 15:36:03 -080044 size_ -= data_.front().size() * sizeof(data_.front()[0]);
45 data_.pop_front();
46 }
Sunil Ravi07ef1912022-05-17 18:01:06 -070047 return AppendStatus::SUCCESS;
xshu5899e8e2018-01-09 15:36:03 -080048}
49
50const std::list<std::vector<uint8_t>>& Ringbuffer::getData() const {
51 return data_;
52}
53
xshuc905ea62021-07-11 19:57:02 -070054void Ringbuffer::clear() {
55 data_.clear();
56 size_ = 0;
57}
58
xshu5899e8e2018-01-09 15:36:03 -080059} // namespace wifi
60} // namespace hardware
61} // namespace android
Gabriel Birene58e2632022-07-15 23:25:39 +000062} // namespace aidl