|
- #include "main.h"
- static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
- static uint8_t myip[4] = {192,168,0,24};
- #define MYWWWPORT 80
- #define MYUDPPORT 1200
- #define BUFFER_SIZE 550
- static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
- static vu32 TimingDelay = 0;
- static uint8_t buf[BUFFER_SIZE+1];
- static char password[]="secret";
- /*u16 fill_tcp_data_p(char *buf,u16 pos, u8 *progmem_s)
- {
- char c;
- // fill in tcp data at position pos
- //
- // with no options the data starts after the checksum + 2 more bytes (urgent ptr)
- while ((c = *(progmem_s++))) {
- buf[TCP_CHECKSUM_L_P+3+pos]=c;
- pos++;
- }
- return(pos);
- }*/
- char strncmp ( char * s1, char * s2, char n)
- {
- if ( !n )//n为无符号整形变量;如果n为0,则返回0
- return(0);
-
- while (--n && *s1 && *s1 == *s2)
- {
- s1++;//S1指针自加1,指向下一个字符
- s2++;//S2指针自加1,指向下一个字符
- }
- return( *s1 - *s2 );//返回比较结果
- }
- 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 slash
- uint16_t moved_perm(uint8_t *buf)
- {
- uint16_t plen;
- plen=fill_tcp_data_p(buf,0,"HTTP/1.0 301 Moved Permanently\r\nLocation: ");
- plen=fill_tcp_data(buf,plen,password);
- plen=fill_tcp_data_p(buf,plen,"/\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
- plen=fill_tcp_data_p(buf,plen,"<h1>301 Moved Permanently</h1>\n");
- plen=fill_tcp_data_p(buf,plen,"add a trailing slash to the url\n");
- return(plen);
- }
- // prepare the webpage by writing the data to the tcp send buffer
- uint16_t print_webpage(uint8_t *buf,uint8_t on_off)
- {
- uint16_t plen;
- plen=fill_tcp_data_p(buf,0,"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,"<center><p>Output is: ");
- if (on_off){
- plen=fill_tcp_data_p(buf,plen,"<font color=\"#00FF00\"> ON</font>");
- }else{
- plen=fill_tcp_data_p(buf,plen,"OFF");
- }
- plen=fill_tcp_data_p(buf,plen," <small><a href=\".\">[refresh status]</a></small></p>\n<p><a href=\".");
- if (on_off){
- plen=fill_tcp_data_p(buf,plen,"/0\">Switch off</a><p>");
- }else{
- plen=fill_tcp_data_p(buf,plen,"/1\">Switch on</a><p>");
- }
- plen=fill_tcp_data_p(buf,plen,"</center><hr><br>version 2.17, TangYC\n");
- return(plen);
- }
- /**
- * @brief Main program.
- * @param None
- * @retval : None
- */
- int main(void)
- {
- uint16_t plen=0;
- uint16_t dat_p;
- int8_t cmd;
- uint8_t payloadlen,cmd_pos;
- char str[30];
- char cmdval;
-
- RCC_Init();
- InterruptConfig();
- SysTick_Config();
- SPI_Config();
- GPIO_Config();
- enc28j60Init(mymac);
-
-
- /* Infinite loop */
- //init the ethernet/ip layer:
- init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
- while(1){
- // get the next new packet:
- plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
- /*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:
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
|
|