blob: d22fd2d041417312eedb8f83ac27b3a072ce455f [file] [log] [blame]
Deepak Kodihalli5de09572017-05-16 23:53:40 -05001## 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 Kodihalli5de09572017-05-16 23:53:40 -05004<%
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -05005import re
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -05006from collections import defaultdict
Patrick Williamsbcf95782021-05-05 16:20:32 -05007from sdbusplus.namedelement import NamedElement
Patrick Williams2b7152f2020-04-02 07:18:32 -05008objects = settingsDict.keys()
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -05009sdbusplus_includes = []
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050010props = defaultdict(list)
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -050011validators = defaultdict(tuple)
Deepak Kodihalli5de09572017-05-16 23:53:40 -050012
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050013def get_setting_sdbusplus_type(setting_intf):
Deepak Kodihalli5de09572017-05-16 23:53:40 -050014 setting = "sdbusplus::" + setting_intf.replace('.', '::')
15 i = setting.rfind('::')
16 setting = setting[:i] + '::server::' + setting[i+2:]
17 return setting
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050018
Deepak Kodihallidb838622017-08-27 02:46:47 -050019def get_setting_type(path):
20 path = path[1:]
21 path = path.replace('/', '::')
22 return path
Deepak Kodihalli5de09572017-05-16 23:53:40 -050023%>\
24#pragma once
25
26% for object in objects:
Deepak Kodihallidb838622017-08-27 02:46:47 -050027 % for item in settingsDict[object]:
Deepak Kodihalli5de09572017-05-16 23:53:40 -050028<%
Deepak Kodihallidb838622017-08-27 02:46:47 -050029 include = item['Interface']
Deepak Kodihalli5de09572017-05-16 23:53:40 -050030 include = include.replace('.', '/')
31 include = include + "/server.hpp"
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050032 sdbusplus_includes.append(include)
Deepak Kodihalli5de09572017-05-16 23:53:40 -050033%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -050034 % endfor
Deepak Kodihalli5de09572017-05-16 23:53:40 -050035% endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050036#include <cereal/archives/json.hpp>
James Feist74e3be72019-02-15 09:59:42 -080037#include <cereal/types/vector.hpp>
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050038#include <fstream>
39#include <utility>
40#include <experimental/filesystem>
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -050041#include <regex>
42#include <phosphor-logging/elog.hpp>
43#include <phosphor-logging/elog-errors.hpp>
44#include <phosphor-logging/log.hpp>
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050045#include "config.h"
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -050046#include <xyz/openbmc_project/Common/error.hpp>
Tom Joseph4636e072017-09-24 20:47:24 +053047using namespace phosphor::logging;
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050048
49% for i in set(sdbusplus_includes):
Deepak Kodihalli5de09572017-05-16 23:53:40 -050050#include "${i}"
51% endfor
52
Deepak Kodihalli5de09572017-05-16 23:53:40 -050053namespace phosphor
54{
55namespace settings
56{
57
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050058namespace fs = std::experimental::filesystem;
59
Deepak Kodihalli242bc772017-08-04 02:47:54 -050060namespace 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__.
71constexpr auto fileSuffix = "__";
72
73}
74
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050075% for object in objects:
76<%
Deepak Kodihallidb838622017-08-27 02:46:47 -050077 ns = object.split('/')
78 ns.pop(0)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050079%>\
80% for n in ns:
81namespace ${n}
82{
83% endfor
Deepak Kodihallidb838622017-08-27 02:46:47 -050084<%
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):
98using 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 Kodihalli7a6f2522017-06-23 23:05:47 -0500104using Parent = ${parent};
105
106class Impl : public Parent
107{
108 public:
109 Impl(sdbusplus::bus::bus& bus, const char* path):
110 Parent(bus, path, true),
111 path(path)
112 {
113 }
114 virtual ~Impl() = default;
115
Deepak Kodihallidb838622017-08-27 02:46:47 -0500116% for index, item in enumerate(settingsDict[object]):
117 % for propName, metaDict in item['Properties'].items():
Patrick Williamsbcf95782021-05-05 16:20:32 -0500118<% t = NamedElement(name=propName).camelCase %>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500119<% fname = "validate" + propName %>\
120 decltype(std::declval<Iface${index}>().${t}()) ${t}(decltype(std::declval<Iface${index}>().${t}()) value) override
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500121 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500122 auto result = Iface${index}::${t}();
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500123 if (value != result)
124 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500125 % if propName in validators:
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500126 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 Kodihallidb838622017-08-27 02:46:47 -0500134 % if validators[propName][0] != "regex":
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500135 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 Kodihalli7a6f2522017-06-23 23:05:47 -0500142 fs::path p(SETTINGS_PERSIST_PATH);
143 p /= path;
Deepak Kodihalli242bc772017-08-04 02:47:54 -0500144 p += persistent::fileSuffix;
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500145 fs::create_directories(p.parent_path());
146 std::ofstream os(p.c_str(), std::ios::binary);
147 cereal::JSONOutputArchive oarchive(os);
Deepak Kodihallidb838622017-08-27 02:46:47 -0500148 result = Iface${index}::${t}(value);
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500149 oarchive(*this);
150 }
151 return result;
152 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500153 using Iface${index}::${t};
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500154
Deepak Kodihallidb838622017-08-27 02:46:47 -0500155 % endfor
Andrew Geisslerc15990a2017-07-06 11:36:31 -0500156% endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500157 private:
158 fs::path path;
Deepak Kodihallidb838622017-08-27 02:46:47 -0500159% for index, item in enumerate(settingsDict[object]):
160 % for propName, metaDict in item['Properties'].items():
Patrick Williamsbcf95782021-05-05 16:20:32 -0500161<% t = NamedElement(name=propName).camelCase %>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500162<% fname = "validate" + propName %>\
163 % if propName in validators:
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500164
Deepak Kodihallidb838622017-08-27 02:46:47 -0500165 bool ${fname}(decltype(std::declval<Iface${index}>().${t}()) value)
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500166 {
167 bool matched = false;
Deepak Kodihallidb838622017-08-27 02:46:47 -0500168 % if (validators[propName][0] == 'regex'):
169 std::regex regexToCheck("${validators[propName][1]}");
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500170 matched = std::regex_search(value, regexToCheck);
171 if (!matched)
172 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500173 std::string err = "Input parameter for ${propName} is invalid "
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500174 "Input: " + value + " not in the format of this regex: "
Deepak Kodihallidb838622017-08-27 02:46:47 -0500175 "${validators[propName][1]}";
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500176 using namespace phosphor::logging;
177 log<level::ERR>(err.c_str());
178 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500179 % elif (validators[propName][0] == 'range'):
180<% lowhigh = re.split('\.\.', validators[propName][1]) %>\
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500181 if ((value <= ${lowhigh[1]}) && (value >= ${lowhigh[0]}))
182 {
183 matched = true;
184 }
185 else
186 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500187 std::string err = "Input parameter for ${propName} is invalid "
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500188 "Input: " + std::to_string(value) + "in uint: "
Deepak Kodihallidb838622017-08-27 02:46:47 -0500189 "${validators[propName][2]} is not in range:${validators[propName][1]}";
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500190 using namespace phosphor::logging;
191 log<level::ERR>(err.c_str());
192 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500193 % else:
194 <% assert("Unknown validation type: propName") %>\
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500195 % endif
196 return matched;
197 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500198 % endif
199 % endfor
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500200% endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500201};
202
203template<class Archive>
204void save(Archive& a,
Vishwanatha Subbannaa29a3eb2017-09-29 19:18:20 +0530205 const Impl& setting,
206 const std::uint32_t version)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500207{
208<%
Deepak Kodihallidb838622017-08-27 02:46:47 -0500209props = []
210for index, item in enumerate(settingsDict[object]):
Patrick Williamsbcf95782021-05-05 16:20:32 -0500211 intfProps = ["setting." + NamedElement(name=propName).camelCase + "()" for \
Deepak Kodihallidb838622017-08-27 02:46:47 -0500212 propName, metaDict in item['Properties'].items()]
213 props.extend(intfProps)
214props = ', '.join(props)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500215%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500216 a(${props});
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500217}
218
219template<class Archive>
220void load(Archive& a,
Vishwanatha Subbannaa29a3eb2017-09-29 19:18:20 +0530221 Impl& setting,
222 const std::uint32_t version)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500223{
Deepak Kodihallidb838622017-08-27 02:46:47 -0500224<% props = [] %>\
225% for index, item in enumerate(settingsDict[object]):
226 % for prop, metaDict in item['Properties'].items():
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500227<%
Patrick Williamsbcf95782021-05-05 16:20:32 -0500228 t = "setting." + NamedElement(name=prop).camelCase + "()"
Deepak Kodihallidb838622017-08-27 02:46:47 -0500229 props.append(prop)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500230%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500231 decltype(${t}) ${prop}{};
232 % endfor
233% endfor
234<% props = ', '.join(props) %>
235 a(${props});
236<% props = [] %>
237% for index, item in enumerate(settingsDict[object]):
238 % for prop, metaDict in item['Properties'].items():
239<%
Patrick Williamsbcf95782021-05-05 16:20:32 -0500240 t = "setting." + NamedElement(name=prop).camelCase + "(" + prop + ")"
Deepak Kodihallidb838622017-08-27 02:46:47 -0500241%>\
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500242 ${t};
Deepak Kodihallidb838622017-08-27 02:46:47 -0500243 % endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500244% endfor
245}
246
247% for n in reversed(ns):
248} // namespace ${n}
249% endfor
Deepak Kodihallidb838622017-08-27 02:46:47 -0500250
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500251% endfor
252
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500253/** @class Manager
254 *
255 * @brief Compose settings objects and put them on the bus.
256 */
257class Manager
258{
259 public:
260 Manager() = delete;
261 Manager(const Manager&) = delete;
262 Manager& operator=(const Manager&) = delete;
263 Manager(Manager&&) = delete;
264 Manager& operator=(Manager&&) = delete;
265 virtual ~Manager() = default;
266
267 /** @brief Constructor to put settings objects on to the bus.
268 * @param[in] bus - Bus to attach to.
269 */
270 Manager(sdbusplus::bus::bus& bus)
271 {
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500272 fs::path path{};
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500273 settings =
274 std::make_tuple(
Deepak Kodihallidb838622017-08-27 02:46:47 -0500275% for index, path in enumerate(objects):
276<% type = get_setting_type(path) + "::Impl" %>\
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500277 std::make_unique<${type}>(
278 bus,
279 % if index < len(settingsDict) - 1:
Deepak Kodihallidb838622017-08-27 02:46:47 -0500280 "${path}"),
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500281 % else:
Deepak Kodihallidb838622017-08-27 02:46:47 -0500282 "${path}"));
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500283 % endif
284% endfor
285
Deepak Kodihallidb838622017-08-27 02:46:47 -0500286% for index, path in enumerate(objects):
287 path = fs::path(SETTINGS_PERSIST_PATH) / "${path}";
Deepak Kodihalli242bc772017-08-04 02:47:54 -0500288 path += persistent::fileSuffix;
Tom Joseph4636e072017-09-24 20:47:24 +0530289 auto initSetting${index} = [&]()
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500290 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500291 % for item in settingsDict[path]:
292 % for propName, metaDict in item['Properties'].items():
Patrick Williamsbcf95782021-05-05 16:20:32 -0500293<% p = NamedElement(name=propName).camelCase %>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500294<% defaultValue = metaDict['Default'] %>\
Matt Spinler23552632021-05-14 09:24:09 -0500295% if isinstance(defaultValue, str) and not \
296 defaultValue.startswith('"') and '::' in defaultValue:
297<% ns = get_setting_sdbusplus_type(item['Interface'])
298i = ns.rfind('::')
299defaultValue = "{}::{}".format(ns[:i], defaultValue)
300%>\
301% endif
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500302 std::get<${index}>(settings)->
Tom Joseph4636e072017-09-24 20:47:24 +0530303 ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue});
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500304 % endfor
Tom Joseph4636e072017-09-24 20:47:24 +0530305% endfor
306 };
307
308 try
309 {
310 if (fs::exists(path))
311 {
312 std::ifstream is(path.c_str(), std::ios::in);
313 cereal::JSONInputArchive iarchive(is);
314 iarchive(*std::get<${index}>(settings));
315 }
316 else
317 {
318 initSetting${index}();
319 }
320 }
321 catch (cereal::Exception& e)
322 {
323 log<level::ERR>(e.what());
324 fs::remove(path);
325 initSetting${index}();
Deepak Kodihallidb838622017-08-27 02:46:47 -0500326 }
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500327 std::get<${index}>(settings)->emit_object_added();
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500328
329% endfor
330 }
331
332 private:
333 /* @brief Composition of settings objects. */
334 std::tuple<
Deepak Kodihallidb838622017-08-27 02:46:47 -0500335% for index, path in enumerate(objects):
336<% type = get_setting_type(path) + "::Impl" %>\
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500337 % if index < len(settingsDict) - 1:
338 std::unique_ptr<${type}>,
339 % else:
340 std::unique_ptr<${type}>> settings;
341 % endif
342% endfor
343};
344
345} // namespace settings
346} // namespace phosphor
Vishwanatha Subbannaa29a3eb2017-09-29 19:18:20 +0530347
348// Now register the class version with Cereal
349% for object in objects:
350<%
351 classname = "phosphor::settings"
352 ns = object.split('/')
353 ns.pop(0)
354%>\
355% for n in ns:
356<%
357 classname += "::" + n
358%>\
359% endfor
360CEREAL_CLASS_VERSION(${classname + "::Impl"}, CLASS_VERSION);
361% endfor