Skip to content

Updated DeviceDefender task interface changes #305

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

Merged
merged 19 commits into from
Aug 3, 2021
Merged
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
10 changes: 5 additions & 5 deletions devicedefender/include/aws/iotdevicedefender/DeviceDefender.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ namespace Aws
public:
~ReportTask();
ReportTask(const ReportTask &) = delete;
ReportTask(ReportTask &&) noexcept;
ReportTask &operator=(const ReportTask &) = delete;
ReportTask &operator=(ReportTask &&) noexcept;

/**
* Initiates stopping of the Defender V1 task.
Expand Down Expand Up @@ -82,9 +80,11 @@ namespace Aws
private:
Crt::Allocator *m_allocator;
ReportTaskStatus m_status;
aws_iotdevice_defender_report_task_config m_taskConfig;
aws_iotdevice_defender_v1_task *m_owningTask;
aws_iotdevice_defender_task_config *m_taskConfig;
aws_iotdevice_defender_task *m_owningTask;
int m_lastError;
std::shared_ptr<Crt::Mqtt::MqttConnection> m_mqttConnection;
Crt::Io::EventLoopGroup &m_eventLoopGroup;

ReportTask(
Crt::Allocator *allocator,
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace Aws
/**
* Builds a device defender v1 task object from the set options.
*/
ReportTask Build() noexcept;
std::shared_ptr<ReportTask> Build() noexcept;

private:
Crt::Allocator *m_allocator;
Expand Down
112 changes: 44 additions & 68 deletions devicedefender/source/DeviceDefender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "aws/common/error.h"
#include "aws/crt/Types.h"
#include "aws/iotdevice/device_defender.h"
#include <aws/common/clock.h>
#include <aws/iotdevicedefender/DeviceDefender.h>

Expand Down Expand Up @@ -37,100 +40,74 @@ namespace Aws
OnTaskCancelledHandler &&onCancelled,
void *cancellationUserdata) noexcept
: OnTaskCancelled(std::move(onCancelled)), cancellationUserdata(cancellationUserdata),
m_allocator(allocator), m_status(ReportTaskStatus::Ready),
m_taskConfig{mqttConnection.get()->GetUnderlyingConnection(),
ByteCursorFromString(thingName),
aws_event_loop_group_get_next_loop(eventLoopGroup.GetUnderlyingHandle()),
reportFormat,
aws_timestamp_convert(taskPeriodSeconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL),
aws_timestamp_convert(
networkConnectionSamplePeriodSeconds,
AWS_TIMESTAMP_SECS,
AWS_TIMESTAMP_NANOS,
NULL),
ReportTask::s_onDefenderV1TaskCancelled,
this},
m_lastError(0)
m_allocator(allocator), m_status(ReportTaskStatus::Ready), m_taskConfig{nullptr}, m_owningTask{nullptr},
m_lastError(0), m_mqttConnection{mqttConnection}, m_eventLoopGroup(eventLoopGroup)
{
}

ReportTask::ReportTask(ReportTask &&toMove) noexcept
: OnTaskCancelled(std::move(toMove.OnTaskCancelled)), cancellationUserdata(toMove.cancellationUserdata),
m_allocator(toMove.m_allocator), m_status(toMove.m_status), m_taskConfig(std::move(toMove.m_taskConfig)),
m_owningTask(toMove.m_owningTask), m_lastError(toMove.m_lastError)
{
m_taskConfig.cancellation_userdata = this;
toMove.OnTaskCancelled = nullptr;
toMove.cancellationUserdata = nullptr;
toMove.m_allocator = nullptr;
toMove.m_status = ReportTaskStatus::Stopped;
toMove.m_taskConfig = {0};
toMove.m_owningTask = nullptr;
toMove.m_lastError = AWS_ERROR_UNKNOWN;
}

ReportTask &ReportTask::operator=(ReportTask &&toMove) noexcept
{
if (this != &toMove)
(void)networkConnectionSamplePeriodSeconds;
struct aws_byte_cursor thingNameCursor = Crt::ByteCursorFromString(thingName);
int return_code =
aws_iotdevice_defender_config_create(&m_taskConfig, allocator, &thingNameCursor, reportFormat);
if (AWS_OP_SUCCESS == return_code)
{
this->~ReportTask();

OnTaskCancelled = std::move(toMove.OnTaskCancelled);
cancellationUserdata = toMove.cancellationUserdata;
m_allocator = toMove.m_allocator;
m_status = toMove.m_status;
m_taskConfig = std::move(toMove.m_taskConfig);
m_taskConfig.cancellation_userdata = this;
m_owningTask = toMove.m_owningTask;
m_lastError = toMove.m_lastError;

toMove.OnTaskCancelled = nullptr;
toMove.cancellationUserdata = nullptr;
toMove.m_allocator = nullptr;
toMove.m_status = ReportTaskStatus::Stopped;
toMove.m_taskConfig = {0};
toMove.m_owningTask = nullptr;
toMove.m_lastError = AWS_ERROR_UNKNOWN;
aws_iotdevice_defender_config_set_task_cancelation_fn(m_taskConfig, s_onDefenderV1TaskCancelled);
aws_iotdevice_defender_config_set_callback_userdata(m_taskConfig, this);
aws_iotdevice_defender_config_set_task_period_ns(
m_taskConfig,
aws_timestamp_convert(taskPeriodSeconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
}
else
{
m_lastError = aws_last_error();
}

return *this;
}

ReportTaskStatus ReportTask::GetStatus() noexcept { return this->m_status; }

int ReportTask::StartTask() noexcept
{
if (this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped)
int return_code = AWS_OP_ERR;
if (m_taskConfig != nullptr && !m_lastError &&
(this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped))
{

this->m_owningTask = aws_iotdevice_defender_v1_report_task(this->m_allocator, &this->m_taskConfig);

if (this->m_owningTask == nullptr)
if (AWS_OP_SUCCESS != aws_iotdevice_defender_task_create(
&m_owningTask,
this->m_taskConfig,
m_mqttConnection->GetUnderlyingConnection(),
aws_event_loop_group_get_next_loop(m_eventLoopGroup.GetUnderlyingHandle())))
{
this->m_lastError = aws_last_error();
aws_raise_error(this->m_lastError);
return AWS_OP_ERR;
}
else
{
this->m_status = ReportTaskStatus::Running;
return_code = AWS_OP_SUCCESS;
}
}
return AWS_OP_SUCCESS;
else
{
aws_raise_error(AWS_ERROR_INVALID_STATE);
}
return return_code;
Copy link
Contributor

Choose a reason for hiding this comment

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

With this change, if the first if statement fails, we'll end up returning AWS_OP_ERR without having ever raised an error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. Raising AWS_ERROR_INVALID_STATE in this situation

}

void ReportTask::StopTask() noexcept
{
if (this->GetStatus() == ReportTaskStatus::Running)
{
aws_iotdevice_defender_v1_stop_task(this->m_owningTask);
aws_iotdevice_defender_task_clean_up(this->m_owningTask);
this->m_owningTask = nullptr;
m_status = ReportTaskStatus::Stopped;
}
}

ReportTask::~ReportTask()
{
StopTask();
if (m_taskConfig)
{
aws_iotdevice_defender_config_clean_up(m_taskConfig);
this->m_taskConfig = nullptr;
}
this->m_owningTask = nullptr;
this->m_allocator = nullptr;
this->OnTaskCancelled = nullptr;
Expand Down Expand Up @@ -183,10 +160,9 @@ namespace Aws
return *this;
}

ReportTask ReportTaskBuilder::Build() noexcept
std::shared_ptr<ReportTask> ReportTaskBuilder::Build() noexcept
{

return ReportTask(
return std::shared_ptr<ReportTask>(new ReportTask(
m_allocator,
m_mqttConnection,
m_thingName,
Expand All @@ -195,8 +171,8 @@ namespace Aws
m_taskPeriodSeconds,
m_networkConnectionSamplePeriodSeconds,
static_cast<OnTaskCancelledHandler &&>(m_onCancelled),
m_cancellationUserdata);
m_cancellationUserdata));
}

} // namespace Iotdevicedefenderv1
} // namespace Aws
} // namespace Aws
28 changes: 17 additions & 11 deletions devicedefender/tests/DeviceDefenderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "aws/common/error.h"
#include <aws/crt/Api.h>

#include <aws/iotdevicecommon/IotDevice.h>
Expand Down Expand Up @@ -61,13 +62,17 @@ static int s_TestDeviceDefenderResourceSafety(Aws::Crt::Allocator *allocator, vo
.WithTaskCancelledHandler(onCancelled)
.WithTaskCancellationUserData(&callbackSuccess);

Aws::Iotdevicedefenderv1::ReportTask task = taskBuilder.Build();
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();

ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task.GetStatus());
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus());

task.StartTask();
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task.GetStatus());
task.StopTask();
ASSERT_SUCCESS(task->StartTask());
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task->GetStatus());
ASSERT_FAILS(task->StartTask());
ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE);
task->StopTask();

ASSERT_TRUE(task->GetStatus() == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped);

{
std::unique_lock<std::mutex> lock(mutex);
Expand All @@ -81,7 +86,7 @@ static int s_TestDeviceDefenderResourceSafety(Aws::Crt::Allocator *allocator, vo

ASSERT_FALSE(mqttClient);

ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task.GetStatus());
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task->GetStatus());
}

return AWS_ERROR_SUCCESS;
Expand Down Expand Up @@ -129,12 +134,13 @@ static int s_TestDeviceDefenderFailedTest(Aws::Crt::Allocator *allocator, void *
.WithTaskPeriodSeconds((uint32_t)1UL)
.WithReportFormat(Aws::Iotdevicedefenderv1::ReportFormat::AWS_IDDRF_SHORT_JSON);

Aws::Iotdevicedefenderv1::ReportTask task = taskBuilder.Build();
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();

ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task.GetStatus());
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus());

ASSERT_INT_EQUALS(AWS_OP_ERR, task.StartTask());
ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task.LastError());
ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task->LastError());
ASSERT_FAILS(task->StartTask());
ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE);

mqttConnection->Disconnect();
ASSERT_TRUE(mqttConnection);
Expand All @@ -145,4 +151,4 @@ static int s_TestDeviceDefenderFailedTest(Aws::Crt::Allocator *allocator, void *
return AWS_ERROR_SUCCESS;
}

AWS_TEST_CASE(DeviceDefenderFailedTest, s_TestDeviceDefenderFailedTest)
AWS_TEST_CASE(DeviceDefenderFailedTest, s_TestDeviceDefenderFailedTest)