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