Skip to content

Commit

Permalink
Merge branch 'master' into issue-967
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Jan 31, 2024
2 parents 5c13b51 + 3c94c60 commit c72acb0
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 29 deletions.
31 changes: 22 additions & 9 deletions main/adv_post_async_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
#include "log.h"
static const char* TAG = "ADV_POST_TASK";

typedef enum adv_post_action_e
{
ADV_POST_ACTION_NONE = 0,
ADV_POST_ACTION_POST_ADVS_TO_RUUVI,
ADV_POST_ACTION_POST_ADVS_TO_CUSTOM,
ADV_POST_ACTION_POST_STATS,
ADV_POST_ACTION_POST_ADVS_TO_MQTT,
} adv_post_action_e;

static uint32_t g_adv_post_nonce;
static adv_post_action_e g_adv_post_action = ADV_POST_ACTION_NONE;

Expand Down Expand Up @@ -342,12 +333,15 @@ adv_post_do_async_comm_in_progress(adv_post_state_t* const p_adv_post_state)
{
case ADV_POST_ACTION_POST_ADVS_TO_RUUVI:
p_adv_post_state->flag_need_to_send_advs1 = false;
g_adv_post_action = ADV_POST_ACTION_NONE;
break;
case ADV_POST_ACTION_POST_ADVS_TO_CUSTOM:
p_adv_post_state->flag_need_to_send_advs2 = false;
g_adv_post_action = ADV_POST_ACTION_NONE;
break;
case ADV_POST_ACTION_POST_STATS:
p_adv_post_state->flag_need_to_send_statistics = false;
g_adv_post_action = ADV_POST_ACTION_NONE;
break;
default:
break;
Expand Down Expand Up @@ -454,3 +448,22 @@ adv_post_set_hmac_sha256_key(const char* const p_key_str)
}
return false;
}

void
adv_post_set_adv_post_http_action(const bool flag_post_to_ruuvi)
{
if (flag_post_to_ruuvi)
{
g_adv_post_action = ADV_POST_ACTION_POST_ADVS_TO_RUUVI;
}
else
{
g_adv_post_action = ADV_POST_ACTION_POST_ADVS_TO_CUSTOM;
}
}

adv_post_action_e
adv_post_get_adv_post_action(void)
{
return g_adv_post_action;
}
15 changes: 15 additions & 0 deletions main/adv_post_async_comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
extern "C" {
#endif

typedef enum adv_post_action_e
{
ADV_POST_ACTION_NONE = 0,
ADV_POST_ACTION_POST_ADVS_TO_RUUVI,
ADV_POST_ACTION_POST_ADVS_TO_CUSTOM,
ADV_POST_ACTION_POST_STATS,
ADV_POST_ACTION_POST_ADVS_TO_MQTT,
} adv_post_action_e;

void
adv_post_async_comm_init(void);

Expand All @@ -27,6 +36,12 @@ adv_post_set_default_period(const uint32_t period_ms);
bool
adv_post_set_hmac_sha256_key(const char* const p_key_str);

void
adv_post_set_adv_post_http_action(const bool flag_post_to_ruuvi);

adv_post_action_e
adv_post_get_adv_post_action(void);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions main/http_post_advs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "gw_cfg_default.h"
#include "reset_task.h"
#include "ruuvi_gateway.h"
#include "adv_post_async_comm.h"

#define LOG_LOCAL_LEVEL LOG_LEVEL_INFO
#include "log.h"
Expand Down Expand Up @@ -221,6 +222,8 @@ http_send_advs_internal(
&p_http_async_info->hmac_sha256);
}

adv_post_set_adv_post_http_action(p_params->flag_post_to_ruuvi);

