Skip to content

Commit

Permalink
Minor Fix in DC abort logic and added more logs for debugging purpose (
Browse files Browse the repository at this point in the history
…#413)

* Minor Fix in DC abort logic and added more logs for debugging purpose

* initialized resource manager

* resolved indentation errors
  • Loading branch information
HarshGandhi-AWS authored May 25, 2023
1 parent 022f8ac commit 239d10d
Showing 1 changed file with 111 additions and 36 deletions.
147 changes: 111 additions & 36 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ void shutdown()
*/
void deviceClientAbort(const string &reason)
{
if (resourceManager != NULL)
{
resourceManager->disconnect();
resourceManager.reset();
}
LoggerFactory::getLoggerInstance()->shutdown();
cout << "AWS IoT Device Client must abort execution, reason: " << reason << endl;
cout << "Please check the AWS IoT Device Client logs for more information" << endl;
exit(EXIT_FAILURE);
Expand All @@ -184,7 +190,6 @@ void attemptConnection()
"IoT credentials, "
"configuration and/or certificate policy. ***",
DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Failed to establish MQTT connection due to credential/configuration error");
return true;
}
Expand All @@ -204,7 +209,6 @@ void attemptConnection()
catch (const std::exception &e)
{
LOGM_ERROR(TAG, "Error attempting to connect: %s", e.what());
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Failure from attemptConnection");
}
}
Expand Down Expand Up @@ -275,7 +279,6 @@ namespace Aws
TAG,
"*** %s: Aborting program due to unrecoverable feature error! ***",
DeviceClient::DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort(feature->getName() + " encountered an error");
#endif
}
Expand All @@ -294,8 +297,11 @@ int main(int argc, char *argv[])
}
if (!Config::ParseCliArgs(argc, argv, cliArgs) || !config.init(cliArgs))
{
LoggerFactory::getLoggerInstance()->shutdown();
return 1;
LOGM_ERROR(
TAG,
"*** %s: AWS IoT Device Client must abort execution, reason: Invalid configuration ***",
DC_FATAL_ERROR);
deviceClientAbort("Invalid configuration");
}

if (!LoggerFactory::reconfigure(config.config) &&
Expand Down Expand Up @@ -329,33 +335,31 @@ int main(int argc, char *argv[])

auto listener = std::make_shared<DefaultClientBaseNotifier>();
resourceManager = std::make_shared<SharedCrtResourceManager>();

if (!resourceManager.get()->initialize(config.config, features))
{
LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Failed to initialize AWS CRT SDK");
}

#if !defined(EXCLUDE_FP)
#if !defined(EXCLUDE_FP) && !defined(DISABLE_MQTT)
if (config.config.fleetProvisioning.enabled &&
!config.config.fleetProvisioningRuntimeConfig.completedFleetProvisioning)
{

/*
* Establish MQTT connection using claim certificates and private key to provision the device/thing.
*/
# if !defined(DISABLE_MQTT)
/**
* init() is currently responsible for making sure only 1 instance of Device Client is running at a given time.
* In the future, we may want to move other Device Client startup logic into this function.
* returns false if an exception is thrown
*/
if (!init(argc, argv))
{
return 1;
LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR);
deviceClientAbort("An instance of Device Client is already running.");
}
/*
* Establish MQTT connection using claim certificates and private key to provision the device/thing.
*/
attemptConnection();
# endif

/*
* Provision Device, parse new runtime conf file and validate its content.
Expand All @@ -373,16 +377,22 @@ int main(int argc, char *argv[])
"Please verify your AWS IoT credentials, "
"configuration, Fleet Provisioning Template, claim certificate and policy used. ***",
DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Fleet provisioning failed");
}
resourceManager->disconnect();
}
#else
if (config.config.fleetProvisioning.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Fleet Provisioning configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort("Invalid configuration. Fleet Provisioning configuration is enabled but feature is not "
"compiled into binary.");
}
#endif
/*
* Establish MQTT connection using permanent certificate and private key to start and run AWS IoT Device Client
* features.
*/

#if !defined(DISABLE_MQTT)
/**
* init() is currently responsible for making sure only 1 instance of Device Client is running at a given time.
Expand All @@ -391,29 +401,42 @@ int main(int argc, char *argv[])
*/
if (!init(argc, argv))
{
return 1;
LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR);
deviceClientAbort("An instance of Device Client is already running.");
}
/*
* Establish MQTT connection using permanent certificate and private key to start and run AWS IoT Device Client
* features.
*/
attemptConnection();
#endif

#if defined(EXCLUDE_SECURE_ELEMENT)
#if defined(EXCLUDE_SECURE_ELEMENT) && !defined(DISABLE_MQTT)
if (config.config.secureElement.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Secure Element configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Invalid configuration");
}
else
{
LOG_INFO(TAG, "Provisioning with Secure Elements is disabled");
}
#else
if (config.config.secureElement.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Secure Element configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort(
"Invalid configuration. Secure Element configuration is enabled but feature is not compiled into binary.");
}
#endif

