Marcelo Rebonatto / Mbed 2 deprecated PMED_Tempo

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers telnet_update.cpp Source File

telnet_update.cpp

00001 #include "TelnetServer.h"
00002 
00003 
00004 char *header_msg = "firmware update\r\n\r\n";
00005 char *usage_msg = "Wrong arguments\r\nUsage: update <firmware filename>\r\n\r\n";
00006 char *invalid_filename_msg = "Invalid filename. It must have at most 8 characters in the name and 3 in the extension\r\n\r\n";
00007 char *file_does_not_exist_msg = "A file with the supplied filename does not exist.\r\n\r\n";
00008 char *version_does_not_exist_msg = "Could not open version.txt. The update operation cannot continue.\r\n\r\n";
00009 char *unable_to_create_remove_msg = "Unable to create remove.txt. The update operation cannot continue.\r\n\r\n";
00010 char *unable_to_truncate_version_msg = "Unable to truncate version.txt. The update operation cannot continue.\r\n\r\n";
00011 char *operation_successful_msg = "Operation was completed successfully. Now resetting...\r\n\r\n";
00012 
00013 extern LocalFileSystem local;
00014 extern "C" void mbed_reset();
00015 
00016 int TelnetServer::UpdateCommand(TCPSocketConnection *conn,char** argv,int argc)
00017 {
00018     char old_filename[20];
00019     char new_filename[20];
00020 
00021     conn->send(header_msg,strlen(header_msg));
00022     //0 - check if the file informed exists
00023     if(argc != 2)
00024     {
00025         conn->send(usage_msg,strlen(usage_msg));
00026         return 0;
00027     }
00028 
00029     strcpy(new_filename,"/local/");
00030     strcat(new_filename,argv[1]);
00031     new_filename[19] = '\0';
00032     printf("New filename = %s\n",new_filename);
00033 
00034     if(strlen(new_filename) > 19)
00035     {
00036         conn->send(invalid_filename_msg,strlen(invalid_filename_msg));
00037         return 0;
00038     }
00039 
00040     FILE* fn = fopen(new_filename,"r");
00041     if(fn == NULL)
00042     {
00043         conn->send(file_does_not_exist_msg,strlen(file_does_not_exist_msg));
00044         return 0;
00045     }
00046     fclose(fn);
00047 
00048     //1 - write remove.txt with the current FW filename
00049     fn = fopen("/local/version.txt","r");
00050     if(fn == NULL)
00051     {
00052         conn->send(version_does_not_exist_msg,strlen(version_does_not_exist_msg));
00053         return 0;
00054     }
00055     int rd = fread(old_filename,1,19,fn);
00056     old_filename[rd]='\0';
00057     fclose(fn); 
00058 
00059     fn = fopen("/local/remove.txt","w");
00060     if(fn == NULL)
00061     {
00062         conn->send(unable_to_create_remove_msg,strlen(unable_to_create_remove_msg));
00063         return 0;
00064     }
00065     fwrite(old_filename,1,strlen(old_filename),fn);
00066     fclose(fn);
00067 
00068     //2 - write version.txt with the new FW filename
00069     fn = fopen("/local/version.txt","w");
00070     if(fn == NULL)
00071     {
00072         conn->send(unable_to_truncate_version_msg,strlen(unable_to_truncate_version_msg));
00073         return 0;
00074     }
00075     fwrite(new_filename,1,strlen(new_filename),fn);
00076     fclose(fn);
00077 
00078     //3 - remove the old fw file
00079     local.remove(old_filename);
00080 
00081     //4 - remove remove.txt
00082     local.remove("remove.txt");
00083 
00084     conn->send(operation_successful_msg,strlen(operation_successful_msg));
00085 
00086     //5 - reset    
00087     delete conn;
00088 
00089     mbed_reset();
00090 
00091     return 0;
00092 }