#!/usr/local/bin/perl5 -w
#========================================================================
#
#              call_home_on_sms_request.pl
#
#   This is an example script that waits for any incoming SMS messages.
#   Each SMS message is read and deleted.  If the SMS contains the text
#   "ET Phone Home", then a data call will be originated.  This script
#   does not setup TCP and therefore even though the data call is
#   originated, no PPP traffic will actually be transferred.  This
#   script will release the data call after 30 seconds.  Any SMS messages
#   that do not contain the magic words are logged but ignored.  All SMS
#   alerts recieved during a data call are ignored.
#
#   This script assumes that AT commands are sent to the control port
#   which is configured for COM1.  COM2 is used for the data port which
#   is opened just to assert DTR in order to establish the data call.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  (c) COPYRIGHT 2001 QUALCOMM, Incorporated. All Rights Reserved
#                     QUALCOMM Proprietary
#
#  Export of this technology or software is regulated by the U.S. Government.
#  Diversion contrary to U.S. law prohibited.
#
#  All ideas, data and information contained in or disclosed by
#  this document are confidential and proprietary information of
#  QUALCOMM Incorporated and all rights therein are expressly reserved.
#  By accepting this material the recipient agrees that this material
#  and the information contained therein are held in confidence and in
#  trust and will not be used, copied, reproduced in whole or in part,
#  nor its contents revealed in any manner to others without the express
#  written permission of QUALCOMM Incorporated.
#
#========================================================================

#===========================================================================
#               INCLUDE FILES
#===========================================================================

use strict;
use CommLib;
use AtCmdLib;

#===========================================================================
#               DEFINITIONS AND DECLARATIONS
#===========================================================================

# Set global to specify that Control port is on COM1

$CommLib::COM_DATA    = "COM2";
$CommLib::COM_CONTROL = "COM1";

my $number_to_dail = "#777";                 # Packet data call.
my $magic_words = "ET Phone Home";


#===========================================================================
#               FUNCTION DEFINITIONS
#===========================================================================

#===========================================================================
#                   MAIN SCRIPT
#===========================================================================

#---------------------------------------------------------------------------
# Initialize CommLib
#---------------------------------------------------------------------------

comm_lib_init();

#---------------------------------------------------------------------------
# Config CommLib to use the control port.
#---------------------------------------------------------------------------

comm_set_config($COMM_CONFIG_BOTH);

#---------------------------------------------------------------------------
# Enable the SMS alert
#---------------------------------------------------------------------------

my $response = at_enable_sms_alert();

# Make sure we got the OK

if (!($response =~ /OK/))
{
  print "Error: AT\$QCSMSA=1 did not return an OK response\n";
}

#---------------------------------------------------------------------------
# Sit and wait for SMS alerts 
#---------------------------------------------------------------------------

print "Waiting for SMS alerts\n";
sleep 1;

for(;;)
{
  #-----------------------------------------------------------------------
  # If there are any existing SMS messages, check them first.
  # Otherwise, wait for a new SMS message to arrive.
  #-----------------------------------------------------------------------

  my $sms_info = at_sms_info();
  my $message_count = $sms_info->{"TOTAL"};

  while ($message_count == 0)
  {
    $response = at_wait_for_alert();

    if ($response =~ /SMS/)    # Is this the SMS alert.
    {
      $sms_info = at_sms_info();
      $message_count = $sms_info->{"TOTAL"};
    }

    else
    {
      print "Unexpected response code: $response\n";
    }
  }

  #-----------------------------------------------------------------------
  # There are SMS messages available.  Start at the top of the list and
  # read each SMS message and log it to the display.  Then delete the
  # current message and advance to the next.  Check for the messages that
  # instructs us to phone home
  #-----------------------------------------------------------------------
    
  at_sms_move(2);    # Move to the top of the list.

  for (1..$message_count)
  {
    #----------------------------------------------------------------------
    # Log the current message, then delete it and advance to the next.
    #----------------------------------------------------------------------

    $response = at_sms_print(1);  # Get the current message.
  
    print "\n-------------------------------------------------------------\n";
    print_sms_message_hash($response);
    print "-------------------------------------------------------------\n\n";
  
    at_sms_move(0, 1);    # Delete current message and then advance.

    if ($response->{"MESSAGE"} =~ /$magic_words/)
    {
      #----------------------------------------------------------------------
      # Originate the data call.
      #----------------------------------------------------------------------

      print "Originating a data call\n";

      if ( at_dial($number_to_dail) =~ /CONNECT/ ) 
      {
        #--------------------------------------------------------------------
        # Just keep the call up for 30 seconds.  At this point the
        # application code would contact the home server via PPP.
        #--------------------------------------------------------------------

        sleep 30;
  
        #--------------------------------------------------------------------
        # Release the call.
        #--------------------------------------------------------------------

        print "Releasing the data call\n";
  
        $response = at_hook_control("0");
  
        print "Unable to release data call\n" if ($response !~ /OK/);
      }

      else
      {
        print "Unable to originate data call\n";
      }
    }
  }
}

#---------------------------------------------------------------------------
# Shutdown CommLib
#---------------------------------------------------------------------------

comm_lib_shutdown();


