int main(void) { uint16_t dat_p; ADC_init(); _delay_ms ( 100 ); //DDRB |= (1<<PB0); initTimer(); //initialize the hardware driver for the enc28j60 enc28j60Init(mymac); enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(0); // 60us enc28j60PhyWrite(PHLCON,0x476); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); while(1) { // read packet, handle ping and wait for a tcp packet: dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0) { // no http request continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0) { // head, post and other methods: dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>")); www_server_reply(buf,dat_p); continue; } // just one web page in the "root directory" of the web server if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0) { //dat_p=print_webpage(buf); dat_p=printIndex(buf); www_server_reply(buf,dat_p); } else if (strncmp("/now.htm", (char *)&(buf[dat_p+4]),8) == 0 ) { dat_p = printNowHTML ( buf ); www_server_reply( buf, dat_p ); } else if (strncmp("/min0.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printMinHTML ( buf, 0 ); www_server_reply( buf, dat_p ); } else if (strncmp("/min1.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printMinHTML ( buf, 1 ); www_server_reply( buf, dat_p ); } else if (strncmp("/wlcm.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printWelcomeHTML ( buf ); www_server_reply( buf, dat_p ); } else { dat_p=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>")); www_server_reply(buf,dat_p); } } return (0); }
/* * Main entry point */ int main(void) { //=====init green led LEDInit(); LEDSet(1); //=====setup eth interface uint16_t plen = 0, dat_p = 0; //initialize enc28j60 enc28j60Init(mymac); _delay_ms(100); // 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); _delay_ms(100); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); register_ping_rec_callback(PingCallback); while(1){ // handle ping and wait for a tcp packet: dat_p = packetloop_arp_icmp_tcp(buf, enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0){ // do nothing continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ dat_p = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")); dat_p = fill_tcp_data_p(buf, dat_p, PSTR("<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *) & (buf[ dat_p + 4] ), 2) == 0 ){ nAccessCount++; dat_p = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")); // webcontent char szWebText[130] = {0}; sprintf(szWebText, "<b>ATmega8 Webserver / ENC28J60</b><br><br>Server stats:<br>Acc:%d Png:%d<br><br>(C) Radu Motisan 2012, www.pocketmagic.net<br/>Tomasz Jokiel", nAccessCount, nPingCount); dat_p = fill_tcp_data(buf, dat_p, szWebText); // goto SENDTCP; } // all other URLs: dat_p = 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>")); SENDTCP: www_server_reply(buf, dat_p); // send web page data } return (0); }
int main(void){ uint16_t dat_p; // 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. #if 0 CLKPR=(1<<CLKPCE); CLKPR=0; // 8 MHZ #endif _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); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); while(1){ // handle ping and wait for a tcp packet: dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0){ // do nothing continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ // head, post and other methods: // // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<p>OK, works great!</p>\n")); goto SENDTCP; } // all other URLs: dat_p=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>")); SENDTCP: www_server_reply(buf,dat_p); // send web page data } return (0); }
int main(void) { static uint8_t buffer[BufferSize + 1]; int8_t i; uint8_t message[4] = { 0, 0, CID, P2CID }; uint16_t received; snesIO port0 = 0xffff, port1 = 0xffff; // Initialise basic I/O. initLed(); initInput(); initOutput(); // Switched mode: B + Y. port0 = recvInput(); if (port0 == 0xfffc) { switchedMode = Enabled; ledSignal(5); } ledOnRed(); // Initialise network interface. enc28j60Init(mymac); _delay_ms(100); // 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); _delay_ms(100); // Get the initial IP via DHCP and configure network. init_mac(mymac); while (i != 1) { received = enc28j60PacketReceive(BufferSize, buffer); buffer[BufferSize] = '\0'; i = packetloop_dhcp_initial_ip_assignment(buffer, received, mymac[5]); } dhcp_get_my_ip(myip, netmask, gwip); client_ifconfig(myip, netmask); // Resolve MAC address from server IP. if (route_via_gw(serverip)) // Must be routed via gateway. get_mac_with_arp(gwip, TransNumGwmac, &arpresolverResultCallback); else // Server is on local network. get_mac_with_arp(serverip, TransNumGwmac, &arpresolverResultCallback); while (get_mac_with_arp_wait()) { received = enc28j60PacketReceive(BufferSize, buffer); // Call packetloop to process ARP reply. packetloop_arp_icmp_tcp(buffer, received); } // Lookup DNS of the server hostname. while (dnslkup_haveanswer() != 1) { uint16_t tmp; received = enc28j60PacketReceive(BufferSize, buffer); tmp = packetloop_arp_icmp_tcp(buffer, received); if (received == 0) { if (!enc28j60linkup()) continue; dnslkup_request(buffer, ServerVHost, gwmac); _delay_ms(100); continue; } if (tmp == 0) udp_client_check_for_dns_answer(buffer, received); } dnslkup_get_ip(serverip); ledOnGreen(); // Connected. while (1) { // Main loop start. received = enc28j60PacketReceive(BufferSize, buffer); // Software reset: L + R + Select + Start. if (port0 == 0xf3f3) reset(); // Do something while no packet in queue. if (received == 0) { port0 = recvInput(); // Prepare message and send it to the server. for (i = 0; i < 8; i++) { // Lo-Byte. char *c = message; *c = port0 & (1 << i) ? *c | (1 << i) : *c & ~(1 << i); } for (i = 0; i < 8; i++) { // Hi-Byte. char *c = message + 1; *c = port0 & (1 << i + 8) ? *c | (1 << i) : *c & ~(1 << i); } send_udp(buffer, message, sizeof(message), 57351, serverip, 57350, gwmac); // Send controller data to SNES. if (switchedMode == Disabled) sendOutput(port0, port1); else sendOutput(port1, port0); continue; } // Answer to ARP requests. if (eth_type_is_arp_and_my_ip(buffer, received)) { make_arp_answer_from_request(buffer, received); continue; } // Check if IP packets (ICMP or UDP) are for us. if (eth_type_is_ip_and_my_ip(buffer, received) == 0) continue; // Answer ping with pong. if ( buffer[IP_PROTO_P] == IP_PROTO_ICMP_V && buffer[ICMP_TYPE_P] == ICMP_TYPE_ECHOREQUEST_V) { make_echo_reply_from_request(buffer, received); continue; } // Listen for UDP packets on port 57351 (0xe007) and process // received data. if ( buffer[IP_PROTO_P] == IP_PROTO_UDP_V && buffer[UDP_DST_PORT_H_P] == 0xe0 && buffer[UDP_DST_PORT_L_P] == 0x07) { for (i = 0; i < 8; i++) { uint16_t *c = &port1; *c = buffer[UDP_DATA_P] & (1 << i) ? *c | (1 << i) : *c & ~(1 << i); } for (i = 0; i < 8; i++) { uint16_t *c = &port1; *c = buffer[UDP_DATA_P + 1] & (1 << i) ? *c | (1 << i + 8) : *c & ~(1 << i + 8); } } } // Main loop end. return (0); }
void ethernet_process(void) { uint16_t plen; uint16_t dat_p; uint8_t cmd_pos=0; int8_t cmd; uint8_t payloadlen=0; char str[30]; char cmdval; // get the next new packet: plen = enc28j60PacketReceive(BUFFER_SIZE, buf); buf[BUFFER_SIZE]='\0'; dat_p=packetloop_arp_icmp_tcp(buf,plen); #if 0 // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0) { // head, post and other methods: dat_p=http200ok(); dat_p=ES_fill_tcp_data(buf,dat_p,"<h1>200 OK</h1>"); goto SENDTCP; } #endif /*plen will ne unequal to zero if there is a valid * packet (without crc error) */ if(plen==0) { //continue; } // arp is broadcast if unknown but a host may also // verify the mac address by sending it to // a unicast address. if(eth_type_is_arp_and_my_ip(buf,plen)) { make_arp_answer_from_request(buf); //continue; } // check if ip packets are for us: if(eth_type_is_ip_and_my_ip(buf,plen)==0) { //continue; } #ifdef _DEBUG_ // led---------- if (i){ /* set output to Vcc, LED off */ PORTB|= (1<<PORTB1); i=0; }else{ /* set output to GND, LED on */ PORTB &= ~(1<<PORTB1); i=1; } #endif if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V) { // a ping packet, let's send pong make_echo_reply_from_request(buf,plen); //continue; } // tcp port www start, compare only the lower byte if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT) { if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V) { make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack //continue; } if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V) { init_len_info(buf); // init some data structures // we can possibly have no data, just ack: dat_p=get_tcp_data_pointer(); if (dat_p==0) { if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V) { // finack, answer with ack make_tcp_ack_from_any(buf,1,0); } // just an ack with no data, wait for next packet //continue; } if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0) { // head, post and other methods: // // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0) { plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")); plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/password</p>\n")); goto SENDTCP; } cmd=analyse_get_url((char *)&(buf[dat_p+5])); // 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; } #ifdef _DEBUG_ if (cmd==1) { PORTD|= (1<<PORTD7);// transistor on } if (cmd==0) { PORTD &= ~(1<<PORTD7);// transistor off } #endif if (cmd==-3) { // redirect to add a trailing slash plen=moved_perm(buf); goto SENDTCP; } #ifndef _DEBUG_ // if (cmd==-2) or any other value // just display the status: //plen=print_webpage(buf,(PORTD & (1<<PORTD7))); plen=print_webpage(buf); // #endif SENDTCP: plen=print_webpage(buf); make_tcp_ack_from_any(buf,1,0); // send ack for http get make_tcp_ack_with_data(buf,plen); // send data //continue; } } // tcp port www end // // udp start, we listen on udp port 1200=0x4B0 if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0) { 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=no_such_cmd"); goto ANSWER; } strcpy(str,"e=invalid_pw"); ANSWER: make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT); } }
/*---------------------------------------------------------------------------*/ int main(void) { uint16_t rval; /* init hardware layer */ init_hci(); timer1_init(); init_serial(); sei(); led1_high(); led2_high(); /* init protothreads */ PT_INIT(&blink_pt); PT_INIT(&nrf24_pt); PT_INIT(&www_client_pt); PT_INIT(&www_server_pt); PT_INIT(&udp_server_pt); PT_INIT(&coap_server_pt); PT_INIT(&temperature_pt); PT_INIT(&udp_broadcast_pt); /* greeting message */ dbg(PSTR("> Hello World!\r\n")); /* init the ethernet chip */ enc28j60Init(mymac); enc28j60PhyWrite(PHLCON,0x476); /* get automatic IP */ rval=0; init_mac(mymac); while(rval==0) { plen=enc28j60PacketReceive(BUFFER_SIZE, buf); rval=packetloop_dhcp_initial_ip_assignment(buf,plen,mymac[5]); } dhcp_get_my_ip(myip,netmask,gwip); client_ifconfig(myip,netmask); /* learn the MAC address of the gateway */ get_mac_with_arp(gwip,0,&arpresolver_result_callback); while(get_mac_with_arp_wait()) { plen=enc28j60PacketReceive(BUFFER_SIZE, buf); packetloop_arp_icmp_tcp(buf,plen); } /* set WWW server port */ www_server_port(MYWWWPORT); dbg(PSTR("> System is ready.\r\n")); led1_low(); led2_low(); /* main loop */ while(1) { if(enc28j60linkup()) { /* poll hardware ethernet buffer */ plen = enc28j60PacketReceive(BUFFER_SIZE,buf); /* terminate the buffer */ buf[BUFFER_SIZE]='\0'; /* handle DHCP messages if neccessary */ plen = packetloop_dhcp_renewhandler(buf,plen); /* handle and analyse the packet slightly */ dat_p = packetloop_arp_icmp_tcp(buf,plen); if( dat_p == 0) { udp_client_check_for_dns_answer(buf,plen); } new_packet = 0xFF; PT_SCHEDULE(blink_thread(&blink_pt)); PT_SCHEDULE(nrf24_thread(&nrf24_pt)); PT_SCHEDULE(www_server_thread(&www_server_pt)); PT_SCHEDULE(udp_server_thread(&udp_server_pt)); PT_SCHEDULE(www_client_thread(&www_client_pt)); PT_SCHEDULE(coap_server_thread(&coap_server_pt)); PT_SCHEDULE(temperature_thread(&temperature_pt)); PT_SCHEDULE(udp_broadcast_thread(&udp_broadcast_pt)); } } return 0; }