Add in basic uIP files into the A2F demo. Ping is working, but nothing else has been tested yet. This check in includes some modifications to the MAC driver to make the Rx zero copy.
parent
521d995b9d
commit
c0bf3cd7f9
@ -0,0 +1,28 @@
|
||||
/*****************************************************************************
|
||||
* (c) Copyright 2009 Actel Corporation. All rights reserved.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Author : Actel Application Team
|
||||
* Rev : 1.0.0.0
|
||||
* Description: Configuration for the ON-BOARD peripherals for SmartFusion KITS.
|
||||
*
|
||||
*******************************************************************************/
|
||||
#ifndef BSP_CONFIG_H_
|
||||
#define BSP_CONFIG_H_
|
||||
|
||||
#include "i2c.h"
|
||||
/* Configuration for OLED */
|
||||
#define OLED_I2C_INSTANCE &g_mss_i2c0
|
||||
|
||||
/* Configuration for the SPI Flash */
|
||||
#define SPI_FLASH_ON_SF_DEV_KIT 0
|
||||
#define SPI_FLASH_ON_SF_EVAL_KIT 1
|
||||
|
||||
#define USE_DMA_FOR_SPI_FLASH 1
|
||||
#define SPI_FLASH_DMA_CHANNEL 0
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -1,233 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* (c) Copyright 2009 SLS Corporation,All Rights Reserved.
|
||||
*
|
||||
* tcpip.h:header file for TCP/IP implementation.
|
||||
* Version Author Comment
|
||||
* 1.0.0 SLS corp. First release
|
||||
*/
|
||||
/***************************************************************************//**
|
||||
* This header file contains the definition and datastructures for the TCP/IP
|
||||
* stack implementation.
|
||||
*/
|
||||
#ifndef _NETTYPE_H_
|
||||
#define _NETTYPE_H_
|
||||
|
||||
#define BUF_LEN 1500
|
||||
|
||||
/* IP header length in terms of 16-bit words */
|
||||
#define IP_HDR_LEN 10
|
||||
#define IP_HDR_CSUM_OFFSET 5
|
||||
#define TCP_HDR_CSUM_OFFSET 8
|
||||
|
||||
#define ETH_ADDR_LEN 6
|
||||
#define IP_ADDR_LEN 4
|
||||
#define ETH_TYPE_LEN 2
|
||||
#define ARP_HW_TYPE_LEN 2
|
||||
#define ARP_PROTO_TYPE_LEN 2
|
||||
#define ARP_OPCODE_LEN 2
|
||||
|
||||
#define ETH_TYPE_0 0x08 /* both IP and ARP have 08 as the first byte */
|
||||
#define ETH_TYPE_ARP_1 0x06 /* 0806 is ARP */
|
||||
#define ETH_TYPE_IP_1 0x00 /* 0800 is IP */
|
||||
|
||||
#define ARP_HW_TYPE_0 0x00 /* 0001 for ethernet */
|
||||
#define ARP_HW_TYPE_1 0x01 /* 0001 for ethernet */
|
||||
|
||||
#define ARP_PROTO_TYPE_0 0x08 /* 0800 is IP */
|
||||
#define ARP_PROTO_TYPE_1 0x00
|
||||
|
||||
#define ARP_OPCODE_0 0x00 /* same for req and reply */
|
||||
#define ARP_OPCODE_REQ_1 0x01 /* 0001 is Request */
|
||||
#define ARP_OPCODE_REPLY_1 0x02 /* 0002 is Reply */
|
||||
|
||||
extern unsigned char my_IP_address[IP_ADDR_LEN];
|
||||
|
||||
typedef struct ether_hdr {
|
||||
unsigned char da[ETH_ADDR_LEN]; /* destination MAC address */
|
||||
unsigned char sa[ETH_ADDR_LEN]; /* source MAC address */
|
||||
unsigned char type_code[ETH_TYPE_LEN]; /* type code */
|
||||
} ether_hdr_t;
|
||||
|
||||
typedef struct arp_pkt {
|
||||
unsigned char hw_type[ARP_HW_TYPE_LEN]; /* Hardware Type */
|
||||
unsigned char proto_type[ARP_PROTO_TYPE_LEN]; /* Protocol Type */
|
||||
unsigned char hw_addr_len; /* Hardware Address Length */
|
||||
unsigned char proto_addr_len; /* Protocol Address Length */
|
||||
unsigned char opcode[ARP_OPCODE_LEN]; /* Opcode */
|
||||
unsigned char mac_sa[ETH_ADDR_LEN]; /* sender MAC address */
|
||||
unsigned char ip_sa[IP_ADDR_LEN]; /* sender IP address */
|
||||
unsigned char mac_ta[ETH_ADDR_LEN]; /* target MAC address */
|
||||
unsigned char ip_ta[IP_ADDR_LEN]; /* target IP address */
|
||||
} arp_pkt_t;
|
||||
|
||||
#define ICMP_PROTO 0x01
|
||||
#define TCP_PROTO 0x06
|
||||
#define UDP_PROTO 0x11
|
||||
|
||||
#define IP_CSUM_LEN 2
|
||||
#define IP_ID_LEN 2
|
||||
#define IP_TLEN_LEN 2
|
||||
#define IP_FRAG_OFF_LEN 2
|
||||
|
||||
typedef struct ip_hdr {
|
||||
unsigned char ver_hlen; /* version - 4 bits; IP hdr len - 4 bits */
|
||||
unsigned char tos; /* Type of service */
|
||||
unsigned char tlen[IP_TLEN_LEN]; /* Size of datagram (header + data) */
|
||||
unsigned char id[IP_ID_LEN]; /* together with sa, uniequly identifies pkt */
|
||||
unsigned char frag_off[IP_FRAG_OFF_LEN]; /* flags - 3 bits; fragment offset - 13 bits */
|
||||
unsigned char ttl; /* time to live */
|
||||
unsigned char proto; /* protocol */
|
||||
unsigned char csum[IP_CSUM_LEN]; /* header checksum */
|
||||
unsigned char sa[IP_ADDR_LEN]; /* IP source address */
|
||||
unsigned char da[IP_ADDR_LEN]; /* IP dest address */
|
||||
} ip_hdr_t;
|
||||
|
||||
|
||||
#define ICMP_TYPE_ECHO_REQUEST 8
|
||||
#define ICMP_TYPE_ECHO_REPLY 0
|
||||
|
||||
typedef struct icmp_hdr {
|
||||
unsigned char type;
|
||||
unsigned char icode;
|
||||
unsigned char csum[IP_CSUM_LEN];
|
||||
} icmp_hdr_t;
|
||||
|
||||
#define TCP_PORT_LEN 2
|
||||
#define TCP_SEQ_LEN 4
|
||||
#define TCP_WSIZE_LEN 2
|
||||
#define TCP_UPTR_LEN 2
|
||||
#define TCP_CSUM_LEN 2
|
||||
#define TCP_PLEN_LEN 2
|
||||
|
||||
typedef struct tcp_hdr {
|
||||
unsigned char sp[TCP_PORT_LEN]; /* Source port */
|
||||
unsigned char dp[TCP_PORT_LEN]; /* Destination port */
|
||||
unsigned char seqnum[TCP_SEQ_LEN]; /* Sequence number */
|
||||
unsigned char acknum[TCP_SEQ_LEN]; /* Acknowledgement number */
|
||||
unsigned char data_off; /* Data Offset - 4 upper bits are valid */
|
||||
unsigned char urg_ack_psh_rst_syn_fin; /* 6 lower bits are valid */
|
||||
unsigned char wsize[TCP_WSIZE_LEN]; /* Window */
|
||||
unsigned char csum[TCP_CSUM_LEN]; /* Chekcsum */
|
||||
unsigned char uptr[TCP_UPTR_LEN]; /* Urgent pointer */
|
||||
} tcp_hdr_t;
|
||||
|
||||
#define UDP_LEN_LEN 2
|
||||
#define UDP_CSUM_LEN 2
|
||||
|
||||
typedef struct udp_hdr {
|
||||
unsigned char sp[TCP_PORT_LEN]; /* Source port */
|
||||
unsigned char dp[TCP_PORT_LEN]; /* Destination port */
|
||||
unsigned char len[UDP_LEN_LEN]; /* length of packet */
|
||||
unsigned char csum[UDP_CSUM_LEN]; /* checksum */
|
||||
} udp_hdr_t;
|
||||
|
||||
#define BOOTP_OPTCODE_DHCP_SUBNET 1 /* Subnet mask */
|
||||
#define BOOTP_OPTCODE_DHCP_ROUTER 3 /* Router*/
|
||||
#define BOOTP_OPTCODE_DHCP_DOMAIN 6 /* Domain */
|
||||
#define BOOTP_OPTCODE_DHCP_LEASE 51 /* Lease time*/
|
||||
#define BOOTP_OPTCODE_DHCP_TYPE 53 /* 53, 1, DHCP_TYPE_* */
|
||||
#define BOOTP_OPTCODE_DHCP_SID 54 /* 54, 4, a.b.c.d, Server ID */
|
||||
#define BOOTP_OPTCODE_DHCP_RENEW 58 /* Renewal time */
|
||||
#define BOOTP_OPTCODE_DHCP_REBIND 59 /* Rebinding time */
|
||||
|
||||
#define BOOTP_OPTCODE_END 255 /* last in options */
|
||||
|
||||
#define DHCP_TYPE_DISCOVER 1
|
||||
#define DHCP_TYPE_OFFER 2
|
||||
#define DHCP_TYPE_REQUEST 3
|
||||
#define DHCP_TYPE_DECLINE 4
|
||||
#define DHCP_TYPE_ACK 5
|
||||
#define DHCP_TYPE_NAK 6
|
||||
#define DHCP_TYPE_RELEASE 7
|
||||
|
||||
|
||||
#define BOOTP_OP_REQUEST 1
|
||||
#define BOOTP_OP_REPLY 2
|
||||
#define BOOTP_HWTYPE_ETH 1
|
||||
#define BOOTP_XID_LEN 4
|
||||
#define BOOTP_SEC_LEN 2
|
||||
#define BOOTP_CHLEN 16
|
||||
#define BOOTP_SN_LEN 64
|
||||
#define BOOTP_FL_LEN 128
|
||||
#define BOOTP_VEN_LEN 64
|
||||
|
||||
#define BOOTP_CLIENT_PORT 68
|
||||
#define BOOTP_SERVER_PORT 67
|
||||
|
||||
typedef struct bootp_pkt {
|
||||
unsigned char op; /* packet op code */
|
||||
unsigned char hwtype; /* hardware type */
|
||||
unsigned char hlen; /* hardware address length */
|
||||
unsigned char hops; /* client sets to zero */
|
||||
unsigned char xid[BOOTP_XID_LEN]; /* transaction ID, random */
|
||||
unsigned char secs[BOOTP_SEC_LEN]; /* seconds since boot */
|
||||
unsigned char flags[2]; /* flags */
|
||||
unsigned char ciaddr[IP_ADDR_LEN]; /* client IP ADDR */
|
||||
unsigned char yiaddr[IP_ADDR_LEN]; /* Your IP Addr */
|
||||
unsigned char siaddr[IP_ADDR_LEN]; /* Server IP ADDR */
|
||||
unsigned char giaddr[IP_ADDR_LEN]; /* Gateway IP ADDR */
|
||||
unsigned char chaddr[BOOTP_CHLEN]; /* Client Hardware Addr */
|
||||
unsigned char sname[BOOTP_SN_LEN]; /* Server Name */
|
||||
unsigned char file[BOOTP_FL_LEN]; /* File Path */
|
||||
unsigned char vend[BOOTP_VEN_LEN]; /* Vendor Data */
|
||||
} bootp_pkt_t;
|
||||
|
||||
typedef struct tcp_pseudo_hdr {
|
||||
unsigned char sa[IP_ADDR_LEN];
|
||||
unsigned char da[IP_ADDR_LEN];
|
||||
unsigned char zero;
|
||||
unsigned char proto;
|
||||
unsigned char plen[TCP_PLEN_LEN];
|
||||
} tcp_pseudo_hdr_t;
|
||||
|
||||
typedef enum tcp_state_e {
|
||||
TCP_STATE_LISTEN = 0,
|
||||
TCP_STATE_SYN_RECVD,
|
||||
TCP_STATE_ESTABLISHED,
|
||||
TCP_STATE_LAST_ACK,
|
||||
TCP_STATE_MY_LAST,
|
||||
TCP_STATE_CLOSED
|
||||
} tcp_state_t;
|
||||
|
||||
typedef struct tcp_control_block {
|
||||
unsigned char local_port[TCP_PORT_LEN];
|
||||
unsigned char remote_port[TCP_PORT_LEN];
|
||||
unsigned char remote_addr[IP_ADDR_LEN];
|
||||
tcp_state_t state;
|
||||
unsigned int local_seq;
|
||||
unsigned int remote_seq;
|
||||
unsigned char remote_mac[ETH_ADDR_LEN]; /* this really doesn't belong here */
|
||||
//<CJ>:
|
||||
const uint8_t * tx_block_addr;
|
||||
unsigned short int tx_block_size;
|
||||
unsigned short int tx_block_idx;
|
||||
uint8_t * tcp_packet;
|
||||
} tcp_control_block_t;
|
||||
|
||||
|
||||
typedef enum tcp_cntrol_flags_e {
|
||||
TCP_CNTRL_FIN = 0x01,
|
||||
TCP_CNTRL_SYN = 0x02,
|
||||
TCP_CNTRL_RST = 0x04,
|
||||
TCP_CNTRL_PSH = 0x08,
|
||||
TCP_CNTRL_ACK = 0x10,
|
||||
TCP_CNTRL_URG = 0x20
|
||||
} tcp_control_flags_t;
|
||||
|
||||
|
||||
typedef struct arp_entry {
|
||||
unsigned char mac[ETH_ADDR_LEN]; /* MAC address */
|
||||
unsigned char ip[IP_ADDR_LEN]; /* IP address */
|
||||
unsigned char used; /* Is this entry used? */
|
||||
} arp_entry_t;
|
||||
|
||||
typedef arp_pkt_t *arp_pkt_xp;
|
||||
typedef ether_hdr_t *eth_hdr_xp;
|
||||
typedef ip_hdr_t *ip_hdr_xp;
|
||||
typedef icmp_hdr_t *icmp_hdr_xp;
|
||||
typedef udp_hdr_t *udp_hdr_xp;
|
||||
typedef tcp_hdr_t *tcp_hdr_xp;
|
||||
typedef tcp_pseudo_hdr_t *tcp_pseudo_hdr_xp;
|
||||
typedef bootp_pkt_t *bootp_pkt_xp;
|
||||
|
||||
#endif /* _NETTYPE_H_ */
|
@ -1,748 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* (c) Copyright 2009 Actel Corporation,All Rights Reserved.
|
||||
*
|
||||
* tcpip.c:TCP/IP implementation for webserver.
|
||||
*/
|
||||
#include "../port_config/cpu_types.h"
|
||||
#include "nettype.h"
|
||||
#include "../mss_ethernet_mac/mss_ethernet_mac.h"
|
||||
#include "../mss_ethernet_mac/mss_ethernet_mac_regs.h"
|
||||
#include "tcpip.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#define OK 0
|
||||
#define ERR 1
|
||||
#define MAC_BASE_ADDRESS 0x40003000
|
||||
|
||||
|
||||
extern char ethAddr[6];
|
||||
|
||||
unsigned char my_ip[IP_ADDR_LEN]={192,168,0,14};
|
||||
unsigned char my_mac[ETH_ADDR_LEN]={0xAA,0xBB,0xCC,0x11,0x22,0x33};
|
||||
unsigned char tcp_packet[1532];
|
||||
unsigned char ip_known;
|
||||
unsigned char dhcp_ip_found;
|
||||
unsigned short int ip_id;
|
||||
unsigned char selected_mode = 0;
|
||||
unsigned char selectedwaveform = 0;
|
||||
unsigned char rtc_count[5]={0,0,0,0,0};
|
||||
unsigned char rtc_match_count[5]={0,5,0,0,0};
|
||||
unsigned char get_count[5];
|
||||
unsigned int num_pkt_tx = 0,num_pkt_rx = 0;
|
||||
#define TCP_START_SEQ 0x10203040
|
||||
static const unsigned char g_client_ip[IP_ADDR_LEN] = { 192, 168, 1, 10 };
|
||||
unsigned char oled_string[20];
|
||||
tcp_control_block_t tcb;
|
||||
MAC_instance_t g_mac;
|
||||
|
||||
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char send_arp_reply(unsigned char *buf)
|
||||
{
|
||||
/* Modify the packet in place */
|
||||
|
||||
arp_pkt_xp arp_pkt = (arp_pkt_xp )(buf + sizeof(ether_hdr_t));
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) buf;
|
||||
|
||||
memcpy(eth_hdr->da, eth_hdr->sa, ETH_ADDR_LEN);
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
arp_pkt->opcode[1] = ARP_OPCODE_REPLY_1;
|
||||
memcpy(arp_pkt->mac_ta, arp_pkt->mac_sa, ETH_ADDR_LEN);
|
||||
memcpy(arp_pkt->ip_ta, arp_pkt->ip_sa, IP_ADDR_LEN);
|
||||
memcpy(arp_pkt->mac_sa, my_mac, ETH_ADDR_LEN);
|
||||
memcpy(arp_pkt->ip_sa, my_ip, IP_ADDR_LEN);
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(buf,42, MSS_MAC_BLOCKING);
|
||||
return OK;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
void send_gratuitous_arp(unsigned char *buf)
|
||||
{
|
||||
arp_pkt_xp arp_pkt = (arp_pkt_xp )(buf + sizeof(ether_hdr_t));
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) buf;
|
||||
memset(eth_hdr->da, 0xFF, ETH_ADDR_LEN); /* broadcast */
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
eth_hdr->type_code[0] = ETH_TYPE_0;
|
||||
eth_hdr->type_code[1] = ETH_TYPE_ARP_1;
|
||||
arp_pkt->hw_type[0] = ARP_HW_TYPE_0;
|
||||
arp_pkt->hw_type[1] = ARP_HW_TYPE_1;
|
||||
arp_pkt->proto_type[0] = ETH_TYPE_0;
|
||||
arp_pkt->proto_type[1] = ETH_TYPE_IP_1;
|
||||
arp_pkt->hw_addr_len = ETH_ADDR_LEN;
|
||||
arp_pkt->proto_addr_len = IP_ADDR_LEN;
|
||||
arp_pkt->opcode[0] = ARP_OPCODE_0;
|
||||
arp_pkt->opcode[1] = ARP_OPCODE_REQ_1;
|
||||
memcpy(arp_pkt->mac_sa, my_mac, ETH_ADDR_LEN);
|
||||
memcpy(arp_pkt->ip_sa, my_ip, IP_ADDR_LEN);
|
||||
memset(arp_pkt->mac_ta, 0x00, ETH_ADDR_LEN);
|
||||
memcpy(arp_pkt->ip_ta, my_ip, IP_ADDR_LEN);
|
||||
//mac_tx_send(buf,42,0);
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(buf,42, MSS_MAC_BLOCKING);
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*
|
||||
*/
|
||||
unsigned short int get_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos)
|
||||
{
|
||||
unsigned int sum; /* our accumulated sum */
|
||||
unsigned short int delta; /* the next 16-bit quantity to add */
|
||||
unsigned short int i;
|
||||
unsigned short int ilen;
|
||||
sum = (unsigned int) 0;
|
||||
ilen=(len&1)?len-1:len;
|
||||
for (i = 0; i < ilen; i += 2) {
|
||||
if (i == pos) continue;
|
||||
delta = (unsigned short int)buf[i+1] + (unsigned short int)((unsigned short int)buf[i] << 8);
|
||||
sum += delta;
|
||||
if (sum & (unsigned int) 0x10000) { /* if there's a carry... */
|
||||
sum &= 0xffff; /* get rid of the carry bit */
|
||||
sum++; /* and move it down here */
|
||||
}
|
||||
}
|
||||
if ( len & 1) {
|
||||
delta = (unsigned short int)((unsigned short int)buf[i] << 8);
|
||||
sum += delta;
|
||||
if (sum & (unsigned int) 0x10000) { /* if there's a carry... */
|
||||
sum &= 0xffff; /* get rid of the carry bit */
|
||||
sum++; /* and move it down here */
|
||||
}
|
||||
}
|
||||
sum = ~sum;
|
||||
return sum;
|
||||
|
||||
} //end calc_checksum
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char fix_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos)
|
||||
{
|
||||
unsigned short int sum = get_checksum(buf,len,pos);
|
||||
buf[pos] = (unsigned char)(sum >> 8);
|
||||
buf[pos+1] = (unsigned char)sum;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char check_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos, char type)
|
||||
{
|
||||
unsigned short int sum = get_checksum(buf,len,pos);
|
||||
|
||||
if ((buf[pos] != (unsigned char)(sum >> 8)) ||
|
||||
(buf[pos+1] != (unsigned char) sum)) {
|
||||
|
||||
type = 0; /* get around compiler warning */
|
||||
return ERR;
|
||||
} else {
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char send_icmp_echo_reply(unsigned char *buf)
|
||||
{
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) buf;
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (buf + sizeof (ether_hdr_t));
|
||||
icmp_hdr_xp icmp_hdr = (icmp_hdr_xp )
|
||||
(buf + sizeof (ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
unsigned short int elen = ((unsigned short int)ip_hdr->tlen[0] << 8) + (unsigned short int)ip_hdr->tlen[1] - sizeof(ip_hdr_t);
|
||||
memcpy(eth_hdr->da, eth_hdr->sa, ETH_ADDR_LEN);
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
memcpy(ip_hdr->da, ip_hdr->sa, IP_ADDR_LEN);
|
||||
memcpy(ip_hdr->sa, my_ip, IP_ADDR_LEN);
|
||||
ip_hdr->ttl--;
|
||||
fix_checksum((unsigned char *)ip_hdr, (unsigned short int) 20, (unsigned short int) 10);
|
||||
icmp_hdr->type = ICMP_TYPE_ECHO_REPLY;
|
||||
if (elen & 1) {
|
||||
((unsigned char *)icmp_hdr)[elen] = 0;
|
||||
}
|
||||
fix_checksum((unsigned char *)icmp_hdr, (unsigned short int) elen, (unsigned short int) 2);
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(buf,elen + sizeof(ether_hdr_t) + sizeof(ip_hdr_t), MSS_MAC_BLOCKING);
|
||||
return OK;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
void dtoa_reverse(unsigned short int n, unsigned char *buf)
|
||||
{
|
||||
buf--;
|
||||
if (n == 0) {
|
||||
*buf = '0';
|
||||
return;
|
||||
}
|
||||
while (n > 0) {
|
||||
*buf-- = (n % 10) + '0';
|
||||
n = n / 10;
|
||||
}
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
void send_bootp_packet (unsigned char *buf)
|
||||
{
|
||||
/* output packet */
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) tcp_packet;
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (tcp_packet + sizeof(ether_hdr_t));
|
||||
udp_hdr_xp udp_hdr = (udp_hdr_xp ) (tcp_packet + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
bootp_pkt_xp bootp_pkt = (bootp_pkt_xp )((unsigned char *)udp_hdr + sizeof(udp_hdr_t));
|
||||
unsigned char *opts = bootp_pkt->vend;
|
||||
/* input packet */
|
||||
// eth_hdr_xp ieth_hdr = (eth_hdr_xp ) buf;
|
||||
// ip_hdr_xp iip_hdr = (ip_hdr_xp ) (buf + sizeof(ether_hdr_t));
|
||||
udp_hdr_xp iudp_hdr = (udp_hdr_xp ) (buf + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
bootp_pkt_xp ibootp_pkt = (bootp_pkt_xp )((unsigned char *)iudp_hdr + sizeof(udp_hdr_t));
|
||||
unsigned short int plen;
|
||||
/* Set up Bootp */
|
||||
memset(bootp_pkt, 0, sizeof(bootp_pkt_t));
|
||||
bootp_pkt->op = BOOTP_OP_REQUEST;
|
||||
bootp_pkt->hwtype = BOOTP_HWTYPE_ETH;
|
||||
bootp_pkt->hlen = ETH_ADDR_LEN;
|
||||
bootp_pkt->secs[1] = 0x64;
|
||||
memcpy(bootp_pkt->chaddr, my_mac, ETH_ADDR_LEN);
|
||||
bootp_pkt->flags[0] = 0x80; /* ask for a broadcast */
|
||||
if (buf) {
|
||||
if (memcmp(my_mac, ibootp_pkt->chaddr, ETH_ADDR_LEN)) /* not for me ignore */
|
||||
return;
|
||||
memcpy(my_ip, ibootp_pkt->yiaddr, IP_ADDR_LEN);
|
||||
ip_known = 1;
|
||||
dhcp_ip_found = 1;
|
||||
memcpy(bootp_pkt->ciaddr, ibootp_pkt->yiaddr, IP_ADDR_LEN);
|
||||
memcpy(bootp_pkt->xid, ibootp_pkt->xid, BOOTP_XID_LEN);
|
||||
} else {
|
||||
bootp_pkt->xid[0] = 0x90;
|
||||
}
|
||||
*opts++ = 99; /* magic number */
|
||||
*opts++ = 130;
|
||||
*opts++ = 83;
|
||||
*opts++ = 99;
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_TYPE;
|
||||
*opts++ = 1;
|
||||
if (buf) {
|
||||
*opts++ = DHCP_TYPE_REQUEST;
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_SID;
|
||||
*opts++ = 4;
|
||||
*opts++ = ibootp_pkt->siaddr[0];
|
||||
*opts++ = ibootp_pkt->siaddr[1];
|
||||
*opts++ = ibootp_pkt->siaddr[2];
|
||||
*opts++ = ibootp_pkt->siaddr[3];
|
||||
} else {
|
||||
*opts++ = DHCP_TYPE_DISCOVER;
|
||||
}
|
||||
*opts++ = BOOTP_OPTCODE_END;
|
||||
|
||||
/* Set up Udp */
|
||||
memset(udp_hdr, 0, sizeof(udp_hdr_t));
|
||||
udp_hdr->sp[1] = BOOTP_CLIENT_PORT;
|
||||
udp_hdr->dp[1] = BOOTP_SERVER_PORT;
|
||||
plen = sizeof(udp_hdr_t) + sizeof(bootp_pkt_t);
|
||||
udp_hdr->len[0] = plen >> 8;
|
||||
udp_hdr->len[1] = (unsigned char) plen;
|
||||
/* leave csum 0 */
|
||||
|
||||
/* Set up IP */
|
||||
memset(ip_hdr, 0, sizeof(ip_hdr_t));
|
||||
ip_hdr->ver_hlen = 0x45; /* IPv4 with 20 byte header */
|
||||
plen += sizeof(ip_hdr_t);
|
||||
ip_hdr->tlen[0] = plen >> 8;
|
||||
ip_hdr->tlen[1] = (unsigned char) plen;
|
||||
ip_hdr->id[0] = ip_id >> 8;
|
||||
ip_hdr->id[1] = (unsigned char) ip_id;
|
||||
ip_id++;
|
||||
ip_hdr->ttl = 32; /* max 32 hops */
|
||||
ip_hdr->proto = UDP_PROTO;
|
||||
memset(ip_hdr->da, 0xFF, IP_ADDR_LEN);
|
||||
fix_checksum((unsigned char *)ip_hdr, sizeof(ip_hdr_t), 10);
|
||||
/* Set up Ethernet */
|
||||
eth_hdr->type_code[0] = ETH_TYPE_0;
|
||||
eth_hdr->type_code[1] = ETH_TYPE_IP_1;
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
memset(eth_hdr->da, 0xFF, ETH_ADDR_LEN); /* broadcast */
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(tcp_packet,plen + sizeof(ether_hdr_t), MSS_MAC_BLOCKING);
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
void send_dhcp_server_packet (unsigned char *buf)
|
||||
{
|
||||
unsigned char * tcp_packet = tcp_packet;
|
||||
/* output packet */
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) tcp_packet;
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (tcp_packet + sizeof(ether_hdr_t));
|
||||
udp_hdr_xp udp_hdr = (udp_hdr_xp ) (tcp_packet + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
bootp_pkt_xp bootp_pkt = (bootp_pkt_xp )((unsigned char *)udp_hdr + sizeof(udp_hdr_t));
|
||||
unsigned char *opts = bootp_pkt->vend;
|
||||
|
||||
/* input packet */
|
||||
eth_hdr_xp ieth_hdr = (eth_hdr_xp ) buf;
|
||||
// ip_hdr_xp iip_hdr = (ip_hdr_xp ) (buf + sizeof(ether_hdr_t));
|
||||
udp_hdr_xp iudp_hdr = (udp_hdr_xp ) (buf + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
bootp_pkt_xp ibootp_pkt = (bootp_pkt_xp )((unsigned char *)iudp_hdr + sizeof(udp_hdr_t));
|
||||
unsigned char *iopts = ibootp_pkt->vend;
|
||||
|
||||
unsigned short int plen;
|
||||
|
||||
/* Set up Bootp */
|
||||
memset(bootp_pkt, 0, sizeof(bootp_pkt_t));
|
||||
bootp_pkt->op = BOOTP_OP_REPLY;
|
||||
bootp_pkt->hwtype = BOOTP_HWTYPE_ETH;
|
||||
bootp_pkt->hlen = ETH_ADDR_LEN;
|
||||
bootp_pkt->secs[1] = 0x64;
|
||||
memcpy(bootp_pkt->chaddr, ieth_hdr->sa, ETH_ADDR_LEN);
|
||||
bootp_pkt->flags[0] = 0x00;
|
||||
if (buf) {
|
||||
memcpy(bootp_pkt->ciaddr, ibootp_pkt->yiaddr, IP_ADDR_LEN);
|
||||
memcpy(bootp_pkt->yiaddr, g_client_ip, IP_ADDR_LEN);
|
||||
memcpy(bootp_pkt->xid, ibootp_pkt->xid, BOOTP_XID_LEN);
|
||||
} else {
|
||||
bootp_pkt->xid[0] = 0x90;
|
||||
}
|
||||
*opts++ = 99; /* magic number */
|
||||
*opts++ = 130;
|
||||
*opts++ = 83;
|
||||
*opts++ = 99;
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_TYPE;
|
||||
*opts++ = 1;
|
||||
if (iopts[6] == DHCP_TYPE_DISCOVER)
|
||||
{
|
||||
*opts++ = DHCP_TYPE_OFFER;
|
||||
}
|
||||
else
|
||||
{
|
||||
*opts++ = DHCP_TYPE_ACK;
|
||||
}
|
||||
/* Server ID */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_SID;
|
||||
*opts++ = 4;
|
||||
*opts++ = my_ip[0];
|
||||
*opts++ = my_ip[1];
|
||||
*opts++ = my_ip[2];
|
||||
*opts++ = my_ip[3];
|
||||
/* Lease time (1 our) */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_LEASE;
|
||||
*opts++ = 4;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x0E;
|
||||
*opts++ = 0x10;
|
||||
/* Renewal time */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_RENEW;
|
||||
*opts++ = 4;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x07;
|
||||
*opts++ = 0x08;
|
||||
/* Rebinding time */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_REBIND;
|
||||
*opts++ = 4;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x00;
|
||||
*opts++ = 0x0C;
|
||||
*opts++ = 0x4E;
|
||||
/* Subnet mask */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_SUBNET;
|
||||
*opts++ = 4;
|
||||
*opts++ = 0xFF;
|
||||
*opts++ = 0xFF;
|
||||
*opts++ = 0xFF;
|
||||
*opts++ = 0x00;
|
||||
/* Router */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_ROUTER;
|
||||
*opts++ = 4;
|
||||
*opts++ = my_ip[0];
|
||||
*opts++ = my_ip[1];
|
||||
*opts++ = my_ip[2];
|
||||
*opts++ = my_ip[3];
|
||||
/* Domain */
|
||||
*opts++ = BOOTP_OPTCODE_DHCP_DOMAIN;
|
||||
*opts++ = 4;
|
||||
*opts++ = my_ip[0];
|
||||
*opts++ = my_ip[1];
|
||||
*opts++ = my_ip[2];
|
||||
*opts++ = my_ip[3];
|
||||
|
||||
*opts++ = BOOTP_OPTCODE_END;
|
||||
|
||||
/* Set up Udp */
|
||||
memset(udp_hdr, 0, sizeof(udp_hdr_t));
|
||||
udp_hdr->sp[1] = BOOTP_SERVER_PORT;
|
||||
udp_hdr->dp[1] = BOOTP_CLIENT_PORT;
|
||||
plen = sizeof(udp_hdr_t) + sizeof(bootp_pkt_t);
|
||||
udp_hdr->len[0] = plen >> 8;
|
||||
udp_hdr->len[1] = (unsigned char) plen;
|
||||
/* leave csum 0 */
|
||||
|
||||
/* Set up IP */
|
||||
memset(ip_hdr, 0, sizeof(ip_hdr_t));
|
||||
ip_hdr->ver_hlen = 0x45; /* IPv4 with 20 byte header */
|
||||
plen += sizeof(ip_hdr_t);
|
||||
ip_hdr->tlen[0] = plen >> 8;
|
||||
ip_hdr->tlen[1] = (unsigned char) plen;
|
||||
ip_hdr->id[0] = ip_id >> 8;
|
||||
ip_hdr->id[1] = (unsigned char) ip_id;
|
||||
ip_id++;
|
||||
ip_hdr->ttl = 255;
|
||||
ip_hdr->proto = UDP_PROTO;
|
||||
memcpy(ip_hdr->sa, my_ip, IP_ADDR_LEN);
|
||||
memset(ip_hdr->da, 0xFF, IP_ADDR_LEN);
|
||||
fix_checksum((unsigned char *)ip_hdr, sizeof(ip_hdr_t), 10);
|
||||
|
||||
/* Set up Ethernet */
|
||||
eth_hdr->type_code[0] = ETH_TYPE_0;
|
||||
eth_hdr->type_code[1] = ETH_TYPE_IP_1;
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
memset(eth_hdr->da, 0xFF, ETH_ADDR_LEN); /* broadcast */
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(tcp_packet,plen + sizeof(ether_hdr_t), MSS_MAC_BLOCKING);
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_udp_packet (unsigned char *buf)
|
||||
{
|
||||
|
||||
udp_hdr_xp udp_hdr = (udp_hdr_xp ) (buf + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
|
||||
if (udp_hdr->dp[1] != BOOTP_CLIENT_PORT) {
|
||||
send_dhcp_server_packet( buf );
|
||||
return OK;
|
||||
}
|
||||
if (ip_known) {
|
||||
return ERR;
|
||||
}
|
||||
/* some more error checking here? */
|
||||
send_bootp_packet(buf);
|
||||
return OK;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
|
||||
void send_tcp_packet (unsigned char control_bits,unsigned short int buflen)
|
||||
{
|
||||
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp ) tcp_packet;
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (tcp_packet + sizeof(ether_hdr_t));
|
||||
tcp_hdr_xp tcp_hdr = (tcp_hdr_xp )
|
||||
(tcp_packet + sizeof(ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
tcp_pseudo_hdr_xp tcp_pseudo_hdr = (tcp_pseudo_hdr_xp )
|
||||
(((unsigned char *)tcp_hdr) - sizeof(tcp_pseudo_hdr_t));
|
||||
unsigned char *tcp_data = tcp_packet + sizeof(ether_hdr_t) + sizeof(ip_hdr_t) + sizeof (tcp_hdr_t);
|
||||
unsigned char *seqp = (unsigned char *)(&tcb.local_seq);
|
||||
unsigned short int plen;
|
||||
memset(tcp_hdr, 0, sizeof(tcp_hdr_t));
|
||||
memcpy(tcp_hdr->sp, tcb.local_port, TCP_PORT_LEN);
|
||||
memcpy(tcp_hdr->dp, tcb.remote_port, TCP_PORT_LEN);
|
||||
tcp_hdr->seqnum[0] = seqp[3];
|
||||
tcp_hdr->seqnum[1] = seqp[2];
|
||||
tcp_hdr->seqnum[2] = seqp[1];
|
||||
tcp_hdr->seqnum[3] = seqp[0];
|
||||
tcb.local_seq++;
|
||||
if (buflen) {
|
||||
tcb.local_seq += buflen - 1;
|
||||
}
|
||||
if (control_bits & TCP_CNTRL_ACK) {
|
||||
seqp = (unsigned char *)(&tcb.remote_seq);
|
||||
tcp_hdr->acknum[3] = seqp[0];
|
||||
tcp_hdr->acknum[2] = seqp[1];
|
||||
tcp_hdr->acknum[1] = seqp[2];
|
||||
tcp_hdr->acknum[0] = seqp[3];
|
||||
}
|
||||
tcp_hdr->data_off = 0x50; /* always 5 32 bit words for us */
|
||||
tcp_hdr->urg_ack_psh_rst_syn_fin = control_bits;
|
||||
tcp_hdr->wsize[0] = 0x08; /* this is 0x0800, which is 2K */
|
||||
if (buflen & 1) {
|
||||
tcp_data[buflen] = 0;
|
||||
}
|
||||
/* memset(tcp_pseudo_hdr, 0, sizeof(tcp_pseudo_hdr_t)); */
|
||||
memcpy(tcp_pseudo_hdr->sa, my_ip, IP_ADDR_LEN);
|
||||
memcpy(tcp_pseudo_hdr->da, tcb.remote_addr, IP_ADDR_LEN);
|
||||
tcp_pseudo_hdr->zero = 0;
|
||||
tcp_pseudo_hdr->proto = TCP_PROTO;
|
||||
plen = buflen + sizeof(tcp_hdr_t);
|
||||
tcp_pseudo_hdr->plen[0] = plen >> 8;
|
||||
tcp_pseudo_hdr->plen[1] = (unsigned char)plen;
|
||||
fix_checksum((unsigned char *)tcp_pseudo_hdr,
|
||||
(unsigned short int)(plen + sizeof(tcp_pseudo_hdr_t)), (unsigned short int)28);
|
||||
|
||||
memset(ip_hdr, 0, sizeof(ip_hdr_t));
|
||||
|
||||
ip_hdr->ver_hlen = 0x45; /* IPv4 with 20 byte header */
|
||||
plen += sizeof(ip_hdr_t); /* add the size of the IP Header */
|
||||
ip_hdr->tlen[0] = plen >> 8;
|
||||
ip_hdr->tlen[1] = (unsigned char) plen;
|
||||
ip_hdr->id[0] = ip_id >> 8;
|
||||
ip_hdr->id[1] = (unsigned char) ip_id;
|
||||
ip_id++;
|
||||
ip_hdr->ttl = 32; /* max 32 hops */
|
||||
ip_hdr->proto = TCP_PROTO;
|
||||
memcpy(ip_hdr->sa, my_ip, IP_ADDR_LEN);
|
||||
memcpy(ip_hdr->da, tcb.remote_addr, IP_ADDR_LEN);
|
||||
fix_checksum((unsigned char *)ip_hdr, sizeof(ip_hdr_t), 10);
|
||||
/* Fix the Ethernet Header */
|
||||
eth_hdr->type_code[0] = ETH_TYPE_0;
|
||||
eth_hdr->type_code[1] = ETH_TYPE_IP_1;
|
||||
memcpy(eth_hdr->sa, my_mac, ETH_ADDR_LEN);
|
||||
memcpy(eth_hdr->da, tcb.remote_mac, ETH_ADDR_LEN); /* should be table lookup */
|
||||
num_pkt_tx++;
|
||||
MSS_MAC_tx_packet(tcp_packet,plen + sizeof(ether_hdr_t), MSS_MAC_BLOCKING);
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char tcp_init(void)
|
||||
{
|
||||
memset(&tcb,0,sizeof(tcp_control_block_t));
|
||||
tcb.state = TCP_STATE_LISTEN;
|
||||
ip_id = 0;
|
||||
ip_known = 0;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char hex_digits_to_byte(unsigned char u, unsigned char l)
|
||||
{
|
||||
if (u > '9')
|
||||
u = u - 'A' + 10;
|
||||
else
|
||||
u = u - '0';
|
||||
if (l > '9')
|
||||
l = l - 'A' + 10;
|
||||
else
|
||||
l = l - '0';
|
||||
return (u << 4) + l;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_icmp_packet(unsigned char *buf)
|
||||
{
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (buf + sizeof (ether_hdr_t));
|
||||
icmp_hdr_xp icmp_hdr = (icmp_hdr_xp )
|
||||
(buf + sizeof (ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
unsigned short int elen = ((unsigned short int)ip_hdr->tlen[0] << 8) + (unsigned short int)ip_hdr->tlen[1] - sizeof(ip_hdr_t);
|
||||
if (check_checksum((unsigned char *)icmp_hdr, (unsigned short int) elen, (unsigned short int) 2, 'M') != OK)
|
||||
return ERR;
|
||||
if (icmp_hdr->type != ICMP_TYPE_ECHO_REQUEST) {
|
||||
return ERR;
|
||||
}
|
||||
return send_icmp_echo_reply(buf);
|
||||
}
|
||||
|
||||
/* See tcpip.h for more information.
|
||||
*/
|
||||
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_tcp_packet(unsigned char *buf)
|
||||
{
|
||||
eth_hdr_xp eth_hdr = (eth_hdr_xp )buf;
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (buf + sizeof (ether_hdr_t));
|
||||
tcp_hdr_xp tcp_hdr = (tcp_hdr_xp )
|
||||
(buf + sizeof (ether_hdr_t) + sizeof(ip_hdr_t));
|
||||
unsigned short int elen = ((unsigned short int)ip_hdr->tlen[0] << 8) + (unsigned short int)ip_hdr->tlen[1] - sizeof(ip_hdr_t);
|
||||
unsigned char state;
|
||||
if ( !memcmp(tcb.remote_addr, ip_hdr->sa, IP_ADDR_LEN) && /* same source IP */
|
||||
!memcmp(tcb.remote_port, tcp_hdr->sp, TCP_PORT_LEN) && /* same source port */
|
||||
!memcmp(tcb.local_port, tcp_hdr->dp, TCP_PORT_LEN)) { /* same dest port */
|
||||
state = tcb.state;
|
||||
} else { /* copy it over, a new IP wants in */
|
||||
memcpy(tcb.remote_addr, ip_hdr->sa, IP_ADDR_LEN);
|
||||
memcpy(tcb.remote_port, tcp_hdr->sp, TCP_PORT_LEN);
|
||||
memcpy(tcb.local_port, tcp_hdr->dp, TCP_PORT_LEN);
|
||||
memcpy(tcb.remote_mac, eth_hdr->sa, ETH_ADDR_LEN);
|
||||
state = TCP_STATE_LISTEN;
|
||||
}
|
||||
switch (state) {
|
||||
case TCP_STATE_LISTEN:
|
||||
if (tcp_hdr->urg_ack_psh_rst_syn_fin & TCP_CNTRL_SYN) {
|
||||
/* recd SYN : new connection; send SYN+ACK */
|
||||
|
||||
tcb.local_seq = TCP_START_SEQ;
|
||||
tcb.remote_seq = 0;
|
||||
tcb.remote_seq = (tcb.remote_seq | tcp_hdr->seqnum[0]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[1]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[2]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[3]);
|
||||
tcb.remote_seq++;
|
||||
send_tcp_packet( TCP_CNTRL_SYN | TCP_CNTRL_ACK, 0);
|
||||
tcb.state = TCP_STATE_SYN_RECVD;
|
||||
}
|
||||
break;
|
||||
case TCP_STATE_SYN_RECVD:
|
||||
if (tcp_hdr->urg_ack_psh_rst_syn_fin & TCP_CNTRL_ACK) {
|
||||
/* recd ack; send nothing */
|
||||
tcb.state = TCP_STATE_ESTABLISHED;
|
||||
}
|
||||
else {
|
||||
tcb.state = TCP_STATE_LISTEN;
|
||||
}
|
||||
break;
|
||||
case TCP_STATE_ESTABLISHED:
|
||||
if (tcp_hdr->urg_ack_psh_rst_syn_fin & TCP_CNTRL_FIN) {
|
||||
/* recd fin; send ack */
|
||||
/* skip CLOSE_WAIT state; send fin along with ack */
|
||||
tcb.remote_seq = 0;
|
||||
tcb.remote_seq = (tcb.remote_seq | tcp_hdr->seqnum[0]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[1]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[2]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[3]);
|
||||
tcb.remote_seq++;
|
||||
send_tcp_packet(TCP_CNTRL_ACK | TCP_CNTRL_FIN, 0);
|
||||
tcb.state = TCP_STATE_LAST_ACK;
|
||||
/* Default scroll message on OLED */
|
||||
}
|
||||
else if (tcp_hdr->dp[0] != 0 || \
|
||||
tcp_hdr->dp[1] != 80) { /* HTTP Port */
|
||||
break;
|
||||
}
|
||||
else if (elen > sizeof(tcp_hdr_t)) { /* dont respond to empty packets*/
|
||||
tcb.remote_seq = 0;
|
||||
tcb.remote_seq = (tcb.remote_seq | tcp_hdr->seqnum[0]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[1]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[2]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[3]);
|
||||
tcb.remote_seq += (unsigned long) (elen - sizeof(tcp_hdr_t));
|
||||
//send_http_response(((unsigned char *)(tcp_hdr)) + sizeof (tcp_hdr_t));
|
||||
tcb.state = TCP_STATE_MY_LAST;
|
||||
}
|
||||
break;
|
||||
case TCP_STATE_MY_LAST:
|
||||
if (tcp_hdr->urg_ack_psh_rst_syn_fin & TCP_CNTRL_FIN) {
|
||||
/* sent fin, got fin, ack the fin */
|
||||
tcb.remote_seq = 0;
|
||||
tcb.remote_seq = (tcb.remote_seq | tcp_hdr->seqnum[0]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[1]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[2]);
|
||||
tcb.remote_seq = ((tcb.remote_seq << 8) |tcp_hdr->seqnum[3]);
|
||||
tcb.remote_seq++;
|
||||
send_tcp_packet(TCP_CNTRL_ACK, 0);
|
||||
tcb.state = TCP_STATE_CLOSED;
|
||||
}
|
||||
break;
|
||||
case TCP_STATE_LAST_ACK:
|
||||
|
||||
if (tcp_hdr->urg_ack_psh_rst_syn_fin & TCP_CNTRL_ACK) {
|
||||
/* recd ack; send nothing */
|
||||
tcb.state = TCP_STATE_CLOSED;
|
||||
}
|
||||
/* no break here... go on to CLOSED directly */
|
||||
case TCP_STATE_CLOSED:
|
||||
memset (&tcb, 0, sizeof (tcp_control_block_t));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_ip_packet(unsigned char *buf)
|
||||
{
|
||||
ip_hdr_xp ip_hdr = (ip_hdr_xp ) (buf + sizeof (ether_hdr_t));
|
||||
/* Is the incoming pkt for me?
|
||||
(either explicity addressed to me or a broadcast address) */
|
||||
if (memcmp(my_ip, ip_hdr->da, IP_ADDR_LEN)) /* not my IP */ {
|
||||
if (ip_known) {
|
||||
return ERR;
|
||||
}
|
||||
if (ip_hdr->da[0] != 0xFF || ip_hdr->da[1] != 0xFF ||
|
||||
ip_hdr->da[2] != 0xFF || ip_hdr->da[3] != 0xFF) {
|
||||
return ERR;
|
||||
}
|
||||
}
|
||||
if (check_checksum((unsigned char *)ip_hdr, (unsigned short int) 20, (unsigned short int) 10, 'I') != OK)
|
||||
return ERR;
|
||||
switch (ip_hdr->proto)
|
||||
{
|
||||
case TCP_PROTO:
|
||||
return process_tcp_packet(buf);
|
||||
case ICMP_PROTO:
|
||||
return process_icmp_packet(buf);
|
||||
case UDP_PROTO:
|
||||
return process_udp_packet(buf);
|
||||
default: {
|
||||
return ERR;
|
||||
}
|
||||
}
|
||||
return ERR;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_arp_packet(unsigned char *buf)
|
||||
{
|
||||
arp_pkt_xp arp_pkt = (arp_pkt_xp )(buf + sizeof(ether_hdr_t));
|
||||
if (arp_pkt->opcode[1] != ARP_OPCODE_REQ_1) {
|
||||
if (arp_pkt->opcode[1] == ARP_OPCODE_REPLY_1)
|
||||
{
|
||||
if (!memcmp(my_ip, arp_pkt->ip_sa, IP_ADDR_LEN))
|
||||
{
|
||||
//printf("IP conflict with MAC");
|
||||
//printf("%02x:%02x:%02x:%02x:%02x:%02x",arp_pkt->mac_sa[0],arp_pkt->mac_sa[1],arp_pkt->mac_sa[2],arp_pkt->mac_sa[3],arp_pkt->mac_sa[4],arp_pkt->mac_sa[5]);
|
||||
}
|
||||
}
|
||||
return ERR;
|
||||
}
|
||||
if (memcmp(my_ip, arp_pkt->ip_ta, IP_ADDR_LEN)) {
|
||||
return ERR;
|
||||
}
|
||||
return send_arp_reply(buf);
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char process_packet( unsigned char * buf )
|
||||
{
|
||||
eth_hdr_xp eth_hdr;
|
||||
unsigned char typ;
|
||||
eth_hdr = (eth_hdr_xp ) buf;
|
||||
typ = eth_hdr->type_code[0];
|
||||
if (typ != ETH_TYPE_0)
|
||||
{
|
||||
return ERR;
|
||||
}
|
||||
typ = eth_hdr->type_code[1];
|
||||
if (typ == ETH_TYPE_ARP_1)
|
||||
{
|
||||
return process_arp_packet(buf);
|
||||
}
|
||||
else if (typ == ETH_TYPE_IP_1) {
|
||||
return process_ip_packet(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ERR;
|
||||
}
|
||||
return ERR;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
unsigned char *xstrcpy(unsigned char *d, const unsigned char *s)
|
||||
{
|
||||
unsigned char c;
|
||||
|
||||
while ((c = *s++))
|
||||
(*d++ = c) ;
|
||||
return d;
|
||||
}
|
||||
/***************************************************************************//**
|
||||
* See tcpip.h for more information.
|
||||
*/
|
||||
// updated html page with fusion board link on page:
|
||||
|
@ -1,234 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* (c) Copyright 2009 SLS Corporation,All Rights Reserved.
|
||||
*
|
||||
* tcpip.h:header file of TCP/IP implementation.
|
||||
*
|
||||
* Version Author Comment
|
||||
* 1.0.0 SLS corp. First release,16 Jan 2009
|
||||
*/
|
||||
#ifndef TCPIP_H_
|
||||
#define TCPIP_H_
|
||||
|
||||
#define FLASH_CONTEXT_INDICATOR 0x20000000
|
||||
#define FLASH_SELFWAKEUP_INDICATOR 0x20000001
|
||||
#define FLASH_CONTEXT_LOCATION 0x20000002
|
||||
|
||||
/***************************************************************************//**
|
||||
* Replies to ARP requests.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
*
|
||||
*/
|
||||
unsigned char send_arp_reply(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Sends gratuitous arp brodcast message to the LAN.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
*
|
||||
*/
|
||||
void send_gratuitous_arp(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Calculates the checksum for Ethernet data in the header.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @param len Number of bytes.
|
||||
* @param pos position for the check sum.
|
||||
*
|
||||
* @return value of the checksum
|
||||
*/
|
||||
unsigned short int get_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos);
|
||||
/***************************************************************************//**
|
||||
* Calls internally to get_checksum and fixes the value of the checksum to
|
||||
* position.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @param len Number of bytes.
|
||||
* @param pos position for the check sum.
|
||||
*
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char fix_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos);
|
||||
/***************************************************************************//**
|
||||
* Checks the calculated checksum for the errors.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @param len Number of bytes.
|
||||
* @param pos position for the check sum.
|
||||
*
|
||||
* @return OK If there is no error
|
||||
* ERR If there is error in the data
|
||||
*/
|
||||
unsigned char check_checksum(unsigned char *buf, unsigned short int len, unsigned short int pos, char type);
|
||||
/***************************************************************************//**
|
||||
* Sends the reply to ICMP request like PING.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
*/
|
||||
unsigned char send_icmp_echo_reply(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Converts the input integer to the ascii char and fills in the buffer.
|
||||
*
|
||||
* @param buf To filled in by the ascii value.
|
||||
* @param n integer number
|
||||
*/
|
||||
void dtoa_reverse(unsigned short int n, unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Sends DHCP request to the server available in LAN.
|
||||
*
|
||||
* @param buf Pointer to the recieved data in case of DHCP reply.Zero for request.
|
||||
*/
|
||||
void send_bootp_packet (unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Processes the UDP datagram.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
*/
|
||||
unsigned char process_udp_packet (unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Sends TCP packet to the network.
|
||||
*
|
||||
* @param buf Pointer to the transmitt buffer to Ethernet MAC.
|
||||
*/
|
||||
void send_tcp_packet (unsigned char control_bits,unsigned short int buflen);
|
||||
/***************************************************************************//**
|
||||
* Initialize TCP for the software TCP/IP stack.
|
||||
*
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char tcp_init(void);
|
||||
/***************************************************************************//**
|
||||
* Converts two hex decimal ascii digits into a sigle integer digit.
|
||||
*
|
||||
* @param u ascii hex digit
|
||||
* l ascii hex digit
|
||||
* @returm converted integer byte
|
||||
*
|
||||
*/
|
||||
unsigned char hex_digits_to_byte(unsigned char u, unsigned char l);
|
||||
/***************************************************************************//**
|
||||
* Processes ICMP packets
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
*
|
||||
* @return ERR if there is an error in the data
|
||||
* or calls further necessary functions.
|
||||
*/
|
||||
unsigned char process_icmp_packet(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Sends logo of ACTEL on the network over HTTP protocol.
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char http_send_logo ();
|
||||
/***************************************************************************//**
|
||||
* Sends appropriate answer to the different HTTP requests.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char send_http_response(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Process incoming TCP requests and handles the TCP state machine.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char process_tcp_packet(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Process incoming IP datagrams and handles the TCP state machine.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char process_ip_packet(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Processes the ARP packets.
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @return OK
|
||||
*/
|
||||
unsigned char process_arp_packet(unsigned char *buf);
|
||||
/***************************************************************************//**
|
||||
* Processes incoming packets and identifies its type.
|
||||
*
|
||||
* @param buf Pointer to the recieved buffer from Ethernet MAC.
|
||||
* @return call the function for further process
|
||||
* ERR if any error
|
||||
*/
|
||||
unsigned char process_packet( unsigned char * buf );
|
||||
/***************************************************************************//**
|
||||
* copies source string to destination address.
|
||||
*
|
||||
* @param d Pointer to the destination
|
||||
* @param s Pointer to the source
|
||||
* @return The last location after copy
|
||||
*
|
||||
*/
|
||||
unsigned char *xstrcpy(unsigned char *d, const unsigned char *s);
|
||||
/***************************************************************************//**
|
||||
* Sends the home page of the demonstration webserver.
|
||||
*
|
||||
*/
|
||||
void http_send_packet();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for waveform mode.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_waveform();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for multimeter mode.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_multimeter();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for DAC mode.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_DAC();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for sleeping stopwatch.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_SLEEPING_STOPWATCH();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for text terminal.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_textterminal();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for VIT auxiliary mode.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_VIT();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for Real Time Data Display.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_RTDD();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for Stock Ticker.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_Stockticker();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for Gadgets mode.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_weatherblog();
|
||||
/***************************************************************************//**
|
||||
* Sends the packet for Selfwakeup.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_SELFWAKEUP();
|
||||
/***************************************************************************//**
|
||||
* Same as above mentioned functions but following functions are applicable
|
||||
* to Internet Explorer.
|
||||
*
|
||||
*/
|
||||
void http_send_packet_IE();
|
||||
void http_send_packet_SELFWAKEUP_IE();
|
||||
void http_send_packet_VIT_IE();
|
||||
void http_send_packet_waveform_IE();
|
||||
void http_send_packet_SLEEPING_STOPWATCH_IE();
|
||||
void http_send_packet_DAC_IE();
|
||||
void http_send_packet_multimeter_IE();
|
||||
void http_send_packet_RTDD_IE();
|
||||
#endif /*TCPIP_H_*/
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
|
||||
|
||||
|
||||
***************************************************************************
|
||||
* *
|
||||
* FreeRTOS tutorial books are available in pdf and paperback. *
|
||||
* Complete, revised, and edited pdf reference manuals are also *
|
||||
* available. *
|
||||
* *
|
||||
* Purchasing FreeRTOS documentation will not only help you, by *
|
||||
* ensuring you get running as quickly as possible and with an *
|
||||
* in-depth knowledge of how to use FreeRTOS, it will also help *
|
||||
* the FreeRTOS project to continue with its mission of providing *
|
||||
* professional grade, cross platform, de facto standard solutions *
|
||||
* for microcontrollers - completely free of charge! *
|
||||
* *
|
||||
* >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
|
||||
* *
|
||||
* Thank you for using FreeRTOS, and thank you for your support! *
|
||||
* *
|
||||
***************************************************************************
|
||||
|
||||
|
||||
This file is part of the FreeRTOS distribution.
|
||||
|
||||
FreeRTOS is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License (version 2) as published by the
|
||||
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
||||
>>>NOTE<<< The modification to the GPL is included to allow you to
|
||||
distribute a combined work that includes FreeRTOS without being obliged to
|
||||
provide the source code for proprietary components outside of the FreeRTOS
|
||||
kernel. FreeRTOS is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details. You should have received a copy of the GNU General Public
|
||||
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
||||
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
||||
by writing to Richard Barry, contact details for whom are available on the
|
||||
FreeRTOS WEB site.
|
||||
|
||||
1 tab == 4 spaces!
|
||||
|
||||
http://www.FreeRTOS.org - Documentation, latest information, license and
|
||||
contact details.
|
||||
|
||||
http://www.SafeRTOS.com - A version that is certified for use in safety
|
||||
critical systems.
|
||||
|
||||
http://www.OpenRTOS.com - Commercial support, development, porting,
|
||||
licensing and training services.
|
||||
*/
|
||||
|
||||
#include "oled.h"
|
||||
|
||||
#define oledFIRST_CHARACTER 0
|
||||
|
||||
static struct oled_data xOLEDData;
|
||||
|
||||
void vOLEDInit( void )
|
||||
{
|
||||
/* Initialise the display itslef. */
|
||||
OLED_init();
|
||||
|
||||
xOLEDData.line1 = FIRST_LINE;
|
||||
xOLEDData.char_offset1 = oledFIRST_CHARACTER;
|
||||
xOLEDData.string1 = "www.FreeRTOS.org";
|
||||
|
||||
xOLEDData.line2 = SECOND_LINE;
|
||||
xOLEDData.char_offset2 = oledFIRST_CHARACTER;
|
||||
xOLEDData.string2 = 0x00;
|
||||
|
||||
xOLEDData.contrast_val = OLED_CONTRAST_VAL;
|
||||
xOLEDData.on_off = OLED_HORIZ_SCROLL_OFF;
|
||||
xOLEDData.column_scrool_per_step = OLED_HORIZ_SCROLL_STEP;
|
||||
xOLEDData.start_page = OLED_START_PAGE;
|
||||
xOLEDData.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
|
||||
xOLEDData.end_page = OLED_END_PAGE;
|
||||
|
||||
OLED_write_data( &xOLEDData, BOTH_LINES );
|
||||
}
|
||||
|
Loading…
Reference in New Issue