Skip to content

Commit

Permalink
fix: MQTT timeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iChizer0 authored Nov 14, 2023
1 parent 91da055 commit b8d8b03
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
19 changes: 14 additions & 5 deletions sscma/callback/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void set_mqtt_server(const std::vector<std::string>& argv, bool has_reply = true
goto Reply;
}

// if the MQTT server is connected, disconnect it
// // if the MQTT server is connected, disconnect it
// while (--conn_retry && static_resource->network->status() == NETWORK_CONNECTED) {
// // TODO: driver add disconnect API
// ret = static_resource->network->disconnect();
Expand All @@ -130,11 +130,13 @@ void set_mqtt_server(const std::vector<std::string>& argv, bool has_reply = true
// }
// while (--poll_retry && static_resource->network->status() == NETWORK_CONNECTED)
// el_sleep(SSCMA_MQTT_CONN_DELAY_MS);
// if (poll_retry <= 0) [[unlikely]] {
// ret = EL_ETIMOUT;
// goto Reply;
// }
// break;
// }

// check if the MQTT server is disconnected again, if not, return IO error
// ret = static_resource->network->status() == NETWORK_JOINED ? EL_OK : EL_EIO;
// ret = conn_retry >= 0 ? EL_OK : EL_ETIMOUT;
// if (ret != EL_OK) [[unlikely]]
// goto Reply;

Expand All @@ -157,8 +159,15 @@ void set_mqtt_server(const std::vector<std::string>& argv, bool has_reply = true
// wait for the MQTT server to be connected
while (--poll_retry && static_resource->network->status() != NETWORK_CONNECTED)
el_sleep(SSCMA_MQTT_CONN_DELAY_MS);
break;
if (poll_retry <= 0) [[unlikely]] {
ret = EL_ETIMOUT;
goto Reply;
}
break; // break if the MQTT server is connected
}
ret = conn_retry >= 0 ? EL_OK : EL_ETIMOUT;
if (ret != EL_OK) [[unlikely]]
goto Reply;

// chain setup MQTT server publish and subscribe topic (skip checking if the MQTT server is connected)
{
Expand Down
20 changes: 17 additions & 3 deletions sscma/callback/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void set_wireless_network(const std::vector<std::string>& argv, bool has_reply =
// check if the network is idle (or ready to connect)
while (--conn_retry && static_resource->network->status() != NETWORK_IDLE) {
// check if the network is connected
while (static_resource->network->status() == NETWORK_JOINED ||
static_resource->network->status() == NETWORK_CONNECTED) {
while (--conn_retry && (static_resource->network->status() == NETWORK_JOINED ||
static_resource->network->status() == NETWORK_CONNECTED)) {
// disconnect the network if joined or connected (an unsubscribe all API is needed)
ret = static_resource->network->quit();
if (ret != EL_OK) [[unlikely]] {
Expand All @@ -48,6 +48,10 @@ void set_wireless_network(const std::vector<std::string>& argv, bool has_reply =
while (--poll_retry && (static_resource->network->status() == NETWORK_JOINED ||
static_resource->network->status() == NETWORK_CONNECTED))
el_sleep(SSCMA_WIRELESS_NETWORK_CONN_DELAY_MS);
if (poll_retry <= 0) [[unlikely]] {
ret = EL_ETIMOUT;
goto Reply;
}
break; // break if the network is disconnected
}

Expand All @@ -57,6 +61,9 @@ void set_wireless_network(const std::vector<std::string>& argv, bool has_reply =
el_sleep(SSCMA_WIRELESS_NETWORK_CONN_DELAY_MS);
}
}
ret = conn_retry >= 0 ? EL_OK : EL_ETIMOUT;
if (ret != EL_OK) [[unlikely]]
goto Reply;

// check if the network is idle again, if not, return IO error
ret = static_resource->network->status() == NETWORK_IDLE ? EL_OK : EL_EIO;
Expand All @@ -82,8 +89,15 @@ void set_wireless_network(const std::vector<std::string>& argv, bool has_reply =
// wait for the network to be joined
while (--poll_retry && static_resource->network->status() != NETWORK_JOINED)
el_sleep(SSCMA_WIRELESS_NETWORK_CONN_DELAY_MS);
break;
if (poll_retry <= 0) [[unlikely]] {
ret = EL_ETIMOUT;
goto Reply;
}
break; // break if the network is joined
}
ret = conn_retry >= 0 ? EL_OK : EL_ETIMOUT;
if (ret != EL_OK) [[unlikely]]
goto Reply;

// chain setup MQTT server (skip checking if the network is joined)
{
Expand Down
8 changes: 4 additions & 4 deletions sscma/definations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

#define SSCMA_WIRELESS_NETWORK_NAME_LEN 32
#define SSCMA_WIRELESS_NETWORK_PASSWD_LEN 64
#define SSCMA_WIRELESS_NETWORK_CONN_RETRY 5
#define SSCMA_WIRELESS_NETWORK_POLL_RERTY 5
#define SSCMA_WIRELESS_NETWORK_CONN_RETRY 10
#define SSCMA_WIRELESS_NETWORK_POLL_RERTY 10
#define SSCMA_WIRELESS_NETWORK_CONN_DELAY_MS 100

#define SSCMA_MQTT_CONN_RETRY 5
#define SSCMA_MQTT_POLL_RETRY 5
#define SSCMA_MQTT_CONN_RETRY 10
#define SSCMA_MQTT_POLL_RETRY 10
#define SSCMA_MQTT_CONN_DELAY_MS 100
#define SSCMA_MQTT_CLIENT_ID_LEN 32
#define SSCMA_MQTT_ADDRESS_LEN 128
Expand Down

0 comments on commit b8d8b03

Please sign in to comment.