forked from libuvc/libuvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrl-gen.py
executable file
·302 lines (250 loc) · 10 KB
/
ctrl-gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
from __future__ import print_function
from collections import OrderedDict
import getopt
import sys
import yaml
class quoted(str): pass
def quoted_presenter(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
yaml.add_representer(quoted, quoted_presenter)
class literal(str): pass
def literal_presenter(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
yaml.add_representer(literal, literal_presenter)
def ordered_dict_presenter(dumper, data):
return dumper.represent_dict(data.items())
yaml.add_representer(OrderedDict, ordered_dict_presenter)
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
yaml.add_constructor(_mapping_tag, dict_constructor)
class IntField(object):
def __init__(self, name, position, length, signed):
self.name = name
self.position = position
self.length = length
self.signed = signed
if not self.length in [1, 2, 4]:
raise Exception("bad length " + str(self.length))
self.user_type = ('u' if not signed else '') + 'int' + str(length * 8) + '_t'
def getter_sig(self):
return "{0}* {1}".format(self.user_type, self.name)
def unpack(self):
if self.length == 1:
return "*{0} = data[{1}];".format(self.name, self.position)
elif self.length == 2:
return "*{0} = SW_TO_SHORT(data + {1});".format(self.name, self.position)
elif self.length == 4:
return "*{0} = DW_TO_INT(data + {1});".format(self.name, self.position)
def setter_sig(self):
return "{0} {1}".format(self.user_type, self.name)
def pack(self):
if self.length == 1:
return "data[{0}] = {1};".format(self.position, self.name)
elif self.length == 2:
return "SHORT_TO_SW({0}, data + {1});".format(self.name, self.position)
elif self.length == 4:
return "INT_TO_DW({0}, data + {1});".format(self.name, self.position)
def spec(self):
rep = [('position', self.position), ('length', self.length)]
if self.signed:
rep.append(('signed', True))
return rep
@staticmethod
def load(spec):
return IntField(spec['name'], spec['position'], spec['length'], spec['signed'] if signed in spec else False)
def load_field(name, spec):
if spec['type'] == 'int':
return IntField(name, spec['position'], spec['length'], spec.get('signed', False))
else:
raise Exception("unknown field type '{0}'".format(spec['type']))
GETTER_TEMPLATE = """/** @ingroup ctrl
* {gen_doc}
* @param devh UVC device handle
* {args_doc}
* @param req_code UVC_GET_* request to execute
*/
uvc_error_t uvc_get_{control_name}(uvc_device_handle_t *devh, {args_signature}, enum uvc_req_code req_code) {{
uint8_t data[{control_length}];
uvc_error_t ret;
ret = libusb_control_transfer(
devh->usb_devh,
REQ_TYPE_GET, req_code,
{control_code} << 8,
{unit_fn} << 8 | devh->info->ctrl_if.bInterfaceNumber,
data,
sizeof(data),
0);
if (ret == sizeof(data)) {{
{unpack}
return UVC_SUCCESS;
}} else {{
return ret;
}}
}}
"""
SETTER_TEMPLATE = """/** @ingroup ctrl
* {gen_doc}
* @param devh UVC device handle
* {args_doc}
*/
uvc_error_t uvc_set_{control_name}(uvc_device_handle_t *devh, {args_signature}) {{
uint8_t data[{control_length}];
uvc_error_t ret;
{pack}
ret = libusb_control_transfer(
devh->usb_devh,
REQ_TYPE_SET, UVC_SET_CUR,
{control_code} << 8,
{unit_fn} << 8 | devh->info->ctrl_if.bInterfaceNumber,
data,
sizeof(data),
0);
if (ret == sizeof(data))
return UVC_SUCCESS;
else
return ret;
}}
"""
def gen_decl(unit_name, unit, control_name, control):
fields = [(load_field(field_name, field_details), field_details['doc']) for field_name, field_details in control['fields'].items()] if 'fields' in control else []
get_args_signature = ', '.join([field.getter_sig() for (field, desc) in fields])
set_args_signature = ', '.join([field.setter_sig() for (field, desc) in fields])
return "uvc_error_t uvc_get_{function_name}(uvc_device_handle_t *devh, {args_signature}, enum uvc_req_code req_code);\n".format(**{
"function_name": control_name,
"args_signature": get_args_signature
}) + "uvc_error_t uvc_set_{function_name}(uvc_device_handle_t *devh, {args_signature});\n".format(**{
"function_name": control_name,
"args_signature": set_args_signature
})
def gen_ctrl(unit_name, unit, control_name, control):
fields = [(load_field(field_name, field_details), field_details['doc']) for field_name, field_details in control['fields'].items()] if 'fields' in control else []
get_args_signature = ', '.join([field.getter_sig() for (field, desc) in fields])
set_args_signature = ', '.join([field.setter_sig() for (field, desc) in fields])
unpack = "\n ".join([field.unpack() for (field, desc) in fields])
pack = "\n ".join([field.pack() for (field, desc) in fields])
get_gen_doc_raw = None
set_gen_doc_raw = None
if 'doc' in control:
doc = control['doc']
if isinstance(doc, str):
get_gen_doc_raw = "\n * ".join(doc.splitlines())
set_gen_doc_raw = get_gen_doc_raw
else:
if 'get' in doc:
get_gen_doc_raw = "\n * ".join(doc['get'].splitlines())
if 'set' in doc:
set_gen_doc_raw = "\n * ".join(doc['set'].splitlines())
if get_gen_doc_raw is not None:
get_gen_doc = get_gen_doc_raw.format(gets_sets='Reads')
else:
get_gen_doc = '@brief Reads the ' + control['control'] + ' control.'
if set_gen_doc_raw is not None:
set_gen_doc = set_gen_doc_raw.format(gets_sets='Sets')
else:
set_gen_doc = '@brief Sets the ' + control['control'] + ' control.'
get_args_doc = "\n * ".join(["@param[out] {0} {1}".format(field.name, desc) for (field, desc) in fields])
set_args_doc = "\n * ".join(["@param {0} {1}".format(field.name, desc) for (field, desc) in fields])
control_code = 'UVC_' + unit['control_prefix'] + '_' + control['control'] + '_CONTROL'
unit_fn = "uvc_get_camera_terminal(devh)->bTerminalID" if (unit_name == "camera_terminal") else ("uvc_get_" + unit_name + "s(devh)->bUnitID")
return GETTER_TEMPLATE.format(
unit=unit,
unit_fn=unit_fn,
control_name=control_name,
control_code=control_code,
control_length=control['length'],
args_signature=get_args_signature,
args_doc=get_args_doc,
gen_doc=get_gen_doc,
unpack=unpack) + "\n\n" + SETTER_TEMPLATE.format(
unit=unit,
unit_fn=unit_fn,
control_name=control_name,
control_code=control_code,
control_length=control['length'],
args_signature=set_args_signature,
args_doc=set_args_doc,
gen_doc=set_gen_doc,
pack=pack
)
def export_unit(unit):
def fmt_doc(doc):
def wrap_doc_entry(entry):
if "\n" in entry:
return literal(entry)
else:
return entry
if isinstance(doc, str):
return wrap_doc_entry(doc)
else:
return OrderedDict([(mode, wrap_doc_entry(text)) for mode, text in doc.items()])
def fmt_ctrl(control_name, control_details):
contents = OrderedDict()
contents['control'] = control_details['control']
contents['length'] = control_details['length']
contents['fields'] = control_details['fields']
if 'doc' in control_details:
contents['doc'] = fmt_doc(control_details['doc'])
return (control_name, contents)
unit_out = OrderedDict()
unit_out['type'] = unit['type']
if 'guid' in unit:
unit_out['guid'] = unit['guid']
if 'description' in unit:
unit_out['description'] = unit['description']
if 'control_prefix' in unit:
unit_out['control_prefix'] = unit['control_prefix']
unit_out['controls'] = OrderedDict([fmt_ctrl(ctrl_name, ctrl_details) for ctrl_name, ctrl_details in unit['controls'].items()])
return unit_out
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:", ["help", "input="])
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(-1)
inputs = []
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(0)
elif opt in ('-i', '--input'):
inputs.append(val)
mode = None
for arg in args:
if arg in ('def', 'decl', 'yaml'):
if mode is None:
mode = arg
else:
print("Can't specify more than one mode")
sys.exit(-1)
else:
print("Invalid mode '{0}'".format(arg))
sys.exit(-1)
def iterunits():
for input_file in inputs:
with open(input_file, "r") as fp:
units = yaml.load(fp)['units']
for unit_name, unit_details in units.iteritems():
yield unit_name, unit_details
if mode == 'def':
print("""/* This is an AUTO-GENERATED file! Update it with the output of `ctrl-gen.py def`. */
#include "libuvc/libuvc.h"
#include "libuvc/libuvc_internal.h"
static const int REQ_TYPE_SET = 0x21;
static const int REQ_TYPE_GET = 0xa1;
""")
fun = gen_ctrl
elif mode == 'decl':
fun = gen_decl
elif mode == 'yaml':
exported_units = OrderedDict()
for unit_name, unit_details in iterunits():
exported_units[unit_name] = export_unit(unit_details)
yaml.dump({'units': exported_units}, sys.stdout, default_flow_style=False)
sys.exit(0)
for unit_name, unit_details in iterunits():
for control_name, control_details in unit_details['controls'].iteritems():
code = fun(unit_name, unit_details, control_name, control_details)
print(code)