printf(“%lxip”) in FreeRTOS+TCP
Hi!
I just saw that FreeRTOS+TCP uses a custom “%lxip” format specifier to print IP adresses.
This format is not supported by newlib, and I can’t use the recommended printf-stdarg.c from the FreeRTOS demos either (LGPL license :/ ).
So I would like to change that to a more portable “%d.%d.%d.%d”.
Has anyone done this already?
Would you accept a patch?
best regards,
Thomas
printf(“%lxip”) in FreeRTOS+TCP
Hi Thomas,
The format “%lxip” is supported by the module “printf-stdarg.c”, which was included in the original FreeRTOS+TCP distribution on freertos.org/tcp
If that module is not included, the statement will print as e.g. “C0A80101ip”, instead of “192.168.1.1”.
This format is not supported by newlib, and I can’t use the recommended printf-stdarg.c from the FreeRTOS demos either (LGPL license :/ ).I am not an expert on licensing, but I think that “printf-stdarg.c” has the same MIT-type licence as the FreeRTOS+TCP library.
printf(“%lxip”) in FreeRTOS+TCP
The idea is to use a macro like this:
~~~
define FreeRTOSntohliptoprintf_args(IP)
(int)(FreeRTOS_ntohl(IP) >> 24) & 255,
(int)(FreeRTOS_ntohl(IP) >> 16) & 255,
(int)(FreeRTOS_ntohl(IP) >> 8) & 255,
(int)(FreeRTOS_ntohl(IP) >> 0) & 255
~~~
and substitute:
~~~
FreeRTOSdebugprintf( ( “vDHCPProcess: acked %lxipn”, FreeRTOS_ntohl( xDHCPData.ulOfferedIPAddress ) ) );
~~~
with:
~~~
FreeRTOSprintf( ( “vDHCPProcess: acked %d.%d.%d.%dn”, FreeRTOSntohliptoprintfargs( xDHCPData.ulOfferedIPAddress ) ) );
~~~
I noticed that there are a lot of places, where ntohl() is missing, and sometimes it’s even mixed in the same printf() (see FreeRTOSTCPIP.c, line 1028):
~~~
FreeRTOSdebugprintf( ( “ARP for %lxip (using %lxip): rc=%d %02X:%02X:%02X %02X:%02X:%02Xn”,
pxSocket->u.xTCP.ulRemoteIP,
FreeRTOS_ntohl( ulRemoteIP ),
~~~
Is this by purpose? I would have expected that +TCP uses a consistent format internally, or at least uses different variable names to identify host and network order.
I think I will use two different macros then. I’m also not so clear what names to use.
Perhaps
FreeRTOSnetiptoprintfargs () for network order and a plain
FreeRTOSiptoprintf_args () for native order IPs?
printf(“%lxip”) in FreeRTOS+TCP
Unfortunately, it’s LGPL:
https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS-Plus/Demo/FreeRTOSPlusTCPMinimalWindows_Simulator/printf-stdarg.c