1 /*
2  * Copyright 2020 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 #pragma once
18 
19 #include <cstddef>
20 #include <deque>
21 #include <iterator>
22 #include <memory>
23 #include <mutex>
24 #include <vector>
25 
26 namespace bluetooth {
27 namespace common {
28 
29 template <typename T>
30 class CircularBuffer {
31  public:
32   explicit CircularBuffer(size_t size);
33 
34   // Push one item to the circular buffer
35   void Push(T item);
36   // Take a snapshot of the circular buffer and return it as a vector
37   std::vector<T> Pull() const;
38   // Drain everything from the circular buffer and return them as a vector
39   std::vector<T> Drain();
40 
41  private:
42   const size_t size_;
43   std::deque<T> queue_;
44   mutable std::mutex mutex_;
45 };
46 
47 class Timestamper {
48  public:
49   virtual long long GetTimestamp() const = 0;
~Timestamper()50   virtual ~Timestamper() {}
51 };
52 
53 class TimestamperInMilliseconds : public Timestamper {
54  public:
GetTimestamp()55   long long GetTimestamp() const override {
56     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
57         .count();
58   }
~TimestamperInMilliseconds()59   virtual ~TimestamperInMilliseconds() {}
60 };
61 
62 template <typename T>
63 struct TimestampedEntry {
64   long long timestamp;
65   T entry;
66 };
67 
68 template <typename T>
69 class TimestampedCircularBuffer : public CircularBuffer<TimestampedEntry<T>> {
70  public:
71   explicit TimestampedCircularBuffer(
72       size_t size, std::unique_ptr<Timestamper> timestamper = std::make_unique<TimestamperInMilliseconds>());
73 
74   void Push(T item);
75   std::vector<TimestampedEntry<T>> Pull() const;
76   std::vector<TimestampedEntry<T>> Drain();
77 
78  private:
79   std::unique_ptr<Timestamper> timestamper_{std::make_unique<TimestamperInMilliseconds>()};
80 };
81 
82 }  // namespace common
83 }  // namespace bluetooth
84 
85 template <typename T>
CircularBuffer(size_t size)86 bluetooth::common::CircularBuffer<T>::CircularBuffer(size_t size) : size_(size) {}
87 
88 template <typename T>
Push(const T item)89 void bluetooth::common::CircularBuffer<T>::Push(const T item) {
90   std::unique_lock<std::mutex> lock(mutex_);
91   queue_.push_back(item);
92   while (queue_.size() > size_) {
93     queue_.pop_front();
94   }
95 }
96 
97 template <typename T>
Pull()98 std::vector<T> bluetooth::common::CircularBuffer<T>::Pull() const {
99   std::unique_lock<std::mutex> lock(mutex_);
100   return std::vector<T>(queue_.cbegin(), queue_.cend());
101 }
102 
103 template <typename T>
Drain()104 std::vector<T> bluetooth::common::CircularBuffer<T>::Drain() {
105   std::unique_lock<std::mutex> lock(mutex_);
106   std::vector<T> items(std::make_move_iterator(queue_.begin()), std::make_move_iterator(queue_.end()));
107   queue_.clear();
108   return items;
109 }
110 
111 template <typename T>
TimestampedCircularBuffer(size_t size,std::unique_ptr<Timestamper> timestamper)112 bluetooth::common::TimestampedCircularBuffer<T>::TimestampedCircularBuffer(
113     size_t size, std::unique_ptr<Timestamper> timestamper)
114     : CircularBuffer<TimestampedEntry<T>>(size), timestamper_(std::move(timestamper)) {}
115 
116 template <typename T>
Push(const T item)117 void bluetooth::common::TimestampedCircularBuffer<T>::Push(const T item) {
118   TimestampedEntry<T> timestamped_entry{timestamper_->GetTimestamp(), item};
119   bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Push(timestamped_entry);
120 }
121 
122 template <typename T>
Pull()123 std::vector<struct bluetooth::common::TimestampedEntry<T>> bluetooth::common::TimestampedCircularBuffer<T>::Pull()
124     const {
125   return bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Pull();
126 }
127 
128 template <typename T>
Drain()129 std::vector<struct bluetooth::common::TimestampedEntry<T>> bluetooth::common::TimestampedCircularBuffer<T>::Drain() {
130   return bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Drain();
131 }
132