From 0d9f25b413c4cb2e3a3f6ea7202ef2c2b124471b Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 21 Mar 2024 11:18:52 +0100 Subject: [PATCH] Expose timeout option in z_get_options_t (#375) * Expose timeout option in z_get_options_t * Add default get timeout --- include/zenoh-pico/api/types.h | 1 + include/zenoh-pico/config.h | 7 +++++++ include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/protocol/definitions/network.h | 3 ++- src/api/api.c | 5 +++-- src/net/primitives.c | 11 ++++++----- src/protocol/definitions/network.c | 5 +++-- 7 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 5b29667f8..dc6fc71aa 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -360,6 +360,7 @@ typedef struct { z_value_t value; z_query_consolidation_t consolidation; z_query_target_t target; + uint32_t timeout_ms; #if Z_FEATURE_ATTACHMENT == 1 // TODO:ATT z_attachment_t attachment; #endif diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 31d8ccf4b..041f3a36b 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -330,4 +330,11 @@ #define ZP_ASM_NOP __asm__("nop") #endif +/** + * Default get timeout in milliseconds. + */ +#ifndef Z_GET_TIMEOUT_DEFAULT +#define Z_GET_TIMEOUT_DEFAULT 10000 +#endif + #endif /* INCLUDE_ZENOH_PICO_CONFIG_H */ diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 20bb27099..8deeec549 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -233,7 +233,7 @@ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const */ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop + void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index ce9c280f9..642ff7b58 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -243,7 +243,8 @@ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, + uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment diff --git a/src/api/api.c b/src/api/api.c index 1c1660be5..c95ec26ab 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -788,6 +788,7 @@ z_get_options_t z_get_options_default(void) { #if Z_FEATURE_ATTACHMENT == 1 // TODO:ATT.attachment = z_attachment_null() #endif + .timeout_ms = Z_GET_TIMEOUT_DEFAULT }; } @@ -836,7 +837,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne } ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - __z_reply_handler, wrapped_ctx, callback->drop, ctx + __z_reply_handler, wrapped_ctx, callback->drop, ctx, opt.timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -1286,4 +1287,4 @@ z_owned_bytes_map_t z_bytes_map_new(void) { return (z_owned_bytes_map_t){._inner z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = NULL}; } z_bytes_t z_bytes_from_str(const char *str) { return z_bytes_wrap((const uint8_t *)str, strlen(str)); } z_bytes_t z_bytes_null(void) { return (z_bytes_t){.len = 0, ._is_alloc = false, .start = NULL}; } -#endif \ No newline at end of file +#endif diff --git a/src/net/primitives.c b/src/net/primitives.c index c4c144d06..0a31e89bd 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -378,7 +378,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val /*------------------ Query ------------------*/ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop + void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment @@ -404,12 +404,13 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { _z_bytes_t params = _z_bytes_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); - _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value + _z_zenoh_message_t z_msg = + _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value, timeout_ms #if Z_FEATURE_ATTACHMENT == 1 - , - attachment + , + attachment #endif - ); + ); if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 056558c3d..a496a1a8b 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -168,7 +168,8 @@ _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id) { return ret; } _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, + uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment @@ -192,7 +193,7 @@ _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes ._ext_budget = 0, ._ext_qos = _Z_N_QOS_DEFAULT, ._ext_target = Z_QUERY_TARGET_BEST_MATCHING, - ._ext_timeout_ms = 0, + ._ext_timeout_ms = timeout_ms, ._ext_timestamp = _z_timestamp_null(), }, };