NordicSemiconductor / zcbor

Low footprint C/C++ CBOR library and Python tool providing code generation from CDDL descriptions.
Apache License 2.0
113 stars 34 forks source link

Support for fragment encoding #244

Open RomainPelletant opened 2 years ago

RomainPelletant commented 2 years ago

As for decoder with the zcbor_bstr_start_decode_fragment function to only read the CBOR header in case of block-wise (COAP) transfer, it could be interesting to create the same capability in encoder to only encode partial data into several fragments and set the header only once.

oyvindronningstad commented 2 years ago

The problem is that the encoder doesn't know the byte count until the end of the string. We could consider adding new APIs to used when the byte count is known beforehand.

Thanks for the suggestion, will look into this in the future.

escherstair commented 1 year ago

I think this request is almost the same as one of my needs, that in other libraries is called streaming array. See as an example libcbor. This is useful when you have large arrays or maps and either their size is known in advance, or they are indefinite (see here). With "streaming encoding" you can encode them on the fly and strem them over the network (as an example) without having to allocate a CBOR buffer large enough.

(With a different CBOR library) I implemented this feature by myself (simplified version), but I'm not satisfied about the API I created.

oyvindronningstad commented 1 year ago

@escherstair Have you looked into zcbor_update_state()?

escherstair commented 1 year ago

I have to investigate and understand how it works

mlasch commented 1 year ago

Such a feature might be an interesting addition. I had to implement generating the CBOR part (serializing CBOR header, then appending opaque on the fly) myself, in order to send a big opaque LwM2M resource block-wise in SenML-CBOR. The data structure looks like this, size of bytes is known in advance:

[{-2: "/32765/0/", 0: "9", 8: h'313233'}]
81                          # array(1)
   A3                       # map(3)
      21                    # negative(1)
      69                    # text(9)
         2F33323736352F302F # "/32765/0/"
      00                    # unsigned(0)
      61                    # text(1)
         39                 # "9"
      08                    # unsigned(8)
      43                    # bytes(3)
         313233             # "123"