zcbor map of empty string not returning what i expect.

This question relates to using zcbor.

Basically I have the following.

void smp_req_image_state() {
    uint8_t payload[128];
    ZCBOR_STATE_E(encoding_state, 0, payload, sizeof(payload), 0);
    // int err;
    bool r                 = true;
    smp_frm_t smp_msg      = {0};
    smp_msg.hdr.smp_cmd_id = GRP_IMG_STATE;
    smp_msg.hdr.smp_grp    = ntohs(MGMT_GRP_ID_IMAGE);
    smp_msg.hdr.smp_op     = SMP_OP_RD;
    smp_msg.hdr.smp_seq    = 1; // because...why not.
    r                      = r && zcbor_map_start_encode(encoding_state, 1);
    r                      = r && zcbor_map_end_encode(encoding_state, 1);
    // smp_msg.hdr.smp_len    = ntohs(512);
    smp_msg.hdr.smp_len    = ntohs(encoding_state->payload - payload);
    uint8_t encoded_lenth  = encoding_state->payload - payload;
    printf("Encoded length is %i\n", encoded_lenth);
    smp_msg.data = malloc(/*sizeof(uint8_t) * */ encoded_lenth);
    memcpy(smp_msg.data, payload, encoded_lenth);
    uint8_t total_size = sizeof(smp_msg.hdr) + encoded_lenth;
    uint8_t *my_ptr    = (uint8_t *)&smp_msg;
    printf("Size of smp 'FRAME' = %i\n", total_size);
    printf("The contents of FRAME - ");
    for (int i = 0; i < total_size; i++) {
        printf("%.2x, ", my_ptr[i]);
    }
    printf("\n");
}

I got 
The contents of FRAME - 00, 00, 00, 02, 00, 01, 01, 00, b0, 06, 

But I was expecting 00, 00, 00, 01, 00, 01, 01, 00, a0.

I've played arround with -DZCBOR_CANONICAL, but that just fails with if (!zcbor_new_backup(state, 0)).

I'm basing the expected on what I've seen come out of mcumgr and smpmgr when i had debug handles on the cli scripts.

The reason I'm using and wraping my messages is because I'm using a zync fpga with FREERTOS app to control the mcuboot on a nordic device.

Parents
  • Hello,

    I am not sure about this, but the issue might be related to memory handling in the code. Could you verify whether sizeof(smp_msg.hdr) is correct and does not overrun smp_msg.data? Maybe try printing sizeof(smp_msg.hdr) and check if it matches the expected size.

    Kind regards,
    Abhijith

  • This particular forum thread has been raised regarding the zcbor code provided by nordic semi. It has nothing to do with the smp_hdr part. The smp_hdr data length is 2 in my instance because the zcbor map of nothing is represented by BF FF. RATHER than A0

    However I've seen other tools generating A0.


    This corresponds to ZCBOR_MAJOR_TYPE_MAP (5) existing on the top 3 bits and length 0 for the rest thus becoming 0xA0.

    However in my code
    r = r && zcbor_map_start_encode(encoding_state, 1);
    r = r && zcbor_map_end_encode(encoding_state, 1);

    Generates ZCBOR_MAJOR_TYPE_MAP (5) existing on the top 3 bits, with a length b'11111' thus becoming 0xBF.
    When the length is indefinite it needs a break which is the 0xFF

    I guess in the simplest terms what I'm asking is can i control the length field?
     

Reply
  • This particular forum thread has been raised regarding the zcbor code provided by nordic semi. It has nothing to do with the smp_hdr part. The smp_hdr data length is 2 in my instance because the zcbor map of nothing is represented by BF FF. RATHER than A0

    However I've seen other tools generating A0.


    This corresponds to ZCBOR_MAJOR_TYPE_MAP (5) existing on the top 3 bits and length 0 for the rest thus becoming 0xA0.

    However in my code
    r = r && zcbor_map_start_encode(encoding_state, 1);
    r = r && zcbor_map_end_encode(encoding_state, 1);

    Generates ZCBOR_MAJOR_TYPE_MAP (5) existing on the top 3 bits, with a length b'11111' thus becoming 0xBF.
    When the length is indefinite it needs a break which is the 0xFF

    I guess in the simplest terms what I'm asking is can i control the length field?
     

Children
No Data
Related