blob: ba7d398dff1289c52ddc052e1bf1d22b4a8bfd0a [file] [log] [blame]
Patrick Williams04e007f2016-10-15 07:15:12 -05001#pragma once
Patrick Williamsc67e1e82020-11-04 12:39:24 -06002#include <limits>
William A. Kennington III4274c112018-11-26 09:50:13 -08003#include <map>
Patrick Venture263712f2018-04-16 14:19:08 -07004#include <sdbusplus/sdbus.hpp>
Patrick Williams0966ce82016-10-17 21:54:09 -05005#include <sdbusplus/server.hpp>
Patrick Williams4690d882020-10-08 11:09:58 -05006#include <sdbusplus/utility/dedup_variant.hpp>
7#include <string>
William A. Kennington III4274c112018-11-26 09:50:13 -08008#include <systemd/sd-bus.h>
9#include <tuple>
William A. Kennington III76f07322019-04-03 20:41:05 -070010% for m in interface.methods + interface.properties + interface.signals:
11${ m.cpp_prototype(loader, interface=interface, ptype='callback-hpp-includes') }
12% endfor
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080013<%
Ramin Izadpanahd2fc8cc2020-11-17 17:08:53 +000014 namespaces = interface.namespaces
15 classname = interface.classname
Patrick Williams9fa85522017-02-01 23:06:33 -060016
17 def setOfPropertyTypes():
18 return set(p.cppTypeParam(interface.name) for p in
19 interface.properties);
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080020%>
Patrick Williams98019e72021-04-30 20:22:29 -050021#ifndef SDBUSPP_NEW_CAMELCASE
22#define SDBUSPP_NEW_CAMELCASE 1
23#endif
24
Patrick Williams04e007f2016-10-15 07:15:12 -050025namespace sdbusplus
26{
Patrick Williams04e007f2016-10-15 07:15:12 -050027 % for s in namespaces:
28namespace ${s}
29{
30 % endfor
Patrick Williams7aa8a1e2016-11-11 13:30:33 -060031namespace server
32{
Patrick Williams04e007f2016-10-15 07:15:12 -050033
34class ${classname}
35{
36 public:
Patrick Williamsadc16822016-10-17 14:43:54 -050037 /* Define all of the basic class operations:
38 * Not allowed:
39 * - Default constructor to avoid nullptrs.
40 * - Copy operations due to internal unique_ptr.
Patrick Williams7904ed62016-11-29 22:02:37 -060041 * - Move operations due to 'this' being registered as the
42 * 'context' with sdbus.
Patrick Williamsadc16822016-10-17 14:43:54 -050043 * Allowed:
Patrick Williamsadc16822016-10-17 14:43:54 -050044 * - Destructor.
45 */
46 ${classname}() = delete;
47 ${classname}(const ${classname}&) = delete;
48 ${classname}& operator=(const ${classname}&) = delete;
Patrick Williams7904ed62016-11-29 22:02:37 -060049 ${classname}(${classname}&&) = delete;
50 ${classname}& operator=(${classname}&&) = delete;
Patrick Williamsadc16822016-10-17 14:43:54 -050051 virtual ~${classname}() = default;
52
53 /** @brief Constructor to put object onto bus at a dbus path.
54 * @param[in] bus - Bus to attach to.
55 * @param[in] path - Path to attach at.
56 */
57 ${classname}(bus::bus& bus, const char* path);
58
Patrick Williams0aa0dde2016-11-16 08:14:33 -060059 % for e in interface.enums:
60 enum class ${e.name}
61 {
62 % for v in e.values:
63 ${v.name},
64 % endfor
65 };
66 % endfor
67
Patrick Williams9fa85522017-02-01 23:06:33 -060068 % if interface.properties:
Patrick Williams4690d882020-10-08 11:09:58 -050069 using PropertiesVariant = sdbusplus::utility::dedup_variant<
Patrick Williams738e45d2020-07-02 16:38:38 -050070 ${",\n ".join(sorted(setOfPropertyTypes()))}>;
Patrick Williams9fa85522017-02-01 23:06:33 -060071
Patrick Williams4d47bf02017-02-01 23:16:23 -060072 /** @brief Constructor to initialize the object from a map of
73 * properties.
74 *
75 * @param[in] bus - Bus to attach to.
76 * @param[in] path - Path to attach at.
Gunnar Mills5f9874f2017-10-25 13:41:41 -050077 * @param[in] vals - Map of property name to value for initialization.
Patrick Williams4d47bf02017-02-01 23:16:23 -060078 */
79 ${classname}(bus::bus& bus, const char* path,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053080 const std::map<std::string, PropertiesVariant>& vals,
81 bool skipSignal = false);
Patrick Williams4d47bf02017-02-01 23:16:23 -060082
Patrick Williams9fa85522017-02-01 23:06:33 -060083 % endif
Patrick Williams04e007f2016-10-15 07:15:12 -050084 % for m in interface.methods:
85${ m.cpp_prototype(loader, interface=interface, ptype='header') }
86 % endfor
87
Patrick Williams5302a462016-10-18 11:11:51 -050088 % for s in interface.signals:
89${ s.cpp_prototype(loader, interface=interface, ptype='header') }
90 % endfor
91
Patrick Williamsb2cca012016-10-18 14:13:39 -050092 % for p in interface.properties:
93 /** Get value of ${p.name} */
Patrick Williams0aa0dde2016-11-16 08:14:33 -060094 virtual ${p.cppTypeParam(interface.name)} ${p.camelCase}() const;
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053095 /** Set value of ${p.name} with option to skip sending signal */
96 virtual ${p.cppTypeParam(interface.name)} \
97${p.camelCase}(${p.cppTypeParam(interface.name)} value,
98 bool skipSignal);
Patrick Williamsb2cca012016-10-18 14:13:39 -050099 /** Set value of ${p.name} */
Patrick Williams0aa0dde2016-11-16 08:14:33 -0600100 virtual ${p.cppTypeParam(interface.name)} \
101${p.camelCase}(${p.cppTypeParam(interface.name)} value);
Patrick Williamsb2cca012016-10-18 14:13:39 -0500102 % endfor
103
Patrick Williams9fa85522017-02-01 23:06:33 -0600104 % if interface.properties:
105 /** @brief Sets a property by name.
James Feistf7509062018-03-23 14:15:56 -0700106 * @param[in] _name - A string representation of the property name.
Patrick Williams9fa85522017-02-01 23:06:33 -0600107 * @param[in] val - A variant containing the value to set.
108 */
James Feistf7509062018-03-23 14:15:56 -0700109 void setPropertyByName(const std::string& _name,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +0530110 const PropertiesVariant& val,
111 bool skipSignal = false);
Patrick Williams9fa85522017-02-01 23:06:33 -0600112
Patrick Williamsdfa19092017-02-02 09:24:56 -0600113 /** @brief Gets a property by name.
James Feistf7509062018-03-23 14:15:56 -0700114 * @param[in] _name - A string representation of the property name.
Patrick Williamsdfa19092017-02-02 09:24:56 -0600115 * @return - A variant containing the value of the property.
116 */
James Feistf7509062018-03-23 14:15:56 -0700117 PropertiesVariant getPropertyByName(const std::string& _name);
Patrick Williamsdfa19092017-02-02 09:24:56 -0600118
Patrick Williams9fa85522017-02-01 23:06:33 -0600119 % endif
Patrick Williams386e8d22016-11-16 16:13:57 -0600120 % for e in interface.enums:
Patrick Williams5e001772020-01-24 14:56:29 -0600121 /** @brief Convert a string to an appropriate enum value.
122 * @param[in] s - The string to convert in the form of
123 * "${interface.name}.<value name>"
124 * @return - The enum value.
125 */
126 static ${e.name} convert${e.name}FromString(const std::string& s);
Patrick Williams978f77d2020-01-28 17:27:34 -0800127
128 /** @brief Convert an enum value to a string.
129 * @param[in] e - The enum to convert to a string.
130 * @return - The string conversion in the form of
131 * "${interface.name}.<value name>"
132 */
133 static std::string convert${e.name}ToString(${e.name} e);
Patrick Williams386e8d22016-11-16 16:13:57 -0600134 % endfor
135
Lei YUe57c38e2019-09-20 17:38:17 +0800136 /** @brief Emit interface added */
137 void emit_added()
138 {
139 _${"_".join(interface.name.split('.'))}_interface.emit_added();
140 }
141
142 /** @brief Emit interface removed */
143 void emit_removed()
144 {
145 _${"_".join(interface.name.split('.'))}_interface.emit_removed();
146 }
147
Lei YU14db20f2020-02-03 14:13:16 +0800148 static constexpr auto interface = "${interface.name}";
149
Patrick Williams04e007f2016-10-15 07:15:12 -0500150 private:
151 % for m in interface.methods:
152${ m.cpp_prototype(loader, interface=interface, ptype='callback-header') }
153 % endfor
154
Patrick Williamsb2cca012016-10-18 14:13:39 -0500155 % for p in interface.properties:
156 /** @brief sd-bus callback for get-property '${p.name}' */
157 static int _callback_get_${p.name}(
158 sd_bus*, const char*, const char*, const char*,
159 sd_bus_message*, void*, sd_bus_error*);
Patrick Williams4f299e02020-07-10 16:13:14 -0500160 % if 'const' not in p.flags and 'readonly' not in p.flags:
Patrick Williamsb2cca012016-10-18 14:13:39 -0500161 /** @brief sd-bus callback for set-property '${p.name}' */
162 static int _callback_set_${p.name}(
163 sd_bus*, const char*, const char*, const char*,
164 sd_bus_message*, void*, sd_bus_error*);
Patrick Williams4f299e02020-07-10 16:13:14 -0500165 % endif
Patrick Williamsb2cca012016-10-18 14:13:39 -0500166
167 % endfor
168
Patrick Williams0966ce82016-10-17 21:54:09 -0500169 static const vtable::vtable_t _vtable[];
Patrick Williams7aa8a1e2016-11-11 13:30:33 -0600170 sdbusplus::server::interface::interface
171 _${"_".join(interface.name.split('.'))}_interface;
Patrick Venture263712f2018-04-16 14:19:08 -0700172 sdbusplus::SdBusInterface *_intf;
Patrick Williamsadc16822016-10-17 14:43:54 -0500173
Patrick Williamsb2cca012016-10-18 14:13:39 -0500174 % for p in interface.properties:
Lei YU4d741892019-01-14 09:59:10 +0800175 % if p.defaultValue is not None:
Patrick Williams2adcefa2016-11-16 08:23:12 -0600176 ${p.cppTypeParam(interface.name)} _${p.camelCase} = \
177 % if p.is_enum():
178${p.cppTypeParam(interface.name)}::\
179 % endif
180${p.defaultValue};
Patrick Williamsb2cca012016-10-18 14:13:39 -0500181 % else:
Patrick Williams0aa0dde2016-11-16 08:14:33 -0600182 ${p.cppTypeParam(interface.name)} _${p.camelCase}{};
Patrick Williamsb2cca012016-10-18 14:13:39 -0500183 % endif
184 % endfor
185
Patrick Williams04e007f2016-10-15 07:15:12 -0500186};
187
Patrick Williams386e8d22016-11-16 16:13:57 -0600188 % for e in interface.enums:
Patrick Williams1013c5e2021-07-13 19:01:25 -0500189/* Specialization of sdbusplus::server::convertForMessage
Patrick Williams386e8d22016-11-16 16:13:57 -0600190 * for enum-type ${classname}::${e.name}.
191 *
192 * This converts from the enum to a constant c-string representing the enum.
193 *
194 * @param[in] e - Enum value to convert.
195 * @return C-string representing the name for the enum value.
196 */
Patrick Williams978f77d2020-01-28 17:27:34 -0800197inline std::string convertForMessage(${classname}::${e.name} e)
198{
199 return ${classname}::convert${e.name}ToString(e);
200}
Patrick Williams386e8d22016-11-16 16:13:57 -0600201 % endfor
202
Patrick Williams7aa8a1e2016-11-11 13:30:33 -0600203} // namespace server
Patrick Williams0e9ad0d2016-10-18 14:24:44 -0500204 % for s in reversed(namespaces):
Patrick Williams04e007f2016-10-15 07:15:12 -0500205} // namespace ${s}
206 % endfor
Patrick Williams06d43b82020-01-28 14:30:05 -0800207
208namespace message
209{
210namespace details
211{
212 % for e in interface.enums:
213template <>
Ramin Izadpanahd2fc8cc2020-11-17 17:08:53 +0000214inline auto convert_from_string<${interface.cppNamespace()}::${e.name}>(
Patrick Williams06d43b82020-01-28 14:30:05 -0800215 const std::string& value)
216{
Ramin Izadpanahd2fc8cc2020-11-17 17:08:53 +0000217 return ${interface.cppNamespace()}::convert${e.name}FromString(value);
Patrick Williams06d43b82020-01-28 14:30:05 -0800218}
Patrick Williams25207042020-01-28 18:00:07 -0800219
220template <>
Ramin Izadpanahd2fc8cc2020-11-17 17:08:53 +0000221inline std::string convert_to_string<${interface.cppNamespace()}::${e.name}>(
222 ${interface.cppNamespace()}::${e.name} value)
Patrick Williams25207042020-01-28 18:00:07 -0800223{
Ramin Izadpanahd2fc8cc2020-11-17 17:08:53 +0000224 return ${interface.cppNamespace()}::convert${e.name}ToString(value);
Patrick Williams25207042020-01-28 18:00:07 -0800225}
Patrick Williams06d43b82020-01-28 14:30:05 -0800226 % endfor
227} // namespace details
228} // namespace message
Patrick Williams04e007f2016-10-15 07:15:12 -0500229} // namespace sdbusplus