shproto Wiki
Status: Pre-Alpha
Brought to you by:
mikerez
Lets consider simple TLV (byte id, byte len, data) parser which handles three types of command. You will need to read binary id, then binary length and then data and extract it. Also we will extract data fields (sub-TLVs) from command 0x01 in any order.
"\x01" len:bin8 { printf("got command 1, len: %hhu\n", state->len); }
{ if( state->len != 0 ) return false; } { state->on_cmd1(); } $ #command finished
"\x11" fld1_len:bin8 fld1:array(fld1_len) { state->len -= state->field_len; }
"\x12" fld2_len:bin8 fld2:array(fld2_len) { state->len -= state->field_len; }
"\x13" fld3_len:bin8 fld3:array(fld3_len) { state->len -= state->field_len; }
default fld_id:bin8 fld_len:bin8 fld:array(fld_len) { state->len -= state->field_len; }
"\x02" len:bin8 cmd:data(len) { printf("got command 2, len: %hhu\n", state->len); }
"\x03" len:bin8 cmd:data(len) { printf("got command 3, len: %hhu\n", state->len); }
default cmd_id:bin8 len:bin8 cmd:data(len)