Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.2.x' into 2.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonéri Le Bouder committed Feb 15, 2012
2 parents 89e58a2 + c638178 commit 3052f82
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 29 deletions.
2 changes: 1 addition & 1 deletion fusioninventory-win32-service
Expand Up @@ -100,7 +100,7 @@ sub getMessage {
sub startAgent {
return if $thread;
$thread = threads->create(sub {
my $agent = new FusionInventory::Agent(
my $agent = FusionInventory::Agent->new(
confdir => $directory . '/../../etc/fusioninventory',
datadir => $directory . '/../../share',
vardir => $directory . '/../../var',
Expand Down
6 changes: 5 additions & 1 deletion lib/FusionInventory/Agent/Task.pm
Expand Up @@ -110,9 +110,13 @@ the logger object to use (default: a new stderr logger)
=back
=head2 isEnabled()
This is a method to be implemented by each subclass.
=head2 run()
This is the method to be implemented by each subclass.
This is a method to be implemented by each subclass.
=head2 getOptionsFromServer($response, $name, $feature)
Expand Down
50 changes: 33 additions & 17 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/Printers.pm
Expand Up @@ -101,7 +101,6 @@ sub doInventory {
}
}

# Search serial when connected in USB
sub _getUSBPrinterSerial {
my ($portName) = @_;

Expand All @@ -110,45 +109,62 @@ sub _getUSBPrinterSerial {
}) or die "Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR";

# first, find the USB container ID for this printer
my $usbId;
my $usbprintKey = $machKey->{"SYSTEM/CurrentControlSet/Enum/USBPRINT"};
my $prefix = _getUSBPrefix(
$machKey->{"SYSTEM/CurrentControlSet/Enum/USBPRINT"},
$portName
);
return unless $prefix;

# second, get the serial number from the ID container entry
my $serial = _getUSBSerial(
$machKey->{"SYSTEM/CurrentControlSet/Enum/USB"},
$prefix
);

return $serial;
}

sub _getUSBPrefix {
my ($printKey, $portName) = @_;

# find the printer entry matching given portname
PRINTER: foreach my $printerKey (values %$usbprintKey) {
foreach my $printerKey (values %$printKey) {
# look for a subkey with expected content
foreach my $subKey (values %$printerKey) {
foreach my $subKeyName (keys %$printerKey) {
my $subKey = $printerKey->{$subKeyName};
next unless
$subKey->{'Device Parameters/'} &&
$subKey->{'Device Parameters/'}->{'/PortName'} &&
$subKey->{'Device Parameters/'}->{'/PortName'} eq $portName;
# got it
$usbId = $subKey->{'/ContainerID'};
last PRINTER;
my $prefix = $subKeyName;
$prefix =~ s{&$portName/$}{};
return $prefix;
};
}

return unless $usbId;
return;
}

# second, get the serial number from the ID container entry
my $serial;
my $usbKey = $machKey->{"SYSTEM/CurrentControlSet/Enum/USB"};
sub _getUSBSerial {
my ($usbKey, $prefix) = @_;

# find the device entry matching given container Id
DEVICE: foreach my $deviceKey (values %$usbKey) {
foreach my $deviceKey (values %$usbKey) {
# look for a subkey with expected content
foreach my $subKeyName (keys %$deviceKey) {
my $subKey = $deviceKey->{$subKeyName};
next unless
$subKey->{'/ContainerId'} &&
$subKey->{'/ContainerId'} eq $usbId;
$subKey->{'/ParentIdPrefix'} &&
$subKey->{'/ParentIdPrefix'} eq $prefix;
# got it
$serial = $subKeyName;
my $serial = $subKeyName;
$serial =~ s{/$}{};
last DEVICE;
return $serial;
}
}

return $serial;
return;
}

1;
Expand Up @@ -17,9 +17,7 @@ sub doInventory {

foreach my $object (getWmiObjects(
class => 'Win32_SystemSlot',
properties => [ qw/
Name Description SlotDesignation Status Shared
/ ]
properties => [ qw/Name Description SlotDesignation Status/ ]
)) {

$inventory->addEntry(
Expand All @@ -29,7 +27,6 @@ sub doInventory {
DESCRIPTION => $object->{Description},
DESIGNATION => $object->{SlotDesignation},
STATUS => $object->{Status},
SHARED => $object->{Shared}
}
);
}
Expand Down
Expand Up @@ -102,9 +102,15 @@ sub _dateFormat {
## no critic (ExplicitReturnUndef)
return undef unless $date;

return unless $date =~ /^(\d{4})(\d{2})(\d{2})/;
if ($date =~ /^(\d{4})(\d{1})(\d{2})$/) {
return "$3/0$2/$1";
}

if ($date =~ /^(\d{4})(\d{2})(\d{2})$/) {
return "$3/$2/$1";
}

return "$3/$2/$1";
return undef;
}

sub _getSoftwares {
Expand Down
19 changes: 15 additions & 4 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/USB.pm
Expand Up @@ -16,6 +16,18 @@ sub doInventory {

my $inventory = $params{inventory};

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

sub _getUSBDevices {

my @devices;

foreach my $object (getWmiObjects(
class => 'CIM_LogicalDevice',
properties => [ qw/DeviceID Name/ ]
Expand All @@ -37,11 +49,10 @@ sub doInventory {
# avoid duplicates
next if $seen->{$device->{SERIAL}}++;

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

return @devices;
}

1;
Binary file removed resources/win32/printer/xppro2/USB.reg
Binary file not shown.
Binary file removed resources/win32/printer/xppro2/USBPRINT.reg
Binary file not shown.
Binary file added resources/win32/registry/7-USB.reg
Binary file not shown.
Binary file added resources/win32/registry/7-USBPRINT.reg
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file added resources/win32/registry/xppro2-USB.reg
Binary file not shown.
Binary file added resources/win32/registry/xppro2-USBPRINT.reg
Binary file not shown.
Binary file added resources/win32/wmi/7-CIM_LogicalDevice
Binary file not shown.
102 changes: 102 additions & 0 deletions t/inventory/windows/printers.t
@@ -0,0 +1,102 @@
#!/usr/bin/perl

use strict;
use warnings;

use English qw(-no_match_vars);
use Test::More;

BEGIN {
# use mock modules for non-available ones
push @INC, 't/fake/windows' if $OSNAME ne 'MSWin32';
}

use FusionInventory::Agent::Task::Inventory::Input::Win32::Printers;

my %tests = (
xppro1 => {
USB001 => [ '6&397bdcac&0', '49R8Ka' ],
USB002 => [ '6&2ad9257f&0', '5&19d1ce61&0&2' ],
USB003 => [ '6&1605722f&0', '5&2377f6ef&0&2' ],
},
xppro2 => {
USB001 => [ '6&1086615&0', 'J5J126789' ],
USB003 => [ '6&159b6df2&0', 'JV40VNJ' ],
USB004 => [ '7&20bd29b5&0', '6&28e27c3d&0&0000' ],
},
7 => {
USB001 => [ '7&17e8a3c3&0', 'MY26K1K34C2L' ],
}
);

my $plan = 0;
foreach my $test (keys %tests) {
$plan += 2 * scalar (keys $tests{$test});
}
plan tests => $plan;

foreach my $test (keys %tests) {
my $printKey = load_registry("resources/win32/registry/$test-USBPRINT.reg");
my $usbKey = load_registry("resources/win32/registry/$test-USB.reg");
foreach my $port (keys $tests{$test}) {
my $prefix = FusionInventory::Agent::Task::Inventory::Input::Win32::Printers::_getUSBPrefix($printKey, $port);
my $serial = FusionInventory::Agent::Task::Inventory::Input::Win32::Printers::_getUSBSerial($usbKey, $prefix);

is($prefix, $tests{$test}->{$port}->[0], "prefix for printer $port");
is($serial, $tests{$test}->{$port}->[1], "serial for printer $port");
}
}

sub load_registry {
my ($file) = @_;

my $root_offset;
my $root_key = {};
my $current_key;

open (my $handle, '<', $file) or die "can't open $file: $ERRNO";

# this is a windows file
binmode $handle, ':encoding(UTF-16LE)';
binmode $handle, ':crlf';

while (my $line = <$handle>) {

if ($line =~ /^ \[ ([^]]+) \] $/x) {
my $path = $1;
my @path = split(/\\/, $path);

if ($root_offset) {
splice @path, 0, $root_offset;
$current_key = $root_key;
foreach my $element (@path) {
my $key_path = $element . '/';

if (!defined $current_key->{$key_path}) {
my $new_key = {};
$current_key->{$key_path} = $new_key;
}

$current_key = $current_key->{$key_path};
}
} else {
$root_offset = scalar @path;
}
next;
}

if ($line =~ /^ " ([^"]+) " = dword:(\d+)/x) {
$current_key->{'/' . $1} = "0x$2";
next;
}

if ($line =~ /^ " ([^"]+) " = " ([^"]+) "/x) {
$current_key->{'/' . $1} = $2;
next;
}

}
close $handle;

return $root_key;
}

0 comments on commit 3052f82

Please sign in to comment.