Skip to content

Commit 1af3791

Browse files
committed
Updating DeviceDefender interfaces.
1 parent d377f2c commit 1af3791

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

devicedefender/include/aws/iotdevicedefender/DeviceDefender.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ namespace Aws
8282
private:
8383
Crt::Allocator *m_allocator;
8484
ReportTaskStatus m_status;
85-
aws_iotdevice_defender_report_task_config m_taskConfig;
86-
aws_iotdevice_defender_v1_task *m_owningTask;
85+
aws_iotdevice_defender_task_config *m_taskConfig;
86+
aws_iotdevice_defender_task *m_owningTask;
8787
int m_lastError;
88+
std::shared_ptr<Crt::Mqtt::MqttConnection> m_mqttConnection;
89+
Crt::Io::EventLoopGroup &m_eventLoopGroup;
8890

8991
ReportTask(
9092
Crt::Allocator *allocator,

devicedefender/source/DeviceDefender.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5+
#include "aws/crt/Types.h"
6+
#include "aws/iotdevice/device_defender.h"
57
#include <aws/common/clock.h>
68
#include <aws/iotdevicedefender/DeviceDefender.h>
79

@@ -38,33 +40,32 @@ namespace Aws
3840
void *cancellationUserdata) noexcept
3941
: OnTaskCancelled(std::move(onCancelled)), cancellationUserdata(cancellationUserdata),
4042
m_allocator(allocator), m_status(ReportTaskStatus::Ready),
41-
m_taskConfig{mqttConnection.get()->GetUnderlyingConnection(),
42-
ByteCursorFromString(thingName),
43-
aws_event_loop_group_get_next_loop(eventLoopGroup.GetUnderlyingHandle()),
44-
reportFormat,
45-
aws_timestamp_convert(taskPeriodSeconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL),
46-
aws_timestamp_convert(
47-
networkConnectionSamplePeriodSeconds,
48-
AWS_TIMESTAMP_SECS,
49-
AWS_TIMESTAMP_NANOS,
50-
NULL),
51-
ReportTask::s_onDefenderV1TaskCancelled,
52-
this},
53-
m_lastError(0)
43+
m_taskConfig{nullptr},
44+
m_lastError(0),
45+
m_mqttConnection{mqttConnection},
46+
m_eventLoopGroup{eventLoopGroup}
5447
{
48+
struct aws_byte_cursor thingNameCursor = Crt::ByteCursorFromString(thingName);
49+
m_lastError = aws_iotdevice_defender_config_create(&m_taskConfig, allocator, &thingNameCursor, reportFormat);
50+
if (AWS_OP_SUCCESS == m_lastError)
51+
{
52+
aws_iotdevice_defender_config_set_task_cancelation_fn(m_taskConfig, s_onDefenderV1TaskCancelled);
53+
aws_iotdevice_defender_config_set_callback_userdata(m_taskConfig, this);
54+
aws_iotdevice_defender_config_set_task_period_ns(m_taskConfig,
55+
aws_timestamp_convert(taskPeriodSeconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
56+
}
5557
}
5658

5759
ReportTask::ReportTask(ReportTask &&toMove) noexcept
5860
: OnTaskCancelled(std::move(toMove.OnTaskCancelled)), cancellationUserdata(toMove.cancellationUserdata),
5961
m_allocator(toMove.m_allocator), m_status(toMove.m_status), m_taskConfig(std::move(toMove.m_taskConfig)),
60-
m_owningTask(toMove.m_owningTask), m_lastError(toMove.m_lastError)
62+
m_owningTask(toMove.m_owningTask), m_lastError(toMove.m_lastError),
63+
m_mqttConnection(toMove.m_mqttConnection), m_eventLoopGroup(toMove.m_eventLoopGroup)
6164
{
62-
m_taskConfig.cancellation_userdata = this;
63-
toMove.OnTaskCancelled = nullptr;
64-
toMove.cancellationUserdata = nullptr;
65-
toMove.m_allocator = nullptr;
65+
toMove.m_taskConfig = m_taskConfig;
66+
toMove.m_owningTask = m_owningTask;
67+
m_owningTask = nullptr;
6668
toMove.m_status = ReportTaskStatus::Stopped;
67-
toMove.m_taskConfig = {0};
6869
toMove.m_owningTask = nullptr;
6970
toMove.m_lastError = AWS_ERROR_UNKNOWN;
7071
}
@@ -79,16 +80,14 @@ namespace Aws
7980
cancellationUserdata = toMove.cancellationUserdata;
8081
m_allocator = toMove.m_allocator;
8182
m_status = toMove.m_status;
82-
m_taskConfig = std::move(toMove.m_taskConfig);
83-
m_taskConfig.cancellation_userdata = this;
83+
m_taskConfig = toMove.m_taskConfig;
8484
m_owningTask = toMove.m_owningTask;
8585
m_lastError = toMove.m_lastError;
8686

8787
toMove.OnTaskCancelled = nullptr;
8888
toMove.cancellationUserdata = nullptr;
8989
toMove.m_allocator = nullptr;
9090
toMove.m_status = ReportTaskStatus::Stopped;
91-
toMove.m_taskConfig = {0};
9291
toMove.m_owningTask = nullptr;
9392
toMove.m_lastError = AWS_ERROR_UNKNOWN;
9493
}
@@ -100,10 +99,11 @@ namespace Aws
10099

101100
int ReportTask::StartTask() noexcept
102101
{
103-
if (this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped)
102+
if (m_taskConfig != nullptr && (this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped))
104103
{
105-
106-
this->m_owningTask = aws_iotdevice_defender_v1_report_task(this->m_allocator, &this->m_taskConfig);
104+
m_lastError = aws_iotdevice_defender_task_create(&m_owningTask, this->m_taskConfig,
105+
m_mqttConnection->GetUnderlyingConnection(),
106+
aws_event_loop_group_get_next_loop(m_eventLoopGroup.GetUnderlyingHandle()));
107107

108108
if (this->m_owningTask == nullptr)
109109
{
@@ -123,15 +123,17 @@ namespace Aws
123123
{
124124
if (this->GetStatus() == ReportTaskStatus::Running)
125125
{
126-
aws_iotdevice_defender_v1_stop_task(this->m_owningTask);
126+
aws_iotdevice_defender_task_clean_up(this->m_owningTask);
127127
this->m_owningTask = nullptr;
128128
}
129129
}
130130

131131
ReportTask::~ReportTask()
132132
{
133133
StopTask();
134+
aws_iotdevice_defender_config_clean_up(m_taskConfig);
134135
this->m_owningTask = nullptr;
136+
this->m_taskConfig = nullptr;
135137
this->m_allocator = nullptr;
136138
this->OnTaskCancelled = nullptr;
137139
this->cancellationUserdata = nullptr;
@@ -199,4 +201,4 @@ namespace Aws
199201
}
200202

201203
} // namespace Iotdevicedefenderv1
202-
} // namespace Aws
204+
} // namespace Aws

devicedefender/tests/DeviceDefenderTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <aws/iotdevicedefender/DeviceDefender.h>
99
#include <aws/testing/aws_test_harness.h>
1010
#include <utility>
11+
#include <iostream>
1112

1213
static int s_TestDeviceDefenderResourceSafety(Aws::Crt::Allocator *allocator, void *ctx)
1314
{
@@ -145,4 +146,4 @@ static int s_TestDeviceDefenderFailedTest(Aws::Crt::Allocator *allocator, void *
145146
return AWS_ERROR_SUCCESS;
146147
}
147148

148-
AWS_TEST_CASE(DeviceDefenderFailedTest, s_TestDeviceDefenderFailedTest)
149+
AWS_TEST_CASE(DeviceDefenderFailedTest, s_TestDeviceDefenderFailedTest)

0 commit comments

Comments
 (0)