This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Missing documentation on TinyCbor for nrfConnect SDK 1.3.0

Hi,

I am trying to use Cbor library provided the SDK version 1.3.0, I have added the module in my prj.conf file, however the documentation (at the top of cborencoder.c)  that describes the cbor_encoder_init function has different parameters on its usage of what the source code has.

From the source code we have:

void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, int flags)
{
    encoder->writer = writer;
    encoder->added = 0;
    encoder->flags = flags;
}

But the documentation at the top of cborencoder.c has the following for when ilustrating its usage:

cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);

Does anyone knows how this is supposed to be used?, is there any documentation anywhere on how this is used?

Any help would be appreciated.

  • So, apparently there was some updates in zephyr from Nordic team which were taken from here:

    https://github.com/apache/mynewt-core

    There are some example usage in 

    https://github.com/apache/mynewt-core/blob/6662f0f37aaf66d8a373594fc97e8eb264a74a5b/net/oic/src/api/oc_rep.c

    Just beware of small changes in the API naming, the related files from which the writer/reader API is located in cbor_buf_reader.c cbor_buf_writer.c

    Updates on the documentation on API usage in top of encoder.c files are still needed.

  • Ok, so I did a quick test of the API and all seems to be in place and runs without any error message, however when encoding an int I do not get the same value back. I tried with different int values, but I always get 0 as the decoded value. Sure I am doing something wrong, but at this point I do not know what it is...

    This is my testing code... which reaches the end with the message "It works!, the integer is 0" regardless of the value assigned to valueToEncode, any help would be appreciated  : )

    struct cbor_buf_writer buf_writer;
    struct cbor_buf_reader buf_reader;
    uint8_t writeBuf[16];
    uint8_t readBuf[16];
    int  valueToEncode = 4;
    
    void cborTest(void)
    {
      CborParser parser;
      CborValue value;
      int result;
      CborEncoder encoder;
    
      /* Encoding */
    
      cbor_buf_writer_init(&buf_writer,writeBuf,sizeof(writeBuf));
     
      cbor_encoder_init(&encoder, &buf_writer.enc, 0);
      if(cbor_encode_int(&encoder, valueToEncode) != CborNoError)
      {
        printk("Error encoding int \n");
        return;
      }
    
      cbor_buf_reader_init(&buf_reader,readBuf,sizeof(readBuf));
    
    
      /* Decoding */
    
      if (cbor_parser_init(&buf_reader.r,0, &parser, &value) != CborNoError)
      {
        printk("Error initializing parser\n");
        return;
      }
      if (!cbor_value_is_integer(&value))
      {
        printk("Not an integer, somehow...\n");
        return;
      }
      if(cbor_value_get_int(&value, &result) != CborNoError)
      {
        printk("Error getting the integer\n");
      }
    
      printk("It works!, the interger is %d\n",result);
    
      return;
    }

  • Hi, and sorry for the late answer.

    As you have found, Zephyr's TinyCbor fork was modified to align with Mynewt's version. However, when the signature of cbor_encoder_init() changed in Mynewt, they did not update the documentation.

    When it comes to why you always get 0, that is because you are not parsing the encoded value, but an empty array. If you initialize your buf_reader with writeBuf instead of readBuf, you should see your value.

    Best regards,

    Didrik

  • Ah... ok, that was dumb...

    I was supposed to copy the buffer from write to read to have a very clear example of the encoded data being transferred...

    Anyway, thanks and sorry for bothering you with that   :(

Related