CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
Configurator.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
14#include <units/time.h>
15#include <mutex>
16
17namespace ctre {
18namespace phoenix6 {
19namespace configs {
20
22 {
23 public:
24 /**
25 * \brief The default maximum amount of time to wait for a config.
26 */
27 units::time::second_t DefaultTimeoutSeconds{0.100_s};
28
29 private:
30 hardware::DeviceIdentifier deviceIdentifier;
31 mutable std::mutex _m;
32
33 mutable units::second_t _creationTime = utils::GetCurrentTime();
34 mutable units::second_t _lastConfigTime = _creationTime;
35 mutable units::second_t _freqConfigStart = 0_s;
36
37 protected:
38 ParentConfigurator(hardware::DeviceIdentifier deviceIdentifier) : deviceIdentifier{std::move(deviceIdentifier)}
39 {
40 }
41
44
45 void ReportIfFrequent() const
46 {
47 auto currentTime = utils::GetCurrentTime();
48 auto lastConfigTime = _lastConfigTime;
49 _lastConfigTime = currentTime;
50
51 if (currentTime - _creationTime < 5_s) {
52 /* this was constructed recently, do not warn */
53 return;
54 }
55
56 if (currentTime - lastConfigTime < 1_s) {
57 /* we should not see multiple configs within a second */
58 if (_freqConfigStart == 0_s) {
59 /* this is the first frequent config, capture the time we started seeing them */
60 _freqConfigStart = lastConfigTime;
61 }
62 } else {
63 /* this is not a frequent config, reset the start time */
64 _freqConfigStart = 0_s;
65 }
66
67 if (_freqConfigStart > 0_s && currentTime - _freqConfigStart > 3_s) {
68 /* we've been seeing frequent config calls continuously for a few seconds, warn user */
70
71 std::stringstream location;
72 location << this->deviceIdentifier.ToString() << " Config";
73 c_ctre_phoenix_report_error(status.IsError(), status, 0, status.GetDescription(), location.str().c_str(), ctre::phoenix::platform::GetStackTrace(1).c_str());
74 }
75 }
76
77 ctre::phoenix::StatusCode SetConfigsPrivate(const std::string &serializedString, units::time::second_t timeoutSeconds, bool futureProofConfigs, bool overrideIfDuplicate)
78 {
80 {
81 std::lock_guard<std::mutex> lock{_m};
82
84 deviceIdentifier.network.c_str(),
85 deviceIdentifier.deviceHash,
86 timeoutSeconds.to<double>(),
87 serializedString,
88 futureProofConfigs,
89 overrideIfDuplicate);
90
92 }
93
94 if (!status.IsOK() && status != ctre::phoenix::StatusCode::TimeoutCannotBeZero) {
95 std::stringstream location;
96 location << this->deviceIdentifier.ToString() << " Apply Config";
97 c_ctre_phoenix_report_error(status.IsError(), status, 0, status.GetDescription(), location.str().c_str(), ctre::phoenix::platform::GetStackTrace(1).c_str());
98 }
99 return status;
100 }
101 ctre::phoenix::StatusCode GetConfigsPrivate(std::string &serializedString, units::time::second_t timeoutSeconds) const
102 {
104 {
105 std::lock_guard<std::mutex> lock{_m};
106
108 deviceIdentifier.network.c_str(),
109 deviceIdentifier.deviceHash,
110 timeoutSeconds.to<double>(),
111 serializedString);
112
114 }
115
116 if (!status.IsOK()) {
117 std::stringstream location;
118 location << this->deviceIdentifier.ToString() << " Refresh Config";
119 c_ctre_phoenix_report_error(status.IsError(), status, 0, status.GetDescription(), location.str().c_str(), ctre::phoenix::platform::GetStackTrace(1).c_str());
120 }
121 return status;
122 }
123 };
124
125}
126}
127}
CTREXPORT void c_ctre_phoenix_report_error(int isError, int32_t errorCode, int isLVCode, const char *details, const char *location, const char *callStack)
Definition Configurator.hpp:22
void ReportIfFrequent() const
Definition Configurator.hpp:45
ctre::phoenix::StatusCode SetConfigsPrivate(const std::string &serializedString, units::time::second_t timeoutSeconds, bool futureProofConfigs, bool overrideIfDuplicate)
Definition Configurator.hpp:77
ParentConfigurator(hardware::DeviceIdentifier deviceIdentifier)
Definition Configurator.hpp:38
ParentConfigurator(ParentConfigurator const &)=delete
ctre::phoenix::StatusCode GetConfigsPrivate(std::string &serializedString, units::time::second_t timeoutSeconds) const
Definition Configurator.hpp:101
ParentConfigurator & operator=(ParentConfigurator const &)=delete
units::time::second_t DefaultTimeoutSeconds
The default maximum amount of time to wait for a config.
Definition Configurator.hpp:27
Definition DeviceIdentifier.hpp:19
std::string ToString() const
Definition DeviceIdentifier.hpp:34
uint32_t deviceHash
Definition DeviceIdentifier.hpp:24
std::string network
Definition DeviceIdentifier.hpp:21
static ctre::phoenix::StatusCode Device_GetConfigValues(const char *network, int deviceHash, double timeoutSeconds, std::string &serializedString)
Gets the config value of the device.
static ctre::phoenix::StatusCode Device_SetConfigValues(const char *network, int deviceHash, double timeoutSeconds, const std::string &serializedString, bool futureProofConfigs, bool overrideIfDuplicate)
Sets the config value of the device.
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
constexpr const char * GetDescription() const
Gets the description of this StatusCode.
Definition StatusCodes.h:1066
static constexpr int FrequentConfigCalls
Do not apply or refresh configs periodically, as configs are blocking.
Definition StatusCodes.h:632
constexpr bool IsOK() const
Definition StatusCodes.h:859
static constexpr int TimeoutCannotBeZero
Blocking operations, such as configs, cannot have a timeout of 0.
Definition StatusCodes.h:776
constexpr bool IsError() const
Definition StatusCodes.h:851
units::second_t GetCurrentTime()
Get the current timestamp.
Definition Utils.hpp:29
CTREXPORT std::string GetStackTrace(int offset)
Get a stack trace, ignoring the first "offset" symbols.
Definition StatusCodes.h:18
Definition span.hpp:401