sq epan/dissectors/pidl/rcg/rcg.cnf
[wireshark-sm.git] / tools / usb-ptp-extract-tables.pl
bloba576c2ae01e3cac26fedbe7ad6f40b13253c65f9
1 #!/usr/bin/perl -w
2 #
3 # USB PTP Dissector
4 # Extracts PTP response codes from libgphoto2
5 # This is then hand-merged into packet-usb-ptp.h
6 #
7 # (c)2013 Max Baker <max@warped.org>
8 #
9 # SPDX-License-Identifier: GPL-2.0-or-later
11 $file = shift @ARGV || 'ptp.h';
12 $outfile = 'epan/dissectors/packet-usb-ptp.h';
14 %tables = (
15 'PTP_AC' => 'StorageInfo Access Capability',
16 'PTP_AT' => 'Association Types',
17 'PTP_DPC' => 'Device Properties Codes',
18 'PTP_DPFF' => 'Device Property Form Flag',
19 'PTP_DPGS' => 'Device Property GetSet type',
20 'PTP_DTC' => 'Data Type Codes',
21 'PTP_EC' => 'Event Codes',
22 'PTP_FST' => 'FilesystemType Values',
23 'PTP_GOH' => 'GetObjectHandles',
24 'PTP_OC' => 'Operation Codes',
25 'PTP_OFC' => 'Object Format Codes',
26 'PTP_OPC' => 'MTP Object Properties',
27 'PTP_OPFF' => 'MTP Device Property Codes',
28 'PTP_PS' => 'Protection Status',
29 'PTP_RC' => 'Response Codes',
30 'PTP_ST' => 'Storage Types',
31 'PTP_VENDOR' => 'Vendor IDs',
34 %manual_entries = (
35 'PTP_OC' => [
36 "USB_PTP_FLAVOR_NIKON , 0xfc01, \"ServiceModeStart\"",
37 "USB_PTP_FLAVOR_NIKON , 0xfc02, \"ServiceModeStop\"",
41 %Flavors = qw/
42 ANDROID USB_PTP_FLAVOR_ANDROID
43 CANON USB_PTP_FLAVOR_CANON
44 CANON_EOS USB_PTP_FLAVOR_CANON
45 CASIO USB_PTP_FLAVOR_CASIO
46 EK USB_PTP_FLAVOR_KODAK
47 FUJI USB_PTP_FLAVOR_FUJI
48 LEICA USB_PTP_FLAVOR_LEICA
49 MTP USB_PTP_FLAVOR_MTP
50 NIKON USB_PTP_FLAVOR_NIKON
51 OLYMPUS USB_PTP_FLAVOR_OLYMPUS
52 OLYMPUS_OMD USB_PTP_FLAVOR_OLYMPUS
53 PARROT USB_PTP_FLAVOR_PARROT
54 PANASONIC USB_PTP_FLAVOR_PANASONIC
55 SONY USB_PTP_FLAVOR_SONY
56 SONY_QX USB_PTP_FLAVOR_SONY
59 $re_hex = '0x[0-9a-f]+';
61 open (H,"<$file") or die "Can't find gphoto2 header '$file'";
62 while (<H>) {
63 chomp;
65 next unless /^\s*#define\s+(\S+)\s+(.*)$/;
67 my ($define,$val) = ($1,$2);
68 # strip c-style comment
69 $val =~ s,/\*.*\*/,,;
70 $val =~ s,//.*,,;
71 $val =~ s/^\s*//g;
72 $val =~ s/\s*$//g;
74 #print "$define=$val\n";
75 $D{$define}=$val;
78 close H;
80 sub output_unmasked_table {
81 my ($table,$desc, $FH) = @_;
83 my $id = lc($table);
84 $id =~ s/^PTP_//i;
85 print $FH "/* $table $desc */\n";
86 print $FH "static const value_string usb_ptp_${id}_vals\[\] = {\n";
87 my @vals;
88 DEFINE:
89 foreach my $define (sort sort_D keys %D) {
90 next unless $define =~ /^${table}_(.*)/i;
91 my $subdefine = $1;
92 my $value = $D{$define};
94 push @vals, sprintf(" {%s, \"%s\"}",$value,$subdefine);
97 # now add manual entries
98 if (exists $manual_entries{$table}) {
99 for $i (0 .. $#{ $manual_entries{$table}}) {
100 push @vals, sprintf(" {%s}", $manual_entries{$table}[$i]);
104 # Add a null entry to mark the end
105 push @vals, " {0, NULL}";
106 print $FH join(",\n",@vals),"\n";
107 print $FH "};\n";
111 sub output_table {
112 my ($table,$desc, $FH) = @_;
113 my $is_masked = ($table ne "PTP_VENDOR");
115 return output_unmasked_table($table,$desc,$FH) unless $is_masked;
117 my $id = lc($table);
118 $id =~ s/^PTP_//i;
119 print $FH "/* $table $desc */\n";
120 print $FH "static const usb_ptp_value_string_masked_t usb_ptp_${id}_mvals\[\] = {\n";
121 my @vals;
122 DEFINE:
123 foreach my $define (sort sort_D keys %D) {
124 next unless $define =~ /^${table}_(.*)/i;
125 next if $define =~ /^.*_MASK/i;
126 my $subdefine = $1;
128 my $type = 'USB_PTP_FLAVOR_ALL';
129 foreach my $flavor (sort {length($b) <=> length($a)} keys %Flavors) {
130 next unless $subdefine =~ s/^${flavor}_//i;
131 $type = $Flavors{$flavor}
134 my $value = $D{$define};
135 if ($value =~ /^0x[0-9A-F]+|\d+$/i) {
136 # number or (lowercase) hex
137 $value = lc($value);
138 } elsif ($value =~ /^\(\s*([A-Z_][A-Z0-9_]*)\s*\|\s([A-Z_][A-Z0-9_]*)\s*\)$/i) {
139 # handle simple case of (A | B) where no recursive expansion
140 $value = sprintf("(%s | %s)", $D{$1}, $D{$2})
141 } else {
142 die "unrecognized value $value for $subdefine"
145 # Ok, not a subflavor
146 push @vals, sprintf(" {%-25s, %s, \"%s\"}",$type,$value,$subdefine);
149 # now add manual entries
150 if (exists $manual_entries{$table}) {
151 for $i (0 .. $#{ $manual_entries{$table}}) {
152 push @vals, sprintf(" {%s}", $manual_entries{$table}[$i]);
156 # Add a null entry to mark the end
157 push @vals, sprintf(" {%-25s, 0, NULL}","USB_PTP_FLAVOR_NONE");
158 print $FH join(",\n",@vals),"\n";
159 print $FH "};\n";
163 sub sort_D {
164 my $aa = $D{$a};
165 $aa = hex($aa) if $aa=~/^${re_hex}$/i;
166 $bb = $D{$b} || $b;
167 $bb = hex($bb) if $bb=~/^${re_hex}$/i;
169 if ($aa eq $bb) {
170 return $a cmp $b;
172 if ($aa =~ /^\d+$/ and $bb=~/^\d+$/) {
173 return $aa <=> $bb;
175 return $aa cmp $bb;
178 open (OUT,">$outfile.tmp") or die;
179 open (SRC,"<$outfile") or die;
180 $in_autogen = 0;
181 while (<SRC>) {
182 my $line = $_;
183 if ($line =~ /(?:START|END) AUTOGENERATED CODE/) {
184 print OUT $line;
185 if ($line =~ /START/) {
186 $in_autogen = 1;
187 # Output tables
188 foreach my $table (sort keys %tables) {
189 output_table($table, $tables{$table}, OUT);
191 } else {
192 $in_autogen = 0;
194 next;
196 print OUT $_ unless $in_autogen;
198 close SRC;
199 close OUT;
200 rename("$outfile.tmp", $outfile) or die;