#ifndef __UDPDEMO_H__
#define __UDPDEMO_H__
//定義應用程式回調函數
#ifndef UIP_APPCALL
#define UIP_APPCALL uip_appcall
#endif
#ifndef UIP_UDP_APPCALL
#define UIP_UDP_APPCALL myudp_appcall
#endif
#endif
#include "stdio.h"
#include "stm32f10x.h"
#include "uip.h"
#include "udpdemo.h"
struct uip_udp_conn *myudp_conn;
void myudp_init(void)
{
uip_ipaddr_t ipaddr;//定義IP類型變量
uip_ipaddr(ipaddr, 10,24,1,188); //遠端IP為192.168.1.116
if(myudp_conn != NULL)
{
uip_udp_remove(myudp_conn);//如果連接配接已經建立,則删除之
}
myudp_conn = uip_udp_new(&ipaddr, HTONS(1000));//建立到遠端ipaddr,端口為1000的連接配接
if(myudp_conn != NULL)
{
uip_udp_bind(myudp_conn, HTONS(2000));//綁定本地端口為2000,也就是2000-->1000 發資料
}
}
void myudp_send(char *str,short n)
{
char *nptr;
nptr = (char *)uip_appdata;
memcpy(nptr, str, n);
uip_udp_send(n); //發送n個資料
}
void newdata()
{
char *nptr;
short len;
len = uip_datalen();//讀取資料長度
nptr = (char *)uip_appdata; //取得資料起始指針
if(len<4){
myudp_send("Please check the command!\n",26);
}else if(strncmp(nptr,"getname",7)==0){
myudp_send("My name is Johnny.\n",19);
}else {
myudp_send("Unkown command!\n",16);
}
}
/*---------------------------------------------------------------------------*/
/** \internal
* The main UDP function.
*/
/*---------------------------------------------------------------------------*/
void myudp_appcall(void)
{
if(myudp_conn->rport == HTONS(1000))//這個地方可能欠妥,不是檢查的myudp的端口,
//而是檢查uip_udp_conn->rport,這個uip_udp_conn是全局變量
{
/*if(uip_poll()) {
myudp_send("hello\n",6);//定時時間到,發hello
}*/
if(uip_newdata()) //如果指定IP的指定端口發來資料
{
newdata(); //這個裡面就是處理接收到的資料的
}
}
}
void uip_appcall(void){
}
//最後需要定義一個宏
//#define UIP_UDP_APPCALL myudp_appcall
//放在uip.h
//列印日志用
void uip_log(char *m)
{
printf("uIP log:%s\r\n",m);
}