⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http.lst

📁 C8051F控制网卡芯片实现网络通信功能。显示出第三代51单片机的强大之处
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.07   HTTP                                                                  11/25/2003 15:47:46 PAGE 1   


C51 COMPILER V7.07, COMPILATION OF MODULE HTTP
OBJECT MODULE PLACED IN HTTP.OBJ
COMPILER INVOKED BY: D:\KEIL707\C51\BIN\C51.EXE HTTP.C OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          //-----------------------------------------------------------------------------
   2          // Copyright (c) 2002 Jim Brady
   3          // Do not use commercially without author's permission
   4          // Last revised August 2002
   5          // Net HTTP.C
   6          //
   7          // This module is the Web Server
   8          // It currently serves a html text page and a jpeg image, or handles
   9          // a POST message to turn an LED on or off.
  10          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
  11          //-----------------------------------------------------------------------------
  12          #include <string.h>
  13          #include <stdlib.h>
  14          #include <ctype.h>              // toupper
  15          #include "C8051f.h"
  16          #include "net.h"
  17          #include "serial.h"  
  18          #include "cksum.h"
  19          #include "analog.h"
  20          #include "ip.h"
  21          #include "tcp.h"
  22          #include "http.h"
  23          
  24          
  25          // These structures keep track of connection information
  26          extern CONNECTION xdata conxn[];
  27           
  28          extern ULONG code my_ipaddr;
  29          extern char xdata text[];
  30          extern UINT idata cpu_temperature;
  31          extern UINT idata air_temperature;
  32          extern UINT idata cpu_voltage;
  33          extern char code html_header[];
  34          extern char code web_page[];
  35          extern char code jpeg_header[];
  36          extern UCHAR code photo1_jpeg[];
  37          extern UCHAR idata rcve_buf_allocated;
  38          extern UCHAR idata debug;
  39          bit CONTROL_LED;
  40          void LightONOFF(bit b);
  41          
  42          void init_http(void)
  43          {
  44   1        CONTROL_LED = 0;
  45   1        LightONOFF(CONTROL_LED);
  46   1      }
  47          
  48          
  49          
  50          //------------------------------------------------------------------------
  51          // This function is the standard string search. The Keil library
  52          // does not provide it.  It looks for one string in another string
  53          // and returns a pointer to it if found, otherwise returns NULL. 
  54          //------------------------------------------------------------------------
  55          char * strstr(char * haystack, char * needle)
