#!/usr/bin/perl -w ############################################################################### # THIS SOFTWARE IS FREE SOFTWARE! IT IS LICENCED UNDER THE GNU PUBLIC LICENCE # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ############################################################################### # (c) Hartmut Schimmel, 07.10.2007 # TODO # extract hardware and firmware revisions from the info dump # add support for __write__ logger settings use IO::Handle; use Getopt::Std; use strict; STDOUT->autoflush(1); ############################################################################### # Declarations and definitions ############################################################################### use constant NULL => chr 0; use constant MEMSIZE => 3993600; my $dump; my %o; ############################################################################### # Usage ############################################################################### ( my $prg = $0 ) =~ s/^.*\/([^\/]+)$/$1/g; sub usage { die < USB device to use, if the automatic detection does not work -i only print out information about the logger settings -o write output to this file instead of the default file name, which is generated from the actual system time Example: $prg -s # scan $prg -d /dev/ttyUSB1 # use /dev/ttyUSB1 as device $prg -i # print information about the logger settings $prg -o my_gps_log.sr # write to file named "my_gps_log.sr" however, in most cases it should do what you want if you only type $prg -a This software is published under the GPL V2.0 EOF } usage if ( $#ARGV == -1 ); usage unless getopts('asd:io:', \%o); die "It does not make sense to combine the options -a and -d\n" if ( (defined $o{a}) and (defined $o{d})); die "It does not make sense to combine the options -s and -d\n" if ( (defined $o{s}) and (defined $o{d})); $o{a} = '' unless defined $o{d}; my @device; push @device, $o{d} if defined $o{d}; ############################################################################### # Autodetect device magic ;-) ############################################################################### if (( defined $o{s}) or ( defined $o{a})) { # Scan directories which typical contain USB devivece files foreach my $dir ( qw( /dev /dev/usb/tts )) { if ( opendir DH, $dir ) { print "Searching for USB device files in $dir\n"; while ( defined( $_ = readdir DH)) { # /dev/usb/tty device files all should match if ( $dir eq '/dev/usb/tts' ) { push @device, "$dir/$_"; } # other devices names should contain at least USB else { next unless /^(.*USB.*\d+).*$/i; push @device, "$dir/$1"; } } close DH; } else { print "Skipping $dir because it does not exist\n"; } } if ( $#device >= 0 ) { print "\nFound USB devices:\n"; foreach ( sort @device ) { print " $_\n"; } } else { die < /dev/null'); We need the stty tool to set mode 8N1 raw to the serial device. It should be available on most common Linux/Unix systems. However here it is missing. Please install it according to your distributions rules. For Debian and Ubuntu it is included in the package 'coreutils'. To install it you have to type as root: apt-get install coreutils Please tell the author of $prg, how stty is installed on other distributions than Debian and Ubuntu. EOF ############################################################################### # Try to access the devices we have found above ############################################################################### print "\nTrying to talk to the USB device(s)\n"; my $device_found = 0; foreach my $device ( sort @device ) { # Serial device settings if ( !system( "stty cs8 raw < $device") ) { print " Set $device to mode 8N1 raw using the stty system command\n"; } else { print " Error while setting $device to 8N1 raw mode: $!\n"; next; } # install an alarm handler to interrupt a pending access to the wrong device my $was_timeout = 0; $SIG{ALRM} = sub { $was_timeout = 1 }; my $name; eval { alarm(2); # timeout after 2 sec # open the serial device unless ( open( SERIAL, "+<$device") ) { print " Error while opening $device: $!\n"; sleep 5; # wait for timeout } print " Opened $device\n"; SERIAL->autoflush(1); # try to Initialize the logger print SERIAL "WP AP-Exit" . NULL; # close possibly pending session print SERIAL "W'P Camera Detect" . NULL; # init new session while (1) { sysread( SERIAL, $_, 1) or die $!; last if $_ eq NULL; $name .= $_; } }; alarm(0); # no timeout if ( $was_timeout == 1 ) { print " Timeout occured during access to $device - assuming that this\n" . " device is not the right one\n\n"; next; } else { print " Device $device answered with \'$name\'\n"; if ( $name eq 'WP GPS+BT' ) { print " *** fine, $device is it ***\n\n"; $device_found = 1; last; } else { print " *** unexpected, assuming that this device is not the right " . "one\n\n"; next; } } } print "USB device talk test finished\n"; die < $kbyte / 10 ) { $kbyte = int( $byte / 1024 ); printf " %04d kByte\r", $kbyte; } } printf " %d Bytes read\n", length( $dump); ############################################################################### # Close session ############################################################################### print SERIAL "WP AP-Exit" . NULL; close SERIAL; ############################################################################### # Check for correct end of data ############################################################################### # 57 50 20 55 70 64 61 74 65 20 4F 76 65 72 00 WP Update Over. die <$outfile" or die "Error while writing $outfile: $!\n"; print OUTFILE $dump; close OUTFILE; print "All done.\n"; exit 0;