Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Homepage FreeRTOS+ Products FreeRTOS Labs Integration Services Contact / Enquiries

FreeRTOS_htons(), FreeRTOS_ntohs(), FreeRTOS_htonl() & FreeRTOS_ntohl()

[FreeRTOS Embedded IP Stack API]

FreeRTOS_sockets.h
uint16_t FreeRTOS_htons( uint16_t usValueToSwap );
uint16_t FreeRTOS_ntohs( uint16_t usValueToSwap );

uint32_t FreeRTOS_htonl( uint32_t ulValueToSwap );
uint32_t FreeRTOS_ntohl( uint32_t ulValueToSwap );
		

The Byte Order and Endian section of the Embedded Networking Basics and Glossary page provides an explanation of byte order considerations in IP networks.

The definition of ipconfigBYTE_ORDER in FreeRTOSIPConfig.h must be correct for the microcontroller on which FreeRTOS+UDP will run. If the microcontroller is big endian then ipconfigBYTE_ORDER must be set to FREERTOS_BIG_ENDIAN. If the microcontroller is little endian then ipconfigBYTE_ORDER must be set to FREERTOS_LITTLE_ENDIAN.

When ipconfigBYTE_ORDER is set to FREERTOS_LITTLE_ENDIAN:

  • FreeRTOS_htons and FreeRTOS_ntohs() return the value of their 16-bit parameter with the high and low bytes swapped. For example, if the usValueToSwap parameter is 0x1122, then both macros return 0x2211.

  • FreeRTOS_htonl and FreeRTOS_ntohl() return the value of their 32-bit parameter with the byte order reversed. For example, if the ulValueToSwap parameter is 0x11223344, then both macros return 0x44332211.

If the microcontroller is big endian (and therefore ipconfigBYTE_ORDER set to FREERTOS_BIG_ENDIAN) then the byte order of the microcontroller and the byte order of the network already match, and all four byte swapping macros are defined to have no effect.

Byte swapping macros are primarily used when specifying the IP address and port number that make up a socket address.

Example usage:

The examples on the FreeRTOS_socket(), FreeRTOS_inet_addr() FreeRTOS_sendto() documentation pages demonstrate the use of FreeRTOS_htons().

The example on the FreeRTOS_recvfrom() documentation page demonstrates the use of FreeRTOS_ntohs().


[ Back to the top ]    [ About FreeRTOS ]    [ FreeRTOS+ Sitemap ]    [ Main FreeRTOS Sitemap ]    [ ]


Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.