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 16 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
6 changes: 4 additions & 2 deletions devicedefender/include/aws/iotdevicedefender/DeviceDefender.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,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
76 changes: 44 additions & 32 deletions devicedefender/source/DeviceDefender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#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,34 +39,37 @@ 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)
{
(void)networkConnectionSamplePeriodSeconds;
struct aws_byte_cursor thingNameCursor = Crt::ByteCursorFromString(thingName);
m_lastError =
aws_iotdevice_defender_config_create(&m_taskConfig, allocator, &thingNameCursor, reportFormat);
if (AWS_OP_SUCCESS == m_lastError)
Copy link
Contributor

Choose a reason for hiding this comment

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

trivial: it's a little weird to save the AWS_OP_ result in the same variable that's supposed to hold an AWS_ERROR_. They're conceptually two different types, even if they both happen to be ints

consider either

if (aws_iotdevice_defender_config_create(...) == AWS_OP_SUCCESS)

or

int result = aws_iotdevice_defender_config_create(...);
if (result == AWS_OP_SUCESS)
    ...

{
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();
}
}

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_owningTask(toMove.m_owningTask), m_lastError(toMove.m_lastError),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this constructor necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure. It was already there so I made adjustments as necessary rather than figure out if it was safe to delete.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure you want to support move operations? It's a lot of subtle stuff that can accidentally go wrong. And if we ever need to pass a pointer to the C++ class down into C as user_data then then move is illegal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't, but after evaluating that callback_userdata is given a value of this, it's clear that a move after that won't be valid. This class has to be non-copyable, non-moveable. Making the adjustments

m_mqttConnection(toMove.m_mqttConnection), m_eventLoopGroup(toMove.m_eventLoopGroup)
{
m_taskConfig.cancellation_userdata = this;
toMove.OnTaskCancelled = nullptr;
toMove.cancellationUserdata = nullptr;
toMove.m_allocator = nullptr;
toMove.m_taskConfig = m_taskConfig;
toMove.m_owningTask = m_owningTask;
m_owningTask = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look right to me. Why are we pushing things back into the thing we're moving out of?

For example here are several side-affect sequences, neither of which seem correct:

m_taskConfig(std::move(toMove.m_taskConfig))
toMove.m_taskConfig = m_taskConfig;

m_owningTask(toMove.m_owningTask) // copy
toMove.m_owningTask = m_owningTask // does nothing
m_owningTask = nullptr; // why?
toMove.m_owningTask = nullptr;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted this entire function

toMove.m_status = ReportTaskStatus::Stopped;
toMove.m_taskConfig = {0};
toMove.m_owningTask = nullptr;
toMove.m_lastError = AWS_ERROR_UNKNOWN;
}
Expand All @@ -79,16 +84,14 @@ namespace Aws
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_taskConfig = toMove.m_taskConfig;
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;
}
Expand All @@ -100,37 +103,46 @@ namespace Aws

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

trivial: there's no point in re-raising the 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.

Removed

return AWS_OP_ERR;
}
else
{
this->m_status = ReportTaskStatus::Running;
return_code = AWS_OP_SUCCESS;
}
}
return AWS_OP_SUCCESS;
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 @@ -199,4 +211,4 @@ namespace Aws
}

} // namespace Iotdevicedefenderv1
} // namespace Aws
} // namespace Aws
6 changes: 4 additions & 2 deletions devicedefender/tests/DeviceDefenderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ static int s_TestDeviceDefenderResourceSafety(Aws::Crt::Allocator *allocator, vo

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

task.StartTask();
ASSERT_SUCCESS(task.StartTask());
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task.GetStatus());
task.StopTask();

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

{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&]() { return taskStopped; });
Expand Down Expand Up @@ -145,4 +147,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)