ReduxLib C++ 2024.3.2
Loading...
Searching...
No Matches
CanandSettings.h
1// Copyright (c) Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the 3-clause BSD license.
3
4#pragma once
5#include <cstdint>
6#include <unordered_map>
7#include <vector>
8#include <fmt/format.h>
9
10namespace redux::canand {
11
12/**
13 * Base (simple) settings class for Redux devices.
14 *
15 * Inheriting classes with more complex firmware interfaces may or may not use this structure.
16 * It's typically used in conjunction with CanandSettingsManager.
17 *
18 * In general, however, it's a pretty useful structure.
19 */
21 public:
22 /** Default constructor. */
23 CanandSettings() = default;
24 /**
25 * Copy constructor -- only copies over a filtered copy of values.
26 * @param stg reference to another CanandSettings.
27 */
29
30 /** Destructor. */
31 ~CanandSettings() = default;
32
33 /**
34 * Return a direct filtered view of settings values as a new unordered_map, limited to only valid
35 * settings.
36 * @return map
37 */
38 inline std::unordered_map<uint8_t, uint64_t> FilteredMap() {
39 std::unordered_map<uint8_t, uint64_t> ret;
40 for (auto addr : SettingAddresses()) {
41 if (values.contains(addr)) {
42 ret[addr] = values[addr];
43 }
44 }
45 return ret;
46 }
47
48 /**
49 * Returns whether or not all settings fields have been written into the object.
50 *
51 * <p>May return false if the a getSettings call did not succeed in fetching every setting.
52 *
53 * @return whether the settings object has been filled
54 */
55 inline bool AllSettingsReceived() const {
56 for (auto addr : SettingAddresses()) {
57 if (!values.contains(addr)) return false;
58 }
59 return true;
60 }
61
62 /**
63 * Gets the array of settings addresses this settings class records.
64 * @return DetailsKey array
65 */
66 virtual const std::vector<uint8_t>& SettingAddresses() const {
68 };
69
70 /**
71 * Gets the backing store.
72 * @return the underlying map.
73 */
74 inline std::unordered_map<uint8_t, uint64_t>& GetMap() {
75 return values;
76 }
77
78 /**
79 * Returns if this CanandSettings has any set settings or not.
80 * Useful when a CanandSettings is returned as a result of setSettings to check if all settings
81 * succeeded.
82 * @return true if empty
83 */
84 inline bool IsEmpty() const {
85 return values.empty();
86 }
87
88 /**
89 * Returns if this CanandSettings is set to be ephemeral.
90 *
91 * Ephemeral settings do not persist on device reboot, but do not impose any flash wear.
92 * @return true if ephemeral
93 */
94 inline bool IsEphemeral() const {
95 return ephemeral;
96 }
97
98 /**
99 * Sets whether or not the settings will be set as ephemeral -- that is, does not persist on
100 * device power cycle.
101 *
102 * Pre-v2024 firmwares will not support this!
103 *
104 * @param value true if ephemeral
105 */
106 inline void SetEphemeral(bool value) {
107 ephemeral = value;
108 }
109
110 /**
111 * Dump the CanandSettings map as a string.
112 * @return string
113 */
114 inline std::string ToString() {
115 std::string s = "CanandSettings {\n";
116 for (auto& it: values) {
117 s.append(fmt::format(" 0x{:x}: {:x},\n", it.first, it.second));
118 }
119 s.append("}");
120 return s;
121 }
122
123 protected:
124 /**
125 * The backing store.
126 */
127 std::unordered_map<uint8_t, uint64_t> values;
128
129 /**
130 * Whether the settings in this class should be set ephemerally.
131 */
132 bool ephemeral = false;
133};
134
135}
Definition: CanandSettings.h:20
CanandSettings(CanandSettings &stg)
Definition: CanandSettings.h:28
bool IsEphemeral() const
Definition: CanandSettings.h:94
std::unordered_map< uint8_t, uint64_t > FilteredMap()
Definition: CanandSettings.h:38
virtual const std::vector< uint8_t > & SettingAddresses() const
Definition: CanandSettings.h:66
void SetEphemeral(bool value)
Definition: CanandSettings.h:106
std::unordered_map< uint8_t, uint64_t > values
Definition: CanandSettings.h:127
bool ephemeral
Definition: CanandSettings.h:132
std::string ToString()
Definition: CanandSettings.h:114
bool IsEmpty() const
Definition: CanandSettings.h:84
std::unordered_map< uint8_t, uint64_t > & GetMap()
Definition: CanandSettings.h:74
bool AllSettingsReceived() const
Definition: CanandSettings.h:55
const std::vector< uint8_t > VDEP_SETTINGS
Definition: CanandDevice.h:192
Definition: CanandMessage.h:10