Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
rebonatto
Date:
Mon Jul 21 00:58:34 2014 +0000
Revision:
2:86c3cb25577b
Parent:
0:c64e1194230b
Problemas com objeto para aquisi??o da rfid (serial)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:c64e1194230b 1 #include "TftpServer.h"
rebonatto 0:c64e1194230b 2
rebonatto 0:c64e1194230b 3 #include <stdio.h>
rebonatto 0:c64e1194230b 4
rebonatto 0:c64e1194230b 5 /*
rebonatto 0:c64e1194230b 6 *
rebonatto 0:c64e1194230b 7 * TFTP Formats
rebonatto 0:c64e1194230b 8
rebonatto 0:c64e1194230b 9
rebonatto 0:c64e1194230b 10 Type Op # Format without header
rebonatto 0:c64e1194230b 11 2 bytes string 1 byte string 1 byte
rebonatto 0:c64e1194230b 12 -----------------------------------------------
rebonatto 0:c64e1194230b 13 RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
rebonatto 0:c64e1194230b 14 WRQ -----------------------------------------------
rebonatto 0:c64e1194230b 15 2 bytes 2 bytes n bytes
rebonatto 0:c64e1194230b 16 ---------------------------------
rebonatto 0:c64e1194230b 17 DATA | 03 | Block # | Data |
rebonatto 0:c64e1194230b 18 ---------------------------------
rebonatto 0:c64e1194230b 19 2 bytes 2 bytes
rebonatto 0:c64e1194230b 20 -------------------
rebonatto 0:c64e1194230b 21 ACK | 04 | Block # |
rebonatto 0:c64e1194230b 22 --------------------
rebonatto 0:c64e1194230b 23 2 bytes 2 bytes string 1 byte
rebonatto 0:c64e1194230b 24 ----------------------------------------
rebonatto 0:c64e1194230b 25 ERROR | 05 | ErrorCode | ErrMsg | 0 |
rebonatto 0:c64e1194230b 26 ----------------------------------------
rebonatto 0:c64e1194230b 27
rebonatto 0:c64e1194230b 28
rebonatto 0:c64e1194230b 29 Listen to UDP port 69
rebonatto 0:c64e1194230b 30
rebonatto 0:c64e1194230b 31 On receive RRQ: Opens the file with semihosting
rebonatto 0:c64e1194230b 32 Send the file
rebonatto 0:c64e1194230b 33
rebonatto 0:c64e1194230b 34 On receive WRQ: Open the file with semihosting
rebonatto 0:c64e1194230b 35 Send Ack
rebonatto 0:c64e1194230b 36 Receive Data Packet and write into the file
rebonatto 0:c64e1194230b 37 Send Ack
rebonatto 0:c64e1194230b 38 *
rebonatto 0:c64e1194230b 39 */
rebonatto 0:c64e1194230b 40
rebonatto 0:c64e1194230b 41 //LocalFileSystem local("local");
rebonatto 0:c64e1194230b 42
rebonatto 0:c64e1194230b 43 /*
rebonatto 0:c64e1194230b 44 static const char* file_not_found_msg = "\x00\x05\x00\x01\File not found.\x00"; //20
rebonatto 0:c64e1194230b 45 static const char* file_too_big_msg = "\x00\x05\x00\x03\File is too big (>512kB).\x00"; //30
rebonatto 0:c64e1194230b 46 static const char* file_already_exists_msg = "\x00\x05\x00\x06\File already exists.\x00"; //25
rebonatto 0:c64e1194230b 47 static const char* file_unknown_error_msg = "\x00\x05\x00\x00Unable to open the file for write.\x00"; //40
rebonatto 0:c64e1194230b 48 */
rebonatto 0:c64e1194230b 49
rebonatto 0:c64e1194230b 50 char* file_not_found_msg = "\x00\x05\x00\x01\File not found.\x00"; //20
rebonatto 0:c64e1194230b 51 char* file_too_big_msg = "\x00\x05\x00\x03\File is too big (>512kB).\x00"; //30
rebonatto 0:c64e1194230b 52 char* file_already_exists_msg = "\x00\x05\x00\x06\File already exists.\x00"; //25
rebonatto 0:c64e1194230b 53 char* file_unknown_error_msg = "\x00\x05\x00\x00Unable to open the file for write.\x00"; //40
rebonatto 0:c64e1194230b 54
rebonatto 0:c64e1194230b 55
rebonatto 0:c64e1194230b 56
rebonatto 0:c64e1194230b 57 void TftpServer::TftpServerThread(void const *arg)
rebonatto 0:c64e1194230b 58 {
rebonatto 0:c64e1194230b 59 /*
rebonatto 0:c64e1194230b 60 printf("TFTP Thread starting...\n");
rebonatto 0:c64e1194230b 61
rebonatto 0:c64e1194230b 62 UDPSocket server;
rebonatto 0:c64e1194230b 63
rebonatto 0:c64e1194230b 64 server.bind(69,NULL);
rebonatto 0:c64e1194230b 65
rebonatto 0:c64e1194230b 66 while(1)
rebonatto 0:c64e1194230b 67 {
rebonatto 0:c64e1194230b 68 char *buffer = NULL;
rebonatto 0:c64e1194230b 69 int length = 0;
rebonatto 0:c64e1194230b 70 Endpoint remote;
rebonatto 0:c64e1194230b 71 if(server.receiveFrom(remote,&buffer,&length) == ERR_OK)
rebonatto 0:c64e1194230b 72 {
rebonatto 0:c64e1194230b 73 printf("Received %d bytes from %s:%d\n",length,remote.get_address(),remote.get_port());
rebonatto 0:c64e1194230b 74 if(length > 2)
rebonatto 0:c64e1194230b 75 {
rebonatto 0:c64e1194230b 76 unsigned short int opcode = buffer[0]*0x100 + buffer[1];
rebonatto 0:c64e1194230b 77 printf("Got opcode [%X]\n",opcode);
rebonatto 0:c64e1194230b 78 if(opcode == 1)
rebonatto 0:c64e1194230b 79 {
rebonatto 0:c64e1194230b 80 TftpDoRead(remote,buffer,length);
rebonatto 0:c64e1194230b 81 }
rebonatto 0:c64e1194230b 82 else if(opcode == 2)
rebonatto 0:c64e1194230b 83 {
rebonatto 0:c64e1194230b 84 TftpDoWrite(remote,buffer,length);
rebonatto 0:c64e1194230b 85 }
rebonatto 0:c64e1194230b 86 delete buffer;
rebonatto 0:c64e1194230b 87 }
rebonatto 0:c64e1194230b 88 }
rebonatto 0:c64e1194230b 89 }
rebonatto 0:c64e1194230b 90 */
rebonatto 0:c64e1194230b 91 }
rebonatto 0:c64e1194230b 92
rebonatto 0:c64e1194230b 93 void TftpServer::TftpDoRead(Endpoint remote,char *buffer,int length)
rebonatto 0:c64e1194230b 94 {
rebonatto 0:c64e1194230b 95 /*
rebonatto 0:c64e1194230b 96 char *filename = buffer+2;
rebonatto 0:c64e1194230b 97
rebonatto 0:c64e1194230b 98 UDPSocket conn;
rebonatto 0:c64e1194230b 99
rebonatto 0:c64e1194230b 100 if(conn.connect(remote.get_address(),remote.get_port()) != ERR_OK)
rebonatto 0:c64e1194230b 101 return;
rebonatto 0:c64e1194230b 102
rebonatto 0:c64e1194230b 103 char fullpath[256];
rebonatto 0:c64e1194230b 104 strcpy(fullpath,"/local/");
rebonatto 0:c64e1194230b 105 strcat(fullpath,filename);
rebonatto 0:c64e1194230b 106 printf("File = %s\n",fullpath);
rebonatto 0:c64e1194230b 107 FILE *f = fopen(fullpath,"rb");
rebonatto 0:c64e1194230b 108
rebonatto 0:c64e1194230b 109 char *ans=NULL;
rebonatto 0:c64e1194230b 110 int len=0;
rebonatto 0:c64e1194230b 111
rebonatto 0:c64e1194230b 112 if(f == NULL)
rebonatto 0:c64e1194230b 113 {
rebonatto 0:c64e1194230b 114 conn.send((char*)file_not_found_msg,20);
rebonatto 0:c64e1194230b 115 conn.receive(&ans,&len);
rebonatto 0:c64e1194230b 116 delete ans;
rebonatto 0:c64e1194230b 117 return;
rebonatto 0:c64e1194230b 118 }
rebonatto 0:c64e1194230b 119
rebonatto 0:c64e1194230b 120 int idx = 1;
rebonatto 0:c64e1194230b 121
rebonatto 0:c64e1194230b 122 int readbytes;
rebonatto 0:c64e1194230b 123 unsigned char *hdrptr = new unsigned char[516];
rebonatto 0:c64e1194230b 124 unsigned char *dataptr = hdrptr+4;
rebonatto 0:c64e1194230b 125
rebonatto 0:c64e1194230b 126 do
rebonatto 0:c64e1194230b 127 {
rebonatto 0:c64e1194230b 128 //opcode
rebonatto 0:c64e1194230b 129 hdrptr[0] = 0;
rebonatto 0:c64e1194230b 130 hdrptr[1] = 3;
rebonatto 0:c64e1194230b 131 //block #
rebonatto 0:c64e1194230b 132 hdrptr[2] = idx/0x100;
rebonatto 0:c64e1194230b 133 hdrptr[3] = idx%0x100;
rebonatto 0:c64e1194230b 134
rebonatto 0:c64e1194230b 135 readbytes = fread(dataptr,1,512,f);
rebonatto 0:c64e1194230b 136 conn.send((char*)hdrptr,readbytes+4);
rebonatto 0:c64e1194230b 137 conn.receive(&ans,&len);
rebonatto 0:c64e1194230b 138 delete ans;
rebonatto 0:c64e1194230b 139 idx++;
rebonatto 0:c64e1194230b 140 }
rebonatto 0:c64e1194230b 141 while(readbytes == 512);
rebonatto 0:c64e1194230b 142
rebonatto 0:c64e1194230b 143 fclose(f);
rebonatto 0:c64e1194230b 144 delete hdrptr;
rebonatto 0:c64e1194230b 145 */
rebonatto 0:c64e1194230b 146 }
rebonatto 0:c64e1194230b 147
rebonatto 0:c64e1194230b 148 void TftpServer::TftpDoWrite(Endpoint remote,char *buffer,int length)
rebonatto 0:c64e1194230b 149 {
rebonatto 0:c64e1194230b 150 /*
rebonatto 0:c64e1194230b 151 char *filename = buffer+2;
rebonatto 0:c64e1194230b 152
rebonatto 0:c64e1194230b 153 UDPSocket conn;
rebonatto 0:c64e1194230b 154 err_t e;
rebonatto 0:c64e1194230b 155 if((e=conn.connect(remote.get_address(),remote.get_port())) != ERR_OK)
rebonatto 0:c64e1194230b 156 {
rebonatto 0:c64e1194230b 157 printf("Connect error %d\n",e);
rebonatto 0:c64e1194230b 158 return;
rebonatto 0:c64e1194230b 159 }
rebonatto 0:c64e1194230b 160
rebonatto 0:c64e1194230b 161 char fullpath[256];
rebonatto 0:c64e1194230b 162 strcpy(fullpath,"/local/");
rebonatto 0:c64e1194230b 163 strcat(fullpath,filename);
rebonatto 0:c64e1194230b 164 printf("File = %s\n",fullpath);
rebonatto 0:c64e1194230b 165
rebonatto 0:c64e1194230b 166 char *ans=NULL;
rebonatto 0:c64e1194230b 167 int len=0;
rebonatto 0:c64e1194230b 168
rebonatto 0:c64e1194230b 169 FILE *f = fopen(fullpath,"rb");
rebonatto 0:c64e1194230b 170 if(f != NULL)
rebonatto 0:c64e1194230b 171 {
rebonatto 0:c64e1194230b 172 conn.send((char*)file_already_exists_msg,25);
rebonatto 0:c64e1194230b 173 //conn.receive(&ans,&len);
rebonatto 0:c64e1194230b 174 fclose(f);
rebonatto 0:c64e1194230b 175 delete ans;
rebonatto 0:c64e1194230b 176 return;
rebonatto 0:c64e1194230b 177 }
rebonatto 0:c64e1194230b 178
rebonatto 0:c64e1194230b 179 f = fopen(fullpath,"wb");
rebonatto 0:c64e1194230b 180 if(f == NULL)
rebonatto 0:c64e1194230b 181 {
rebonatto 0:c64e1194230b 182 conn.send((char*)file_unknown_error_msg,40);
rebonatto 0:c64e1194230b 183 //conn.receive(&ans,&len);
rebonatto 0:c64e1194230b 184 delete ans;
rebonatto 0:c64e1194230b 185 return;
rebonatto 0:c64e1194230b 186 }
rebonatto 0:c64e1194230b 187
rebonatto 0:c64e1194230b 188 //int buflen;
rebonatto 0:c64e1194230b 189 unsigned char ack[4];
rebonatto 0:c64e1194230b 190 ack[0] = 0;
rebonatto 0:c64e1194230b 191 ack[1] = 4;
rebonatto 0:c64e1194230b 192 ack[2] = 0;
rebonatto 0:c64e1194230b 193 ack[3] = 0;
rebonatto 0:c64e1194230b 194
rebonatto 0:c64e1194230b 195 conn.send((char*)ack,4);
rebonatto 0:c64e1194230b 196 int error_tries = 5;
rebonatto 0:c64e1194230b 197 char *buf = NULL;
rebonatto 0:c64e1194230b 198
rebonatto 0:c64e1194230b 199 do
rebonatto 0:c64e1194230b 200 {
rebonatto 0:c64e1194230b 201
rebonatto 0:c64e1194230b 202 if(conn.receive(&buf,&len) != ERR_OK)
rebonatto 0:c64e1194230b 203 {
rebonatto 0:c64e1194230b 204 printf("Error\n");
rebonatto 0:c64e1194230b 205 error_tries--;
rebonatto 0:c64e1194230b 206 if(error_tries == 0)
rebonatto 0:c64e1194230b 207 {
rebonatto 0:c64e1194230b 208 return;
rebonatto 0:c64e1194230b 209 }
rebonatto 0:c64e1194230b 210 conn.send((char*)ack,4);
rebonatto 0:c64e1194230b 211 continue;
rebonatto 0:c64e1194230b 212 }
rebonatto 0:c64e1194230b 213
rebonatto 0:c64e1194230b 214 error_tries = 5;
rebonatto 0:c64e1194230b 215 int idx = buf[2]*0x100 + buf[3];
rebonatto 0:c64e1194230b 216
rebonatto 0:c64e1194230b 217 printf("Len = %d, Idx = %d\n",len,idx);
rebonatto 0:c64e1194230b 218
rebonatto 0:c64e1194230b 219 fwrite(buf+4,1,len-4,f);
rebonatto 0:c64e1194230b 220
rebonatto 0:c64e1194230b 221 delete buf;
rebonatto 0:c64e1194230b 222
rebonatto 0:c64e1194230b 223 if(idx >= 1024)
rebonatto 0:c64e1194230b 224 {
rebonatto 0:c64e1194230b 225 conn.send((char*)file_too_big_msg,30);
rebonatto 0:c64e1194230b 226 //conn.receive(&ans,&len);
rebonatto 0:c64e1194230b 227 delete ans;
rebonatto 0:c64e1194230b 228 return;
rebonatto 0:c64e1194230b 229 }
rebonatto 0:c64e1194230b 230
rebonatto 0:c64e1194230b 231 ack[2] = idx/0x100;
rebonatto 0:c64e1194230b 232 ack[3] = idx%0x100;
rebonatto 0:c64e1194230b 233 conn.send((char*)ack,4);
rebonatto 0:c64e1194230b 234 }
rebonatto 0:c64e1194230b 235 while(len == 516);
rebonatto 0:c64e1194230b 236
rebonatto 0:c64e1194230b 237 fclose(f);
rebonatto 0:c64e1194230b 238 */
rebonatto 0:c64e1194230b 239 }