From 7a28c18bfdff8760508aa6abf9b7388a10d0e3a7 Mon Sep 17 00:00:00 2001 From: Conrad Lara - KG6JEI Date: Fri, 13 Dec 2013 21:59:34 -0800 Subject: [PATCH] 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. --- files/www/cgi-bin/perlfunc.pm | 158 ++++++++++++++++++---------------- files/www/cgi-bin/setup | 13 ++- 2 files changed, 89 insertions(+), 82 deletions(-) diff --git a/files/www/cgi-bin/perlfunc.pm b/files/www/cgi-bin/perlfunc.pm index c8fac2cc..7af38b52 100644 --- a/files/www/cgi-bin/perlfunc.pm +++ b/files/www/cgi-bin/perlfunc.pm @@ -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 sub wifi_maxpower { - $boardtype = `/usr/local/bin/get_hardwaretype`; - chomp($boardtype); - if ($boardtype eq "bullet-m"){ - #bullet-m has a 12db offchip amp. - return 16; - } - elsif ($boardtype eq "rocket-m") { - #Rocket-m is 28dbm but has a 10db offchip amp. - return 18; - } - else + $boardinfo = hardware_info(); + if ( exists $boardinfo->{'maxpower'} ) { + return $boardinfo->{'maxpower'}; + } else { #When in doubt lets return 27 for safety. return 27; } - } sub wifi_validant { - #CMLARA -- Basic mapping of antennas to models, we really don't know all the models yet - # so we will just put the ones we know here first - $boardtype = `/usr/local/bin/get_hardwaretype`; - chomp($boardtype); - if ($boardtype eq "bullet-m"){ - %antennas = ( 1 => "N-Connector" ); - } - elsif ($boardtype eq "rocket-m") { - %antennas = ( 1 => "Chain0", 2 => "Chain1", 3 => "Diversity" ); - } - else + $boardinfo = hardware_info(); + if ( exists $boardinfo->{'antennas'} ) { + return $boardinfo->{'antennas'}; + } else { - %antennas = ( 0 => "Left", 1 => "Right", 2 => "Diversity" ); + return { 0 => "Left", 1 => "Right", 2 => "Diversity" }; } - - return %antennas; } sub wifi_defaultant { - $boardtype = `/usr/local/bin/get_hardwaretype`; - chomp($boardtype); - if ($boardtype eq "bullet-m"){ - return 1; - } - elsif ($boardtype eq "rocket-m"){ - return 3; - } - else + + $boardinfo = hardware_info(); + if ( exists $boardinfo->{'defaultant'} ) { + return $boardinfo->{'defaultant'}; + } else { #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; @@ -925,12 +961,11 @@ sub wifi_defaultant sub wifi_useschains { - $boardtype = `/usr/local/bin/get_hardwaretype`; - chomp($boardtype); - if ($boardtype eq "rocket-m"){ - return 1; - } - else + + $boardinfo = hardware_info(); + if ( exists $boardinfo->{'usechains'} ) { + return $boardinfo->{'usechains'}; + } else { return 0; } @@ -941,48 +976,21 @@ sub wifi_useschains #has increased it to a higher level. sub wifi_txpoweroffset { - $boardtype = `/usr/local/bin/get_hardwaretype`; - chomp($boardtype); - if ($boardtype eq "bullet-m"){ - return 12; - } - elsif ($boardtype eq "rocket--m"){ - return 10; - } - else + + $boardinfo = hardware_info(); + if ( exists $boardinfo->{'pwroffset'} ) { + return $boardinfo->{'pwroffset'}; + } else { 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 { - my $boardid = hardware_boardid(); - # model['boardid']= 1(supported) 0(unsupported} -1 untested - %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; - } - + $boardinfo = hardware_info(); + return $boardinfo->{'supported'}; } diff --git a/files/www/cgi-bin/setup b/files/www/cgi-bin/setup index 0e34b610..e7cdf243 100755 --- a/files/www/cgi-bin/setup +++ b/files/www/cgi-bin/setup @@ -112,11 +112,11 @@ unless($parms{reload}) } # 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 = 1 if $wifi_txpower < 1; -$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_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_distance = 0 unless defined $wifi_distance; $wifi_distance = 0 if $wifi_distance =~ /\D/; @@ -503,16 +503,15 @@ if($wifi_proto ne "disabled") print "Rx Antenna\n"; print "\n"; print "Tx Antenna\n"; print "\n";