1 #pragma once
2 
3 #include <cstdint>
4 #include <functional>
5 #include <map>
6 #include <string>
7 #include <tuple>
8 
9 /**
10  * Key-value store for string-type system info parameters.
11  */
12 class SysInfoParamStoreIntf
13 {
14   public:
~SysInfoParamStoreIntf()15     virtual ~SysInfoParamStoreIntf() {}
16 
17     /**
18      * Returns true if parameter is found. If and only if s is non-null,
19      * invokes the parameter's callback and writes the value.
20      *
21      * @param[in] paramSelector - the key to lookup.
22      * @return tuple of bool and string, true if parameter is found and
23      * string set accordingly.
24      */
25     virtual std::tuple<bool, std::string>
26         lookup(uint8_t paramSelector) const = 0;
27 
28     /**
29      * Update a parameter by its code with a string value.
30      *
31      * @param[in] paramSelector - the key to update.
32      * @param[in] s - the value to set.
33      */
34     virtual void update(uint8_t paramSelector, const std::string& s) = 0;
35 
36     /**
37      * Update a parameter by its code with a callback that is called to retrieve
38      * its value whenever called. Callback must be idempotent, as it may be
39      * called multiple times by the host to retrieve the parameter by chunks.
40      *
41      * @param[in] paramSelector - the key to update.
42      * @param[in] callback - the callback to use for parameter retrieval.
43      */
44     virtual void update(uint8_t paramSelector,
45                         const std::function<std::string()>& callback) = 0;
46 
47     // TODO: Store "read-only" flag for each parameter.
48     // TODO: Function to erase a parameter?
49 };
50 
51 /**
52  * Implement the system info parameters store as a map of callbacks.
53  */
54 class SysInfoParamStore : public SysInfoParamStoreIntf
55 {
56   public:
57     std::tuple<bool, std::string> lookup(uint8_t paramSelector) const override;
58     void update(uint8_t paramSelector, const std::string& s) override;
59     void update(uint8_t paramSelector,
60                 const std::function<std::string()>& callback) override;
61 
62   private:
63     std::map<uint8_t, std::function<std::string()>> params;
64 };
65