protegemed, aquisição via A/D simples utilizando interrupção do timer

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of ptgm_semDMA by Marcelo Rebonatto

Committer:
rebonatto
Date:
Tue Jan 05 11:47:35 2016 +0000
Revision:
0:fac116e94d44
Vers?o est?vel sem DMA e FFT. 128 amostras.

Who changed what in which revision?

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