#!/usr/local/bin/perl5 -w
#========================================================================
#
#              sms_browse.pl
#
#   This is an example script for reading all the SMS messages stored on
#   the Globalstar GSP-1620 data modem (module).  The script includes a -d
#   option which deletes each message after it has been read and displayed.
#   The script assumes that AT commands are sent to the control port which
#   is configured for COM1.  The data port is not used.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  (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;
use Getopt::Long;

#===========================================================================
#               DEFINITIONS AND DECLARATIONS
#===========================================================================

# Set global to specify that Control port is on COM1

$CommLib::COM_DATA    = "COM2";
$CommLib::COM_CONTROL = "COM1";

#============================================================================
#                COMMAND LINE OPTIONS
#============================================================================

# Caller can specify --delete on command line to delete all the messages

my $delete_all = 0;           # By default, don't delete any messages

&GetOptions('delete!', \$delete_all);

#===========================================================================
#                   MAIN SCRIPT
#===========================================================================

#---------------------------------------------------------------------------
# Initialize CommLib
#---------------------------------------------------------------------------

comm_lib_init();

#---------------------------------------------------------------------------
# Config CommLib to use the control port.
#---------------------------------------------------------------------------

comm_set_config($COMM_CONFIG_CONTROL);

#---------------------------------------------------------------------------
# Get the SMS status
#---------------------------------------------------------------------------

my $sms_info = at_sms_info();
my $message_count = $sms_info->{"TOTAL"};

print "There are $message_count total messages available to read\n\n";

#---------------------------------------------------------------------------
# Start at the top of the list
#---------------------------------------------------------------------------

at_sms_move(2);

#---------------------------------------------------------------------------
# Print each message and then advance to the next
#---------------------------------------------------------------------------

my $i;
my $response;

for($i = 0; $i < $message_count; ++$i)
{
  $response = at_sms_print(1);  # "Print" the current message.

  print "\n--------------------------------------------------------------\n";
  print_sms_message_hash($response);
  print "--------------------------------------------------------------\n\n";

  at_sms_move(0, $delete_all);
}

#---------------------------------------------------------------------------
# Shutdown CommLib
#---------------------------------------------------------------------------

comm_lib_shutdown();

