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

📄 socket.c

📁 dvr
💻 C
📖 第 1 页 / 共 4 页
字号:
*                sbufsize => 00010110, rbufsize => 01010101 : Assign 4KBytes for Tx, 2KBytes for Rx to channel #0
*                                                             2KBytes for Tx, 2KBytes for Rx to channel #1
*                                                             2KBytes for Tx, 2KBytes for Rx to channel #2
*                                                             2KBytes is available exclusively for Rx in channel #3. There's no memory for Tx.
********************************************************************************
*/
void SOCK_sysinit(void)
{
	lan_enable();
	
	// Configure W3100A Tx/Rx memory configuration
							// CH3: CH2: CH1: CH0:
//	TX_DMEM_SIZE = 0x0A;	//   0    0    4K   4K  --> 0x0A
	idmw(TX_DMEM_SIZE, 0x06);   //   0    0    2K   4K  --> 0x06
	idmw(RX_DMEM_SIZE, 0x0A);   //   0    0    4K   4K  --> 0x0A
	
	SBUFBASEADDRESS[0] = SEND_DATA_BUF;
	RBUFBASEADDRESS[0] = RECV_DATA_BUF;
	RSIZE[0]=SSIZE[0]=4096;
	RMASK[0]=SMASK[0]=0x00000FFF;
	
	SBUFBASEADDRESS[1] = SEND_DATA_BUF + SSIZE[0];
	RBUFBASEADDRESS[1] = RECV_DATA_BUF + RSIZE[0];
	RSIZE[1]=SSIZE[1]=VIDEO_BUF_SIZE;	//4096;
	RMASK[1]=SMASK[1]=VIDEO_BUF_SZMK;	//0x00000FFF;

	idmw(COMMAND(0), CSYS_INIT);

	while(!(I_STATUS[0] & SSYS_INIT_OK))
	{
#ifdef DEBUG_SOCK
		printf("\rWait W3100A Interrupt!");
#else
		;
#endif
	}
#ifdef DEBUG_SOCK
	printf("\n\rSysinit of W3100A is OK");
#endif	
	dv03_enable();
}

void SOCK_update_mac()
{
/*
	u_char xdata pingbuf[8];
	
	lan_enable()
	setIPprotocol(0, IPPROTO_ICMP);  
	socket(0, SOCK_IPL_RAW, 3000,0);     // Create a socket for ARP update

	pingbuf[0] = 8;                      // ICMP TYPE
	pingbuf[1] = 0;                      // ICMP CODE
	pingbuf[2] = 0xf7;                   // CHECKSUM (already calculated)
	pingbuf[3] = 0xfd;
	pingbuf[4] = 0;                      // ID
	pingbuf[5] = 1;
	pingbuf[6] = 0;                      // SEQ #
	pingbuf[7] = 1;
	pingbuf[8] = 0;                      // Data 1 Byte
       
	sendto(0, pingbuf, 9, GATEWAY_PTR,3000);  // Ping Request
	close(0);
	dv03_enable();
/*/
	u8 i;
	u8 xdata pingbuf[9];

	pingbuf[0] = 8;                      // ICMP TYPE
	pingbuf[1] = 0;                      // ICMP CODE
	pingbuf[2] = 0xf7;                   // CHECKSUM (already calculated)
	pingbuf[3] = 0xfd;
	pingbuf[4] = 0;                      // ID
	pingbuf[5] = 1;
	pingbuf[6] = 0;                      // SEQ #
	pingbuf[7] = 1;
	pingbuf[8] = 0;                      // Data 1 Byte

	lan_enable();
	idmw(IP_PROTOCOL(0), IPPROTO_ICMP);
	SOCK_socket(0, SOCK_IPL_RAW, 3000,0);
	lan_enable();

	while(idmr(COMMAND(0)) & CSEND)								// Wait until previous send commnad has completed.
		if(select(0, SEL_CONTROL) == SOCK_CLOSED)
		{
			dv03_enable();	
			return;	// Error.
		}
	
	idmw(DST_PORT_PTR(0,0), (u8)((3000 & 0xff00) >> 8));
	idmw(DST_PORT_PTR(0,1), (u8)(3000 & 0x00ff));

		//*
	for (i = 0; i < 4; i++) 
		idmw(DST_IP_PTR(0,i), idmr(GATEWAY_PTR(i)));

#ifdef DEBUG_ARP
	printf("\r\nupdate_mac\r\ndestin IP %u.%u.%u.%u",
			(u16)idmr(DST_IP_PTR(0,0)),
			(u16)idmr(DST_IP_PTR(0,1)),
			(u16)idmr(DST_IP_PTR(0,2)),
			(u16)idmr(DST_IP_PTR(0,3)));
	printf("\r\ndestin port %02X %02X\r\n",
			(u16)idmr(DST_PORT_PTR(0,0)),(u16)idmr(DST_PORT_PTR(0,1)));
	for(i=0;i<9;i++)
		printf(" 0x%02X",(u16)pingbuf[i]);
#endif
	SOCK_send(0,pingbuf,9,SEND_NODELAY,0);
	wait_1ms(10);
#ifdef DEBUG_ARP
	printf("\n\rwait response\n\r");
	delay_time(10000,4);
#endif
	SOCK_close(0);
	
	lan_enable();
	idmw(IP_PROTOCOL(0), IPPROTO_TCP);
	dv03_enable();
//*/
}
/*
********************************************************************************
*              Function to set subnet mask 
*
* Description: 
* Arguments  : addr--> Pointer that has the value to be set 
* Returns    : None.
* Note       :
********************************************************************************
*/
void SOCK_setsubmask(u8 * addr)
{
	xdata u8 i;

	lan_enable();
	for (i = 0; i < 4; i++) 
		idmw(SUBNET_MASK_PTR(i), addr[i]);
	dv03_enable();
}

