Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <tuple> |
| 3 | #include <systemd/sd-bus.h> |
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> |
Abhishek Pandit | aa71a3e | 2016-11-10 13:33:17 -0800 | [diff] [blame] | 6 | <% |
| 7 | namespaces = interface.name.split('.') |
| 8 | classname = namespaces.pop() |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 9 | |
| 10 | def setOfPropertyTypes(): |
| 11 | return set(p.cppTypeParam(interface.name) for p in |
| 12 | interface.properties); |
Abhishek Pandit | aa71a3e | 2016-11-10 13:33:17 -0800 | [diff] [blame] | 13 | %> |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 14 | namespace sdbusplus |
| 15 | { |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 16 | % for s in namespaces: |
| 17 | namespace ${s} |
| 18 | { |
| 19 | % endfor |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 20 | namespace server |
| 21 | { |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 22 | |
| 23 | class ${classname} |
| 24 | { |
| 25 | public: |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 26 | /* Define all of the basic class operations: |
| 27 | * Not allowed: |
| 28 | * - Default constructor to avoid nullptrs. |
| 29 | * - Copy operations due to internal unique_ptr. |
Patrick Williams | 7904ed6 | 2016-11-29 22:02:37 -0600 | [diff] [blame] | 30 | * - Move operations due to 'this' being registered as the |
| 31 | * 'context' with sdbus. |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 32 | * Allowed: |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 33 | * - Destructor. |
| 34 | */ |
| 35 | ${classname}() = delete; |
| 36 | ${classname}(const ${classname}&) = delete; |
| 37 | ${classname}& operator=(const ${classname}&) = delete; |
Patrick Williams | 7904ed6 | 2016-11-29 22:02:37 -0600 | [diff] [blame] | 38 | ${classname}(${classname}&&) = delete; |
| 39 | ${classname}& operator=(${classname}&&) = delete; |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 40 | virtual ~${classname}() = default; |
| 41 | |
| 42 | /** @brief Constructor to put object onto bus at a dbus path. |
| 43 | * @param[in] bus - Bus to attach to. |
| 44 | * @param[in] path - Path to attach at. |
| 45 | */ |
| 46 | ${classname}(bus::bus& bus, const char* path); |
| 47 | |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 48 | % for e in interface.enums: |
| 49 | enum class ${e.name} |
| 50 | { |
| 51 | % for v in e.values: |
| 52 | ${v.name}, |
| 53 | % endfor |
| 54 | }; |
| 55 | % endfor |
| 56 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 57 | % if interface.properties: |
| 58 | using PropertiesVariant = sdbusplus::message::variant< |
| 59 | ${",\n ".join(setOfPropertyTypes())}>; |
| 60 | |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 61 | /** @brief Constructor to initialize the object from a map of |
| 62 | * properties. |
| 63 | * |
| 64 | * @param[in] bus - Bus to attach to. |
| 65 | * @param[in] path - Path to attach at. |
Gunnar Mills | 5f9874f | 2017-10-25 13:41:41 -0500 | [diff] [blame] | 66 | * @param[in] vals - Map of property name to value for initialization. |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 67 | */ |
| 68 | ${classname}(bus::bus& bus, const char* path, |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame^] | 69 | const std::map<std::string, PropertiesVariant>& vals, |
| 70 | bool skipSignal = false); |
Patrick Williams | 4d47bf0 | 2017-02-01 23:16:23 -0600 | [diff] [blame] | 71 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 72 | % endif |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 73 | % for m in interface.methods: |
| 74 | ${ m.cpp_prototype(loader, interface=interface, ptype='header') } |
| 75 | % endfor |
| 76 | |
Patrick Williams | 5302a46 | 2016-10-18 11:11:51 -0500 | [diff] [blame] | 77 | % for s in interface.signals: |
| 78 | ${ s.cpp_prototype(loader, interface=interface, ptype='header') } |
| 79 | % endfor |
| 80 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 81 | % for p in interface.properties: |
| 82 | /** Get value of ${p.name} */ |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 83 | virtual ${p.cppTypeParam(interface.name)} ${p.camelCase}() const; |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame^] | 84 | /** Set value of ${p.name} with option to skip sending signal */ |
| 85 | virtual ${p.cppTypeParam(interface.name)} \ |
| 86 | ${p.camelCase}(${p.cppTypeParam(interface.name)} value, |
| 87 | bool skipSignal); |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 88 | /** Set value of ${p.name} */ |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 89 | virtual ${p.cppTypeParam(interface.name)} \ |
| 90 | ${p.camelCase}(${p.cppTypeParam(interface.name)} value); |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 91 | % endfor |
| 92 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 93 | % if interface.properties: |
| 94 | /** @brief Sets a property by name. |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 95 | * @param[in] _name - A string representation of the property name. |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 96 | * @param[in] val - A variant containing the value to set. |
| 97 | */ |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 98 | void setPropertyByName(const std::string& _name, |
Richard Marian Thomaiyar | 261fe75 | 2018-06-18 13:53:56 +0530 | [diff] [blame^] | 99 | const PropertiesVariant& val, |
| 100 | bool skipSignal = false); |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 101 | |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 102 | /** @brief Gets a property by name. |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 103 | * @param[in] _name - A string representation of the property name. |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 104 | * @return - A variant containing the value of the property. |
| 105 | */ |
James Feist | f750906 | 2018-03-23 14:15:56 -0700 | [diff] [blame] | 106 | PropertiesVariant getPropertyByName(const std::string& _name); |
Patrick Williams | dfa1909 | 2017-02-02 09:24:56 -0600 | [diff] [blame] | 107 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 108 | % endif |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 109 | % for e in interface.enums: |
| 110 | /** @brief Convert a string to an appropriate enum value. |
| 111 | * @param[in] s - The string to convert in the form of |
| 112 | * "${interface.name}.<value name>" |
| 113 | * @return - The enum value. |
| 114 | */ |
Patrick Williams | 93c246c | 2017-06-16 05:04:01 -0500 | [diff] [blame] | 115 | static ${e.name} convert${e.name}FromString(const std::string& s); |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 116 | % endfor |
| 117 | |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 118 | private: |
| 119 | % for m in interface.methods: |
| 120 | ${ m.cpp_prototype(loader, interface=interface, ptype='callback-header') } |
| 121 | % endfor |
| 122 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 123 | % for p in interface.properties: |
| 124 | /** @brief sd-bus callback for get-property '${p.name}' */ |
| 125 | static int _callback_get_${p.name}( |
| 126 | sd_bus*, const char*, const char*, const char*, |
| 127 | sd_bus_message*, void*, sd_bus_error*); |
| 128 | /** @brief sd-bus callback for set-property '${p.name}' */ |
| 129 | static int _callback_set_${p.name}( |
| 130 | sd_bus*, const char*, const char*, const char*, |
| 131 | sd_bus_message*, void*, sd_bus_error*); |
| 132 | |
| 133 | % endfor |
| 134 | |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 135 | static constexpr auto _interface = "${interface.name}"; |
Patrick Williams | 0966ce8 | 2016-10-17 21:54:09 -0500 | [diff] [blame] | 136 | static const vtable::vtable_t _vtable[]; |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 137 | sdbusplus::server::interface::interface |
| 138 | _${"_".join(interface.name.split('.'))}_interface; |
Patrick Venture | 263712f | 2018-04-16 14:19:08 -0700 | [diff] [blame] | 139 | sdbusplus::SdBusInterface *_intf; |
Patrick Williams | adc1682 | 2016-10-17 14:43:54 -0500 | [diff] [blame] | 140 | |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 141 | % for p in interface.properties: |
| 142 | % if p.defaultValue: |
Patrick Williams | 2adcefa | 2016-11-16 08:23:12 -0600 | [diff] [blame] | 143 | ${p.cppTypeParam(interface.name)} _${p.camelCase} = \ |
| 144 | % if p.is_enum(): |
| 145 | ${p.cppTypeParam(interface.name)}::\ |
| 146 | % endif |
| 147 | ${p.defaultValue}; |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 148 | % else: |
Patrick Williams | 0aa0dde | 2016-11-16 08:14:33 -0600 | [diff] [blame] | 149 | ${p.cppTypeParam(interface.name)} _${p.camelCase}{}; |
Patrick Williams | b2cca01 | 2016-10-18 14:13:39 -0500 | [diff] [blame] | 150 | % endif |
| 151 | % endfor |
| 152 | |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 153 | }; |
| 154 | |
Patrick Williams | 386e8d2 | 2016-11-16 16:13:57 -0600 | [diff] [blame] | 155 | % for e in interface.enums: |
| 156 | /* Specialization of sdbusplus::server::bindings::details::convertForMessage |
| 157 | * for enum-type ${classname}::${e.name}. |
| 158 | * |
| 159 | * This converts from the enum to a constant c-string representing the enum. |
| 160 | * |
| 161 | * @param[in] e - Enum value to convert. |
| 162 | * @return C-string representing the name for the enum value. |
| 163 | */ |
| 164 | std::string convertForMessage(${classname}::${e.name} e); |
| 165 | % endfor |
| 166 | |
Patrick Williams | 7aa8a1e | 2016-11-11 13:30:33 -0600 | [diff] [blame] | 167 | } // namespace server |
Patrick Williams | 0e9ad0d | 2016-10-18 14:24:44 -0500 | [diff] [blame] | 168 | % for s in reversed(namespaces): |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 169 | } // namespace ${s} |
| 170 | % endfor |
Patrick Williams | 04e007f | 2016-10-15 07:15:12 -0500 | [diff] [blame] | 171 | } // namespace sdbusplus |