blob: 9e0b3fd3b206113438219d03d3c3952b94676b20 [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
Deepak Kodihalli5de09572017-05-16 23:53:40 -05007objects = list(settingsDict.viewkeys())
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -05008sdbusplus_namespaces = []
9sdbusplus_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>
37#include <fstream>
38#include <utility>
39#include <experimental/filesystem>
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -050040#include <regex>
41#include <phosphor-logging/elog.hpp>
42#include <phosphor-logging/elog-errors.hpp>
43#include <phosphor-logging/log.hpp>
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050044#include "config.h"
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -050045#include <xyz/openbmc_project/Common/error.hpp>
Tom Joseph4636e072017-09-24 20:47:24 +053046using namespace phosphor::logging;
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050047
48% for i in set(sdbusplus_includes):
Deepak Kodihalli5de09572017-05-16 23:53:40 -050049#include "${i}"
50% endfor
51
52% for object in objects:
Deepak Kodihallidb838622017-08-27 02:46:47 -050053 % for item in settingsDict[object]:
Deepak Kodihalli5de09572017-05-16 23:53:40 -050054<%
Deepak Kodihallidb838622017-08-27 02:46:47 -050055 ns = get_setting_sdbusplus_type(item['Interface'])
Deepak Kodihalli5de09572017-05-16 23:53:40 -050056 i = ns.rfind('::')
57 ns = ns[:i]
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050058 sdbusplus_namespaces.append(ns)
Deepak Kodihalli5de09572017-05-16 23:53:40 -050059%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -050060 % endfor
Deepak Kodihalli5de09572017-05-16 23:53:40 -050061% endfor
Deepak Kodihalli5de09572017-05-16 23:53:40 -050062
63namespace phosphor
64{
65namespace settings
66{
67
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050068namespace fs = std::experimental::filesystem;
69
Deepak Kodihalli242bc772017-08-04 02:47:54 -050070namespace 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__.
81constexpr auto fileSuffix = "__";
82
83}
84
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050085% for n in set(sdbusplus_namespaces):
86using namespace ${n};
87% endfor
88
89% for object in objects:
90<%
Deepak Kodihallidb838622017-08-27 02:46:47 -050091 ns = object.split('/')
92 ns.pop(0)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -050093%>\
94% for n in ns:
95namespace ${n}
96{
97% endfor
Deepak Kodihallidb838622017-08-27 02:46:47 -050098<%
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):
112using 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 Kodihalli7a6f2522017-06-23 23:05:47 -0500118using Parent = ${parent};
119
120class 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 Kodihallidb838622017-08-27 02:46:47 -0500130% 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 Kodihalli7a6f2522017-06-23 23:05:47 -0500135 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500136 auto result = Iface${index}::${t}();
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500137 if (value != result)
138 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500139 % if propName in validators:
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500140 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 Kodihallidb838622017-08-27 02:46:47 -0500148 % if validators[propName][0] != "regex":
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500149 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 Kodihalli7a6f2522017-06-23 23:05:47 -0500156 fs::path p(SETTINGS_PERSIST_PATH);
157 p /= path;
Deepak Kodihalli242bc772017-08-04 02:47:54 -0500158 p += persistent::fileSuffix;
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500159 fs::create_directories(p.parent_path());
160 std::ofstream os(p.c_str(), std::ios::binary);
161 cereal::JSONOutputArchive oarchive(os);
Deepak Kodihallidb838622017-08-27 02:46:47 -0500162 result = Iface${index}::${t}(value);
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500163 oarchive(*this);
164 }
165 return result;
166 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500167 using Iface${index}::${t};
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500168
Deepak Kodihallidb838622017-08-27 02:46:47 -0500169 % endfor
Andrew Geisslerc15990a2017-07-06 11:36:31 -0500170% endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500171 private:
172 fs::path path;
Deepak Kodihallidb838622017-08-27 02:46:47 -0500173% 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 Subhashchandran61d3b6a2017-07-25 09:36:54 -0500178
Deepak Kodihallidb838622017-08-27 02:46:47 -0500179 bool ${fname}(decltype(std::declval<Iface${index}>().${t}()) value)
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500180 {
181 bool matched = false;
Deepak Kodihallidb838622017-08-27 02:46:47 -0500182 % if (validators[propName][0] == 'regex'):
183 std::regex regexToCheck("${validators[propName][1]}");
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500184 matched = std::regex_search(value, regexToCheck);
185 if (!matched)
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: " + value + " not in the format of this regex: "
Deepak Kodihallidb838622017-08-27 02:46:47 -0500189 "${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 % elif (validators[propName][0] == 'range'):
194<% lowhigh = re.split('\.\.', validators[propName][1]) %>\
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500195 if ((value <= ${lowhigh[1]}) && (value >= ${lowhigh[0]}))
196 {
197 matched = true;
198 }
199 else
200 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500201 std::string err = "Input parameter for ${propName} is invalid "
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500202 "Input: " + std::to_string(value) + "in uint: "
Deepak Kodihallidb838622017-08-27 02:46:47 -0500203 "${validators[propName][2]} is not in range:${validators[propName][1]}";
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500204 using namespace phosphor::logging;
205 log<level::ERR>(err.c_str());
206 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500207 % else:
208 <% assert("Unknown validation type: propName") %>\
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500209 % endif
210 return matched;
211 }
Deepak Kodihallidb838622017-08-27 02:46:47 -0500212 % endif
213 % endfor
Dhruvaraj Subhashchandran61d3b6a2017-07-25 09:36:54 -0500214% endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500215};
216
217template<class Archive>
218void save(Archive& a,
219 const Impl& setting)
220{
221<%
Deepak Kodihallidb838622017-08-27 02:46:47 -0500222props = []
223for index, item in enumerate(settingsDict[object]):
224 intfProps = ["setting." + propName[:1].lower() + propName[1:] + "()" for \
225 propName, metaDict in item['Properties'].items()]
226 props.extend(intfProps)
227props = ', '.join(props)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500228%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500229 a(${props});
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500230}
231
232template<class Archive>
233void load(Archive& a,
234 Impl& setting)
235{
Deepak Kodihallidb838622017-08-27 02:46:47 -0500236<% props = [] %>\
237% for index, item in enumerate(settingsDict[object]):
238 % for prop, metaDict in item['Properties'].items():
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500239<%
Deepak Kodihallidb838622017-08-27 02:46:47 -0500240 t = "setting." + prop[:1].lower() + prop[1:] + "()"
241 props.append(prop)
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500242%>\
Deepak Kodihallidb838622017-08-27 02:46:47 -0500243 decltype(${t}) ${prop}{};
244 % endfor
245% endfor
246<% props = ', '.join(props) %>
247 a(${props});
248<% props = [] %>
249% for index, item in enumerate(settingsDict[object]):
250 % for prop, metaDict in item['Properties'].items():
251<%
252 t = "setting." + prop[:1].lower() + prop[1:] + "(" + prop + ")"
253%>\
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500254 ${t};
Deepak Kodihallidb838622017-08-27 02:46:47 -0500255 % endfor
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500256% endfor
257}
258
259% for n in reversed(ns):
260} // namespace ${n}
261% endfor
Deepak Kodihallidb838622017-08-27 02:46:47 -0500262
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500263% endfor
264
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500265/** @class Manager
266 *
267 * @brief Compose settings objects and put them on the bus.
268 */
269class Manager
270{
271 public:
272 Manager() = delete;
273 Manager(const Manager&) = delete;
274 Manager& operator=(const Manager&) = delete;
275 Manager(Manager&&) = delete;
276 Manager& operator=(Manager&&) = delete;
277 virtual ~Manager() = default;
278
279 /** @brief Constructor to put settings objects on to the bus.
280 * @param[in] bus - Bus to attach to.
281 */
282 Manager(sdbusplus::bus::bus& bus)
283 {
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500284 fs::path path{};
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500285 settings =
286 std::make_tuple(
Deepak Kodihallidb838622017-08-27 02:46:47 -0500287% for index, path in enumerate(objects):
288<% type = get_setting_type(path) + "::Impl" %>\
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500289 std::make_unique<${type}>(
290 bus,
291 % if index < len(settingsDict) - 1:
Deepak Kodihallidb838622017-08-27 02:46:47 -0500292 "${path}"),
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500293 % else:
Deepak Kodihallidb838622017-08-27 02:46:47 -0500294 "${path}"));
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500295 % endif
296% endfor
297
Deepak Kodihallidb838622017-08-27 02:46:47 -0500298% for index, path in enumerate(objects):
299 path = fs::path(SETTINGS_PERSIST_PATH) / "${path}";
Deepak Kodihalli242bc772017-08-04 02:47:54 -0500300 path += persistent::fileSuffix;
Tom Joseph4636e072017-09-24 20:47:24 +0530301 auto initSetting${index} = [&]()
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500302 {
Deepak Kodihallidb838622017-08-27 02:46:47 -0500303 % for item in settingsDict[path]:
304 % for propName, metaDict in item['Properties'].items():
305<% p = propName[:1].lower() + propName[1:] %>\
306<% defaultValue = metaDict['Default'] %>\
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500307 std::get<${index}>(settings)->
Tom Joseph4636e072017-09-24 20:47:24 +0530308 ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue});
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500309 % endfor
Tom Joseph4636e072017-09-24 20:47:24 +0530310% 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 }
326 catch (cereal::Exception& e)
327 {
328 log<level::ERR>(e.what());
329 fs::remove(path);
330 initSetting${index}();
Deepak Kodihallidb838622017-08-27 02:46:47 -0500331 }
Deepak Kodihalli7a6f2522017-06-23 23:05:47 -0500332 std::get<${index}>(settings)->emit_object_added();
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500333
334% endfor
335 }
336
337 private:
338 /* @brief Composition of settings objects. */
339 std::tuple<
Deepak Kodihallidb838622017-08-27 02:46:47 -0500340% for index, path in enumerate(objects):
341<% type = get_setting_type(path) + "::Impl" %>\
Deepak Kodihalli5de09572017-05-16 23:53:40 -0500342 % 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