本文整理汇总了C++中FreeRTOS_htons函数的典型用法代码示例。如果您正苦于以下问题:C++ FreeRTOS_htons函数的具体用法?C++ FreeRTOS_htons怎么用?C++ FreeRTOS_htons使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FreeRTOS_htons函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: prvGetPrivatePortNumber
static uint16_t prvGetPrivatePortNumber( void )
{
static uint16_t usNextPortToUse = socketAUTO_PORT_ALLOCATION_START_NUMBER - 1;
uint16_t usReturn;
/* Assign the next port in the range. */
taskENTER_CRITICAL();
{
usNextPortToUse++;
/* Has it overflowed? */
if( usNextPortToUse == 0U )
{
/* Don't go right back to the start of the dynamic/private port
range numbers as any persistent sockets are likely to have been
create first so the early port numbers may still be in use. */
usNextPortToUse = socketAUTO_PORT_ALLOCATION_RESET_NUMBER;
}
usReturn = FreeRTOS_htons( usNextPortToUse );
}
taskEXIT_CRITICAL();
return usReturn;
} /* Tested */
示例2: prvOpenTCPServerSocket
static Socket_t prvOpenTCPServerSocket( uint16_t usPort )
{
struct freertos_sockaddr xBindAddress;
Socket_t xSocket;
static const TickType_t xReceiveTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 20;
BaseType_t xReuseSocket = pdTRUE;
/* Attempt to open the socket. */
xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
/* Set a time out so accept() will just wait for a connection. */
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
/* Only one connection will be used at a time, so re-use the listening
socket as the connected socket. See SimpleTCPEchoServer.c for an example
that accepts multiple connections. */
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_REUSE_LISTEN_SOCKET, &xReuseSocket, sizeof( xReuseSocket ) );
/* NOTE: The CLI is a low bandwidth interface (typing characters is slow),
so the TCP window properties are left at their default. See
SimpleTCPEchoServer.c for an example of a higher throughput TCP server that
uses are larger RX and TX buffer. */
/* Bind the socket to the port that the client task will send to, then
listen for incoming connections. */
xBindAddress.sin_port = usPort;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
FreeRTOS_bind( xSocket, &xBindAddress, sizeof( xBindAddress ) );
FreeRTOS_listen( xSocket, xBacklog );
return xSocket;
}
示例3: nabto_read
ssize_t nabto_read( nabto_socket_t socket,
uint8_t* buf,
size_t len,
uint32_t* addr,
uint16_t* port )
{
int res;
struct freertos_sockaddr xAddress;
socklen_t xAddresslen = sizeof(xAddress);
memset( &xAddress, 0, sizeof( xAddress ) );
res = FreeRTOS_recvfrom( socket, buf, ( int ) len , 0, &xAddress, &xAddresslen );
if( res > 0 )
{
*addr = FreeRTOS_htonl( xAddress.sin_addr );
*port = FreeRTOS_htons( xAddress.sin_port );
}
else
{
res = 0;
}
return res;
}
示例4: pbpal_resolv_and_connect
enum pubnub_res pbpal_resolv_and_connect(pubnub_t *pb)
{
struct freertos_sockaddr addr;
PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
PUBNUB_ASSERT_OPT((pb->state == PBS_IDLE) || (pb->state == PBS_WAIT_DNS));
addr.sin_port = FreeRTOS_htons(HTTP_PORT);
addr.sin_addr = FreeRTOS_gethostbyname(PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN);
if (addr.sin_addr == 0) {
return PNR_CONNECT_FAILED;
}
pb->pal.socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
if (pb->pal.socket == SOCKET_INVALID) {
return PNR_CONNECT_FAILED;
}
if (FreeRTOS_connect(pb->pal.socket, &addr, sizeof addr) != 0) {
FreeRTOS_closesocket(pb->pal.socket);
pb->pal.socket = SOCKET_INVALID;
return PNR_CONNECT_FAILED;
}
{
TickType_t tmval = pdMS_TO_TICKS(pb->transaction_timeout_ms);
FreeRTOS_setsockopt(pb->pal.socket, 0, FREERTOS_SO_RCVTIMEO, &tmval, sizeof tmval);
}
return PNR_OK;
}
示例5: prvSimpleClientTask
static void prvSimpleClientTask( void *pvParameters )
{
xSocket_t xClientSocket;
struct freertos_sockaddr xDestinationAddress;
uint8_t cString[ 50 ];
portBASE_TYPE lReturned;
uint32_t ulCount = 0UL, ulIPAddress;
const uint32_t ulLoopsPerSocket = 10UL;
const portTickType x150ms = 150UL / portTICK_RATE_MS;
/* Remove compiler warning about unused parameters. */
( void ) pvParameters;
/* It is assumed that this task is not created until the network is up,
so the IP address can be obtained immediately. store the IP address being
used in ulIPAddress. This is done so the socket can send to a different
port on the same IP address. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
/* This test sends to itself, so data sent from here is received by a server
socket on the same IP address. Setup the freertos_sockaddr structure with
this nodes IP address, and the port number being sent to. The strange
casting is to try and remove compiler warnings on 32 bit machines. */
xDestinationAddress.sin_addr = ulIPAddress;
xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
for( ;; )
{
/* Create the socket. */
xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
/* The count is used to differentiate between different messages sent to
the server, and to break out of the do while loop below. */
ulCount = 0UL;
do
{
/* Create the string that is sent to the server. */
sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
/* Send the string to the socket. ulFlags is set to 0, so the zero
copy option is not selected. That means the data from cString[] is
copied into a network buffer inside FreeRTOS_sendto(), and cString[]
can be reused as soon as FreeRTOS_sendto() has returned. */
lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
ulCount++;
} while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
FreeRTOS_closesocket( xClientSocket );
/* A short delay to prevent the messages printed by the server task
scrolling off the screen too quickly, and to prevent reduce the network
loading. */
vTaskDelay( x150ms );
}
}
示例6: nabto_init_socket
bool nabto_init_socket( uint32_t localAddr, uint16_t* localPort, nabto_socket_t* socketDescriptor )
{
int to = 0;
struct freertos_sockaddr xAddress, *pxAddressToUse;
bool bReturn = true;
if (MAX_SOCKETS <= nSockets)
{
bReturn = false;
}
else
{
*socketDescriptor = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
if( NULL == *socketDescriptor )
{
bReturn = false;
}
else
{
memset( &xAddress, 0, sizeof( xAddress ) );
xAddress.sin_addr = FreeRTOS_htonl( localAddr );
xAddress.sin_port = FreeRTOS_htons( *localPort );
pxAddressToUse = &xAddress;
if( 0 > FreeRTOS_bind(*socketDescriptor, pxAddressToUse, sizeof( xAddress ) ) )
{
FreeRTOS_closesocket( *socketDescriptor );
bReturn = false;
}
else
{
/* Set receive time out to 0. Timeouts are performed using a
select() call. */
FreeRTOS_setsockopt( *socketDescriptor, 0, FREERTOS_SO_RCVTIMEO, &to, sizeof( to ) );
activeSockets[nSockets++] = *socketDescriptor;
}
*localPort = FreeRTOS_htons( xAddress.sin_port );
}
}
return bReturn;
}
示例7: vStartNTPTask
void vStartNTPTask( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
{
/* The only public function in this module: start a task to contact
some NTP server. */
if( xNTPTaskhandle != NULL )
{
switch( xStatus )
{
case EStatusPause:
xStatus = EStatusAsking;
vSignalTask();
break;
case EStatusLookup:
FreeRTOS_printf( ( "NTP looking up server\n" ) );
break;
case EStatusAsking:
FreeRTOS_printf( ( "NTP still asking\n" ) );
break;
case EStatusFailed:
FreeRTOS_printf( ( "NTP failed somehow\n" ) );
ulIPAddressFound = 0ul;
xStatus = EStatusLookup;
vSignalTask();
break;
}
}
else
{
xUDPSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
if( xUDPSocket != NULL )
{
struct freertos_sockaddr xAddress;
#if( ipconfigUSE_CALLBACKS != 0 )
BaseType_t xReceiveTimeOut = pdMS_TO_TICKS( 0 );
#else
BaseType_t xReceiveTimeOut = pdMS_TO_TICKS( 5000 );
#endif
xAddress.sin_addr = 0ul;
xAddress.sin_port = FreeRTOS_htons( NTP_PORT );
FreeRTOS_bind( xUDPSocket, &xAddress, sizeof( xAddress ) );
FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
xTaskCreate( prvNTPTask, /* The function that implements the task. */
( const char * ) "NTP client", /* Just a text name for the task to aid debugging. */
usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
NULL, /* The task parameter, not used in this case. */
uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
&xNTPTaskhandle ); /* The task handle. */
}
else
{
FreeRTOS_printf( ( "Creating socket failed\n" ) );
}
}
}
示例8: prvMultipleSocketTxTask
static void prvMultipleSocketTxTask( void *pvParameters )
{
uint32_t ulTxValue = 0;
struct freertos_sockaddr xDestinationAddress;
uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;
xSocket_t xTxSocket;
const TickType_t xShortDelay = 100 / portTICK_RATE_MS, xSendBlockTime = 500 / portTICK_RATE_MS;
srand( ( unsigned int ) &xPortNumber );
/* The first destination port number is passed in as the task parameter.
Other destination port numbers used are consecutive from this. */
ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;
/* Create the socket used to send to the sockets created by the Rx task.
Let the IP stack select a port to bind to. */
xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );
/* The Rx and Tx tasks execute at the same priority so it is possible that
the Tx task will fill up the send queue - set a Tx block time to ensure
flow control is managed if this happens. */
FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );
/* It is assumed that this task is not created until the network is up,
so the IP address can be obtained immediately. Store the IP address being
used in ulIPAddress. This is done so the socket can send to a different
port on the same IP address. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
/* This test sends to itself, so data sent from here is received by a server
socket on the same IP address. Setup the freertos_sockaddr structure with
this nodes IP address. */
xDestinationAddress.sin_addr = ulIPAddress;
/* Block for a short time to ensure the task implemented by the
prvMultipleSocketRxTask() function has finished creating the Rx sockets. */
vTaskDelay( xShortDelay );
for( ;; )
{
/* Pseudo randomly select the destination port number from the range of
valid destination port numbers. */
xPortNumber = rand() % selNUMBER_OF_SOCKETS;
xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );
xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
/* Send an incrementing value. */
FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
ulTxValue++;
/* Delay here because in the Windows simulator the MAC interrupt
simulator delays, so network trafic cannot be received any faster
than this. */
vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );
}
}
示例9: prvSimpleZeroCopyServerTask
static void prvSimpleZeroCopyServerTask( void *pvParameters )
{
int32_t lBytes;
uint8_t *pucUDPPayloadBuffer;
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
xSocket_t xListeningSocket;
/* Just to prevent compiler warnings. */
( void ) pvParameters;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* This test receives data sent from a different port on the same IP address.
Obtain the nodes IP address. Configure the freertos_sockaddr structure with
the address being bound to. The strange casting is to try and remove
compiler warnings on 32 bit machines. Note that this task is only created
after the network is up, so the IP address is valid here. */
FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
xBindAddress.sin_addr = ulIPAddress;
xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
/* Bind the socket to the port that the client task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
for( ;; )
{
/* Receive data on the socket. ulFlags has the zero copy bit set
(FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
received data should be passed out to this task using the second
parameter to the FreeRTOS_recvfrom() call. When this is done the
IP stack is no longer responsible for releasing the buffer, and
the task *must* return the buffer to the stack when it is no longer
needed. By default the block time is portMAX_DELAY. */
lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
/* It is expected to receive one more byte than the string length as
the NULL terminator is also transmitted. */
configASSERT( lBytes == ( ( portBASE_TYPE ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
/* Print the received characters. */
if( lBytes > 0 )
{
vOutputString( ( char * ) pucUDPPayloadBuffer );
}
if( lBytes >= 0 )
{
/* The buffer *must* be freed once it is no longer needed. */
FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
}
}
}
示例10: prvReplyDNSMessage
static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )
{
UDPPacket_t *pxUDPPacket;
IPHeader_t *pxIPHeader;
UDPHeader_t *pxUDPHeader;
pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
pxIPHeader = &pxUDPPacket->xIPHeader;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
/* HT: started using defines like 'ipSIZE_OF_xxx' */
pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );
/* HT:endian: should not be translated, copying from packet to packet */
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;
pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
usPacketIdentifier++;
pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );
vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );
#if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
{
/* calculate the IP header checksum */
pxIPHeader->usHeaderChecksum = 0x00;
pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
/* calculate the UDP checksum for outgoing package */
usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pdTRUE );
}
#endif
/* Important: tell NIC driver how many bytes must be sent */
pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );
/* This function will fill in the eth addresses and send the packet */
vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
}
示例11: prvSimpleServerTask
static void prvSimpleServerTask( void *pvParameters )
{
long lBytes;
uint8_t cReceivedString[ 60 ];
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient );
xSocket_t xListeningSocket;
/* Just to prevent compiler warnings. */
( void ) pvParameters;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* This test receives data sent from a different port on the same IP
address. Configure the freertos_sockaddr structure with the address being
bound to. The strange casting is to try and remove compiler warnings on 32
bit machines. Note that this task is only created after the network is up,
so the IP address is valid here. */
xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
/* Bind the socket to the port that the client task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
for( ;; )
{
/* Zero out the receive array so there is NULL at the end of the string
when it is printed out. */
memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
/* Receive data on the socket. ulFlags is zero, so the zero copy option
is not set and the received data is copied into the buffer pointed to by
cReceivedString. By default the block time is portMAX_DELAY.
xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
appropriately in case future versions do use it. */
lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
/* Print the received characters. */
if( lBytes > 0 )
{
vOutputString( ( char * ) cReceivedString );
}
/* Error check. */
configASSERT( lBytes == ( portBASE_TYPE ) strlen( ( const char * ) cReceivedString ) );
}
}
示例12: nabto_write
ssize_t nabto_write( nabto_socket_t socket,
const uint8_t* buf,
size_t len,
uint32_t addr,
uint16_t port )
{
int res;
struct freertos_sockaddr xAddress;
memset( &xAddress, 0, sizeof( xAddress ) );
xAddress.sin_addr = FreeRTOS_htonl( addr );
xAddress.sin_port = FreeRTOS_htons( port );
res = FreeRTOS_sendto( socket, buf, ( int )len, 0, ( struct freertos_sockaddr * ) &xAddress, sizeof( xAddress ) );
return res;
}
示例13: prvConnectionListeningTask
static void prvConnectionListeningTask( void *pvParameters )
{
struct freertos_sockaddr xClient, xBindAddress;
xSocket_t xListeningSocket, xConnectedSocket;
socklen_t xSize = sizeof( xClient );
static const TickType_t xReceiveTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 20;
xWinProperties_t xWinProps;
/* Just to prevent compiler warnings. */
( void ) pvParameters;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* Set a time out so accept() will just wait for a connection. */
FreeRTOS_setsockopt( xListeningSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
/* Fill in the buffer and window sizes that will be used by the socket. */
xWinProps.lTxBufSize = 6 * ipconfigTCP_MSS;
xWinProps.lTxWinSize = 3;
xWinProps.lRxBufSize = 6 * ipconfigTCP_MSS;
xWinProps.lRxWinSize = 3;
/* Set the window and buffer sizes. */
FreeRTOS_setsockopt( xListeningSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
/* Bind the socket to the port that the client task will send to, then
listen for incoming connections. */
xBindAddress.sin_port = tcpechoPORT_NUMBER;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
FreeRTOS_listen( xListeningSocket, xBacklog );
/* Create the clients that will connect to the listening socket. */
prvCreateWindowsThreadClients();
for( ;; )
{
/* Wait for a client to connect. */
xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );
/* Spawn a task to handle the connection. */
xTaskCreate( prvServerConnectionInstance, "EchoServer", usUsedStackSize, ( void * ) xConnectedSocket, tskIDLE_PRIORITY, NULL );
}
}
示例14: prvOpenUDPServerSocket
static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )
{
struct freertos_sockaddr xServer;
xSocket_t xSocket = FREERTOS_INVALID_SOCKET;
xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
if( xSocket != FREERTOS_INVALID_SOCKET) {
/* Zero out the server structure. */
memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );
/* Set family and port. */
xServer.sin_port = FreeRTOS_htons( usPort );
/* Bind the address to the socket. */
if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 ) {
FreeRTOS_closesocket( xSocket );
xSocket = FREERTOS_INVALID_SOCKET;
}
}
return xSocket;
}
示例15: prvNTPTask
static void prvNTPTask( void *pvParameters )
{
BaseType_t xServerIndex = 3;
struct freertos_sockaddr xAddress;
#if( ipconfigUSE_CALLBACKS != 0 )
F_TCP_UDP_Handler_t xHandler;
#endif /* ipconfigUSE_CALLBACKS != 0 */
xStatus = EStatusLookup;
#if( ipconfigSOCKET_HAS_USER_SEMAPHORE != 0 ) || ( ipconfigUSE_CALLBACKS != 0 )
{
xNTPWakeupSem = xSemaphoreCreateBinary();
}
#endif
#if( ipconfigUSE_CALLBACKS != 0 )
{
memset( &xHandler, '\0', sizeof ( xHandler ) );
xHandler.pOnUdpReceive = xOnUdpReceive;
FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_UDP_RECV_HANDLER, ( void * ) &xHandler, sizeof( xHandler ) );
}
#endif
#if( ipconfigSOCKET_HAS_USER_SEMAPHORE != 0 )
{
FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_SET_SEMAPHORE, ( void * ) &xNTPWakeupSem, sizeof( xNTPWakeupSem ) );
}
#endif
for( ; ; )
{
switch( xStatus )
{
case EStatusLookup:
if( ( ulIPAddressFound == 0ul ) || ( ulIPAddressFound == ~0ul ) )
{
if( ++xServerIndex == sizeof pcTimeServers / sizeof pcTimeServers[ 0 ] )
{
xServerIndex = 0;
}
FreeRTOS_printf( ( "Looking up server '%s'\n", pcTimeServers[ xServerIndex ] ) );
FreeRTOS_gethostbyname_a( pcTimeServers[ xServerIndex ], vDNS_callback, (void *)NULL, 1200 );
}
else
{
xStatus = EStatusAsking;
}
break;
case EStatusAsking:
{
char pcBuf[16];
prvNTPPacketInit( );
xAddress.sin_addr = ulIPAddressFound;
xAddress.sin_port = FreeRTOS_htons( NTP_PORT );
FreeRTOS_inet_ntoa( xAddress.sin_addr, pcBuf );
FreeRTOS_printf( ( "Sending UDP message to %s:%u\n",
pcBuf,
FreeRTOS_ntohs( xAddress.sin_port ) ) );
uxSendTime = xTaskGetTickCount( );
FreeRTOS_sendto( xUDPSocket, ( void * )&xNTPPacket, sizeof( xNTPPacket ), 0, &xAddress, sizeof( xAddress ) );
}
break;
case EStatusPause:
break;
case EStatusFailed:
break;
}
#if( ipconfigUSE_CALLBACKS != 0 )
{
xSemaphoreTake( xNTPWakeupSem, 5000 );
}
#else
{
uint32_t xAddressSize;
BaseType_t xReturned;
xAddressSize = sizeof( xAddress );
xReturned = FreeRTOS_recvfrom( xUDPSocket, ( void * ) cRecvBuffer, sizeof( cRecvBuffer ), 0, &xAddress, &xAddressSize );
switch( xReturned )
{
case 0:
case -pdFREERTOS_ERRNO_EAGAIN:
case -pdFREERTOS_ERRNO_EINTR:
break;
default:
if( xReturned < sizeof( xNTPPacket ) )
{
FreeRTOS_printf( ( "FreeRTOS_recvfrom: returns %ld\n", xReturned ) );
}
else
{
prvReadTime( ( struct SNtpPacket *)cRecvBuffer );
if( xStatus != EStatusPause )
{
xStatus = EStatusPause;
}
//.........这里部分代码省略.........