aboutsummaryrefslogtreecommitdiffstats
path: root/tools/usb-ptp-extract-models.pl
blob: 957d8aec6c65ea55e7cff5bf9b1143a4fd2e5aeb (plain)
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
#!/usr/bin/perl -w
# 
# USAGE: $0 </path/to/libgphoto2/camlibs/ptp2>
#
# USB PTP Dissector
#    Extracts USB devices from libgphoto2
#  This is then parsed by make-usb.py to make epan/dissectors/usb.c
# 
# (c)2013 Max Baker <max@warped.org>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

my $path = shift @ARGV || '.';

$re_hex = '0x[0-9a-f]+';

parse_file("$path/library.c",1);
parse_file("$path/music-players.h",0);

open (O,"> tools/usb-ptp-extract-models.txt") or die $!;

foreach my $vendor (sort {hex($a) <=> hex($b)} keys %devices) {
    my $p = $devices{$vendor};
    foreach my $product (sort {hex($a) <=> hex($b)} keys %$p) {
        my $pd = $product; $pd =~ s/^0x//i;
        my $v = $vendor; $v =~ s/^0x//i;
        # { 0xeb1ae355, "KWorld DVB-T 355U Digital TV Dongle" },
        #printf "    { 0x%s%s, \"%s\" },\n",$v, $pd, $p->{$product};

        printf O "%s%s %s\n", $v, $pd, $p->{$product};
    }
}

close O or die $!;

exit;

sub parse_file {
    my $file = shift;
    my $detect = shift;

    my $start = !$detect;

    open (H,"<$file") or die "Could not find $file. $!";
    while (<H>) {
        chomp;

        # Look for models[] line as start
        if (/\bmodels\[\]/) {
            $start = 1;
            next;
        }

        # Look for }; as the end
        $start = 0 if /^\s*};/;

        next unless $start;
        # Skip comment lines

        # Remove comments
        s,/\*.*\*/,,g;

        s,^\s*,,;
        s,\s*$,,;

        # Skip blank lines
        next if /^$/;
        next if m,^\s*/?\*,;

        my $line = $_;

        my ($model, $vendor, $product, $manif);

        # {"Nikon:DSC D90 (PTP mode)",  0x04b0, 0x0421, PTP_CAP|PTP_CAP_PREVIEW},
        if($line =~ m/^\{
            "([^"]+)",\s*
            ($re_hex),\s*
            ($re_hex),\s*
            /xi) {

            ($model, $vendor, $product) = ($1,$2,$3);
            $model =~ s/:/ /;
            $model =~ s/\(.*\)//;
        }
        # { "Creative", 0x041e, "ZEN X-Fi 3", 0x4169,
        # { "TrekStor", 0x0402, "i.Beat Sweez FM", 0x0611,
        if($line=~ m/^\{\s*
            "([^"]+)",\s*
            ($re_hex),\s*
            "([^"]+)",\s*
            ($re_hex),\s*
            /xi) {
            ($manif, $vendor, $model, $product) = ($1,$2,$3,$4);
            $model = "$manif $model";
        }
        
        next unless defined $vendor;

        $model =~ s/\s+/ /g;
        $model =~ s/\s*$//;

        #print "$vendor $product $model\n";
        $devices{$vendor}->{$product}=$model;
    }
}