#!/usr/bin/perl =head1 navilink.pl This Script implements the NaviLink protocol to communicate with a Locosys NaviGPS via serial USB Please note: This script is far from complete. If you want to help, refer to the protocol specification at http://wiki.splitbrain.org/navilink at send patches in unified diff format. Copyright (c) 2007, Andreas Gohr Contributors: Andreas Gohr Martijn van Oosterhout Nick Lamb Richard Fairhurst All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Andreas Gohr nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =cut $| = 1; use Device::SerialPort; use Data::Dumper; use Getopt::Std; use constant PID_SYNC => "\xd6"; use constant PID_ACK => "\x0c"; use constant PID_NAK => "\x00"; use constant PID_QRY_INFORMATION => "\x20"; use constant PID_QRY_FW_VERSION => "\xfe"; use constant PID_DATA => "\x03"; use constant PID_ADD_A_WAYPOINT => "\x3C"; use constant PID_QRY_WAYPOINTS => "\x28"; use constant PID_QRY_ROUTE => "\x24"; use constant PID_DEL_WAYPOINT => "\x36"; use constant PID_DEL_ALL_WAYPOINT => "\x37"; use constant PID_DEL_ROUTE => "\x34"; use constant PID_DEL_ALL_ROUTE => "\x35"; use constant PID_ADD_A_ROUTE => "\x3D"; use constant PID_ERASE_TRACK => "\x11"; use constant PID_READ_TRACKPOINTS => "\x14"; use constant PID_CMD_OK => "\xf3"; use constant PID_CMD_FAIL => "\xf4"; use constant PID_QUIT => "\xf2"; my %OPT; =head2 sendRawPacket I I Sends a package over the line. =cut sub sendRawPacket { my $type = shift(); my $data = shift(); my $packet = "\xA0\xA2"; # start sequence $packet .= pack('v',length($data)+1); # payload length (incl. packet type) $packet .= $type; # packet type $packet .= $data; # data $packet .= pack('v',checkSum($type.$data)); # checksum $packet .= "\xB0\xB3"; # end sequence print STDERR '-> '.hexdump($packet)."\n" if($OPT{'v'}); $DEV->write($packet); } =head2 readPacket Read a packet from the line and return type and data in an array. =cut sub readPacket { my ($rl, $b, $msg); my $start = time(); $msg = ""; do { ($rl, $b) = $DEV->read(1); $msg .= $b; while( length($msg) > 2 and substr($msg,0,2) ne "\xA0\xA2" ) { $msg = substr($msg,1); } if( (time() - $start) > 8){ printf STDERR "Timeout while reading. Last bytes where '%s'.\n", hexdump(substr($msg,-10)); return undef; } } until (length($msg) > 4 and length($msg)-8 == unpack("v", substr($msg,2,2))); print STDERR '<- '.hexdump($msg)."\n" if($OPT{'v'}); my $payload = substr($msg,4,-4); my $sum = unpack("v",substr($msg,-4,2)); if($sum != checkSum($payload)) { nicedie('Expected checksum '.checkSum($payload).', got '.$sum); } my $type = substr($payload,0,1); my $data = substr($payload,1); return ($type,$data); } =head2 downloadInfo Reads general information from the device and returns it as hash =cut sub downloadInfo { sendRawPacket(PID_QRY_INFORMATION,""); my ($type,$data) = readPacket(); if($type ne PID_DATA){ print STDERR "Got no info data\n"; return undef; } my @bytes = unpack('vCCVVvv',$data); my %info = ( 'waypoints' => $bytes[0], 'routes' => $bytes[1], 'tracks' => $bytes[2], 'trackbuffer' => $bytes[3], 'serial' => $bytes[4], 'trackpoints' => $bytes[5], 'protocol' => $bytes[6], 'username' => substr($data,-16) ); return %info; } =head2 downloadFWInfo Reads the firmware information =cut sub downloadFWInfo { sendRawPacket(PID_QRY_FW_VERSION,''); my ($type,$data) = readPacket(); if($type ne PID_DATA){ print STDERR "Got no info data\n"; return undef; } $data=~s/,.*$//; return $data; } =head2 downloadWaypointData Reads all Waypoints from the device and prints it as GPX data =cut sub downloadWaypointData { my $dogpx = shift(); # should a GPX be created? (or do we just need waypoint names) my @wpnames; # check for available waypoints my %info = downloadInfo(); if(!defined(%info) || !$info{'waypoints'}){ if($dogpx){ nicedie("There are no waypoints available"); }else{ return @wpnames; } } my ($type,$data); my $read = 0; # waypoints alreay read my $max = $info{'waypoints'}; # waypoints to read my $points = ''; while($read < $max){ my $toread = 32; $toread = ($max - $read) if( ($max - $read) < $toread); # request data my $msg = pack("V",$read).pack("v",$toread)."\x01"; sendRawPacket(PID_QRY_WAYPOINTS,$msg); # read answer ($type,$data) = readPacket(); if($type ne PID_DATA){ # something went wrong print STDERR "Did not receive expected waypoint data"; return 0; } $points .= $data; # increase read counter $read += $toread; } # Create GPX file if($dogpx){ print OUT "\n"; print OUT "\n"; } # now parse the waypoints for(my $i=0; $i<$read; $i++){ my @tp = unpack("vva7CllvC6CCCC",substr($points,$i*32,32)); #print hexdump(substr($points,$i*32,32))."\n"; #print Dumper(\@tp); $tp[2] =~ s/[\x00\s]+$//; if($dogpx){ printf OUT "\n", $tp[4]/10000000, $tp[5]/10000000; printf OUT " %s\n", $tp[2]; printf OUT " %f\n", $tp[6]*0.3048; #feet to meters printf OUT " %s\n", waypointSymbol($tp[13]); printf OUT " \n", $tp[7]+2000, $tp[8], $tp[9], $tp[10], $tp[11], $tp[12]; printf OUT "\n"; }else{ $wpnames[$tp[1]] = $tp[2]; } } if($dogpx){ print OUT "\n"; return 1; }else{ return @wpnames; } } =head2 downloadTrackData Reads all Trackpoints from the device and prints it as GPX data =cut sub downloadTrackData { my %info = downloadInfo(); if(!defined(%info) || !$info{'trackpoints'}){ nicedie("There are no trackpoints available"); } my ($type,$data); my $addr = $info{'trackbuffer'}; # buffer address my $read = 0; # bytes already read my $max = $info{'trackpoints'} * 32; # maximum bytes to read my $track = ''; while($read < $max){ my $toread = 512*32; $toread = ($max - $read) if( ($max - $read) < $toread); # request data my $msg = pack("V",$addr+$read).pack("v",$toread)."\x00"; sendRawPacket(PID_READ_TRACKPOINTS,$msg); # read answer ($type,$data) = readPacket(); if($type ne PID_DATA){ # something went wrong print STDERR "Did not receive expected Trackpoint data"; return 0; } $track .= $data; # increase read counter $read += $toread; # send ACK sendRawPacket(PID_ACK,""); } # Create GPX file print OUT "\n"; print OUT "\n"; print OUT "\n"; print OUT "\n"; # now parse the track log for(my $i=0; $i<$read; $i+=32){ my @tp = unpack("vvVVVVvCCCCCCCCCC",substr($track,$i,32)); for (my $j=2; $j<6; $j++) { if ($tp[$j]>0x7FFFFFFF) { $tp[$j]=$tp[$j]-0x80000000-0x80000000; } } #print hexdump(substr($track,$i,32))."\n"; #print Dumper(\@tp); printf OUT "\n", $tp[4]/10000000, $tp[5]/10000000; printf OUT " %f\n", $tp[6]*0.3048; #feet to meters printf OUT " \n", $tp[7]+2000, $tp[8], $tp[9], $tp[10], $tp[11], $tp[12]; printf OUT " %02d\n",$tp[14]*2; printf OUT "\n"; } print OUT "\n"; print OUT "\n"; print OUT "\n"; return 1; } =head2 deleteTrackData Deletes all trackpoints from the device =cut sub deleteTrackData { my %info = downloadInfo(); if(!defined(%info) || !$info{'trackpoints'}){ nicedie("There are no trackpoints available"); } my $addr = $info{'trackbuffer'}; # buffer address dowait('All track data will be deleted from the device!'); my $msg = pack("V",$addr)."\x00\x00\x00"; sendRawPacket(PID_ERASE_TRACK,$msg); my ($type,$data) = readPacket(); nicedie("Trackpoint deletion failed.") if($type ne PID_CMD_OK); if ($info{'trackpoints'} > 4096){ $msg = pack("V",$addr + (4096 * 32))."\x00\x00\x00"; sendRawPacket(PID_ERASE_TRACK,$msg); ($type,$data) = readPacket(); nicedie("Trackpoint deletion failed.") if($type ne PID_CMD_OK); } } =head2 deleteAllWaypointData Deletes all waypoints from the device =cut sub deleteAllWaypointData { my %info = downloadInfo(); if(!defined(%info) || !$info{'waypoints'}){ nicedie("There are no waypoints available"); } dowait('All waypoints will be deleted from the device!'); my $msg = "\x00\xf0\x00\x00"; sendRawPacket(PID_DEL_ALL_WAYPOINT,$msg); my ($type,$data) = readPacket(); nicedie("Waypoint deletion failed.") if($type ne PID_ACK); } =head2 deleteWaypointData Deletes the waypoints given in the input GPX from the device. (Based on the waypoint names, not the coordinates). =cut sub deleteWaypointData { my @wpnames = @_; # existing waypoints nicedie("No deletable waypoints on the device") if(!scalar(@wpnames)); my @data = parseGPX('wpt'); nicedie("Could not read any waypoints to delete") if(! scalar(@data) ); my $max = scalar(@data); # waypoints to delete dowait("Attempting to remove $max waypoints from the device!"); my $del=0; for(my $i=0; $i<$max; $i++){ my ($id) = grep $wpnames[$_] eq $data[$i]->{name}, 0..$#wpnames; if($id){ $msg = "\x00\x00"; $msg .= pack('v',$id); sendRawPacket(PID_DEL_WAYPOINT,$msg); my ($type,$data) = readPacket(); if($type ne PID_ACK){ printf STDERR "removal of waypoint '%s' failed - maybe in use?\n", $data[$i]->{name}; }else{ $del++; } }else{ printf STDERR "waypoint '%s' not found on the device\n", $data[$i]->{name}; } } } =head2 uploadWaypointData Uploads waypoint read from the input GPX file =cut sub uploadWaypointData { my @data = parseGPX('wpt'); nicedie("Could not read any waypoints to upload") if(! scalar(@data) ); my $max = scalar(@data); # waypoints to upload my @wpnames = @_; # existing waypoints for(my $i=0; $i<$max; $i++){ if (grep $data[$i]->{name} eq $_, @wpnames){ printf STDERR "skipped existing waypoint '%s'\n",$data[$i]->{name}; next; } $msg = "\x00\x40\x00\x00"; $msg .= pack('a6',$data[$i]->{name}); $msg .= "\x00\x00"; $msg .= pack('l',int($data[$i]->{lat}*10000000)); $msg .= pack('l',int($data[$i]->{lon}*10000000)); $msg .= pack('v',int($data[$i]->{ele}/0.3048)); #feet to meters $msg .= pack('C6',$data[$i]->{time}); $msg .= pack('C',$data[$i]->{sym}); $msg .= "\x00\x7e"; sendRawPacket(PID_ADD_A_WAYPOINT,$msg); my ($type,$data) = readPacket(); if($type ne PID_DATA){ printf STDERR "upload of waypoint %s, %s (%s) failed\n", $data[$i]->{lat}, $data[$i]->{lon}, $data[$i]->{name}; } } } =head2 parseGPX I Parses a GPX file into a data structure. This is not a real XML parser! =cut sub parseGPX { my $type = shift; my @data; my $gpx = join('',); while($gpx =~ s/<$type\s([^>]+)>(.*?)<\/$type>//is){ my $point = $1.' '.$2; my %pt; if($point =~ m/lat="(-?\d+(\.\d+)?)"/is){ $pt{lat} = $1; } if($point =~ m/lon="(-?\d+(\.\d+)?)"/is){ $pt{lon} = $1; } next if(!$pt{lat} || !$pt{lon}); if($point =~ m/\s*(-?\d+(\.\d+)?)\s*<\/ele>/is){ $pt{ele} = $1; } if($point =~ m/