3 Copyright (c) 2011-2013 ARM Limited
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 from os.path import join
18 from jinja2 import Template
20 from workspace_tools.paths import TOOLS_DATA, MBED_RPC
22 RPC_TEMPLATES_PATH = join(TOOLS_DATA, "rpc")
24 RPC_TEMPLATE = "RPCClasses.h"
25 CLASS_TEMPLATE = "class.cpp"
26 RPC_CLASSES_PATH = join(MBED_RPC, RPC_TEMPLATE)
29 def get_template(name):
30 return Template(open(join(RPC_TEMPLATES_PATH, name)).read())
33 def write_rpc_classes(classes):
34 template = get_template(RPC_TEMPLATE)
35 open(RPC_CLASSES_PATH, "w").write(template.render({"classes":classes}))
41 "cons_args": ["PinName"],
43 (None , "write", ["int"]),
49 "cons_args": ["PinName"],
55 "name": "DigitalInOut",
56 "cons_args": ["PinName"],
59 (None , "write" , ["int"]),
60 (None , "input" , []),
61 (None , "output", []),
66 "required": "ANALOGIN",
67 "cons_args": ["PinName"],
69 ("float" , "read" , []),
70 ("unsigned short", "read_u16", []),
75 "required": "ANALOGOUT",
76 "cons_args": ["PinName"],
78 ("float", "read" , []),
79 (None , "write" , ["float"]),
80 (None , "write_u16", ["unsigned short"]),
86 "cons_args": ["PinName"],
88 ("float", "read" , []),
89 (None , "write" , ["float"]),
90 (None , "period" , ["float"]),
91 (None , "period_ms" , ["int"]),
92 (None , "pulsewidth" , ["float"]),
93 (None , "pulsewidth_ms", ["int"]),
99 "cons_args": ["PinName", "PinName", "PinName"],
101 (None , "format" , ["int", "int"]),
102 (None , "frequency", ["int"]),
103 ("int", "write" , ["int"]),
108 "required": "SERIAL",
109 "cons_args": ["PinName", "PinName"],
111 (None , "baud" , ["int"]),
112 ("int", "readable" , []),
113 ("int", "writeable", []),
114 ("int", "putc" , ["int"]),
115 ("int", "getc" , []),
116 ("int", "puts" , ["const char *"]),
123 (None , "start" , []),
124 (None , "stop" , []),
125 (None , "reset" , []),
126 ("float", "read" , []),
127 ("int" , "read_ms", []),
128 ("int" , "read_us", []),
134 def get_args_proto(args_types, extra=None):
135 args = ["%s a%d" % (s, n) for n, s in enumerate(args_types)]
138 return ', '.join(args)
141 def get_args_call(args):
142 return ', '.join(["a%d" % (n) for n in range(len(args))])
146 class_template = get_template(CLASS_TEMPLATE)
148 for c in RPC_CLASSES:
149 c_args = c['cons_args']
152 'cons_type': ', '.join(c_args + ['const char*']),
153 "cons_proto": get_args_proto(c_args, ["const char *name=NULL"]),
154 "cons_call": get_args_call(c_args)
157 c_name = "Rpc" + c['name']
161 for r, m, a in c['methods']:
162 ret_proto = r if r else "void"
165 ret_defin = "return " if r else ""
169 args_proto = get_args_proto(a)
170 args_defin = get_args_call(a)
172 proto = "%s %s(%s)" % (ret_proto, m, args_proto)
173 defin = "{%so.%s(%s);}" % (ret_defin, m, args_defin)
174 methods.append("%s %s" % (proto, defin))
176 rpc_method_type = [r] if r else []
177 rpc_method_type.append(c_name)
178 rpc_method_type.extend(a)
179 rpc_methods.append('{"%s", rpc_method_caller<%s, &%s::%s>}' % (m, ', '.join(rpc_method_type), c_name, m))
181 data['methods'] = "\n ".join(methods)
182 data['rpc_methods'] = ",\n ".join(rpc_methods)
184 class_decl = class_template.render(data)
186 class_decl = "#if DEVICE_%s\n%s\n#endif" % (c['required'], class_decl)
188 classes.append(class_decl)
190 write_rpc_classes('\n\n'.join(classes))