From bffc5493964e4c99166e510c71880e4d85fafaf8 Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Mon, 11 Sep 2023 15:53:47 +0200 Subject: [PATCH 1/3] Fix codec bugs --- .../zenoh-pico/protocol/definitions/network.h | 2 +- src/protocol/codec/network.c | 30 +++++++++++++++++-- zenohpico.pc | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index e439b276a..558a46146 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -63,7 +63,7 @@ typedef struct { } _z_n_qos_t; #define _z_n_qos_make(express, nodrop, priority) \ - (_z_n_qos_t) { ._val = ((express << 4) | (nodrop << 3) | priority) } + (_z_n_qos_t) { ._val = (((express) << 4) | ((nodrop) << 3) | priority) } #define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) // RESPONSE FINAL message flags: diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 857e391aa..f64f1bf70 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -39,17 +39,18 @@ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { uint8_t header = _Z_MID_N_PUSH | (_z_keyexpr_is_local(&msg->_key) ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); + _Bool has_qos_ext = msg->_qos._val != _Z_N_QOS_DEFAULT._val; _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; } - if (has_timestamp_ext) { + if (has_qos_ext || has_timestamp_ext) { header |= _Z_FLAG_N_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); - if (msg->_qos._val != _Z_N_QOS_DEFAULT._val) { + if (has_qos_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_qos._val)); } @@ -411,12 +412,37 @@ int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t return ret; } + +int8_t _z_response_final_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_n_msg_response_t *msg = (_z_n_msg_response_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { + msg->_ext_qos._val = extension->_body._zint._val; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_timestamp_decode(&msg->_ext_timestamp, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0d); + } + } + return ret; +} + int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); *msg = (_z_n_msg_response_final_t){0}; int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_request_id, zbf); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_response_final_decode_extension, msg)); + } return ret; } diff --git a/zenohpico.pc b/zenohpico.pc index 6b54a5398..5617384c4 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230906dev +Version: 0.10.20230911dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From 97f85f8c1b9451da1acf710990c1df16687e09fe Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Mon, 11 Sep 2023 16:10:18 +0200 Subject: [PATCH 2/3] Ignore response final extensions --- src/protocol/codec/network.c | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index f64f1bf70..28abb51d1 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -413,27 +413,6 @@ int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t return ret; } -int8_t _z_response_final_decode_extension(_z_msg_ext_t *extension, void *ctx) { - int8_t ret = _Z_RES_OK; - _z_n_msg_response_t *msg = (_z_n_msg_response_t *)ctx; - switch (_Z_EXT_FULL_ID(extension->_header)) { - case _Z_MSG_EXT_ENC_ZINT | 0x01: { - msg->_ext_qos._val = extension->_body._zint._val; - break; - } - case _Z_MSG_EXT_ENC_ZBUF | 0x02: { - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_timestamp_decode(&msg->_ext_timestamp, &zbf); - break; - } - default: - if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { - ret = _z_msg_ext_unknown_error(extension, 0x0d); - } - } - return ret; -} - int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); @@ -441,7 +420,7 @@ int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_request_id, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { - _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_response_final_decode_extension, msg)); + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x1a)); } return ret; } From 92961943959351e5ee7ab0b7691d623960dfd005 Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Mon, 11 Sep 2023 16:11:08 +0200 Subject: [PATCH 3/3] Make _z_n_qos_make macro even safer --- include/zenoh-pico/protocol/definitions/network.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 558a46146..69226b6e0 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -63,7 +63,7 @@ typedef struct { } _z_n_qos_t; #define _z_n_qos_make(express, nodrop, priority) \ - (_z_n_qos_t) { ._val = (((express) << 4) | ((nodrop) << 3) | priority) } + (_z_n_qos_t) { ._val = (((express) << 4) | ((nodrop) << 3) | (priority)) } #define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) // RESPONSE FINAL message flags: