#!/usr/local/bin/perl5 -w
#========================================================================
#
#              position_location.pl
#
#   This is an example script that determines and reports the position
#   of the module every 15 minutes.  A data call is established in order
#   to make the position request but no PPP traffic is generated.  The
#   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.
#
#   Note that Perl uses the $ as a metacharacter to name scalar variables.
#   For this reason, the $ metacharacter needs to be escaped with the \
#   character to make it a literal character in the AT command strings.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  (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 $time_interval_for_position_update = 900; # Seconds between position updates

#===========================================================================
#               FUNCTION DEFINITIONS
#===========================================================================

#===========================================================================
# Function:     display_position
#
# Description:
#   Displays the position location information returned from a PLS request.
#
#    Hash key     Data contents
#    --------     -----------------------------------------------
#    W            Latitude as DDD MM SS (Degrees, Minutes, and Seconds)
#    N            Longitude as DDD MM SS (Degrees, Minutes, and Seconds)
#    TIME         The time of the measurement as (DD MM YYYY HH:MM:SS)
#    ERR          The estimated accuracy of the position.
#
# Parameters:
#   pls_data  - A hash containing the position information
#
# Returns:
#   None
#===========================================================================

sub display_position
{
  my ( $pls_data ) = @_;

  if ( defined($pls_data) )
  {
    print "LAT : $pls_data->{W}\n";
    print "LNG : $pls_data->{N}\n";
    print "TIME: $pls_data->{TIME}\n";
    print "Estimate accuracy is $pls_data->{ERR}\n\n";
  }
}

#===========================================================================
#                   MAIN SCRIPT
#===========================================================================

#---------------------------------------------------------------------------
# Initialize CommLib
#---------------------------------------------------------------------------

comm_lib_init();

#---------------------------------------------------------------------------
# Config CommLib to use the control port.
#---------------------------------------------------------------------------

comm_set_config($COMM_CONFIG_BOTH);

#------------------------------------------------------------------------
# Display the last known position (if available).
#------------------------------------------------------------------------

display_position( at_qcpls( 1 ) );    # 1 indicates last known position

#------------------------------------------------------------------------
# Update the position information at the specified interval.
#------------------------------------------------------------------------

for(;;)
{
  my $status;
  my $response;

  #------------------------------------------------------------------------
  # Check if we have service.  No use making a call otherwise.
  #------------------------------------------------------------------------

  $status = at_qcstatus();

  die "Unable to get status from the module" if ( !defined($status) );

  if ($status->{"SERVICE AVAILABLE"} =~ /YES/)
  {
    #------------------------------------------------------------------------
    # Originate the call.  Don't bother with PLS if the call fails.
    #------------------------------------------------------------------------

    if ( at_dial($number_to_dail) =~ /CONNECT/ ) 
    {
      #------------------------------------------------------------------------
      # Get and display the current position.
      #------------------------------------------------------------------------

      display_position( at_qcpls( 0 ) );    # 0 indicates get current position

      #------------------------------------------------------------------------
      # Release the call.
      #------------------------------------------------------------------------

      $response = at_hook_control("0");

      print "Unable to release data call\n" if ($response !~ /OK/);
    }
  }

  #------------------------------------------------------------------------
  # Sleep until the next interval.
  #------------------------------------------------------------------------

  sleep $time_interval_for_position_update;
}

#---------------------------------------------------------------------------
# Shutdown CommLib
#---------------------------------------------------------------------------

comm_lib_shutdown();

