how to use CBOR decoding with ncs/zephyr

Hallo Nordic

i am working with nrf52832 and 840, with ncs v2.3.0 (working on updating to v2.5.0)

i want to pass a cbor file which holds some data from cloud to edg device, the adg device is the nrf52 that works with ncs.

i want to be able to decode the cbor file by value, not as a whole struct (in order to maintain flexibility in the data fields in the file).

1. is there some example for using zcbor or some other cbor library embedded in ncs ?

2. is it possible to decode a cbor file value by value and not as a whole struct ? (if so how can this be done, or what is the existing API for that ? )

hope to read from you soon 

best regards

Ziv

Parents
  • Hi,

    1. zcbor is part of nRF Connect SDK (thorugh Zephyr) and is used in e.g. the Bluetooth: Central SMP Client sample.

    2. For what is possible with zcbor, please refer to the zcbor documentation.

    Regards,
    Terje

  • hi Tesc

    well i have done this code which works in general, i can parse according to balue and if some value is missing i will that number of decoded vals are less then i expect

        size_t                 decoded;
        zcbor_state_t          zsd[3];
        zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), file_data, bytes_read, 1);
        
        struct zcbor_map_decode_key_val test_attrs_upload_decode[] = {
            ZCBOR_MAP_DECODE_KEY_DECODER("one", zcbor_int_decode, &cfg_test.one),
            ZCBOR_MAP_DECODE_KEY_DECODER("ziv", zcbor_uint32_decode, &cfg_test.ziv),
            ZCBOR_MAP_DECODE_KEY_DECODER("biz", zcbor_int_decode, &cfg_test.biz),
        };
    
         rc = zcbor_map_decode_bulk(zsd, test_attrs_upload_decode,
                                   ARRAY_SIZE(test_attrs_upload_decode), &decoded) == 0;
    
        if ( rc ) 

    the problem is that i always get and error from zcbor_map_decode_bulk(),

    it fails to close the map if i get it right, i get rc = 1 (false) 

    i think it fails on this line in ncs

    return zcbor_map_end_decode(zsd) ? 0 : -EBADMSG;
    any idea why ? 
    i see it decodes the values right but return an error for not ending the map
    also i don't realy understand the issue with the zcbor_state_t array, why do i need several elements of it, if i put less then 3 i get an error and no decoding happens ???
    hope to read you soon
    best regards
    Ziv
  • Hi,

    It looks like the return value from zcbor_map_decode_bulk is different from the other zcbor functions, due to zcbor_map_decode_bulk being a Zephyr-specific function intended for internal use in MCUmgr. For details, see the zcbor_map_decode_bulk return code is unlike the other zcbor API functions issue for zephyr.

    Regarding the zcbor_state_t array, I am afraid this is a bit too far down into the internals for me to comment on at the moment.

    Regards,
    Terje

Reply Children
No Data
Related