blob: c9361518e35be6f85bc6063a5c22357010a67585 [file] [log] [blame]
Patrick Williams386e8d22016-11-16 16:13:57 -06001#include <algorithm>
William A. Kennington III4274c112018-11-26 09:50:13 -08002#include <map>
Patrick Venture263712f2018-04-16 14:19:08 -07003#include <sdbusplus/sdbus.hpp>
Patrick Williams0966ce82016-10-17 21:54:09 -05004#include <sdbusplus/server.hpp>
Patrick Williams31691cf2016-11-13 19:07:09 -06005#include <sdbusplus/exception.hpp>
William A. Kennington III4274c112018-11-26 09:50:13 -08006#include <string>
7#include <tuple>
8#include <variant>
9
Patrick Williams831839a2016-10-16 18:09:00 -050010#include <${"/".join(interface.name.split('.') + [ 'server.hpp' ])}>
William A. Kennington III3cf374e2019-04-03 20:40:16 -070011% for m in interface.methods + interface.properties + interface.signals:
Patrick Williams31691cf2016-11-13 19:07:09 -060012${ m.cpp_prototype(loader, interface=interface, ptype='callback-cpp-includes') }
13% endfor
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080014<%
15 namespaces = interface.name.split('.')
16 classname = namespaces.pop()
Patrick Williamsa475c032016-10-18 22:03:41 -050017
Abhishek Panditaa71a3e2016-11-10 13:33:17 -080018 def interface_instance():
19 return "_".join(interface.name.split('.') + ['interface'])
20%>
Patrick Williams831839a2016-10-16 18:09:00 -050021namespace sdbusplus
22{
Patrick Williams831839a2016-10-16 18:09:00 -050023 % for s in namespaces:
24namespace ${s}
25{
26 % endfor
Patrick Williams7aa8a1e2016-11-11 13:30:33 -060027namespace server
28{
Patrick Williamsadc16822016-10-17 14:43:54 -050029
30${classname}::${classname}(bus::bus& bus, const char* path)
Patrick Williamsa475c032016-10-18 22:03:41 -050031 : _${interface_instance()}(
Lei YU14db20f2020-02-03 14:13:16 +080032 bus, path, interface, _vtable, this), _intf(bus.getInterface())
Patrick Williamsadc16822016-10-17 14:43:54 -050033{
34}
35
Patrick Williams4d47bf02017-02-01 23:16:23 -060036 % if interface.properties:
37${classname}::${classname}(bus::bus& bus, const char* path,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053038 const std::map<std::string, PropertiesVariant>& vals,
39 bool skipSignal)
Patrick Williams4d47bf02017-02-01 23:16:23 -060040 : ${classname}(bus, path)
41{
42 for (const auto& v : vals)
43 {
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053044 setPropertyByName(v.first, v.second, skipSignal);
Patrick Williams4d47bf02017-02-01 23:16:23 -060045 }
46}
47
48 % endif
Patrick Williams831839a2016-10-16 18:09:00 -050049 % for m in interface.methods:
50${ m.cpp_prototype(loader, interface=interface, ptype='callback-cpp') }
51 % endfor
52
Patrick Williams5302a462016-10-18 11:11:51 -050053 % for s in interface.signals:
54${ s.cpp_prototype(loader, interface=interface, ptype='callback-cpp') }
55 % endfor
56
Patrick Williamsb2cca012016-10-18 14:13:39 -050057 % for p in interface.properties:
Adriana Kobylakee6ac692018-06-06 09:36:51 -050058${ p.cpp_prototype(loader, interface=interface, ptype='callback-cpp') }
Patrick Williamsb2cca012016-10-18 14:13:39 -050059 % endfor
60
Patrick Williams9fa85522017-02-01 23:06:33 -060061 % if interface.properties:
James Feistf7509062018-03-23 14:15:56 -070062void ${classname}::setPropertyByName(const std::string& _name,
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053063 const PropertiesVariant& val,
64 bool skipSignal)
Patrick Williams9fa85522017-02-01 23:06:33 -060065{
66 % for p in interface.properties:
James Feistf7509062018-03-23 14:15:56 -070067 if (_name == "${p.name}")
Patrick Williams9fa85522017-02-01 23:06:33 -060068 {
William A. Kennington III4274c112018-11-26 09:50:13 -080069 auto& v = std::get<${p.cppTypeParam(interface.name)}>(\
Patrick Williams9fa85522017-02-01 23:06:33 -060070val);
Richard Marian Thomaiyar261fe752018-06-18 13:53:56 +053071 ${p.camelCase}(v, skipSignal);
Patrick Williams9fa85522017-02-01 23:06:33 -060072 return;
73 }
74 % endfor
75}
76
James Feistf7509062018-03-23 14:15:56 -070077auto ${classname}::getPropertyByName(const std::string& _name) ->
Patrick Williamsdfa19092017-02-02 09:24:56 -060078 PropertiesVariant
79{
80 % for p in interface.properties:
James Feistf7509062018-03-23 14:15:56 -070081 if (_name == "${p.name}")
Patrick Williamsdfa19092017-02-02 09:24:56 -060082 {
83 return ${p.camelCase}();
84 }
85 % endfor
86
87 return PropertiesVariant();
88}
89
Patrick Williams9fa85522017-02-01 23:06:33 -060090 % endif
Patrick Williams386e8d22016-11-16 16:13:57 -060091 % for e in interface.enums:
92
93namespace
94{
95/** String to enum mapping for ${classname}::${e.name} */
96static const std::tuple<const char*, ${classname}::${e.name}> \
97mapping${classname}${e.name}[] =
98 {
99 % for v in e.values:
100 std::make_tuple( "${interface.name}.${e.name}.${v.name}", \
101 ${classname}::${e.name}::${v.name} ),
102 % endfor
103 };
104
105} // anonymous namespace
106
Patrick Williams93c246c2017-06-16 05:04:01 -0500107auto ${classname}::convert${e.name}FromString(const std::string& s) ->
Patrick Williams386e8d22016-11-16 16:13:57 -0600108 ${e.name}
109{
110 auto i = std::find_if(
111 std::begin(mapping${classname}${e.name}),
112 std::end(mapping${classname}${e.name}),
113 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
114 if (std::end(mapping${classname}${e.name}) == i)
115 {
116 throw sdbusplus::exception::InvalidEnumString();
117 }
118 else
119 {
120 return std::get<1>(*i);
121 }
122}
123
Patrick Williams978f77d2020-01-28 17:27:34 -0800124std::string ${classname}::convert${e.name}ToString(${classname}::${e.name} v)
Patrick Williams386e8d22016-11-16 16:13:57 -0600125{
126 auto i = std::find_if(
127 std::begin(mapping${classname}${e.name}),
128 std::end(mapping${classname}${e.name}),
129 [v](auto& e){ return v == std::get<1>(e); });
Lei YUb641d102018-09-05 17:17:47 +0800130 if (i == std::end(mapping${classname}${e.name}))
131 {
132 throw std::invalid_argument(std::to_string(static_cast<int>(v)));
133 }
Patrick Williams386e8d22016-11-16 16:13:57 -0600134 return std::get<0>(*i);
135}
136 % endfor
137
Patrick Williams178e8fc2016-10-17 16:37:24 -0500138const vtable::vtable_t ${classname}::_vtable[] = {
139 vtable::start(),
140 % for m in interface.methods:
141${ m.cpp_prototype(loader, interface=interface, ptype='vtable') }
142 % endfor
Patrick Williams5302a462016-10-18 11:11:51 -0500143 % for s in interface.signals:
144${ s.cpp_prototype(loader, interface=interface, ptype='vtable') }
145 % endfor
Patrick Williamsb2cca012016-10-18 14:13:39 -0500146 % for p in interface.properties:
147 vtable::property("${p.name}",
148 details::${classname}::_property_${p.name}
149 .data(),
150 _callback_get_${p.name},
Patrick Williamse1c73d32020-07-10 16:02:27 -0500151 % if 'const' not in p.flags and 'readonly' not in p.flags:
Patrick Williamsa475c032016-10-18 22:03:41 -0500152 _callback_set_${p.name},
Waqar Hameed20255a52020-06-04 12:38:48 +0200153 % endif
154 % if not p.cpp_flags:
Patrick Williamsa475c032016-10-18 22:03:41 -0500155 vtable::property_::emits_change),
Waqar Hameed20255a52020-06-04 12:38:48 +0200156 % else:
157 ${p.cpp_flags}),
158 % endif
Patrick Williamsb2cca012016-10-18 14:13:39 -0500159 % endfor
Patrick Williams178e8fc2016-10-17 16:37:24 -0500160 vtable::end()
161};
162
Patrick Williams7aa8a1e2016-11-11 13:30:33 -0600163} // namespace server
Patrick Williams0e9ad0d2016-10-18 14:24:44 -0500164 % for s in reversed(namespaces):
Patrick Williams831839a2016-10-16 18:09:00 -0500165} // namespace ${s}
166 % endfor
Patrick Williams831839a2016-10-16 18:09:00 -0500167} // namespace sdbusplus