diff options
author | Xo Wang <xow@google.com> | 2017-08-09 15:31:33 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-09-24 14:49:47 +0000 |
commit | 069db2fd856db744a254120ed3054d49f241ae80 (patch) | |
tree | f18da9a2afe68433a22be34d2f25148fffce8d43 | |
parent | ba23ff7102ce7d9c63affecc626efb4ee97124b1 (diff) | |
download | phosphor-host-ipmid-069db2fd856db744a254120ed3054d49f241ae80.tar.gz phosphor-host-ipmid-069db2fd856db744a254120ed3054d49f241ae80.zip |
apphandler: Add storage for Get/Set System Info Parameter
All but one standard System Info Parameter and most (all?) OEM
parameters are string type. Add a interface to connect string callbacks
to System Info Parameter selector codes. Also add a convenience call
that can connect a static string to a parameter selector code.
Change-Id: I5e35d0418b8ddf5b2575fac093acfc7d7ca2217c
Signed-off-by: Xo Wang <xow@google.com>
Signed-off-by: Patrick Venture <venture@google.com>
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | sys_info_param.cpp | 33 | ||||
-rw-r--r-- | sys_info_param.hpp | 66 |
3 files changed, 100 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 8286fd4..28ae7ed 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,6 +70,7 @@ libipmi20_la_SOURCES = \ app/watchdog.cpp \ app/watchdog_service.cpp \ apphandler.cpp \ + sys_info_param.cpp \ sensorhandler.cpp \ storagehandler.cpp \ chassishandler.cpp \ diff --git a/sys_info_param.cpp b/sys_info_param.cpp new file mode 100644 index 0000000..c9bee32 --- /dev/null +++ b/sys_info_param.cpp @@ -0,0 +1,33 @@ +#include "sys_info_param.hpp" + +std::tuple<bool, std::string> + SysInfoParamStore::lookup(uint8_t paramSelector) const +{ + const auto iterator = params.find(paramSelector); + if (iterator == params.end()) + { + return std::make_tuple(false, ""); + } + + auto& callback = iterator->second; + auto s = callback(); + return std::make_tuple(true, s); +} + +void SysInfoParamStore::update(uint8_t paramSelector, const std::string& s) +{ + // Add a callback that captures a copy of the string passed and returns it + // when invoked. + + // clang-format off + update(paramSelector, [s]() { + return s; + }); + // clang-format on +} + +void SysInfoParamStore::update(uint8_t paramSelector, + const std::function<std::string()>& callback) +{ + params[paramSelector] = callback; +} diff --git a/sys_info_param.hpp b/sys_info_param.hpp new file mode 100644 index 0000000..6088626 --- /dev/null +++ b/sys_info_param.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include <cstdint> +#include <functional> +#include <map> +#include <string> +#include <tuple> + +/** + * Key-value store for string-type system info parameters. + */ +class SysInfoParamStoreIntf +{ + public: + virtual ~SysInfoParamStoreIntf() + { + } + + /** + * Returns true if parameter is found. If and only if s is non-null, + * invokes the parameter's callback and writes the value. + * + * @param[in] paramSelector - the key to lookup. + * @return tuple of bool and string, true if parameter is found and + * string set accordingly. + */ + virtual std::tuple<bool, std::string> + lookup(uint8_t paramSelector) const = 0; + + /** + * Update a parameter by its code with a string value. + * + * @param[in] paramSelector - the key to update. + * @param[in] s - the value to set. + */ + virtual void update(uint8_t paramSelector, const std::string& s) = 0; + + /** + * Update a parameter by its code with a callback that is called to retrieve + * its value whenever called. Callback must be idempotent, as it may be + * called multiple times by the host to retrieve the parameter by chunks. + * + * @param[in] paramSelector - the key to update. + * @param[in] callback - the callback to use for parameter retrieval. + */ + virtual void update(uint8_t paramSelector, + const std::function<std::string()>& callback) = 0; + + // TODO: Store "read-only" flag for each parameter. + // TODO: Function to erase a parameter? +}; + +/** + * Implement the system info parameters store as a map of callbacks. + */ +class SysInfoParamStore : public SysInfoParamStoreIntf +{ + public: + std::tuple<bool, std::string> lookup(uint8_t paramSelector) const override; + void update(uint8_t paramSelector, const std::string& s) override; + void update(uint8_t paramSelector, + const std::function<std::string()>& callback) override; + + private: + std::map<uint8_t, std::function<std::string()>> params; +}; |