⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.c

📁 FreeRTOSV4.1.0 安裝文件 FreeRTOS 是一个源码公开的免费的嵌入式实时操作系统
💻 C
📖 第 1 页 / 共 4 页
字号:
* Arguments  : addr--> Pointer that has Gateway IP to be set
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setgateway(u_char * addr)
{
u_char i;
u_char far* gw_ptr = GATEWAY_PTR;   // We can only convert to 'regular'
                                   // pointer if we're confident arithmetic
                                   // won't take us out of current window.
for (i = 0; i < 4; i++)
  {
  WRITE_VALUE(gw_ptr + SA_OFFSET(i), addr[i]);
  }
}

/*
****************************************************************************************************
*                 Function to set W3100A IP
*
* Description:
* Arguments  : addr--> Pointer that has Source IP to be set
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setIP(u_char * addr)
{
u_char i;
u_char far* src_ptr = SRC_IP_PTR;   // We can only convert to 'regular'
                                   // pointer if we're confident arithmetic
                                   // won't take us out of current window.

for (i = 0; i < 4; i++)
  {
  WRITE_VALUE(src_ptr + SA_OFFSET(i), addr[i]);
  }
}

// DEBUG
void getIP(u_char* addr)
{
u_char i;
u_char far* src_ptr = SRC_IP_PTR;   // We can only convert to 'regular'
                                   // pointer if we're confident arithmetic
                                   // won't take us out of current window.

for (i = 0; i < 4; i++)
	addr[i] = READ_VALUE(src_ptr + SA_OFFSET(i));
}


/*
****************************************************************************************************
*                Function to set MAC
*
* Description:
* Arguments  : addr--> Pointer that has MAC to be set
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setMACAddr(u_char * addr)
{
u_char i;
u_char far* ha_ptr = SRC_HA_PTR;   // We can only convert to 'regular'
                                   // pointer if we're confident arithmetic
                                   // won't take us out of current window.

for (i = 0; i < 6; i++)
  {
  WRITE_VALUE(ha_ptr + SA_OFFSET(i), addr[i]);
  }
}

/*
****************************************************************************************************
*                  Function to set TCP timeout
*
* Description: The function that used to adjust time to resend TCP
* Arguments  : val	--> Pointer that has the value to be set
*					Upper 2 byte:Initial timeout value
*					Last 1 byte:The count to retry till timeout
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void settimeout(u_char * val)
{
u_char i;
u_char far* tout_ptr = TIMEOUT_PTR;   // We can only convert to 'regular'
                                   // pointer if we're confident arithmetic
                                   // won't take us out of current window.

for (i = 0; i < 3; i++)
  {
  WRITE_VALUE(tout_ptr + SA_OFFSET(i), val[i]);
  }
}

/*
****************************************************************************************************
*                 Function to set interrupt mask.
*
* Description:
* Arguments  : mask--> Mask value to be set ('1'-> interrupt )
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setINTMask(u_char mask)
{
WRITE_VALUE(INTMASK, mask);
}

/*
****************************************************************************************************
*                  Function to set enable in sending and receiving of broadcast data
*
* Description:  Enable to process of broadcating data in UDP or IP RAW mode.
* Arguments  : s	--> Channel No. to be set
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setbroadcast(SOCKET s)
{
u_char val = READ_VALUE(OPT_PROTOCOL(s));
WRITE_VALUE(OPT_PROTOCOL(s), val | SOCKOPT_BROADCAST);
}

/*
****************************************************************************************************
*                Function to set process protocol  in IP RAW mode.
*
* Description:
* Arguments  : s--> Channel No. to be set
*	        tos-->Protocol Value to be set
* Returns    : None.
* Note       :
****************************************************************************************************
*/
void setTOS(SOCKET s, u_char tos)
{
WRITE_VALUE(TOS(s), tos);
}

