📄 socket.c
字号:
* Arguments : addr - pointer having the value for setting up the Subnet Mask
* Returns : None
* Note : API Function
********************************************************************************
*/
void setsubmask(u_char * addr)
{
IndirectWriteBuf(SUBNET_MASK_PTR,addr,4);
}
/*
********************************************************************************
* gateway IP setup function
*
* Description : gateway IP setup function
* Arguments : addr - pointer having the value for setting up the gateway IP
* Returns : None
* Note : API Function
********************************************************************************
*/
void setgateway(u_char * addr)
{
IndirectWriteBuf(GATEWAY_PTR,addr,4);
}
/*
********************************************************************************
* W3100A IP Address setup function
*
* Description : W3100A IP Address setup function
* Arguments : addr - pointer having the value for setting up the source IP Address
* Returns : None
* Note : API Function
********************************************************************************
*/
void setIP(u_char * addr)
{
IndirectWriteBuf(SRC_IP_PTR,addr,4);
}
/*
********************************************************************************
* MAC Address setup function
*
* Description : MAC Address setup function
* Arguments : addr - pointer having the value for setting up the MAC Address
* Returns : None
* Note : API Function
********************************************************************************
*/
void setMACAddr(u_char * addr)
{
IndirectWriteBuf(SRC_HA_PTR,addr,6);
}
#ifdef __IP_RAW__
/*
********************************************************************************
* Upper layer protocol setup function in IP RAW Mode
*
* Description : Upper layer protocol setup function in protocol field of IP header when
* developing upper layer protocol like ICMP, IGMP, EGP etc. by using IP Protocol
* Arguments : s - Channel number
* ipprotocol - Upper layer protocol setting value of IP Protocol
* (Possible to use designated IPPROTO_ in header file)
* Returns : None
* Note : API Function
* This function should be called before calling socket() that is, before socket initialization.
********************************************************************************
*/
void setIPprotocol(SOCKET s, u_char ipprotocol)
{
IndirectWriteByte(IP_PROTOCOL(s),ipprotocol);
}
#endif
#ifdef __OPT__
/*
********************************************************************************
* TCP timeout setup function
*
* Description : TCP retransmission time setup function.
* Timeout Interrupt occurs if the number of retransmission exceed the limit when establishing connection and data transmission.
* Arguments : val - Pointer having the value to setup timeout
* Upper 2byte is for initial timeout, lower 1byte is for the number of retransmission by timeout
* Returns : None
* Note : API Function
********************************************************************************
*/
void settimeout(u_char * val)
{
IndirectWriteBuf(TIMEOUT_PTR,val,3);
}
/*
********************************************************************************
* interrupt mask setup function
*
* Description : Interrupt mask setup function. Enable/Disable appropriate Interrupt.
* Arguments : mask - mask value to setup ('1' : interrupt enable)
* Returns : None
* Note : API Function
********************************************************************************
*/
void setINTMask(u_char mask)
{
IndirectWriteByte(INTMASK,mask);
}
/*
********************************************************************************
* TOS value setup function for TOS field of IP header
*
* Description : TOS value setup function for TOS field of IP header
* Arguments : s - channel number
* tos - Valuse to setup for TOS field of IP Header
* Returns : None
* Note : API Function
********************************************************************************
*/
void setTOS(SOCKET s, u_char tos)
{
IndirectWriteByte(TOS(s),tos);
}
#endif
/*
********************************************************************************
* Initialization function to appropriate channel
*
* Description : Initialize designated channel and wait until W3100 has done.
* Arguments : s - channel number
* protocol - designate protocol for channel
* SOCK_STREAM(0x01) -> TCP.
* SOCK_DGRAM(0x02) -> UDP.
* SOCK_IPL_RAW(0x03) -> IP LAYER RAW.
* SOCK_MACL_RAW(0x04) -> MAC LAYER RAW.
* port - designate source port for appropriate channel
* flag - designate option to be used in appropriate.
* SOCKOPT_BROADCAST(0x80) -> Send/receive broadcast message in UDP
* SOCKOPT_NDTIMEOUT(0x40) -> Use register value which designated TIMEOUT value
* SOCKOPT_NDACK(0x20) -> When not using no delayed ack
* SOCKOPT_SWS(0x10) -> When not using silly window syndrome
* Returns : When succeeded : Channel number, failed :1
* Note : API Function
********************************************************************************
*/
char socket(SOCKET s, u_char protocol, u_int port, u_char flag)
{
IndirectWriteByte(OPT_PROTOCOL(s),protocol | flag); // Designate socket protocol and option
if (port != 0) // setup designated port number
{
IndirectWriteBuf(SRC_PORT_PTR(s),(u_char*)&port,2);
}
else // Designate random port number which is managed by local when you didn't designate source port
{
Local_Port++;
IndirectWriteBuf(SRC_PORT_PTR(s),(u_char*)&Local_Port,2);
}
I_STATUS[s] = 0;
IndirectWriteByte(COMMAND(s),CSOCK_INIT); // SOCK_INIT
while (I_STATUS[s] == 0); // Waiting Interrupt to CSOCK_INIT
if (!(I_STATUS[s] & SSOCK_INIT_OK)) return (-1); // Error
initseqnum(s); // Use initial seq# with random number
return (s);
}
#ifdef __TCP_CLIENT__
/*
********************************************************************************
* Connection establishing function to designated peer.
*
* Description : This function establish a connection to the peer by designated channel,
* and wait until the connection is established successfully. (TCP client mode)
* Arguments : s - channel number
* addr - destination IP Address
* port - destination Port Number
* Returns : when succeeded : 1, failed : -1
* Note : API Function
********************************************************************************
*/
char connect(SOCKET s, u_char * addr, u_int port)
{
if (port != 0)
{ // designate destination port
IndirectWriteBuf(DST_PORT_PTR(s),(u_char*)&port,2);
}
else return (-1);
IndirectWriteBuf(DST_IP_PTR(s),addr,4); // designate destination IP address
I_STATUS[s] = 0;
IndirectWriteByte(COMMAND(s),CCONNECT); // CONNECT
while (I_STATUS[s] == 0) // Wait until connection is established successfully
if (select(s, SEL_CONTROL) == SOCK_CLOSED) return -1; // When failed, appropriate channel will be closed and return an error
if (!(I_STATUS[s] & SESTABLISHED)) return (-1); // Error
return (1);
}
#endif
#ifdef __TCP_SERVER__
/*
********************************************************************************
* Waits for connection request from a peer (Blocking Mode)
*
* Description : Wait for connection request from a peer through designated channel (TCP Server mode)
* Arguments : s - channel number
* addr - IP Address of the peer when a connection is established
* port - Port number of the peer when a connection is established
* Returns : When succeeded : 1, failed : -1
* Note : API Function
********************************************************************************
*/
/*
char listen(SOCKET s, u_char * addr, u_int * port)
{
I_STATUS[s] = 0;
IndirectWriteByte(COMMAND(s),CLISTEN); // LISTEN
while (I_STATUS[s] == 0) // Wait until connection is established
if (select(s, SEL_CONTROL) == SOCK_CLOSED) return -1; // When failed to connect, the designated channel will be closed and return an error.
if (I_STATUS[s] & SESTABLISHED) // Receive IP address and port number of the peer connected
{
IndirectReadBuf(DST_PORT_PTR(s),(u_char*)&port,2);
IndirectReadBuf(DST_IP_PTR(s),addr,4);
}
else return (-1); // Error
return (1);
}
*/
/*
********************************************************************************
* Waits for connection request from a peer (Non-blocking Mode)
*
* Description : Wait for connection request from a peer through designated channel (TCP Server mode)
* Arguments : s - channel number
* Returns : None
* Note : API Function
********************************************************************************
*/
void NBlisten(SOCKET s)
{
I_STATUS[s] = 0;
IndirectWriteByte(COMMAND(s),CLISTEN); // LISTEN
}
#endif
/*
********************************************************************************
* Create random value for initial Seq# when establishing TCP connection
*
* Description : In this function, you can add some source codes to create random number for initial Seq#
* In real, TCP initial SEQ# should be random value.
* (Currently, we're using static value in EVB/DK.)
* Arguments : s - channel number
* Returns : None
* Note : API Function
********************************************************************************
*/
void initseqnum(SOCKET s)
{
int i;
i = s;
SEQ_NUM.lVal++; // Designate initial seq#
// If you have random number generation function, assign random number instead of SEQ_NUM.lVal++.
IndirectWriteBuf(TX_WR_PTR(s),(u_char*)&SEQ_NUM,4);
wait_1us(2); // Wait until TX_WR_PRT has been written safely. ( Must have delay(1.6us) if next action is to write 4byte-pointer register )
IndirectWriteBuf(TX_RD_PTR(s),(u_char*)&SEQ_NUM,4);
wait_1us(2); // Wait until TX_RD_PRT has been written safely.
IndirectWriteBuf(TX_ACK_PTR(s),(u_char*)&SEQ_NUM,4);
}
#ifdef __TCP__
/*
********************************************************************************
* Function for sending TCP data.
*
* Description : Function for sending TCP data and Composed of the send() and send_in() functions.
* The send() function is an application I/F function.
* It continues to call the send_in() function to complete the sending of the data up to the size of the data to be sent
* when the application is called.
* The send_in() function receives the return value (the size of the data sent), calculates the size of the data to be sent,
* and calls the send_in() function again if there is any data left to be sent.
* Arguments : s - channel number
* buf - Pointer pointing data to send
* len - data size to send
* Returns : Succeed: sent data size, Failed: -1;
* Note : API Function
********************************************************************************
*/
int send(SOCKET s, const u_char xdata* buf, u_int len)
{
int ptr, size;
if (len <= 0) return (0);
else
{
ptr = 0;
do {
size = send_in(s, buf + ptr, len);
if (size == -1) return -1; // Error
len = len - size;
ptr += size;
} while ( len > 0);
}
return ptr;
}
/*
********************************************************************************
* Internal function for sending TCP data.
*
* Description : Called by the send() function for TCP transmission.
* It first calculates the free transmit buffer size
* and compares it with the size of the data to be transmitted to determine the transmission size.
* After calculating the data size, it copies data from TX_WR_PTR.
* It waits if there is a previous send command in process.
* When the send command is cleared, it updates the TX_WR_PTR up to the size to be transmitted and performs the send command.
* Arguments : s - channel number
* buf - Pointer pointing data to send
* len - data size to send
* Returns : Succeeded: sent data size, Failed: -1
* Note : Internal Function
********************************************************************************
*/
int send_in(SOCKET s, const u_char xdata * buf, u_int len)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -