Skip to content

Commit

Permalink
encoding alignment with zenoh-rust (#728)
Browse files Browse the repository at this point in the history
* encoding alignment with zenoh-rust

* reintroduce z_encoding_equals;
fix encoding_from_substr to work similarly to zenoh-rust;

* add encoding constants test

* format
  • Loading branch information
DenisBiryukov91 authored Oct 9, 2024
1 parent 6d03ac2 commit 0698b23
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 217 deletions.
15 changes: 1 addition & 14 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,8 @@ See details at :ref:`owned_types_concept`
Predefined Encodings
^^^^^^^^^^^^^^^^^^^^
.. autocfunction:: encoding.h::z_encoding_zenoh_bytes
.. autocfunction:: encoding.h::z_encoding_zenoh_int8
.. autocfunction:: encoding.h::z_encoding_zenoh_int16
.. autocfunction:: encoding.h::z_encoding_zenoh_int32
.. autocfunction:: encoding.h::z_encoding_zenoh_int64
.. autocfunction:: encoding.h::z_encoding_zenoh_int128
.. autocfunction:: encoding.h::z_encoding_zenoh_uint8
.. autocfunction:: encoding.h::z_encoding_zenoh_uint16
.. autocfunction:: encoding.h::z_encoding_zenoh_uint32
.. autocfunction:: encoding.h::z_encoding_zenoh_uint64
.. autocfunction:: encoding.h::z_encoding_zenoh_uint128
.. autocfunction:: encoding.h::z_encoding_zenoh_float32
.. autocfunction:: encoding.h::z_encoding_zenoh_float64
.. autocfunction:: encoding.h::z_encoding_zenoh_bool
.. autocfunction:: encoding.h::z_encoding_zenoh_string
.. autocfunction:: encoding.h::z_encoding_zenoh_error
.. autocfunction:: encoding.h::z_encoding_zenoh_serialized
.. autocfunction:: encoding.h::z_encoding_application_octet_stream
.. autocfunction:: encoding.h::z_encoding_text_plain
.. autocfunction:: encoding.h::z_encoding_application_json
Expand Down
6 changes: 5 additions & 1 deletion examples/unix/c11/z_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ int main(void) {
assert(memcmp(input_bytes, z_slice_data(z_loan(output_bytes)), z_slice_len(z_loan(output_bytes))) == 0);
z_drop(z_move(payload));
z_drop(z_move(output_bytes));
// Corresponding encoding to be used in operations options like `z_put()`, `z_get()`, etc.
// const z_loaned_encoding* encoding = z_encoding_zenoh_bytes();

// The same can be done for const char*
const char *input_str = "test";
Expand All @@ -56,6 +58,8 @@ int main(void) {
assert(strncmp(input_str, z_string_data(z_loan(output_string)), z_string_len(z_loan(output_string))) == 0);
z_drop(z_move(payload));
z_drop(z_move(output_string));
// Corresponding encoding to be used in operations options like `z_put()`, `z_get()`, etc.
// const z_loaned_encoding* encoding = z_encoding_zenoh_string();
}

// Serialization
Expand All @@ -68,7 +72,7 @@ int main(void) {
assert(input_u32 == output_u32);
z_drop(z_move(payload));
// Corresponding encoding to be used in operations options like `z_put()`, `z_get()`, etc.
// const z_loaned_encoding* encoding = z_encoding_zenoh_uint32();
// const z_loaned_encoding* encoding = z_encoding_zenoh_serialized();
}

// Writer/reader for raw bytes
Expand Down
130 changes: 10 additions & 120 deletions include/zenoh-pico/api/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,141 +47,31 @@ extern "C" {
*
* Constant alias for string: `"zenoh/bytes"`.
*
* Usually used for types: `uint8_t[]`.
* This encoding supposes that the payload was created with c:func:`z_bytes_from_buf`, c:func:`z_bytes_from_slice` or
* similar functions and its data can be accessed via c:func:`z_bytes_to_slice`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_bytes(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_BYTES;

/**
* A VLE-encoded signed little-endian 8bit integer. Binary representation uses two's complement.
* Constant alias for string: `"zenoh/int8"`.
*
* Usually used for types: `int8_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_int8(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_INT8;

/**
* A VLE-encoded signed little-endian 16bit integer. Binary representation uses two's complement.
* Constant alias for string: `"zenoh/int16"`.
*
* Usually used for types: `int16_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_int16(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_INT16;

/**
* A VLE-encoded signed little-endian 32bit integer. Binary representation uses two's complement.
* Constant alias for string: `"zenoh/int32"`.
*
* Usually used for types: `int32_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_int32(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_INT32;

/**
* A VLE-encoded signed little-endian 64bit integer. Binary representation uses two's complement.
* Constant alias for string: `"zenoh/int64"`.
*
* Usually used for types: `int64_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_int64(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_INT64;

/**
* A VLE-encoded signed little-endian 128bit integer. Binary representation uses two's complement.
* Constant alias for string: `"zenoh/int128"`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_int128(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_INT128;

/**
* A VLE-encoded unsigned little-endian 8bit integer.
* Constant alias for string: `"zenoh/uint8"`.
*
* Usually used for types: `uint8_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_uint8(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_UINT8;

/**
* A VLE-encoded unsigned little-endian 16bit integer.
* Constant alias for string: `"zenoh/uint16"`.
*
* Usually used for types: `uint16_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_uint16(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_UINT16;

/**
* A VLE-encoded unsigned little-endian 32bit integer.
* Constant alias for string: `"zenoh/uint32"`.
*
* Usually used for types: `uint32_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_uint32(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_UINT32;

/**
* A VLE-encoded unsigned little-endian 64bit integer.
* Constant alias for string: `"zenoh/uint64"`.
*
* Usually used for types: `uint64_t`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_uint64(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_UINT64;

/**
* A VLE-encoded unsigned little-endian 128bit integer.
* Constant alias for string: `"zenoh/uint128"`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_uint128(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_UINT128;

/**
* A VLE-encoded 32bit float. Binary representation uses *IEEE 754-2008* *binary32*.
* Constant alias for string: `"zenoh/float32"`.
*
* Usually used for types: `float`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_float32(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_FLOAT32;

/**
* A VLE-encoded 64bit float. Binary representation uses *IEEE 754-2008* *binary64*.
* Constant alias for string: `"zenoh/float64"`.
*
* Usually used for types: `double`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_float64(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_FLOAT64;

/**
* A boolean. `0` is `false`, `1` is `true`. Other values are invalid.
* Constant alias for string: `"zenoh/bool"`.
*
* Usually used for types: `bool`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_bool(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_BOOL;

/**
* A UTF-8 string.
* Constant alias for string: `"zenoh/string"`.
*
* Usually used for types: `char[]`.
* This encoding supposes that the payload was created with c:func:`z_bytes_from_str`, c:func:`z_bytes_from_string` or
* similar functions and its data can be accessed via c:func:`z_bytes_to_string`.
*/
const z_loaned_encoding_t *z_encoding_zenoh_string(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_STRING;

/**
* A zenoh error.
* Constant alias for string: `"zenoh/error"`.
* Zenoh serialized data.
* Constant alias for string: `"zenoh/serialized"`.
*
* Usually used for types: `z_reply_err_t`.
* This encoding supposes that the payload was created with serialization functions.
* The `schema` field may contain the details of serialziation format.
*/
const z_loaned_encoding_t *z_encoding_zenoh_error(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_ERROR;
const z_loaned_encoding_t *z_encoding_zenoh_serialized(void);
extern const z_owned_encoding_t ZP_ENCODING_ZENOH_SERIALIZED;

/**
* An application-specific stream of bytes.
Expand Down
4 changes: 4 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ z_result_t zp_config_insert(z_loaned_config_t *config, uint8_t key, const char *
_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_encoding_t, encoding, _z_encoding_check, _z_encoding_null, _z_encoding_copy,
_z_encoding_clear)

bool z_encoding_equals(const z_loaned_encoding_t *left, const z_loaned_encoding_t *right) {
return left->id == right->id && _z_string_equals(&left->schema, &right->schema);
}

z_result_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, size_t len) {
slice->_val = _z_slice_copy_from_buf(data, len);
if (slice->_val.start == NULL) {
Expand Down
143 changes: 61 additions & 82 deletions src/api/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,89 +32,63 @@
const z_loaned_encoding_t *z_encoding_##_fname(void) { return &ZP_ENCODING_##_cname._val; }

ENCODING_CONSTANT_MACRO(ZENOH_BYTES, zenoh_bytes, 0)
ENCODING_CONSTANT_MACRO(ZENOH_INT8, zenoh_int8, 1)
ENCODING_CONSTANT_MACRO(ZENOH_INT16, zenoh_int16, 2)
ENCODING_CONSTANT_MACRO(ZENOH_INT32, zenoh_int32, 3)
ENCODING_CONSTANT_MACRO(ZENOH_INT64, zenoh_int64, 4)
ENCODING_CONSTANT_MACRO(ZENOH_INT128, zenoh_int128, 5)
ENCODING_CONSTANT_MACRO(ZENOH_UINT8, zenoh_uint8, 6)
ENCODING_CONSTANT_MACRO(ZENOH_UINT16, zenoh_uint16, 7)
ENCODING_CONSTANT_MACRO(ZENOH_UINT32, zenoh_uint32, 8)
ENCODING_CONSTANT_MACRO(ZENOH_UINT64, zenoh_uint64, 9)
ENCODING_CONSTANT_MACRO(ZENOH_UINT128, zenoh_uint128, 10)
ENCODING_CONSTANT_MACRO(ZENOH_FLOAT32, zenoh_float32, 11)
ENCODING_CONSTANT_MACRO(ZENOH_FLOAT64, zenoh_float64, 12)
ENCODING_CONSTANT_MACRO(ZENOH_BOOL, zenoh_bool, 13)
ENCODING_CONSTANT_MACRO(ZENOH_STRING, zenoh_string, 14)
ENCODING_CONSTANT_MACRO(ZENOH_ERROR, zenoh_error, 15)
ENCODING_CONSTANT_MACRO(APPLICATION_OCTET_STREAM, application_octet_stream, 16)
ENCODING_CONSTANT_MACRO(TEXT_PLAIN, text_plain, 17)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON, application_json, 18)
ENCODING_CONSTANT_MACRO(TEXT_JSON, text_json, 19)
ENCODING_CONSTANT_MACRO(APPLICATION_CDR, application_cdr, 20)
ENCODING_CONSTANT_MACRO(APPLICATION_CBOR, application_cbor, 21)
ENCODING_CONSTANT_MACRO(APPLICATION_YAML, application_yaml, 22)
ENCODING_CONSTANT_MACRO(TEXT_YAML, text_yaml, 23)
ENCODING_CONSTANT_MACRO(TEXT_JSON5, text_json5, 24)
ENCODING_CONSTANT_MACRO(APPLICATION_PYTHON_SERIALIZED_OBJECT, application_python_serialized_object, 25)
ENCODING_CONSTANT_MACRO(APPLICATION_PROTOBUF, application_protobuf, 26)
ENCODING_CONSTANT_MACRO(APPLICATION_JAVA_SERIALIZED_OBJECT, application_java_serialized_object, 27)
ENCODING_CONSTANT_MACRO(APPLICATION_OPENMETRICS_TEXT, application_openmetrics_text, 28)
ENCODING_CONSTANT_MACRO(IMAGE_PNG, image_png, 29)
ENCODING_CONSTANT_MACRO(IMAGE_JPEG, image_jpeg, 30)
ENCODING_CONSTANT_MACRO(IMAGE_GIF, image_gif, 31)
ENCODING_CONSTANT_MACRO(IMAGE_BMP, image_bmp, 32)
ENCODING_CONSTANT_MACRO(IMAGE_WEBP, image_webp, 33)
ENCODING_CONSTANT_MACRO(APPLICATION_XML, application_xml, 34)
ENCODING_CONSTANT_MACRO(APPLICATION_X_WWW_FORM_URLENCODED, application_x_www_form_urlencoded, 35)
ENCODING_CONSTANT_MACRO(TEXT_HTML, text_html, 36)
ENCODING_CONSTANT_MACRO(TEXT_XML, text_xml, 37)
ENCODING_CONSTANT_MACRO(TEXT_CSS, text_css, 38)
ENCODING_CONSTANT_MACRO(TEXT_JAVASCRIPT, text_javascript, 39)
ENCODING_CONSTANT_MACRO(TEXT_MARKDOWN, text_markdown, 40)
ENCODING_CONSTANT_MACRO(TEXT_CSV, text_csv, 41)
ENCODING_CONSTANT_MACRO(APPLICATION_SQL, application_sql, 42)
ENCODING_CONSTANT_MACRO(APPLICATION_COAP_PAYLOAD, application_coap_payload, 43)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON_PATCH_JSON, application_json_patch_json, 44)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON_SEQ, application_json_seq, 45)
ENCODING_CONSTANT_MACRO(APPLICATION_JSONPATH, application_jsonpath, 46)
ENCODING_CONSTANT_MACRO(APPLICATION_JWT, application_jwt, 47)
ENCODING_CONSTANT_MACRO(APPLICATION_MP4, application_mp4, 48)
ENCODING_CONSTANT_MACRO(APPLICATION_SOAP_XML, application_soap_xml, 49)
ENCODING_CONSTANT_MACRO(APPLICATION_YANG, application_yang, 50)
ENCODING_CONSTANT_MACRO(AUDIO_AAC, audio_aac, 51)
ENCODING_CONSTANT_MACRO(AUDIO_FLAC, audio_flac, 52)
ENCODING_CONSTANT_MACRO(AUDIO_MP4, audio_mp4, 53)
ENCODING_CONSTANT_MACRO(AUDIO_OGG, audio_ogg, 54)
ENCODING_CONSTANT_MACRO(AUDIO_VORBIS, audio_vorbis, 55)
ENCODING_CONSTANT_MACRO(VIDEO_H261, video_h261, 56)
ENCODING_CONSTANT_MACRO(VIDEO_H263, video_h263, 57)
ENCODING_CONSTANT_MACRO(VIDEO_H264, video_h264, 58)
ENCODING_CONSTANT_MACRO(VIDEO_H265, video_h265, 59)
ENCODING_CONSTANT_MACRO(VIDEO_H266, video_h266, 60)
ENCODING_CONSTANT_MACRO(VIDEO_MP4, video_mp4, 61)
ENCODING_CONSTANT_MACRO(VIDEO_OGG, video_ogg, 62)
ENCODING_CONSTANT_MACRO(VIDEO_RAW, video_raw, 63)
ENCODING_CONSTANT_MACRO(VIDEO_VP8, video_vp8, 64)
ENCODING_CONSTANT_MACRO(VIDEO_VP9, video_vp9, 65)
ENCODING_CONSTANT_MACRO(ZENOH_STRING, zenoh_string, 1)
ENCODING_CONSTANT_MACRO(ZENOH_SERIALIZED, zenoh_serialized, 2)
ENCODING_CONSTANT_MACRO(APPLICATION_OCTET_STREAM, application_octet_stream, 3)
ENCODING_CONSTANT_MACRO(TEXT_PLAIN, text_plain, 4)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON, application_json, 5)
ENCODING_CONSTANT_MACRO(TEXT_JSON, text_json, 6)
ENCODING_CONSTANT_MACRO(APPLICATION_CDR, application_cdr, 7)
ENCODING_CONSTANT_MACRO(APPLICATION_CBOR, application_cbor, 8)
ENCODING_CONSTANT_MACRO(APPLICATION_YAML, application_yaml, 9)
ENCODING_CONSTANT_MACRO(TEXT_YAML, text_yaml, 10)
ENCODING_CONSTANT_MACRO(TEXT_JSON5, text_json5, 11)
ENCODING_CONSTANT_MACRO(APPLICATION_PYTHON_SERIALIZED_OBJECT, application_python_serialized_object, 12)
ENCODING_CONSTANT_MACRO(APPLICATION_PROTOBUF, application_protobuf, 13)
ENCODING_CONSTANT_MACRO(APPLICATION_JAVA_SERIALIZED_OBJECT, application_java_serialized_object, 14)
ENCODING_CONSTANT_MACRO(APPLICATION_OPENMETRICS_TEXT, application_openmetrics_text, 15)
ENCODING_CONSTANT_MACRO(IMAGE_PNG, image_png, 16)
ENCODING_CONSTANT_MACRO(IMAGE_JPEG, image_jpeg, 17)
ENCODING_CONSTANT_MACRO(IMAGE_GIF, image_gif, 18)
ENCODING_CONSTANT_MACRO(IMAGE_BMP, image_bmp, 19)
ENCODING_CONSTANT_MACRO(IMAGE_WEBP, image_webp, 20)
ENCODING_CONSTANT_MACRO(APPLICATION_XML, application_xml, 21)
ENCODING_CONSTANT_MACRO(APPLICATION_X_WWW_FORM_URLENCODED, application_x_www_form_urlencoded, 22)
ENCODING_CONSTANT_MACRO(TEXT_HTML, text_html, 23)
ENCODING_CONSTANT_MACRO(TEXT_XML, text_xml, 24)
ENCODING_CONSTANT_MACRO(TEXT_CSS, text_css, 25)
ENCODING_CONSTANT_MACRO(TEXT_JAVASCRIPT, text_javascript, 26)
ENCODING_CONSTANT_MACRO(TEXT_MARKDOWN, text_markdown, 27)
ENCODING_CONSTANT_MACRO(TEXT_CSV, text_csv, 28)
ENCODING_CONSTANT_MACRO(APPLICATION_SQL, application_sql, 29)
ENCODING_CONSTANT_MACRO(APPLICATION_COAP_PAYLOAD, application_coap_payload, 30)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON_PATCH_JSON, application_json_patch_json, 31)
ENCODING_CONSTANT_MACRO(APPLICATION_JSON_SEQ, application_json_seq, 32)
ENCODING_CONSTANT_MACRO(APPLICATION_JSONPATH, application_jsonpath, 33)
ENCODING_CONSTANT_MACRO(APPLICATION_JWT, application_jwt, 34)
ENCODING_CONSTANT_MACRO(APPLICATION_MP4, application_mp4, 35)
ENCODING_CONSTANT_MACRO(APPLICATION_SOAP_XML, application_soap_xml, 36)
ENCODING_CONSTANT_MACRO(APPLICATION_YANG, application_yang, 37)
ENCODING_CONSTANT_MACRO(AUDIO_AAC, audio_aac, 38)
ENCODING_CONSTANT_MACRO(AUDIO_FLAC, audio_flac, 39)
ENCODING_CONSTANT_MACRO(AUDIO_MP4, audio_mp4, 40)
ENCODING_CONSTANT_MACRO(AUDIO_OGG, audio_ogg, 41)
ENCODING_CONSTANT_MACRO(AUDIO_VORBIS, audio_vorbis, 42)
ENCODING_CONSTANT_MACRO(VIDEO_H261, video_h261, 43)
ENCODING_CONSTANT_MACRO(VIDEO_H263, video_h263, 44)
ENCODING_CONSTANT_MACRO(VIDEO_H264, video_h264, 45)
ENCODING_CONSTANT_MACRO(VIDEO_H265, video_h265, 46)
ENCODING_CONSTANT_MACRO(VIDEO_H266, video_h266, 47)
ENCODING_CONSTANT_MACRO(VIDEO_MP4, video_mp4, 48)
ENCODING_CONSTANT_MACRO(VIDEO_OGG, video_ogg, 49)
ENCODING_CONSTANT_MACRO(VIDEO_RAW, video_raw, 50)
ENCODING_CONSTANT_MACRO(VIDEO_VP8, video_vp8, 51)
ENCODING_CONSTANT_MACRO(VIDEO_VP9, video_vp9, 52)

const char *ENCODING_VALUES_ID_TO_STR[] = {
"zenoh/bytes",
"zenoh/int8",
"zenoh/int16",
"zenoh/int32",
"zenoh/int64",
"zenoh/int128",
"zenoh/uint8",
"zenoh/uint16",
"zenoh/uint32",
"zenoh/uint64",
"zenoh/uint128",
"zenoh/float32",
"zenoh/float64",
"zenoh/bool",
"zenoh/string",
"zenoh/error",
"zenoh/serialized",
"application/octet-stream",
"text/plain",
"application/json",
Expand Down Expand Up @@ -183,12 +157,17 @@ static z_result_t _z_encoding_convert_from_substr(z_owned_encoding_t *encoding,
}

// Check id_end value + corner cases
if ((pos != len) && (pos != 0)) {
if (pos != 0) {
uint16_t id = _z_encoding_values_str_to_int(s, pos);
// Check id
if (id != UINT16_MAX) {
const char *ptr = (pos + 1 == len) ? NULL : s + pos + 1;
return _z_encoding_make(&encoding->_val, id, ptr, len - pos - 1);
const char *ptr = NULL;
size_t remaining = 0;
if (pos + 1 < len) {
ptr = s + pos + 1;
remaining = len - pos - 1;
}
return _z_encoding_make(&encoding->_val, id, ptr, remaining);
}
}
// By default store the string as schema
Expand Down
Loading

0 comments on commit 0698b23

Please sign in to comment.