Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 1 | ## This file is a template. The comment below is emitted |
| 2 | ## into the rendered file; feel free to edit this file. |
| 3 | // WARNING: Generated header. Do not edit! |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 4 | <% |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 5 | import re |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 6 | from collections import defaultdict |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 7 | from sdbusplus.namedelement import NamedElement |
Patrick Williams | 2b7152f | 2020-04-02 07:18:32 -0500 | [diff] [blame] | 8 | objects = settingsDict.keys() |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 9 | sdbusplus_includes = [] |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 10 | props = defaultdict(list) |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 11 | validators = defaultdict(tuple) |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 12 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 13 | def get_setting_sdbusplus_type(setting_intf): |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 14 | setting = "sdbusplus::" + setting_intf.replace('.', '::') |
| 15 | i = setting.rfind('::') |
| 16 | setting = setting[:i] + '::server::' + setting[i+2:] |
| 17 | return setting |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 18 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 19 | def get_setting_type(path): |
| 20 | path = path[1:] |
| 21 | path = path.replace('/', '::') |
| 22 | return path |
Matt Spinler | ffdf865 | 2023-05-24 11:09:54 -0500 | [diff] [blame] | 23 | |
| 24 | def get_default_value(object, interface, prop): |
| 25 | default_value = None |
| 26 | for item in settingsDict[object]: |
| 27 | if item['Interface'] == interface: |
| 28 | default_value = item['Properties'][prop]['Default'] |
| 29 | break |
| 30 | |
| 31 | if isinstance(default_value, str) and not \ |
| 32 | default_value.startswith('"') and '::' in default_value: |
| 33 | ns = get_setting_sdbusplus_type(interface) |
| 34 | i = ns.rfind('::') |
| 35 | default_value = "{}::{}".format(ns[:i], default_value) |
| 36 | |
| 37 | return default_value |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 38 | %>\ |
| 39 | #pragma once |
| 40 | |
| 41 | % for object in objects: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 42 | % for item in settingsDict[object]: |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 43 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 44 | include = item['Interface'] |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 45 | include = include.replace('.', '/') |
| 46 | include = include + "/server.hpp" |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 47 | sdbusplus_includes.append(include) |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 48 | %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 49 | % endfor |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 50 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 51 | #include <cereal/archives/json.hpp> |
James Feist | 74e3be7 | 2019-02-15 09:59:42 -0800 | [diff] [blame] | 52 | #include <cereal/types/vector.hpp> |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 53 | #include <fstream> |
| 54 | #include <utility> |
Patrick Williams | 6306e5e | 2022-06-16 17:14:54 -0500 | [diff] [blame] | 55 | #include <filesystem> |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 56 | #include <regex> |
| 57 | #include <phosphor-logging/elog.hpp> |
| 58 | #include <phosphor-logging/elog-errors.hpp> |
| 59 | #include <phosphor-logging/log.hpp> |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 60 | #include <phosphor-logging/lg2.hpp> |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 61 | #include <xyz/openbmc_project/Common/error.hpp> |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 62 | using namespace phosphor::logging; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 63 | |
Jagpal Singh Gill | 4d28bcd | 2023-04-23 23:34:05 -0700 | [diff] [blame] | 64 | /* The DBus busname to own */ |
| 65 | #define SETTINGS_BUSNAME "xyz.openbmc_project.Settings" |
| 66 | /* Path of directory housing persisted settings */ |
| 67 | #define SETTINGS_PERSIST_PATH "/var/lib/phosphor-settings-manager/settings" |
| 68 | |
| 69 | /* Class version to register with Cereal */ |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 70 | static constexpr size_t CLASS_VERSION = 2; |
| 71 | static constexpr size_t CLASS_VERSION_WITH_NVP = 2; |
Jagpal Singh Gill | 4d28bcd | 2023-04-23 23:34:05 -0700 | [diff] [blame] | 72 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 73 | % for i in set(sdbusplus_includes): |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 74 | #include "${i}" |
| 75 | % endfor |
| 76 | |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 77 | namespace phosphor |
| 78 | { |
| 79 | namespace settings |
| 80 | { |
| 81 | |
Patrick Williams | 6306e5e | 2022-06-16 17:14:54 -0500 | [diff] [blame] | 82 | namespace fs = std::filesystem; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 83 | |
Deepak Kodihalli | 242bc77 | 2017-08-04 02:47:54 -0500 | [diff] [blame] | 84 | namespace persistent |
| 85 | { |
| 86 | |
| 87 | // A setting d-bus object /foo/bar/baz is persisted in the filesystem with the |
| 88 | // same path. This eases re-construction of settings objects when we restore |
| 89 | // from the filesystem. This can be a problem though when you have two objects |
| 90 | // such as - /foo/bar and /foo/bar/baz. This is because 'bar' will be treated as |
| 91 | // a file in the first case, and a subdir in the second. To solve this, suffix |
| 92 | // files with a trailing __. The __ is a safe character sequence to use, because |
| 93 | // we won't have d-bus object paths ending with this. |
| 94 | // With this the objects would be persisted as - /foo/bar__ and /foo/bar/baz__. |
| 95 | constexpr auto fileSuffix = "__"; |
| 96 | |
| 97 | } |
| 98 | |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 99 | static fs::path getFilePath(const fs::path& objectPath) |
| 100 | { |
| 101 | fs::path p(SETTINGS_PERSIST_PATH); |
| 102 | p /= objectPath.relative_path(); |
| 103 | p += persistent::fileSuffix; |
| 104 | return p; |
| 105 | } |
| 106 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 107 | % for object in objects: |
| 108 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 109 | ns = object.split('/') |
| 110 | ns.pop(0) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 111 | %>\ |
| 112 | % for n in ns: |
| 113 | namespace ${n} |
| 114 | { |
| 115 | % endfor |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 116 | <% |
| 117 | interfaces = [] |
| 118 | aliases = [] |
| 119 | for item in settingsDict[object]: |
| 120 | interfaces.append(item['Interface']) |
| 121 | for name, meta in item['Properties'].items(): |
| 122 | if 'Validation' in meta: |
| 123 | dict = meta['Validation'] |
| 124 | if dict['Type'] == "range": |
| 125 | validators[name] = (dict['Type'], dict['Validator'], dict['Unit']) |
| 126 | else: |
| 127 | validators[name] = (dict['Type'], dict['Validator']) |
| 128 | %> |
| 129 | % for index, intf in enumerate(interfaces): |
| 130 | using Iface${index} = ${get_setting_sdbusplus_type(intf)}; |
| 131 | <% aliases.append("Iface" + str(index)) %>\ |
| 132 | % endfor |
| 133 | <% |
Patrick Williams | 7c4181c | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 134 | parent = "sdbusplus::server::object_t" + "<" + ", ".join(aliases) + ">" |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 135 | %>\ |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 136 | using Parent = ${parent}; |
| 137 | |
| 138 | class Impl : public Parent |
| 139 | { |
| 140 | public: |
Patrick Williams | 7c4181c | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 141 | Impl(sdbusplus::bus_t& bus, const char* path): |
Patrick Williams | 74c4f3b | 2022-04-05 16:16:20 -0500 | [diff] [blame] | 142 | Parent(bus, path, Parent::action::defer_emit), |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 143 | path(path) |
| 144 | { |
| 145 | } |
| 146 | virtual ~Impl() = default; |
| 147 | |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 148 | void setInitialVersion(std::uint32_t v) |
| 149 | { |
| 150 | initialVersion = v; |
| 151 | } |
| 152 | |
| 153 | std::uint32_t getInitialVersion() const |
| 154 | { |
| 155 | return initialVersion; |
| 156 | } |
| 157 | |
| 158 | bool deserialize() |
| 159 | { |
| 160 | auto p = getFilePath(path); |
| 161 | if (fs::exists(p)) |
| 162 | { |
| 163 | std::ifstream is(p.c_str(), std::ios::in); |
| 164 | cereal::JSONInputArchive iarchive(is); |
| 165 | iarchive(*this); |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | void serialize() |
| 172 | { |
| 173 | auto p = getFilePath(path); |
| 174 | if (!fs::exists(p.parent_path())) |
| 175 | { |
| 176 | fs::create_directories(p.parent_path()); |
| 177 | } |
| 178 | std::ofstream os(p.c_str(), std::ios::binary); |
| 179 | cereal::JSONOutputArchive oarchive(os); |
| 180 | oarchive(*this); |
| 181 | } |
| 182 | |
| 183 | void removeFile() const |
| 184 | { |
| 185 | std::error_code ec; |
| 186 | fs::remove(getFilePath(path), ec); |
| 187 | } |
| 188 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 189 | % for index, item in enumerate(settingsDict[object]): |
| 190 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 191 | <% t = NamedElement(name=propName).camelCase %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 192 | <% fname = "validate" + propName %>\ |
| 193 | decltype(std::declval<Iface${index}>().${t}()) ${t}(decltype(std::declval<Iface${index}>().${t}()) value) override |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 194 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 195 | auto result = Iface${index}::${t}(); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 196 | if (value != result) |
| 197 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 198 | % if propName in validators: |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 199 | if (!${fname}(value)) |
| 200 | { |
| 201 | namespace error = |
| 202 | sdbusplus::xyz::openbmc_project::Common::Error; |
| 203 | namespace metadata = |
| 204 | phosphor::logging::xyz::openbmc_project::Common; |
| 205 | phosphor::logging::report<error::InvalidArgument>( |
| 206 | metadata::InvalidArgument::ARGUMENT_NAME("${t}"), |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 207 | % if validators[propName][0] != "regex": |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 208 | metadata::InvalidArgument::ARGUMENT_VALUE(std::to_string(value).c_str())); |
| 209 | % else: |
| 210 | metadata::InvalidArgument::ARGUMENT_VALUE(value.c_str())); |
| 211 | % endif |
| 212 | return result; |
| 213 | } |
| 214 | % endif |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 215 | result = Iface${index}::${t}(value); |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 216 | serialize(); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 217 | } |
| 218 | return result; |
| 219 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 220 | using Iface${index}::${t}; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 221 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 222 | % endfor |
Andrew Geissler | c15990a | 2017-07-06 11:36:31 -0500 | [diff] [blame] | 223 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 224 | private: |
| 225 | fs::path path; |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 226 | std::uint32_t initialVersion = 0; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 227 | % for index, item in enumerate(settingsDict[object]): |
| 228 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 229 | <% t = NamedElement(name=propName).camelCase %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 230 | <% fname = "validate" + propName %>\ |
| 231 | % if propName in validators: |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 232 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 233 | bool ${fname}(decltype(std::declval<Iface${index}>().${t}()) value) |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 234 | { |
| 235 | bool matched = false; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 236 | % if (validators[propName][0] == 'regex'): |
| 237 | std::regex regexToCheck("${validators[propName][1]}"); |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 238 | matched = std::regex_search(value, regexToCheck); |
| 239 | if (!matched) |
| 240 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 241 | std::string err = "Input parameter for ${propName} is invalid " |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 242 | "Input: " + value + " not in the format of this regex: " |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 243 | "${validators[propName][1]}"; |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 244 | using namespace phosphor::logging; |
| 245 | log<level::ERR>(err.c_str()); |
| 246 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 247 | % elif (validators[propName][0] == 'range'): |
| 248 | <% lowhigh = re.split('\.\.', validators[propName][1]) %>\ |
Jagpal Singh Gill | cfd49eb | 2023-04-23 23:09:09 -0700 | [diff] [blame] | 249 | % if lowhigh[0] == '0': |
| 250 | if (value <= ${lowhigh[1]}) |
| 251 | % else: |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 252 | if ((value <= ${lowhigh[1]}) && (value >= ${lowhigh[0]})) |
Jagpal Singh Gill | cfd49eb | 2023-04-23 23:09:09 -0700 | [diff] [blame] | 253 | % endif |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 254 | { |
| 255 | matched = true; |
| 256 | } |
| 257 | else |
| 258 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 259 | std::string err = "Input parameter for ${propName} is invalid " |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 260 | "Input: " + std::to_string(value) + "in uint: " |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 261 | "${validators[propName][2]} is not in range:${validators[propName][1]}"; |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 262 | using namespace phosphor::logging; |
| 263 | log<level::ERR>(err.c_str()); |
| 264 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 265 | % else: |
| 266 | <% assert("Unknown validation type: propName") %>\ |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 267 | % endif |
| 268 | return matched; |
| 269 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 270 | % endif |
| 271 | % endfor |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 272 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | template<class Archive> |
| 276 | void save(Archive& a, |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 277 | const Impl& setting, |
Jagpal Singh Gill | cfd49eb | 2023-04-23 23:09:09 -0700 | [diff] [blame] | 278 | [[maybe_unused]] const std::uint32_t version) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 279 | { |
| 280 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 281 | props = [] |
| 282 | for index, item in enumerate(settingsDict[object]): |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 283 | props.extend(item['Properties'].keys()) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 284 | %>\ |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 285 | ## Since the iface isn't saved, property names need to be unique on |
| 286 | ## the object path. This could be supported by providing unique |
| 287 | ## field names to make_nvp() if ever necessary. |
| 288 | % if len(set(props)) != len(props): |
| 289 | #error Duplicate property names on object path ${object} |
| 290 | %endif |
| 291 | <% |
| 292 | args = [] |
| 293 | for prop in props: |
| 294 | t = "setting." + NamedElement(name=prop).camelCase + "()" |
| 295 | args.append(f"cereal::make_nvp(\"{prop}\", {t})") |
| 296 | args = ", ".join(args) |
| 297 | %>\ |
| 298 | a(${args}); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | template<class Archive> |
| 302 | void load(Archive& a, |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 303 | Impl& setting, |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 304 | const std::uint32_t version) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 305 | { |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 306 | setting.setInitialVersion(version); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 307 | <% |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 308 | props = [] |
| 309 | for index, item in enumerate(settingsDict[object]): |
| 310 | for prop in item['Properties'].keys(): |
| 311 | t = "setting." + NamedElement(name=prop).camelCase + "()" |
| 312 | props.append({'prop' : prop, 'iface': item['Interface'], 'func': t}) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 313 | %>\ |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 314 | % for p in props: |
| 315 | decltype(${p['func']}) ${p['prop']}{}; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 316 | % endfor |
Matt Spinler | c2f84c7 | 2023-05-24 13:51:03 -0500 | [diff] [blame] | 317 | <% propList = ', '.join([p['prop'] for p in props]) %> |
| 318 | % if propList: |
| 319 | if (version < CLASS_VERSION_WITH_NVP) |
| 320 | { |
| 321 | a(${propList}); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | % for p in props: |
| 326 | try |
| 327 | { |
| 328 | a(CEREAL_NVP(${p['prop']})); |
| 329 | } |
| 330 | catch (const cereal::Exception& e) |
| 331 | { |
| 332 | lg2::info("Could not restore property ${p['prop']} on ${object}, setting to default value"); |
| 333 | ${p['prop']} = ${get_default_value(object, p['iface'], p['prop'])}; |
| 334 | } |
| 335 | % endfor |
| 336 | } |
Lei YU | c0ce992 | 2022-03-09 16:01:10 +0800 | [diff] [blame] | 337 | % endif |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 338 | <% props = [] %> |
| 339 | % for index, item in enumerate(settingsDict[object]): |
| 340 | % for prop, metaDict in item['Properties'].items(): |
| 341 | <% |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 342 | t = "setting." + NamedElement(name=prop).camelCase + "(" + prop + ")" |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 343 | %>\ |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 344 | ${t}; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 345 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 346 | % endfor |
| 347 | } |
| 348 | |
| 349 | % for n in reversed(ns): |
| 350 | } // namespace ${n} |
| 351 | % endfor |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 352 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 353 | % endfor |
| 354 | |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 355 | /** @class Manager |
| 356 | * |
| 357 | * @brief Compose settings objects and put them on the bus. |
| 358 | */ |
| 359 | class Manager |
| 360 | { |
| 361 | public: |
| 362 | Manager() = delete; |
| 363 | Manager(const Manager&) = delete; |
| 364 | Manager& operator=(const Manager&) = delete; |
| 365 | Manager(Manager&&) = delete; |
| 366 | Manager& operator=(Manager&&) = delete; |
| 367 | virtual ~Manager() = default; |
| 368 | |
| 369 | /** @brief Constructor to put settings objects on to the bus. |
| 370 | * @param[in] bus - Bus to attach to. |
| 371 | */ |
Patrick Williams | 7c4181c | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 372 | explicit Manager(sdbusplus::bus_t& bus) : |
Patrick Williams | 0f6903d | 2022-04-15 10:03:58 -0500 | [diff] [blame] | 373 | settings( |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 374 | std::make_tuple( |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 375 | % for index, path in enumerate(objects): |
| 376 | <% type = get_setting_type(path) + "::Impl" %>\ |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 377 | std::make_unique<${type}>( |
| 378 | bus, |
| 379 | % if index < len(settingsDict) - 1: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 380 | "${path}"), |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 381 | % else: |
Patrick Williams | 0f6903d | 2022-04-15 10:03:58 -0500 | [diff] [blame] | 382 | "${path}") |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 383 | % endif |
| 384 | % endfor |
Patrick Williams | 0f6903d | 2022-04-15 10:03:58 -0500 | [diff] [blame] | 385 | ) |
| 386 | ) |
| 387 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 388 | % for index, path in enumerate(objects): |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 389 | auto initSetting${index} = [&]() |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 390 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 391 | % for item in settingsDict[path]: |
| 392 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 393 | <% p = NamedElement(name=propName).camelCase %>\ |
Matt Spinler | ffdf865 | 2023-05-24 11:09:54 -0500 | [diff] [blame] | 394 | <% defaultValue = get_default_value(path, item['Interface'], propName) %>\ |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 395 | std::get<${index}>(settings)-> |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 396 | ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue}); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 397 | % endfor |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 398 | % endfor |
| 399 | }; |
| 400 | |
| 401 | try |
| 402 | { |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 403 | if (std::get<${index}>(settings)->deserialize()) |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 404 | { |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 405 | /* Update the archive to use name/value pairs if it isn't. */ |
| 406 | if (std::get<${index}>(settings)->getInitialVersion() < CLASS_VERSION_WITH_NVP) |
| 407 | { |
| 408 | std::get<${index}>(settings)->serialize(); |
| 409 | } |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 410 | } |
| 411 | else |
| 412 | { |
| 413 | initSetting${index}(); |
| 414 | } |
| 415 | } |
Patrick Williams | b6fa9bb | 2021-10-06 12:27:57 -0500 | [diff] [blame] | 416 | catch (const cereal::Exception& e) |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 417 | { |
| 418 | log<level::ERR>(e.what()); |
Matt Spinler | fb1ad7c | 2023-05-24 14:28:29 -0500 | [diff] [blame^] | 419 | std::get<${index}>(settings)->removeFile(); |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 420 | initSetting${index}(); |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 421 | } |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 422 | std::get<${index}>(settings)->emit_object_added(); |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 423 | |
| 424 | % endfor |
| 425 | } |
| 426 | |
| 427 | private: |
| 428 | /* @brief Composition of settings objects. */ |
| 429 | std::tuple< |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 430 | % for index, path in enumerate(objects): |
| 431 | <% type = get_setting_type(path) + "::Impl" %>\ |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 432 | % if index < len(settingsDict) - 1: |
| 433 | std::unique_ptr<${type}>, |
| 434 | % else: |
| 435 | std::unique_ptr<${type}>> settings; |
| 436 | % endif |
| 437 | % endfor |
| 438 | }; |
| 439 | |
| 440 | } // namespace settings |
| 441 | } // namespace phosphor |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 442 | |
| 443 | // Now register the class version with Cereal |
| 444 | % for object in objects: |
| 445 | <% |
| 446 | classname = "phosphor::settings" |
| 447 | ns = object.split('/') |
| 448 | ns.pop(0) |
| 449 | %>\ |
| 450 | % for n in ns: |
| 451 | <% |
| 452 | classname += "::" + n |
| 453 | %>\ |
| 454 | % endfor |
| 455 | CEREAL_CLASS_VERSION(${classname + "::Impl"}, CLASS_VERSION); |
| 456 | % endfor |