#!/usr/bin/perl # vim:ts=3:ai package SMS; use Expect; use strict; # Define the object itself, and bless into space sub new { my $self = {}; # Define various name spaces $self->{CONNECTION} = undef; $self->{INIT} = undef; $self->{TIMEOUT} = 10; $self->{INITIATED} = undef; $self->{CURRENTHANDLE} = undef; $self->{SYNCCODE} = 0; $self->{RESTARTCODE} = "n"; # Bless the object bless($self); # return the object return $self; } sub closeAll { my $self = shift; if ($self->{INITIATED} eq "1") { $self->sendQuit(); } undef $self->{CONNECTION}; undef $self->{INITIATED}; } sub sendQuit { my $self = shift; $self->{CONNECTION}->send("quit\n"); return; } # Connects to "Host" sub ExpectConnect { my $self = shift; $Expect::Log_Stdout = 0; my $exp = Expect->spawn("/usr/bin/sudo /usr/bin/cu -l /dev/ttyS0 -s19200") or die "Cannot spawn serial: $!\n";; $exp->debug(0); $self->{CONNECTION} = $exp; if (defined($exp)) { return 1; } else { return 0; } } sub sendSMS { my $self = shift; my ($message, $number) = @_; if ((!$message) || (!$number)) { print "Not enough information supplied for sendSMS!\n"; return 0; } if (!grep(/^\+61/, $number)) { print "Number not international, please reinput your destination mobile number using +61!\n"; return 0; } if (!$self->{CONNECTION}) { $self->ExpectConnect(); } if (!$self->{INITIATED}) { $self->initiate(); } my $code; # print "Sending SMS...."; $self->{CONNECTION}->send("AT+CMGS=\"$number\"\r"); $self->{CONNECTION}->expect($self->{TIMEOUT}, [ '>', sub { $self->{CONNECTION}->send("$message \r"); exp_continue; } ], [ 'OK', sub { # print "Got OK!\n"; $code = 1; } ], '-re', 'OK', #' wait for shell prompt, then exit expect ); # print $self->{SYNCCODE}; return $code; } sub initiate { my $self = shift; if (!$self->{CONNECTION}) { $self->ExpectConnect(); } my $code; # print "Initiating.. ("; # First code $self->{CONNECTION}->send_slow(.1,"ATV1E1\r"); $self->{CONNECTION}->expect($self->{TIMEOUT}, [ 'OK', # detect ok? sub { $self->{INIT} = 1; my $fh = shift; $self->{CURRENTHANDLE} = $fh; # print "reset modem,"; exp_continue; } ], [ 'ERROR $', # detect ERROR sub { my $fh = shift; $self->{CURRENTHANDLE} = $fh; return 0; } ], [ eof => sub { } ], '-re', '^\r', #' wait for shell prompt, then exit expect ); $self->{CONNECTION}->send_slow(.1,"AT+CMGF=1\r"); $self->{CONNECTION}->expect($self->{TIMEOUT}, [ 'OK', # detect ok? sub { $self->{INIT} = 1; my $fh = shift; $self->{CURRENTHANDLE} = $fh; # print "turn sms_on,"; exp_continue; } ], [ 'ERROR $', # detect ERROR sub { my $fh = shift; $self->{CURRENTHANDLE} = $fh; return 0; } ], '-re', '^\r', #' wait for shell prompt, then exit expect ); $self->{CONNECTION}->send_slow(.1,"AT+CSCA=\"+6111111111\"\r"); $self->{CONNECTION}->expect($self->{TIMEOUT}, [ 'OK', # detect ok? sub { $self->{INIT} = 1; my $fh = shift; $self->{CURRENTHANDLE} = $fh; # print "supply server number"; exp_continue; } ], [ 'ERROR $', # detect ERROR sub { my $fh = shift; $self->{CURRENTHANDLE} = $fh; return 0; } ], '-re', '^\r', #' wait for shell prompt, then exit expect ); # print ")\n"; return 1; } 1;