/*
********************************************************************************
*               Function to set gateway IP
*
* Description: 
* Arguments  : addr--> Pointer that has Gateway IP to be set 
* Returns    : None.
* Note       :
********************************************************************************
*/
void SOCK_setgateway(u8 * addr)
{
	xdata u8 i;

	lan_enable();
	for (i = 0; i < 4; i++) 
		idmw(GATEWAY_PTR(i), addr[i]);
	dv03_enable();
}

/*
********************************************************************************
*                 Function to set W3100A IP
*
* Description: 
* Arguments  : addr--> Pointer that has Source IP to be set 
* Returns    : None.
* Note       :
********************************************************************************
*/
void SOCK_setIP(u8 * addr)
{
	xdata u8 i;

	lan_enable();
	for (i = 0; i < 4; i++)
		idmw(SRC_IP_PTR(i), addr[i]);
	dv03_enable();
}

/*
********************************************************************************
*                Function to set MAC 
*
* Description: 
* Arguments  : addr--> Pointer that has MAC to be set 
* Returns    : None.
* Note       :
********************************************************************************
*/
void SOCK_setMACAddr(u8 * addr)
{
	xdata u8 i;

#ifdef DEBUG_ARP	
		printf("\n\rMAC = ");
#endif
	lan_enable();
	for (i = 0; i < 6; i++) {
#ifdef DEBUG_ARP	
		printf(".%i", (int)addr[i]);
#endif
		idmw(SRC_HA_PTR(i), addr[i]);
	}
	dv03_enable();
#ifdef DEBUG_ARP	
	printf("\n\r");
#endif
}

/*
********************************************************************************
*                  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 SOCK_settimeout(u8 * val)
{
	u8 i;

	lan_enable();
	for (i = 0; i < 3; i++)
		idmw(TIMEOUT_PTR(i), val[i]);
	dv03_enable();
}
*/

