📄 socket.c
字号:
while(!(I_STATUS[0] & SSYS_INIT_OK));
}
/*
********************************************************************************
* Subnet mask setup function
*
* Description : Subnet mask setup function
* Arguments : addr - pointer having the value for setting up the Subnet Mask
* Returns : None
* Note : API Function
********************************************************************************
*/
void setsubmask(u_char * addr)
{
u_char i;
for (i = 0; i < 4; i++) {
*(SUBNET_MASK_PTR + i) = addr[i];
}
}
/*
********************************************************************************
* 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)
{
u_char i;
for (i = 0; i < 4; i++) {
*(GATEWAY_PTR + i) = addr[i];
}
}
/*
********************************************************************************
* 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)
{
u_char i;
for (i = 0; i < 4; i++) {
*(SRC_IP_PTR + i) = addr[i];
}
}
/*
********************************************************************************
* 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)
{
u_char i;
for (i = 0; i < 6; i++) {
*(SRC_HA_PTR + i) = addr[i];
}
}
#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)
{
IP_PROTOCOL(s) = ipprotocol;
}
#endif
/*
********************************************************************************
* 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)
{
u_char i;
for (i = 0; i < 3; i++) {
*(TIMEOUT_PTR + i) = val[i];
}
}
#ifdef __OPT__
/*
********************************************************************************
* 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)
{
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)
{
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)
{
u_char k;
OPT_PROTOCOL(s) = protocol | flag; // Designate socket protocol and option
if (port != 0) // setup designated port number
{
k = (u_char)((port & 0xff00) >> 8);
*(SRC_PORT_PTR(s)) = k;
k = (u_char)(port & 0x00ff);
*(SRC_PORT_PTR(s) + 1) = k;
}
else // Designate random port number which is managed by local when you didn't designate source port
{
Local_Port++;
*SRC_PORT_PTR(s) = (u_char)((Local_Port & 0xff00) >> 8);
*(SRC_PORT_PTR(s) + 1) = (u_char)(Local_Port & 0x00ff);
}
I_STATUS[s] = 0;
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
*DST_PORT_PTR(s) = (u_char)((port & 0xff00) >> 8);
*(DST_PORT_PTR(s) + 1) = (u_char)(port & 0x00ff);
}
else return (-1);
*DST_IP_PTR(s) = addr[0]; // designate destination IP address
*(DST_IP_PTR(s) + 1) = addr[1];
*(DST_IP_PTR(s) + 2) = addr[2];
*(DST_IP_PTR(s) + 3) = addr[3];
I_STATUS[s] = 0;
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)
{
u_int i;
I_STATUS[s] = 0;
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
{
i = *DST_PORT_PTR(s);
*port = (u_int)((i & 0xff00) >> 8);
i = *(DST_PORT_PTR(s) + 1);
i = (u_int)(i & 0x00ff);
*port += (i << 8);
addr[0] = *DST_IP_PTR(s);
addr[1] = *(DST_IP_PTR(s) + 1);
addr[2] = *(DST_IP_PTR(s) + 2);
addr[3] = *(DST_IP_PTR(s) + 3);
}
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;
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++.
*TX_WR_PTR(s) = SEQ_NUM.cVal[0];
*(TX_WR_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_WR_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_WR_PTR(s) + 3) = SEQ_NUM.cVal[3];
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 )
*TX_RD_PTR(s) = SEQ_NUM.cVal[0];
*(TX_RD_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_RD_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_RD_PTR(s) + 3) = SEQ_NUM.cVal[3];
wait_1us(2); // Wait until TX_RD_PRT has been written safely.
*TX_ACK_PTR(s) = SEQ_NUM.cVal[0];
*(TX_ACK_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_ACK_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_ACK_PTR(s) + 3) = SEQ_NUM.cVal[3];
}
#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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -