I have a use case where a header can contain 7 bytes plus an optional 0-15 Bytes of information where the size information is in the lower 4 bits of the 5th Byte so the format is:
4 bytes | 4 bits | 4 bits <- length of extra bytes | 2 bytes | 0-15 extra Bytes
And I modelled that in the following case class
case class FHDR(DevAddr:Long, ADR:Boolean, ADRACKReq:Boolean, ACK:Boolean, FPending:Boolean, FCnt:Int, FOpts:ByteVector)
implicit val FHDRCodec = {
("dev_addr" | uint32L) ::
("adr" | bool) ::
("adr_ack_req" | bool) ::
("ack" | bool) ::
("f_pending" | bool) ::
variableSizePrefixedBytes(uint4,
"f_cnt" | uint16L,
"f_opts" | bytes)
}.as[FHDR]
According to the scala docs in this case i can use the variableSizePrefixedBytes
method to model the 2 extra bytes between the size and the extra bytes.
But i'm doing something wrong as the compiler cannot prove this codec can be converted to/from the FHDR
class, i've been staring at this for a while now, but am clueless