Refactor code for better hardware managment

Refactor to create a central hash matrix that provides details on
devices.

The if else else else tree was going to get very messy based on
the realization that each device has custom attributes and the
need to provide diffrent settings for each unit.

Allows easier managment long term.
This commit is contained in:
Conrad Lara - KG6JEI 2013-12-13 21:59:34 -08:00
parent d45cbb1b6b
commit 7a28c18bfd
2 changed files with 89 additions and 82 deletions

View File

@ -864,59 +864,95 @@ sub validate_longitude
} }
# Get boardid
sub hardware_boardid
{
my $boardid = `cat /sys/devices/pci0000:00/0000:00:00.0/subsystem_device`;
chomp($boardid);
return $boardid;
}
# Return a hashref with device details
sub hardware_info
{
%model = (
'0x168c' => {
'name' => 'NannoBridge M3',
'comment' => 'Not Tested',
'supported' => '-1',
},
'0xc2a2' => {
'name' => 'Bullet 2 HP',
'comment' => 'Not enough Ram or flash',
'supported' => '-1',
},
'0xe1b2' => {
'name' => 'Rocket M2',
'comment' => '',
'supported' => '1',
'maxpower' => '18',
'pwroffset' => '10',
'antennas' => { 1 => "Chain0", 2 => "Chain1", 3 => "Diversity"},
'defaultant' => 3,
'usechains' => 1,
},
'0xe202' => {
'name' => 'Bullet M2',
'comment' => '',
'supported' => '1',
'maxpower' => '16',
'pwroffset' => '12',
'antennas' => { 1 => 'N Connector' },
'defaultant' => 1,
'usechains' => 0,
},
);
$boardid = hardware_boardid();
if ( exists $model{ $boardid } ){
return $model{$boardid};
} else
{
return { 'name' => 'Unknown Hardware', => 'comment' => "We do not have this hardware in our database", supported => '-1',} ;
}
}
# Return maximum dbm value for tx power # Return maximum dbm value for tx power
sub wifi_maxpower sub wifi_maxpower
{ {
$boardtype = `/usr/local/bin/get_hardwaretype`; $boardinfo = hardware_info();
chomp($boardtype); if ( exists $boardinfo->{'maxpower'} ) {
if ($boardtype eq "bullet-m"){ return $boardinfo->{'maxpower'};
#bullet-m has a 12db offchip amp. } else
return 16;
}
elsif ($boardtype eq "rocket-m") {
#Rocket-m is 28dbm but has a 10db offchip amp.
return 18;
}
else
{ {
#When in doubt lets return 27 for safety. #When in doubt lets return 27 for safety.
return 27; return 27;
} }
} }
sub wifi_validant sub wifi_validant
{ {
#CMLARA -- Basic mapping of antennas to models, we really don't know all the models yet $boardinfo = hardware_info();
# so we will just put the ones we know here first if ( exists $boardinfo->{'antennas'} ) {
$boardtype = `/usr/local/bin/get_hardwaretype`; return $boardinfo->{'antennas'};
chomp($boardtype); } else
if ($boardtype eq "bullet-m"){
%antennas = ( 1 => "N-Connector" );
}
elsif ($boardtype eq "rocket-m") {
%antennas = ( 1 => "Chain0", 2 => "Chain1", 3 => "Diversity" );
}
else
{ {
%antennas = ( 0 => "Left", 1 => "Right", 2 => "Diversity" ); return { 0 => "Left", 1 => "Right", 2 => "Diversity" };
} }
return %antennas;
} }
sub wifi_defaultant sub wifi_defaultant
{ {
$boardtype = `/usr/local/bin/get_hardwaretype`;
chomp($boardtype); $boardinfo = hardware_info();
if ($boardtype eq "bullet-m"){ if ( exists $boardinfo->{'defaultant'} ) {
return 1; return $boardinfo->{'defaultant'};
} } else
elsif ($boardtype eq "rocket-m"){
return 3;
}
else
{ {
#Most likely to catch all models. Some have 3 and start at 1, some start at 0, others have 1 at 1, etc #Most likely to catch all models. Some have 3 and start at 1, some start at 0, others have 1 at 1, etc
return 1; return 1;
@ -925,12 +961,11 @@ sub wifi_defaultant
sub wifi_useschains sub wifi_useschains
{ {
$boardtype = `/usr/local/bin/get_hardwaretype`;
chomp($boardtype); $boardinfo = hardware_info();
if ($boardtype eq "rocket-m"){ if ( exists $boardinfo->{'usechains'} ) {
return 1; return $boardinfo->{'usechains'};
} } else
else
{ {
return 0; return 0;
} }
@ -941,48 +976,21 @@ sub wifi_useschains
#has increased it to a higher level. #has increased it to a higher level.
sub wifi_txpoweroffset sub wifi_txpoweroffset
{ {
$boardtype = `/usr/local/bin/get_hardwaretype`;
chomp($boardtype); $boardinfo = hardware_info();
if ($boardtype eq "bullet-m"){ if ( exists $boardinfo->{'pwroffset'} ) {
return 12; return $boardinfo->{'pwroffset'};
} } else
elsif ($boardtype eq "rocket--m"){
return 10;
}
else
{ {
return 0; return 0;
} }
} }
sub hardware_boardid
{
my $boardid = `cat /sys/devices/pci0000:00/0000:00:00.0/subsystem_device`;
chomp($boardid);
return $boardid;
}
sub is_hardware_supported sub is_hardware_supported
{ {
my $boardid = hardware_boardid(); $boardinfo = hardware_info();
# model['boardid']= 1(supported) 0(unsupported} -1 untested return $boardinfo->{'supported'};
%model = (
'0x168c' => -1, # NanoBridge M3
'0xc2a2' => 0, # Bullet 2 HP
'0xe1b2' => 0, # Rocket M2
'0xe202' => 1, # Bullet M2
);
if (exists $model{$boardid})
{
return $model{$boardid};
}
else
{
return -1;
}
} }

View File

@ -112,11 +112,11 @@ unless($parms{reload})
} }
# sanitize the active settings # sanitize the active settings
%valid_ant = wifi_validant(); $valid_ant = wifi_validant();
$wifi_txpower = wifi_maxpower() if not defined $wifi_txpower or $wifi_txpower > wifi_maxpower(); $wifi_txpower = wifi_maxpower() if not defined $wifi_txpower or $wifi_txpower > wifi_maxpower();
$wifi_txpower = 1 if $wifi_txpower < 1; $wifi_txpower = 1 if $wifi_txpower < 1;
$wifi_rxant = wifi_defaultant() if not defined $wifi_rxant or not exists $valid_ant{$wifi_rxant}; $wifi_rxant = wifi_defaultant() if not defined $wifi_rxant or not exists $valid_ant->{$wifi_rxant};
$wifi_txant = wifi_defaultant() if not defined $wifi_txant or not exists $valid_ant{$wifi_txant}; $wifi_txant = wifi_defaultant() if not defined $wifi_txant or not exists $valid_ant->{$wifi_txant};
$wifi_distance = 0 unless defined $wifi_distance; $wifi_distance = 0 unless defined $wifi_distance;
$wifi_distance = 0 if $wifi_distance =~ /\D/; $wifi_distance = 0 if $wifi_distance =~ /\D/;
@ -503,16 +503,15 @@ if($wifi_proto ne "disabled")
print "<tr><td><nobr>Rx Antenna</nobr></td>\n"; print "<tr><td><nobr>Rx Antenna</nobr></td>\n";
print "<td><select name=wifi_rxant>\n"; print "<td><select name=wifi_rxant>\n";
%validants = wifi_validant(); $validants = wifi_validant();
while (($value, $name) = each (%validants)){ while (($value, $name) = each (%{$validants})){
selopt($name, $value, $wifi_rxant); selopt($name, $value, $wifi_rxant);
} }
print "</select></td></tr>\n"; print "</select></td></tr>\n";
print "<tr><td><nobr>Tx Antenna</nobr></td>\n"; print "<tr><td><nobr>Tx Antenna</nobr></td>\n";
print "<td><select name=wifi_txant>\n"; print "<td><select name=wifi_txant>\n";
%validants = wifi_validant(); while (($value, $name) = each (%{$validants})){
while (($value, $name) = each (%validants)){
selopt($name, $value, $wifi_txant); selopt($name, $value, $wifi_txant);
} }
print "</select></td></tr>\n"; print "</select></td></tr>\n";