UIP协议栈移植到u-boot详解
???????? Author: 楊正? date:2014.11.5? Email:y2012ww@gmail.com?QQ: 1209758756
1、uip簡(jiǎn)介
?????? Uip網(wǎng)絡(luò)是一個(gè)簡(jiǎn)單好用的嵌入式協(xié)議棧,易于移植且消耗的內(nèi)存空間較少,應(yīng)用于很多嵌入式產(chǎn)品。uIP 協(xié)議棧去掉了完整的TCP/IP系統(tǒng)中不常用的功能,簡(jiǎn)化了通訊流程,只保留了網(wǎng)絡(luò)通信必須使用的協(xié)議,設(shè)計(jì)重點(diǎn)放在了IP/TCP/ICMP/UDP/ARP這些網(wǎng)絡(luò)層和傳輸層的協(xié)議上,因此保證了其代碼的通用性和結(jié)構(gòu)的穩(wěn)定性。由于uIP協(xié)議棧專門為嵌入式系統(tǒng)而設(shè)計(jì),因此還具有以下優(yōu)越功能:
(1)代碼非常少,其協(xié)議棧代碼不到6K,方便閱讀與移植。
(2)占用的內(nèi)存數(shù)極少,RAM占用僅幾百字節(jié)。
(3)它的硬件處理層、協(xié)議棧層和應(yīng)用層共用一個(gè)全局緩存區(qū),不存在數(shù)據(jù)拷貝,而且發(fā)送和接收都是依靠這個(gè)緩存區(qū),極大的節(jié)省了空間和時(shí)間。
(4)支持多個(gè)主動(dòng)連接和被動(dòng)連接并發(fā)。
(5)它的源代碼中提供一套實(shí)例程序:web 客戶端,web 服務(wù)器,Telnet 服務(wù)器,電子郵件發(fā)送程序(SMTP 客戶端),DNS 主機(jī)名解析程序等。
(6)在數(shù)據(jù)的處理上采用輪循機(jī)制,不需要操作系統(tǒng)的支持。由于 uIP 對(duì)資源的需求少而且移植容易,大部分的8位微控制器都使用過uIP協(xié)議棧, 而且很多著名的嵌入式產(chǎn)品和項(xiàng)目(如衛(wèi)星,Cisco 路由器,無線傳感器網(wǎng)絡(luò))中均在使用uIP 協(xié)議棧。
下面就開始進(jìn)入移植過程。
2、uip移植
2.1 uip架構(gòu)
在移植uip之前先簡(jiǎn)單看一下uip的大致框架,uIP相當(dāng)于一個(gè)代碼庫,通過一系列的函數(shù)實(shí)現(xiàn)與底層硬件和高層應(yīng)用程序之間的通訊,對(duì)于整個(gè)系統(tǒng)來說它內(nèi)部的協(xié)議組是透明的,從而增加了協(xié)議的通用性。uIP協(xié)議棧與系統(tǒng)底層和高層應(yīng)用之間的關(guān)系如下:
uIP 協(xié)議棧主要提供了三個(gè)函數(shù)供系統(tǒng)底層調(diào)用。即uip_init(), uip_input()和uip_periodic()。其與應(yīng)用程序的主要接口是UIP_APPCALL( )。
uip_init()是系統(tǒng)初始化時(shí)調(diào)用的,主要用于初始化協(xié)議棧的偵聽端口和默認(rèn)所有連接是關(guān)閉的。當(dāng)網(wǎng)卡驅(qū)動(dòng)收到一個(gè)輸入包時(shí),將其放入全局緩沖區(qū) uip_buf 中,包的大小由全局變量uip_len 約束。同時(shí)將調(diào)用uip_input()函數(shù),這個(gè)函數(shù)將會(huì)根據(jù)包首部的協(xié)議處理這個(gè)包并在需要時(shí)調(diào)用應(yīng)用程序。當(dāng)uip_input()返回時(shí),一個(gè)輸出包同樣放在全局緩沖區(qū)uip_buf 里,并把大小賦給uip_len。若uip_len 是0,則說明沒有包要發(fā)送;否則調(diào)用底層系統(tǒng)的發(fā)包函數(shù)就會(huì)將包發(fā)送到網(wǎng)絡(luò)上。uIP周期計(jì)時(shí)用于驅(qū)動(dòng)所有的uIP內(nèi)部時(shí)鐘事件:當(dāng)周期計(jì)時(shí)激發(fā),每一個(gè)TCP連接都會(huì)調(diào)用uIP函數(shù)uip_periodic()。類似于uip_input()函數(shù),uip_periodic()函數(shù)返回時(shí),輸出的IP 包要放到uip_buf 中,供底層系統(tǒng)查詢uip_len 的大小并發(fā)送。由于TCP/IP 的應(yīng)用場(chǎng)景很多,所以應(yīng)用程序作為單獨(dú)的模塊由用戶實(shí)現(xiàn)。uIP 協(xié)議棧提供一系列接口函數(shù)供用戶程序調(diào)用,其中大部分函數(shù)是作為C的宏命令實(shí)現(xiàn)的,主要是為了速度、代碼大小、堆棧和效率的使用。用戶需要將應(yīng)用層入口程序作為接口提供給uIP協(xié)議棧,并將這個(gè)函數(shù)定義為UIP_APPCALL()。這樣以來,uIP在接受到底層傳來的數(shù)據(jù)包后,在需要送到上層應(yīng)用程序處理的地方,調(diào)用UIP_APPCALL(),在不用修改協(xié)議棧的情況下可以適配不同的應(yīng)用程序。
?
2.2 UIP移植過程
2.2.1 拷貝UIP-0.9到u-boot-2010.06/net目錄
以u(píng)-boot_sources_for_tp-link_AR9331_by_pepe2k為藍(lán)本進(jìn)行移植,將該藍(lán)本里面的uip-0.9這個(gè)目錄復(fù)制到我的u-boot 的u-boot-2010.06/net目錄下,這個(gè)uip-0.9里面有這個(gè)一些文件:
ap121.h? ctype.h?fsdata.c?? tapdev.c??? uip_arch.h?uip_arp.h?uip.h
ar7240.h??????? flash.h?fsdata.h? httpd.c? main.c???tapdev.h??? cmd_confdefs.h? fs.c????fs.h?????httpd.h? Makefile? uip_arch.c?uip_arp.c??uip.c?????uipopt.h
當(dāng)然不是所有的文件都用到。拷貝過來以后要修改makefile不然編譯會(huì)通不過的,我的makefile修改如下:
CC=g++
CFLAGS=-Wall -O2-fpack-struct -DDUMP=0
?
CFLAGS += -I../../include
?
all: uip
?
uip: uip.o uip_arch.otapdev.o httpd.o main.o fs.o uip_arp.o
#uip: uip.o uip_arch.otapdev.o main.o fs.o uip_arp.o
??? $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
?
%.o: %.c
??? $(CC) $(CFLAGS) -c? $^ -o $@
?
clean:
rm-f *.o *~ *core uip
接下來就進(jìn)入u-boot-2010.06/net目錄下的net.c文件,其實(shí)整個(gè)移植過程都是在這個(gè)文件里面打轉(zhuǎn)。
?
2.2.2 在u-boot-2010.06/net/net.c里面添加函數(shù)
在net.c里面添加NetReceiveHttpd()函數(shù):
void NetReceiveHttpd(volatileuchar * inpkt, int len) {
??? memcpy(uip_buf, (const void *) inpkt, len);
??? uip_len = len;
?
#ifdef ET_DEBUG???? //debug?by yangzheng
??? DBG("NetReceiveHttpd buf->type =%04X\n", ntohs(BUF->type));
#endif
??? if (BUF->type == htons(UIP_ETHTYPE_IP)){
#ifdef ET_DEBUG???? //debug?by yangzheng
??? DBG("buf type isUIP_ETHTYPE_IP\n");
#endif
??????? uip_arp_ipin();?? //處理傳入的ip包
??????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
??????? DBG("uip_len2=%d\n",uip_len);
??????? uip_input();? //從上往下封裝包的函數(shù),這個(gè)函數(shù)會(huì)調(diào)用UIP_APPCALL()
??????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
??????? DBG("uip_len2=%d\n",uip_len);
??????? if (uip_len > 0) {
??????????? DBG("ipin->uip bufferinside data\n");
??????????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
??????????? uip_arp_out();?? //arp請(qǐng)求發(fā)送函數(shù)
??????????? NetSendHttpd();?? //調(diào)用網(wǎng)卡驅(qū)動(dòng)的發(fā)送函數(shù)
??????? }
??? } else if (BUF->type ==htons(UIP_ETHTYPE_ARP)) {
#ifdef ET_DEBUG???? //debug?by yangzheng
??? DBG("buf type isUIP_ETHTYPE_ARP\n");
#endif
uip_arp_arpin();? ?//處理arp應(yīng)答
??????? DBG("uip_len3=%d\n",uip_len);
??????? if (uip_len > 0) {
??????????? DBG("arpin->uip bufferinside data\n");
??????????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
??????????? NetSendHttpd();
??????? }
??? }
}
在net.c的NetReceive()函數(shù)里面還要添加如下代碼,調(diào)用NetReceiveHttpd()函數(shù):
?
if(webfailsafe_is_running)
{
NetReceiveHttpd(inpkt,len);? //這個(gè)函數(shù)上面已經(jīng)說明
return;
}
?
?
?
?
?
這里有一個(gè)函數(shù)需要說明一下,因?yàn)槲以谶@里徘徊了很久。ARP請(qǐng)求發(fā)送函數(shù):
void uip_arp_out(void)(在uip-0.9目錄的uip_arp.c里面定義)。
*==================================================================
* 為傳出的IP包添加以太網(wǎng)頭并看是否需要發(fā)送ARP請(qǐng)求.?
* 此函數(shù)應(yīng)該在發(fā)送IP包時(shí)調(diào)用,它會(huì)檢查IP包的目的IP地址,看看以太網(wǎng)應(yīng)該使用什么目的MAC地址.
* 如果目的IP地址是在局域網(wǎng)中(由IP地址與子網(wǎng)掩碼的與邏輯決定),函數(shù)就會(huì)從ARP緩存表中查找有
* 無對(duì)應(yīng)項(xiàng).若有,就取對(duì)應(yīng)的MAC地址,加上以太網(wǎng)頭,并返回,否則uip_buf[]中的數(shù)據(jù)包會(huì)被替換成一個(gè)
* 目的IP在址的ARP請(qǐng)求.原來的IP包會(huì)被簡(jiǎn)單的仍掉,此函數(shù)假設(shè)高層協(xié)議(如TCP)會(huì)最終重傳扔掉的包.
* 如果目標(biāo)IP地址并非一個(gè)局域網(wǎng)IP,則會(huì)使用默認(rèn)路由的IP地址.
* uip_len.函數(shù)返回時(shí),uip_buf[]中已經(jīng)有了一個(gè)包,其長(zhǎng)度由uip_len指定.
*===================================================================
void?uip_arp_out(void)
{
??? ?struct arp_entry?*tabptr=0;
?
ipaddr[0] =IPBUF->destipaddr[0];
??? ipaddr[1] = IPBUF->destipaddr[1];
???DBG("****************ipaddr[0]=%x******************ipaddr[1]=%x\n",ipaddr[0], ???ipaddr[1]);
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
?????
? for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
??? tabptr = &arp_table[i];
??? if(ipaddr[0] == tabptr->ipaddr[0]&&
?????? ipaddr[1] == tabptr->ipaddr[1])
????? break;
? }
?
?????????if(i?==?UIP_ARPTAB_SIZE)
?? ? ???{
????? ? ???/*?如果遍歷到頭沒找到,將原IP包替換為ARP請(qǐng)求并返回?*/
?????????????
??? memset(BUF->ethhdr.dest.addr, 0xff, 6);
??? memset(BUF->dhwaddr.addr, 0x00, 6);
??? memcpy(BUF->ethhdr.src.addr,uip_ethaddr.addr, 6);
??? memcpy(BUF->shwaddr.addr,uip_ethaddr.addr, 6);
???
??? BUF->dipaddr[0] = ipaddr[0];
??? BUF->dipaddr[1] = ipaddr[1];
??? BUF->sipaddr[0] = uip_hostaddr[0];
??? BUF->sipaddr[1] = uip_hostaddr[1];
??? BUF->opcode = HTONS(ARP_REQUEST); /* ARPrequest. */
??? BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
??? BUF->protocol = HTONS(UIP_ETHTYPE_IP);
??? BUF->hwlen = 6;
??? BUF->protolen = 4;
??? BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__ );
?
??? uip_appdata = &uip_buf[40 +UIP_LLH_LEN];
???
??? uip_len = sizeof(struct arp_hdr);
??? return;
? }
?
? /* Build an ethernet header. */
? memcpy(IPBUF->ethhdr.dest.addr,tabptr->ethaddr.addr, 6);
? memcpy(IPBUF->ethhdr.src.addr,uip_ethaddr.addr, 6);
?
? IPBUF->ethhdr.type =HTONS(UIP_ETHTYPE_IP);
?
? uip_len += sizeof(struct uip_eth_hdr);
}
?
接下來看arp應(yīng)答函數(shù):uip_arp_arpin()在uip-0.9目錄的uip_arp.c里面定義。
uip_arp_arpin()函數(shù)主要是處理ARP應(yīng)答。這個(gè)函數(shù)是在設(shè)備接收到ARP包時(shí),由驅(qū)動(dòng)程序調(diào)用的.如果收到是ARP包是一個(gè)對(duì)本地主機(jī)上次發(fā)送的ARP請(qǐng)求的應(yīng)答,那么就從包中取得自己想要的主機(jī)的MAC地址,加入自己的ARP緩存表中.如果收到是一個(gè)ARP請(qǐng)求,那就把自己的MAC地址打包成一個(gè)ARP應(yīng)答,發(fā)送給請(qǐng)求的主機(jī)。
/*-----------------------------------------------------------------------------------*/
/**
?* ARP processing for incoming ARP packets.
?*
?* This function should be called by the devicedriver when an ARP
?* packet has been received. The function willact differently
?* depending on the ARP packet type: if it is areply for a request
?* that we previously sent out, the ARP cachewill be filled in with
?* the values from the ARP reply. If theincoming ARP packet is an ARP
?* request for our IP address, an ARP replypacket is created and put
?* into the uip_buf[] buffer.
?*
?* When the function returns, the value of theglobal variable uip_len
?* indicates whether the device driver shouldsend out a packet or
?* not. If uip_len is zero, no packet should besent. If uip_len is
?* non-zero, it contains the length of theoutbound packet that is
?* present in the uip_buf[] buffer.
?*
?* This function expects an ARP packet with aprepended Ethernet
?* header in the uip_buf[] buffer, and thelength of the packet in the
?* global variable uip_len.
?*/
/*-----------------------------------------------------------------------------------*/
void
uip_arp_arpin(void)
{
? if(uip_len < sizeof(struct arp_hdr)) {
??? uip_len = 0;
??? return;
? }
?
? uip_len = 0;
?
? int ar = HTONS(ARP_REQUEST);
?
? case HTONS(ARP_REQUEST):? //arp請(qǐng)求:1,arp應(yīng)答:2
??? /* ARP request. If it asked for ouraddress, we send out a
?????? reply. */
??? if(BUF->dipaddr[0] == uip_hostaddr[0]&&
?????? BUF->dipaddr[1] == uip_hostaddr[1]) {
????? /* The reply opcode is 2. */
????? BUF->opcode = HTONS(2); ?//回應(yīng)的操作碼是2
?????
????? //將收到的arp包的發(fā)送端以太網(wǎng)地址變?yōu)槟康囊蕴W(wǎng)地址
????? memcpy(BUF->dhwaddr.addr,BUF->shwaddr.addr, 6);
????? //將自己的以太網(wǎng)地址賦值給arp包的發(fā)送端以太網(wǎng)地址
????? memcpy(BUF->shwaddr.addr,uip_ethaddr.addr, 6);
????? memcpy(BUF->ethhdr.src.addr,uip_ethaddr.addr, 6);
????? memcpy(BUF->ethhdr.dest.addr,BUF->dhwaddr.addr, 6);
?????
????? BUF->dipaddr[0] = BUF->sipaddr[0];
????? BUF->dipaddr[1] = BUF->sipaddr[1];
?
????? BUF->sipaddr[0] = uip_hostaddr[0];
????? BUF->sipaddr[1] = uip_hostaddr[1];
?
?
????? BUF->ethhdr.type =HTONS(UIP_ETHTYPE_ARP);?????
????? uip_len = sizeof(struct arp_hdr);
?????
??? }?????
??? break;
? case HTONS(ARP_REPLY):
??? /* ARP reply. We insert or update the ARPtable if it was meant
?????? for us. */
??? if(BUF->dipaddr[0] == uip_hostaddr[0]&&
?????? BUF->dipaddr[1] == uip_hostaddr[1]) {
?
???? ??uip_arp_update(BUF->sipaddr,&BUF->shwaddr);
??? }
??? break;
? }
?
? return;
}
?
在net.c里面還要添加如下函數(shù):
#define BUF???????????? ((struct uip_eth_hdr*)&uip_buf[0])
voidNetSendHttpd(void) {
??? volatile uchar *tmpbuf = NetTxPacket;
??? int i;
?
??? for (i = 0; i < 40 + UIP_LLH_LEN; i++) {
??????? tmpbuf[i] = uip_buf[i];
??????? //printf("uip_buf[%d]=%d\n",i, uip_buf[i]);
??? }
?
??? for (; i < uip_len; i++) {
??????? tmpbuf[i] = uip_appdata[i - 40 -UIP_LLH_LEN];
??? }
??? eth_send(NetTxPacket, uip_len);? //這里就是調(diào)用uboot里面網(wǎng)卡?驅(qū)動(dòng)的發(fā)送函數(shù)
??? //NetSendPacket(NetTxPacket, uip_len);
}
?
voidHttpdHandler(void) {??? //
??? int i;
?
??? for (i = 0; i < UIP_CONNS; i++) {
??????? uip_periodic(i);
?
??????? if (uip_len > 0) {
??????????? uip_arp_out();
??????????? NetSendHttpd();
??????? }
??? }
?
??? // TODO: check this
??? if (++arptimer == 20) {
??????? uip_arp_timer();
??????? arptimer = 0;
??? }
}
?
// start http daemon
void HttpdStart(void){
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
??? uip_init();??? ?//下面講到
??? httpd_init();
}
?
?
uip協(xié)議結(jié)合網(wǎng)卡芯片組成嵌入式網(wǎng)卡,硬件提供能力,uip提供策略。由上往下逐步封裝數(shù)據(jù),如:
應(yīng)用層----------傳輸層------------網(wǎng)絡(luò)層---------------數(shù)據(jù)鏈路層---------物理層
應(yīng)用數(shù)據(jù)------>tcp封裝頭------>IP封裝頭-----------MAC封裝+尾部------->發(fā)送
?
任何事物都需要一個(gè)初始化的過程,uip協(xié)議棧也不例外,uip協(xié)議通過uip_init()來初始化。uip_init()函數(shù)里主要的工作是:
1.? 將uip_state結(jié)構(gòu)體全部清零。
2.? 初始化用于TCP連接的uip_conn結(jié)構(gòu)體,將連接狀態(tài)置為close。
3.? 設(shè)置用于TCP連接的端口號(hào)lastport=1024。
4.? 如果定義了UDP,同樣也要初始化。
/*-----------------------------------------------------------------------------------*/
void
uip_init(void)
{
? for(c = 0; c < UIP_LISTENPORTS; ++c) {
??? uip_listenports[c] = 0;
? }
? for(c = 0; c < UIP_CONNS; ++c) {
??? uip_conns[c].tcpstateflags = CLOSED;
? }
#if UIP_ACTIVE_OPEN
? lastport = 1024;
#endif /*UIP_ACTIVE_OPEN */
?
#if UIP_UDP
? for(c = 0; c < UIP_UDP_CONNS; ++c) {
??? uip_udp_conns[c].lport = 0;
? }
#endif /* UIP_UDP */
?
? /* IPv4 initialization. */
#if UIP_FIXEDADDR == 0
? uip_hostaddr[0] = uip_hostaddr[1] = 0;
#endif /*UIP_FIXEDADDR */
}
/*-----------------------------------------------------------------------------------*/
?
?
在net.c里面還要添加NetLoopHttpd(void)函數(shù),這個(gè)函數(shù)前半部分可以模仿net.c里面的NetLoop()函數(shù)。紅色部分是字節(jié)序的轉(zhuǎn)換,在移植過程當(dāng)中,字節(jié)序轉(zhuǎn)換也是花了很長(zhǎng)時(shí)間。
int NetLoopHttpd(void){
?????? bd_t *bd = gd->bd;
?????? unsigned short int ip[2];
?????? unsigned char ethinit_attempt = 0;
?????? struct uip_eth_addr eaddr;
?
#ifdef CONFIG_NET_MULTI
?????? NetRestarted = 0;
?????? NetDevExists = 0;
#endif
?
?????? /* XXX problem with bss workaround */
?????? NetArpWaitPacketMAC = NULL;
?????? NetArpWaitTxPacket = NULL;
?????? NetArpWaitPacketIP = 0;
?????? NetArpWaitReplyIP = 0;
?????? NetArpWaitTxPacket = NULL;
?????? NetTxPacket = NULL;
?
?????? if (!NetTxPacket) {
????????????? int i;
????????????? // Setup packet buffers, alignedcorrectly.
????????????? NetTxPacket = &PktBuf[0] +(PKTALIGN - 1);
????????????? NetTxPacket -= (ulong) NetTxPacket% PKTALIGN;
?
????????????? for (i = 0; i < PKTBUFSRX; i++){
???????????????????? NetRxPackets[i] =NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
????????????? }
?????? }
?
?????? if (!NetArpWaitTxPacket) {
????????????? NetArpWaitTxPacket =&NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
????????????? NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
????????????? NetArpWaitTxPacketSize = 0;
?????? }
?
?????? // restart label
?????? restart:
?
?????? eth_halt();
?
#ifdef CONFIG_NET_MULTI
?????? eth_set_current();
#endif
?
??? int ret = eth_init(bd);
??? DBG("eth_init = %d\n", ret);
?????? while(ethinit_attempt < 10){
????????????? if(!eth_init(bd)){???? //eth_init->!eth_init???? add by yangzheng
??????????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
???????????????????? ethinit_attempt = 0;
???????????????????? break;
????????????? } else {
???????????????????? ethinit_attempt++;
???????????????????? eth_halt();
???????????????????? milisecdelay(1000);
????????????? }
?????? }
?
?????? if (ethinit_attempt > 0) {
????????????? eth_halt();
????????????? printf("## Error: couldn'tinitialize eth (cable disconnected?)!\n\n");
????????????? return (-1);
?????? }
?
?????? // get MAC address
#ifdefCONFIG_NET_MULTI
?????? memcpy(NetOurEther,eth_get_dev()->enetaddr, 6);
#else
?????? eth_getenv_enetaddr("ethaddr",NetOurEther);
#endif
?
??? //獲取物理地址
?????? eaddr.addr[0] = NetOurEther[0];
?????? eaddr.addr[1] = NetOurEther[1];
?????? eaddr.addr[2] = NetOurEther[2];
?????? eaddr.addr[3] = NetOurEther[3];
?????? eaddr.addr[4] = NetOurEther[4];
?????? eaddr.addr[5] = NetOurEther[5];
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
??? DBG("%x:%x:%x:%x:%x:%x\n",eaddr.addr[0], eaddr.addr[1], eaddr.addr[2], eaddr.addr[3], eaddr.addr[4],eaddr.addr[5]);
?
?????? // set MAC address
?????? uip_setethaddr(eaddr);
?
?????? // set ip and other addresses
?????? // TODO: do we need this with uIP stack?
?????? NetCopyIP(&NetOurIP,&bd->bi_ip_addr);
?????? NetOurGatewayIP = getenv_IPaddr("gatewayip");
?????? NetOurSubnetMask =getenv_IPaddr("netmask");
?????? NetOurVLAN =getenv_VLAN("vlan");
?????? NetOurNativeVLAN =getenv_VLAN("nvlan");
?
?????? // start server...
//???? printf("HTTP server is starting at IP:%ld.%ld.%ld.%ld\n", (bd->bi_ip_addr & 0xff000000) >> 24,(bd->bi_ip_addr & 0x00ff0000) >> 16, (bd->bi_ip_addr &0x0000ff00) >> 8, (bd->bi_ip_addr & 0x000000ff));
//?? printf("HTTP server is starting at IP:%ld.%ld.%ld.%ld\n", (bd->bi_ip_addr & 0x000000ff),(bd->bi_ip_addr & 0x0000ff00) >> 8, (bd->bi_ip_addr & 0x00ff0000)>> 16, (bd->bi_ip_addr & 0xff000000) >> 24);
?
??? IPaddr_t x =ntohl(bd->bi_ip_addr); //add ?yangzheng
??? char tmp[22];
??? ip_to_string(bd->bi_ip_addr,tmp);
??? printf("HTTP server is starting at IP: %s\n",tmp);
?
??? DBG("file=%s, func=%s, line=%d\n",__FILE__, __FUNCTION__, __LINE__);
?????? HttpdStart();? //這里就會(huì)初始化uip和http
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
?
?????? // set local host ip address
?????? //ip[1] = ((bd->bi_ip_addr &0xFFFF0000) >> 16);
?????? //ip[0] = (bd->bi_ip_addr &0x0000FFFF);
?????? ip[0] = htons(((x& 0xFFFF0000) >> 16));?? //dbgyangzheng
?????? ip[1] = htons((x & 0x0000FFFF));
?
?????? uip_sethostaddr(ip);
?
?????? // set network mask (255.255.255.0 ->local network)
?????? ip[0] = ((0xFFFFFF00 & 0xFFFF0000)>> 16);
?????? ip[1] = (0xFFFFFF00 & 0x0000FFFF);
?????? //ip[0] = htons(0xFFFF); //dbg yangzheng
?????? //ip[1] = htons(0xFF00);
?
?????? uip_setnetmask(ip);
??? ip[0] = 0xFFFF;? //dbg yangzheng
??? ip[1] = 0xFFFF;
??? uip_setdraddr(ip);
?
?????? // should we also set default router ipaddress?
?????? //uip_setdraddr();
?
?????? // show current progress of the process
?????? do_http_progress(WEBFAILSAFE_PROGRESS_START);
?
?????? webfailsafe_is_running = 1;
?
?????? // infinite loop
?????? for (;;) {
?
????????????? // TODO: ??
????????????? WATCHDOG_RESET();
?
????????????? /*
????????????? ?*??? Checkthe ethernet for a new packet.
????????????? ?*??? Theethernet receive routine will process it.
????????????? ?*/
????????????? if (eth_rx() > 0) {
???????????????????? HttpdHandler();
????????????? }
?
????????????? // if CTRL+C was pressed ->return!
????????????? if (ctrlc()) {
???????????????????? eth_halt();
???????????????????? printf("\nWeb failsafemode aborted!\n\n");
???????????????????? return (-1);
????????????? }
?
????????????? // until upload is not completed,get back to the start of the loop
????????????? if(!webfailsafe_ready_for_upgrade) continue;
?
????????????? // stop eth interface
????????????? eth_halt();
?
????????????? // show progress
?????? ???do_http_progress(WEBFAILSAFE_PROGRESS_UPLOAD_READY);
?
????????????? // try to make upgrade!
// try to makeupgrade!
??????? if ( !do_upgrade());??? //這個(gè)函數(shù)后面會(huì)重點(diǎn)說明
??????? {
??????????? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
???????????do_http_progress(WEBFAILSAFE_PROGRESS_UPGRADE_READY);
??????????? do_reset(NULL, 0, 0, NULL);? //這個(gè)函數(shù)是在uboot里面的
??????????? return 0;
??????? }
#if 0
????????????? if(do_http_upgrade(NetBootFileXferSize, webfailsafe_upgrade_type) >= 0) {
???????????????????? do_http_progress(WEBFAILSAFE_PROGRESS_UPGRADE_READY);
???????????????????? udelay(1000 * 10);
???????????????????? do_reset(0, 0, 0, 0);
???????????????????? return 0;
????????????? }
#endif
????????????? break;
?????? }
?
?????? webfailsafe_is_running = 0;
?????? webfailsafe_ready_for_upgrade = 0;
?????? webfailsafe_upgrade_type =WEBFAILSAFE_UPGRADE_TYPE_FIRMWARE;
?
?????? NetBootFileXferSize = 0;
??? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
?
?????? do_http_progress(WEBFAILSAFE_PROGRESS_UPGRADE_FAILED);
?
?????? // go to restart
?????? goto restart;
?
?????? return -1;
}
?
?
?
下面就說一下do_upgrade()函數(shù)。我移植uip協(xié)議棧的目的就是通過這個(gè)協(xié)議來升級(jí)uboot,內(nèi)核,文件系統(tǒng)。這個(gè)函數(shù)就是將通過網(wǎng)頁上傳到buffer的數(shù)據(jù)即bin文件寫到Flash里去,那么具體寫的函數(shù)可以參考u-boot-2010.06/common/cmd_sf.c里面的函數(shù)。(這里解釋一下,上傳的文件時(shí)存放在一個(gè)buffer里面,而不是直接寫到Flash里面,所以需要用下面這個(gè)函數(shù)來寫到Flash)我也是參考uboot里面cmd_sf.c文件,添加了如下代碼:
int do_upgrade (void)
{
???DBG("file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__,__LINE__);
???printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
???printf("+zmodo_upgrade - upgrade kenel ,rootfilesystem,andAPP\n");
???printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
??? int argc =3;
??? char*argv[10];
?
??? argv[0] ="sf";
???argv[1]="probe";
??? argv[2]="0";
???do_spi_flash_probe(argc - 1, argv + 1);
?
??? argc = 4;
??? argv[0] ="sf";
??? argv[1] ="erase";
??? argv[2] ="0";
??? //argv[3]= "80000";
??? argv[3] ="1000000"; ??//所燒錄文件的大小,我的是燒錄的是整個(gè)燒片文件16M
???do_spi_flash_erase(argc - 1, argv + 1);
?
??? argc = 5;
??? argv[0] ="sf";
??? argv[1] ="write";
??? argv[2] ="82000000";
??? argv[3] ="0";
??? //argv[4]= "80000";
??? argv[4] ="1000000";
???do_spi_flash_read_write(argc - 1, argv + 1);
?
??? return 0;
}???????
?
下面三個(gè)函數(shù)我完全是從u-boot-2010.06/common/cmd_sf.c文件里面拷貝過來的,僅供參考:
static int do_spi_flash_probe(int argc, char*argv[])
{
?????? unsignedint bus = 0;
?????? unsignedint cs;
?????? unsignedint speed = CONFIG_SF_DEFAULT_SPEED;
?????? unsignedint mode = CONFIG_SF_DEFAULT_MODE;
?????? char*endp;
?????? structspi_flash *new;
?
?????? if (argc< 2)
????????????? gotousage;
?
?????? cs =simple_strtoul(argv[1], &endp, 0);
?????? if(*argv[1] == 0 || (*endp != 0 && *endp != ':'))
????????????? gotousage;
?????? if(*endp == ':') {
????????????? if(endp[1] == 0)
???????????????????? gotousage;
?
????????????? bus= cs;
????????????? cs= simple_strtoul(endp + 1, &endp, 0);
????????????? if(*endp != 0)
???????????????????? gotousage;
?????? }
?
?????? if (argc>= 3) {
????????????? speed= simple_strtoul(argv[2], &endp, 0);
????????????? if(*argv[2] == 0 || *endp != 0)
???????????????????? gotousage;
?????? }
?????? if (argc>= 4) {
????????????? mode= simple_strtoul(argv[3], &endp, 16);
????????????? if(*argv[3] == 0 || *endp != 0)
???????????????????? gotousage;
?????? }
?
?????? new =spi_flash_probe(bus, cs, speed, mode);
?????? if(!new) {
????????????? printf("Failedto initialize SPI flash at %u:%u\n", bus, cs);
????????????? return1;
?????? }
?
?????? if(flash)
????????????? spi_flash_free(flash);
?????? flash =new;
?
?????? printf("%uKiB %s at %u:%u is now current device\n",
???????????????????? flash->size>> 10, flash->name, bus, cs);
?
?????? return0;
?
usage:
?????? puts("Usage:sf probe [bus:]cs [hz] [mode]\n");
?????? return1;
}
?
static int do_spi_flash_read_write(int argc, char*argv[])
{
?????? unsignedlong addr;
?????? unsignedlong offset;
?????? unsignedlong len;
?????? void*buf;
?????? char*endp;
?????? int ret;
?????? structmtd_info_ex *spiflash_info = get_spiflash_info();
?
?????? if (argc< 4)
????????????? gotousage;
?
?????? addr =simple_strtoul(argv[1], &endp, 16);
?????? if(*argv[1] == 0 || *endp != 0)
????????????? gotousage;
?????? offset =simple_strtoul(argv[2], &endp, 16);
?????? if(*argv[2] == 0 || *endp != 0)
????????????? gotousage;
?????? len =simple_strtoul(argv[3], &endp, 16);
?????? if(*argv[3] == 0 || *endp != 0)
????????????? gotousage;
????????????? if(offset + len >
????????????? spiflash_info->chipsize* spiflash_info->numchips) {
???????????????????? printf(
???????????????????? "ERROR:read/write area is out of range!\n\n");
???????????????????????????????????????????????? return-1;
????????????? }
?
?????? buf =map_physmem(addr, len, MAP_WRBACK);
?????? if(!buf) {
????????????? puts("Failedto map physical memory\n");
????????????? return1;
?????? }
?
?????? if(strcmp(argv[0], "read") == 0)
????????????? ret= spi_flash_read(flash, offset, len, buf);
?????? else {
????????????? unsignedlong write_start, write_len, write_step;
????????????? intpercent_complete = -1;
????????????? char*pbuf = buf;
?
????????????? write_start= offset;
????????????? write_len?? = len;
????????????? write_step? = spiflash_info->erasesize;
?
????????????? while(len > 0) {
???????????????????? if(len < write_step)
??????????????????????????? write_step= len;
?
???????????????????? ret= spi_flash_write(flash, offset, write_step, pbuf);
???????????????????? if(ret)
??????????????????????????? break;
?
???????????????????? offset+= write_step;
???????????????????? pbuf?? += write_step;
???????????????????? len??? -= write_step;
?
???????????????????? do{
??????????????????????????? unsignedlong long n = (unsigned long long)
?????????????????????????????????? (offset- write_start) * 100;
??????????????????????????? intpercent;
?
??????????????????????????? do_div(n,write_len);
??????????????????????????? percent= (int)n;
?
??????????????????????????? /*output progress message only at whole percent
??????????????????????????? ?* steps to reduce the number of messages
??????????????????????????? ?* printed on (slow) serial consoles
??????????????????????????? ?*/
??????????????????????????? if(percent != percent_complete) {
?????????????????????????????????? percent_complete= percent;
?
?????????????????????????????????? printf("\rWritingat 0x%lx -- %3d%% "
????????????????????????????????????????? "complete.",offset, percent);
??????????????????????????? }
???????????????????? }while (0);
????????????? }
?????? }
?????? puts("\n");
?
?????? unmap_physmem(buf,len);
?
?????? if (ret){
????????????? printf("SPIflash %s failed\n", argv[0]);
????????????? return1;
?????? }
?
?????? return0;
?
usage:
?????? printf("Usage:sf %s addr offset len\n", argv[0]);
?????? return1;
}
?
static int do_spi_flash_erase(int argc, char*argv[])
{
?????? unsignedlong offset;
?????? unsignedlong len;
?????? char*endp;
?????? int ret;
?????? structmtd_info_ex *spiflash_info = get_spiflash_info();
?????? unsignedlong erase_start, erase_len, erase_step;
?????? intpercent_complete = -1;
?
?????? if (argc< 3)
????????????? gotousage;
?
?????? offset =simple_strtoul(argv[1], &endp, 16);
?????? if(*argv[1] == 0 || *endp != 0)
????????????? gotousage;
?????? len =simple_strtoul(argv[2], &endp, 16);
?????? if(*argv[2] == 0 || *endp != 0)
????????????? gotousage;
?
?????? if(offset + len > spiflash_info->chipsize * spiflash_info->numchips) {
????????????? printf("ERROR:erase area is out of range!\n\n");
????????????? return1;
?????? }
?
?????? if(offset & (spiflash_info->erasesize-1)) {
????????????? printf("ERROR:erase start address is not block aligned!\n\n");
????????????? return1;
?????? }
?
?????? if (len& (spiflash_info->erasesize-1)) {
????????????? printf("ERROR:erase length is not block aligned!\n\n");
????????????? return1;
?????? }
?
?????? erase_start= offset;
?????? erase_len?? = len;
?????? erase_step? = spiflash_info->erasesize;
?
?????? while(len > 0) {
????????????? if(len < erase_step)
???????????????????? erase_step= len;
?
????????????? ret= spi_flash_erase(flash, offset, erase_step);
????????????? if(ret) {
???????????????????? printf("SPIflash %s failed\n", argv[0]);
???????????????????? return1;
????????????? }
?
????????????? len-= erase_step;
????????????? offset+= erase_step;
?
????????????? do{
???????????????????? unsignedlong long n = (unsigned long long)
??????????????????????????? (offset- erase_start) * 100;
???????????????????? intpercent;
?
???????????????????? do_div(n,erase_len);
???????????????????? percent= (int)n;
?
???????????????????? /*output progress message only at whole percent
???????????????????? ?* steps to reduce the number of messagesprinted
???????????????????? ?* on (slow) serial consoles
???????????????????? ?*/
???????????????????? if(percent != percent_complete) {
??????????????????????????? percent_complete= percent;
?
??????????????????????????? printf("\rErasingat 0x%lx -- %3d%% complete.",
????????????????????????????????????????? offset,percent);
???????????????????? }
????????????? }while (0);
?????? }
?????? puts("\n");
?
?????? return0;
?
usage:
?????? puts("Usage:sf erase offset len\n");
?????? return1;
}
?
當(dāng)數(shù)據(jù)寫到Flash以后,需要重啟單板,所以在net.c里面調(diào)用uboot的重啟函數(shù),do_reset(),這個(gè)函數(shù)在grub模式下執(zhí)行reset的時(shí)候就會(huì)被調(diào)用,現(xiàn)在把它添加到net.c里面,當(dāng)上傳的文件寫到Flash之后就會(huì)自動(dòng)重啟單板:
if ( !do_upgrade() );
{???
???? DBG("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);??????do_http_progress(WEBFAILSAFE_PROGRESS_UPGRADE_READY);
???? do_reset (NULL, 0, 0, NULL);
???? return 0;
}???
?
上面do_http_progress()這個(gè)函數(shù)只是打印文件加載成功或失敗的信息:
int do_http_progress(const int state) {
?
?????? /*toggle LED's here */
?????? switch(state) {
????????????? caseWEBFAILSAFE_PROGRESS_START:
???????????????????? printf("HTTPserver is ready!\n\n");
???????????????????? break;
?
????????????? caseWEBFAILSAFE_PROGRESS_TIMEOUT:
???????????????????? //printf("Waitingfor request...\n");
???????????????????? break;
?
????????????? caseWEBFAILSAFE_PROGRESS_UPLOAD_READY:
???????????????????? printf("HTTPupload is done! Upgrading...\n");
???????????????????? break;
?
????????????? caseWEBFAILSAFE_PROGRESS_UPGRADE_READY:
???????????????????? printf("HTTPugrade is done! Rebooting...\n\n");
???????????????????? break;
?
????????????? caseWEBFAILSAFE_PROGRESS_UPGRADE_FAILED:
???????????????????? printf("##Error: HTTP ugrade failed!\n\n");
???????????????????? //wait 1 sec
???????????????????? milisecdelay(1000);
?
???????????????????? break;
?????? }
?????? return0;
}
?
到這里在net.c里面要添加的函數(shù)基本上就完成了,當(dāng)然還有個(gè)別的宏定義,全局變量等一些定義的話,在編譯的時(shí)候參考出錯(cuò)信息來修改添加就可以了。例如我在文件開頭添加了如下信息:
/*--------------------------add byyangzheng---------------------*/
#include <common.h>
#include <spi_flash.h>
?
#include <asm/io.h>
#include <linux/mtd/mtd.h>
?
#ifndef CONFIG_SF_DEFAULT_SPEED
# define CONFIG_SF_DEFAULT_SPEED? 1000000
#endif
#ifndef CONFIG_SF_DEFAULT_MODE
# define CONFIG_SF_DEFAULT_MODE????????? SPI_MODE_3
#endif
?
#define WEBFAILSAFE_UPGRADE_TYPE_FIRMWARE?????????? 0
#define WEBFAILSAFE_PROGRESS_START????????????????? 0
#define WEBFAILSAFE_PROGRESS_TIMEOUT??????????????? 1
#define WEBFAILSAFE_PROGRESS_UPLOAD_READY?????????? 2
#define WEBFAILSAFE_PROGRESS_UPGRADE_FAILED???????? 4
#define WEBFAILSAFE_PROGRESS_UPGRADE_READY????????? 3
#define ET_DEBUG
?
#define milisecdelay(_x)???? udelay((_x) * 1000)
?
#if 0
#define DBG(x...)?printf(x)
#else
#define DBG(x...)??do { } while (0)
#endif
?
static int arptimer = 0;
int??webfailsafe_is_running = 0;
int??webfailsafe_ready_for_upgrade = 0;
int??webfailsafe_upgrade_type = WEBFAILSAFE_UPGRADE_TYPE_FIRMWARE;
unsigned char *webfailsafe_data_pointer = NULL;
extern int do_reset (cmd_tbl_t *cmdtp, int flag, intargc, char *argv[]);
static struct spi_flash *flash;
/*---------------------------------------------------------------*/
?
小結(jié):uip 的移植大部分時(shí)間就是用在net.c里面,net.c里面需要修改的東西很多,尤其是字節(jié)序的問題也是讓我花了很長(zhǎng)時(shí)間去轉(zhuǎn)換,總之net.c需要修改的大致就這么多,可能還有個(gè)別地方?jīng)]講到,但是那都是小問題,這篇文檔已經(jīng)說明了移植uip協(xié)議的整體思路,那么沒有提及的個(gè)別小問題就靠自己分析了,這樣做移植才能有更多收獲。下面還有一點(diǎn)需要完善,我需要系統(tǒng)啟動(dòng)時(shí),在找不到內(nèi)核的情況下,自動(dòng)調(diào)用httpd服務(wù)程序,然后通過web來升級(jí)我的系統(tǒng),方法如下。
?
2.2.3 在uboot里面添加httpd命令
?????? 首先要知道,uboot里面的那些命令是在哪兒定義的,比如,printenv, tftp, help等。這些命令是在u-boot-2010.06/common的.c文件里面定義的。可以模仿其中的一個(gè)命令來添加我們的httpd命令,我的代碼添加如下:
[kernel@localhost common]$vim cmd_httpd.c
/*********************************************************************************
?*?????Copyright:? (C) 2014 YangZheng<yz2012ww@gmail.com>?
?*?????????????????All rights reserved.
?*
?*??????Filename:? cmd_httpd.c
?*???Description:? This file
?*????????????????
?*???????Version:? 1.0.0(10/09/2014~)
?*????????Author:? Yang Zheng<yz2012ww@gmail.com>
?*?????ChangeLog:? 1, Release initialversion on "10/09/2014 03:48:07 PM"
?*????????????????
?********************************************************************************/
?
#include<common.h>
#include<command.h>
#include <net.h>
?
extern intNetLoopHttpd(void);
int do_httpd(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
??? printf("file=%s, func=%s,line=%d\n", __FILE__, __FUNCTION__, __LINE__);
??? return NetLoopHttpd();? //這里就會(huì)調(diào)用net.c里面的NetLoopHttpd()函數(shù)
}
U_BOOT_CMD(httpd, 1,1, do_httpd, "start www server for firmware recovery\n", NULL);??????????
?
?
然后再添加一個(gè)功能,那就是上面提到的,系統(tǒng)啟動(dòng)找不到內(nèi)核是就調(diào)用httpd服務(wù)程序。在u-boot-2010.06/common/cmd_bootm.c的bootm_start()函數(shù)里面添加NetLoopHttpd():
extern int NetLoopHttpd(void);
static int bootm_start(cmd_tbl_t *cmdtp, int flag,int argc, char *argv[])
{
??? void??????? *os_hdr;
??? int???? ret;
?
??? memset((void *)&images, 0, sizeof (images));
???images.verify = getenv_yesno ("verify");
?
???bootm_start_lmb();
?
??? /* getkernel image header, start address and length */
??? os_hdr =boot_get_kernel (cmdtp, flag, argc, argv,
???????????&images, &images.os.image_start, &images.os.image_len);
?? ?if (images.os.image_len == 0) {
??????? puts("ERROR: can't get kernel image!\n");?? //找不到內(nèi)核時(shí)會(huì)打印這一句信息
??????? puts ("#############start httpdserver!###############\n");
??????? NetLoopHttpd();
??????? return1;
…………
?
2.2.4 展示結(jié)果
?????? 終于到了看結(jié)果的時(shí)候了,有點(diǎn)激動(dòng)。。。。。
首先在單板上運(yùn)行httpd服務(wù),在windows的DOS下,使用命令ping命令:
圖1 ?運(yùn)行httpd
圖2 ping單板
?
使用web升級(jí)系統(tǒng),在瀏覽器地址欄輸入單板ip,就會(huì)看到如下圖所示:
圖3 ?web升級(jí)界面
點(diǎn)擊選擇文件->升級(jí)就開始上傳文件,上傳完以后就會(huì)自動(dòng)寫到Flash了,這里就不再演示,不然我又得再次升級(jí)。。。
?
3、總結(jié)
3.1 uip協(xié)議關(guān)鍵庫函數(shù)的功能及使用方法
?????? 應(yīng)用程序必須作為C函數(shù)去實(shí)現(xiàn),uIP在任何一個(gè)事件發(fā)生時(shí)調(diào)用UIP_APPCALL()。表 1 列出了可能的事件和每個(gè)事件的對(duì)應(yīng)測(cè)試函數(shù),測(cè)試函數(shù)用于區(qū)別不同的事件。函數(shù)是作為C宏命令實(shí)現(xiàn)的,將會(huì)是零或非零值。注意,某些函數(shù)可以在互相連接時(shí)發(fā)生(即新數(shù)據(jù)可以在數(shù)據(jù)確應(yīng)的同時(shí)到達(dá))。
表 1? uIP應(yīng)用事件和對(duì)應(yīng)的測(cè)試參數(shù)
| 一個(gè)數(shù)據(jù)包到達(dá),確定先前發(fā)送到數(shù)據(jù) | uip_acked() |
| 應(yīng)用程序的新數(shù)據(jù)包已到達(dá) | uip_newdata() |
| 一個(gè)遠(yuǎn)程主機(jī)連接到監(jiān)聽端口 | uip_connected() |
| 一個(gè)到達(dá)遠(yuǎn)程主機(jī)的連接建立成功 | uip_connected() |
| 計(jì)時(shí)時(shí)間滿重發(fā) | uip_rexmit() |
| 計(jì)時(shí)時(shí)間滿周期性輪詢 | uip_poll() |
| 遠(yuǎn)程主機(jī)關(guān)閉連接 | uip_closed() |
| 遠(yuǎn)程主機(jī)中斷連接 | uip_aborted() |
| 由于太多重傳,連接中斷 | uip_timedout() |
?
當(dāng)應(yīng)用程序調(diào)用時(shí),uIP設(shè)置全局變量uip_conn去指向當(dāng)前連接的uip_conn結(jié)構(gòu),這可以用于區(qū)別不同的服務(wù)。一個(gè)典型的應(yīng)用是檢查uip_conn->lport (當(dāng)?shù)豑CP端口號(hào))去決定哪個(gè)服務(wù)連接應(yīng)該提供。例如,如果值uip_conn->lport等于80,應(yīng)用程序可以決定啟動(dòng)一個(gè)HTTP服務(wù);若值是23,則是啟動(dòng)TELNET服務(wù)。
?
3.1.1 接收數(shù)據(jù)
如果uIP測(cè)試函數(shù)uip_newdata()的值為1,則遠(yuǎn)程連接的主機(jī)有發(fā)送新數(shù)據(jù),uip_appdata指針指向?qū)嶋H數(shù)據(jù),數(shù)據(jù)的大小通過uIP函數(shù)uip_datalen()獲得。在數(shù)據(jù)不是被緩沖后,應(yīng)用程序必須立刻啟動(dòng)。
?
3.1.2 發(fā)送數(shù)據(jù)
應(yīng)用程序通過使用uIP函數(shù)uip_send()發(fā)送數(shù)據(jù)。uip_send()函數(shù)采用兩個(gè)參數(shù):一個(gè)指針指向發(fā)送數(shù)據(jù)和數(shù)據(jù)的長(zhǎng)度。如果應(yīng)用程序?yàn)榱水a(chǎn)生要發(fā)送的實(shí)際數(shù)據(jù)需要RAM空間,包緩存(通過uip_appdata指針指向)可以用于這方面。在一個(gè)時(shí)間里應(yīng)用程序只能在連接中發(fā)送一塊數(shù)據(jù),所以不可以在每個(gè)應(yīng)用程序啟用中調(diào)用uip_send()超過一次,只有上一次調(diào)用的數(shù)據(jù)將會(huì)發(fā)出后才可以。注意,調(diào)用uip_send()后會(huì)改變某些全局變量,在應(yīng)用函數(shù)返回前它不能被調(diào)用。
?
3.1.3 重發(fā)數(shù)據(jù)
若數(shù)據(jù)在網(wǎng)絡(luò)中丟失,則應(yīng)用程序必須重新發(fā)數(shù)據(jù)。無論是數(shù)據(jù)收到還是沒有收到,uIP都保并通知應(yīng)用程序什么時(shí)候察覺出數(shù)據(jù)丟失了。若測(cè)試函數(shù)uip_rexmit()為真,則應(yīng)用程序要重持跟蹤,發(fā)上一次發(fā)出的數(shù)據(jù)。重發(fā)就好像原來那樣發(fā)送,也就是通過uip_send()發(fā)送。
?
3.1.4 關(guān)閉連接
應(yīng)用程序通過調(diào)用uip_close()關(guān)閉當(dāng)前連接,這會(huì)導(dǎo)致連接干凈地關(guān)閉。為了指出致命的錯(cuò)誤,應(yīng)用程序可以通過中止連接和調(diào)用uip_abort()函數(shù)完成這項(xiàng)工作。若連接已經(jīng)被遠(yuǎn)端關(guān)閉,則測(cè)試函數(shù)uip_closed()為真,應(yīng)用程序接著可以做一些必要的清理工作。
?
?
3.1.5 報(bào)告出錯(cuò)
有兩個(gè)致命的錯(cuò)誤可以發(fā)生在連接中:連接由遠(yuǎn)程主機(jī)中止和連接多次重發(fā)上一數(shù)據(jù)而被中止。uIP通過調(diào)用函數(shù)報(bào)告這些問題,應(yīng)用程序使用兩個(gè)測(cè)試函數(shù)uip_aborted()和uip_timedout() 去測(cè)試這些錯(cuò)誤情況。
?
?
3.1.6 輪詢
當(dāng)連接空閑時(shí),uIP周期性地輪詢應(yīng)用程序,應(yīng)用程序使用測(cè)試函數(shù)uip_poll()去檢查它是否被輪詢過。
?
3.1.7 監(jiān)聽端口
uIP維持一個(gè)監(jiān)聽TCP端口列表,通過uip_listen()函數(shù),一個(gè)新的監(jiān)聽端口被打開。當(dāng)一個(gè)連接請(qǐng)求在一個(gè)監(jiān)聽端口到達(dá),uIP產(chǎn)生一個(gè)新的連接。若一個(gè)新連接產(chǎn)生,則應(yīng)用程序被調(diào)用,測(cè)試函數(shù)uip_connected()為真。
?
3.1.8 打開連接
作為uIP的0.6版,在uIP里面通過使用uip_connect()函數(shù)打開一個(gè)新連接。這個(gè)函數(shù)打開一個(gè)新連接到指定的IP地址和端口,返回一個(gè)新連接的指針到uip_conn結(jié)構(gòu)。若是沒有空余的連接槽,則函數(shù)返回空值。為了方便,函數(shù)uip_ipaddr()可以用于將IP地址打包進(jìn)兩個(gè)單元16位數(shù)組里,通過uIP去代表IP地址。
接下來用兩個(gè)例子說明。第一個(gè)例子展示了怎樣打開一個(gè)連接去遠(yuǎn)端TCP端口8080。若沒有足夠的TCP連接插槽去允許一個(gè)新連接打開,則uip_connect()函數(shù)返回NULL并通過uip_abort()中止當(dāng)前連接。第二個(gè)例子展示怎樣打開一個(gè)新連接去指定的IP地址。
例1:打開一個(gè)連接去遠(yuǎn)端TCP端口8080。
void connect_example1_app(void)
{
if(uip_connect(uip_conn->ripaddr,8080) == NULL)
?{
uip_abort();
}
}
例2:打開一個(gè)連接去當(dāng)前連接的遠(yuǎn)端的端口8080。
void connect_example2(void)
?{
u16_t? ipaddr[2];
uip_ipaddr(ipaddr,192,168,0,1);
uip_connect(ipaddr,8080);
}
?
3.1.8 數(shù)據(jù)流控制
uIP通過函數(shù)uip_stop()和函數(shù)uip_restart()提供對(duì)存取TCP數(shù)據(jù)流的控制途徑。假如一個(gè)應(yīng)用程序下載數(shù)據(jù)到一個(gè)慢速設(shè)備,例如磁盤驅(qū)動(dòng)器。當(dāng)磁盤驅(qū)動(dòng)器的作業(yè)隊(duì)列滿時(shí),應(yīng)用程序不會(huì)準(zhǔn)備從服務(wù)器接收更多的數(shù)據(jù),直到隊(duì)列排出空位。函數(shù)uip_stop()可以用于維護(hù)流控制和停止遠(yuǎn)程主機(jī)發(fā)送數(shù)據(jù)。當(dāng)應(yīng)用程序準(zhǔn)備好接收更多數(shù)據(jù)時(shí),可用函數(shù)uip_restart()告知遠(yuǎn)程終端再次發(fā)送數(shù)據(jù)。函數(shù)uip_stopped()可以用于檢查當(dāng)前連接是否停止。
?
?
3.1.9 UIP函數(shù)總結(jié)
表2 包含了所有uIP提供的函數(shù)
表2? uIP 函數(shù)總結(jié)
| 系統(tǒng)接口 | ? |
| uip_init() uip_input() uip_periodic() | 初始化uIP 處理輸入包 處理周期計(jì)時(shí)事件 |
| 應(yīng)用程序接口 | ? |
| uip_listen() uip_connect() uip_send() uip_datalen() uip_close() uip_abort() uip_stop() uip_stopped() uip_restart() | 開始監(jiān)聽端口 連接到遠(yuǎn)程主機(jī) 在當(dāng)前連接發(fā)送數(shù)據(jù) 輸入數(shù)據(jù)的大小 關(guān)閉當(dāng)前連接 中止當(dāng)前連接 停止當(dāng)前連接 查找連接是否停止 重新啟動(dòng)當(dāng)前連接 |
| 測(cè)試函數(shù) | ? |
| uip_newdata() uip_acked() uip_connected() uip_closed() uip_aborted() uip_timeout() uip_rexmit uip_poll() | 遠(yuǎn)程主機(jī)已經(jīng)發(fā)出數(shù)據(jù) 確定發(fā)出的數(shù)據(jù) 當(dāng)前連接剛連上 當(dāng)前連接剛關(guān)閉 當(dāng)前連接剛中止 當(dāng)前連接剛超時(shí) 數(shù)據(jù)重發(fā) 應(yīng)用程序循環(huán)運(yùn)行 |
| 其它 | ? |
| uip_mss() uip_ipaddr() htons(),ntohs() | 獲得當(dāng)前連接的最大段的大小 將IP地址結(jié)構(gòu)打包 在主機(jī)和網(wǎng)絡(luò)之間轉(zhuǎn)換字節(jié)次序 |
?
上面的這些都是引用網(wǎng)上別人博客里面的內(nèi)容,總結(jié)的也很全面。
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的UIP协议栈移植到u-boot详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 人脸检测代码,在Andr
- 下一篇: 浅谈人工智能与医疗