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 |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 23 | %>\ |
| 24 | #pragma once |
| 25 | |
| 26 | % for object in objects: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 27 | % for item in settingsDict[object]: |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 28 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 29 | include = item['Interface'] |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 30 | include = include.replace('.', '/') |
| 31 | include = include + "/server.hpp" |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 32 | sdbusplus_includes.append(include) |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 33 | %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 34 | % endfor |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 35 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 36 | #include <cereal/archives/json.hpp> |
James Feist | 74e3be7 | 2019-02-15 09:59:42 -0800 | [diff] [blame] | 37 | #include <cereal/types/vector.hpp> |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 38 | #include <fstream> |
| 39 | #include <utility> |
| 40 | #include <experimental/filesystem> |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 41 | #include <regex> |
| 42 | #include <phosphor-logging/elog.hpp> |
| 43 | #include <phosphor-logging/elog-errors.hpp> |
| 44 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 45 | #include "config.h" |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 46 | #include <xyz/openbmc_project/Common/error.hpp> |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 47 | using namespace phosphor::logging; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 48 | |
| 49 | % for i in set(sdbusplus_includes): |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 50 | #include "${i}" |
| 51 | % endfor |
| 52 | |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 53 | namespace phosphor |
| 54 | { |
| 55 | namespace settings |
| 56 | { |
| 57 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 58 | namespace fs = std::experimental::filesystem; |
| 59 | |
Deepak Kodihalli | 242bc77 | 2017-08-04 02:47:54 -0500 | [diff] [blame] | 60 | namespace persistent |
| 61 | { |
| 62 | |
| 63 | // A setting d-bus object /foo/bar/baz is persisted in the filesystem with the |
| 64 | // same path. This eases re-construction of settings objects when we restore |
| 65 | // from the filesystem. This can be a problem though when you have two objects |
| 66 | // such as - /foo/bar and /foo/bar/baz. This is because 'bar' will be treated as |
| 67 | // a file in the first case, and a subdir in the second. To solve this, suffix |
| 68 | // files with a trailing __. The __ is a safe character sequence to use, because |
| 69 | // we won't have d-bus object paths ending with this. |
| 70 | // With this the objects would be persisted as - /foo/bar__ and /foo/bar/baz__. |
| 71 | constexpr auto fileSuffix = "__"; |
| 72 | |
| 73 | } |
| 74 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 75 | % for object in objects: |
| 76 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 77 | ns = object.split('/') |
| 78 | ns.pop(0) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 79 | %>\ |
| 80 | % for n in ns: |
| 81 | namespace ${n} |
| 82 | { |
| 83 | % endfor |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 84 | <% |
| 85 | interfaces = [] |
| 86 | aliases = [] |
| 87 | for item in settingsDict[object]: |
| 88 | interfaces.append(item['Interface']) |
| 89 | for name, meta in item['Properties'].items(): |
| 90 | if 'Validation' in meta: |
| 91 | dict = meta['Validation'] |
| 92 | if dict['Type'] == "range": |
| 93 | validators[name] = (dict['Type'], dict['Validator'], dict['Unit']) |
| 94 | else: |
| 95 | validators[name] = (dict['Type'], dict['Validator']) |
| 96 | %> |
| 97 | % for index, intf in enumerate(interfaces): |
| 98 | using Iface${index} = ${get_setting_sdbusplus_type(intf)}; |
| 99 | <% aliases.append("Iface" + str(index)) %>\ |
| 100 | % endfor |
| 101 | <% |
| 102 | parent = "sdbusplus::server::object::object" + "<" + ", ".join(aliases) + ">" |
| 103 | %>\ |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 104 | using Parent = ${parent}; |
| 105 | |
| 106 | class Impl : public Parent |
| 107 | { |
| 108 | public: |
| 109 | Impl(sdbusplus::bus::bus& bus, const char* path): |
Patrick Williams | 74c4f3b | 2022-04-05 16:16:20 -0500 | [diff] [blame^] | 110 | Parent(bus, path, Parent::action::defer_emit), |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 111 | path(path) |
| 112 | { |
| 113 | } |
| 114 | virtual ~Impl() = default; |
| 115 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 116 | % for index, item in enumerate(settingsDict[object]): |
| 117 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 118 | <% t = NamedElement(name=propName).camelCase %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 119 | <% fname = "validate" + propName %>\ |
| 120 | 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] | 121 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 122 | auto result = Iface${index}::${t}(); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 123 | if (value != result) |
| 124 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 125 | % if propName in validators: |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 126 | if (!${fname}(value)) |
| 127 | { |
| 128 | namespace error = |
| 129 | sdbusplus::xyz::openbmc_project::Common::Error; |
| 130 | namespace metadata = |
| 131 | phosphor::logging::xyz::openbmc_project::Common; |
| 132 | phosphor::logging::report<error::InvalidArgument>( |
| 133 | metadata::InvalidArgument::ARGUMENT_NAME("${t}"), |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 134 | % if validators[propName][0] != "regex": |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 135 | metadata::InvalidArgument::ARGUMENT_VALUE(std::to_string(value).c_str())); |
| 136 | % else: |
| 137 | metadata::InvalidArgument::ARGUMENT_VALUE(value.c_str())); |
| 138 | % endif |
| 139 | return result; |
| 140 | } |
| 141 | % endif |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 142 | fs::path p(SETTINGS_PERSIST_PATH); |
| 143 | p /= path; |
Deepak Kodihalli | 242bc77 | 2017-08-04 02:47:54 -0500 | [diff] [blame] | 144 | p += persistent::fileSuffix; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 145 | fs::create_directories(p.parent_path()); |
| 146 | std::ofstream os(p.c_str(), std::ios::binary); |
| 147 | cereal::JSONOutputArchive oarchive(os); |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 148 | result = Iface${index}::${t}(value); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 149 | oarchive(*this); |
| 150 | } |
| 151 | return result; |
| 152 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 153 | using Iface${index}::${t}; |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 154 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 155 | % endfor |
Andrew Geissler | c15990a | 2017-07-06 11:36:31 -0500 | [diff] [blame] | 156 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 157 | private: |
| 158 | fs::path path; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 159 | % for index, item in enumerate(settingsDict[object]): |
| 160 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 161 | <% t = NamedElement(name=propName).camelCase %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 162 | <% fname = "validate" + propName %>\ |
| 163 | % if propName in validators: |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 164 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 165 | bool ${fname}(decltype(std::declval<Iface${index}>().${t}()) value) |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 166 | { |
| 167 | bool matched = false; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 168 | % if (validators[propName][0] == 'regex'): |
| 169 | std::regex regexToCheck("${validators[propName][1]}"); |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 170 | matched = std::regex_search(value, regexToCheck); |
| 171 | if (!matched) |
| 172 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 173 | std::string err = "Input parameter for ${propName} is invalid " |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 174 | "Input: " + value + " not in the format of this regex: " |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 175 | "${validators[propName][1]}"; |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 176 | using namespace phosphor::logging; |
| 177 | log<level::ERR>(err.c_str()); |
| 178 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 179 | % elif (validators[propName][0] == 'range'): |
| 180 | <% lowhigh = re.split('\.\.', validators[propName][1]) %>\ |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 181 | if ((value <= ${lowhigh[1]}) && (value >= ${lowhigh[0]})) |
| 182 | { |
| 183 | matched = true; |
| 184 | } |
| 185 | else |
| 186 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 187 | std::string err = "Input parameter for ${propName} is invalid " |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 188 | "Input: " + std::to_string(value) + "in uint: " |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 189 | "${validators[propName][2]} is not in range:${validators[propName][1]}"; |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 190 | using namespace phosphor::logging; |
| 191 | log<level::ERR>(err.c_str()); |
| 192 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 193 | % else: |
| 194 | <% assert("Unknown validation type: propName") %>\ |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 195 | % endif |
| 196 | return matched; |
| 197 | } |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 198 | % endif |
| 199 | % endfor |
Dhruvaraj Subhashchandran | 61d3b6a | 2017-07-25 09:36:54 -0500 | [diff] [blame] | 200 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | template<class Archive> |
| 204 | void save(Archive& a, |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 205 | const Impl& setting, |
| 206 | const std::uint32_t version) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 207 | { |
| 208 | <% |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 209 | props = [] |
| 210 | for index, item in enumerate(settingsDict[object]): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 211 | intfProps = ["setting." + NamedElement(name=propName).camelCase + "()" for \ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 212 | propName, metaDict in item['Properties'].items()] |
| 213 | props.extend(intfProps) |
| 214 | props = ', '.join(props) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 215 | %>\ |
Lei YU | c0ce992 | 2022-03-09 16:01:10 +0800 | [diff] [blame] | 216 | % if props: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 217 | a(${props}); |
Lei YU | c0ce992 | 2022-03-09 16:01:10 +0800 | [diff] [blame] | 218 | % endif |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | template<class Archive> |
| 222 | void load(Archive& a, |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 223 | Impl& setting, |
| 224 | const std::uint32_t version) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 225 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 226 | <% props = [] %>\ |
| 227 | % for index, item in enumerate(settingsDict[object]): |
| 228 | % for prop, metaDict in item['Properties'].items(): |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 229 | <% |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 230 | t = "setting." + NamedElement(name=prop).camelCase + "()" |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 231 | props.append(prop) |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 232 | %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 233 | decltype(${t}) ${prop}{}; |
| 234 | % endfor |
| 235 | % endfor |
| 236 | <% props = ', '.join(props) %> |
Lei YU | c0ce992 | 2022-03-09 16:01:10 +0800 | [diff] [blame] | 237 | % if props: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 238 | a(${props}); |
Lei YU | c0ce992 | 2022-03-09 16:01:10 +0800 | [diff] [blame] | 239 | % endif |
| 240 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 241 | <% props = [] %> |
| 242 | % for index, item in enumerate(settingsDict[object]): |
| 243 | % for prop, metaDict in item['Properties'].items(): |
| 244 | <% |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 245 | t = "setting." + NamedElement(name=prop).camelCase + "(" + prop + ")" |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 246 | %>\ |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 247 | ${t}; |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 248 | % endfor |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 249 | % endfor |
| 250 | } |
| 251 | |
| 252 | % for n in reversed(ns): |
| 253 | } // namespace ${n} |
| 254 | % endfor |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 255 | |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 256 | % endfor |
| 257 | |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 258 | /** @class Manager |
| 259 | * |
| 260 | * @brief Compose settings objects and put them on the bus. |
| 261 | */ |
| 262 | class Manager |
| 263 | { |
| 264 | public: |
| 265 | Manager() = delete; |
| 266 | Manager(const Manager&) = delete; |
| 267 | Manager& operator=(const Manager&) = delete; |
| 268 | Manager(Manager&&) = delete; |
| 269 | Manager& operator=(Manager&&) = delete; |
| 270 | virtual ~Manager() = default; |
| 271 | |
| 272 | /** @brief Constructor to put settings objects on to the bus. |
| 273 | * @param[in] bus - Bus to attach to. |
| 274 | */ |
| 275 | Manager(sdbusplus::bus::bus& bus) |
| 276 | { |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 277 | fs::path path{}; |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 278 | settings = |
| 279 | std::make_tuple( |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 280 | % for index, path in enumerate(objects): |
| 281 | <% type = get_setting_type(path) + "::Impl" %>\ |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 282 | std::make_unique<${type}>( |
| 283 | bus, |
| 284 | % if index < len(settingsDict) - 1: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 285 | "${path}"), |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 286 | % else: |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 287 | "${path}")); |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 288 | % endif |
| 289 | % endfor |
| 290 | |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 291 | % for index, path in enumerate(objects): |
| 292 | path = fs::path(SETTINGS_PERSIST_PATH) / "${path}"; |
Deepak Kodihalli | 242bc77 | 2017-08-04 02:47:54 -0500 | [diff] [blame] | 293 | path += persistent::fileSuffix; |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 294 | auto initSetting${index} = [&]() |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 295 | { |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 296 | % for item in settingsDict[path]: |
| 297 | % for propName, metaDict in item['Properties'].items(): |
Patrick Williams | bcf9578 | 2021-05-05 16:20:32 -0500 | [diff] [blame] | 298 | <% p = NamedElement(name=propName).camelCase %>\ |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 299 | <% defaultValue = metaDict['Default'] %>\ |
Matt Spinler | 2355263 | 2021-05-14 09:24:09 -0500 | [diff] [blame] | 300 | % if isinstance(defaultValue, str) and not \ |
| 301 | defaultValue.startswith('"') and '::' in defaultValue: |
| 302 | <% ns = get_setting_sdbusplus_type(item['Interface']) |
| 303 | i = ns.rfind('::') |
| 304 | defaultValue = "{}::{}".format(ns[:i], defaultValue) |
| 305 | %>\ |
| 306 | % endif |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 307 | std::get<${index}>(settings)-> |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 308 | ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue}); |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 309 | % endfor |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 310 | % endfor |
| 311 | }; |
| 312 | |
| 313 | try |
| 314 | { |
| 315 | if (fs::exists(path)) |
| 316 | { |
| 317 | std::ifstream is(path.c_str(), std::ios::in); |
| 318 | cereal::JSONInputArchive iarchive(is); |
| 319 | iarchive(*std::get<${index}>(settings)); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | initSetting${index}(); |
| 324 | } |
| 325 | } |
Patrick Williams | b6fa9bb | 2021-10-06 12:27:57 -0500 | [diff] [blame] | 326 | catch (const cereal::Exception& e) |
Tom Joseph | 4636e07 | 2017-09-24 20:47:24 +0530 | [diff] [blame] | 327 | { |
| 328 | log<level::ERR>(e.what()); |
| 329 | fs::remove(path); |
| 330 | initSetting${index}(); |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 331 | } |
Deepak Kodihalli | 7a6f252 | 2017-06-23 23:05:47 -0500 | [diff] [blame] | 332 | std::get<${index}>(settings)->emit_object_added(); |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 333 | |
| 334 | % endfor |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | /* @brief Composition of settings objects. */ |
| 339 | std::tuple< |
Deepak Kodihalli | db83862 | 2017-08-27 02:46:47 -0500 | [diff] [blame] | 340 | % for index, path in enumerate(objects): |
| 341 | <% type = get_setting_type(path) + "::Impl" %>\ |
Deepak Kodihalli | 5de0957 | 2017-05-16 23:53:40 -0500 | [diff] [blame] | 342 | % if index < len(settingsDict) - 1: |
| 343 | std::unique_ptr<${type}>, |
| 344 | % else: |
| 345 | std::unique_ptr<${type}>> settings; |
| 346 | % endif |
| 347 | % endfor |
| 348 | }; |
| 349 | |
| 350 | } // namespace settings |
| 351 | } // namespace phosphor |
Vishwanatha Subbanna | a29a3eb | 2017-09-29 19:18:20 +0530 | [diff] [blame] | 352 | |
| 353 | // Now register the class version with Cereal |
| 354 | % for object in objects: |
| 355 | <% |
| 356 | classname = "phosphor::settings" |
| 357 | ns = object.split('/') |
| 358 | ns.pop(0) |
| 359 | %>\ |
| 360 | % for n in ns: |
| 361 | <% |
| 362 | classname += "::" + n |
| 363 | %>\ |
| 364 | % endfor |
| 365 | CEREAL_CLASS_VERSION(${classname + "::Impl"}, CLASS_VERSION); |
| 366 | % endfor |