blob: ee6a31bd4822d05004e7e50af46994efd814f92b [file] [log] [blame]
Patrick Williams04e007f2016-10-15 07:15:12 -05001#pragma once
William A. Kennington III4274c112018-11-26 09:50:13 -08002#include <map>
3#include <string>
Patrick Venture263712f2018-04-16 14:19:08 -07004#include <sdbusplus/sdbus.hpp>
Patrick Williams0966ce82016-10-17 21:54:09 -05005#include <sdbusplus/server.hpp>
William A. Kennington III4274c112018-11-26 09:50:13 -08006#include <systemd/sd-bus.h>
7#include <tuple>
8#include <variant>
William A. Kennington III76f07322019-04-03 20:41:05 -07009% for m in interface.methods + interface.properties + interface.signals:
10${ m.cpp_prototype(loader, interface=interface, ptype='callback-hpp-includes') }
11% endfor
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080012<%
13 namespaces = interface.name.split('.')
14 classname = namespaces.pop()
Patrick Williams9fa85522017-02-01 23:06:33 -060015
16 def setOfPropertyTypes():
17 return set(p.cppTypeParam(interface.name) for p in
18 interface.properties);
Patrick Williams06d43b82020-01-28 14:30:05 -080019
20 def cppNamespace():
21 return "::".join(namespaces) + "::server::" + classname
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080022%>
Patrick Williams04e007f2016-10-15 07:15:12 -050023namespace sdbusplus
24{
Patrick Williams04e007f2016-10-15 07:15:12 -050025 % for s in namespaces:
26namespace ${s}
27{
28 % endfor
Patrick Williams7aa8a1e2016-11-11 13:30:33 -060029namespace server
30{
Patrick Williams04e007f2016-10-15 07:15:12 -050031
32class ${classname}
33{
34 public:
Patrick Williamsadc16822016-10-17 14:43:54 -050035 /* Define all of the basic class operations:
36 * Not allowed:
37 * - Default constructor to avoid nullptrs.
38 * - Copy operations due to internal unique_ptr.
Patrick Williams7904ed62016-11-29 22:02:37 -060039 * - Move operations due to 'this' being registered as the
40 * 'context' with sdbus.
Patrick Williamsadc16822016-10-17 14:43:54 -050041 * Allowed:
Patrick Williamsadc16822016-10-17 14:43:54 -050042 * - Destructor.
43 */
44 ${classname}() = delete;
45 ${classname}(const ${classname}&) = delete;
46 ${classname}& operator=(const ${classname}&) = delete;
Patrick Williams7904ed62016-11-29 22:02:37 -060047 ${classname}(${classname}&&) = delete;
48 ${classname}& operator=(${classname}&&) = delete;
Patrick Williamsadc16822016-10-17 14:43:54 -050049 virtual ~${classname}() = default;
50
51 /** @brief Constructor to put object onto bus at a dbus path.
52 * @param[in] bus - Bus to attach to.
53 * @param[in] path - Path to attach at.
54 */
55 ${classname}(bus::bus& bus, const char* path);
56
Patrick Williams0aa0dde2016-11-16 08:14:33 -060057 % for e in interface.enums:
58 enum class ${e.name}
59 {
60 % for v in e.values:
61 ${v.name},
62 % endfor
63 };
64 % endfor
65
Patrick Williams9fa85522017-02-01 23:06:33 -060066 % if interface.properties:
William A. Kennington III4274c112018-11-26 09:50:13 -080067 using PropertiesVariant = std::variant<
Patrick Williams9fa85522017-02-01 23:06:33 -060068 ${",\n ".join(setOfPropertyTypes())}>;
69
Patrick Williams4d47bf02017-02-01 23:16:23 -060070 /** @brief Constructor to initialize the object from a map of
71 * properties.
72 *
73 * @param[in] bus - Bus to attach to.
74 * @param[in] path - Path to attach at.
Gunnar Mills5f9874f2017-10-25 13:41:41 -050075 * @param[in] vals - Map of property name to value for initialization.
Patrick Williams4d47bf02017-02-01 23:16:23 -060076 */
77 ${classname}(bus::bus& bus, const char* path,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053078 const std::map<std::string, PropertiesVariant>& vals,
79 bool skipSignal = false);
Patrick Williams4d47bf02017-02-01 23:16:23 -060080
Patrick Williams9fa85522017-02-01 23:06:33 -060081 % endif
Patrick Williams04e007f2016-10-15 07:15:12 -050082 % for m in interface.methods:
83${ m.cpp_prototype(loader, interface=interface, ptype='header') }
84 % endfor
85
Patrick Williams5302a462016-10-18 11:11:51 -050086 % for s in interface.signals:
87${ s.cpp_prototype(loader, interface=interface, ptype='header') }
88 % endfor
89
Patrick Williamsb2cca012016-10-18 14:13:39 -050090 % for p in interface.properties:
91 /** Get value of ${p.name} */
Patrick Williams0aa0dde2016-11-16 08:14:33 -060092 virtual ${p.cppTypeParam(interface.name)} ${p.camelCase}() const;
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053093 /** Set value of ${p.name} with option to skip sending signal */
94 virtual ${p.cppTypeParam(interface.name)} \
95${p.camelCase}(${p.cppTypeParam(interface.name)} value,
96 bool skipSignal);
Patrick Williamsb2cca012016-10-18 14:13:39 -050097 /** Set value of ${p.name} */
Patrick Williams0aa0dde2016-11-16 08:14:33 -060098 virtual ${p.cppTypeParam(interface.name)} \
99${p.camelCase}(${p.cppTypeParam(interface.name)} value);
Patrick Williamsb2cca012016-10-18 14:13:39 -0500100 % endfor
101
Patrick Williams9fa85522017-02-01 23:06:33 -0600102 % if interface.properties:
103 /** @brief Sets a property by name.
James Feistf7509062018-03-23 14:15:56 -0700104 * @param[in] _name - A string representation of the property name.
Patrick Williams9fa85522017-02-01 23:06:33 -0600105 * @param[in] val - A variant containing the value to set.
106 */
James Feistf7509062018-03-23 14:15:56 -0700107 void setPropertyByName(const std::string& _name,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +0530108 const PropertiesVariant& val,
109 bool skipSignal = false);
Patrick Williams9fa85522017-02-01 23:06:33 -0600110
Patrick Williamsdfa19092017-02-02 09:24:56 -0600111 /** @brief Gets a property by name.
James Feistf7509062018-03-23 14:15:56 -0700112 * @param[in] _name - A string representation of the property name.
Patrick Williamsdfa19092017-02-02 09:24:56 -0600113 * @return - A variant containing the value of the property.
114 */
James Feistf7509062018-03-23 14:15:56 -0700115 PropertiesVariant getPropertyByName(const std::string& _name);
Patrick Williamsdfa19092017-02-02 09:24:56 -0600116
Patrick Williams9fa85522017-02-01 23:06:33 -0600117 % endif
Patrick Williams386e8d22016-11-16 16:13:57 -0600118 % for e in interface.enums:
Patrick Williams5e001772020-01-24 14:56:29 -0600119 /** @brief Convert a string to an appropriate enum value.
120 * @param[in] s - The string to convert in the form of
121 * "${interface.name}.<value name>"
122 * @return - The enum value.
123 */
124 static ${e.name} convert${e.name}FromString(const std::string& s);
Patrick Williams978f77d2020-01-28 17:27:34 -0800125
126 /** @brief Convert an enum value to a string.
127 * @param[in] e - The enum to convert to a string.
128 * @return - The string conversion in the form of
129 * "${interface.name}.<value name>"
130 */
131 static std::string convert${e.name}ToString(${e.name} e);
Patrick Williams386e8d22016-11-16 16:13:57 -0600132 % endfor
133
Lei YUe57c38e2019-09-20 17:38:17 +0800134 /** @brief Emit interface added */
135 void emit_added()
136 {
137 _${"_".join(interface.name.split('.'))}_interface.emit_added();
138 }
139
140 /** @brief Emit interface removed */
141 void emit_removed()
142 {
143 _${"_".join(interface.name.split('.'))}_interface.emit_removed();
144 }
145
Lei YU14db20f2020-02-03 14:13:16 +0800146 static constexpr auto interface = "${interface.name}";
147
Patrick Williams04e007f2016-10-15 07:15:12 -0500148 private:
149 % for m in interface.methods:
150${ m.cpp_prototype(loader, interface=interface, ptype='callback-header') }
151 % endfor
152
Patrick Williamsb2cca012016-10-18 14:13:39 -0500153 % for p in interface.properties:
154 /** @brief sd-bus callback for get-property '${p.name}' */
155 static int _callback_get_${p.name}(
156 sd_bus*, const char*, const char*, const char*,
157 sd_bus_message*, void*, sd_bus_error*);
158 /** @brief sd-bus callback for set-property '${p.name}' */
159 static int _callback_set_${p.name}(
160 sd_bus*, const char*, const char*, const char*,
161 sd_bus_message*, void*, sd_bus_error*);
162
163 % endfor
164
Patrick Williams0966ce82016-10-17 21:54:09 -0500165 static const vtable::vtable_t _vtable[];
Patrick Williams7aa8a1e2016-11-11 13:30:33 -0600166 sdbusplus::server::interface::interface
167 _${"_".join(interface.name.split('.'))}_interface;
Patrick Venture263712f2018-04-16 14:19:08 -0700168 sdbusplus::SdBusInterface *_intf;
Patrick Williamsadc16822016-10-17 14:43:54 -0500169
Patrick Williamsb2cca012016-10-18 14:13:39 -0500170 % for p in interface.properties:
Lei YU4d741892019-01-14 09:59:10 +0800171 % if p.defaultValue is not None:
Patrick Williams2adcefa2016-11-16 08:23:12 -0600172 ${p.cppTypeParam(interface.name)} _${p.camelCase} = \
173 % if p.is_enum():
174${p.cppTypeParam(interface.name)}::\
175 % endif
176${p.defaultValue};
Patrick Williamsb2cca012016-10-18 14:13:39 -0500177 % else:
Patrick Williams0aa0dde2016-11-16 08:14:33 -0600178 ${p.cppTypeParam(interface.name)} _${p.camelCase}{};
Patrick Williamsb2cca012016-10-18 14:13:39 -0500179 % endif
180 % endfor
181
Patrick Williams04e007f2016-10-15 07:15:12 -0500182};
183
Patrick Williams386e8d22016-11-16 16:13:57 -0600184 % for e in interface.enums:
185/* Specialization of sdbusplus::server::bindings::details::convertForMessage
186 * for enum-type ${classname}::${e.name}.
187 *
188 * This converts from the enum to a constant c-string representing the enum.
189 *
190 * @param[in] e - Enum value to convert.
191 * @return C-string representing the name for the enum value.
192 */
Patrick Williams978f77d2020-01-28 17:27:34 -0800193inline std::string convertForMessage(${classname}::${e.name} e)
194{
195 return ${classname}::convert${e.name}ToString(e);
196}
Patrick Williams386e8d22016-11-16 16:13:57 -0600197 % endfor
198
Patrick Williams7aa8a1e2016-11-11 13:30:33 -0600199} // namespace server
Patrick Williams0e9ad0d2016-10-18 14:24:44 -0500200 % for s in reversed(namespaces):
Patrick Williams04e007f2016-10-15 07:15:12 -0500201} // namespace ${s}
202 % endfor
Patrick Williams06d43b82020-01-28 14:30:05 -0800203
204namespace message
205{
206namespace details
207{
208 % for e in interface.enums:
209template <>
210inline auto convert_from_string<${cppNamespace()}::${e.name}>(
211 const std::string& value)
212{
213 return ${cppNamespace()}::convert${e.name}FromString(value);
214}
Patrick Williams25207042020-01-28 18:00:07 -0800215
216template <>
217inline std::string convert_to_string<${cppNamespace()}::${e.name}>(
218 ${cppNamespace()}::${e.name} value)
219{
220 return ${cppNamespace()}::convert${e.name}ToString(value);
221}
Patrick Williams06d43b82020-01-28 14:30:05 -0800222 % endfor
223} // namespace details
224} // namespace message
Patrick Williams04e007f2016-10-15 07:15:12 -0500225} // namespace sdbusplus