Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Protect start function from being executed when libpax is already started

## [1.1.0] - 2022-10-29
- Adapt Wifi country settings to ESP IDF v4.4 Wi-Fi API
Expand Down
26 changes: 20 additions & 6 deletions lib/libpax/libpax_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int libpax_deserialize_config(char* source,
struct libpax_config_storage_t storage_buffer;
memcpy(&storage_buffer, source, sizeof(struct libpax_config_storage_t));
if (storage_buffer.major_version != CONFIG_MAJOR_VERSION) {
ESP_LOGE("configuration",
ESP_LOGE("libpax",
"Restoring incompatible config with different MAJOR version: "
"%d.%d instead of %d.%d",
storage_buffer.major_version, storage_buffer.minor_version,
Expand All @@ -75,7 +75,7 @@ int libpax_deserialize_config(char* source,
}
if (storage_buffer.minor_version != CONFIG_MINOR_VERSION) {
ESP_LOGW(
"configuration",
"libpax",
"Restoring config with different MINOR version: %d.%d instead of %d.%d",
storage_buffer.major_version, storage_buffer.minor_version,
CONFIG_MAJOR_VERSION, CONFIG_MINOR_VERSION);
Expand Down Expand Up @@ -108,15 +108,15 @@ int libpax_update_config(struct libpax_config_t* configuration) {

#ifndef LIBPAX_WIFI
if (configuration->wificounter) {
ESP_LOGE("configuration",
ESP_LOGE("libpax",
"Configuration requests Wi-Fi but was disabled at compile time.");
result &= LIBPAX_ERROR_WIFI_NOT_AVAILABLE;
}
#endif

#ifndef LIBPAX_BLE
if (configuration->blecounter) {
ESP_LOGE("configuration",
ESP_LOGE("libpax",
"Configuration requests BLE but was disabled at compile time.");
result &= LIBPAX_ERROR_BLE_NOT_AVAILABLE;
}
Expand All @@ -140,7 +140,7 @@ int libpax_counter_init(void (*init_callback)(void),
uint16_t init_pax_report_interval_sec,
int init_counter_mode) {
if (PaxReportTimer != NULL && xTimerIsTimerActive(PaxReportTimer)) {
ESP_LOGW("initialization", "lib already active. Ignoring new init.");
ESP_LOGW("libpax", "lib already active. Ignoring new init.");
return -1;
}

Expand All @@ -157,11 +157,21 @@ int libpax_counter_init(void (*init_callback)(void),
return 0;
}

#define LIBPAX_STARTED 1
#define LIBPAX_STOPPED 2
int libpax_state = LIBPAX_STOPPED;

int libpax_counter_start() {
if (config_set == 0) {
ESP_LOGE("configuration", "Configuration was not yet set.");
ESP_LOGE("libpax", "Configuration was not yet set, aborting libpax_counter_start.");
return -1;
}

if (libpax_state != LIBPAX_STOPPED) {
ESP_LOGW("libpax", "libpax was not in stopped state, not executing start again.");
return -1;
}

// turn on BT before Wifi, since the ESP32 API coexistence configuration
// option depends on the Bluetooth configuration option
if (current_config.blecounter) {
Expand All @@ -175,6 +185,8 @@ int libpax_counter_start() {
set_wifi_channels(current_config.wifi_channel_map);
set_wifi_rssi_filter(current_config.wifi_rssi_threshold);
}

libpax_state = LIBPAX_STARTED;
return 0;
}

Expand All @@ -186,6 +198,8 @@ int libpax_counter_stop() {
stop_BLE_scan();
xTimerStop(PaxReportTimer, 0);
PaxReportTimer = NULL;

libpax_state = LIBPAX_STOPPED;
return 0;
}

Expand Down
36 changes: 34 additions & 2 deletions test/libpax_test_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,48 @@ void test_integration() {
test_stop();
}

void run_tests() {
void test_dual_start_with_ble() {
struct libpax_config_t configuration;
libpax_default_config(&configuration);
// only blecounter is active
configuration.blecounter = 1;
configuration.wificounter = 0;
libpax_update_config(&configuration);
int err_code = libpax_counter_init(process_count, &count_from_libpax, 1, 1);
TEST_ASSERT_EQUAL(0, err_code);
err_code = libpax_counter_start();
TEST_ASSERT_EQUAL(0, err_code);
// Should not crash the firmware
err_code = libpax_counter_start();
TEST_ASSERT_EQUAL(-1, err_code);
err_code = libpax_counter_stop();
TEST_ASSERT_EQUAL(0, err_code);
}

void test_no_unusual_reset() {
const soc_reset_reason_t reason = esp_rom_get_reset_reason(0);
TEST_ASSERT_MESSAGE(reason != RESET_REASON_CPU0_SW, "Should not be software reset (lib crash?)");
}

int run_tests() {
UNITY_BEGIN();

RUN_TEST(test_no_unusual_reset);

const soc_reset_reason_t reason = esp_rom_get_reset_reason(0);

if (reason == RESET_REASON_CPU0_SW) {
return UNITY_END();
}

RUN_TEST(test_mac_add_bytes);
RUN_TEST(test_collision_add);
RUN_TEST(test_counter_reset);
RUN_TEST(test_config_store);
RUN_TEST(test_dual_start_with_ble);
RUN_TEST(test_integration);

UNITY_END();
return UNITY_END();
}

#ifdef LIBPAX_ARDUINO
Expand Down