diff --git a/CMakeLists.txt b/CMakeLists.txt index 52272cc94..2a8233b12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,6 +244,7 @@ set(Z_FEATURE_LOCAL_SUBSCRIBER 0 CACHE STRING "Toggle local subscriptions") set(Z_FEATURE_PUBLISHER_SESSION_CHECK 1 CACHE STRING "Toggle publisher session check") set(Z_FEATURE_BATCHING 1 CACHE STRING "Toggle batching") set(Z_FEATURE_RX_CACHE 0 CACHE STRING "Toggle RX_CACHE") +set(Z_FEATURE_AUTO_RECONNECT 1 CACHE STRING "Toggle automatic reconnection") # Add a warning message if someone tries to enable Z_FEATURE_LINK_SERIAL_USB directly if(Z_FEATURE_LINK_SERIAL_USB AND NOT Z_FEATURE_UNSTABLE_API) @@ -261,6 +262,7 @@ message(STATUS "Building with feature confing:\n\ * QUERYABLE: ${Z_FEATURE_QUERYABLE}\n\ * LIVELINESS: ${Z_FEATURE_LIVELINESS}\n\ * INTEREST: ${Z_FEATURE_INTEREST}\n\ +* AUTO_RECONNECT: ${Z_FEATURE_AUTO_RECONNECT}\n\ * RAWETH: ${Z_FEATURE_RAWETH_TRANSPORT}") configure_file( diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 1d22d110b..7ec06acdf 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -27,7 +27,7 @@ #define Z_FEATURE_SUBSCRIPTION 1 #define Z_FEATURE_QUERY 1 #define Z_FEATURE_QUERYABLE 1 -#define Z_FEATURE_LIVELINESS 0 +#define Z_FEATURE_LIVELINESS 1 #define Z_FEATURE_RAWETH_TRANSPORT 0 #define Z_FEATURE_INTEREST 1 #define Z_FEATURE_DYNAMIC_MEMORY_ALLOCATION 0 @@ -48,6 +48,7 @@ #define Z_FEATURE_PUBLISHER_SESSION_CHECK 1 #define Z_FEATURE_BATCHING 1 #define Z_FEATURE_RX_CACHE 0 +#define Z_FEATURE_AUTO_RECONNECT 1 // End of CMake generation /*------------------ Runtime configuration properties ------------------*/ diff --git a/include/zenoh-pico/config.h.in b/include/zenoh-pico/config.h.in index a01c6f668..92b7006ea 100644 --- a/include/zenoh-pico/config.h.in +++ b/include/zenoh-pico/config.h.in @@ -48,6 +48,7 @@ #define Z_FEATURE_PUBLISHER_SESSION_CHECK @Z_FEATURE_PUBLISHER_SESSION_CHECK@ #define Z_FEATURE_BATCHING @Z_FEATURE_BATCHING@ #define Z_FEATURE_RX_CACHE @Z_FEATURE_RX_CACHE@ +#define Z_FEATURE_AUTO_RECONNECT @Z_FEATURE_AUTO_RECONNECT@ // End of CMake generation /*------------------ Runtime configuration properties ------------------*/ diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index c4326a397..4babe6771 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -38,8 +38,6 @@ extern "C" { typedef struct _z_session_t { #if Z_FEATURE_MULTI_THREAD == 1 _z_mutex_t _mutex_inner; - z_task_attr_t *_lease_task_attr; - z_task_attr_t *_read_task_attr; #endif // Z_FEATURE_MULTI_THREAD == 1 // Zenoh-pico is considering a single transport per session. @@ -58,10 +56,13 @@ typedef struct _z_session_t { _z_resource_list_t *_local_resources; _z_resource_list_t *_remote_resources; +#if Z_FEATURE_AUTO_RECONNECT == 1 // Information for session restoring - // Empty _config means session is not restorable _z_config_t _config; _z_network_message_list_t *_decalaration_cache; + z_task_attr_t *_lease_task_attr; + z_task_attr_t *_read_task_attr; +#endif // Session subscriptions #if Z_FEATURE_SUBSCRIPTION == 1 diff --git a/src/api/api.c b/src/api/api.c index 5b6bd9ca1..207f4b467 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -702,13 +702,15 @@ z_result_t z_open(z_owned_session_t *zs, z_moved_config_t *config, const z_open_ z_config_drop(config); return ret; } + // Clean up - if (/* session is restorable*/ true) { - _Z_OWNED_RC_IN_VAL(zs)->_config = config->_this._val; - z_internal_config_null(&config->_this); - } else { - z_config_drop(config); - } +#if Z_FEATURE_AUTO_RECONNECT == 1 + _Z_OWNED_RC_IN_VAL(zs)->_config = config->_this._val; + z_internal_config_null(&config->_this); +#else + z_config_drop(config); +#endif + return _Z_RES_OK; } diff --git a/src/net/primitives.c b/src/net/primitives.c index 4864dc836..b7651bfb2 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -45,18 +45,26 @@ static z_result_t _z_send_decalre(_z_session_t *zn, const _z_network_message_t *n_msg) { z_result_t ret = _Z_RES_OK; ret = _z_send_n_msg(zn, n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + +#if Z_FEATURE_AUTO_RECONNECT == 1 if (ret == _Z_RES_OK) { _z_cache_declaration(zn, n_msg); } +#endif + return ret; } static z_result_t _z_send_undecalre(_z_session_t *zn, const _z_network_message_t *n_msg) { z_result_t ret = _Z_RES_OK; ret = _z_send_n_msg(zn, n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + +#if Z_FEATURE_AUTO_RECONNECT == 1 if (ret == _Z_RES_OK) { _z_prune_declaration(zn, n_msg); } +#endif + return ret; } /*------------------ Scouting ------------------*/ diff --git a/src/net/session.c b/src/net/session.c index 4de7c199c..d00750a61 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -165,6 +165,8 @@ z_result_t _z_open(_z_session_rc_t *zn, _z_config_t *config, const _z_id_t *zid) return ret; } +#if Z_FEATURE_AUTO_RECONNECT == 1 + z_result_t _z_reopen(_z_session_rc_t *zn) { z_result_t ret = _Z_RES_OK; _z_session_t *zs = _Z_RC_IN_VAL(zn); @@ -260,6 +262,7 @@ void _z_prune_declaration(_z_session_t *zs, const _z_network_message_t *n_msg) { assert(cnt_before == cnt_after + 1); #endif } +#endif // Z_FEATURE_AUTO_RECONNECT == 1 void _z_close(_z_session_t *zn) { _z_session_close(zn, _Z_CLOSE_GENERIC); } @@ -329,8 +332,10 @@ z_result_t _zp_start_read_task(_z_session_t *zn, z_task_attr_t *attr) { // Free task if operation failed if (ret != _Z_RES_OK) { z_free(task); +#if Z_FEATURE_AUTO_RECONNECT == 1 } else { zn->_read_task_attr = attr; +#endif } return ret; } @@ -360,8 +365,10 @@ z_result_t _zp_start_lease_task(_z_session_t *zn, z_task_attr_t *attr) { // Free task if operation failed if (ret != _Z_RES_OK) { z_free(task); +#if Z_FEATURE_AUTO_RECONNECT == 1 } else { zn->_lease_task_attr = attr; +#endif } return ret; } diff --git a/src/session/utils.c b/src/session/utils.c index 899dc4719..7bb9da967 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -66,7 +66,10 @@ z_result_t _z_session_init(_z_session_t *zn, const _z_id_t *zid) { zn->_resource_id = 1; zn->_query_id = 1; +#if Z_FEATURE_AUTO_RECONNECT == 1 + _z_config_init(&zn->_config); zn->_decalaration_cache = NULL; +#endif // Initialize the data structs zn->_local_resources = NULL; @@ -108,8 +111,10 @@ void _z_session_clear(_z_session_t *zn) { _zp_stop_lease_task(zn); #endif +#if Z_FEATURE_AUTO_RECONNECT == 1 _z_config_clear(&zn->_config); _z_network_message_list_free(&zn->_decalaration_cache); +#endif _z_close(zn); // Clear Zenoh PID diff --git a/src/transport/multicast/lease.c b/src/transport/multicast/lease.c index 515be2585..b29603169 100644 --- a/src/transport/multicast/lease.c +++ b/src/transport/multicast/lease.c @@ -21,6 +21,7 @@ #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/multicast/lease.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" #if Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1 @@ -55,7 +56,12 @@ z_result_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { #if Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) -static void _zp_multicast_failed(_z_transport_multicast_t *ztm) { _z_reopen(ztm->_common._session); } +static void _zp_multicast_failed(_z_transport_multicast_t *ztm) { + _ZP_UNUSED(ztm); +#if Z_FEATURE_AUTO_RECONNECT == 1 + _z_reopen(ztm->_common._session); +#endif +} static _z_zint_t _z_get_minimum_lease(_z_transport_peer_entry_list_t *peers, _z_zint_t local_lease) { _z_zint_t ret = local_lease; diff --git a/src/transport/unicast/lease.c b/src/transport/unicast/lease.c index 0f461979c..45ac8276c 100644 --- a/src/transport/unicast/lease.c +++ b/src/transport/unicast/lease.c @@ -42,10 +42,13 @@ z_result_t _zp_unicast_send_keep_alive(_z_transport_unicast_t *ztu) { #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 static void _zp_unicast_failed(_z_transport_unicast_t *ztu) { - _z_session_rc_ref_t *zs = ztu->_common._session; _z_unicast_transport_close(ztu, _Z_CLOSE_EXPIRED); _z_unicast_transport_clear(ztu, true); + +#if Z_FEATURE_AUTO_RECONNECT == 1 + _z_session_rc_ref_t *zs = ztu->_common._session; _z_reopen(zs); +#endif } void *_zp_unicast_lease_task(void *ztu_arg) {