Add extracted tools: CitrineOS, OpenOCPP, ShapeShifter
- CitrineOS core extracted (CSMS OCPP 2.0.1) - OpenOCPP extracted (firmware OCPP 1.6J/2.0.1) - ShapeShifter library installed (pip install -e) - ShapeShifter specification extracted - EVerest extracted TODO updated with progress
This commit is contained in:
2
tools/EVerest-main/lib/everest/timer/.gitignore
vendored
Normal file
2
tools/EVerest-main/lib/everest/timer/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
build/
|
||||
dist/
|
||||
9
tools/EVerest-main/lib/everest/timer/BUILD.bazel
Normal file
9
tools/EVerest-main/lib/everest/timer/BUILD.bazel
Normal file
@@ -0,0 +1,9 @@
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "libtimer",
|
||||
hdrs = ["include/everest/timer.hpp"],
|
||||
deps = [],
|
||||
strip_include_prefix = "include",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
77
tools/EVerest-main/lib/everest/timer/CMakeLists.txt
Normal file
77
tools/EVerest-main/lib/everest/timer/CMakeLists.txt
Normal file
@@ -0,0 +1,77 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(everest-timer
|
||||
VERSION 0.1.4
|
||||
DESCRIPTION "EVerest timer library"
|
||||
LANGUAGES CXX C
|
||||
)
|
||||
|
||||
find_package(everest-cmake 0.1 REQUIRED
|
||||
PATHS ../everest-cmake
|
||||
)
|
||||
|
||||
# options
|
||||
option(${PROJECT_NAME}_BUILD_TESTING "Build unit tests, used if included as dependency" OFF)
|
||||
option(BUILD_TESTING "Build unit tests, used if standalone project" OFF)
|
||||
option(BUILD_EXAMPLES "Build examples" OFF)
|
||||
option(TIMER_INSTALL "Install the library (shared data might be installed anyway)" ${EVC_MAIN_PROJECT})
|
||||
option(CMAKE_RUN_CLANG_TIDY "Run clang-tidy" OFF)
|
||||
|
||||
if((${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME} OR ${PROJECT_NAME}_BUILD_TESTING) AND BUILD_TESTING)
|
||||
set(LIBTIMER_BUILD_TESTING ON)
|
||||
include(CTest)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
# this policy allows us to continue using the removed FindBoost module for now
|
||||
if(POLICY CMP0167)
|
||||
cmake_policy(SET CMP0167 OLD)
|
||||
endif()
|
||||
|
||||
# dependencies
|
||||
find_package(Boost)
|
||||
if(Boost_VERSION_STRING VERSION_LESS "1.69.0")
|
||||
find_package(Boost
|
||||
COMPONENTS
|
||||
system
|
||||
REQUIRED
|
||||
)
|
||||
endif()
|
||||
|
||||
if (NOT DISABLE_EDM)
|
||||
evc_setup_edm()
|
||||
|
||||
# In EDM mode, we can't install exports (because the dependencies usually do not install their exports)
|
||||
set(TIMER_INSTALL OFF)
|
||||
else()
|
||||
find_package(date REQUIRED)
|
||||
endif()
|
||||
|
||||
|
||||
# logging library
|
||||
add_subdirectory(lib)
|
||||
|
||||
if(BUILD_EXAMPLES)
|
||||
add_subdirectory(examples)
|
||||
endif()
|
||||
|
||||
# packaging
|
||||
if (TIMER_INSTALL)
|
||||
install(
|
||||
TARGETS timer
|
||||
EXPORT timer-targets
|
||||
)
|
||||
|
||||
install(
|
||||
DIRECTORY include/
|
||||
TYPE INCLUDE
|
||||
)
|
||||
|
||||
evc_setup_package(
|
||||
NAME everest-timer
|
||||
EXPORT timer-targets
|
||||
NAMESPACE everest
|
||||
ADDITIONAL_CONTENT
|
||||
"find_dependency(Boost COMPONENTS system)"
|
||||
)
|
||||
endif()
|
||||
201
tools/EVerest-main/lib/everest/timer/LICENSE
Normal file
201
tools/EVerest-main/lib/everest/timer/LICENSE
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
91
tools/EVerest-main/lib/everest/timer/README.md
Normal file
91
tools/EVerest-main/lib/everest/timer/README.md
Normal file
@@ -0,0 +1,91 @@
|
||||
C++ timer library for the EVerest framework
|
||||
===========================================
|
||||
EVerest is aiming to enable logfile replay. All EVerest components have to utilize this library for time (and delay) related things in order to enable accelerated logfile replay speed in the future.
|
||||
|
||||
All documentation and the issue tracking can be found in our main repository here: https://github.com/EVerest/everest
|
||||
|
||||
|
||||
Prerequisites
|
||||
=============
|
||||
|
||||
Dependencies (Ubuntu)
|
||||
---------------------------
|
||||
|
||||
On Ubuntu all other dependencies can be installed with the advanced
|
||||
packaging tool:
|
||||
|
||||
```
|
||||
sudo apt install build-essential libboost-all-dev gcovr lcov
|
||||
```
|
||||
|
||||
Cmake 3.14.7 (or higher)
|
||||
------------------------
|
||||
|
||||
Visit <https://cmake.org/download/> and download the latest version of
|
||||
cmake, then install it.
|
||||
|
||||
### Linux
|
||||
|
||||
Download the corresponding .sh-script for your architecture (x86\_64 or
|
||||
aarch64). Move the downloaded file to ```/opt``` and make it executable.
|
||||
|
||||
```
|
||||
sudo mv cmake-3.*version*.sh /opt
|
||||
sudo chmod +x cmake-3.*version*.sh
|
||||
```
|
||||
|
||||
Run the script and press ‚y‘ twice. The script will then install cmake.
|
||||
|
||||
```
|
||||
sudo ./cmake-3.*version*.sh
|
||||
```
|
||||
|
||||
In order to use the new installed cmake version instead of the old one
|
||||
you have to create a symbolic link:
|
||||
|
||||
```
|
||||
sudo ln -s /opt/cmake-3.*version*/bin/* /usr/local/bin
|
||||
```
|
||||
|
||||
To see if this whole procedure was successful, type:
|
||||
|
||||
```
|
||||
cmake --version
|
||||
```
|
||||
|
||||
It should now show the fresh installed version of cmake.
|
||||
|
||||
Build instructions
|
||||
==================
|
||||
|
||||
Clone the repository:
|
||||
|
||||
```
|
||||
git clone https://github.com/EVerest/time
|
||||
```
|
||||
|
||||
Create a folder named build and cd into it.
|
||||
Execute cmake and then make install:
|
||||
|
||||
```
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
make install
|
||||
```
|
||||
|
||||
To check your code with clang-tidy you can use the following cmake
|
||||
command:
|
||||
|
||||
```
|
||||
cmake .. -DCMAKE_RUN_CLANG_TIDY=ON make
|
||||
```
|
||||
|
||||
To run unit tests and generate a code coverage report you can run the
|
||||
following commands:
|
||||
|
||||
```
|
||||
make && make install && make gcovr_coverage
|
||||
```
|
||||
|
||||
The coverage report will be available in the index.html file in the
|
||||
```build/gcovr_coverage``` directory.
|
||||
4
tools/EVerest-main/lib/everest/timer/THIRD_PARTY.md
Normal file
4
tools/EVerest-main/lib/everest/timer/THIRD_PARTY.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Third-party dependencies used by this project
|
||||
|
||||
- [CodeCoverage.cmake](https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake) licensed under [The 3-Clause BSD License](https://opensource.org/licenses/BSD-3-Clause)
|
||||
- [Date](https://github.com/HowardHinnant/date) licensed under [The MIT License](https://opensource.org/licenses/MIT)
|
||||
10
tools/EVerest-main/lib/everest/timer/dependencies.yaml
Normal file
10
tools/EVerest-main/lib/everest/timer/dependencies.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
date:
|
||||
git: https://github.com/HowardHinnant/date.git
|
||||
git_tag: v3.0.4
|
||||
options: ["BUILD_TZ_LIB ON", "HAS_REMOTE_API 0", "USE_AUTOLOAD 0", "USE_SYSTEM_TZ_DB ON"]
|
||||
gtest:
|
||||
# GoogleTest now follows the Abseil Live at Head philosophy. We recommend updating to the latest commit in the main branch as often as possible.
|
||||
git: https://github.com/google/googletest.git
|
||||
git_tag: release-1.12.1
|
||||
cmake_condition: "LIBTIMER_BUILD_TESTING"
|
||||
|
||||
12
tools/EVerest-main/lib/everest/timer/examples/CMakeLists.txt
Normal file
12
tools/EVerest-main/lib/everest/timer/examples/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads)
|
||||
|
||||
find_package(Boost COMPONENTS program_options REQUIRED)
|
||||
|
||||
add_executable(libtimer_main main.cpp)
|
||||
target_link_libraries(libtimer_main
|
||||
PRIVATE
|
||||
Threads::Threads
|
||||
Boost::program_options
|
||||
everest::timer
|
||||
)
|
||||
84
tools/EVerest-main/lib/everest/timer/examples/main.cpp
Normal file
84
tools/EVerest-main/lib/everest/timer/examples/main.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright 2020 - 2026 Pionix GmbH and Contributors to EVerest
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/exception/diagnostic_information.hpp>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <date/date.h>
|
||||
#include <date/tz.h>
|
||||
|
||||
#include <everest/timer.hpp>
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
using date::operator<<;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
po::options_description desc("EVerest::time example");
|
||||
desc.add_options()("help,h", "produce help message");
|
||||
|
||||
po::variables_map vm;
|
||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||
po::notify(vm);
|
||||
|
||||
if (vm.count("help") != 0) {
|
||||
std::cout << desc << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
std::cout << "start time: " << date::utc_clock::now() << std::endl;
|
||||
|
||||
int count_timer1 = 0;
|
||||
int count_timer2 = 0;
|
||||
auto timer0 = std::make_unique<Everest::SteadyTimer>(&io_context);
|
||||
timer0->timeout([]() { std::cout << "Goodbye after 25s" << std::endl; }, 25s);
|
||||
auto timer1 = std::make_unique<Everest::SteadyTimer>(&io_context);
|
||||
timer1->at(
|
||||
[&]() {
|
||||
std::cout << "timer1 (asio) after 5s: " << date::utc_clock::now() << std::endl;
|
||||
timer1->interval(
|
||||
[&]() {
|
||||
std::cout << "timer1 (asio) interval (1s): " << date::utc_clock::now() << std::endl;
|
||||
count_timer1++;
|
||||
if (count_timer1 > 3) {
|
||||
timer1->timeout(
|
||||
[]() {
|
||||
std::cout << "timer1 (asio) timeout (3s): " << date::utc_clock::now() << std::endl;
|
||||
},
|
||||
3s);
|
||||
}
|
||||
},
|
||||
1s);
|
||||
},
|
||||
date::utc_clock::now() + 5s);
|
||||
|
||||
auto timer2 = std::make_unique<Everest::SteadyTimer>();
|
||||
timer2->at(
|
||||
[&]() {
|
||||
std::cout << "timer2 (thread) after 12s: " << date::utc_clock::now() << std::endl;
|
||||
timer2->interval(
|
||||
[&]() {
|
||||
std::cout << "timer2 (thread) interval (1s): " << date::utc_clock::now() << std::endl;
|
||||
count_timer2++;
|
||||
if (count_timer2 > 3) {
|
||||
timer2->timeout(
|
||||
[]() {
|
||||
std::cout << "timer2 (thread) timeout (3s): " << date::utc_clock::now() << std::endl;
|
||||
},
|
||||
3s);
|
||||
}
|
||||
},
|
||||
1s);
|
||||
},
|
||||
date::utc_clock::now() + 12s);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
244
tools/EVerest-main/lib/everest/timer/include/everest/timer.hpp
Normal file
244
tools/EVerest-main/lib/everest/timer/include/everest/timer.hpp
Normal file
@@ -0,0 +1,244 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright 2020 - 2021 Pionix GmbH and Contributors to EVerest
|
||||
#ifndef EVEREST_TIMER_HPP
|
||||
#define EVEREST_TIMER_HPP
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <date/date.h>
|
||||
#include <date/tz.h>
|
||||
|
||||
namespace Everest {
|
||||
template <typename TimerClock = date::utc_clock> class Timer {
|
||||
private:
|
||||
std::unique_ptr<boost::asio::basic_waitable_timer<TimerClock>> timer = nullptr;
|
||||
std::function<void()> timer_callback;
|
||||
std::function<void(const boost::system::error_code& error)> callback_wrapper;
|
||||
std::chrono::nanoseconds interval_nanoseconds = std::chrono::nanoseconds(0);
|
||||
std::atomic<bool> running = false;
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work;
|
||||
std::unique_ptr<std::thread> timer_thread = nullptr;
|
||||
|
||||
std::mutex mutex;
|
||||
|
||||
public:
|
||||
/// This timer will initialize a boost::asio::io_context
|
||||
Timer() :
|
||||
work(boost::asio::make_work_guard(this->io_context)),
|
||||
timer_thread(std::make_unique<std::thread>([this]() { this->io_context.run(); })) {
|
||||
this->timer = std::make_unique<boost::asio::basic_waitable_timer<TimerClock>>(this->io_context);
|
||||
}
|
||||
|
||||
explicit Timer(const std::function<void()>& callback) :
|
||||
timer_callback(callback),
|
||||
work(boost::asio::make_work_guard(this->io_context)),
|
||||
timer_thread(std::make_unique<std::thread>([this]() { this->io_context.run(); })) {
|
||||
this->timer = std::make_unique<boost::asio::basic_waitable_timer<TimerClock>>(this->io_context);
|
||||
}
|
||||
|
||||
explicit Timer(boost::asio::io_context* io_context) :
|
||||
timer(std::make_unique<boost::asio::basic_waitable_timer<TimerClock>>(*io_context)),
|
||||
work(boost::asio::make_work_guard(*io_context)) {
|
||||
}
|
||||
|
||||
Timer(boost::asio::io_context* io_context, const std::function<void()>& callback) :
|
||||
timer(std::make_unique<boost::asio::basic_waitable_timer<TimerClock>>(*io_context)),
|
||||
timer_callback(callback),
|
||||
work(boost::asio::make_work_guard(*io_context)) {
|
||||
}
|
||||
|
||||
/// \brief Cancel the asio timer and join the io_context thread.
|
||||
///
|
||||
/// The mutex is released BEFORE joining the io_context thread. A timer callback running on
|
||||
/// that thread may re-enter \ref at, \ref timeout, \ref interval, or \ref stop — each of which
|
||||
/// takes the mutex; if the destructor still held it, \c join would deadlock waiting for the
|
||||
/// callback to exit while the callback waited for the mutex.
|
||||
~Timer() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
if (this->timer) {
|
||||
this->timer->cancel();
|
||||
}
|
||||
}
|
||||
if (this->timer_thread) {
|
||||
this->io_context.stop();
|
||||
this->timer_thread->join();
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes the given callback at the given timepoint
|
||||
template <class Clock, class Duration = typename Clock::duration>
|
||||
void at(const std::function<void()>& callback, const std::chrono::time_point<Clock, Duration>& time_point) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->timer_callback = callback;
|
||||
|
||||
this->at_internal(time_point);
|
||||
}
|
||||
|
||||
/// Executes a previously configured callback at the given timepoint
|
||||
template <class Clock, class Duration = typename Clock::duration>
|
||||
void at(const std::chrono::time_point<Clock, Duration>& time_point) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->at_internal<Clock, Duration>(time_point);
|
||||
}
|
||||
|
||||
/// Executes the given callback periodically from now in the given interval
|
||||
template <class Rep, class Period>
|
||||
void interval(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->timer_callback = callback;
|
||||
|
||||
this->interval_internal(interval);
|
||||
}
|
||||
|
||||
/// Executes a previously configured callback periodically from now in the given interval
|
||||
template <class Rep, class Period> void interval(const std::chrono::duration<Rep, Period>& interval) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->interval_internal(interval);
|
||||
}
|
||||
|
||||
/// Executes the given callback once after the given interval
|
||||
template <class Rep, class Period>
|
||||
void timeout(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->timer_callback = callback;
|
||||
|
||||
this->timeout_internal(interval);
|
||||
}
|
||||
|
||||
/// Executes a previously configured callback once after the given interval
|
||||
template <class Rep, class Period> void timeout(const std::chrono::duration<Rep, Period>& interval) {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
this->stop_internal();
|
||||
this->timeout_internal(interval);
|
||||
}
|
||||
|
||||
/// Stops timer from executing its callback
|
||||
void stop() {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
stop_internal();
|
||||
}
|
||||
|
||||
/// Indicates if the timer is running
|
||||
bool is_running() {
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
private:
|
||||
template <class Clock, class Duration = typename Clock::duration>
|
||||
void at_internal(const std::chrono::time_point<Clock, Duration>& time_point) {
|
||||
if (this->timer_callback == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->timer) {
|
||||
running = true;
|
||||
|
||||
// use asio timer
|
||||
this->timer->expires_at(time_point);
|
||||
this->timer->async_wait([this](const boost::system::error_code& e) {
|
||||
if (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->timer_callback();
|
||||
running = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template <class Rep, class Period> void interval_internal(const std::chrono::duration<Rep, Period>& interval) {
|
||||
this->interval_nanoseconds = interval;
|
||||
if (interval_nanoseconds == std::chrono::nanoseconds(0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->timer_callback == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->timer) {
|
||||
running = true;
|
||||
|
||||
// use asio timer
|
||||
this->callback_wrapper = [this](const boost::system::error_code& error) {
|
||||
if (error) {
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->mutex);
|
||||
this->timer->expires_after(
|
||||
std::chrono::duration_cast<typename TimerClock::duration>(this->interval_nanoseconds));
|
||||
this->timer->async_wait(this->callback_wrapper);
|
||||
}
|
||||
|
||||
this->timer_callback();
|
||||
};
|
||||
|
||||
this->timer->expires_after(
|
||||
std::chrono::duration_cast<typename TimerClock::duration>(this->interval_nanoseconds));
|
||||
this->timer->async_wait(this->callback_wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Rep, class Period> void timeout_internal(const std::chrono::duration<Rep, Period>& interval) {
|
||||
if (this->timer_callback == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->timer) {
|
||||
running = true;
|
||||
|
||||
// use asio timer
|
||||
this->timer->expires_after(interval);
|
||||
this->timer->async_wait([this](const boost::system::error_code& error) {
|
||||
if (error) {
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
this->timer_callback();
|
||||
running = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void stop_internal() {
|
||||
if (this->timer) {
|
||||
// asio based timer
|
||||
this->timer->cancel();
|
||||
}
|
||||
|
||||
running = false;
|
||||
}
|
||||
};
|
||||
|
||||
using SteadyTimer = Timer<date::utc_clock>;
|
||||
using SystemTimer = Timer<date::utc_clock>;
|
||||
} // namespace Everest
|
||||
|
||||
#endif // EVEREST_TIMER_HPP
|
||||
18
tools/EVerest-main/lib/everest/timer/lib/CMakeLists.txt
Normal file
18
tools/EVerest-main/lib/everest/timer/lib/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
add_library(timer INTERFACE)
|
||||
add_library(everest::timer ALIAS timer)
|
||||
|
||||
target_include_directories(timer
|
||||
INTERFACE
|
||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
if(Boost_VERSION_STRING VERSION_LESS "1.69.0")
|
||||
set(EVEREST_LIBTIMER_BOOST_SYSTEM_LINK_LIBRARY Boost::system)
|
||||
endif()
|
||||
|
||||
target_link_libraries(timer
|
||||
INTERFACE
|
||||
${EVEREST_LIBTIMER_BOOST_SYSTEM_LINK_LIBRARY}
|
||||
date::date-tz
|
||||
)
|
||||
23
tools/EVerest-main/lib/everest/timer/tests/CMakeLists.txt
Normal file
23
tools/EVerest-main/lib/everest/timer/tests/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
if(DISABLE_EDM)
|
||||
find_package(GTest REQUIRED)
|
||||
else()
|
||||
set(GTEST_LIBRARIES
|
||||
GTest::gtest
|
||||
GTest::gtest_main
|
||||
)
|
||||
endif()
|
||||
set(TEST_TARGET_NAME ${PROJECT_NAME}_tests)
|
||||
add_executable(${TEST_TARGET_NAME} libtimer_unit_test.cpp)
|
||||
|
||||
target_include_directories(${TEST_TARGET_NAME}
|
||||
PUBLIC
|
||||
${GTEST_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(${TEST_TARGET_NAME} PRIVATE
|
||||
${GTEST_LIBRARIES}
|
||||
everest::timer
|
||||
)
|
||||
|
||||
add_test(${TEST_TARGET_NAME} ${TEST_TARGET_NAME})
|
||||
@@ -0,0 +1,66 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright Pionix GmbH and Contributors to EVerest
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <everest/timer.hpp>
|
||||
|
||||
namespace libtimer {
|
||||
class LibTimerUnitTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LibTimerUnitTest, just_an_example) {
|
||||
ASSERT_TRUE(1 == 1);
|
||||
}
|
||||
|
||||
// Regression: callback re-arms its own timer; pre-fix destructor deadlocks here.
|
||||
TEST_F(LibTimerUnitTest, destructor_does_not_deadlock_on_reentrant_callback) {
|
||||
std::atomic<bool> finished{false};
|
||||
std::atomic<int> fire_count{0};
|
||||
std::thread watchdog([&finished]() {
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
if (finished.load()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
ADD_FAILURE() << "Timer destructor deadlocked on re-entrant callback";
|
||||
finished.store(true);
|
||||
});
|
||||
|
||||
{
|
||||
Everest::SteadyTimer timer;
|
||||
std::atomic<bool> in_callback{false};
|
||||
|
||||
timer.interval(
|
||||
[&timer, &fire_count, &in_callback]() {
|
||||
in_callback.store(true);
|
||||
fire_count.fetch_add(1);
|
||||
// Sleep long enough for the main thread to enter ~Timer and start
|
||||
// blocking on the mutex before this callback calls stop().
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
timer.stop();
|
||||
in_callback.store(false);
|
||||
},
|
||||
std::chrono::milliseconds(1));
|
||||
|
||||
while (!in_callback.load()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
finished.store(true);
|
||||
watchdog.join();
|
||||
EXPECT_GE(fire_count.load(), 1);
|
||||
}
|
||||
} // namespace libtimer
|
||||
Reference in New Issue
Block a user