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