This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Convert int64_t to string

I'm using NCS 1.8.0 and nRF9160 DK, and I need to convert unix_time from int64_t to string, my problem is exactly what is reported in these cases:

devzone.nordicsemi.com/.../snprintf-int64_t-not-working
https://devzone.nordicsemi.com/f/nordic-q-a/83967/long-long-int-value-sprintf

My conversion attempts can be seen below, but in all of them I don't get the number in value_buf_time, but I get lu.

snprintf(value_buf_time, sizeof(value_buf_time), "%"PRIu64, unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%ld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%lld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
The output of all printk is:
 
Publicando o time: lu
 
In other cases the solution was to insert CONFIG_NEWLIB_LIBC_NANO=n. But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call. Is there another solution to make this conversion work?
Thank you.
  • Hello Gabriel,

    But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call.

    What kind of hard fault do you get?

    Is there another solution to make this conversion work?

    I have modified the hello_world sample as shown below and everything appears to work as expected so far:

    main.c

    #include <zephyr.h>
    #include <sys/printk.h>
    #include <stdio.h>
    #include <inttypes.h>
    #include <logging/log.h>
    
    #include <modem/lte_lc.h>
    
    LOG_MODULE_REGISTER(main);
    
    int main()
    {
        int64_t t = 1634617662563;
    	char buf[64];
    	int ret;
    
        printk("Hello World! %s\n", CONFIG_BOARD);
    	printk("ts:%"PRIi64"\n", t);
    
    	snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%" PRIi64
    		"}\n",
            t);    
    	printk("%s", buf);
    
    	ret = lte_lc_init_and_connect();
    	if(ret){
    		printk("Modem initialisation and network connection failed: %d\n",ret);
    	}
    
        return 0;
    }

    prj.conf

    # Logging
    CONFIG_LOG=y
    CONFIG_LTE_LINK_CONTROL_LOG_LEVEL_DBG=y
    
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    CONFIG_LTE_NETWORK_MODE_LTE_M_GPS=y
    
    # Other configurations
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_NANO=n
    CONFIG_MAIN_STACK_SIZE=2056
    CONFIG_RESET_ON_FATAL_ERROR=n

    Terminal output

    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Hello World! nrf9160dk_nrf9160
    ts:1634617662563
    {"ts":1634617662563}
    [00:00:00.619,171] <dbg> lte_lc.init_and_config: System mode (4) and preference (0) are already configured
    [00:00:01.404,083] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 2,"76C1","014ACE00",7
    ~
    [00:00:01.404,235] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 2
    [00:00:01.404,266] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7
    [00:00:01.444,641] <dbg> lte_lc.at_handler_cscon: +CSCON notification
    [00:00:02.536,224] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 1,"76C1","014ACE00",7,,~
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 1
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7

    Regards,

    Markus

  • Hello Markus,

    Sorry, I made a mistake and maybe the error is not related to lte_lc_init_and_connect(). I got confused because right after the connection I get the error, but in fact this is caused by the LTE_LINK_CONTROL=y, which allows the execution of the code only after the connection.

    To make it easier for you to reproduce the error, I've replicated this behavior in the mqtt_simple sample. To get unix_time I'm using the date_time library, and I need to add in prj.conf:

    CONFIG_DATE_TIME=y

    CONFIG_DATE_TIME_MODEM=y

    Adding the above library and settings doesn't cause a problem in the sample, but when adding:

    CONFIG_NEWLIB_LIBC_NANO=n

    I get the same error in the mqtt_simple sample that I get in my application:

    [00:00:00.220,550] <inf> mqtt_simple: LTE Link Connecting...
    [00:00:03,974,517] <err> os: ***** USAGE FAULT *****
    [00:00:03,974,548] <err> os: Stack overflow (context area not valid)
    [00:00:03,974,548] <err> os: r0/a1: 0x20018530 r1/a2: 0x2001fb48 r2/a3: 0x0002ded4
    [00:00:03,974,548] <err> os: r3/a4: 0x2001fc24 r12/ip: 0x00000060 r14/lr: 0x00025a45
    [00:00:03,974,578] <err> os: xpsr: 0x61000200
    [00:00:03,974,578] <err> os: Faulting instruction address (r15/pc): 0x00022884
    [00:00:03,974,578] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:00:03,974,578] <err> os: Current thread: 0x20019690 (unknown)
    [00:00:04,032,897] <err> fatal_error: Resetting system

    If you just add CONFIG_NEWLIB_LIBC_NANO=n in the mqtt_simple sample everything works. But to get the unix_time it is also necessary to add CONFIG_DATE_TIME=y and CONFIG_DATE_TIME_MODEM=y. So adding these 3 settings in the mqtt_simple example is possible to reproduce the error, I believe there is some incompatibility between these 3 settings!

  • Thanks a lot for your feedback, Gabriel!

    Thabet said:
    To make it easier for you to reproduce the error, I've replicated this behavior in the mqtt_simple sample. To get unix_time I'm using the date_time library, and I need to add in prj.conf:

    Could you share the lines you have added to main.c of the mqtt_simple sample as well?

    Regards,

    Markus

  • I ended up not adding anything to the main of the mqtt_simple sample, I was going to do that but I noticed that just adding the 3 settings to prj.conf the sample no longer works, the same way it did in my application.

    But in my application I add the following lines to main to get the unix time:

    #include <date_time.h>

    int64_t unix_time_ms;

    date_time_now(&unix_time_ms);

    printk("Measurement Instant %d: %lld \n",i, unix_time_ms);

    My output is:

    Measurement Instant 0: 1646661619367

    This works, I get the correct unix_time! But I can't turn this value into a string, when I try:

    snprintf(InstMed[i], sizeof(InstMed[i]), "%lld", unix_time_ms);

    printk("Instant of Measurement %d: %s \n",i, InstMed[i]);

    My output is:

    Measurement Instant 0: ld

    So to solve this, following the recommendations found in Devzone I must add CONFIG_NEWLIB_LIBC_NANO=n, so that it is possible to transform int64_t into a string, and when I add this the error occurs.

  • Hello Gabriel,

    Thabet said:
    So to solve this, following the recommendations found in Devzone I must add CONFIG_NEWLIB_LIBC_NANO=n, so that it is possible to transform int64_t into a string, and when I add this the error occurs.

    I’m still not able to reproduce the hard fault.

    main.c

    void main(void)
    {
    	int err;
    	uint32_t connect_attempt = 0;
    
    	char buf[64];
    	int64_t unix_time_ms;
    
    	LOG_INF("The MQTT simple sample started");
    
    	err = date_time_now(&unix_time_ms);
    	if(err){
    		printk("Couldn't get date time: %d\n",err);
    	}
    
    	printk("Measurement Instant: %lld \n", unix_time_ms);
    	snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%lld"
    		"}\n",
            unix_time_ms);    
    	printk("%s", buf);	
    
    #if defined(CONFIG_MQTT_LIB_TLS)

    Terminal output

    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Couldn't get date time: -61
    Measurement Instant: 42949768085 
    {"ts":42949768085}
    [00:00:00.631,042] <inf> mqtt_simple: The MQTT simple sample started
    [00:00:00.631,072] <wrn> date_time: Valid time not currently available
    [00:00:00.638,397] <inf> mqtt_simple: Disabling PSM and eDRX
    [00:00:00.645,355] <inf> mqtt_simple: LTE Link Connecting...
    [00:00:02.573,303] <inf> mqtt_simple: LTE Link Connected!
    [00:00:02.883,850] <inf> mqtt_simple: IPv4 Address found 137.135.83.217
    [00:00:02.884,338] <dbg> mqtt_simple.client_id_get: client_id = nrf-352656106118402
    [00:00:03.262,268] <inf> mqtt_simple: MQTT client connected
    [00:00:03.262,268] <inf> mqtt_simple: Subscribing to: my/subscribe/topic len 18
    [00:00:03.414,276] <inf> mqtt_simple: SUBACK packet id: 1234

    What is the current stack size value of the main thread? Have you tried to increase it?

    Regards,

    Markus

Related