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
Codes/TelnetServer.cpp@2:86c3cb25577b, 2014-07-21 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:c64e1194230b | 1 | /* |
rebonatto | 0:c64e1194230b | 2 | Original autor: Francisco |
rebonatto | 0:c64e1194230b | 3 | Modificacao do uso da rede: rebonatto |
rebonatto | 0:c64e1194230b | 4 | */ |
rebonatto | 0:c64e1194230b | 5 | |
rebonatto | 0:c64e1194230b | 6 | |
rebonatto | 0:c64e1194230b | 7 | #include "TelnetServer.h" |
rebonatto | 0:c64e1194230b | 8 | |
rebonatto | 0:c64e1194230b | 9 | #include "Split.h" |
rebonatto | 0:c64e1194230b | 10 | |
rebonatto | 0:c64e1194230b | 11 | #define OPT_WILL 251 |
rebonatto | 0:c64e1194230b | 12 | #define OPT_WONT 252 |
rebonatto | 0:c64e1194230b | 13 | #define OPT_DO 253 |
rebonatto | 0:c64e1194230b | 14 | #define OPT_DONT 254 |
rebonatto | 0:c64e1194230b | 15 | |
rebonatto | 0:c64e1194230b | 16 | char* welcome_msg = "Protegemed - welcome to telnet\r\nType 'help' for a list of commands\r\n"; |
rebonatto | 0:c64e1194230b | 17 | char* prompt_msg = "\r\n$ "; |
rebonatto | 0:c64e1194230b | 18 | char iac_will_echo[] = {0xFF,0xFB,0x01}; |
rebonatto | 0:c64e1194230b | 19 | char erase_cmd[] = { 0x1B,0x5B,0x44,0x1B,0x5B,0x4B}; |
rebonatto | 0:c64e1194230b | 20 | |
rebonatto | 0:c64e1194230b | 21 | const struct telnet_cmd_handler TelnetServer::cmds[] = { |
rebonatto | 0:c64e1194230b | 22 | {"exit",(cmd_function)-1}, |
rebonatto | 0:c64e1194230b | 23 | {"help",TelnetServer::HelpCommand}, |
rebonatto | 0:c64e1194230b | 24 | {"reset",TelnetServer::ResetCommand}, |
rebonatto | 0:c64e1194230b | 25 | {"remove",TelnetServer::RemoveCommand}, |
rebonatto | 0:c64e1194230b | 26 | {"listparam",TelnetServer::ListParamCommand}, |
rebonatto | 0:c64e1194230b | 27 | {"getparam",TelnetServer::GetParamCommand}, |
rebonatto | 0:c64e1194230b | 28 | {"setparam",TelnetServer::SetParamCommand}, |
rebonatto | 0:c64e1194230b | 29 | {"version",TelnetServer::VersionCommand}, |
rebonatto | 0:c64e1194230b | 30 | {"update",TelnetServer::UpdateCommand}, |
rebonatto | 0:c64e1194230b | 31 | {"",(cmd_function)0} |
rebonatto | 0:c64e1194230b | 32 | }; |
rebonatto | 0:c64e1194230b | 33 | |
rebonatto | 0:c64e1194230b | 34 | void TelnetServer::TelnetServer_Thread(void const* arg) |
rebonatto | 0:c64e1194230b | 35 | { |
rebonatto | 0:c64e1194230b | 36 | int r=0; |
rebonatto | 0:c64e1194230b | 37 | printf("Telnet Thread starting... \r\nCommands up to 100 chars\n"); |
rebonatto | 0:c64e1194230b | 38 | |
rebonatto | 0:c64e1194230b | 39 | TCPSocketServer server; |
rebonatto | 0:c64e1194230b | 40 | |
rebonatto | 0:c64e1194230b | 41 | server.bind(23); |
rebonatto | 0:c64e1194230b | 42 | server.listen(); |
rebonatto | 0:c64e1194230b | 43 | |
rebonatto | 0:c64e1194230b | 44 | while(1) |
rebonatto | 0:c64e1194230b | 45 | { |
rebonatto | 0:c64e1194230b | 46 | TCPSocketConnection* conn ; |
rebonatto | 0:c64e1194230b | 47 | TCPSocketConnection aux; |
rebonatto | 0:c64e1194230b | 48 | r = server.accept(aux); |
rebonatto | 0:c64e1194230b | 49 | conn = &aux; |
rebonatto | 0:c64e1194230b | 50 | printf("Aceitou conexao \n"); |
rebonatto | 0:c64e1194230b | 51 | if (r != 0){ |
rebonatto | 0:c64e1194230b | 52 | printf("Error in connection\n"); |
rebonatto | 0:c64e1194230b | 53 | exit(1); |
rebonatto | 0:c64e1194230b | 54 | } |
rebonatto | 0:c64e1194230b | 55 | |
rebonatto | 0:c64e1194230b | 56 | TelnetSession(conn); |
rebonatto | 0:c64e1194230b | 57 | printf("Fechou\n"); |
rebonatto | 0:c64e1194230b | 58 | conn->close(); |
rebonatto | 0:c64e1194230b | 59 | } |
rebonatto | 0:c64e1194230b | 60 | } |
rebonatto | 0:c64e1194230b | 61 | |
rebonatto | 0:c64e1194230b | 62 | void TelnetServer::TelnetSession(TCPSocketConnection *conn) |
rebonatto | 0:c64e1194230b | 63 | { |
rebonatto | 0:c64e1194230b | 64 | int r; |
rebonatto | 0:c64e1194230b | 65 | int buffer_ptr=0; |
rebonatto | 0:c64e1194230b | 66 | |
rebonatto | 0:c64e1194230b | 67 | printf("Connected to %s:%d\n",conn->get_address(),conn->get_port()); |
rebonatto | 0:c64e1194230b | 68 | |
rebonatto | 0:c64e1194230b | 69 | conn->send(welcome_msg,strlen(welcome_msg)); |
rebonatto | 0:c64e1194230b | 70 | conn->send(prompt_msg,strlen(prompt_msg)); |
rebonatto | 0:c64e1194230b | 71 | |
rebonatto | 0:c64e1194230b | 72 | conn->set_blocking(true); |
rebonatto | 0:c64e1194230b | 73 | |
rebonatto | 0:c64e1194230b | 74 | while(1) |
rebonatto | 0:c64e1194230b | 75 | { |
rebonatto | 0:c64e1194230b | 76 | char buf_rec[260]; |
rebonatto | 0:c64e1194230b | 77 | unsigned char buf[260]; |
rebonatto | 0:c64e1194230b | 78 | char buffer[260]; |
rebonatto | 0:c64e1194230b | 79 | |
rebonatto | 0:c64e1194230b | 80 | //printf("Vai recevber\n"); |
rebonatto | 0:c64e1194230b | 81 | r = conn->receive(buf_rec, strlen(buf_rec) ); |
rebonatto | 0:c64e1194230b | 82 | |
rebonatto | 0:c64e1194230b | 83 | /* need memcpy becouse commands are ready to unsigned char and tcp receive char */ |
rebonatto | 0:c64e1194230b | 84 | memcpy(buf, buf_rec, r); |
rebonatto | 0:c64e1194230b | 85 | |
rebonatto | 0:c64e1194230b | 86 | //printf("Receive %d\n", r); |
rebonatto | 0:c64e1194230b | 87 | |
rebonatto | 0:c64e1194230b | 88 | if(r == -1) |
rebonatto | 0:c64e1194230b | 89 | { |
rebonatto | 0:c64e1194230b | 90 | printf("Error %d %s\n", r, buf); |
rebonatto | 0:c64e1194230b | 91 | break; |
rebonatto | 0:c64e1194230b | 92 | } |
rebonatto | 0:c64e1194230b | 93 | |
rebonatto | 0:c64e1194230b | 94 | if (r > 120){ |
rebonatto | 0:c64e1194230b | 95 | printf("Max command length = 100 chars\n"); |
rebonatto | 0:c64e1194230b | 96 | //send |
rebonatto | 0:c64e1194230b | 97 | break; |
rebonatto | 0:c64e1194230b | 98 | } |
rebonatto | 0:c64e1194230b | 99 | |
rebonatto | 0:c64e1194230b | 100 | //printf("Got Here\n"); |
rebonatto | 0:c64e1194230b | 101 | |
rebonatto | 0:c64e1194230b | 102 | for(int i=0;i<r;i++) |
rebonatto | 0:c64e1194230b | 103 | { |
rebonatto | 0:c64e1194230b | 104 | //check if it is a printable or any of the line control chars |
rebonatto | 0:c64e1194230b | 105 | if((buf[i]>31 && buf[i] < 128) || buf[i]=='\r' || buf[i]=='\n' || buf[i]=='\t') |
rebonatto | 0:c64e1194230b | 106 | { |
rebonatto | 0:c64e1194230b | 107 | //append to the buffer |
rebonatto | 0:c64e1194230b | 108 | buffer[buffer_ptr] = buf[i]; |
rebonatto | 0:c64e1194230b | 109 | buffer_ptr++; |
rebonatto | 0:c64e1194230b | 110 | } |
rebonatto | 0:c64e1194230b | 111 | else if(buf[i] == '\b') //backspace |
rebonatto | 0:c64e1194230b | 112 | { |
rebonatto | 0:c64e1194230b | 113 | //erases the character from the command buffer |
rebonatto | 0:c64e1194230b | 114 | if(buffer_ptr > 0) |
rebonatto | 0:c64e1194230b | 115 | { |
rebonatto | 0:c64e1194230b | 116 | buffer_ptr--; |
rebonatto | 0:c64e1194230b | 117 | } |
rebonatto | 0:c64e1194230b | 118 | //resets m variable state (will not cause error on the next block of code |
rebonatto | 0:c64e1194230b | 119 | |
rebonatto | 0:c64e1194230b | 120 | } |
rebonatto | 0:c64e1194230b | 121 | else if((int)buf[i] == 255) //IAC - Interpret As Command |
rebonatto | 0:c64e1194230b | 122 | { |
rebonatto | 0:c64e1194230b | 123 | if((int)buf[i+1] >= 251) |
rebonatto | 0:c64e1194230b | 124 | { |
rebonatto | 0:c64e1194230b | 125 | option_negotiator(conn,buf[i+1],buf[i+2]); |
rebonatto | 0:c64e1194230b | 126 | i+=2; |
rebonatto | 0:c64e1194230b | 127 | } |
rebonatto | 0:c64e1194230b | 128 | else |
rebonatto | 0:c64e1194230b | 129 | i+=1; |
rebonatto | 0:c64e1194230b | 130 | } |
rebonatto | 0:c64e1194230b | 131 | } |
rebonatto | 0:c64e1194230b | 132 | |
rebonatto | 0:c64e1194230b | 133 | //detect a carriage return and line feed sequence. Trigger command processor |
rebonatto | 0:c64e1194230b | 134 | if(buffer_ptr >= 2) |
rebonatto | 0:c64e1194230b | 135 | { |
rebonatto | 0:c64e1194230b | 136 | if(buffer[buffer_ptr-1] == '\n' && buffer[buffer_ptr-2] == '\r') |
rebonatto | 0:c64e1194230b | 137 | { |
rebonatto | 0:c64e1194230b | 138 | char **command; |
rebonatto | 0:c64e1194230b | 139 | int command_count; |
rebonatto | 0:c64e1194230b | 140 | |
rebonatto | 0:c64e1194230b | 141 | buffer[buffer_ptr-2] = '\0'; |
rebonatto | 0:c64e1194230b | 142 | command_count = split((char*)buffer," ",&command); |
rebonatto | 0:c64e1194230b | 143 | |
rebonatto | 0:c64e1194230b | 144 | printf("Command found: %s\n", command[0]); |
rebonatto | 0:c64e1194230b | 145 | |
rebonatto | 0:c64e1194230b | 146 | //must find a function in the table and then execute it, passing the command array as an argument |
rebonatto | 0:c64e1194230b | 147 | for(int i=0;cmds[i].pfn != 0;i++) |
rebonatto | 0:c64e1194230b | 148 | { |
rebonatto | 0:c64e1194230b | 149 | if(!strcasecmp(command[0],cmds[i].command_name)) |
rebonatto | 0:c64e1194230b | 150 | { |
rebonatto | 0:c64e1194230b | 151 | if((int)cmds[i].pfn == -1)//exit cmd |
rebonatto | 0:c64e1194230b | 152 | { |
rebonatto | 0:c64e1194230b | 153 | //delete buffer; |
rebonatto | 0:c64e1194230b | 154 | return; |
rebonatto | 0:c64e1194230b | 155 | } |
rebonatto | 0:c64e1194230b | 156 | else |
rebonatto | 0:c64e1194230b | 157 | { |
rebonatto | 0:c64e1194230b | 158 | cmds[i].pfn(conn,command,command_count); |
rebonatto | 0:c64e1194230b | 159 | break; |
rebonatto | 0:c64e1194230b | 160 | } |
rebonatto | 0:c64e1194230b | 161 | } |
rebonatto | 0:c64e1194230b | 162 | } |
rebonatto | 0:c64e1194230b | 163 | |
rebonatto | 0:c64e1194230b | 164 | //write the prompt |
rebonatto | 0:c64e1194230b | 165 | conn->send(prompt_msg,strlen(prompt_msg)); |
rebonatto | 0:c64e1194230b | 166 | buffer_ptr=0; |
rebonatto | 0:c64e1194230b | 167 | } |
rebonatto | 0:c64e1194230b | 168 | } |
rebonatto | 0:c64e1194230b | 169 | } |
rebonatto | 0:c64e1194230b | 170 | //delete buffer; |
rebonatto | 0:c64e1194230b | 171 | } |
rebonatto | 0:c64e1194230b | 172 | |
rebonatto | 0:c64e1194230b | 173 | void TelnetServer::option_negotiator(TCPSocketConnection *conn,unsigned char opt_cmd,unsigned char opt_param) |
rebonatto | 0:c64e1194230b | 174 | { |
rebonatto | 0:c64e1194230b | 175 | char opt[3]={0,0,0}; |
rebonatto | 0:c64e1194230b | 176 | |
rebonatto | 0:c64e1194230b | 177 | if(opt_param == 1 && (opt_cmd == OPT_DO || opt_cmd == OPT_DONT)) //response for our will echo |
rebonatto | 0:c64e1194230b | 178 | { |
rebonatto | 0:c64e1194230b | 179 | printf("HERE"); |
rebonatto | 0:c64e1194230b | 180 | return; |
rebonatto | 0:c64e1194230b | 181 | } |
rebonatto | 0:c64e1194230b | 182 | if(opt_cmd == OPT_DO) //every other option that it will ask to us to do, we won't |
rebonatto | 0:c64e1194230b | 183 | { |
rebonatto | 0:c64e1194230b | 184 | opt[0] = 255; |
rebonatto | 0:c64e1194230b | 185 | opt[1] = OPT_WONT; |
rebonatto | 0:c64e1194230b | 186 | opt[2] = opt_param; |
rebonatto | 0:c64e1194230b | 187 | conn->send(opt,3); |
rebonatto | 0:c64e1194230b | 188 | } |
rebonatto | 0:c64e1194230b | 189 | else if(opt_cmd == OPT_WILL && opt_param==3) //OK to supperss go ahead |
rebonatto | 0:c64e1194230b | 190 | { |
rebonatto | 0:c64e1194230b | 191 | opt[0] = 255; |
rebonatto | 0:c64e1194230b | 192 | opt[1] = OPT_DO; |
rebonatto | 0:c64e1194230b | 193 | opt[2] = opt_param; |
rebonatto | 0:c64e1194230b | 194 | conn->send(opt,3); |
rebonatto | 0:c64e1194230b | 195 | } |
rebonatto | 0:c64e1194230b | 196 | else if(opt_cmd == OPT_WILL) //every other option that it will ask do, we don't |
rebonatto | 0:c64e1194230b | 197 | { |
rebonatto | 0:c64e1194230b | 198 | opt[0] = 255; |
rebonatto | 0:c64e1194230b | 199 | opt[1] = OPT_DONT; |
rebonatto | 0:c64e1194230b | 200 | opt[2] = opt_param; |
rebonatto | 0:c64e1194230b | 201 | conn->send(opt,3); |
rebonatto | 0:c64e1194230b | 202 | |
rebonatto | 0:c64e1194230b | 203 | } |
rebonatto | 0:c64e1194230b | 204 | |
rebonatto | 0:c64e1194230b | 205 | } |
rebonatto | 0:c64e1194230b | 206 |