-
Notifications
You must be signed in to change notification settings - Fork 110
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
Changes from 16 commits
1af3791
84e22c9
4c814f4
fee4a48
5633de3
df46e84
7f27d2d
2422ccd
809e6c1
2110c76
fef2311
36381c0
cc45a5b
12f4338
719764c
5a0a956
26f8b83
1e93643
2e6328a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
+33 −5 | .github/workflows/ci.yml | |
+1 −1 | CMakeLists.txt | |
+21 −36 | README.md | |
+349 −22 | include/aws/iotdevice/device_defender.h | |
+5 −3 | include/aws/iotdevice/exports.h | |
+5 −0 | include/aws/iotdevice/iotdevice.h | |
+1,026 −264 | source/device_defender.c | |
+14 −1 | source/iotdevice.c | |
+4 −1 | tests/CMakeLists.txt | |
+128 −40 | tests/aws_iot_devicedefender_client_test.c | |
+431 −67 | tests/metrics_tests.c | |
+7 −11 | tests/secure_tunneling_tests.c |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
|
@@ -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) | ||
{ | ||
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this constructor necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) m_owningTask(toMove.m_owningTask) // copy There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trivial: there's no point in re-raising the error There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change, if the first There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -199,4 +211,4 @@ namespace Aws | |
} | ||
|
||
} // namespace Iotdevicedefenderv1 | ||
} // namespace Aws | ||
} // namespace Aws |
There was a problem hiding this comment.
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 anAWS_ERROR_
. They're conceptually two different types, even if they both happen to be intsconsider either
or