Marcelo Rebonatto / Mbed 2 deprecated PMED_Tempo

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TftpServer.cpp Source File

TftpServer.cpp

00001 #include "TftpServer.h"
00002 
00003 #include <stdio.h>
00004 
00005 /*
00006  *
00007  * TFTP Formats
00008 
00009 
00010 Type   Op #     Format without header
00011        2 bytes    string   1 byte     string   1 byte
00012        -----------------------------------------------
00013 RRQ/  | 01/02 |  Filename  |   0  |    Mode    |   0  |
00014 WRQ    -----------------------------------------------
00015        2 bytes    2 bytes       n bytes
00016        ---------------------------------
00017 DATA  | 03    |   Block #  |    Data    |
00018        ---------------------------------
00019        2 bytes    2 bytes
00020        -------------------
00021 ACK   | 04    |   Block #  |
00022        --------------------
00023        2 bytes  2 bytes        string    1 byte
00024        ----------------------------------------
00025 ERROR | 05    |  ErrorCode |   ErrMsg   |   0  |
00026        ----------------------------------------
00027 
00028 
00029     Listen to UDP port 69
00030 
00031     On receive RRQ: Opens the file with semihosting
00032     Send the file
00033 
00034     On receive WRQ: Open the file with semihosting
00035     Send Ack
00036     Receive Data Packet and write into the file
00037     Send Ack
00038  *
00039  */
00040  
00041  //LocalFileSystem local("local");
00042  
00043 /*
00044 static const char* file_not_found_msg = "\x00\x05\x00\x01\File not found.\x00";          //20
00045 static const char* file_too_big_msg = "\x00\x05\x00\x03\File is too big (>512kB).\x00";    //30
00046 static const char* file_already_exists_msg = "\x00\x05\x00\x06\File already exists.\x00";  //25
00047 static const char* file_unknown_error_msg = "\x00\x05\x00\x00Unable to open the file for write.\x00";   //40
00048 */
00049 
00050 char* file_not_found_msg = "\x00\x05\x00\x01\File not found.\x00";          //20
00051 char* file_too_big_msg = "\x00\x05\x00\x03\File is too big (>512kB).\x00";    //30
00052 char* file_already_exists_msg = "\x00\x05\x00\x06\File already exists.\x00";  //25
00053 char* file_unknown_error_msg = "\x00\x05\x00\x00Unable to open the file for write.\x00";   //40
00054 
00055 
00056 
00057 void TftpServer::TftpServerThread(void const *arg)
00058 {
00059 /*
00060     printf("TFTP Thread starting...\n");
00061     
00062     UDPSocket server;
00063     
00064     server.bind(69,NULL);
00065     
00066     while(1)
00067     {
00068         char *buffer = NULL;
00069         int length = 0;
00070         Endpoint remote;
00071         if(server.receiveFrom(remote,&buffer,&length) == ERR_OK)
00072         {
00073             printf("Received %d bytes from %s:%d\n",length,remote.get_address(),remote.get_port());
00074             if(length > 2)
00075             {
00076                 unsigned short int opcode = buffer[0]*0x100 + buffer[1];
00077                 printf("Got opcode [%X]\n",opcode);
00078                 if(opcode == 1)
00079                 {
00080                     TftpDoRead(remote,buffer,length);
00081                 }
00082                 else if(opcode == 2)
00083                 {
00084                     TftpDoWrite(remote,buffer,length);
00085                 }
00086                 delete buffer;
00087             }
00088         }
00089     } 
00090 */
00091 }
00092 
00093 void TftpServer::TftpDoRead(Endpoint remote,char *buffer,int length)
00094 {
00095 /*
00096     char *filename = buffer+2;
00097     
00098     UDPSocket conn;
00099     
00100     if(conn.connect(remote.get_address(),remote.get_port()) != ERR_OK)
00101         return;
00102     
00103     char fullpath[256];
00104     strcpy(fullpath,"/local/");
00105     strcat(fullpath,filename);
00106     printf("File = %s\n",fullpath);    
00107     FILE *f = fopen(fullpath,"rb");
00108     
00109     char *ans=NULL;
00110     int len=0;
00111     
00112     if(f == NULL)
00113     {
00114         conn.send((char*)file_not_found_msg,20);
00115         conn.receive(&ans,&len);
00116         delete ans;
00117         return;
00118     }
00119     
00120     int idx = 1;
00121 
00122     int readbytes;
00123     unsigned char *hdrptr = new unsigned char[516];
00124     unsigned char *dataptr = hdrptr+4;
00125     
00126     do
00127     {
00128         //opcode
00129         hdrptr[0] = 0;
00130         hdrptr[1] = 3;
00131         //block #
00132         hdrptr[2] = idx/0x100;
00133         hdrptr[3] = idx%0x100;
00134         
00135         readbytes = fread(dataptr,1,512,f);
00136         conn.send((char*)hdrptr,readbytes+4);
00137         conn.receive(&ans,&len);
00138         delete ans;
00139         idx++;
00140     }
00141     while(readbytes == 512);
00142     
00143     fclose(f);
00144     delete hdrptr;
00145 */        
00146 }
00147 
00148 void TftpServer::TftpDoWrite(Endpoint remote,char *buffer,int length)
00149 {
00150 /*
00151     char *filename = buffer+2;
00152     
00153     UDPSocket conn;
00154     err_t e;
00155     if((e=conn.connect(remote.get_address(),remote.get_port())) != ERR_OK)
00156     {
00157         printf("Connect error %d\n",e);
00158         return;
00159     }
00160     
00161     char fullpath[256];
00162     strcpy(fullpath,"/local/");
00163     strcat(fullpath,filename);
00164     printf("File = %s\n",fullpath);    
00165     
00166     char *ans=NULL;
00167     int len=0;
00168     
00169     FILE *f = fopen(fullpath,"rb");
00170     if(f != NULL)
00171     {
00172         conn.send((char*)file_already_exists_msg,25);
00173         //conn.receive(&ans,&len);
00174         fclose(f);
00175         delete ans;
00176         return;
00177     }
00178     
00179     f = fopen(fullpath,"wb");
00180     if(f == NULL)
00181     {
00182         conn.send((char*)file_unknown_error_msg,40);
00183         //conn.receive(&ans,&len);
00184         delete ans;
00185         return;
00186     }
00187     
00188     //int buflen;
00189     unsigned char ack[4];
00190     ack[0] = 0;
00191     ack[1] = 4;
00192     ack[2] = 0;
00193     ack[3] = 0;
00194     
00195     conn.send((char*)ack,4);
00196     int error_tries = 5;
00197     char *buf = NULL;
00198     
00199     do
00200     {
00201         
00202         if(conn.receive(&buf,&len) != ERR_OK)
00203         {
00204             printf("Error\n");
00205             error_tries--;
00206             if(error_tries == 0)
00207             {
00208                 return;
00209             }
00210             conn.send((char*)ack,4);
00211             continue;
00212         }
00213         
00214         error_tries = 5;
00215         int idx = buf[2]*0x100 + buf[3];
00216         
00217         printf("Len = %d, Idx = %d\n",len,idx);
00218         
00219         fwrite(buf+4,1,len-4,f);
00220         
00221         delete buf;
00222         
00223         if(idx >= 1024)
00224         {
00225             conn.send((char*)file_too_big_msg,30);
00226             //conn.receive(&ans,&len);
00227             delete ans;
00228             return;
00229         }
00230         
00231         ack[2] = idx/0x100;
00232         ack[3] = idx%0x100;
00233         conn.send((char*)ack,4);
00234     }
00235     while(len == 516);
00236     
00237     fclose(f);
00238     */
00239 }