Skip to content

Commit

Permalink
fix: bring up logic, add log
Browse files Browse the repository at this point in the history
  • Loading branch information
iChizer0 authored Dec 1, 2023
1 parent 96baf22 commit d669653
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
28 changes: 20 additions & 8 deletions sscma/interface/transport/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,18 @@ class MQTT final : public Supervisable, public Transport {
_interface->remove_pre_down_callback(this);

this->_is_present = false;

_interface = nullptr;
_network = nullptr;
_mqtt_handler_ptr = nullptr;
_interface = nullptr;
_network = nullptr;

if (_buffer) [[likely]] {
delete[] _buffer;
_buffer = nullptr;
}

_mqtt_handler_ptr = nullptr;
}

void poll_from_supervisor() override {
EL_LOGI("[SSCMA] MQTT::poll_from_supervisor()");
auto mqtt_server_config = std::pair<mqtt_sta_e, mqtt_server_config_t>{};
bool mqtt_server_config_synced = true;
{
Expand All @@ -94,6 +93,10 @@ class MQTT final : public Supervisable, public Transport {
}
}

EL_LOGI("[SSCMA] MQTT::poll_from_supervisor() - mqtt_server_config_synced: %d", mqtt_server_config_synced);
EL_LOGI("[SSCMA] MQTT::poll_from_supervisor() - target_status: %d", mqtt_server_config.first);
EL_LOGI("[SSCMA] MQTT::poll_from_supervisor() - mqtt address: %s", mqtt_server_config.second.address);

if (mqtt_server_config_synced) [[likely]] {
auto current_sta = sync_status_from_driver();
if (current_sta < mqtt_server_config.first) [[unlikely]]
Expand Down Expand Up @@ -217,15 +220,14 @@ class MQTT final : public Supervisable, public Transport {
static inline void mqtt_subscribe_callback(char* top, int tlen, char* msg, int mlen) {
if (!_mqtt_handler_ptr) [[unlikely]]
return;

auto this_ptr = static_cast<MQTT*>(_mqtt_handler_ptr);

if (tlen ^ std::strlen(this_ptr->_mqtt_pubsub_config.sub_topic) ||
std::strncmp(top, this_ptr->_mqtt_pubsub_config.sub_topic, tlen) || mlen <= 1)
return;

if (!this_ptr->push_to_buffer(msg, mlen)) [[unlikely]]
EL_LOGI("MQTT buffer may corrupted");
EL_LOGI("[SSCMA] MQTT::mqtt_subscribe_callback() - buffer corrupted");
}

inline bool push_to_buffer(const char* bytes, std::size_t size) {
Expand Down Expand Up @@ -254,14 +256,18 @@ class MQTT final : public Supervisable, public Transport {
}

void bring_up(const std::pair<mqtt_sta_e, mqtt_server_config_t>& config) {
EL_LOGI("[SSCMA] MQTT::bring_up() checking interface status");
if (!_interface->is_interface_up()) [[unlikely]]
return;

auto current_sta = sync_status_from_driver();

EL_LOGI("[SSCMA] MQTT::bring_up() current status: %d, target status: %d", current_sta, config.first);

if (current_sta == mqtt_sta_e::DISCONNECTED) {
if (current_sta > config.first) return;
if (current_sta >= config.first) return;
// TODO: driver change topic callback API
EL_LOGI("[SSCMA] MQTT::bring_up() driver connect: %s:%d", config.second.address, config.second.port);
auto ret = _network->connect(config.second, mqtt_subscribe_callback); // driver connect
if (ret != EL_OK) [[unlikely]]
return;
Expand All @@ -270,11 +276,13 @@ class MQTT final : public Supervisable, public Transport {
}

if (current_sta == mqtt_sta_e::CONNECTED) {
EL_LOGI("[SSCMA] MQTT::bring_up() driver subscribe: %s", _mqtt_pubsub_config.sub_topic);
auto ret = _network->subscribe(_mqtt_pubsub_config.sub_topic,
static_cast<mqtt_qos_t>(_mqtt_pubsub_config.sub_qos)); // driver subscribe
if (ret != EL_OK) [[unlikely]]
return;
_sub_topics_set.emplace(_mqtt_pubsub_config.sub_topic);
EL_LOGI("[SSCMA] MQTT::bring_up() emit_mqtt_discover()");
emit_mqtt_discover();
}
}
Expand All @@ -284,14 +292,18 @@ class MQTT final : public Supervisable, public Transport {

auto current_sta = sync_status_from_driver();

EL_LOGI("[SSCMA] MQTT::set_down() current status: %d, target status: %d", current_sta, config.first);

if (current_sta == mqtt_sta_e::CONNECTED) {
if (current_sta < config.first) return;

EL_LOGI("[SSCMA] MQTT::set_down() driver unsubscribe topic count: %d", _sub_topics_set.size());
if (_sub_topics_set.size()) [[likely]] {
for (const auto& sub_topic : _sub_topics_set) _network->unsubscribe(sub_topic.c_str());
_sub_topics_set.clear(); // clear old topics
}

EL_LOGI("[SSCMA] MQTT::set_down() - driver disconnect()");
auto ret = _network->disconnect(); // driver disconnect
if (ret != EL_OK) [[unlikely]]
return;
Expand Down
18 changes: 16 additions & 2 deletions sscma/interface/wifi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WiFi final : public Supervisable, public StatefulInterface {
~WiFi() = default;

void poll_from_supervisor() override {
EL_LOGI("[SSCMA] WiFi::poll_from_supervisor()");
auto wifi_config = std::pair<wifi_sta_e, wifi_config_t>{};
bool wifi_config_synced = true;
{
Expand All @@ -54,6 +55,10 @@ class WiFi final : public Supervisable, public StatefulInterface {
}
}

EL_LOGI("[SSCMA] WiFi::poll_from_supervisor() - wifi_config_synced: %d", wifi_config_synced);
EL_LOGI("[SSCMA] WiFi::poll_from_supervisor() - target_status: %d", wifi_config.first);
EL_LOGI("[SSCMA] WiFi::poll_from_supervisor() - wifi name: %s", wifi_config.second.name);

if (wifi_config_synced) [[likely]] {
auto current_sta = sync_status_from_driver();
if (current_sta < wifi_config.first) [[unlikely]]
Expand Down Expand Up @@ -102,15 +107,18 @@ class WiFi final : public Supervisable, public StatefulInterface {
void bring_up(const std::pair<wifi_sta_e, wifi_config_t>& config) {
auto current_sta = sync_status_from_driver();

EL_LOGI("[SSCMA] WiFi::bring_up() - current_sta: %d, target_sta: %d", current_sta, config.first);

if (current_sta == wifi_sta_e::UNINTIALIZED) {
if (current_sta > config.first) return;
if (current_sta >= config.first) return;
_network->init(); // driver init
current_sta =
try_ensure_wifi_status_changed_from(current_sta, SSCMA_WIFI_POLL_DELAY_MS, SSCMA_WIFI_POLL_RETRY);
}

if (current_sta == wifi_sta_e::IDLE) {
if (current_sta > config.first) return;
if (current_sta >= config.first) return;
EL_LOGI("[SSCMA] WiFi::bring_up() - wifi name: %s", config.second.name);
auto ret = _network->join(config.second.name, config.second.passwd); // driver join
if (ret != EL_OK) [[unlikely]]
return;
Expand All @@ -119,21 +127,26 @@ class WiFi final : public Supervisable, public StatefulInterface {
}

if (current_sta >= wifi_sta_e::JOINED) {
EL_LOGI("[SSCMA] WiFi::bring_up() - invoke_post_up_callbacks()");
this->invoke_post_up_callbacks(); // StatefulInterface::invoke_post_up_callbacks()
}
}

void set_down(const std::pair<wifi_sta_e, wifi_config_t>& config) {
auto current_sta = sync_status_from_driver();

EL_LOGI("[SSCMA] WiFi::set_down() - current_sta: %d, target_sta: %d", current_sta, config.first);

if (current_sta >= wifi_sta_e::BUSY) {
if (current_sta < config.first) return;
EL_LOGI("[SSCMA] WiFi::set_down() - invoke_pre_down_callbacks()");
this->invoke_pre_down_callbacks(); // StatefulInterface::invoke_pre_down_callbacks()
current_sta = ensure_status_changed_from(current_sta, SSCMA_WIFI_POLL_DELAY_MS);
}

if (current_sta == wifi_sta_e::JOINED) {
if (current_sta < config.first) return;
EL_LOGI("[SSCMA] WiFi::set_down() - driver quit()");
auto ret = _network->quit(); // driver quit
if (ret != EL_OK) [[unlikely]]
return;
Expand All @@ -142,6 +155,7 @@ class WiFi final : public Supervisable, public StatefulInterface {

if (current_sta == wifi_sta_e::IDLE) {
if (current_sta < config.first) return;
EL_LOGI("[SSCMA] WiFi::set_down() - driver deinit()");
_network->deinit(); // driver deinit
current_sta = ensure_status_changed_from(current_sta, SSCMA_WIFI_POLL_DELAY_MS);
}
Expand Down

0 comments on commit d669653

Please sign in to comment.