Real time embedded FreeRTOS RSS feed 
Homepage FreeRTOS+ Products FreeRTOS Labs Support Forum Contact / Enquiries

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

[FreeRTOS+TCP API Reference]

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+TCP will run. If the microcontroller is big endian then ipconfigBYTE_ORDER must be set to pdFREERTOS_BIG_ENDIAN. If the microcontroller is little endian then ipconfigBYTE_ORDER must be set to pdFREERTOS_LITTLE_ENDIAN.

When ipconfigBYTE_ORDER is set to pdFREERTOS_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 pdFREERTOS_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 ]    [ Privacy ]    [ FreeRTOS Labs Sitemap ]    [ Main FreeRTOS Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.