Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/tools/dev/rpc_classes.py@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | """ |
borlanic | 0:fbdae7e6d805 | 2 | mbed SDK |
borlanic | 0:fbdae7e6d805 | 3 | Copyright (c) 2011-2013 ARM Limited |
borlanic | 0:fbdae7e6d805 | 4 | |
borlanic | 0:fbdae7e6d805 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:fbdae7e6d805 | 6 | you may not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 7 | You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 8 | |
borlanic | 0:fbdae7e6d805 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 10 | |
borlanic | 0:fbdae7e6d805 | 11 | Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:fbdae7e6d805 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 14 | See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 15 | limitations under the License. |
borlanic | 0:fbdae7e6d805 | 16 | """ |
borlanic | 0:fbdae7e6d805 | 17 | from os.path import join |
borlanic | 0:fbdae7e6d805 | 18 | from jinja2 import Template |
borlanic | 0:fbdae7e6d805 | 19 | |
borlanic | 0:fbdae7e6d805 | 20 | from tools.paths import TOOLS_DATA, MBED_RPC |
borlanic | 0:fbdae7e6d805 | 21 | |
borlanic | 0:fbdae7e6d805 | 22 | RPC_TEMPLATES_PATH = join(TOOLS_DATA, "rpc") |
borlanic | 0:fbdae7e6d805 | 23 | |
borlanic | 0:fbdae7e6d805 | 24 | RPC_TEMPLATE = "RPCClasses.h" |
borlanic | 0:fbdae7e6d805 | 25 | CLASS_TEMPLATE = "class.cpp" |
borlanic | 0:fbdae7e6d805 | 26 | RPC_CLASSES_PATH = join(MBED_RPC, RPC_TEMPLATE) |
borlanic | 0:fbdae7e6d805 | 27 | |
borlanic | 0:fbdae7e6d805 | 28 | |
borlanic | 0:fbdae7e6d805 | 29 | def get_template(name): |
borlanic | 0:fbdae7e6d805 | 30 | return Template(open(join(RPC_TEMPLATES_PATH, name)).read()) |
borlanic | 0:fbdae7e6d805 | 31 | |
borlanic | 0:fbdae7e6d805 | 32 | |
borlanic | 0:fbdae7e6d805 | 33 | def write_rpc_classes(classes): |
borlanic | 0:fbdae7e6d805 | 34 | template = get_template(RPC_TEMPLATE) |
borlanic | 0:fbdae7e6d805 | 35 | open(RPC_CLASSES_PATH, "w").write(template.render({"classes":classes})) |
borlanic | 0:fbdae7e6d805 | 36 | |
borlanic | 0:fbdae7e6d805 | 37 | |
borlanic | 0:fbdae7e6d805 | 38 | RPC_CLASSES = ( |
borlanic | 0:fbdae7e6d805 | 39 | { |
borlanic | 0:fbdae7e6d805 | 40 | "name": "DigitalOut", |
borlanic | 0:fbdae7e6d805 | 41 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 42 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 43 | (None , "write", ["int"]), |
borlanic | 0:fbdae7e6d805 | 44 | ("int", "read" , []), |
borlanic | 0:fbdae7e6d805 | 45 | ] |
borlanic | 0:fbdae7e6d805 | 46 | }, |
borlanic | 0:fbdae7e6d805 | 47 | { |
borlanic | 0:fbdae7e6d805 | 48 | "name": "DigitalIn", |
borlanic | 0:fbdae7e6d805 | 49 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 50 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 51 | ("int", "read" , []), |
borlanic | 0:fbdae7e6d805 | 52 | ] |
borlanic | 0:fbdae7e6d805 | 53 | }, |
borlanic | 0:fbdae7e6d805 | 54 | { |
borlanic | 0:fbdae7e6d805 | 55 | "name": "DigitalInOut", |
borlanic | 0:fbdae7e6d805 | 56 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 57 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 58 | ("int", "read" , []), |
borlanic | 0:fbdae7e6d805 | 59 | (None , "write" , ["int"]), |
borlanic | 0:fbdae7e6d805 | 60 | (None , "input" , []), |
borlanic | 0:fbdae7e6d805 | 61 | (None , "output", []), |
borlanic | 0:fbdae7e6d805 | 62 | ] |
borlanic | 0:fbdae7e6d805 | 63 | }, |
borlanic | 0:fbdae7e6d805 | 64 | { |
borlanic | 0:fbdae7e6d805 | 65 | "name": "AnalogIn", |
borlanic | 0:fbdae7e6d805 | 66 | "required": "ANALOGIN", |
borlanic | 0:fbdae7e6d805 | 67 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 68 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 69 | ("float" , "read" , []), |
borlanic | 0:fbdae7e6d805 | 70 | ("unsigned short", "read_u16", []), |
borlanic | 0:fbdae7e6d805 | 71 | ] |
borlanic | 0:fbdae7e6d805 | 72 | }, |
borlanic | 0:fbdae7e6d805 | 73 | { |
borlanic | 0:fbdae7e6d805 | 74 | "name": "AnalogOut", |
borlanic | 0:fbdae7e6d805 | 75 | "required": "ANALOGOUT", |
borlanic | 0:fbdae7e6d805 | 76 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 77 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 78 | ("float", "read" , []), |
borlanic | 0:fbdae7e6d805 | 79 | (None , "write" , ["float"]), |
borlanic | 0:fbdae7e6d805 | 80 | (None , "write_u16", ["unsigned short"]), |
borlanic | 0:fbdae7e6d805 | 81 | ] |
borlanic | 0:fbdae7e6d805 | 82 | }, |
borlanic | 0:fbdae7e6d805 | 83 | { |
borlanic | 0:fbdae7e6d805 | 84 | "name": "PwmOut", |
borlanic | 0:fbdae7e6d805 | 85 | "required": "PWMOUT", |
borlanic | 0:fbdae7e6d805 | 86 | "cons_args": ["PinName"], |
borlanic | 0:fbdae7e6d805 | 87 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 88 | ("float", "read" , []), |
borlanic | 0:fbdae7e6d805 | 89 | (None , "write" , ["float"]), |
borlanic | 0:fbdae7e6d805 | 90 | (None , "period" , ["float"]), |
borlanic | 0:fbdae7e6d805 | 91 | (None , "period_ms" , ["int"]), |
borlanic | 0:fbdae7e6d805 | 92 | (None , "pulsewidth" , ["float"]), |
borlanic | 0:fbdae7e6d805 | 93 | (None , "pulsewidth_ms", ["int"]), |
borlanic | 0:fbdae7e6d805 | 94 | ] |
borlanic | 0:fbdae7e6d805 | 95 | }, |
borlanic | 0:fbdae7e6d805 | 96 | { |
borlanic | 0:fbdae7e6d805 | 97 | "name": "SPI", |
borlanic | 0:fbdae7e6d805 | 98 | "required": "SPI", |
borlanic | 0:fbdae7e6d805 | 99 | "cons_args": ["PinName", "PinName", "PinName"], |
borlanic | 0:fbdae7e6d805 | 100 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 101 | (None , "format" , ["int", "int"]), |
borlanic | 0:fbdae7e6d805 | 102 | (None , "frequency", ["int"]), |
borlanic | 0:fbdae7e6d805 | 103 | ("int", "write" , ["int"]), |
borlanic | 0:fbdae7e6d805 | 104 | ] |
borlanic | 0:fbdae7e6d805 | 105 | }, |
borlanic | 0:fbdae7e6d805 | 106 | { |
borlanic | 0:fbdae7e6d805 | 107 | "name": "Serial", |
borlanic | 0:fbdae7e6d805 | 108 | "required": "SERIAL", |
borlanic | 0:fbdae7e6d805 | 109 | "cons_args": ["PinName", "PinName"], |
borlanic | 0:fbdae7e6d805 | 110 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 111 | (None , "baud" , ["int"]), |
borlanic | 0:fbdae7e6d805 | 112 | ("int", "readable" , []), |
borlanic | 0:fbdae7e6d805 | 113 | ("int", "writeable", []), |
borlanic | 0:fbdae7e6d805 | 114 | ("int", "putc" , ["int"]), |
borlanic | 0:fbdae7e6d805 | 115 | ("int", "getc" , []), |
borlanic | 0:fbdae7e6d805 | 116 | ("int", "puts" , ["const char *"]), |
borlanic | 0:fbdae7e6d805 | 117 | ] |
borlanic | 0:fbdae7e6d805 | 118 | }, |
borlanic | 0:fbdae7e6d805 | 119 | { |
borlanic | 0:fbdae7e6d805 | 120 | "name": "Timer", |
borlanic | 0:fbdae7e6d805 | 121 | "cons_args": [], |
borlanic | 0:fbdae7e6d805 | 122 | "methods": [ |
borlanic | 0:fbdae7e6d805 | 123 | (None , "start" , []), |
borlanic | 0:fbdae7e6d805 | 124 | (None , "stop" , []), |
borlanic | 0:fbdae7e6d805 | 125 | (None , "reset" , []), |
borlanic | 0:fbdae7e6d805 | 126 | ("float", "read" , []), |
borlanic | 0:fbdae7e6d805 | 127 | ("int" , "read_ms", []), |
borlanic | 0:fbdae7e6d805 | 128 | ("int" , "read_us", []), |
borlanic | 0:fbdae7e6d805 | 129 | ] |
borlanic | 0:fbdae7e6d805 | 130 | } |
borlanic | 0:fbdae7e6d805 | 131 | ) |
borlanic | 0:fbdae7e6d805 | 132 | |
borlanic | 0:fbdae7e6d805 | 133 | |
borlanic | 0:fbdae7e6d805 | 134 | def get_args_proto(args_types, extra=None): |
borlanic | 0:fbdae7e6d805 | 135 | args = ["%s a%d" % (s, n) for n, s in enumerate(args_types)] |
borlanic | 0:fbdae7e6d805 | 136 | if extra: |
borlanic | 0:fbdae7e6d805 | 137 | args.extend(extra) |
borlanic | 0:fbdae7e6d805 | 138 | return ', '.join(args) |
borlanic | 0:fbdae7e6d805 | 139 | |
borlanic | 0:fbdae7e6d805 | 140 | |
borlanic | 0:fbdae7e6d805 | 141 | def get_args_call(args): |
borlanic | 0:fbdae7e6d805 | 142 | return ', '.join(["a%d" % (n) for n in range(len(args))]) |
borlanic | 0:fbdae7e6d805 | 143 | |
borlanic | 0:fbdae7e6d805 | 144 | |
borlanic | 0:fbdae7e6d805 | 145 | classes = [] |
borlanic | 0:fbdae7e6d805 | 146 | class_template = get_template(CLASS_TEMPLATE) |
borlanic | 0:fbdae7e6d805 | 147 | |
borlanic | 0:fbdae7e6d805 | 148 | for c in RPC_CLASSES: |
borlanic | 0:fbdae7e6d805 | 149 | c_args = c['cons_args'] |
borlanic | 0:fbdae7e6d805 | 150 | data = { |
borlanic | 0:fbdae7e6d805 | 151 | 'name': c['name'], |
borlanic | 0:fbdae7e6d805 | 152 | 'cons_type': ', '.join(c_args + ['const char*']), |
borlanic | 0:fbdae7e6d805 | 153 | "cons_proto": get_args_proto(c_args, ["const char *name=NULL"]), |
borlanic | 0:fbdae7e6d805 | 154 | "cons_call": get_args_call(c_args) |
borlanic | 0:fbdae7e6d805 | 155 | } |
borlanic | 0:fbdae7e6d805 | 156 | |
borlanic | 0:fbdae7e6d805 | 157 | c_name = "Rpc" + c['name'] |
borlanic | 0:fbdae7e6d805 | 158 | |
borlanic | 0:fbdae7e6d805 | 159 | methods = [] |
borlanic | 0:fbdae7e6d805 | 160 | rpc_methods = [] |
borlanic | 0:fbdae7e6d805 | 161 | for r, m, a in c['methods']: |
borlanic | 0:fbdae7e6d805 | 162 | ret_proto = r if r else "void" |
borlanic | 0:fbdae7e6d805 | 163 | args_proto = "void" |
borlanic | 0:fbdae7e6d805 | 164 | |
borlanic | 0:fbdae7e6d805 | 165 | ret_defin = "return " if r else "" |
borlanic | 0:fbdae7e6d805 | 166 | args_defin = "" |
borlanic | 0:fbdae7e6d805 | 167 | |
borlanic | 0:fbdae7e6d805 | 168 | if a: |
borlanic | 0:fbdae7e6d805 | 169 | args_proto = get_args_proto(a) |
borlanic | 0:fbdae7e6d805 | 170 | args_defin = get_args_call(a) |
borlanic | 0:fbdae7e6d805 | 171 | |
borlanic | 0:fbdae7e6d805 | 172 | proto = "%s %s(%s)" % (ret_proto, m, args_proto) |
borlanic | 0:fbdae7e6d805 | 173 | defin = "{%so.%s(%s);}" % (ret_defin, m, args_defin) |
borlanic | 0:fbdae7e6d805 | 174 | methods.append("%s %s" % (proto, defin)) |
borlanic | 0:fbdae7e6d805 | 175 | |
borlanic | 0:fbdae7e6d805 | 176 | rpc_method_type = [r] if r else [] |
borlanic | 0:fbdae7e6d805 | 177 | rpc_method_type.append(c_name) |
borlanic | 0:fbdae7e6d805 | 178 | rpc_method_type.extend(a) |
borlanic | 0:fbdae7e6d805 | 179 | rpc_methods.append('{"%s", rpc_method_caller<%s, &%s::%s>}' % (m, ', '.join(rpc_method_type), c_name, m)) |
borlanic | 0:fbdae7e6d805 | 180 | |
borlanic | 0:fbdae7e6d805 | 181 | data['methods'] = "\n ".join(methods) |
borlanic | 0:fbdae7e6d805 | 182 | data['rpc_methods'] = ",\n ".join(rpc_methods) |
borlanic | 0:fbdae7e6d805 | 183 | |
borlanic | 0:fbdae7e6d805 | 184 | class_decl = class_template.render(data) |
borlanic | 0:fbdae7e6d805 | 185 | if 'required' in c: |
borlanic | 0:fbdae7e6d805 | 186 | class_decl = "#if DEVICE_%s\n%s\n#endif" % (c['required'], class_decl) |
borlanic | 0:fbdae7e6d805 | 187 | |
borlanic | 0:fbdae7e6d805 | 188 | classes.append(class_decl) |
borlanic | 0:fbdae7e6d805 | 189 | |
borlanic | 0:fbdae7e6d805 | 190 | write_rpc_classes('\n\n'.join(classes)) |