From d01379ac7e41b65644f5d2dc65af60fe645e3caf Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 6 Mar 2024 10:48:59 +0100 Subject: [PATCH] refactor: use flag macros --- .../zenoh-pico/protocol/definitions/core.h | 1 + src/protocol/codec/declarations.c | 20 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/zenoh-pico/protocol/definitions/core.h b/include/zenoh-pico/protocol/definitions/core.h index 58d16a430..3fd9110b1 100644 --- a/include/zenoh-pico/protocol/definitions/core.h +++ b/include/zenoh-pico/protocol/definitions/core.h @@ -44,5 +44,6 @@ #define _Z_FLAGS(h) (_Z_FLAGS_MASK & (h)) #define _Z_HAS_FLAG(h, f) (((h) & (f)) != 0) #define _Z_SET_FLAG(h, f) (h |= f) +#define _Z_CLEAR_FLAG(h, f) (h &= ~(f)) #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H */ diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 092480ec2..44135727a 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -133,27 +133,27 @@ int8_t _z_decl_interest_encode(_z_wbuf_t *wbf, const _z_decl_interest_t *decl) { // Set header uint8_t header = _Z_DECL_INTEREST_MID; if (_Z_HAS_FLAG(decl->interest_flags, _Z_INTEREST_FLAG_CURRENT)) { - header |= _Z_INTEREST_FLAG_CURRENT; + _Z_SET_FLAG(header, _Z_INTEREST_FLAG_CURRENT); } if (_Z_HAS_FLAG(decl->interest_flags, _Z_INTEREST_FLAG_FUTURE)) { - header |= _Z_INTEREST_FLAG_FUTURE; + _Z_SET_FLAG(header, _Z_INTEREST_FLAG_FUTURE); } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); // Set id _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl->_id)); // Copy flags but clear double use ones. uint8_t interest_flags = decl->interest_flags; - interest_flags &= ~_Z_DECL_SUBSCRIBER_FLAG_N; - interest_flags &= ~_Z_DECL_SUBSCRIBER_FLAG_M; + _Z_CLEAR_FLAG(interest_flags, _Z_INTEREST_FLAG_CURRENT); + _Z_CLEAR_FLAG(interest_flags, _Z_INTEREST_FLAG_FUTURE); // Process restricted flag if (_Z_HAS_FLAG(interest_flags, _Z_INTEREST_FLAG_RESTRICTED)) { // Set Named & Mapping flags _Bool has_kesuffix = _z_keyexpr_has_suffix(decl->_keyexpr); if (has_kesuffix) { - interest_flags |= _Z_DECL_SUBSCRIBER_FLAG_N; + _Z_SET_FLAG(interest_flags, _Z_DECL_SUBSCRIBER_FLAG_N); } if (_z_keyexpr_is_local(&decl->_keyexpr)) { - interest_flags |= _Z_DECL_SUBSCRIBER_FLAG_M; + _Z_SET_FLAG(interest_flags, _Z_DECL_SUBSCRIBER_FLAG_M); } // Set decl flags & keyexpr _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, decl->interest_flags)); @@ -398,13 +398,13 @@ int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t } } // Replace named & mapping by current & future flags - decl->interest_flags &= ~_Z_DECL_SUBSCRIBER_FLAG_M; - decl->interest_flags &= ~_Z_DECL_SUBSCRIBER_FLAG_N; + _Z_CLEAR_FLAG(decl->interest_flags, _Z_DECL_SUBSCRIBER_FLAG_M); + _Z_CLEAR_FLAG(decl->interest_flags, _Z_DECL_SUBSCRIBER_FLAG_N); if (_Z_HAS_FLAG(header, _Z_INTEREST_FLAG_CURRENT)) { - decl->interest_flags |= _Z_INTEREST_FLAG_CURRENT; + _Z_SET_FLAG(decl->interest_flags, _Z_INTEREST_FLAG_CURRENT); } if (_Z_HAS_FLAG(header, _Z_INTEREST_FLAG_FUTURE)) { - decl->interest_flags |= _Z_INTEREST_FLAG_FUTURE; + _Z_SET_FLAG(decl->interest_flags, _Z_INTEREST_FLAG_FUTURE); } // Decode extention if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) {