#!/usr/bin/perl

# For Linux
use Device::SerialPort;

$devname = $ARGV[0];
if ($devname eq '') {
	print STDERR "Usage: perl identify_port.pl /dev/ttyUSB0\n";
	exit 1;
}

$done = 0;
$porttype = "unknown"; 
$baudrate = "unknown";

foreach $baud(19200,38400,57600,115200) {
	for($i=0;$i<1;$i++) {
		print STDERR "Baud ".$baud." try ".$i."\n";
		$port = Device::SerialPort->new($devname);
		if ($port eq undef) {
			print STDERR "Port open failed: ".$!."\n";
			next;
		}
		$port->baudrate($baud); # Configure this to match your device
		$port->databits(8);
		$port->parity("none");
		$port->stopbits(1);
	
		$port->read_char_time(0); 
		$port->read_const_time(0); 

		($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
		print STDERR "Flush ".$flushcount."\n";
		($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
		($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
		($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars

		$port->read_char_time(0); # don't wait for each character
		$port->read_const_time(1000); # 1 second per unfulfilled "read" call

		($count,$buffer)=$port->read(2048); # will read _up to_ 2048 chars
		
		if ($buffer =~ /\$GPGSV|\$GPVTG|\$GPRMC|\$GPGGA/s) {
			print STDERR $count." ".$buffer."\n";
			$porttype = "gps";
			$baudrate = $baud;
			$done = 1;
			$port->close();
			last;
		}
		$port->close();
	}
	if ($done == 1) { last; }
}

if ($done == 0) {
	for($i=0;$i<3;$i++) {
		for($j=0;$j<20;$j++) {
			$port = Device::SerialPort->new($devname);
			if ($port eq undef) {
				print STDERR "Port open failed: ".$!."\n";
				next;
			}
			$port->baudrate(19200); # Configure this to match your device
			$port->databits(8);
			$port->parity("none");
			$port->stopbits(1);
		
			$port->read_char_time(0); 
			$port->read_const_time(1000); 
			
			$port->write("ATZ\r");

			sleep(0.5);
			($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
			print STDERR "Flush ".$flushcount."\n";
			if ($flushcount >= 1024) {
				$port->close();
				next;
			}
			$port->read_const_time(0); 
			($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
			($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
			($flushcount,$flushsaw)=$port->read(262144); # will read _up to_ 262144 chars
			last;
		}
	
		$port->write("AT\r");
	
		$port->read_char_time(0); # don't wait for each character
		$port->read_const_time(2000); # 1 second per unfulfilled "read" call
	
		($count,$buffer)=$port->read(2048); # will read _up to_ 2048 chars
		$port->close();
		#if ($buffer =~ /OK[\r\n][\r\n]/s) {
		if ($buffer =~ /[^a-zA-Z]OK[\r\n]/s) {
			print STDERR $buffer."\n";
			$porttype = "rockblock";
			$baudrate = 19200;
			$done = 1;
			last;
		}
	}
}

print $porttype.' '.$baudrate."\n";
if ($done == 1) { exit 0; } else { exit 1; }

# EOF
