📄 webserver.c
字号:
/********************************************* * vim:sw=8:ts=8:si:et * To use the above modeline in vim you must have "set modeline" in your .vimrc * Author: Guido Socher * Copyright: GPL V2 * See http://www.gnu.org/licenses/gpl.html * * Ethernet remote device and sensor * UDP and HTTP interface url looks like this http://baseurl/password/command or http://baseurl/password/ * * Chip type : Atmega88 or Atmega168 with ENC28J60 * Note: there is a version number in the text. Search for tuxgraphics *********************************************/
/*
\\\|///
\\ - - //
( @ @ )
+---------------------oOOo-(_)-oOOo-------------------------+
| WEB SERVER |
| ported to LPC2103 ARM7TDMI-S CPU |
| by Xiaoran Liu |
| 2007.12.16 |
| ZERO research Instutute |
| www.the0.net |
| Oooo |
+----------------------oooO--( )--------------------------+
( ) ) /
\ ( (_/
\_)
*/
//#include <stdlib.h>#include <stdio.h>#include <string.h>#include "ip_arp_udp_tcp.h"#include "enc28j60.h"#include "net.h"#include <LPC2103.H>
#define LED ( 1 << 17 ) //P0.17控制LED
#define PSTR(s) sextern void delay_ms(unsigned char ms);
// please modify the following two lines. mac and ip have to be unique// in your local area network. You can not have the same numbers in// two devices:static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUXstatic uint8_t myip[4] = {192,168,1,25};// listen port for tcp/www (max range 1-254)#define MYWWWPORT 80//// listen port for udp#define MYUDPPORT 1200#define BUFFER_SIZE 1500//450static uint8_t buf[BUFFER_SIZE+1];// the password string (only the first 5 char checked), (only a-z,0-9,_ characters):static char password[]="the0.net"; // must not be longer than 9 char// uint8_t verify_password(char *str){ // the first characters of the received string are // a simple password/cookie: if (strncmp(password,str,5)==0){ return(1); } return(0);}// takes a string of the form password/commandNumber and analyse it// return values: -1 invalid password, otherwise command number// -2 no command given but password valid// -3 valid password, no command and no trailing "/"int8_t analyse_get_url(char *str){ uint8_t loop=1; uint8_t i=0; while(loop){ if(password[i]){ if(*str==password[i]){ str++; i++; }else{ return(-1); } }else{ // end of password loop=0; } } // is is now one char after the password if (*str == '/'){ str++; }else{ return(-3); } // check the first char, garbage after this is ignored (including a slash) if (*str < 0x3a && *str > 0x2f){ // is a ASCII number, return it return(*str-0x30); } return(-2);}
// answer HTTP/1.0 301 Moved Permanently\r\nLocation: password/\r\n\r\n// to redirect to the url ending in a slashuint16_t moved_perm(uint8_t *buf){ uint16_t plen; plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 301 Moved Permanently\r\nLocation: ")); plen=fill_tcp_data(buf,plen,password); plen=fill_tcp_data_p(buf,plen,PSTR("/\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")); plen=fill_tcp_data_p(buf,plen,PSTR("<h1>301 Moved Permanently</h1>\n")); plen=fill_tcp_data_p(buf,plen,PSTR("add a trailing slash to the url\n")); return(plen);}// prepare the webpage by writing the data to the tcp send bufferuint16_t print_webpage(uint8_t *buf,uint8_t on_off){ uint16_t plen; plen=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")); plen=fill_tcp_data_p(buf,plen,PSTR("<center><p>Output is: ")); if (on_off){ plen=fill_tcp_data_p(buf,plen,PSTR("<font color=\"#00FF00\"> ON</font>")); }else{ plen=fill_tcp_data_p(buf,plen,PSTR("OFF")); } plen=fill_tcp_data_p(buf,plen,PSTR(" <small><a href=\".\">[refresh status]</a></small></p>\n<p><a href=\".")); if (on_off){ plen=fill_tcp_data_p(buf,plen,PSTR("/0\">Switch off</a><p>")); }else{ plen=fill_tcp_data_p(buf,plen,PSTR("/1\">Switch on</a><p>")); } plen=fill_tcp_data_p(buf,plen,PSTR("</center><hr><br>version 2.10, tuxgraphics.org www.the0.net\n")); return(plen);}int main(void){ uint16_t plen; uint16_t dat_p; uint8_t i=0; uint8_t cmd_pos=0; int8_t cmd; uint8_t payloadlen=0; char str[30]; char cmdval;
// 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. //CLKPR=(1<<CLKPCE); // change enable //CLKPR=0; // "no pre-scaler" //delay_ms(200);
//printStr("A simple Web Server\n");
/* enable PD2/INT0, as input */ //DDRD&= ~(1<<DDD2); /*initialize enc28j60*/ enc28j60Init(mymac); enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz delay_ms(10); // LED /* enable PB1, LED as output */ //DDRB|= (1<<DDB1); IODIR |= LED; //设置IO口为输出口
/* set output to Vcc, LED off */ //PORTB|= (1<<PB1);
IOSET = LED; /* // the transistor on PD7 DDRD|= (1<<DDD7); PORTD &= ~(1<<PD7);// transistor off */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -