// analyse the url given // return values: -1 invalid password // -2 no command given // 0 switch off // 1 switch on // // The string passed to this function will look like this: // /?mn=1&pw=secret HTTP/1..... // / HTTP/1..... int8_t analyse_get_url(char *str) { uint8_t mn=0; char kvalstrbuf[10]; // the first slash: if (str[0] == '/' && str[1] == ' '){ // end of url, display just the web page return(2); } // str is now something like ?pw=secret&mn=0 or just end of url if (find_key_val(str,kvalstrbuf,10,"mn")){ if (kvalstrbuf[0]=='1'){ mn=1; } // to change the mail notification one needs also a valid passw: if (find_key_val(str,kvalstrbuf,10,"pw")){ if (verify_password(kvalstrbuf)){ return(mn); }else{ return(-1); } } } // browsers looking for /favion.ico, non existing pages etc... return(-1); }
// analyse the url given // return values: -1 invalid password // -2 no command given but password valid // -3 just refresh page // 0 switch off // 1 switch on // 2 favicon.ico // // The string passed to this function will look like this: // /password/?s=1 HTTP/1..... // /password/?s=0 HTTP/1..... // /password HTTP/1..... int8_t analyse_get_url(char *str) { uint8_t loop=15; // the first slash: if (*str == '/'){ str++; }else{ return(-1); } if (strncmp("favicon.ico",str,11)==0){ return(2); } // the password: if(verify_password(str)==0){ return(-1); } // move forward to the first space or '/' while(loop){ if(*str==' '){ // end of url and no slash after password: return(-2); } if(*str=='/'){ // end of password loop=0; continue; } str++; loop--; // do not loop too long } // str is now something like password?sw=1 or just end of url if (find_key_val(str,gStrbuf,5,"sw")){ if (gStrbuf[0]=='0'){ return(0); } if (gStrbuf[0]=='1'){ return(1); } } return(-3); }
static PT_THREAD(run_hello(struct httpd_state *s, char *ptr)) { //NOTE:local variables are not preserved during the calls to proto socket functins static char hello_name[20] = ""; PSOCK_BEGIN(&s->sout); //check if there are parameters passed if (s->param[0] && (find_key_val(s->param, hello_name, 20, "name") > 0)) { PSOCK_SEND_PSTR(&s->sout, PSTR("<H1>Hello ")); PSOCK_SEND_STR(&s->sout, hello_name); PSOCK_SEND_PSTR(&s->sout, PSTR("</H1><br>")); } PSOCK_SEND_PSTR(&s->sout, PSTR("\ <form action=\"/hello.shtml\" method=\"get\" bgcolor=\"#808080\">\ Enter your name: <input type=\"text\" name=\"name\" size=\"10\" value=\"")); PSOCK_SEND_STR(&s->sout, hello_name); PSOCK_SEND_PSTR(&s->sout, PSTR("\ \"/><br><input type = \"submit\" value=\"Send\" size=\"8\"> <input type = \"reset\" value=\"cancel\" size=\"8\">")); PSOCK_END(&s->sout); }
uint8_t EtherShield::ES_find_key_val(char *str,char *strbuf, uint8_t maxlen,char *key) { return find_key_val(str,strbuf, maxlen,key); }
uint8_t analyse_get_url(char *str) { errmsg="invalid pw"; if (strncmp("now",str,3)==0){ return(3); } if (strncmp("cnf",str,3)==0){ return(6); } if (strncmp("room1",str,5)==0){ return(10); } if (strncmp("room2",str,5)==0){ return(11); } if (strncmp("clk",str,3)==0){ return(12); } // Set the new time or date if (strncmp("cdclk",str,4)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"h")){ urldecode(strbuf); time.hr = atoi(strbuf); if (find_key_val(str,"m")){ urldecode(strbuf); time.min = atoi(strbuf); if (find_key_val(str,"s")){ urldecode(strbuf); time.sec = atoi(strbuf); if (find_key_val(str,"dd")){ urldecode(strbuf); time.dat = atoi(strbuf); if (find_key_val(str,"mm")){ urldecode(strbuf); time.mon = atoi(strbuf); if (find_key_val(str,"yy")){ urldecode(strbuf); time.yr = atoi(strbuf); strbuf[7]='\0'; return(35); } } } } } } } } } // change temp in room1 if (strncmp("tmpc",str,4)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"ntemp")){ urldecode(strbuf); strbuf[2]='\0'; return(20); } } } } // change temp in room2 if (strncmp("tmpc2",str,5)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"ntemp2")){ urldecode(strbuf); strbuf[2]='\0'; return(21); } } } } // change on / off status on slave-modules if (strncmp("stat1",str,5)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"onoff")){ urldecode(strbuf); strbuf[2]='\0'; return(34); } } } } // change on / off status on slave-modules if (strncmp("stat2",str,5)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"onoff2")){ urldecode(strbuf); strbuf[2]='\0'; return(32); } } } } // change own ip and pw if (strncmp("ipc",str,3)==0){ if (find_key_val(str,"pw")){ urldecode(strbuf); if (verify_password(strbuf)){ if (find_key_val(str,"nip")){ urldecode(strbuf); if (parse_ip(myip,strbuf)){ errmsg="invalid ip"; return(0); } strbuf[7]='\0'; return(7); } } if (find_key_val(str,"npw")){ urldecode(strbuf); strbuf[7]='\0'; strcpy(password,strbuf); return(2); } } return(0); } if (strncmp("mod",str,3)==0){ return(2); } errmsg="inv. url"; return(0); }
// analyse the url given // The string passed to this function will look like this: // ?s=1 HTTP/1..... // We start after the first slash ("/" already removed) int8_t analyse_get_url(char *str) { // the first slash: if (*str == 'c'){ // alarm configpage: gPlen=print_alarm_config(); return(10); } if (*str == 'n'){ // network configpage: gPlen=print_net_config(); return(10); } if (*str == 'u'){ if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"pw")){ urldecode(gStrbuf); if (verify_password(gStrbuf)){ if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"n")){ urldecode(gStrbuf); gStrbuf[MYNAME_LEN]='\0'; strcpy(myname,gStrbuf); } if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"di")){ urldecode(gStrbuf); if (parse_ip(udpsrvip,gStrbuf)!=0){ return(-2); } // we've found destip, which means this is update from the // alarm conf page (this is a mandatory field) // Check alarm check box here if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"ae")){ alarmOn=1; }else{ alarmOn=0; } } if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"dp")){ gStrbuf[4]='\0'; udpsrvport=atoi(gStrbuf); } if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"hb")){ gStrbuf[4]='\0'; heartbeat_timeout_sec=atoi(gStrbuf); // we've found heartbeat, which means this is update from the // network conf page // Check dhcp check box here if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"dh")){ dhcpOn=1; }else{ dhcpOn=0; } } if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"gi")){ urldecode(gStrbuf); if (parse_ip(gwip,gStrbuf)!=0){ return(-2); } } if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"ip")){ urldecode(gStrbuf); if (parse_ip(myip,gStrbuf)!=0){ return(-2); } } data2eeprom(); gPlen=http200ok(); gPlen=fill_tcp_data_p(buf,gPlen,PSTR("<a href=/>[home]</a>")); gPlen=fill_tcp_data_p(buf,gPlen,PSTR("<h2>OK</h2>")); return(10); } } return(-1); } return(0); }
int main(void){ uint16_t plen; uint16_t dat_p; //uint8_t cmd_pos=0; int8_t cmd; //uint8_t payloadlen=0; //char str[20]; //char cmdval; //char req[16]; //char i; DATA_DAIKIN daikin; // set the clock speed to "no pre-scaler" (8MHz with internal osc or // full external speed) // set the clock prescaler. First write CLKPCE to enable setting // of clock the next four instructions. // Note that the CKDIV8 Fuse determines the initial // value of the CKKPS bits. CLKPR=(1<<CLKPCE); // change enable CLKPR=0; // "no pre-scaler" _delay_loop_1(0); // 60us /*initialize enc28j60*/ enc28j60Init(mymac); //enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(0); // 60us /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ // LEDB=yellow LEDA=green // // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); enc28j60PhyWrite(PHLCON,0x476); //DDRB|= (1<<DDB1); // enable PB1, LED as output // the transistor on PD7: //DDRD|= (1<<DDD7); //PORTD &= ~(1<<PORTD7);// transistor off //PORTB = 0xff; //DDRB = 0xff; init_power_settings(); uart_init(); init_ir(); init_airController(&daikin); sei(); xfunc_out = (void (*)(char))uart_put; xputs(PSTR("AVR-Ethernet test monitor\n")); xprintf(PSTR("ENC28J60 Rev.%d\n"), enc28j60getrev()); xprintf(PSTR("PRR=%02X\n"),PRR); //init the web server ethernet/ip layer: init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT); while(1){ // handle ping and wait for a tcp packet plen=enc28j60PacketReceive(BUFFER_SIZE, buf); buf[BUFFER_SIZE]='\0'; dat_p=packetloop_icmp_tcp(buf,plen); if(dat_p==0){ // check if udp otherwise continue goto UDP; } // send data everytime we get a http request xprintf(PSTR("get http request\n")); if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ xprintf(PSTR("'GET \n'")); // head, post and other methods: // // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/pc or m</p>\n")); goto SENDTCP; } if ((strncmp("/pc ",(char *)&(buf[dat_p+4]),4)==0) || (strncmp("/pc/ ",(char *)&(buf[dat_p+4]),5)==0)){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,pc_html); goto SENDTCP; } if ((strncmp("/m ",(char *)&(buf[dat_p+4]),3)==0) || (strncmp("/m/ ",(char *)&(buf[dat_p+4]),4)==0)){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,mobile_html); goto SENDTCP; } // GET /ir?power=on&temp=28 if (strncmp("/ir?",(char *)&(buf[dat_p+4]),4)==0){ if(find_key_val( (char *)&(buf[dat_p+8]), gStrbuf, 5, "power" ) ) { xprintf(PSTR("power=%s\n"),gStrbuf); if(strncmp("on", gStrbuf, 2) == 0){ airController_on(&daikin); setData(DAIKIN, daikin.buf, 35*8); }else if(strncmp("off", gStrbuf, 3) == 0){ airController_off(&daikin); setData(DAIKIN, daikin.buf, 35*8); } } if(find_key_val( (char *)&(buf[dat_p+8]), gStrbuf, 4, "temp" ) ) { xprintf(PSTR("temp=%s\n"),gStrbuf); airController_setTemp(&daikin, atoi(gStrbuf)); setData(DAIKIN, daikin.buf, 35*8); } plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("Done\n")); goto SENDTCP; } cmd=analyse_get_url((char *)&(buf[dat_p+4])); // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html if (cmd==-1){ plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>")); goto SENDTCP; } if (cmd==1){ //PORTD|= (1<<PORTD7);// transistor on } if (cmd==0){ //PORTD &= ~(1<<PORTD7);// transistor off } if (cmd==2){ // favicon: //plen=moved_perm(buf,0); goto SENDTCP; } if (cmd==-2){ // redirect to the right base url (e.g add a trailing slash): plen=moved_perm(buf,1); goto SENDTCP; } // if (cmd==-2) or any other value // just display the status: //plen=print_webpage(buf,/*(PORTD & (1<<PORTD7))*/0); // SENDTCP: www_server_reply(buf,plen); // send data continue; // tcp port www end // ------------------------------- // udp start, we listen on udp port 1200=0x4B0 UDP: // xprintf(PSTR("here is UDP:\n")); // check if ip packets are for us: if(eth_type_is_ip_and_my_ip(buf,plen)==0){ // xprintf(PSTR("here is eth_type_is_ip_andd_my_ip\n")); continue; } /*if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==(MYUDPPORT>>8)&&buf[UDP_DST_PORT_L_P]==(MYUDPPORT&0xff)){ payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; // you must sent a string starting with v // e.g udpcom version 10.0.0.24 if (verify_password((char *)&(buf[UDP_DATA_P]))){ // find the first comma which indicates // the start of a command: cmd_pos=0; while(cmd_pos<payloadlen){ cmd_pos++; if (buf[UDP_DATA_P+cmd_pos]==','){ cmd_pos++; // put on start of cmd break; } } // a command is one char and a value. At // least 3 characters long. It has an '=' on // position 2: if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='='){ strcpy(str,"e=no_cmd"); goto ANSWER; } // supported commands are // t=1 t=0 t=? if (buf[UDP_DATA_P+cmd_pos]=='t'){ cmdval=buf[UDP_DATA_P+cmd_pos+2]; if(cmdval=='1'){ PORTD|= (1<<PORTD7);// transistor on strcpy(str,"t=1"); goto ANSWER; }else if(cmdval=='0'){ PORTD &= ~(1<<PORTD7);// transistor off strcpy(str,"t=0"); goto ANSWER; }else if(cmdval=='?'){ if (PORTD & (1<<PORTD7)){ strcpy(str,"t=1"); goto ANSWER; } strcpy(str,"t=0"); goto ANSWER; } } strcpy(str,"e=inv_cmd"); goto ANSWER; } strcpy(str,"e=inv_pw"); ANSWER: make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT); }*/ } return (0); }