📄 socket.c
字号:
}
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;
u_int cnt;
cnt = 0;
while(COMMAND(s) & CSEND) { // Wait until previous send commnad has completed
// kkk modified by KJB 2002.10.2
if(select(s,SEL_CONTROL) == SOCK_CLOSED) return -1; // Errors.
cnt++;
if (cnt >= 0x1000) {
//PutStringLn(" Timeout");
return(0);
}
}
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;
}
*/
int sendto(SOCKET s, const u_char * buf, u_int len, u_char * addr, u_int port)
{
int ptr, size;
if (len <= 0) return (0);
if (port != 0) /* Peer's destination port num. */
{
*DST_PORT_PTR(s) = (u_char)((port & 0xff00) >> 8);
*(DST_PORT_PTR(s) + 1) = (u_char)(port & 0x00ff);
}
*DST_IP_PTR(s) = addr[0]; /* Peer's 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 : %.2x%.2x\r\n", *(DST_PORT_PTR(s)), *(DST_PORT_PTR(s)+1));
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));
#endif
ptr = 0;
do {
if ((size = sendto_in(s, buf + ptr, len)) == -1) return -1;
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);
}
*/
int sendto_in(SOCKET s, const u_char * buf, u_int len)
{
u_char k;
u_int size;
un_l2cval wr_ptr, rd_ptr;
u_char * send_ptr;
u_int port;
S2_START:
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);
/* Calculate Tx free buffer size */
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]) /* Recalulates, after some delay, because of error in pointer caluation */
{
#ifdef DEBUG
printf("sendto_in() at S2_START : SSIZE\r\n");
#endif
wait_1ms(1);
goto S2_START;
}
if (size == 0) /* Waits when previous sending has not finished yet and there's no free buffer */
{
#ifdef DEBUG
printf("sendto_in() at S2_START : size == 0\r\n");
#endif
wait_1ms(1);
goto S2_START;
}
else if (size < len) len = size;
/* Calculate pointer to copy data */
send_ptr = (u_char *)(SBUFBASEADDRESS[s] + (UINT)(wr_ptr.lVal & SMASK[s]));
write_data(s, (u_char *)buf, send_ptr, len);
/* Update tx_wr_ptr */
wr_ptr.lVal = wr_ptr.lVal + len;
*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];
wait_1us(2);
COMMAND(s) = CSEND; /* SEND command */
size = 0;
/* Wait for the completion of sending UDP data */
while (COMMAND(s) & CSEND) {
wait_1ms(1);
if (size++ > 1000) { /* There is some bug. So you must reinitalize the socket */
#ifdef DEBUG
printf("Destination Port : %.2x%.2x\r\n", *(DST_PORT_PTR(s)), *(DST_PORT_PTR(s)+1));
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));
printf("command : %d : %.2x : %.2x\r\n", s, COMMAND(s), I_STATUS[s]);
printf("sendto_in : %.8lx : %.8lx : %.4x\r\n", wr_ptr.lVal, rd_ptr.lVal, len);
#endif
// 2003.03.28
*TX_RD_PTR(s) = wr_ptr.cVal[0];
*(TX_RD_PTR(s) + 1) = wr_ptr.cVal[1];
*(TX_RD_PTR(s) + 2) = wr_ptr.cVal[2];
*(TX_RD_PTR(s) + 3) = wr_ptr.cVal[3];
close(s);
port = *(SRC_PORT_PTR(s)) << 8;
port = port + *(SRC_PORT_PTR(s) + 1);
socket(s, SOCK_DGRAM, port, OPT_PROTOCOL(s) & 0xF0);
return -1; /* fail to send */
}
}
return (len);
}
/*
********************************************************************************
* UDP 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 - The value is greater than zero and less than returned value at select(s,SEL_RECV)
* addr - Peer IP address for receiving
* port - Peer port number for receiving
* Returns : Received data size
* 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,ret1;
u_char * recv_ptr;
un_l2cval wr_ptr, rd_ptr;
u_long size;
u_char k;
R1_START:
if(select(s,SEL_CONTROL)==SOCK_CLOSED) return -1;
EX0 = 0;
k = *SHADOW_RXWR_PTR(s);
wait_1us(2);
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);
wait_1us(2);
rd_ptr.cVal[0] = *RX_RD_PTR(s);
rd_ptr.cVal[1] = *(RX_RD_PTR(s) + 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -