-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
panic for constants #241
Comments
I tried this patch:
It creates 2 unnecessary types, but it compiles, and the serialization code looks good, but haven't tested it yet. Of course, this would be an ugly solution, it should just ignore it for constants and not trying to create types for it, but haven't looked at the rest of the code how to do this. |
try:
if that format works for you. What are the unnecessary types? Did you create 2 extra In case you're talking about a
which wouldn't generate the extra |
Thanks, but with the @ comments doesn't work with zcbor, and I want to use the same CDDL files for both, the C program (a Zephyr project) and the Rust project. With my patch it works. It generates an extra type, but also an enum. Here is the generated lib.rs for the CDDL file I posted: #![allow(clippy::too_many_arguments)]
pub mod error;
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen
pub mod serialization;
use crate::error::*;
use std::collections::BTreeMap;
use std::convert::TryFrom;
pub type CmdStart = u8;
#[derive(Copy, Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
#[wasm_bindgen::prelude::wasm_bindgen]
pub enum CmdStartOrCmdStop {
CmdStart,
CmdStop,
}
pub type CmdStop = u8;
#[derive(Clone, Debug)]
pub struct MyCommand {
pub command: CmdStartOrCmdStop,
}
impl MyCommand {
pub fn new(command: CmdStartOrCmdStop) -> Self {
Self { command }
}
} I think it is a bug in cddl-codegen that it tries to generate a type for a number constant. And I think it should be valid CDDL, at least syntactically: According to the ABNF of RFC 8610, a rule is a typename = type, where type can be type1, which can be type2, which can be a value, which can be a number. |
Yeah the
It is.
It probably is. I thought we had constant support although for wasm it does it via function like |
Might be a related problems with constants, haven't looked into it, but it currently blocks my project. This file:
results in this crash (with my applied patch, otherwise it crashes already for the constants earlier) :
Problem is the last foobar definition. If I remove it, then it works, but it is not usable for me. I guess this is a good candidate for a new test case? I simplified it already from my real usecase. zcbor works just fine with it. Calling it like this:
generates this file: /*
* Generated using zcbor version 0.9.0
* https://github.com/NordicSemiconductor/zcbor
* at: 2024-11-06 16:34:17
* Generated with a --default-max-qty of 3
*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "zcbor_decode.h"
#include "test.h"
#include "zcbor_print.h"
#if DEFAULT_MAX_QTY != 3
#error "The type file was generated with a different default_max_qty than this file"
#endif
#define log_result(state, result, func) do { \
if (!result) { \
zcbor_trace_file(state); \
zcbor_log("%s error: %s\r\n", func, zcbor_error_str(zcbor_peek_error(state))); \
} else { \
zcbor_log("%s success\r\n", func); \
} \
} while(0)
static bool decode_foo(zcbor_state_t *state, struct foo *result);
static bool decode_bar(zcbor_state_t *state, struct bar *result);
static bool decode_foobar(zcbor_state_t *state, struct foobar *result);
static bool decode_foo(
zcbor_state_t *state, struct foo *result)
{
zcbor_log("%s\r\n", __func__);
bool res = (((((zcbor_uint32_expect(state, (1))))
&& ((zcbor_uint32_decode(state, (&(*result).foo_something1)))
&& ((((*result).foo_something1 <= UINT32_MAX)) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))))));
log_result(state, res, __func__);
return res;
}
static bool decode_bar(
zcbor_state_t *state, struct bar *result)
{
zcbor_log("%s\r\n", __func__);
bool res = (((((zcbor_uint32_expect(state, (2))))
&& ((zcbor_uint32_decode(state, (&(*result).bar_something2)))
&& ((((*result).bar_something2 <= UINT32_MAX)) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)))
&& ((zcbor_uint32_decode(state, (&(*result).bar_something3)))
&& ((((*result).bar_something3 <= UINT32_MAX)) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))))));
log_result(state, res, __func__);
return res;
}
static bool decode_foobar(
zcbor_state_t *state, struct foobar *result)
{
zcbor_log("%s\r\n", __func__);
bool int_res;
bool res = (((zcbor_list_start_decode(state) && ((((zcbor_union_start_code(state) && (int_res = ((((decode_foo(state, (&(*result).foobar_union_foo_m)))) && (((*result).foobar_union_choice = foobar_union_foo_m_c), true))
|| (zcbor_union_elem_code(state) && (((decode_bar(state, (&(*result).foobar_union_bar_m)))) && (((*result).foobar_union_choice = foobar_union_bar_m_c), true)))), zcbor_union_end_code(state), int_res)))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state))));
log_result(state, res, __func__);
return res;
}
int cbor_decode_foobar(
const uint8_t *payload, size_t payload_len,
struct foobar *result,
size_t *payload_len_out)
{
zcbor_state_t states[4];
return zcbor_entry_function(payload, payload_len, (void *)result, payload_len_out, states,
(zcbor_decoder_t *)decode_foobar, sizeof(states) / sizeof(zcbor_state_t), 1);
} |
I'm not sure this is technically proper CBOR as both are basic groups still and not yet concrete types. Try doing
or
or group choices
We use a lot of similar cddl defs written like:
or
I hope this helps. |
Thanks, looks like zcbor generates the same code for "//". But I need a comma at the end of the lines:
cddl-codegen works without commas as well (with my patch with the constants). Haven't checked the BNF, is a comma required? Will check if the Rust code works tomorrow. |
Commas work. Sorry, that was a mistake on my end, I simply forgot to add them there since I was only typing in the browser as an example and had copied/pasted from other lines. I'm surprised it worked without commas, I would have thought they would be required based on the CDDL RFC. I didn't write the parsing code though, we use the external |
I tested it, and there is one compilation error for the generated code. Here is the test file:
For this, it generates this code in serialization.rs:
The error is in line "+ match &self.group_baz.max {". It needs to be "+ match &group_baz.max {". This is probably easy to fix, but couldn't find it. Haven't tested yet if it runs, but at least now it compiles everything. |
Looks like the deserialize had a tricky problem: It did a read_len.read_elems(4), independent of the type. But with a group_foo_or_bar, the array has only 1 element. I also fixed this manually, looks like now I can read it correctly. Might be good to add my last test as a new test case and then enhance the tests to try to serialize and deserialize it. |
Another problem: The Rust code expected an additional array for the inner group_baz//group_foo_or_bar. But the zcbor code generated it inline, without an additional array. I think zcbor is right. After manually fixing this, both types work now for my application. But would be good, if it could be fixed for the cddl-codegen project as well, because I might need to regenerate the code when the CDDL changes, and then I would need to apply the manual patches again. |
I've only ever seen group choices at the full group level e.g. What happens if you do this? e.g. move the
|
I'll test it, but is my example valid CDDL, and which program is right, zcbor or cddl-codegen? But I think this is a different problem. I think I fixed the constant problem, see here: #242 and we can close this issue, and create a new issue for the nested array, if it is wrong, otherwise we should create an issue in the zcbor repository ( https://github.com/NordicSemiconductor/zcbor ). |
I have this CDDL file:
This works with zcbor.py, but with cddl-codegen, I get this output:
I think it is valid CDDL, but maybe it is some edge case. Would be also ok if there is a way to write it differently, and if this works then with zcbor as well.
The text was updated successfully, but these errors were encountered: