📄 socket.c
字号:
* 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;
}
else if (size < len) len = size;
send_ptr = (u_char *)( SBUFBASEADDRESS[s] + (UINT)(wr_ptr.lVal & SMASK[s])); // Calculate pointer to data copy
write_data(s, buf, send_ptr, len); // data copy
while (COMMAND(s) & CSEND) // Confirm send command
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
wr_ptr.lVal = wr_ptr.lVal + len; // tx_wr_ptr update
*TX_WR_PTR(s) = wr_ptr.cVal[0];
*(TX_WR_PTR(s) + 1) = wr_ptr.cVal[1];
*(TX_WR_PTR(s) + 2) = wr_ptr.cVal[2];
*(TX_WR_PTR(s) + 3) = wr_ptr.cVal[3];
COMMAND(s) = CSEND; // SEND
return (len);
}
/*
********************************************************************************
* TCP data receiving function.
*
* Description : This function is for receiving TCP data.
* The recv() function is an application I/F function. It continues to wait for as much data as the application wants to receive.
* Arguments : s - channel number
* buf - Pointer where the data to be received is copied
* len - Size of the data to be received
* Returns : Succeeded: received data size, Failed: -1
* Note : API Fcuntion
********************************************************************************
*/
int recv(SOCKET s, const u_char xdata* buf, u_int len)
{
u_char k;
u_int size;
un_l2cval wr_ptr, rd_ptr;
u_char * recv_ptr;
R_START:
EX0 = 0;
k = *SHADOW_RXWR_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] = *RX_WR_PTR(s);
wr_ptr.cVal[1] = *(RX_WR_PTR(s) + 1);
wr_ptr.cVal[2] = *(RX_WR_PTR(s) + 2);
wr_ptr.cVal[3] = *(RX_WR_PTR(s) + 3);
k = *SHADOW_RXRD_PTR(s); // Must read the shadow register for reading 4byte pointer registers
wait_1us(2); // wait for reading 4byte pointer registers safely
rd_ptr.cVal[0] = *RX_RD_PTR(s);
rd_ptr.cVal[1] = *(RX_RD_PTR(s) + 1);
rd_ptr.cVal[2] = *(RX_RD_PTR(s) + 2);
rd_ptr.cVal[3] = *(RX_RD_PTR(s) + 3);
EX0 = 1;
// calculate received data size
if ( len <= 0 ) return (0);
else if (wr_ptr.lVal >= rd_ptr.lVal) size = wr_ptr.lVal - rd_ptr.lVal;
else size = 0 - rd_ptr.lVal + wr_ptr.lVal;
if (size < len) // Wait until receiving is done when received data size is less then len
{
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
wait_1ms(1);
#ifdef DEBUG
// printf("size < len\r\n");
PutStringLn("size < len");
#endif
goto R_START;
}
recv_ptr = (u_char *) (RBUFBASEADDRESS[s] + (UINT)(rd_ptr.lVal & RMASK[s])); // Calculate pointer to be copied received data
read_data(s, recv_ptr, buf, len); // Copy receibed data
rd_ptr.lVal += len; // Update rx_rd_ptr
*RX_RD_PTR(s) = rd_ptr.cVal[0];
*(RX_RD_PTR(s) + 1) = rd_ptr.cVal[1];
*(RX_RD_PTR(s) + 2) = rd_ptr.cVal[2];
*(RX_RD_PTR(s) + 3) = rd_ptr.cVal[3];
COMMAND(s) = CRECV; // RECV
return (len);
}
#endif // __TCP__
#ifdef __UDP__
/*
********************************************************************************
* UDP data sending function.
*
* Description : Composed of the sendto()and sendto_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.Unlike TCP transmission, it designates the destination address and the port.
* Arguments : s - channel port
* buf - Pointer pointing data to send
* len - data size to send
* addr - destination IP address to send data
* port - destination port number to send data
* Returns : Sent data size
* Note : API Function
********************************************************************************
*/
u_int sendto(SOCKET s, const u_char xdata* buf, u_int len, u_char * addr, u_int port)
{
int ptr, size;
while(COMMAND(s) & CSEND) // Wait until previous send commnad has completed
if(select(s,SEL_CONTROL) == SOCK_CLOSED) return -1; // Errors.
if (port != 0) // Designate destination port number
{
*DST_PORT_PTR(s) = (u_char)((port & 0xff00) >> 8);
*(DST_PORT_PTR(s) + 1) = (u_char)(port & 0x00ff);
}
*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];
#ifdef DEBUG
// printf("sendto Parameter\r\n");
// printf("Destination Port : %d\r\n", (u_int)*(DST_PORT_PTR(s)));
// printf("Destination IP : %d.%d.%d.%d\r\n", *DST_IP_PTR(s) ,*DST_IP_PTR(s)+1, *DST_IP_PTR(s)+2, *DST_IP_PTR(s)+3);
PutStringLn("sendto Parameter");
PutString("Destination Port : ");PutHTOA(*DST_PORT_PTR(s));PutHTOA(*(DST_PORT_PTR(s)+1));PutStringLn("");
PutString("Destination IP : ");
PutHTOA(*DST_IP_PTR(s)); PutByte('.'); PutHTOA(*(DST_IP_PTR(s)+1)); PutByte('.'); PutHTOA(*(DST_IP_PTR(s)+2)); PutByte('.'); PutHTOA(*(DST_IP_PTR(s)+3));
PutStringLn("");
#endif
if (len <= 0) return (0);
else
{
ptr = 0;
do{
size = sendto_in(s, buf + ptr, len);
if(size == -1) return -1; // Error
len = len - size;
ptr += size;
}while ( len > 0);
}
return ptr;
}
/*
********************************************************************************
* UDP data sending function.
*
* Description : An internal function that is the same as the send_in() function of the TCP.
* Arguments : s - Channel number
* buf - Pointer indicating the data to send
* len - data size to send
* Returns : Sent data size
* Note : Internal Function
********************************************************************************
*/
u_int sendto_in(SOCKET s, const u_char xdata* buf, u_int len)
{
u_char k;
u_int size;
un_l2cval wr_ptr, rd_ptr;
u_char * send_ptr;
S2_START:
if(select(s,SEL_CONTROL)==SOCK_CLOSED) return -1; // Error
EX0 = 0;
k = *SHADOW_TXWR_PTR(s);
wait_1us(2);
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_TXRD_PTR(s);
wait_1us(2);
rd_ptr.cVal[0] = *TX_RD_PTR(s);
rd_ptr.cVal[1] = *(TX_RD_PTR(s) + 1);
rd_ptr.cVal[2] = *(TX_RD_PTR(s) + 2);
rd_ptr.cVal[3] = *(TX_RD_PTR(s) + 3);
EX0 = 1;
// Calculate free buffer size to send
if (wr_ptr.lVal >= rd_ptr.lVal) size = SSIZE[s] - (wr_ptr.lVal - rd_ptr.lVal);
else size = SSIZE[s] - (0 - rd_ptr.lVal + wr_ptr.lVal);
if (size > SSIZE[s]) // Recalulate after some delay because of error in pointer caluation
{
wait_1ms(1);
#ifdef DEBUG
PutString("sendto_in() at S2_START : ");PutHTOA(s); PutString(" : "); PutLTOA(wr_ptr.lVal) ; PutString(" : ");PutLTOA(rd_ptr.lVal) ;PutStringLn("");
#endif
goto S2_START;
}
if (size == 0) // Wait when previous sending has not finished yet and there's no free buffer
{
wait_1ms(1);
#ifdef DEBUG
PutString("sendto_in() at S2_START : size == 0");
#endif
goto S2_START;
}
else if (size < len) len = size;
send_ptr = (u_char *)(SBUFBASEADDRESS[s] + (UINT)(wr_ptr.lVal & SMASK[s])); // Calculate pointer to copy data pointer
write_data(s, buf, send_ptr, len); // Copy data
while (COMMAND(s) & CSEND) // Confirm previous send command
if(select(s,SEL_CONTROL)==SOCK_CLOSED) return -1; // Error
wr_ptr.lVal = wr_ptr.lVal + len; // Update tx_wr_ptr
*TX_WR_PTR(s) = wr_ptr.cVal[0];
*(TX_WR_PTR(s) + 1) = wr_ptr.cVal[1];
*(TX_WR_PTR(s) + 2) = wr_ptr.cVal[2];
*(TX_WR_PTR(s) + 3) = wr_ptr.cVal[3];
COMMAND(s) = CSEND; // SEND
return (len);
}
/*
********************************************************************************
* UDP or IP data receiving function.
*
* Description : Function for receiving UDP and IP layer RAW mode data, and handling the data header.
* Arguments : s - channel number
* buf - Pointer where the data to be received is copied
* len - any number greater than zero.
* addr - Peer IP address for receiving
* port - Peer port number for receiving
* Returns : Data size of received packet
* Note : API Function
********************************************************************************
*/
u_int recvfrom(SOCKET s, const u_char xdata * buf, u_int len,u_char * addr, u_int * port)
{
struct _UDPHeader // When receiving UDP data, header added by W3100A
{
union
{
struct
{
u_int size;
u_char addr[4];
u_int port;
}header;
u_char stream[8];
}u;
}xdata UDPHeader;
u_int ret;
u_char * recv_ptr;
un_l2cval wr_ptr, rd_ptr;
u_long size;
u_char k;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -