Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
parameter_cache.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <expected>
5#include <filesystem>
6#include <nlohmann/json_fwd.hpp>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <vector>
11
13#include "node/synapticon.h" // isSomanetDevice
14
15namespace mm::node {
16
17// The cache is enabled by default only for a SOMANET device (@c isSomanetDevice): for SOMANET
18// firmware the CoE object dictionary is uniquely determined by @c (productCode, revisionNumber) —
19// Synapticon bumps the revision whenever the dictionary changes, so an on-disk cache keyed on
20// identity alone can never serve definitions that do not match the device. That guarantee does not
21// hold for arbitrary third-party vendors (see @c ParameterCacheConfig::cacheAllVendors).
22
27 bool cacheAllVendors = false;
28 std::string directory;
29 bool enabled = true;
30};
31
53 public:
67 static constexpr int kFormatVersion = 2;
68
69 ParameterCache() = default;
70 explicit ParameterCache(ParameterCacheConfig config);
71
74
79 bool enabledForVendor(uint32_t vendorId) const;
80
89 std::optional<std::vector<DeviceParameter>> load(uint32_t vendorId, uint32_t productCode,
90 uint32_t revisionNumber) const;
91
97 void store(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber,
98 const std::vector<DeviceParameter>& parameters) const;
99
101 struct CacheEntry {
102 std::string id;
103 uint32_t vendorId = 0;
104 uint32_t productCode = 0;
105 uint32_t revisionNumber = 0;
106 uint32_t parameterCount = 0;
107 std::uintmax_t sizeBytes = 0;
108 };
109
113 static std::string makeId(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber);
114
121 std::vector<CacheEntry> list() const;
122
129 std::expected<std::vector<uint8_t>, std::string> readRaw(std::string_view id) const;
130
133 std::expected<void, std::string> remove(std::string_view id) const;
134
135 private:
139 std::filesystem::path resolveDir() const;
140
143 std::filesystem::path pathFor(uint32_t vendorId, uint32_t productCode,
144 uint32_t revisionNumber) const;
145
146 ParameterCacheConfig config_;
147};
148
151void to_json(nlohmann::json& j, const ParameterCache::CacheEntry& e);
152
153} // namespace mm::node
On-disk cache of CoE parameter definitions, keyed by device identity.
Definition parameter_cache.h:52
bool enabledForVendor(uint32_t vendorId) const
Whether the cache is active for vendorId under the current policy.
Definition parameter_cache.cc:129
static constexpr int kFormatVersion
Bumped when an older file's contents can no longer be trusted, whatever its shape.
Definition parameter_cache.h:67
void store(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber, const std::vector< DeviceParameter > &parameters) const
Persists parameter definitions for an identity (atomic temp-file + rename).
Definition parameter_cache.cc:194
std::expected< void, std::string > remove(std::string_view id) const
Deletes one cache file addressed by its id. Policy-independent.
Definition parameter_cache.cc:281
static std::string makeId(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber)
Builds the opaque id a client uses to address one cache file, from an identity triple....
Definition parameter_cache.cc:147
std::optional< std::vector< DeviceParameter > > load(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber) const
Loads the cached parameter definitions for an identity.
Definition parameter_cache.cc:158
std::expected< std::vector< uint8_t >, std::string > readRaw(std::string_view id) const
Reads the raw JSON bytes of one cache file, addressed by its id (for download).
Definition parameter_cache.cc:267
void configure(ParameterCacheConfig config)
Replaces the active policy/location. Called by DeviceManager::init.
Definition parameter_cache.cc:127
std::vector< CacheEntry > list() const
Lists every valid cache file in the cache directory.
Definition parameter_cache.cc:227
mailboxStatusFmmu useCompleteAccess parameters
Definition config.h:245
Definition bus_health_source.h:7
void to_json(nlohmann::json &j, const Cia402Status &s)
Serialises a Cia402Status. Emits state/modeName as human-readable strings alongside the numeric statu...
Definition cia402_drive.cc:13
Policy and location for the on-disk parameter cache.
Definition parameter_cache.h:26
std::string directory
"" = a standard per-user cache directory (see resolveDir).
Definition parameter_cache.h:28
bool cacheAllVendors
false: cache SOMANET devices only; true: cache every vendor.
Definition parameter_cache.h:27
bool enabled
Master switch for the whole cache (false disables it entirely).
Definition parameter_cache.h:29
Summary of one cache file on disk, for the management UI.
Definition parameter_cache.h:101
uint32_t revisionNumber
Revision number from the file header.
Definition parameter_cache.h:105
std::uintmax_t sizeBytes
File size on disk, in bytes.
Definition parameter_cache.h:107
std::string id
Opaque "<vendor>-<product>-<revision>" key (see makeId).
Definition parameter_cache.h:102
uint32_t vendorId
Vendor ID from the file header.
Definition parameter_cache.h:103
uint32_t productCode
Product code from the file header.
Definition parameter_cache.h:104
uint32_t parameterCount
Number of cached parameter definitions in the file.
Definition parameter_cache.h:106