Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Williams | c67e1e8 | 2020-11-04 12:39:24 -0600 | [diff] [blame] | 2 | #include <limits> |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 3 | #include <map> |
Patrick Venture | 263712f | 2018-04-16 14:19:08 -0700 | [diff] [blame] | 4 | #include <sdbusplus/sdbus.hpp> |
Patrick Williams | 0966ce8 | 2016-10-17 21:54:09 -0500 | [diff] [blame] | 5 | #include <sdbusplus/server.hpp> |
Patrick Williams | 4690d88 | 2020-10-08 11:09:58 -0500 | [diff] [blame] | 6 | #include <sdbusplus/utility/dedup_variant.hpp> |
| 7 | #include <string> |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 8 | #include <systemd/sd-bus.h> |
| 9 | #include <tuple> |
William A. Kennington III | 76f0732 | 2019-04-03 20:41:05 -0700 | [diff] [blame] | 10 | % for m in interface.methods + interface.properties + interface.signals: |
| 11 | ${ m.cpp_prototype(loader, interface=interface, ptype='callback-hpp-includes') } |
| 12 | % endfor |
Abhishek Pandit | aa71a3e | 2016-11-10 13:33:17 -0800 | [diff] [blame] | 13 | <% |
Ramin Izadpanah | d2fc8cc | 2020-11-17 17:08:53 +0000 | [diff] [blame] | 14 | namespaces = interface.namespaces |
| 15 | classname = interface.classname |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 16 | |
| 17 | def setOfPropertyTypes(): |
| 18 | return set(p.cppTypeParam(interface.name) for p in |
| 19 | interface.properties); |
Abhishek Pandit | aa71a3e | 2016-11-10 13:33:17 -0800 | [diff] [blame] | 20 | %> |
Patrick Williams | 98019e7 | 2021-04-30 20:22:29 -0500 | [diff] [blame] | 21 | #ifndef SDBUSPP_NEW_CAMELCASE |
| 22 | #define SDBUSPP_NEW_CAMELCASE 1 |
| 23 | #endif |
| 24 | |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 25 | namespace sdbusplus |
| 26 | { |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 27 | % for s in namespaces: |
| 28 | namespace ${s} |
| 29 | { |
| 30 | % endfor |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 31 | namespace server |
| 32 | { |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 33 | |
| 34 | class ${classname} |
| 35 | { |
| 36 | public: |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 37 | /* 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 Williams | 7904ed6 | 2016-11-29 22:02:37 -0600 | [diff] [blame] | 41 | * - Move operations due to 'this' being registered as the |
| 42 | * 'context' with sdbus. |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 43 | * Allowed: |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 44 | * - Destructor. |
| 45 | */ |
| 46 | ${classname}() = delete; |
| 47 | ${classname}(const ${classname}&) = delete; |
| 48 | ${classname}& operator=(const ${classname}&) = delete; |
Patrick Williams | 7904ed6 | 2016-11-29 22:02:37 -0600 | [diff] [blame] | 49 | ${classname}(${classname}&&) = delete; |
| 50 | ${classname}& operator=(${classname}&&) = delete; |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 51 | 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 Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 59 | % 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 Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 68 | % if interface.properties: |
Patrick Williams | 4690d88 | 2020-10-08 11:09:58 -0500 | [diff] [blame] | 69 | using PropertiesVariant = sdbusplus::utility::dedup_variant< |
Patrick Williams | 738e45d | 2020-07-02 16:38:38 -0500 | [diff] [blame] | 70 | ${",\n ".join(sorted(setOfPropertyTypes()))}>; |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 71 | |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 72 | /** @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 Mills | 5f9874f | 2017-10-25 13:41:41 -0500 | [diff] [blame] | 77 | * @param[in] vals - Map of property name to value for initialization. |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 78 | */ |
| 79 | ${classname}(bus::bus& bus, const char* path, |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame] | 80 | const std::map<std::string, PropertiesVariant>& vals, |
| 81 | bool skipSignal = false); |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 82 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 83 | % endif |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 84 | % for m in interface.methods: |
| 85 | ${ m.cpp_prototype(loader, interface=interface, ptype='header') } |
| 86 | % endfor |
| 87 | |
Patrick Williams | 5302a46 | 2016-10-18 11:11:51 -0500 | [diff] [blame] | 88 | % for s in interface.signals: |
| 89 | ${ s.cpp_prototype(loader, interface=interface, ptype='header') } |
| 90 | % endfor |
| 91 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 92 | % for p in interface.properties: |
| 93 | /** Get value of ${p.name} */ |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 94 | virtual ${p.cppTypeParam(interface.name)} ${p.camelCase}() const; |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame] | 95 | /** 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 Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 99 | /** Set value of ${p.name} */ |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 100 | virtual ${p.cppTypeParam(interface.name)} \ |
| 101 | ${p.camelCase}(${p.cppTypeParam(interface.name)} value); |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 102 | % endfor |
| 103 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 104 | % if interface.properties: |
| 105 | /** @brief Sets a property by name. |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 106 | * @param[in] _name - A string representation of the property name. |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 107 | * @param[in] val - A variant containing the value to set. |
| 108 | */ |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 109 | void setPropertyByName(const std::string& _name, |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame] | 110 | const PropertiesVariant& val, |
| 111 | bool skipSignal = false); |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 112 | |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 113 | /** @brief Gets a property by name. |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 114 | * @param[in] _name - A string representation of the property name. |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 115 | * @return - A variant containing the value of the property. |
| 116 | */ |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 117 | PropertiesVariant getPropertyByName(const std::string& _name); |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 118 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 119 | % endif |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 120 | % for e in interface.enums: |
Patrick Williams | 5e00177 | 2020-01-24 14:56:29 -0600 | [diff] [blame] | 121 | /** @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 Williams | 978f77d | 2020-01-28 17:27:34 -0800 | [diff] [blame] | 127 | |
| 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 Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 134 | % endfor |
| 135 | |
Lei YU | e57c38e | 2019-09-20 17:38:17 +0800 | [diff] [blame] | 136 | /** @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 YU | 14db20f | 2020-02-03 14:13:16 +0800 | [diff] [blame] | 148 | static constexpr auto interface = "${interface.name}"; |
| 149 | |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 150 | private: |
| 151 | % for m in interface.methods: |
| 152 | ${ m.cpp_prototype(loader, interface=interface, ptype='callback-header') } |
| 153 | % endfor |
| 154 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 155 | % 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 Williams | 4f299e0 | 2020-07-10 16:13:14 -0500 | [diff] [blame] | 160 | % if 'const' not in p.flags and 'readonly' not in p.flags: |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 161 | /** @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 Williams | 4f299e0 | 2020-07-10 16:13:14 -0500 | [diff] [blame] | 165 | % endif |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 166 | |
| 167 | % endfor |
| 168 | |
Patrick Williams | 0966ce8 | 2016-10-17 21:54:09 -0500 | [diff] [blame] | 169 | static const vtable::vtable_t _vtable[]; |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 170 | sdbusplus::server::interface::interface |
| 171 | _${"_".join(interface.name.split('.'))}_interface; |
Patrick Venture | 263712f | 2018-04-16 14:19:08 -0700 | [diff] [blame] | 172 | sdbusplus::SdBusInterface *_intf; |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 173 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 174 | % for p in interface.properties: |
Lei YU | 4d74189 | 2019-01-14 09:59:10 +0800 | [diff] [blame] | 175 | % if p.defaultValue is not None: |
Patrick Williams | 2adcefa | 2016-11-16 08:23:12 -0600 | [diff] [blame] | 176 | ${p.cppTypeParam(interface.name)} _${p.camelCase} = \ |
| 177 | % if p.is_enum(): |
| 178 | ${p.cppTypeParam(interface.name)}::\ |
| 179 | % endif |
| 180 | ${p.defaultValue}; |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 181 | % else: |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 182 | ${p.cppTypeParam(interface.name)} _${p.camelCase}{}; |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 183 | % endif |
| 184 | % endfor |
| 185 | |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 186 | }; |
| 187 | |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 188 | % for e in interface.enums: |
Patrick Williams | 1013c5e | 2021-07-13 19:01:25 -0500 | [diff] [blame] | 189 | /* Specialization of sdbusplus::server::convertForMessage |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 190 | * 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 Williams | 978f77d | 2020-01-28 17:27:34 -0800 | [diff] [blame] | 197 | inline std::string convertForMessage(${classname}::${e.name} e) |
| 198 | { |
| 199 | return ${classname}::convert${e.name}ToString(e); |
| 200 | } |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 201 | % endfor |
| 202 | |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 203 | } // namespace server |
Patrick Williams | 0e9ad0d | 2016-10-18 14:24:44 -0500 | [diff] [blame] | 204 | % for s in reversed(namespaces): |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 205 | } // namespace ${s} |
| 206 | % endfor |
Patrick Williams | 06d43b8 | 2020-01-28 14:30:05 -0800 | [diff] [blame] | 207 | |
| 208 | namespace message |
| 209 | { |
| 210 | namespace details |
| 211 | { |
| 212 | % for e in interface.enums: |
| 213 | template <> |
Ramin Izadpanah | d2fc8cc | 2020-11-17 17:08:53 +0000 | [diff] [blame] | 214 | inline auto convert_from_string<${interface.cppNamespace()}::${e.name}>( |
Patrick Williams | 06d43b8 | 2020-01-28 14:30:05 -0800 | [diff] [blame] | 215 | const std::string& value) |
| 216 | { |
Ramin Izadpanah | d2fc8cc | 2020-11-17 17:08:53 +0000 | [diff] [blame] | 217 | return ${interface.cppNamespace()}::convert${e.name}FromString(value); |
Patrick Williams | 06d43b8 | 2020-01-28 14:30:05 -0800 | [diff] [blame] | 218 | } |
Patrick Williams | 2520704 | 2020-01-28 18:00:07 -0800 | [diff] [blame] | 219 | |
| 220 | template <> |
Ramin Izadpanah | d2fc8cc | 2020-11-17 17:08:53 +0000 | [diff] [blame] | 221 | inline std::string convert_to_string<${interface.cppNamespace()}::${e.name}>( |
| 222 | ${interface.cppNamespace()}::${e.name} value) |
Patrick Williams | 2520704 | 2020-01-28 18:00:07 -0800 | [diff] [blame] | 223 | { |
Ramin Izadpanah | d2fc8cc | 2020-11-17 17:08:53 +0000 | [diff] [blame] | 224 | return ${interface.cppNamespace()}::convert${e.name}ToString(value); |
Patrick Williams | 2520704 | 2020-01-28 18:00:07 -0800 | [diff] [blame] | 225 | } |
Patrick Williams | 06d43b8 | 2020-01-28 14:30:05 -0800 | [diff] [blame] | 226 | % endfor |
| 227 | } // namespace details |
| 228 | } // namespace message |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 229 | } // namespace sdbusplus |