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