From 7d64e765ca7f30d25ae4dd39873bd82a58a1befc Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Mon, 29 Jul 2024 17:05:05 +0200 Subject: [PATCH] fixup! Add session_id to endpoint logs --- api/cloud/oc_cloud.c | 38 ++++++++++++++++++++++++++++++++- api/cloud/oc_cloud_deregister.c | 9 +------- api/cloud/oc_cloud_internal.h | 3 +++ api/cloud/oc_cloud_manager.c | 27 +++-------------------- api/oc_client_api.c | 2 +- api/oc_endpoint.c | 10 +++++---- api/oc_endpoint_internal.h | 2 +- api/oc_session_events.c | 4 ++-- 8 files changed, 54 insertions(+), 41 deletions(-) diff --git a/api/cloud/oc_cloud.c b/api/cloud/oc_cloud.c index 2831ee5f0..224e1efa6 100644 --- a/api/cloud/oc_cloud.c +++ b/api/cloud/oc_cloud.c @@ -128,9 +128,34 @@ oc_cloud_set_endpoint(oc_cloud_context_t *ctx) return success; } +void +oc_cloud_endpoint_log(oc_endpoint_t *endpoint, const char *prefix) +{ +#if OC_INFO_IS_ENABLED + const char *ep_str = ""; + oc_string64_t ep = { 0 }; + if (oc_endpoint_to_string64(endpoint, &ep)) { + ep_str = oc_string(ep); + } +#if OC_DBG_IS_ENABLED + int64_t session_id = + (endpoint->flags & TCP) != 0 ? (int64_t)endpoint->session_id : -1; + OC_CLOUD_INFO("%s%s (session_id=%" PRId64 ")", prefix, ep_str, session_id); +#else /* !OC_DBG_IS_ENABLED */ + OC_CLOUD_INFO("%s%s", prefix, ep_str); +#endif /* OC_DBG_IS_ENABLED */ +#else /* !OC_INFO_IS_ENABLED */ + (void)endpoint; + (void)prefix; +#endif /* OC_INFO_IS_ENABLED */ +} + void oc_cloud_close_endpoint(const oc_endpoint_t *ep) { + if (oc_endpoint_is_empty(ep)) { + return; + } OC_CLOUD_DBG("oc_cloud_close_endpoint"); oc_close_session(ep); } @@ -311,10 +336,21 @@ cloud_ep_session_event_handler(const oc_endpoint_t *endpoint, oc_cloud_context_t *ctx = (oc_cloud_context_t *)user_data; if (oc_endpoint_compare(endpoint, ctx->cloud_ep) != 0) { OC_CLOUD_DBG("session handler skipped: endpoint does not match"); + oc_endpoint_log("cloud->ep: ", ctx->cloud_ep); + oc_endpoint_log("endpoint: ", endpoint); return; } - OC_CLOUD_DBG("cloud_ep_session_event_handler ep_state: %d (current: %d)", +#if OC_DBG_IS_ENABLED + int64_t session_id = + (ctx->cloud_ep->flags & TCP) != 0 ? (int64_t)ctx->cloud_ep->session_id : -1; + OC_CLOUD_DBG("ep(%" PRId64 ") ep_state: %d (current: %d)", session_id, (int)state, (int)ctx->cloud_ep_state); + +#endif /* OC_DBG_IS_ENABLED */ + if (ctx->cloud_ep->session_id == 0 && state == OC_SESSION_CONNECTED) { + OC_CLOUD_DBG("session_id set to %" PRIu32, endpoint->session_id); + ctx->cloud_ep->session_id = endpoint->session_id; + } bool state_changed = ctx->cloud_ep_state != state; if (!state_changed) { return; diff --git a/api/cloud/oc_cloud_deregister.c b/api/cloud/oc_cloud_deregister.c index 423f4290e..9e0a0ff1a 100644 --- a/api/cloud/oc_cloud_deregister.c +++ b/api/cloud/oc_cloud_deregister.c @@ -187,14 +187,7 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout, &conf)) { goto error; } -#if OC_INFO_IS_ENABLED - const char *ep_str = ""; - oc_string64_t ep = { 0 }; - if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { - ep_str = oc_string(ep); - } - OC_CLOUD_INFO("Deregistering from %s", ep_str); -#endif /* OC_INFO_IS_ENABLED */ + oc_cloud_endpoint_log(ctx->cloud_ep, "Deregistering from "); if (oc_cloud_access_deregister( conf, oc_string(ctx->store.uid), useAccessToken ? oc_string(ctx->store.access_token) : NULL)) { diff --git a/api/cloud/oc_cloud_internal.h b/api/cloud/oc_cloud_internal.h index 26ce52bf0..7a471800c 100644 --- a/api/cloud/oc_cloud_internal.h +++ b/api/cloud/oc_cloud_internal.h @@ -127,6 +127,9 @@ void oc_cloud_update_by_resource(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data) OC_NONNULL(); +/** Log cloud endpoint address and session id */ +void oc_cloud_endpoint_log(oc_endpoint_t *ep, const char *prefix) OC_NONNULL(); + #ifdef __cplusplus } #endif diff --git a/api/cloud/oc_cloud_manager.c b/api/cloud/oc_cloud_manager.c index 4acf17c2b..c5d1ca2e2 100644 --- a/api/cloud/oc_cloud_manager.c +++ b/api/cloud/oc_cloud_manager.c @@ -418,14 +418,7 @@ cloud_manager_register_async(void *data) ctx->schedule_action.timeout, &conf)) { goto retry; } -#if OC_INFO_IS_ENABLED - const char *ep_str = ""; - oc_string64_t ep = { 0 }; - if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { - ep_str = oc_string(ep); - } - OC_CLOUD_INFO("Registering to %s", ep_str); -#endif /* OC_INFO_IS_ENABLED */ + oc_cloud_endpoint_log(ctx->cloud_ep, "Registering to "); if (!oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { @@ -664,14 +657,7 @@ cloud_manager_login_async(void *data) ctx->schedule_action.timeout, &conf)) { goto retry; } -#if OC_INFO_IS_ENABLED - const char *ep_str = ""; - oc_string64_t ep = { 0 }; - if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { - ep_str = oc_string(ep); - } - OC_CLOUD_INFO("Login to %s", ep_str); -#endif /* OC_INFO_IS_ENABLED */ + oc_cloud_endpoint_log(ctx->cloud_ep, "Login to "); if (!oc_cloud_access_login(conf, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { OC_CLOUD_ERR("failed to sent sign in request to cloud"); @@ -892,14 +878,7 @@ cloud_manager_refresh_token_async(void *data) goto retry; } -#if OC_INFO_IS_ENABLED - const char *ep_str = ""; - oc_string64_t ep = { 0 }; - if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { - ep_str = oc_string(ep); - } - OC_CLOUD_INFO("Refreshing access token for %s", ep_str); -#endif /* OC_INFO_IS_ENABLED */ + oc_cloud_endpoint_log(ctx->cloud_ep, "Refreshing access token for "); if (!oc_cloud_access_refresh_access_token( conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid), oc_string(ctx->store.refresh_token))) { diff --git a/api/oc_client_api.c b/api/oc_client_api.c index f95f103f7..cff391775 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -961,7 +961,7 @@ oc_do_ip_discovery_at_endpoint(const char *rt, oc_discovery_handler_t handler, void oc_close_session(const oc_endpoint_t *endpoint) { - oc_endpoint_short_log("oc_close_session:", endpoint); + oc_endpoint_log("oc_close_session: ", endpoint); #ifdef OC_SECURITY if ((endpoint->flags & SECURED) != 0) { oc_tls_close_connection(endpoint); diff --git a/api/oc_endpoint.c b/api/oc_endpoint.c index 41fb61c99..6f1e77802 100644 --- a/api/oc_endpoint.c +++ b/api/oc_endpoint.c @@ -733,6 +733,8 @@ oc_endpoint_is_empty(const oc_endpoint_t *endpoint) { oc_endpoint_t empty; memset(&empty, 0, sizeof(oc_endpoint_t)); + // ignore the next pointer + empty.next = endpoint->next; // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison) return memcmp(&empty, endpoint, sizeof(oc_endpoint_t)) == 0; } @@ -807,19 +809,19 @@ oc_endpoint_set_local_address(oc_endpoint_t *ep, unsigned interface_index) #endif /* OC_CLIENT */ void -oc_endpoint_short_log(const char *prefix, const oc_endpoint_t *endpoint) +oc_endpoint_log(const char *prefix, const oc_endpoint_t *endpoint) { -#if OC_DBG_IS_ENABLED // GCOVR_EXCL_START +#if OC_DBG_IS_ENABLED oc_string64_t endpoint_str; oc_endpoint_to_string64(endpoint, &endpoint_str); int64_t session_id = (endpoint->flags & TCP) != 0 ? (int64_t)endpoint->session_id : -1; - OC_ERR("%s endpoint(addr=%s, session_id=%" PRId64 ")", prefix, + OC_ERR("%sendpoint(addr=%s, session_id=%" PRId64 ")", prefix, oc_string(endpoint_str), session_id); - // GCOVR_EXCL_STOP #else /* !OC_DBG_IS_ENABLED */ (void)prefix; (void)endpoint; #endif /* OC_DBG_IS_ENABLED */ + // GCOVR_EXCL_STOP } diff --git a/api/oc_endpoint_internal.h b/api/oc_endpoint_internal.h index 57acb62fe..30d6e541f 100644 --- a/api/oc_endpoint_internal.h +++ b/api/oc_endpoint_internal.h @@ -127,7 +127,7 @@ bool oc_endpoint_to_string64(const oc_endpoint_t *endpoint, oc_string64_t *endpoint_str); /** Log session id and address. */ -void oc_endpoint_short_log(const char *prefix, const oc_endpoint_t *endpoint) +void oc_endpoint_log(const char *prefix, const oc_endpoint_t *endpoint) OC_NONNULL(); #ifdef __cplusplus diff --git a/api/oc_session_events.c b/api/oc_session_events.c index 32de92a60..53caffd91 100644 --- a/api/oc_session_events.c +++ b/api/oc_session_events.c @@ -136,7 +136,7 @@ oc_session_start_event(const oc_endpoint_t *endpoint) // only a specific session should be connected assert(endpoint->session_id != 0); - oc_endpoint_short_log("oc_session_start_event:", endpoint); + oc_endpoint_log("oc_session_start_event: ", endpoint); oc_endpoint_t *ep = oc_new_endpoint(); memcpy(ep, endpoint, sizeof(oc_endpoint_t)); @@ -159,7 +159,7 @@ oc_session_end_event(const oc_endpoint_t *endpoint) // only a specific session should be disconnected assert(endpoint->session_id != 0); - oc_endpoint_short_log("oc_session_end_event:", endpoint); + oc_endpoint_log("oc_session_end_event: ", endpoint); oc_endpoint_t *ep = oc_new_endpoint(); memcpy(ep, endpoint, sizeof(oc_endpoint_t));