#========================================================================= 
#          Copyright © 2001-2004 QUALCOMM Incorporated.
#                     All Rights Reserved.
#========================================================================= 
#! /usr/local/bin/perl -w
use strict;

use lib "$ENV{BREWTOOLSDIR}\\BREW Perl Interface";
use BREWPerlIF;

###########################################################################
# Sample perl application to demonstrate file-related operatoins:
#
#        1) It copies some files from PC to device 
#           (in "<BREW MOD directory>/helloworld" directory), 
#        2) copy above files again in overwrite mode, 
#        3) set two files (.dat and .txt) to UNRESTRICTED mode,
#        4) copy above two files back to PC,
#        5) delete above files from device.
#
#
# Note: Following internal conversion will be done to the file path specified 
#       in the client PERL sctipt:
#
# -----------------------------------------------------------------------
# User specified path		==>	Converted full device path
# -----------------------------------------------------------------------
# /xxx				==> <BREW_MOD_DIR>/xxx
# /mod/xxx			==> <BREW_MOD_DIR>/xxx
# /shared/xxx			==> <BREW_SHARED_DIR>/xxx
# /mif/yyy.mif			==> <BREW_MIF_DIR>/yyy.mif
# /yyy.mif			==> <BREW_MIF_DIR>/yyy.mif
# /mod/yyy.mif			==> NOT ALLOWED
# /shared/yyy.mif		==> NOT ALLOWED
#
# where xxx could be any file path, e.g. helloworld/temp.dat, 
# helloworld/helloworld.mod, etc.
#
###########################################################################

###########################################################################
# Variable Declaration Section
###########################################################################

# serial port name to which the device is connected
my $port = "COM1";

my $dllName = "$ENV{BTILDIR}\\OEMLayer\\QCOMOEM.dll";

# Array containing file names to be copied to the device
my @fileToDevice = ("C:\\tmp\\helloworld.mod", 
                    "C:\\tmp\\helloworld.sig",                 
                    "C:\\tmp\\helloworld.bar",
                    "C:\\tmp\\helloworld.dat",
                    "C:\\tmp\\help.txt");

# Array containing file names to be copied to PC
my @fileToPC     = ("C:\\tmp\\helloworld.dat",
                    "C:\\tmp\\help.txt");

# destination directory on the device
my $deviceDes = "/helloworld";

# destination directory on PC
my $pcDes   = "C:\\tmp\\FromDevice";

# place holder for storing basename of device file
my $baseName;

# place holder for storing file name while iterating thru filename array
my $file;

my $UNRESTRICTED_FILE_ATTRIBUTE = 0x00FF;

##########################################################################
# Here starts the actual processing
##########################################################################

# connect to the device
printf "Connecting to device\n" ;
if( BREWPerlIF::ConnecttoDevice($port,$dllName )) {
	print "Connection to the device successful\n";
} else {
	die "ERROR - connect to the device on specified port $port\n";
}

# Prepare the device environment
# delete the directory and files if exist
if (BREWPerlIF::DeviceDirExists($deviceDes)) {
   printf "\n Deleting device dir: $deviceDes \n";

   if (BREWPerlIF::DeleteDeviceDir($deviceDes)) {
      printf "\n Device Dir deleted:  $deviceDes \n";
   } else {
      printf "\n ERROR - Device dir NOT deleted: $deviceDes \n";
   }
}

# copy files to device without overwrite
foreach $file (@fileToDevice) {
    $baseName = (reverse(split(/\\/,$file)))[0];

    if (BREWPerlIF::CopyFileToDevice($file, "$deviceDes/$baseName", 0)) {
	printf "Successfully copied $file to the device\n\n";
    } 
    else {
	printf "ERROR - in copying $file to the device\n\n";
    }
}

# Copy files again with overwrite
printf "Start copy with overwrite ... \n";
foreach $file (@fileToDevice) {
   $baseName = (reverse(split(/\\/,$file)))[0];

   if (BREWPerlIF::CopyFileToDevice($file, "$deviceDes/$baseName", 1)) {
       printf "Successfully copied $file to the device\n\n";
   } 
   else {
       printf "ERROR -  in copying $file to the device\n\n";
   }
}


# Set filest to UNRESTRICTED mode
foreach $file (@fileToPC) {
   $baseName = (reverse(split(/\\/,$file)))[0];

   if (BREWPerlIF::SetDeviceFileAttribute("$deviceDes/$baseName", $UNRESTRICTED_FILE_ATTRIBUTE)) {
       printf "Successfully set mode for file: $deviceDes/$baseName\n\n";
   } 
   else {
       printf "ERROR - Failed to set mode for file: $deviceDes/$baseName\n\n";
   }
}


# Copy files back to PC
foreach $file (@fileToPC) {
   $baseName = (reverse(split(/\\/,$file)))[0];

   if (BREWPerlIF::CopyFileFromDevice("$deviceDes/$baseName", "$pcDes")) {
       printf "Successfully copied $deviceDes/$baseName to PC\n\n";
   } 
   else {
       printf "ERROR -  in copying $deviceDes/$baseName to PC\n\n";
   }
}

# Clean up the device environment
printf "Start delete files ... \n";
foreach $file (@fileToDevice) {
   $baseName = (reverse(split(/\\/,$file)))[0];

   if (BREWPerlIF::DeviceFileExists("$deviceDes/$baseName")) {

      if (BREWPerlIF::DeleteFileFromDevice("$deviceDes/$baseName")) {
          printf "Successfully deleted $file from the device\n\n";
      } 
      else {
          printf "ERROR - Failed to delete $deviceDes/$baseName from device\n\n";
      }
   } else{
      printf "\nERROR - file $deviceDes/$baseName doesn't exist \n";
   }
}

# delete the directory and files
if (BREWPerlIF::DeviceDirExists($deviceDes)) {
   printf "\nDeleting device dir: $deviceDes \n";

   if (BREWPerlIF::DeleteDirFromDevice($deviceDes)) {
      printf "\nDevice Dir deleted:  $deviceDes \n";
   } else {
      printf "\nERROR - Device dir NOT deleted: $deviceDes \n";
   }
}

printf "disconnecting from device\n" ;
if( BREWPerlIF::ReleaseDevice() ) {
	print "disconnect from the device successful\n";
} else {
	die "Could not disconnect from the device\n";
}