if (!http_send_async(p_http_async_info))
{
LOG_DBG("esp_http_client_cleanup");
Expand Down
15 changes: 11 additions & 4 deletions main/main_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ main_task_handle_sig_log_heap_usage(void)
{
static uint32_t g_heap_usage_stat_cnt = 0;
static uint32_t g_heap_usage_min_free_heap = 0xFFFFFFFFU;
static uint32_t g_heap_usage_max_free_heap = 0;
static uint32_t g_heap_limit_cnt = 0;

const uint32_t free_heap = esp_get_free_heap_size();
Expand All @@ -194,14 +195,19 @@ main_task_handle_sig_log_heap_usage(void)
{
g_heap_usage_min_free_heap = free_heap;
}
if (free_heap > g_heap_usage_max_free_heap)
{
g_heap_usage_max_free_heap = free_heap;
}
}
else
{
LOG_INFO("free heap: %lu", (printf_ulong_t)g_heap_usage_min_free_heap);
if (g_heap_usage_min_free_heap < (RUUVI_FREE_HEAP_LIM_KIB * RUUVI_NUM_BYTES_IN_1KB))
LOG_INFO(
"free heap: %lu .. %lu",
(printf_ulong_t)g_heap_usage_min_free_heap,
(printf_ulong_t)g_heap_usage_max_free_heap);
if (g_heap_usage_max_free_heap < (RUUVI_FREE_HEAP_LIM_KIB * RUUVI_NUM_BYTES_IN_1KB))
{
// TODO: in ESP-IDF v4.x there is API heap_caps_register_failed_alloc_callback,
// which allows to catch 'no memory' event and reboot.
g_heap_limit_cnt += 1;
if (g_heap_limit_cnt >= RUUVI_MAX_LOW_HEAP_MEM_CNT)
{
Expand All @@ -217,6 +223,7 @@ main_task_handle_sig_log_heap_usage(void)
}
g_heap_usage_stat_cnt = 0;
g_heap_usage_min_free_heap = UINT32_MAX;
g_heap_usage_max_free_heap = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion main/ruuvi_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {
#define RUUVI_BITS_PER_BYTE (8U)
#define RUUVI_BYTE_MASK (0xFFU)

#define RUUVI_FREE_HEAP_LIM_KIB (25U)
#define RUUVI_FREE_HEAP_LIM_KIB (50U)
#define RUUVI_MAX_LOW_HEAP_MEM_CNT (5)

#define ADV_POST_DEFAULT_INTERVAL_SECONDS (10)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_adv_post_async_comm/test_adv_post_async_comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ TEST_F(TestAdvPostAsyncComm, test_regular_sequence) // NOLINT
ASSERT_EQ(0, this->m_mqtt_publish_adv_call_cnt);
ASSERT_TRUE(this->m_adv_post_timers_start_timer_sig_do_async_comm);
this->m_adv_post_timers_start_timer_sig_do_async_comm = false;
ASSERT_EQ(ADV_POST_ACTION_POST_ADVS_TO_RUUVI, adv_post_get_adv_post_action());

this->m_hmac_sha256_set_key_for_http_ruuvi_res = true;
ASSERT_TRUE(adv_post_set_hmac_sha256_key("key_str123"));
Expand All @@ -499,6 +500,7 @@ TEST_F(TestAdvPostAsyncComm, test_regular_sequence) // NOLINT
ASSERT_EQ(0, this->m_adv_post_statistics_do_send_call_cnt);
ASSERT_EQ(0, this->m_mqtt_publish_adv_call_cnt);
this->m_http_async_poll_res = false;
ASSERT_EQ(ADV_POST_ACTION_NONE, adv_post_get_adv_post_action());

adv_post_state.flag_need_to_send_advs1 = false;
adv_post_state.flag_need_to_send_advs2 = true;
Expand Down Expand Up @@ -655,6 +657,15 @@ TEST_F(TestAdvPostAsyncComm, test_regular_sequence) // NOLINT
ASSERT_TRUE(this->m_mem_alloc_trace.is_empty());
}

TEST_F(TestAdvPostAsyncComm, test_adv_post_set_adv_post_http_action) // NOLINT
{
ASSERT_EQ(ADV_POST_ACTION_NONE, adv_post_get_adv_post_action());
adv_post_set_adv_post_http_action(true);
ASSERT_EQ(ADV_POST_ACTION_POST_ADVS_TO_RUUVI, adv_post_get_adv_post_action());
adv_post_set_adv_post_http_action(false);
ASSERT_EQ(ADV_POST_ACTION_POST_ADVS_TO_CUSTOM, adv_post_get_adv_post_action());
}

TEST_F(TestAdvPostAsyncComm, test_no_timestamps) // NOLINT
{
this->m_flag_time_is_synchronized = true;
Expand Down
110 changes: 95 additions & 15 deletions tests/test_http_server_cb/test_http_server_cb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,51 @@ gw_status_resume_http_relaying(void)
http_server_resp_t
http_check_post_advs(const http_check_params_t* const p_params, const TimeUnitsSeconds_t timeout_seconds)
{
const http_server_resp_t resp = {
.http_resp_code = HTTP_RESP_CODE_500,
.content_location = HTTP_CONTENT_LOCATION_NO_CONTENT,
.flag_no_cache = true,
.flag_add_header_date = true,
.content_type = HTTP_CONENT_TYPE_TEXT_HTML,
.p_content_type_param = NULL,
.content_len = 0,
.content_encoding = HTTP_CONENT_ENCODING_NONE,
.select_location = {
.memory = {
.p_buf = NULL,
if ((0 == strcmp(p_params->p_url, "https://myserver.com/")) && (GW_CFG_HTTP_AUTH_TYPE_NONE == p_params->auth_type))
{
static const char resp_content[]
= "{\n"
"\t\"status\":\t200,\n"
"\t\"message\":\t\"{}\"\n"
"}";
const char* p_resp = strdup(resp_content);
assert(NULL != p_resp);
const http_server_resp_t resp = {
.http_resp_code = HTTP_RESP_CODE_200,
.content_location = HTTP_CONTENT_LOCATION_HEAP,
.flag_no_cache = true,
.flag_add_header_date = true,
.content_type = HTTP_CONENT_TYPE_APPLICATION_JSON,
.p_content_type_param = NULL,
.content_len = strlen(p_resp),
.content_encoding = HTTP_CONENT_ENCODING_NONE,
.select_location = {
.memory = {
.p_buf = (const uint8_t*)p_resp,
},
},
},
};
return resp;
};
return resp;
}
else
{
const http_server_resp_t resp = {
.http_resp_code = HTTP_RESP_CODE_500,
.content_location = HTTP_CONTENT_LOCATION_NO_CONTENT,
.flag_no_cache = true,
.flag_add_header_date = true,
.content_type = HTTP_CONENT_TYPE_TEXT_HTML,
.p_content_type_param = NULL,
.content_len = 0,
.content_encoding = HTTP_CONENT_ENCODING_NONE,
.select_location = {
.memory = {
.p_buf = NULL,
},
},
};
return resp;
}
}

http_server_resp_t
Expand Down Expand Up @@ -4827,6 +4856,57 @@ TEST_F(TestHttpServerCb, http_server_cb_on_get_validate_url_check_fw_update_url_
ASSERT_TRUE(esp_log_wrapper_is_empty());
}

TEST_F(TestHttpServerCb, http_server_cb_on_get_validate_url_check_post_advs__custom_http) // NOLINT
{
const char* const expected_json = "{\n\t\"status\":\t200,\n\t\"message\":\t\"{}\"\n}";

const char* const p_uri_params
= "validate_type=check_post_advs&url=https://myserver.com/"
"&auth_type=none&use_ssl_client_cert=false&use_ssl_server_cert=false";

const http_server_resp_t resp = http_server_cb_on_get("validate_url", p_uri_params, true, nullptr);

ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code);
ASSERT_EQ(HTTP_CONTENT_LOCATION_HEAP, resp.content_location);
ASSERT_TRUE(resp.flag_no_cache);
ASSERT_EQ(HTTP_CONENT_TYPE_APPLICATION_JSON, resp.content_type);
ASSERT_EQ(nullptr, resp.p_content_type_param);
ASSERT_EQ(string(expected_json), string(reinterpret_cast<const char*>(resp.select_location.memory.p_buf)));
ASSERT_EQ(HTTP_CONENT_ENCODING_NONE, resp.content_encoding);
ASSERT_NE(nullptr, resp.select_location.memory.p_buf);

ASSERT_EQ(
"D http_server: http_server_cb_on_get "
"/validate_url?validate_type=check_post_advs&url=https://myserver.com/"
"&auth_type=none&use_ssl_client_cert=false&use_ssl_server_cert=false\n"
"D http_server: HTTP params: auth_type=none\n"
"D http_server: HTTP params: key 'auth_type=': value (encoded): none\n"
"D http_server: HTTP params: key 'auth_type=': value (decoded): none\n"
"D http_server: HTTP params: url=https://myserver.com/\n"
"D http_server: HTTP params: key 'url=': value (encoded): https://myserver.com/\n"
"D http_server: HTTP params: key 'url=': value (decoded): https://myserver.com/\n"
"D http_server: Can't find key 'user=' in URL params\n"
"E http_server: HTTP params: Can't find 'user='\n"
"D http_server: Can't find key 'encrypted_password=' in URL params\n"
"E http_server: HTTP params: Can't find 'encrypted_password='\n"
"D http_server: Can't find key 'use_saved_password=' in URL params\n"
"E http_server: HTTP params: Can't find 'use_saved_password='\n"
"E http_server: Can't find key: use_saved_password=\n"
"I http_server: Found use_ssl_client_cert: false\n"
"I http_server: Found use_ssl_server_cert: false\n"
"D http_server: Got URL from params: https://myserver.com/\n"
"I http_server: Found validate_type: check_post_advs\n"
"I http_server: Validate URL (POST advs): https://myserver.com/\n"
"I http_server: Validate URL (POST advs): auth_type=none\n"
"I http_server: Validate URL (POST advs): user=NULL\n"
"I http_server: Validate URL (POST advs): use_saved_password=0\n"
"D http_server: Validate URL (POST advs): saved_password=\n"
"D http_server: Validate URL (POST advs): password=NULL\n"
"I http_server: Validate URL (POST advs): use_ssl_client_cert=0\n"
"I http_server: Validate URL (POST advs): use_ssl_server_cert=0\n",
esp_log_wrapper_get_logs());
}

TEST_F(TestHttpServerCb, test_http_server_cb_on_user_req__update_cycle_regular__latest_and_beta) // NOLINT
{
{
Expand Down

0 comments on commit c72acb0

Please sign in to comment.