/*
****************************************************************************************************
*               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)
{
WRITE_VALUE(IP_PROTOCOL(s), ipprotocol);
}

/*
****************************************************************************************************
*              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;

//Designate socket protocol and option
WRITE_VALUE(OPT_PROTOCOL(s), protocol | flag);

// setup designated port number
if (port != 0)
  {
  k = (u_char)((port & 0xff00) >> 8);
  WRITE_VALUE(SRC_PORT_PTR(s), k);
  k = (u_char)(port & 0x00ff);
  WRITE_VALUE(SRC_PORT_PTR(s) + SA_OFFSET(1), k);
  }
else
  {
  // Designate random port number which is managed by local when you didn't designate source port
  Local_Port++;

  WRITE_VALUE(SRC_PORT_PTR(s), (u_char)((Local_Port & 0xff00) >> 8));
  WRITE_VALUE(SRC_PORT_PTR(s) + SA_OFFSET(1), (u_char)(Local_Port & 0x00ff));
  }

// SOCK_INIT
I_STATUS[s] = 0;
WRITE_VALUE(COMMAND(s), CSOCK_INIT);

// Waiting Interrupt to CSOCK_INIT
while (I_STATUS[s] == 0)
	I2CHIP_POLL_ISR(in4_isr_i2chip);

if (!(I_STATUS[s] & SSOCK_INIT_OK))
  return(-1);

initseqnum(s);							//  Use initial seq# with random number

return(s);
}

/*
****************************************************************************************************
*               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 far * addr, u_int port)
{

if (port != 0)
  {						//designate destination port
  WRITE_VALUE(DST_PORT_PTR(s), (u_char)((port & 0xff00) >> 8));
  WRITE_VALUE(DST_PORT_PTR(s) + SA_OFFSET(1), (u_char)(port & 0x00ff));
  }
else
  return(-1);

  WRITE_VALUE(DST_IP_PTR(s), addr[0]);				//designate destination IP address
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(1), addr[1]);
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(2), addr[2]);
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(3), addr[3]);

I_STATUS[s] = 0;

  WRITE_VALUE(COMMAND(s), CCONNECT);					// CONNECT
  I2CHIP_POLL_ISR(in4_isr_i2chip);

// Wait until connection is established successfully
while (I_STATUS[s] == 0)
  {
  // When failed, appropriate channel will be closed and return an error
  if (select(s, SEL_CONTROL) == SOCK_CLOSED)
    return -1;
  }

if (!(I_STATUS[s] & SESTABLISHED))
  return(-1);

return(1);
}

/*
****************************************************************************************************
*               Connection establishing function to designated peer. (Non-blocking Mode)
*
* Description : This function establish a connection to the peer by designated channel.
*
* Arguments   : s    - channel number
*               addr - destination IP Address
*               port - destination Port Number
* Returns     : when succeeded : 1, failed : -1
* Note        : API Function
****************************************************************************************************
*/
char NBconnect(SOCKET s, u_char far * addr, u_int port)
{

if (port != 0)
  {						//designate destination port
	WRITE_VALUE(DST_PORT_PTR(s), (u_char) ((port & 0xff00) >> 8) );
   WRITE_VALUE(DST_PORT_PTR(s) + SA_OFFSET(1), (u_char)(port & 0x00ff));
  }
else
  return(-1);

  WRITE_VALUE(DST_IP_PTR(s), addr[0]);				//designate destination IP address
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(1), addr[1]);
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(2), addr[2]);
  WRITE_VALUE(DST_IP_PTR(s) + SA_OFFSET(3), addr[3]);

I_STATUS[s] = 0;

WRITE_VALUE(COMMAND(s), CCONNECT);					// CONNECT
return(1);
}

/*
****************************************************************************************************
*            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 far * addr, u_int far * port)
{
u_int i;

I_STATUS[s] = 0;

// LISTEN
COMMAND(s) = CLISTEN;

// Wait until connection is established
while (I_STATUS[s] == 0)
  {
  // When failed to connect, the designated channel will be closed and return an error.
  if (select(s, SEL_CONTROL) == SOCK_CLOSED)
    return -1;
  }

// Receive IP address and port number of the peer connected
if (I_STATUS[s] & SESTABLISHED)
  {
  i = *DST_PORT_PTR(s);
  *port = (u_int)((i & 0xff00) >> 8);
  i = *(DST_PORT_PTR(s) + 2);
  i = (u_int)(i & 0x00ff);
  *port += (i << 8);

  addr[0] = *DST_IP_PTR(s);
  addr[1] = *(DST_IP_PTR(s) + 2);
  addr[2] = *(DST_IP_PTR(s) + 4);
  addr[3] = *(DST_IP_PTR(s) + 6);
  }
else
  return(-1);

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
****************************************************************************************************
*/
char NBlisten(SOCKET s)
{
I_STATUS[s] = 0;

// LISTEN
WRITE_VALUE(COMMAND(s), CLISTEN);

return(1);
}

/*
****************************************************************************************************
*               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)
{
// Designate initial seq#
// If you have random number generation function, assign random number instead of SEQ_NUM.lVal++.
SEQ_NUM.lVal++;

//randomize();
//SEQ_NUM.lVal = rand();

WRITE_VALUE(TX_WR_PTR(s), SEQ_NUM.cVal[0]);
WRITE_VALUE(TX_WR_PTR(s) + SA_OFFSET(1), SEQ_NUM.cVal[1]);
WRITE_VALUE(TX_WR_PTR(s) + SA_OFFSET(2), SEQ_NUM.cVal[2]);
WRITE_VALUE(TX_WR_PTR(s) + SA_OFFSET(3), SEQ_NUM.cVal[3]);
delay0(2);

WRITE_VALUE(TX_RD_PTR(s), SEQ_NUM.cVal[0]);
WRITE_VALUE(TX_RD_PTR(s) + SA_OFFSET(1), SEQ_NUM.cVal[1]);
WRITE_VALUE(TX_RD_PTR(s) + SA_OFFSET(2), SEQ_NUM.cVal[2]);
WRITE_VALUE(TX_RD_PTR(s) + SA_OFFSET(3), SEQ_NUM.cVal[3]);
delay0(2);

WRITE_VALUE(TX_ACK_PTR(s), SEQ_NUM.cVal[0]);
WRITE_VALUE(TX_ACK_PTR(s) + SA_OFFSET(1), SEQ_NUM.cVal[1]);
WRITE_VALUE(TX_ACK_PTR(s) + SA_OFFSET(2), SEQ_NUM.cVal[2]);
WRITE_VALUE(TX_ACK_PTR(s) + SA_OFFSET(3), SEQ_NUM.cVal[3]);
delay0(2);
}

/*
****************************************************************************************************
*              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, u_char far * buf, u_int len)
{
int ptr, size;
u_char huge* huge_buf = (u_char huge*)buf;
u_char far*  local_buf = (u_char far*)huge_buf;    

if (len <= 0)
  return (0);
else
  {
  ptr = 0;
  do
    {
	 size = send_in(s, local_buf + ptr, len);
	 if (size == -1)
      return -1;
	 len = len - size;
	 ptr += size;
	 } while ( len > 0);
  }
return ptr;
}

/*
****************************************************************************************************
*              Internal function for sending TCP data.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -