app.c

来自「一个AVR 上的UIP移植程序」· C语言 代码 · 共 98 行

C
98
字号
/*****************************************************************************
*  "A Very Simple Application" from the uIP 0.6 documentation
*****************************************************************************/

#include <io.h>


#include "global.h"
#include "app.h"
#include "rprintf.h"
#include "uart.h"

u08* sendBufferPtr;
u08 sendBufferLen;


void appInit(void)
{
	// listen for telnet
	uip_listen(23);
	// listen for http
	uip_listen(80);
	
	DDRB = 0xFF;
	PORTB = 0xFF;

	rprintf("application initialized\r\n");
}

void appSendBufferPutchar(u08 c)
{
	*sendBufferPtr++ = c;
	sendBufferLen++;
}

void appEvent(void)
{
	switch(uip_conn->lport)
	{
	case htons(23):
		appServiceTelnet();
		break;
	case htons(80):
		appServiceHTTP();
		break;
	default:
		break;
	}
}

void appServiceTelnet(void)
{
	// redirect rprintf output to network buffer
	sendBufferPtr = uip_appdata;
	sendBufferLen = 0;
	rprintfInit(appSendBufferPutchar);

	if(uip_connected())
	{
		rprintf("Welcome to the Internet-AVR\r\n");
		rprintf("Type CTRL-D to disconnect\r\n");
		rprintf("avr>");
		uip_send(uip_appdata, sendBufferLen);
	}
	
	if(uip_newdata() || uip_rexmit())
	{
		switch(*uip_appdata)
		{
		//process control characters
		case 0x04:
			// process CTRL-D
			// terminate connection
			uip_close();
			break;
		case 0x0D:
			// process CR
			rprintf("\r\navr>");
			uip_send(uip_appdata, sendBufferLen);
			break;
		default:
			// echo typed characters
			PORTB = ~(*uip_appdata);
			uip_send(uip_appdata, uip_datalen());
			break;
		}
	}
	rprintfInit(uartSendByte);
}

void appServiceHTTP(void)
{
	if(uip_newdata() || uip_rexmit())
	{
		uip_send("Content-type: text/html\n\nHello", 3);
	}
}

⌨️ 快捷键说明

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