blob: 5e8f2ac68599a54e1509954fe474bd4d88d76c5c [file] [log] [blame]
Patrick Williams04e007f2016-10-15 07:15:12 -05001<%
2 def cpp_return_type():
3 if len(method.returns) == 0:
4 return "void"
5 elif len(method.returns) == 1:
6 return method.returns[0].typeName
7 else:
8 return "std::tuple<" + \
Patrick Williams178e8fc2016-10-17 16:37:24 -05009 returns_as_list() + \
Patrick Williams04e007f2016-10-15 07:15:12 -050010 ">"
11
12 def parameters(defaultValue=False):
13 return ",\n ".\
14 join([ parameter(p, defaultValue) for p in method.parameters ])
15
Patrick Williams831839a2016-10-16 18:09:00 -050016 def parameters_as_local():
17 return "{};\n ".join([ parameter(p) for p in method.parameters ])
18
19 def parameters_as_list():
20 return ", ".join([ p.camelCase for p in method.parameters ])
21
Patrick Williams178e8fc2016-10-17 16:37:24 -050022 def parameters_types_as_list():
23 return ", ".join([ p.typeName for p in method.parameters ])
24
Patrick Williams04e007f2016-10-15 07:15:12 -050025 def parameter(p, defaultValue=False):
Patrick Williamsce8a4672016-10-15 10:38:54 -050026 r = "%s %s" % (p.typeName, p.camelCase)
Patrick Williams04e007f2016-10-15 07:15:12 -050027 if defaultValue:
28 r += default_value(p)
29 return r
30
Patrick Williams178e8fc2016-10-17 16:37:24 -050031 def returns_as_list():
32 return ", ".join([ r.typeName for r in method.returns ])
33
Patrick Williams831839a2016-10-16 18:09:00 -050034 def returns_as_tuple_index(tuple):
35 return ", ".join([ "std::move(std::get<%d>(%s))" % (i,tuple) \
36 for i in range(len(method.returns))])
37
Patrick Williams04e007f2016-10-15 07:15:12 -050038 def default_value(p):
39 if p.defaultValue != None:
40 return " = " + str(p.defaultValue)
41 else:
42 return ""
Patrick Williams831839a2016-10-16 18:09:00 -050043
44 def interface_name():
45 return interface.name.split('.').pop()
Patrick Williams04e007f2016-10-15 07:15:12 -050046%>
47###
48### Emit 'header'
49###
50 % if ptype == 'header':
51 /** @brief Implementation for ${ method.name }
52 * ${ method.description.strip() }
53 % if len(method.parameters) != 0:
54 *
55 % for p in method.parameters:
Patrick Williamsce8a4672016-10-15 10:38:54 -050056 * @param[in] ${p.camelCase} - ${p.description.strip()}
Patrick Williams04e007f2016-10-15 07:15:12 -050057 % endfor
58 % endif
59 % if len(method.returns) != 0:
60 *
61 % for r in method.returns:
Patrick Williamsce8a4672016-10-15 10:38:54 -050062 * @return ${r.camelCase}[${r.typeName}] - ${r.description.strip()}
Patrick Williams04e007f2016-10-15 07:15:12 -050063 % endfor
64 % endif
65 */
Patrick Williamsce8a4672016-10-15 10:38:54 -050066 virtual ${cpp_return_type()} ${ method.camelCase }(
Patrick Williams04e007f2016-10-15 07:15:12 -050067 ${ parameters() }) = 0;
68###
69### Emit 'callback-header'
70###
71 % elif ptype == 'callback-header':
72 /** @brief sd-bus callback for ${ method.name }
73 */
Patrick Williamsce8a4672016-10-15 10:38:54 -050074 static int _callback_${ method.CamelCase }(
Patrick Williams04e007f2016-10-15 07:15:12 -050075 sd_bus_message*, void*, sd_bus_error*);
Patrick Williams831839a2016-10-16 18:09:00 -050076###
Patrick Williams178e8fc2016-10-17 16:37:24 -050077### Emit 'vtable'
78###
79 % elif ptype == 'vtable':
80 vtable::method("${method.name}",
81 details::${interface_name()}::_param_${ method.CamelCase }
82 .data(),
83 details::${interface_name()}::_return_${ method.CamelCase }
84 .data(),
85 _callback_${ method.CamelCase }),
86###
Patrick Williams831839a2016-10-16 18:09:00 -050087### Emit 'callback-cpp'
88###
89 % elif ptype == 'callback-cpp':
90int ${interface_name()}::_callback_${ method.CamelCase }(
91 sd_bus_message* msg, void* context, sd_bus_error* error)
92{
93 ### Need to add a ref to msg since we attached it to an sdbusplus::message.
94 auto m = sdbusplus::message::message(sd_bus_message_ref(msg));
95
96 % if len(method.parameters) != 0:
97 ${parameters_as_local()}{};
98
99 m.read(${parameters_as_list()});
100 % endif
101
102 auto o = static_cast<${interface_name()}*>(context);
103 % if len(method.returns) != 0:
104 auto r = \
105 %endif
106 o->${ method.camelCase }(${parameters_as_list()});
107
108 auto reply = m.new_method_return();
109 % if len(method.returns) == 0:
110 // No data to append on reply.
111 % elif len(method.returns) == 1:
112 reply.append(std::move(r));
113 % else:
114 reply.append(${returns_as_tuple_index("r")});
115 % endif
116
Patrick Williams425dc9b2016-10-17 16:50:53 -0500117 reply.method_return();
Patrick Williams831839a2016-10-16 18:09:00 -0500118
119 return 0;
120}
Patrick Williams178e8fc2016-10-17 16:37:24 -0500121
122namespace details
123{
124namespace ${interface_name()}
125{
126static const auto _param_${ method.CamelCase } =
127 % if len(method.parameters) == 0:
128 utility::tuple_to_array(std::make_tuple('\0'));
129 % else:
130 utility::tuple_to_array(message::types::type_id<
131 ${ parameters_types_as_list() }>());
132 % endif
133static const auto _return_${ method.CamelCase } =
134 % if len(method.parameters) == 0:
135 utility::tuple_to_array(std::make_tuple('\0'));
136 % else:
137 utility::tuple_to_array(message::types::type_id<
138 ${ returns_as_list() }>());
139 % endif
140}
141}
Patrick Williams04e007f2016-10-15 07:15:12 -0500142 % endif