Subject: [Cbor] CDDL: expressing allowed values in bitfield

From: Jim Hague <[email-masked]>
Date: Thu, 9 Aug 2018 11:23:03 +0100

I'm trying to specify a bitfield in CDDL which contains flags and a
value, which is one of a specified set. An example C structure:

enum Vals{A=0, B, C, D};
struct Field {
  unsigned flag1 : 1;
  enum Vals val : 4;   /* 2 bits spare for future new Vals */
  unsigned flag2 : 1;
};

So I'm using the .bits control:

Vals = &(
  A: 0,
  B: 1,
  C: 2,
  D: 3
)
FieldBits = &(
  flag1 : 0,
  flag2 : 5) / (1..4)
Field = uint .bits FieldBits

Is there a cunning way to express the restricted value set expected in
bits 1..4 in CDDL?

Thanks.
-- 
Jim Hague - [email-masked]          Never trust a computer you can't lift.

---

From: Carsten Bormann <[email-masked]>
Date: Thu, 9 Aug 2018 13:09:52 +0200

Hi Jim,

great question.

CDDL has limited capabilities to “reach into” the (non-container) component data.
We have ranges and .lt etc. for numeric control.
We have regexes for text strings, but there is a lot of interest to be able to embed/relate to ABNF for reaching into text strings.
We have .bits for byte strings (and uints that essentially serve as such), and we could try to come up with something ABNF-like for that as well.

Say:

Field = uint .bitfield Fieldbits

Fieldbits = [
  flag1: [1, bool],
  val: [4, Vals],
  flag2: [1, bool],
]

Vals = &(A: 0, B: 1, C: 2, D: 3)

This is just a strawman, but can serve as a proof of concept how this could be added as a control.  If we want, we can start developing this right away and then register it into CDDL 1.0.  I have put this into the freezer document for now…

Grüße, Carsten

> On Aug 9, 2018, at 12:23, Jim Hague <[email-masked]> wrote:
> 
> _______________________________________________
> CBOR mailing list
> [email-masked]
> https://www.ietf.org/mailman/listinfo/cbor
>

---

From: Jim Hague <[email-masked]>
Date: Thu, 9 Aug 2018 17:02:25 +0100

> This is just a strawman, but can serve as a proof of concept how this could be added as a control.  If we want, we can start developing this right away and then register it into CDDL 1.0.  I have p…

I'm happy to hear I didn't miss something. The above looks an excellent
approach. I'd certainly like to be able to specify types for bitfields;
even with a field that's only a single bit, it can be valuable to
distinguish a numeric value that can only be 0 or 1 from a boolean.
-- 
Jim Hague - [email-masked]          Never trust a computer you can't lift.