#!/usr/bin/perl

use Net::Kismet;
use Data::Dumper;

$numnetworks = 0;
$numpackets = 0;

$gpslight = 'b';
$netlight = 0;
$packetlight = 0;

$k = new Net::Kismet('10.254.254.1', 2501);

# Just enough to control our LEDs
$k->register('info', \&infohandler, 'networks,packets');
$k->register('gps', \&gpsfixhandler, 'fix');

KismetRun($k);

sub gpsfixhandler($) {
    $vars = shift;

    if (($vars->{'fix'}) > 1) {
        $gpslight = 1;
    } else {
        $gpslight = "b";
    }

    writeled();
}

sub infohandler($) {
    $vars = shift;

    if (($vars->{'networks'}) != $numnetworks) {
        $numnetworks = ($vars->{'networks'});
        $netlight = 1;
    } else {
        $netlight = 0;
    }

	if (($vars->{'packets'}) != $numpackets) {
		$numpackets = ($vars->{'packets'});
		$packetlight = 1;
	} else {
		$packetlight = 0;
	}

    writeled();
}

sub writeled() {
    open LEDFIL, ">/tmp/led_state";

    print LEDFIL "$packetlight$netlight$gpslight\n";

    close LEDFIL;
}

