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

📄 https_callbacks.c

📁 Embeded MCU Tcpip code.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.                                             */
/*                 (C) Fujitsu Microelectronics Europe GmbH                  */
/*---------------------------------------------------------------------------

/*
 *Copyright (c) 2000-2002 Viola Systems Ltd.
 *All rights reserved.
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions 
 *are met:
 *
 *1. Redistributions of source code must retain the above copyright 
 *notice, this list of conditions and the following disclaimer.
 *
 *2. Redistributions in binary form must reproduce the above copyright 
 *notice, this list of conditions and the following disclaimer in the 
 *documentation and/or other materials provided with the distribution.
 *
 *3. The end-user documentation included with the redistribution, if 
 *any, must include the following acknowledgment:
 *	"This product includes software developed by Viola 
 *	Systems (http://www.violasystems.com/)."
 *
 *Alternately, this acknowledgment may appear in the software itself, 
 *if and wherever such third-party acknowledgments normally appear.
 *
 *4. The names "OpenTCP" and "Viola Systems" must not be used to 
 *endorse or promote products derived from this software without prior 
 *written permission. For written permission, please contact 
 *opentcp@opentcp.org.
 *
 *5. Products derived from this software may not be called "OpenTCP", 
 *nor may "OpenTCP" appear in their name, without prior written 
 *permission of the Viola Systems Ltd.
 *
 *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 
 *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE 
 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================
 *
 *OpenTCP is the unified open source TCP/IP stack available on a series 
 *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>.
 *
 *For more information on how to network-enable your devices, or how to 
 *obtain commercial technical support for OpenTCP, please see 
 *<http://www.violasystems.com/>.
 */

/** \file https_callbacks.c
 *	\brief HTTP server callback functions
 *	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\version 1.0
 *	\date 9.10.2002
 *  \bug
 *	\warning
 *	\todo
 *
 *	This file holds definitions and descriptions of HTTP callback functions
 *	that will be overridden by user code. These callback functions are 
 *	invoked by HTTP server to get a feedback from the part of HTTP server
 *	who's behaviour is defined by the user application.
 */ 
#include <string.h>
#include <stddef.h>
#include <stdio.h>

#include "../inet/datatypes.h"
#include "../inet/debug.h"
#include "../inet/globalvariables.h"
#include "../inet/system.h"
#include "../inet/tcp_ip.h"
#include "../inet/http/http_server.h"
#include "../inet/http/http_fs.h"
#include "../../stats.h"
#include "../../help.h"
#include "../../mb96348hs.h"

/* refresh counter */
static UINT8 browsercount;

/* Hash List:

	index.html	=>	11
	*/

/* Func: https_generateheader(https_generateheader(char* buffer, int version, int modifydate, int ctype, int clength)
 * Ret: length of modified *buffer pointer
 * Generates special header for browser
 */
UINT16 https_generateheader(UINT8* buffer, int version, int modifydate, int ctype, int clength)
{
  /* Header length */
  int hlength = 0, alength = 0;
 
  /* hit the browsercounter for better refresh */
  browsercount++;
     
  /* First, store the http version in buffer */
  switch(version)
  {
    case 0:
      /* HTTP Version 1.0 */
      alength += sprintf((char *)buffer, "HTTP/1.0 200 OK\r\n");
      buffer += alength;
      hlength += alength;
      alength = 0;
      break;
    
    case 1:
      /* HTTP Version 1.1 */
      alength += sprintf((char *)buffer, "HTTP/1.1 200 OK\r\n");
      buffer += alength;
      hlength += alength;
      alength = 0;
      break;
    
    default:
      printf("ERROR IN SETTING HTTP VERSION IN FUNC HTTPS_GENERATEHEADER!\n");
  }
  
  /* Set the last modified date */
  switch(modifydate)
  {
    case 0:
      /* Standard, my be changed if rtc runs */
      alength += sprintf((char *)buffer, "Last-modified: Fri, 18 Oct %i 12:04:32 GMT\r\n", browsercount);
      buffer += alength;
      hlength += alength;
      alength = 0;
      break;
    
    default:
      printf("ERROR IN SETTING HTTP VERSION IN FUNC HTTPS_GENERATEHEADER!\n");
  }
  
  /* Ok, now we will set the connection type of the http packet */
  switch(ctype)
  {
    case 0:
      /* standard plaintext */
      alength += sprintf((char *)buffer, "Server: ESERV-10/1.0\nContent-type: text/html\r\n");
	  buffer += alength;
      hlength += alength;
      alength = 0;
      break;
    case 1:
      /* image/gif */
      alength += sprintf((char *)buffer, "Server: ESERV-10/1.0\nContent-type: image/gif\r\n");
	  buffer += alength;
      hlength += alength;
      alength = 0;
      break;
    case 2:
      /* image/jpeg */
      alength += sprintf((char *)buffer, "Server: ESERV-10/1.0\nContent-type: image/jpeg\r\n");
	  buffer += alength;
      hlength += alength;
      alength = 0;
      break;
      
    default:
      printf("ERROR IN SETTING HTTP VERSION IN FUNC HTTPS_GENERATEHEADER!\n");
  }
  
  /* OK, now we will set the message length */
  alength += sprintf((char *)buffer, "Content-length: %i\r\n\r\n", clength);
  buffer += alength;
  hlength += alength;
  alength = 0;
      
  return hlength;
}	

/* Func: int getPortPins(int port, char *buf
 * Desc: generates statistics for int port and write it into char *buf)
 */
int getPortPins(UINT8 port, UINT8 part, char *portbuf)
{
  char text[][5] = {"Low", "High"};
  char color[][5] = {"red", "lime"};
  UINT16 textlength;
  
  switch(part)
  {
    case 1:
    {
      textlength = sprintf(portbuf, 
		 mcustatstemplate1,
		 port, 
		 color[PDR00>>port & 0x1], port, text[PDR00>>port & 0x1], 
		 color[PDR01>>port & 0x1], port, text[PDR01>>port & 0x1], 
		 color[PDR02>>port & 0x1], port, text[PDR02>>port & 0x1], 
		 color[PDR03>>port & 0x1], port, text[PDR03>>port & 0x1],
		 color[PDR04>>port & 0x1], port, text[PDR04>>port & 0x1]);
       break;
    }
    
    case 2:
    {
      textlength = sprintf(portbuf, 
		 mcustatstemplate2,
		 color[PDR05>>port & 0x1], port, text[PDR05>>port & 0x1], 
		 color[PDR06>>port & 0x1], port, text[PDR06>>port & 0x1], 
		 color[PDR07>>port & 0x1], port, text[PDR07>>port & 0x1], 
		 color[PDR08>>port & 0x1], port, text[PDR08>>port & 0x1], 
		 color[PDR09>>port & 0x1], port, text[PDR09>>port & 0x1]);
      break;
    }
  }
  
  return(textlength);
}

int getNetStats(int buflen, char *buf, unsigned char stat)
{
  buflen = 0;
  
  switch(stat)
  {
    case 0: { return(sprintf(buf, "<H1>Network Statistics</H1><BR><table border=\"0\"><TR><TD align=\"left\">Received Ethernet Packets</TD><TD align=\"right\">%li</TD></TR><TR><TD align=\"left\">Sent Ethernet Packets</TD><TD align=\"right\">%li</TD></TR>\n", ShowRecETH(), ShowTransETH())); }
    case 1: { return(sprintf(buf, "<TR><TD align=\"left\">Received IP Packets</TD><TD align=\"right\">%li</TD></TD>\n<TR><TD align=\"left\">Sent IP Packets</TD><TD align=\"right\">%li</TD></TR>\n", ShowRecIP(), ShowTransIP())); }
    case 2: { return(sprintf(buf, "<TR><TD align=\"left\">Received TCP Packets</TD><TD align=\"right\">%li</TD></TR>\n<TR><TD align=\"left\">Sent TCP Packets</TD><TD align=\"right\">%li</TD></TR>\n", ShowRecTCP(), ShowTransTCP())); }
    case 3: { return(sprintf(buf, "<TR><TD align=\"left\">IP Checksum Errors</TD><TD align=\"right\">%li</TD></TR>\n<TR><TD align=\"left\">ICMP Checksum Errors</TD><TD align=\"right\">%li</TD></TR>\n", ShowIpCSE(), ShowIcmpCSE())); }
    case 4: { return(sprintf(buf, "<TR><TD align=\"left\">TCP Checksum Errors</TD><TD align=\"right\">%li</TD></TR>\n<TR><TD align=\"left\">Zombie Packets</TD><TD align=\"right\">%li</TD></TR></TABLE><BR>\n", ShowTcpCSE(), ShowZombiePckts())); }
    
    default:
    {
      // do nothing;
    }
  }
  
  return 0;
}

int getFileStats(int buflen, char *buf, unsigned char stat)
{
  buflen = 0;
  
  switch(stat)
  {
    case 0: { return(sprintf(buf, "<H1>File Statistics</H1><BR><table border=\"0\"><TR><TD align=\"left\">/index.html</TD><TD align=\"left\"><img src=\"fade.png\" height=10 width=%i></TD></TR>\n", SH_index_html())); }
    case 1: { return(sprintf(buf, "<TR><TD align=\"left\">/mcustatus.html</TD><TD align=\"left\"><img src=\"fade.png\" height=10 width=%i></TD>\n", SH_mcustatus_html())); }
    case 2: { return(sprintf(buf, "<TR><TD align=\"left\">/files.html</TD><TD align=\"left\"><img src=\"fade.png\" height=10 width=%i></TD>\n", SH_filestats_html())); }
    case 3: { return(sprintf(buf, "<TR><TD align=\"left\">/stats.html</TD><TD align=\"left\"><img src=\"fade.png\" height=10 width=%i></TD></TR>\n</TABLE><BR>\n", SH_netstats_html())); }
  }
  
  return 0;
}
/** \brief Brief function description here
 * 	\author 
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 09.10.2002
 *	\param hash Calculated file-name hash value. Used so that the whole
 *		file name doesn't need to be stored in RAM
 *	\param ses HTTP session identifier
 *	\return
 *		\li -1 - This function should return -1 if no file has been found
 *		\li 1 - This function should return 1 if a file with appropriate
 *			hash value has been found.
 *	\warning
 *		\li This function <b>MUST</b> be implemented by user application
 *		to work with local configuration
 *
 *	This function is invoked by the HTTP server once a hash value of a 
 *	requested file name has been calculated. User application uses this
 *	hash value to check if appropriate file is available to web server.
 *	Appropriate https session entry is then filled accordingly.	
 *
 */
INT16 https_findfile (UINT8 hash, UINT8 ses)
{
	int file_not_found = 1;
	int filefound = 1;
	
	/* Access the File table on FLASH with given hash key	*/
	/* and modify session File parametera					*/
	
	switch (hash)
	{
	  case 11: /* index.html */
	    {
	    
	      https[ses].fstart = 0x00000001;
	      https[ses].flen = strlen(&https_main_html[0]);
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      INC_index_html();
	      return (-1);
	      break;
	    }
	  	    
	  case 28: /* style.css */
	    {
	    
	      https[ses].fstart = 0x00000003;
	      https[ses].flen = strlen(&https_style_css[0]);
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      return (-1);
	      break;
	    }
	    
	  case 237: /* logo.gif */   
	    {
	    
	      https[ses].fstart = 0x00000005;
	      https[ses].flen = 1727; /* Binary data, so size must be set by hand */
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      return (-1);
	      break;
	    }
	    
	  case 250: /* stats.html */
	    {
	    
	      https[ses].fstart = 0x00000009;
	      https[ses].flen = 1727; /* Binary data, so size must be set by hand */
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      INC_netstats_html();
	      return (-1);
	      break;
	    }
	  
	  case 171: /* location-bg.gif */
	    {
	    
	      https[ses].fstart = 0x0000000A;
	      https[ses].flen = 300; /* Binary data, so size must be set by hand */
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      return (-1);
	      break;
	    } 
	    
	  case 108: /* mcustatus.html */
	    {
	    
	      https[ses].fstart = 0x0000000D;
	      https[ses].flen = 100; 
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      INC_mcustatus_html();
	      return (-1);
	      break;
	    }
	    
	    
	  case 86: /* files.html */
	    {
	    
	      https[ses].fstart = 0x0000001F;
	      https[ses].flen = 300; 
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      INC_filestats_html();
	      return (-1);
	      break;
	    }
	    
	  case 147: /* fade.png */
	    {
	    
	      https[ses].fstart = 0x0000001E;
	      https[ses].flen = 196; 
	      https[ses].fpoint = 0;
	      https[ses].funacked = 0;
	      return (-1);
	      break;
	    }
	    
	  
	  default:
	    {
	      https[ses].fstart = 0xFFFFFFFF;
		  https[ses].funacked  = 0;
		  https[ses].flen = strlen(&https_not_found_page[0]);
		  https[ses].fpoint = 0;
		  return (-1);
		  break;
        }	
	}

	return(-1);  
}

/** \brief Fill network transmit buffer with HTTP headers&data
 * 	\author
 *		\li Jari Lahti (jari.lahti@violasystems.com)
 *	\date 09.10.2002
 *	\param ses HTTP session identifier
 *	\param buf Pointer to buffer where data is to be stored
 *	\param buflen Length of the buffer in bytes
 *	\return
 *		\li >=0 - Number of bytes written to buffer
 *	\warning 
 *		\li This function <b>MUST</b> be implemented by user application
 *		to work with local configuration
 *
 *	This handlers' job is to fill the buffer with the data that web server
 *	should return back through the TCP connection. This is accomplished
 *	based session identifer and values of variables in appropriate
 *	https entry.
 */

⌨️ 快捷键说明

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