Skip to content

Commit

Permalink
keep parsing separated from filtering
Browse files Browse the repository at this point in the history
The low-level _getDevicesFromXXX get the raw list of USB devices, and
the _getDevice function handles selection of relevant ones.
  • Loading branch information
guillomovitch committed Nov 21, 2012
1 parent 3091afd commit 3961f49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 78 deletions.
22 changes: 17 additions & 5 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Generic/USB.pm
Expand Up @@ -16,6 +16,19 @@ sub doInventory {
my $inventory = $params{inventory};

foreach my $device (_getDevices(logger => $params{logger})) {
$inventory->addEntry(
section => 'USBDEVICES',
entry => $device,
);
}
}

sub _getDevices {
my (%params) = @_;

my @devices;

foreach my $device (_getDevicesFromLsusb(%params)) {
next unless $device->{PRODUCTID};
next unless $device->{VENDORID};

Expand All @@ -40,14 +53,13 @@ sub doInventory {
}
}

$inventory->addEntry(
section => 'USBDEVICES',
entry => $device,
);
push @devices, $device;
}

return @devices;
}

sub _getDevices {
sub _getDevicesFromLsusb {
my $handle = getFileHandle(
@_,
command => 'lsusb -v',
Expand Down
32 changes: 21 additions & 11 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/USB.pm
Expand Up @@ -23,31 +23,41 @@ sub doInventory {
}

sub _getDevices {
my (%params) = @_;

my @devices;
my $seen;

foreach my $device (_getDevicesFromWMI(%params)) {
next if $device->{VENDORID} =~ /^0+$/;

# avoid duplicates
next if $seen->{$device->{SERIAL}}++;

# pseudo serial generated by windows
delete $device->{SERIAL} if $device->{SERIAL} =~ /&/;

push @devices, $device;
}

return @devices;
}

sub _getDevicesFromWMI {
my @devices;

foreach my $object (getWMIObjects(
class => 'CIM_LogicalDevice',
properties => [ qw/DeviceID Name/ ]
)) {
next unless $object->{DeviceID} =~ /^USB\\VID_(\w+)&PID_(\w+)\\(.*)/;

my $device = {
push @devices, {
NAME => $object->{Name},
VENDORID => $1,
PRODUCTID => $2,
SERIAL => $3
};

next if $device->{VENDORID} =~ /^0+$/;

# avoid duplicates
next if $seen->{$device->{SERIAL}}++;

# pseudo serial generated by windows
delete $device->{SERIAL} if $device->{SERIAL} =~ /&/;

push @devices, $device;
}

return @devices;
Expand Down
62 changes: 0 additions & 62 deletions t/inventory/generic/usb.t
Expand Up @@ -9,13 +9,6 @@ use FusionInventory::Agent::Task::Inventory::Input::Generic::USB;

my %tests = (
'dell-xt2' => [
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '0a5c',
SUBCLASS => '0',
Expand All @@ -40,73 +33,18 @@ my %tests = (
CLASS => '254',
PRODUCTID => '8160'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '0a5c',
SERIAL => '0123456789ABCD',
SUBCLASS => '0',
CLASS => '254',
PRODUCTID => '5801'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '1b96',
SUBCLASS => '0',
CLASS => '0',
PRODUCTID => '0001'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0001'
},
{
VENDORID => '047d',
SUBCLASS => '1',
CLASS => '3',
PRODUCTID => '101f'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0002'
},
{
VENDORID => '1d6b',
SERIAL => '0000',
SUBCLASS => '0',
CLASS => '9',
PRODUCTID => '0002'
}
]
);
Expand Down

0 comments on commit 3961f49

Please sign in to comment.