How to Send the thread network name through CoAP Response

Hi i was wondering how could i send, with a coap response, one's device network name and a, for exaple a temperature value, like this:

<device's network name>: <temperature value>

I tried this, but failed:

const char *myTemperatureJson = "23.32";
const char *buf[] = {CONFIG_OPENTHREAD_NETWORK_NAME,'   ', myTemperatureJson};
.
.
.
error = otMessageAppend(p_response, buf, strlen(buf));
Is there any other way to do this?
thanks
Parents
  • Oh, ok. So this has nothing to do with OpenThread. It has to do with the way you are trying to create a character string. 

    First of all, you need to change the 

    ' '
    with 
    " "
    and lastly, I don't think the line

    const char *buf[] = {CONFIG_BOARD, " ", myTemperature};

    works as you intend it to.

    Please have a look at what was printed from this snippet:

        const char *myTemperature = "23.32";
        const char *buf[] = {CONFIG_BOARD, " ", myTemperature};
    
        LOG_INF("buf[1]: %s", buf[0]);
        LOG_INF("buf[2]: %s", buf[1]);
        LOG_INF("buf[3]: %s", buf[2]);
        LOG_INF("location buf[0]: %p", buf[0]);
        LOG_INF("location buf[1]: %p", buf[1]);
        LOG_INF("location buf[2]: %p", buf[2]);

    I got the log:

    00> [00:00:00.004,608] <inf> peripheral_uart: buf[1]: nrf52840dk_nrf52840
    00> [00:00:00.004,608] <inf> peripheral_uart: buf[2]:  
    00> [00:00:00.004,638] <inf> peripheral_uart: buf[3]: 23.32
    00> [00:00:00.004,669] <inf> peripheral_uart: location buf[0]: 0x31414
    00> [00:00:00.004,669] <inf> peripheral_uart: location buf[1]: 0x31d11
    00> [00:00:00.004,699] <inf> peripheral_uart: location buf[2]: 0x3143e

    Note the location of the different buffers, which are also visible during a debug session:

    And if I read out the data in these locations, you can see that they are indeed not in the same place in flash:

    C:\Nordic_Semiconductor\SDKs\ncs\v2.1.2\zephyr\samples\hello_world\src>nrfjprog --memrd 0x31414 --n 32
    0x00031414: 3566726E 30343832 6E5F6B64 32356672   |nrf52840dk_nrf52|
    0x00031424: 00303438 5B667562 203A5D32 62007325   |840.buf[2]: %s.b|
    
    C:\Nordic_Semiconductor\SDKs\ncs\v2.1.2\zephyr\samples\hello_world\src>nrfjprog --memrd 0x31d11 --n 32
    0x00031D11: 20007C00 2563001B 5B313B33 316D001B   | .|.%c..[1;31m..|
    0x00031D21: 5B313B33 336D0065 72720077 726E0069   |[1;33m.err.wrn.i|
    
    C:\Nordic_Semiconductor\SDKs\ncs\v2.1.2\zephyr\samples\hello_world\src>nrfjprog --memrd 0x3143e --n 32
    0x0003143E: 3332332E 00326F6C 61636974 6E6F6220   |23.32.location b|
    0x0003144E: 6675305B 3A5D2520 00706F6C 61636974   |uf[0]: %p.locati|

    And please note that all of these strings are null terminated. 

    So as long as you are aware that these strings are not adjacent in flash, then you can use it like this. If not, you need to set up your buffer in another way. 

    BR,
    Edvin

  • i've implemented these lines in order to fix that problem, but appers an other one:
    char ot_name[] = CONFIG_OPENTHREAD_NETWORK_NAME;
    const char *mTemperature = "23.32";
    const char *buf[] = {ot_name," ", mTemperature};

    .

    .

    .

    Building coap_rec_myserver
    west build --build-dir c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\build c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver

    [0/16] Performing build step for '802154_rpmsg_subimage'
    ninja: no work to do.
    [2/14] Building C object CMakeFiles/app.dir/src/main.c.obj
    FAILED: CMakeFiles/app.dir/src/main.c.obj
    C:\ncs\toolchains\v2.1.1\opt\zephyr-sdk\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe -DKERNEL -DMBEDTLS_CONFIG_FILE=\"nrf-config.h\" -DMBEDTLS_USER_CONFIG_FILE=\"nrf-config-user.h\" -DNRF5340_XXAA_APPLICATION -DNRF_802154_ACK_TIMEOUT_ENABLED=1 -DNRF_802154_ECB_PRIORITY=-1 -DNRF_802154_ENCRYPTION_ENABLED=1 -DNRF_802154_IE_WRITER_ENABLED=1 -DNRF_802154_INTERNAL_RADIO_IRQ_HANDLING=0 -DNRF_802154_PENDING_EXTENDED_ADDRESSES="" -DNRF_802154_PENDING_SHORT_ADDRESSES="" -DNRF_802154_RX_BUFFERS=16 -DNRF_802154_SECURITY_WRITER_ENABLED=1 -DNRF_802154_SWI_PRIORITY=1 -DNRF_802154_TX_STARTED_NOTIFY_ENABLED=1 -DNRF_802154_USE_RAW_API=1 -DNRF_SKIP_FICR_NS_COPY_TO_RAM -DUSE_PARTITION_MANAGER=1 -D_FORTIFY_SOURCE=2 -D__LINUX_ERRNO_EXTENSIONS__ -D__PROGRAM_START -D__ZEPHYR__=1 -IC:/ncs/v2.1.1/zephyr/include/zephyr -IC:/ncs/v2.1.1/zephyr/include -Izephyr/include/generated -IC:/ncs/v2.1.1/zephyr/soc/arm/nordic_nrf/nrf53 -IC:/ncs/v2.1.1/zephyr/lib/libc/newlib/include -IC:/ncs/v2.1.1/zephyr/lib/util/fnmatch/. -IC:/ncs/v2.1.1/zephyr/soc/arm/nordic_nrf/common/. -IC:/ncs/v2.1.1/zephyr/subsys/net/lib/sockets/. -IC:/ncs/v2.1.1/zephyr/subsys/settings/include -IC:/ncs/v2.1.1/nrf/include -IC:/ncs/v2.1.1/zephyr/modules/openthread/platform/. -IC:/ncs/v2.1.1/modules/hal/cmsis/CMSIS/Core/Include -IC:/ncs/v2.1.1/modules/hal/nordic/nrfx -IC:/ncs/v2.1.1/modules/hal/nordic/nrfx/drivers/include -IC:/ncs/v2.1.1/modules/hal/nordic/nrfx/mdk -IC:/ncs/v2.1.1/zephyr/modules/hal_nordic/nrfx/. -Imodules/libmetal/libmetal/lib/include -IC:/ncs/v2.1.1/modules/lib/open-amp/open-amp/lib/include -IC:/ncs/v2.1.1/nrfxlib/openthread/include -IC:/ncs/v2.1.1/nrfxlib/nrf_802154/serialization/include -IC:/ncs/v2.1.1/nrfxlib/nrf_802154/serialization/include/platform -IC:/ncs/v2.1.1/nrfxlib/nrf_802154/serialization/include/serialization -IC:/ncs/v2.1.1/nrfxlib/nrf_802154/serialization/include/host -Imodules/nrfxlib/nrfxlib/nrf_security/src/include/generated -IC:/ncs/v2.1.1/nrfxlib/nrf_security/include -IC:/ncs/v2.1.1/nrfxlib/nrf_security/include/mbedtls -IC:/ncs/v2.1.1/nrfxlib/nrf_security/include/psa -IC:/ncs/v2.1.1/mbedtls/include -IC:/ncs/v2.1.1/mbedtls/include/mbedtls -IC:/ncs/v2.1.1/mbedtls/include/psa -IC:/ncs/v2.1.1/mbedtls/library -IC:/ncs/v2.1.1/nrfxlib/nrf_security/../crypto/nrf_oberon/include -IC:/ncs/v2.1.1/nrfxlib/nrf_security/../crypto/nrf_oberon/include/mbedtls -IC:/ncs/v2.1.1/nrfxlib/nrf_security/../crypto/nrf_oberon/include/psa -IC:/ncs/v2.1.1/nrfxlib/crypto/nrf_oberon/include/mbedtls -IC:/ncs/v2.1.1/nrfxlib/crypto/nrf_oberon/include -isystem C:/ncs/v2.1.1/zephyr/subsys/cpp/include -isystem C:/ncs/v2.1.1/nrfxlib/crypto/nrf_cc312_platform/include -Os -imacros C:/progetti/nexter/VS/esempi/server-send__client-receive/coap_rec_myserver/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m33 -mthumb -mabi=aapcs -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mfp16-format=ieee --sysroot=C:/ncs/toolchains/v2.1.1/opt/zephyr-sdk/arm-zephyr-eabi/arm-zephyr-eabi -imacros C:/ncs/v2.1.1/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=C:/progetti/nexter/VS/esempi/server-send__client-receive/coap_rec_myserver=CMAKE_SOURCE_DIR -fmacro-prefix-map=C:/ncs/v2.1.1/zephyr=ZEPHYR_BASE -fmacro-prefix-map=C:/ncs/v2.1.1=WEST_TOPDIR -ffunction-sections -fdata-sections -specs=nano.specs -std=c99 -MD -MT CMakeFiles/app.dir/src/main.c.obj -MF CMakeFiles\app.dir\src\main.c.obj.d -o CMakeFiles/app.dir/src/main.c.obj -c ../src/main.c
    c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\src\main.c:26:35: error: initializer element is not constant
    26 | const char *buf[] = {ot_name," ", mTemperature};
    | ^~~~~~~~~~~~
    c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\src\main.c:26:35: note: (near initialization for 'buf[2]')
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'c:\ncs\toolchains\v2.1.1\opt\bin\cmake.EXE' --build 'c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\build'

    how could i fix that?

  • These are not really Nordic specific questions, but really a general question on how to use strings in C. 

    simon8 said:
    c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\src\main.c:26:35: error: initializer element is not constant

    Since you are trying to write a constant char buffer, all the elements needs to be constants as well. In your case, the ot_name is not constant. 

    FYI: When you declare something as a constant, it will be stored in flash, rather than RAM. This means that it needs to be constant at all times. Therefore, if a variable is not constant meaning it will be stored in RAM), it can't store the resulting buffer in flash, because what happens if you at some point decides to change something in your ot_name[].

    Do you intentionally want to have all these as const? I guess the name may not change, but the temperature parameter looks like something you may want to change during runtime?

    I suggest you either remove all your "const"s, or you can look into sprintf() or memcpy()

    Best regards,

    Edvin

    BR,
    Edvin

Reply
  • These are not really Nordic specific questions, but really a general question on how to use strings in C. 

    simon8 said:
    c:\progetti\nexter\VS\esempi\server-send__client-receive\coap_rec_myserver\src\main.c:26:35: error: initializer element is not constant

    Since you are trying to write a constant char buffer, all the elements needs to be constants as well. In your case, the ot_name is not constant. 

    FYI: When you declare something as a constant, it will be stored in flash, rather than RAM. This means that it needs to be constant at all times. Therefore, if a variable is not constant meaning it will be stored in RAM), it can't store the resulting buffer in flash, because what happens if you at some point decides to change something in your ot_name[].

    Do you intentionally want to have all these as const? I guess the name may not change, but the temperature parameter looks like something you may want to change during runtime?

    I suggest you either remove all your "const"s, or you can look into sprintf() or memcpy()

    Best regards,

    Edvin

    BR,
    Edvin

Children
No Data
Related