#!/usr/bin/perl -w

use strict;

use IO::Socket::SSL qw(debug4);
use Device::SerialPort;

#{
#  my $PortObj = new Device::SerialPort ("/dev/usb/ttyUSB0", 1, "/tmp/serial.lock");
#  $PortObj->save("serial.config");
#}

unlink "/tmp/serial.lock";

#my $PortObj = tie (*SERIAL, 'Device::SerialPort',"serial.config") || die "Can't tie using serial.config: $!\n";
#$PortObj->baudrate(57600);
#$PortObj->parity("odd");
#$PortObj->databits(8);
#$PortObj->stopbits(1); 
#$PortObj->debug(1);
#$PortObj->save("serial.config");


open SERIAL,"+</dev/usb/ttyUSB0" ;

print SERIAL "START_SSL\n";

while(1)
{
  $_=<SERIAL>;
  print if(defined $_);
  last if(defined($_) and $_=~m/START_SSL/);
}
print "START_SSL detected.\n";

print "Fileno: ".fileno(SERIAL)."\n";

my $f=fileno(SERIAL);

#sleep(0);

my $ssl=IO::Socket::SSL->new_from_fd($f,SSL_use_cert=>1, SSL_version=>"SSLv2",SSL_cert_file=>"cacert.pem",SSL_key_file=>"key.pem",SSL_server=>1);

print "Error-String: ".IO::Socket::SSL::errstr()."\n";

if(defined($ssl))
{
  print "SSL Connection steht.\n";

  $ssl->write("Hallo");
}


