summaryrefslogtreecommitdiff
path: root/src/util/ao-make-product.5c
blob: 3ab8d16ec474aba3403cc9a6654c24f4868eefa9 (plain) (blame)
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
#!/usr/bin/nickle

autoimport ParseArgs;

file  out = stdout;

void
write_ucs2(string a, string description)
{
	int len = String::length(a);

	File::fprintf(out, "/* %s */\n", description);
	File::fprintf(out, "#define AO_%s_LEN 0x%02x\n", description, len * 2 + 2);
	File::fprintf(out, "#define AO_%s_STRING \"%s\"\n", description, a);
	File::fprintf(out, "#define AO_%s_UCS2", description);
	for (int i = 0; i < len; i++) {
		int	c = a[i];
		if (i > 0)
			File::fprintf(out, ",");
		if (0x20 <= c && c < 128)
			File::fprintf(out, " '%c', 0", c);
		else
			File::fprintf(out, " LE_WORD(0x%04x),", c);
	}
	File::fprintf(out, "\n\n");
}

void
write_string(string a, string description)
{
	File::fprintf(out, "/* %s */\n", description);
	File::fprintf(out, "#define AO_%s_STRING \"%s\"\n", description, a);
}

void
write_int(int a, string description)
{
	File::fprintf(out, "/* %s */\n", description);
	File::fprintf(out, "#define AO_%s_NUMBER %d\n\n", description, a);
}

void
write_hex(int a, string description)
{
	File::fprintf(out, "/* %s */\n", description);
	File::fprintf(out, "#define AO_%s_NUMBER 0x%04x\n\n", description, a);
}

string manufacturer = "altusmetrum.org";
string product = "TeleMetrum";
string version = "0.0";
string output = "";
int serial = 1;
int user_argind = 0;
int id_vendor = 0xfffe;
int id_product = 0x000a;

argdesc argd = {
	.args = {
		{
			.var = { .arg_string = &manufacturer },
			.abbr = 'm',
			.name = "manufacturer",
			.expr_name = "manf",
			.desc = "Manufacturer name." },
		{
			.var = { .arg_string = &product },
			.abbr = 'p',
			.name = "product",
			.expr_name = "prod",
			.desc = "Product name." },
		{
			.var = { .arg_int = &id_vendor },
			.abbr = 'V',
			.name = "id_vendor",
			.expr_name = "id_v",
			.desc = "Vendor ID." },
		{
			.var = { .arg_int = &id_product },
			.abbr = 'i',
			.name = "id_product",
			.expr_name = "id_p",
			.desc = "Product ID." },
		{
			.var = { .arg_int = &serial },
			.abbr = 's',
			.name = "serial",
			.expr_name = "number",
			.desc = "Serial number." },
		{
			.var = { .arg_string = &version },
			.abbr = 'v',
			.name = "version",
			.expr_name = "string",
			.desc = "Program version." },
		{
			.var = { .arg_string = &output },
			.abbr = 'o',
			.name = "output",
			.expr_name = "out",
			.desc = "Output file." },
	},
	.prog_name = "usb descriptors",
};

void
main()
{
	string[dim(argv)-1] nargv = {[n] = argv[n+1]};
	parseargs(&argd, &nargv);
	if (output != "")
		out = File::open(output, "w");
	write_ucs2(manufacturer, "iManufacturer");
	write_ucs2(product, "iProduct");
	write_ucs2(sprintf("%06d", serial), "iSerial");
	write_int(serial, "iSerial");
	write_hex(id_product, "idProduct");
	write_hex(id_vendor, "idVendor");
	write_string(version, "iVersion");
}

main();