/*
********************************************************************************
*                 Function to set interrupt mask.
*
* Description: 
* Arguments  : mask--> Mask value to be set ('1'-> interrupt )
* Returns    : None.
* Note       :
********************************************************************************
*/
u8 SOCK_setINTMask(u8 mask)
{
	xdata u8 ret;
	lan_enable();
	idmw(INTMASK, mask);
	w3100a_intrrupt_mask = mask;
	ret = idmr(INTMASK);
	dv03_enable();
	return ret;
}
/*
void SOCK_setINTMask_enable(void)
{
	lan_enable();
	w3100a_intrrupt_mask = 0xff;
	idmw(INTMASK, w3100a_intrrupt_mask);
	dv03_enable();
}
*/

/*
********************************************************************************
*                Function to get Destination IP in connected socket 
*
* Description: 
* Arguments  : Socket number and pointer to save IP address
* Returns    : None.
* Note       :
********************************************************************************
*/
void SOCK_getDIP(SOCKET s, u8* dip)
{
	lan_enable();
	
	dip[0] = idmr(DST_IP_PTR(s,0));
	dip[1] = idmr(DST_IP_PTR(s,1));
	dip[2] = idmr(DST_IP_PTR(s,2));
	dip[3] = idmr(DST_IP_PTR(s,3));
	
	dv03_enable();
}

/*
*********************************************************************************************************
*              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 SOCK_socket(SOCKET s, u8 protocol, u16 port, u8 flag)
{
	xdata u8 k;
	xdata char ret=s;

	lan_enable();
	
	idmw(OPT_PROTOCOL(s), protocol | flag);		//Designate socket protocol and option
//	idmw(TOS(s),0x80);							// Delay Cirtical Type Of Service
	if (port != 0) {						// setup designated port number
		k = (u8)((port & 0xff00) >> 8);
		idmw(SRC_PORT_PTR(s,0), k);
		k = (u8)(port & 0x00ff);
		idmw(SRC_PORT_PTR(s,1), k);
	} else {
		Local_Port++;						// Designate random port number which is managed by local 
											// when you didn't designate source port
		idmw(SRC_PORT_PTR(s,0), (u8)((Local_Port & 0xff00) >> 8));
		idmw(SRC_PORT_PTR(s,1), (u8)(Local_Port & 0x00ff));
	}
	I_STATUS[s] = 0;
	idmw(COMMAND(s), CSOCK_INIT);				// SOCK_INIT
	while (I_STATUS[s] == 0)				// Waiting Interrupt to CSOCK_INIT
	{
#ifdef DEBUG_SOCK
		printf("\rsocket:W");
#else
		;
#endif
	}
	if (!(I_STATUS[s] & SSOCK_INIT_OK)){ ret=-1; goto ErrReturn; }
	if( protocol == SOCK_STREAM )
		initseqnum(s);							//  Use initial seq# with random number

ErrReturn:	
	dv03_enable();
	return	(ret);
}


/*
*********************************************************************************************************
*                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 SOCK_NBlisten(SOCKET s)
{
	lan_enable();
	I_STATUS[s] = 0;
	idmw(COMMAND(s), CLISTEN);	// LISTEN

	dv03_enable();
}

/*
*********************************************************************************************************
*              Function for sending TCP data.
*
* Description : Function for sending TCP data and Composed of the SOCK_send() and send_in() functions.
*     The SOCK_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 SOCK_send(SOCKET s, u8 * buf, u16 len, u8 flag, u8 isTcp)
{
	xdata int ptr, size;
	xdata int ret;

	lan_enable();
	
	if (len <= 0)
	{
		ret=0;
	}
	else {
		ptr = 0;
		do {
			size = send_in(s, buf + ptr, len, flag, isTcp);
			if (size == -1)
			{
				ret = -1;
				goto EXIT_CASE;
			}
			len = len - size;
			ptr += size;
		} while ( len > 0);
		ret = ptr;
	}
EXIT_CASE:
	dv03_enable();
	return ptr;
}


/*
*********************************************************************************************************
* Provide the network target buffer address to application
* The network target buffer address is from send_in(,,,SEND_DELAY) 
*********************************************************************************************************
*/
void SOCK_get_netbuf_addr(u8* addr_h, u8* addr_l)
{
	*addr_h = bufaddr_h;
	*addr_l = bufaddr_l;

⌨️ 快捷键说明

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