📄 socket.lst
字号:
886 2 }
887 1
888 1 recv_ptr = (u_char *) (RBUFBASEADDRESS[s] + (UINT)(rd_ptr.lVal & RMASK[s])); // Calculate pointer to b
-e copied received data
889 1
890 1 read_data(s, recv_ptr, buf, len); // Copy receibed data
891 1
892 1 rd_ptr.lVal += len; // Update rx_rd_ptr
893 1 *RX_RD_PTR(s) = rd_ptr.cVal[0];
894 1 *(RX_RD_PTR(s) + 1) = rd_ptr.cVal[1];
895 1 *(RX_RD_PTR(s) + 2) = rd_ptr.cVal[2];
896 1 *(RX_RD_PTR(s) + 3) = rd_ptr.cVal[3];
897 1
898 1 COMMAND(s) = CRECV; // RECV
899 1
900 1 return (len);
901 1 }
902 #endif // __TCP__
903
904 #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");
C51 COMPILER V8.02 SOCKET 10/17/2006 16:52:42 PAGE 17
// 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);
C51 COMPILER V8.02 SOCKET 10/17/2006 16:52:42 PAGE 18
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 sen
-ding 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 c
-opy 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
{
C51 COMPILER V8.02 SOCKET 10/17/2006 16:52:42 PAGE 19
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;
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);
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 == 0) return 0;
recv_ptr = (u_char *)(RBUFBASEADDRESS[s] + (UINT)(rd_ptr.lVal & RMASK[s])); // Calulate received data
- pointer
if ((OPT_PROTOCOL(s) & 0x07) == SOCK_DGRAM) // Handle UDP data
{
read_data(s,recv_ptr,UDPHeader.u.stream,8); // W3100A UDP header copy
addr[0] = UDPHeader.u.header.addr[0]; // Read IP address of the peer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -