SMP command using ZCBOR command line and CDDL file

Hello !

We would like to implement a SMP client code as described in https://docs.zephyrproject.org/latest/services/device_mgmt/smp_protocol.html.
In Zephyr we can use the zcbor lib that you implemented: https://github.com/NordicSemiconductor/zcbor/blob/main/ARCHITECTURE.md  

zcbor -c cbor.cddl --no-prelude  --default-max-qty 2 code -t MultiList --encode --output-cmake imageslot

Image = (
  "slot" => uint,
  "version" => bstr,
  ? "bootable" => bool,
  ? "pending" => bool,
  ? "confirmed" => bool,
  ? "active" => bool,
  ? "permanent" => bool,
)

MultiList = {
  "images" => [+{
    "image" => uint,
    Image
  }]
}

I am struggling writing the proper cddl message so that the decoding code is generated by the zcbor command line.
The one above adds the "image" tstr. which is not expected.

bf 66 69 6d 61 67 65 73  9f bf 64 73 6c 6f 74 00 |.fimages ..dslot.
67 76 65 72 73 69 6f 6e  65 30 2e 30 2e 33 64 68 |gversion e0.0.3dh
61 73 68 58 20 90 70 cc  23 30 17 54 06 a8 ce 15 |ashX .p. #0.T....
28 63 3e 9c f4 19 a7 4a  ec 7e ca ce 10 24 c4 af |(c>....J .~...$..
48 68 e1 2c c7 68 62 6f  6f 74 61 62 6c 65 f5 67 |Hh.,.hbo otable.g
70 65 6e 64 69 6e 67 f4  69 63 6f 6e 66 69 72 6d |pending. iconfirm
65 64 f5 66 61 63 74 69  76 65 f5 69 70 65 72 6d |ed.facti ve.iperm
61 6e 65 6e 74 f4 ff ff  6b 73 70 6c 69 74 53 74 |anent... ksplitSt
61 74 75 73 00 ff                                |atus..

Do you have an example on a CDDL code for doing an SMP upload request or a get state request?

?

Thanks a lot!

  • Hello,

    I have asked the developer, and I will let you know when he replies.

  • Can you please elaborate your question? I have talked to two developers now, and they are both confused about what you are asking.

  • The ZCBOR repository contains a command line tool to generate code for encoding/decoding a message in C.
    It can be done using the following command line (deduced from the README)

    zcbor -c cbor.cddl --no-prelude  --default-max-qty 2 code -t MultiList --encode --output-cmake imageslot

    where the cbor.cddl is the description of the message that can be encoded/decoded. It gives multiple files:

    - imageslot_decode.c: to decode a buffer to a structure
    - imageslot_encode.c: to encode a structure and get the corresponding buffer
    - imageslot_types.h: containing the structures in C of the corresponding .cddl description

    I want to implement an SMP client to get the image state and upload an image on a BLE device as described in https://docs.zephyrproject.org/latest/services/device_mgmt/smp_protocol.html 

    When I generate the code using the .cddl I shared, it adds the field "image":

    	bool tmp_result = (((zcbor_map_start_decode(state) && (((((zcbor_tstr_expect(state, ((tmp_str.value = (uint8_t *)"image", tmp_str.len = sizeof("image") - 1, &tmp_str)))))
    	&& (zcbor_uint32_decode(state, (&(*result)._map_image))))
    	&& ((decode_Image(state, (&(*result)._images_map__Image))))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_map_end_decode(state))));
    
    which is not expected in the payload: where I have only the fields "images", "slot", "version", "hash"
    bf 66 69 6d 61 67 65 73  9f bf 64 73 6c 6f 74 00 |.fimages ..dslot.
    67 76 65 72 73 69 6f 6e  65 30 2e 30 2e 33 64 68 |gversion e0.0.3dh
    61 73 68 58 20 90 70 cc  23 30 17 54 06 a8 ce 15 |ashX .p. #0.T....
    28 63 3e 9c f4 19 a7 4a  ec 7e ca ce 10 24 c4 af |(c>....J .~...$..
    48 68 e1 2c c7 68 62 6f  6f 74 61 62 6c 65 f5 67 |Hh.,.hbo otable.g
    70 65 6e 64 69 6e 67 f4  69 63 6f 6e 66 69 72 6d |pending. iconfirm
    65 64 f5 66 61 63 74 69  76 65 f5 69 70 65 72 6d |ed.facti ve.iperm
    61 6e 65 6e 74 f4 ff ff  6b 73 70 6c 69 74 53 74 |anent... ksplitSt
    61 74 75 73 00 ff                                |atus..
    Is there a .cddl file you can share that would generate the code to decode the SMP request/response (CBOR) ?
    If not, how can I write such file so that I can generate the C code using ZCBOR CLI to decode/encode SMP messages?
    Thanks
  • We could figure out the proper cddl to use:

    Image = {
      ? "image" => uint,
      "slot" => uint,
      "version" => tstr,
      ? "hash"  => bstr,
      ? "bootable" => bool,
      ? "pending" => bool,
      ? "confirmed" => bool,
      ? "active" => bool,
      ? "permanent" => bool,
    }
    
    Images = {
      "images" => [0*2 Image],
      ? "splitStatus" => uint
    }
    

  • Hello Depraz,

    Did you manage to make this work ? How did you register a specific SMP group ID ? I'm trying to do something very similar : use a cddl file to generate the encode / decode functions, then send data on SMP over BLE with a specific group ID. I would like to read and write data in CBOR over SMP, for now I focus on the sending only.

    I registered a new mcu mgr handler, with read and write callbacks with the following macro :

    MCUMGR_HANDLER_DEFINE(my_handler, my_handler_init);

    and I'm very close to encode correctly the data in CBOR before sending them on BLE, but I'm facing the issue that in the Zephyr file smp.c, the characters to start a map and end a map are automatically added : 

    	if (handler_fn) {
    		*handler_found = true;
    
            /***** HERE, this add a character to start a map in CBOR *****/
    		zcbor_map_start_encode(cbuf->writer->zs, CONFIG_MGMT_MAX_MAIN_MAP_ENTRIES);
    
            /***** my custom function, where I encore data in CBOR as a map *****/
    		MGMT_CTXT_SET_RC_RSN(cbuf, NULL);
    		rc = handler_fn(cbuf);
    
    		/* End response payload. */
            /***** HERE, this add a character to end a map in CBOR *****/
    		if (!zcbor_map_end_encode(cbuf->writer->zs, CONFIG_MGMT_MAX_MAIN_MAP_ENTRIES) &&
    		    rc == 0) {
    			rc = MGMT_ERR_EMSGSIZE;
    		}

    therefore, two consecutive "map start" (0xBF) & "map end" (0xFF) are sent on the BLE link, and the data is not correct according to the playground cbor.me

    Since the nRF Connect SDK spec says : The payload for above groups, except for user groups (64 and above) is always CBOR encoded. The group 64, and above can define their own scheme for data communication. I think this should be possible to use the SMP over BLE link without having the mcumgr adding any CBOR encoding, but I don't know how to do that.

    I'm interested to know how you did to work around this issue !

Related