1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CIRCLE_BUFFER_H_ 17 #define CIRCLE_BUFFER_H_ 18 19 #include <cstdint> 20 #include <chrono> 21 #include <condition_variable> 22 #include <map> 23 #include <mutex> 24 #include <new> 25 #include <securec.h> 26 #include <thread> 27 28 namespace Hdc { 29 constexpr uint64_t BUF_SIZE = 62464; // MAX_USBFFS_BULK 30 31 struct Data { 32 uint8_t *buf; 33 bool used; 34 std::chrono::steady_clock::time_point begin; 35 }; 36 37 class CircleBuffer { 38 public: 39 CircleBuffer(); 40 ~CircleBuffer(); 41 uint8_t *Malloc(); 42 void Free(const uint8_t *buf); 43 44 private: 45 std::mutex mutex_; 46 std::map<uint64_t, Data *> buffers_; 47 bool run_; 48 std::thread thread_; 49 std::mutex timerMutex_; 50 std::condition_variable timerCv_; 51 static void Timer(void *object); 52 void FreeMemory(); 53 void TimerNotify(); 54 void TimerSleep(); 55 void TimerStart(); 56 void TimerStop(); 57 }; 58 } 59 60 #endif 61 62