Esempio n. 1
0
//---------------------------------------------------------------------------
//
// Function:    EplSdoUdpuSendData
//
// Description: send data using exisiting connection
//
//
//
// Parameters:  SdoConHandle_p  = connection handle
//              pSrcData_p      = pointer to data
//              dwDataSize_p    = number of databyte
//                                  -> without asend-header!!!
//
// Returns:     tEplKernel  = Errorcode
//
//
// State:
//
//---------------------------------------------------------------------------
tEplKernel PUBLIC EplSdoUdpuSendData(tEplSdoConHdl SdoConHandle_p,
				     tEplFrame * pSrcData_p, DWORD dwDataSize_p)
{
	tEplKernel Ret;
	int iError;
	unsigned int uiArray;
	struct sockaddr_in Addr;

	Ret = kEplSuccessful;

	uiArray = (SdoConHandle_p & ~EPL_SDO_ASY_HANDLE_MASK);
	if (uiArray >= EPL_SDO_MAX_CONNECTION_UDP) {
		Ret = kEplSdoUdpInvalidHdl;
		goto Exit;
	}
	//set message type
	AmiSetByteToLe(&pSrcData_p->m_le_bMessageType, 0x06);	// SDO
	// target node id (for Udp = 0)
	AmiSetByteToLe(&pSrcData_p->m_le_bDstNodeId, 0x00);
	// set source-nodeid (for Udp = 0)
	AmiSetByteToLe(&pSrcData_p->m_le_bSrcNodeId, 0x00);

	// calc size
	dwDataSize_p += EPL_ASND_HEADER_SIZE;

	// call sendto
	Addr.sin_family = AF_INET;
#if (TARGET_SYSTEM == _WIN32_)
	// enter  critical section for process function
	EnterCriticalSection(SdoUdpInstance_g.m_pCriticalSection);
#endif

	Addr.sin_port =
	    (unsigned short)SdoUdpInstance_g.m_aSdoAbsUdpConnection[uiArray].
	    m_uiPort;
	Addr.sin_addr.s_addr =
	    SdoUdpInstance_g.m_aSdoAbsUdpConnection[uiArray].m_ulIpAddr;

#if (TARGET_SYSTEM == _WIN32_)
	// leave critical section for process function
	LeaveCriticalSection(SdoUdpInstance_g.m_pCriticalSection);
#endif

	iError = sendto(SdoUdpInstance_g.m_UdpSocket,	// sockethandle
			(const char *)&pSrcData_p->m_le_bMessageType,	// data to send
			dwDataSize_p,	// number of bytes to send
			0,	// flags
			(struct sockaddr *)&Addr,	// target
			sizeof(struct sockaddr_in));	// sizeof targetadress
	if (iError < 0) {
		EPL_DBGLVL_SDO_TRACE1
		    ("EplSdoUdpuSendData: sendto() finished with %i\n", iError);
		Ret = kEplSdoUdpSendError;
		goto Exit;
	}

      Exit:
	return Ret;

}
//---------------------------------------------------------------------------
//
// Function:    EplSdoUdpuConfig
//
// Description: reconfigurate socket with new IP-Address
//              -> needed for NMT ResetConfiguration
//
// Parameters:  ulIpAddr_p      = IpAddress in platform byte order
//              uiPort_p        = port number in platform byte order
//
//
// Returns:     tEplKernel  = Errorcode
//
//
// State:
//
//---------------------------------------------------------------------------
tEplKernel EplSdoUdpuConfig(unsigned long ulIpAddr_p, unsigned int uiPort_p)
{
	tEplKernel Ret;
	struct sockaddr_in Addr;
	int iError;

	Ret = kEplSuccessful;

	if (uiPort_p == 0) {	// set UDP port to default port number
		uiPort_p = EPL_C_SDO_EPL_PORT;
	} else if (uiPort_p > 65535) {
		Ret = kEplSdoUdpSocketError;
		goto Exit;
	}

	if (SdoUdpInstance_g.m_ThreadHandle != 0) {	// listen thread was started

		// close old thread
		SdoUdpInstance_g.m_iTerminateThread = 1;
		/* kill_proc(SdoUdpInstance_g.m_ThreadHandle, SIGTERM, 1 ); */
		send_sig(SIGTERM, SdoUdpInstance_g.m_ThreadHandle, 1);
		wait_for_completion(&SdoUdpInstance_g.m_CompletionUdpThread);
		SdoUdpInstance_g.m_iTerminateThread = 0;
		SdoUdpInstance_g.m_ThreadHandle = 0;
	}

	if (SdoUdpInstance_g.m_UdpSocket != INVALID_SOCKET) {
		// close socket
		iError = closesocket(SdoUdpInstance_g.m_UdpSocket);
		SdoUdpInstance_g.m_UdpSocket = INVALID_SOCKET;
		if (iError != 0) {
			Ret = kEplSdoUdpSocketError;
			goto Exit;
		}
	}
	// create Socket
	SdoUdpInstance_g.m_UdpSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (SdoUdpInstance_g.m_UdpSocket == INVALID_SOCKET) {
		Ret = kEplSdoUdpNoSocket;
		EPL_DBGLVL_SDO_TRACE0("EplSdoUdpuConfig: socket() failed\n");
		goto Exit;
	}
	// bind socket
	Addr.sin_family = AF_INET;
	Addr.sin_port = htons((unsigned short)uiPort_p);
	Addr.sin_addr.s_addr = htonl(ulIpAddr_p);
	iError =
	    bind(SdoUdpInstance_g.m_UdpSocket, (struct sockaddr *)&Addr,
		 sizeof(Addr));
	if (iError < 0) {
		//iError = WSAGetLastError();
		EPL_DBGLVL_SDO_TRACE1
		    ("EplSdoUdpuConfig: bind() finished with %i\n", iError);
		Ret = kEplSdoUdpNoSocket;
		goto Exit;
	}
	// create Listen-Thread
	SdoUdpInstance_g.m_ThreadHandle =
	    kernel_thread(EplSdoUdpThread, &SdoUdpInstance_g, CLONE_KERNEL);
	if (SdoUdpInstance_g.m_ThreadHandle == 0) {
		Ret = kEplSdoUdpThreadError;
		goto Exit;
	}

      Exit:
	return Ret;

}