📄 socket.c
字号:
* 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
#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;
}
/*
********************************************************************************
* 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];
}
}
/*
********************************************************************************
* 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;
}
/*
********************************************************************************
* Output destination IP address of appropriate channel
*
* Description : Output destination IP address of appropriate channel
* Arguments : s - Channel number which try to get destination IP Address
* addr - Buffer address to store destination IP address
* Returns : None
* Note : API Function
* Output format is written in Hexadecimal.
********************************************************************************
*/
void GetDestAddr(SOCKET s,u_char* addr)
{
u_char i;
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);
PutString("DestAddr = ");
for(i = 0; i < 4; i++) PutHTOA(addr[i]);
PutStringLn("");
}
#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.
* 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)
{
u_char k;
int size;
un_l2cval wr_ptr, ack_ptr;
u_char * send_ptr;
S_START:
EX0 = 0;
k = *SHADOW_TXWR_PTR(s); // Must read the shadow register for reading 4byte pointer registers
wait_1us(2); // wait for reading 4byte pointer registers safely
wr_ptr.cVal[0] = *TX_WR_PTR(s);
wr_ptr.cVal[1] = *(TX_WR_PTR(s) + 1);
wr_ptr.cVal[2] = *(TX_WR_PTR(s) + 2);
wr_ptr.cVal[3] = *(TX_WR_PTR(s) + 3);
k = *SHADOW_TXACK_PTR(s); // Must read the shadow register for reading 4byte pointer registers
wait_1us(2); // wait for reading 4byte pointer registers safely
ack_ptr.cVal[0] = *TX_ACK_PTR(s);
ack_ptr.cVal[1] = *(TX_ACK_PTR(s) + 1);
ack_ptr.cVal[2] = *(TX_ACK_PTR(s) + 2);
ack_ptr.cVal[3] = *(TX_ACK_PTR(s) + 3);
EX0 = 1;
// Calculate send free buffer size
if (wr_ptr.lVal >= ack_ptr.lVal) size = SSIZE[s] - (wr_ptr.lVal - ack_ptr.lVal);
else size = SSIZE[s] - (0 - ack_ptr.lVal + wr_ptr.lVal);
if (size > SSIZE[s]) // Recalulate after some delay because of error in pointer caluation
{
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
wait_1ms(1);
#ifdef DEBUG
// printf("1 ");
// printf("%.8lx ", wr_ptr.lVal);
// printf("%.8lx\r\n", ack_ptr.lVal);
PutString("send_in() at S_START : ");PutHTOA(s); PutString(" : "); PutLTOA(wr_ptr.lVal) ; PutString(" : ");PutLTOA(ack_ptr.lVal) ;PutStringLn("");
#endif
goto S_START;
}
if (size == 0) // Wait when previous sending has not finished yet and there's no free buffer
{
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
wait_1ms(1);
#ifdef DEBUG
PutStringLn("send_in() at S_START : size == 0");
#endif
goto S_START;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -