Sending Files

You can send files to the device if you know the specific full path of the destination file. Using this path, you can send the file using the File:Xfer category of commands. 

Commands include:

File:Xfer:Put <filename> [ YYYY/MM/DD HH:MM:SS ]

File:Xfer:Buffer <count> [ <checksum> ]

File:Xfer:SameBuffer <count> [ <checksum> ]

File:Xfer:DonePut


Be sure you know what you're doing before you send files to the device. This feature can be dangerous since you can overwrite system files that are necessary for proper operation.


The basic operations requires first issuing an "File:Xfer:Put <filename> [ YYY/MM/DD HH:MM:SS ]" command. The modification date and time are optional. This will initiate the file transfer resulting in a file created at "filename" location. Be sure to first do a "stc" to clear the status queue, and read the status queue after the command using "st?" to know if you can continue or if an error occurred. Once the "File:Xfer:Put" command is completed, you can send multiple "File:Xfer:Buffer <count> [<checksum>]" commands immediately followed by "count" number of binary bytes which will get appended to the file being created. Again, be sure to first do a "stc" to clear the status queue, and read the status queue after the command using "st?" to know if you can continue or if an error occurred. If the error "[Checksum_Error]" results, then the buffer transmission was unsuccessful and you can retry the transmission using the "File:Xfer:SameBuffer <count> [<checksum>]". The checksum provided is optional. If no checksum is provided, no checksum operations will be performed. The checksum calculation is simply the arithmetic sum of all the 8-bit unsigned bytes in the buffer being transmitted. Repeat this operation until the file is transferred completely, and then send a "File:Xfer:DonePut" command to indicate completion.

The following is an example of how to program sending files to the device. This example runs in the Windows programming environment.


Be careful using this feature.



#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <conio.h> // getch


#include "UtilSocket.h" 

#include "OsCompatibility.h"



int main( int argc, char *argv[] )

{

       bool helpFlag=false;

       bool verboseFlag=false;

       char *localSendFileName=NULL;

       char *remoteDestinationFileName=NULL;

       char *ipAddress=NULL;

       int portNumber=923;

       bool unknownFlag = false;


       while( *(++argv) )

       {

               if( !strcmp( *argv, "-h" ) ) helpFlag=true;

               else if( !strcmp( *argv, "-help" ) ) helpFlag=true;

               else if( !strcmp( *argv, "-v" ) ) verboseFlag=true;

               else if( !strcmp( *argv, "-port" ) ) portNumber = atoi(*(++argv));

               else if( ipAddress==NULL ) ipAddress = *argv;

               else if( localSendFileName==NULL ) localSendFileName = *argv;

               else if( remoteDestinationFileName==NULL ) remoteDestinationFileName = *argv;

               else

               {        

                       unknownFlag=true;

                       printf("Error: unknown argument [%s]\n", *argv );

               }

       }


       if( helpFlag==true || unknownFlag==true )

       {

               printf("Usage: BwPut [options] <ip> <send-file> [<destination-file-name>]\n");

               printf("Options:     -h          ... show this help message\n");

               printf("             -v          ... verbose messages displayed\n");

               printf("IP:          IP address of host machine\n");

               printf("Send:        Complete file path on local machine\n");

               printf("Destination: Destination file path on remote machine\n");

               printf("Note:        If no destination is specified, uses current working directory\n");

               exit(0);

       }


       if( ipAddress==NULL )

       {

               printf("Must provide IP address\n"); 

               exit(0);

       }


       if( localSendFileName==NULL )

       {

               printf("Must provide local file path to send\n");

               exit(0);

       }


       char destBuffer[1024];


       if( remoteDestinationFileName==NULL )

       {

               char fname[MAX_FNAME];

               char fext[MAX_EXT];


               _splitpath(localSendFileName,NULL,NULL,fname,fext);

               sprintf(destBuffer,"%s%s",fname,fext);


               remoteDestinationFileName = destBuffer;

       }


       if( verboseFlag )

       {

               printf("IP Address...............%s\n", ipAddress );

               printf("IP Port..................%u\n", portNumber );

               printf("LocalSendFile............%s\n", localSendFileName );

               printf("RemoteDestinationFile....%s\n", remoteDestinationFileName );

       }


       if( verboseFlag )

               printf("Creating socket object\n");


       UtilSocket *socket = new UtilSocket(AF_INET);

       HANDLE f=INVALID_HANDLE_VALUE;


       try

       {

               if( verboseFlag )

                       printf("Connecting to: %s, port %u\n", ipAddress, portNumber );


               socket->Connect(ipAddress,portNumber);


               if( verboseFlag )

                       printf("Connection successful\n");


               //==========================================


               // clear status and event queue

               socket->Send( "stc; evc\n");


               //==========================================


               if( verboseFlag )

                       printf("Opening source file: %s\n", localSendFileName );


               HANDLE f = CreateFile(

                                       localSendFileName, 

                    FILE_SHARE_READ, 

                                       0,

                    NULL, 

                                       OPEN_EXISTING, 

                    FILE_ATTRIBUTE_NORMAL, 

                                       NULL

                                       );


               if( f==INVALID_HANDLE_VALUE )

                       throw "[Unable_To_Open_Sending_File]";


               //==========================================

               static const int BUFSIZE=4096;

               char buffer[BUFSIZE];

               int blen=0;


               FILETIME lastWriteTime;

               GetFileTime(f,(LPFILETIME)NULL, (LPFILETIME)NULL, &lastWriteTime );

               SYSTEMTIME systemTime;

               FileTimeToSystemTime( &lastWriteTime, &systemTime );


               sprintf(buffer,"File:Xfer:Put \"%s\" %u/%u/%u %u:%u:%u\n"

                       remoteDestinationFileName,

                       systemTime.wYear,

                       systemTime.wMonth,

                       systemTime.wDay,

                       systemTime.wHour,

                       systemTime.wMinute,

                       systemTime.wSecond

                       );


               socket->Send( buffer );


               //==========================================


               static const char * None_String = "[none]";

               static const char * Checksum_Error_String = "[Checksum_Error]";


               socket->Send( "st?\n");

               blen=socket->Receive( buffer, BUFSIZE );

               if(blen>0) buffer[blen-1]=0; // remove newline


               if( verboseFlag )

                       printf("Response to Put command: %s\n", buffer );


               if( strcmp(buffer,None_String) )

                       throw "[Error_Status_To_Put_Command]" ;


               //==========================================


               char xfer[BUFSIZE];

               uint32_t count;


               while( ReadFile( f, xfer, BUFSIZE, &count, NULL ) && count>0 )

               {

                       uint32_t sum=0;

                       for( uint32_t i=0; i<count; i++ )

                               sum += (uint8_t) xfer[i];


                       sprintf(buffer,"stc; File:Xfer:Buffer %u 0x%x\n", count, sum );


                       socket->Send( buffer );

                       socket->Send( xfer, count );


                       socket->Send( "st?\n");

                       blen = socket->Receive( buffer, BUFSIZE );

                       if(blen>0) buffer[blen-1]=0; // remove newline


                       if( verboseFlag )

                               printf("Response to Buffer command: %s\n", buffer );


                       static const int RETRIES=3;


                       for( int n=0; !strcmp(buffer,Checksum_Error_String) && n<RETRIES; n++ )

                       {

                               sprintf(buffer,"stc; File:Xfer:SameBuffer %u 0x%x\n", count, sum );


                               socket->Send( buffer );

                               socket->Send( xfer, count );


                               socket->Send( "st?\n");

                               blen = socket->Receive( buffer, BUFSIZE );

                               if(blen>0) buffer[blen-1]=0; // remove newline


                               if( verboseFlag )

                                       printf("Response to SameBuffer command: %s\n", buffer );

                       }


                       if( strcmp(buffer,None_String) )

                               throw "[Error_Status_From_Buffer_Command]" ;

               }


               socket->Send( "File:Xfer:DonePut\n" );


               CloseHandle(f);

               delete socket;

       }

       catch(const char *msg)

       {

               printf("Error: %s\n", msg );

               delete socket;


               if( f!=INVALID_HANDLE_VALUE ) 

                       CloseHandle(f);

       }

       catch(...)

       {

               printf("Unknown error encountered\n");

               delete socket;


               if( f!=INVALID_HANDLE_VALUE )

                       CloseHandle(f);

       }


       return 0;

}


/* EOF */