Skip to content

Commit d748272

Browse files
committed
Better string table generation tool.
1 parent 86127c6 commit d748272

2 files changed

Lines changed: 80 additions & 37 deletions

File tree

examples/bulkloop/string_table.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

utils/descriptors_string_table.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/python3
2+
3+
"""
4+
Generate a C file containing strings in the format needed by the USB descriptors.
5+
6+
Put the following in your Makefile
7+
-------------------------------------------
8+
descriptors_strings.h: descriptors_string_table.py descriptors.strings
9+
@python3 descriptors_string_table.py --header < descriptors.strings > descriptors_strings.h
10+
11+
descriptors_strings.inc: descriptors_string_table.py descriptors.strings
12+
@python3 descriptors_string_table.py --cfile < descriptors.strings > descriptors_strings.inc
13+
14+
descriptors.h: date.h descriptors_strings.h
15+
descriptors.c: descriptors_strings.inc
16+
17+
check-descriptors: descriptors.c
18+
gcc -I../../third_party/fx2lib/include descriptors.c
19+
-------------------------------------------
20+
"""
21+
22+
import sys
23+
24+
strings = [x.strip() for x in sys.stdin.readlines()]
25+
26+
if sys.argv[1] == "--header":
27+
print("""\
28+
// This is an auto-generated file!
29+
#include <linux/ch9.h>
30+
#include <linux/ch9-extra.h>
31+
32+
#ifndef DESCRIPTORS_STRING_TABLE_H_
33+
#define DESCRIPTORS_STRING_TABLE_H_
34+
35+
struct usb_descriptors_strings {
36+
struct usb_string_lang {
37+
__u8 bLength;
38+
__u8 bDescriptorType;
39+
__le16 wData[1];
40+
} language;""")
41+
for i, string in enumerate(strings):
42+
print("""\
43+
struct usb_string_%(i)i {
44+
__u8 bLength;
45+
__u8 bDescriptorType;
46+
__le16 wData[%(l)i];
47+
} string%(i)i;""" % {'l': len(string), 'i': i})
48+
print("""\
49+
};
50+
51+
#endif // DESCRIPTORS_STRING_TABLE_H_
52+
""")
53+
54+
if sys.argv[1] == "--cfile":
55+
print("""\
56+
.strings = {
57+
// English language header
58+
.language = {
59+
.bLength = sizeof(struct usb_string_lang),
60+
.bDescriptorType = USB_DT_STRING,
61+
.wData = { 0x0409 }, // 0x0409 is English
62+
},""")
63+
for i, string in enumerate(strings):
64+
d = ["((__le16)('%s'))" % s for s in string]
65+
66+
print("""\
67+
// "%(s)s"
68+
.string%(i)i = {
69+
.bLength = sizeof(struct usb_string_%(i)i),
70+
.bDescriptorType = USB_DT_STRING,
71+
.wData = {%(d)s},
72+
},""" % {
73+
's': string,
74+
'i': i,
75+
'l': len(string),
76+
'd': ", ".join(d),
77+
})
78+
print("""\
79+
},
80+
""")

0 commit comments

Comments
 (0)