C51 COMPILER V7.07   HTTP                                                                  11/25/2003 15:47:46 PAGE 2   

  56          {
  57   1              char *ptr1, *ptr2;
  58   1              
  59   1              // Protect against NULL pointer
  60   1              if (*needle == 0) return(haystack);
  61   1              for( ; *haystack; haystack++ )
  62   1              {
  63   2                      // Look for needle in haystack.  If there is a
  64   2            // match then this will continue all the way
  65   2            // until ptr1 reaches the NULL at the end of needle 
  66   2                      for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
  67   2                                                              
  68   2                      // If there is a match then return pointer to needle in haystack
  69   2                      if(*ptr1 == 0) return(haystack);        
  70   2              }
  71   1              return NULL;                    // no matching string found
  72   1      }
  73          
  74          
  75          
  76          //------------------------------------------------------------------------
  77          // This sends an TCP segment to the ip layer.  The segment is 
  78          // is normally either a web page or a graphic.
  79          // See "TCP/IP Illustrated, Volume 1" Sect 17.3
  80          //------------------------------------------------------------------------
  81          void http_send(UCHAR xdata * outbuf, UINT len, UCHAR nr)
  82          {
  83   1         TCP_HEADER xdata * tcp;
  84   1         IP_HEADER xdata * ip;
  85   1         ULONG idata sum;
  86   1         UINT idata result;
  87   1                
  88   1         // Fill in TCP segment header
  89   1         tcp = (TCP_HEADER xdata *)(outbuf + 34);
  90   1         ip = (IP_HEADER xdata *)(outbuf + 14);
  91   1      
  92   1         tcp->source_port = HTTP_PORT;
  93   1         tcp->dest_port = conxn[nr].port;
  94   1         tcp->sequence = conxn[nr].my_sequence;
  95   1         tcp->ack_number = conxn[nr].his_sequence;
  96   1            
  97   1              // Header is always 20 bytes long
  98   1         tcp->flags = 0x5000 | FLG_ACK | FLG_PSH;
  99   1         tcp->window = 1024;
 100   1         tcp->checksum = 0;
 101   1         tcp->urgent_ptr = 0;
 102   1         
 103   1         // Compute checksum including 12 bytes of pseudoheader
 104   1              // Must pre-fill 2 items in ip header to do this
 105   1              ip->dest_ipaddr = conxn[nr].ipaddr;
 106   1              ip->source_ipaddr = my_ipaddr;
 107   1                      
 108   1              // Sum source_ipaddr, dest_ipaddr, and entire TCP message 
 109   1              sum = (ULONG)cksum(outbuf + 26, 8 + len);
 110   1                                      
 111   1              // Add in the rest of pseudoheader which is
 112   1              // protocol id and TCP segment length
 113   1              sum += (ULONG)0x0006;
 114   1              sum += (ULONG)len;
 115   1      
 116   1              // In case there was a carry, add it back around
 117   1              result = (UINT)(sum + (sum >> 16));
C51 COMPILER V7.07   HTTP                                                                  11/25/2003 15:47:46 PAGE 3   

 118   1              tcp->checksum = ~result;
 119   1         
 120   1         if (debug) serial_send("TCP: Sending msg to IP layer\r");
 121   1              ip_send(outbuf, conxn[nr].ipaddr, TCP_TYPE, len);
 122   1      
 123   1         // (Re)start TCP retransmit timer
 124   1         conxn[nr].timer = TCP_TIMEOUT;
 125   1      }
 126          
 127          
 128          
 129          //------------------------------------------------------------------------
 130          // This searches a web page looking for a specified tag.  If found,
 131          // it replaces the tag with the text in * sub.  Tags are fixed length -
 132          // The first 4 chars of the tag is always "TAG:" and the rest of it
 133          // is always 4 chars for a total of 8 chars. 
 134          //------------------------------------------------------------------------
 135          void replace_tag(UCHAR xdata * start, char * tag, char * sub) 
 136          { 
 137   1         UCHAR idata i, flg;
 138   1         UCHAR xdata * ptr;
 139   1         
 140   1         // Find tag.  If not found - just return
 141   1         ptr = strstr(start, tag);
 142   1              if (ptr == NULL) return;
 143   1         
 144   1              flg = TRUE;
 145   1      
 146   1         // Replace the 8 char tag with the substitute text
 147   1              // Pad on the right with spaces
 148   1         for (i=0; i < 8; i++)
 149   1              {
 150   2              if (sub[i] == 0) flg = FALSE;
 151   2              if (flg) ptr[i] = sub[i]; else ptr[i] = SPACE;
 152   2              }
 153   1      }
 154          
 155          
 156          
 157          //------------------------------------------------------------------------
 158          //      This serves up either a HTML page, a JPEG image, or controls an 
 159          // LED,  depending what it gets from the browser.  The received header
 160          // must contain the word "GET" or "POST" to be considered a valid request.
 161          // With HTTP 1.1 where the connection is left open, the header I send
 162          // should include content length. With HTTP 1.0 you can just close the
 163          // connection after sending the page and the browser knows its done. 
 164          //
 165          // The HTTP protocol specification is at http://www.w3.org/Protocols/ 
 166          //------------------------------------------------------------------------
 167          UINT http_server(UCHAR xdata * inbuf, UINT header_len, UCHAR nr, UCHAR resend)
 168          {
 169   1              UCHAR i;
 170   1              UINT idata body_len, hhdr_len, jhdr_len, page_len, jpeg_len;
 171   1              UINT idata sent, remaining;
 172   1              UCHAR xdata * outbuf;
 173   1              UCHAR xdata * ptr;
 174   1              UCHAR xdata * tcp_data;
 175   1              UCHAR idata request;
 176   1         static UCHAR idata post_flg = FALSE;
 177   1                              
 178   1              // Make sure this is a valid connection
 179   1              if (nr == NO_CONNECTION) return 0;
C51 COMPILER V7.07   HTTP                                                                  11/25/2003 15:47:46 PAGE 4   

 180   1              
 181   1              // Compute start of TCP data
 182   1         
 183   1         // Save first 20 chars and seq number just in case
 184   1         // we need to re-generate page
 185   1         // TODO: if post, then save switch state infomation
 186   1         if (!resend)
 187   1         {
 188   2            tcp_data = inbuf + 34 + header_len;
 189   2            memcpy(conxn[nr].query, tcp_data, 20);
 190   2            conxn[nr].old_sequence = conxn[nr].my_sequence;
 191   2         }
 192   1         // If this is a resend, set sequence number to what it was
 193   1         // the last time we sent this
 194   1         else
 195   1         {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -