Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add support for lastWill and keep alive #468

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion source/SharedCrtResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add documentation around using this feature. I suspect user will also have to create an IoT Rule, add detailed documentation around setup required for this feature to help other users use it with ease. You will also have to update setup file and config file template to help other users set the config easily.

It is not very clear on what is the use case for this feature and why you would like to add this feature to know the device state on when it is disconnected. Can you share more details on that?

Maintaining a shadow for this feature alone is not a good idea since shadow feature is expensive than using MQTT Pub Sub messages to determine the device state.

Also consider using Device Defender feature which in timely manner uploads device side metrics to cloud. Check is you can use any DD metric to determine the device state instead?

connection = mqttClient->NewConnection(clientConfig);

if (config.fleetProvisioningRuntimeConfig.completedFleetProvisioning && config.lastWillTopic.has_value() && config.lastWillMessage.has_value()) {
Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString(config.lastWillMessage->c_str());
if (connection->SetWill(config.lastWillTopic->c_str(), Aws::Crt::Mqtt::QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false, payload)) {
LOG_INFO(TAG, "MQTT connection set will succeeded");
} else {
LOG_INFO(TAG, "MQTT connection set will failed");
Comment on lines +387 to +389
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have more meaning full logs pasted over here? Also DEBUG logs will make more sense over here?

}
}

if (!*connection)
{
LOGM_ERROR(TAG, "MQTT Connection Creation failed with error: %s", ErrorDebugString(connection->LastError()));
Expand Down Expand Up @@ -461,7 +470,18 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config)
LOG_ERROR(TAG, "Device Client is not able to set reconnection settings. Device Client will retry again.");
return RETRY;
}
if (!connection->Connect(config.thingName->c_str(), false))

int keepAliveTimeSecs = 0;
if (config.connectKeepAlive.has_value()) {
keepAliveTimeSecs = *config.connectKeepAlive;
}

int pingTimeoutMs = 0;
if (config.connectTimeout.has_value()) {
pingTimeoutMs = *config.connectTimeout;
}

if (!connection->Connect(config.thingName->c_str(), false, keepAliveTimeSecs, pingTimeoutMs))
{
LOGM_ERROR(TAG, "MQTT Connection failed with error: %s", ErrorDebugString(connection->LastError()));
return RETRY;
Expand Down
44 changes: 44 additions & 0 deletions source/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ constexpr char PlainConfig::JSON_KEY_FLEET_PROVISIONING[];
constexpr char PlainConfig::JSON_KEY_RUNTIME_CONFIG[];
constexpr char PlainConfig::JSON_KEY_SAMPLES[];
constexpr char PlainConfig::JSON_KEY_PUB_SUB[];
constexpr char PlainConfig::JSON_KEY_LAST_WILL_TOPIC[];
constexpr char PlainConfig::JSON_KEY_LAST_WILL_MESSAGE[];
constexpr char PlainConfig::JSON_KEY_CONNECT_TIMEOUT[];
constexpr char PlainConfig::JSON_KEY_CONNECT_KEEPALIVE[];
constexpr char PlainConfig::JSON_KEY_SAMPLE_SHADOW[];
constexpr char PlainConfig::JSON_KEY_CONFIG_SHADOW[];
constexpr char PlainConfig::JSON_KEY_SECURE_ELEMENT[];
Expand Down Expand Up @@ -208,6 +212,30 @@ bool PlainConfig::LoadFromJson(const Crt::JsonView &json)
}
}

jsonKey = JSON_KEY_LAST_WILL_TOPIC;
if (json.ValueExists(jsonKey))
{
lastWillTopic = json.GetString(jsonKey).c_str();
}

jsonKey = JSON_KEY_LAST_WILL_MESSAGE;
if (json.ValueExists(jsonKey))
{
lastWillMessage = json.GetString(jsonKey).c_str();
}

jsonKey = JSON_KEY_CONNECT_TIMEOUT;
if (json.ValueExists(jsonKey))
{
connectTimeout = json.GetInteger(jsonKey);
}

jsonKey = JSON_KEY_CONNECT_KEEPALIVE;
if (json.ValueExists(jsonKey))
{
connectKeepAlive = json.GetInteger(jsonKey);
}

jsonKey = JSON_KEY_SAMPLE_SHADOW;
if (json.ValueExists(jsonKey))
{
Expand Down Expand Up @@ -436,6 +464,22 @@ void PlainConfig::SerializeToObject(Crt::JsonObject &object) const
{
object.WithString(JSON_KEY_THING_NAME, thingName->c_str());
}
if (lastWillTopic.has_value() && lastWillTopic->c_str())
{
object.WithString(JSON_KEY_LAST_WILL_TOPIC, lastWillTopic->c_str());
}
if (lastWillMessage.has_value() && lastWillMessage->c_str())
{
object.WithString(JSON_KEY_LAST_WILL_MESSAGE, lastWillMessage->c_str());
}
if (connectTimeout.has_value() && connectTimeout)
{
object.WithInteger(JSON_KEY_CONNECT_TIMEOUT, *connectTimeout);
}
if (connectKeepAlive.has_value() && connectKeepAlive)
{
object.WithInteger(JSON_KEY_CONNECT_KEEPALIVE, *connectKeepAlive);
}

Crt::JsonObject loggingObject;
logConfig.SerializeToObject(loggingObject);
Expand Down
10 changes: 10 additions & 0 deletions source/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ namespace Aws
static constexpr char JSON_KEY_SAMPLES[] = "samples";
static constexpr char JSON_KEY_PUB_SUB[] = "pub-sub";

static constexpr char JSON_KEY_LAST_WILL_TOPIC[] = "last-will-topic";
static constexpr char JSON_KEY_LAST_WILL_MESSAGE[] = "last-will-message";

static constexpr char JSON_KEY_CONNECT_TIMEOUT[] = "connect-timeout";
static constexpr char JSON_KEY_CONNECT_KEEPALIVE[] = "keep-alive";

static constexpr char JSON_KEY_SAMPLE_SHADOW[] = "sample-shadow";
static constexpr char JSON_KEY_CONFIG_SHADOW[] = "config-shadow";
static constexpr char JSON_KEY_SENSOR_PUBLISH[] = "sensor-publish";
Expand All @@ -107,6 +113,10 @@ namespace Aws
Aws::Crt::Optional<std::string> key;
Aws::Crt::Optional<std::string> rootCa;
Aws::Crt::Optional<std::string> thingName;
Aws::Crt::Optional<std::string> lastWillTopic;
Aws::Crt::Optional<std::string> lastWillMessage;
Aws::Crt::Optional<int> connectTimeout;
Aws::Crt::Optional<int> connectKeepAlive;

std::string lockFilePath{DEFAULT_LOCK_FILE_PATH};

Expand Down