//net读
//api/api.c
static cfp_t calls_table[API_MAXCALL] = { NULL, };
void api_init(void)
{
…
calls_table[API_RSVD] = NULL;
calls_table[API_GETC] = &API_getc;
calls_table[API_PUTC] = &API_putc;
calls_table[API_TSTC] = &API_tstc;
calls_table[API_PUTS] = &API_puts;
calls_table[API_RESET] = &API_reset;
calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
calls_table[API_UDELAY] = &API_udelay;
calls_table[API_GET_TIMER] = &API_get_TImer;
calls_table[API_DEV_ENUM] = &API_dev_enum;
calls_table[API_DEV_OPEN] = &API_dev_open;
calls_table[API_DEV_CLOSE] = &API_dev_close;
calls_table[API_DEV_READ] = &API_dev_read;
calls_table[API_DEV_WRITE] = &API_dev_write;
calls_table[API_ENV_GET] = &API_env_get;
calls_table[API_ENV_SET] = &API_env_set;
calls_table[API_ENV_ENUM] = &API_env_enum;
calls_no = API_MAXCALL;
…
}
staTIc int API_dev_read(va_list ap)
{
…
else if (di->type & DEV_TYP_NET)
{
len_net = (int *)va_arg(ap, u_int32_t);
if (!len_net)
return API_EINVAL;
if (*len_net <= 0)
return API_EINVAL;
act_len_net = (int *)va_arg(ap, u_int32_t);
if (!act_len_net)
return API_EINVAL;
*act_len_net = dev_read_net(di->cookie, buf, *len_net);
}
…
}
//api/api_net.c
int dev_read_net(void *cookie, void *buf, int len)
{
return eth_receive(buf, len);
}
//net/eth.c
int eth_receive(volaTIle void *packet, int length)
{
if (eth_rcv_current == eth_rcv_last)
{
…
push_packet = eth_save_packet;
eth_rx();
push_packet = pp;
if (eth_rcv_current == eth_rcv_last)
return -1;
…
}
//drivers\net\higmacv300\Higmac.c
int eth_rx(void)
{
…
NetReceive(NetRxPackets[0], len);
…
}
//net/net.c
void NetReceive(volatile uchar * inpkt, int len)
{
…
x = ntohs(vet->vet_type);
…
switch (x)
{
case PROT_ARP:
…
switch (ntohs(arp->ar_op))
{
…
case ARPOP_REQUEST:
…
case ARPOP_REPLY:
…
tmp = NetReadIP(&arp->ar_data[6]);
if (tmp == NetArpWaitReplyIP)
{
…
(*packetHandler)(0,0,0,0);
…
}
case PROT_RARP:
…
case PROT_IP:
…
if (ip->ip_p == IPPROTO_ICMP)
{
…
switch (icmph->type)
{
case ICMP_REDIRECT:
…
case ICMP_ECHO_REPLY:
…
case ICMP_ECHO_REQUEST:
…
}
}
}
}
}
static rxhand_f *packetHandler; /* Current RX packet handler */
void NetSetHandler(rxhand_f * f)
{
packetHandler = f;
}
//net/net.h
typedef void rxhand_f(uchar *, unsigned, unsigned, unsigned);
举例tftp通讯
//net/tftp.c
void TftpStart (void)
{
…
NetSetHandler (TftpHandler);
…
}
static void TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
{
…
s = (ushort *)pkt;
proto = *s++;
…
switch (ntohs(proto))
{
case TFTP_RRQ:
case TFTP_WRQ:
break;
case TFTP_ACK:
}
}
好记忆不如烂笔头