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