Motion Master 6.0.0-alpha.86
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" // kSynapticonVendorId
14
15namespace mm::node {
16
17// The cache is enabled by default only for @c kSynapticonVendorId: for this vendor the CoE object
18// dictionary is uniquely determined by @c (productCode, revisionNumber) — Synapticon bumps the
19// revision whenever the dictionary changes, so an on-disk cache keyed on identity alone can never
20// serve definitions that do not match the device. That guarantee does not hold for arbitrary
21// third-party vendors (see @c ParameterCacheConfig::cacheAllVendors).
22
28 false;
29 std::string directory;
30 bool enabled = true;
31};
32
54 public:
68 static constexpr int kFormatVersion = 2;
69
70 ParameterCache() = default;
71 explicit ParameterCache(ParameterCacheConfig config);
72
75
80 bool enabledForVendor(uint32_t vendorId) const;
81
90 std::optional<std::vector<DeviceParameter>> load(uint32_t vendorId, uint32_t productCode,
91 uint32_t revisionNumber) const;
92
98 void store(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber,
99 const std::vector<DeviceParameter>& parameters) const;
100
102 struct CacheEntry {
103 std::string id;
104 uint32_t vendorId = 0;
105 uint32_t productCode = 0;
106 uint32_t revisionNumber = 0;
107 uint32_t parameterCount = 0;
108 std::uintmax_t sizeBytes = 0;
109 };
110
114 static std::string makeId(uint32_t vendorId, uint32_t productCode, uint32_t revisionNumber);
115
122 std::vector<CacheEntry> list() const;
123
130 std::expected<std::vector<uint8_t>, std::string> readRaw(std::string_view id) const;
131
134 std::expected<void, std::string> remove(std::string_view id) const;
135
136 private:
140 std::filesystem::path resolveDir() const;
141
144 std::filesystem::path pathFor(uint32_t vendorId, uint32_t productCode,
145 uint32_t revisionNumber) const;
146
147 ParameterCacheConfig config_;
148};
149
152void to_json(nlohmann::json& j, const ParameterCache::CacheEntry& e);
153
154} // namespace mm::node
On-disk cache of CoE parameter definitions, keyed by device identity.
Definition parameter_cache.h:53
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:68
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
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:29
bool cacheAllVendors
false: cache Synapticon (0x22D2) 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:30
Summary of one cache file on disk, for the management UI.
Definition parameter_cache.h:102
uint32_t revisionNumber
Revision number from the file header.
Definition parameter_cache.h:106
std::uintmax_t sizeBytes
File size on disk, in bytes.
Definition parameter_cache.h:108
std::string id
Opaque "<vendor>-<product>-<revision>" key (see makeId).
Definition parameter_cache.h:103
uint32_t vendorId
Vendor ID from the file header.
Definition parameter_cache.h:104
uint32_t productCode
Product code from the file header.
Definition parameter_cache.h:105
uint32_t parameterCount
Number of cached parameter definitions in the file.
Definition parameter_cache.h:107