add usb_modeswitch.d converter
[project/usbmode.git] / convert-modeswitch.pl
1 #!/usr/bin/perl
2 use strict;
3
4 my $msg_ctr = 0;
5 my %messages;
6 my %devices;
7
8 sub add_message {
9 my $msg = shift;
10 my $val = $messages{$msg};
11
12 $val or do {
13 $val = $msg_ctr++;
14 $messages{$msg} = $val;
15 };
16
17 return $val;
18 }
19
20 sub add_device($) {
21 my $id = shift;
22 my $dev = {};
23 my $match;
24
25 $id =~ /^(\w{4}:\w{4})(:.*)?/ or do {
26 warn "Invalid device ID string $id\n";
27 return $dev;
28 };
29
30 $id = $1;
31 $match = $2 or $match = "*";
32
33 $devices{$id} or $devices{$id} = {};
34 $devices{$id}->{$match} = $dev;
35
36 return $dev;
37 }
38
39 sub add_hex {
40 $_[0] =~ s/^0x//;
41 return hex($_[0]);
42 }
43
44 sub add_mode {
45 $_[1] =~ s/^(\w+)Mode$/$1/;
46 return $_[1];
47 }
48
49 my $hex_option = [ undef, \&add_hex ];
50 my $msg_option = [ undef, \&add_message ];
51 my $mode_option = [ "Mode", \&add_mode ];
52 my %options = (
53 TargetVendor => $hex_option,
54 TargetProductList => [ "TargetProduct", sub { return [ map(hex,split(/,/, $_[0])) ]; } ],
55 TargetProduct => [ "TargetProduct", sub { return [ hex($_[0]) ]; } ],
56 TargetClass => $hex_option,
57 MessageContent => $msg_option,
58 MessageContent2 => $msg_option,
59 MessageContent3 => $msg_option,
60 WaitBefore => [ ],
61 DetachStorageOnly => [ ],
62 HuaweiMode => $mode_option,
63 SierraMode => $mode_option,
64 SonyMode => $mode_option,
65 QisdaMode => $mode_option,
66 GCTMode => $mode_option,
67 KobilMode => $mode_option,
68 SequansMode => $mode_option,
69 MobileActionMode => $mode_option,
70 CiscoMode => $mode_option,
71 NoDriverLoading => [],
72 MessageEndpoint => $hex_option,
73 ReleaseDelay => [],
74 NeedResponse => [],
75 ResponseEndpoint => $hex_option,
76 ResetUSB => [],
77 InquireDevice => [],
78 CheckSuccess => $hex_option,
79 Interface => $hex_option,
80 Configuration => $hex_option,
81 AltSetting => $hex_option,
82 );
83
84 sub parse_file($) {
85 my $file = shift;
86 my $id;
87
88 $id = $file;
89 $file =~ /\/?([^\/]+)$/ and $id = $1;
90
91 my $dev = add_device($id);
92
93 open FILE, "<$file" or die "Cannot open file '$file'\n";
94 while (<FILE>) {
95 chomp;
96 s/^\s*(.+?)\s*$/$1/;
97 s/#.+$//;
98 next unless /\w/;
99 /(\w+)\s*=\s*(.+)\s*/ or do {
100 warn "Invalid Line: $_";
101 next;
102 };
103
104 my ($var, $val) = ($1, $2);
105 $val =~ s/^"(.+)"$/$1/;
106
107 my $opt = $options{$var};
108 $opt or do {
109 warn "Unrecognized option $var in file $file\n";
110 next;
111 };
112
113 $opt->[1] and $val = &{$opt->[1]}($val, $var);
114 $opt->[0] and $var = $opt->[0];
115 $dev->{$var} = $val;
116 }
117 }
118
119 foreach my $file (@ARGV) {
120 parse_file $file;
121 }
122
123 sub json_chr {
124 my $chr = shift;
125
126 $chr eq "\b" and return "\\b";
127 $chr eq "\n" and return "\\n";
128 $chr eq "\r" and return "\\r";
129 $chr eq "\t" and return "\\t";
130 $chr eq "\\" and return "\\\\";
131 $chr eq "\"" and return "\\\"";
132 $chr eq '/' and return "\\/";
133 return sprintf("\\u%04x", ord($chr));
134 };
135
136 sub json_str {
137 $_[0] =~ s/([\x00- \/"\\])/json_chr($1)/eg;
138 return $_[0];
139 }
140
141 sub json_val($$) {
142 my ($val, $type) = (shift, shift);
143 $type eq 'bool' and $val = $val > 0 ? "true" : "false";
144 $type eq 'string' and $val = "\"$val\"";
145 return $val;
146 }
147
148 sub dev_opt {
149 my ($val, $name, $type, $sep) = (shift, shift, shift, shift);
150 return unless defined($val);
151 if ($type =~ /array:(.+)/) {
152 $type = $1;
153 my @val = @$val;
154 undef $val;
155 foreach my $elem (@val) {
156 if (defined $val) {
157 $val = "$val, "
158 } else {
159 $val = "";
160 }
161 $val .= json_val($elem, $type);
162 }
163 $val = "[ $val ]";
164 } else {
165 $val = json_val($val, $type);
166 }
167 print "$$sep\t\t\t\t\"".json_str($name)."\": $val";
168 $$sep = ",\n";
169 }
170
171 print <<EOF;
172 {
173 "messages" : [
174 EOF
175 my $suffix = "";
176 foreach my $msg (sort { $messages{$a} <=> $messages{$b} } keys %messages) {
177 print "$suffix\t\t\"".json_str($msg)."\"";
178 $suffix = ",\n";
179 }
180 print <<EOF;
181
182 ],
183
184 "devices" : {
185 EOF
186 my $dev_sep = "";
187 foreach my $devid (sort keys %devices) {
188 my $dev = $devices{$devid};
189
190 print "$dev_sep\t\t\"".json_str($devid)."\": {\n";
191 $dev_sep = ",\n";
192
193 my $match_sep = "";
194 foreach my $match (sort keys %$dev) {
195 my $cur = $dev->{$match};
196 my $sep = "";
197
198 print "$match_sep\t\t\t\"".json_str($match)."\": {\n";
199 $match_sep = ",\n";
200
201 dev_opt($cur->{TargetVendor}, "t_vendor", "int", \$sep);
202 dev_opt($cur->{TargetProduct}, "t_product", "array:int", \$sep);
203 dev_opt($cur->{TargetClass}, "t_class", "int", \$sep);
204 dev_opt($cur->{DetachStorageOnly}, "detach_storage", "bool", \$sep);
205 dev_opt($cur->{Mode}, "mode", "string", \$sep);
206 dev_opt($cur->{NoDriverLoading}, "no_driver", "bool", \$sep);
207 dev_opt($cur->{MessageEndpoint}, "msg_endpoint", "int", \$sep);
208 dev_opt($cur->{MessageContent}, "msg", "int", \$sep);
209 dev_opt($cur->{MessageContent2}, "msg2", "int", \$sep);
210 dev_opt($cur->{MessageContent3}, "msg3", "int", \$sep);
211 dev_opt($cur->{WaitBefore}, "wait", "int", \$sep);
212 dev_opt($cur->{ReleaseDelay}, "release_delay", "int", \$sep);
213 dev_opt($cur->{NeedResponse}, "response", "bool", \$sep);
214 dev_opt($cur->{ResponseEndpoint}, "response_endpoint", "int", \$sep);
215 dev_opt($cur->{ResetUSB}, "reset", "bool", \$sep);
216 dev_opt($cur->{InquireDevice}, "inquire", "int", \$sep);
217 dev_opt($cur->{CheckSuccess}, "check", "bool", \$sep);
218 dev_opt($cur->{Interface}, "interface", "int", \$sep);
219 dev_opt($cur->{Configuration}, "config", "int", \$sep);
220 dev_opt($cur->{AltSetting}, "alt", "int", \$sep);
221 print "\n\t\t\t}";
222 }
223 print "\n\t\t}"
224 }
225 print <<EOF;
226
227 }
228 }
229 EOF