#if !defined(EXCLUDE_SHADOW)
# if !defined(EXCLUDE_CONFIG_SHADOW)
#if !defined(EXCLUDE_SHADOW) && !defined(EXCLUDE_CONFIG_SHADOW) && !defined(DISABLE_MQTT)
if (config.config.configShadow.enabled)
{
LOG_INFO(TAG, "Config shadow is enabled");
Expand All @@ -426,10 +449,18 @@ int main(int argc, char *argv[])
{
LOG_INFO(TAG, "Config shadow is disabled");
}
# endif
#else
if (config.config.configShadow.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Config Shadow configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort("Invalid configuration");
}
#endif

#if !defined(EXCLUDE_JOBS)
#if !defined(EXCLUDE_JOBS) && !defined(DISABLE_MQTT)
if (config.config.jobs.enabled)
{
shared_ptr<JobsFeature> jobs;
Expand All @@ -443,6 +474,14 @@ int main(int argc, char *argv[])
LOG_INFO(TAG, "Jobs is disabled");
features->add(JobsFeature::NAME, nullptr);
}
#else
if (config.config.jobs.enabled)
{
LOGM_ERROR(
TAG, "*** %s: Jobs configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR);
deviceClientAbort(
"Invalid configuration. Config Shadow configuration is enabled but feature is not compiled into binary.");
}
#endif

#if !defined(EXCLUDE_ST)
Expand All @@ -459,9 +498,19 @@ int main(int argc, char *argv[])
LOG_INFO(TAG, "Secure Tunneling is disabled");
features->add(SecureTunnelingFeature::NAME, nullptr);
}
#else
if (config.config.tunneling.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Secure Tunneling configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort("Invalid configuration. Secure Tunneling configuration is enabled but feature is not "
"compiled into binary.");
}
#endif

#if !defined(EXCLUDE_DD)
#if !defined(EXCLUDE_DD) && !defined(DISABLE_MQTT)
if (config.config.deviceDefender.enabled)
{
shared_ptr<DeviceDefenderFeature> deviceDefender;
Expand All @@ -475,10 +524,19 @@ int main(int argc, char *argv[])
LOG_INFO(TAG, "Device Defender is disabled");
features->add(DeviceDefenderFeature::NAME, nullptr);
}
#else
if (config.config.sampleShadow.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Device Defender configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort(
"Invalid configuration. Device Defender configuration is enabled but feature is not compiled into binary.");
}
#endif

#if !defined(EXCLUDE_SHADOW)
# if !defined(EXCLUDE_SAMPLE_SHADOW)
#if !defined(EXCLUDE_SHADOW) && !defined(EXCLUDE_SAMPLE_SHADOW) && !defined(DISABLE_MQTT)
if (config.config.sampleShadow.enabled)
{
shared_ptr<SampleShadowFeature> sampleShadow;
Expand All @@ -492,11 +550,19 @@ int main(int argc, char *argv[])
LOG_INFO(TAG, "Sample shadow is disabled");
features->add(SampleShadowFeature::NAME, nullptr);
}
# endif
#else
if (config.config.sampleShadow.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: Sample Shadow configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort(
"Invalid configuration. Sample Shadow configuration is enabled but feature is not compiled into binary.");
}
#endif

#if !defined(EXCLUDE_SAMPLES)
# if !defined(EXCLUDE_PUBSUB)
#if !defined(EXCLUDE_SAMPLES) && !defined(EXCLUDE_PUBSUB) && !defined(DISABLE_MQTT)
if (config.config.pubSub.enabled)
{
shared_ptr<PubSubFeature> pubSub;
Expand All @@ -510,10 +576,19 @@ int main(int argc, char *argv[])
LOG_INFO(TAG, "Pub Sub is disabled");
features->add(PubSubFeature::NAME, nullptr);
}
# endif
#else
if (config.config.pubSub.enabled)
{
LOGM_ERROR(
TAG,
"*** %s: PubSub sample configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
deviceClientAbort(
"Invalid configuration. PubSub sample configuration is enabled but feature is not compiled into binary.");
}
#endif

#if !defined(EXCLUDE_SENSOR_PUBLISH)
#if !defined(EXCLUDE_SENSOR_PUBLISH) && !defined(DISABLE_MQTT)
if (config.config.sensorPublish.enabled)
{
shared_ptr<SensorPublishFeature> sensorPublish;
Expand All @@ -534,8 +609,8 @@ int main(int argc, char *argv[])
TAG,
"*** %s: Sensor Publish configuration is enabled but feature is not compiled into binary.",
DC_FATAL_ERROR);
LoggerFactory::getLoggerInstance()->shutdown();
deviceClientAbort("Invalid configuration");
deviceClientAbort(
"Invalid configuration. Sensor Publish configuration is enabled but feature is not compiled into binary.");
}
#endif

Expand Down

0 comments on commit 239d10d

Please sign in to comment.