Skip to content

Commit ff08290

Browse files
committed
Updating DeviceDefender interfaces.
1 parent 9f78f83 commit ff08290

File tree

4 files changed

+67
-85
lines changed

4 files changed

+67
-85
lines changed

devicedefender/include/aws/iotdevicedefender/DeviceDefender.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ namespace Aws
5151
public:
5252
~ReportTask();
5353
ReportTask(const ReportTask &) = delete;
54-
ReportTask(ReportTask &&) noexcept;
5554
ReportTask &operator=(const ReportTask &) = delete;
56-
ReportTask &operator=(ReportTask &&) noexcept;
5755

5856
/**
5957
* Initiates stopping of the Defender V1 task.
@@ -82,9 +80,11 @@ namespace Aws
8280
private:
8381
Crt::Allocator *m_allocator;
8482
ReportTaskStatus m_status;
85-
aws_iotdevice_defender_report_task_config m_taskConfig;
86-
aws_iotdevice_defender_v1_task *m_owningTask;
83+
aws_iotdevice_defender_task_config *m_taskConfig;
84+
aws_iotdevice_defender_task *m_owningTask;
8785
int m_lastError;
86+
std::shared_ptr<Crt::Mqtt::MqttConnection> m_mqttConnection;
87+
Crt::Io::EventLoopGroup &m_eventLoopGroup;
8888

8989
ReportTask(
9090
Crt::Allocator *allocator,
@@ -141,7 +141,7 @@ namespace Aws
141141
/**
142142
* Builds a device defender v1 task object from the set options.
143143
*/
144-
ReportTask Build() noexcept;
144+
std::shared_ptr<ReportTask> Build() noexcept;
145145

146146
private:
147147
Crt::Allocator *m_allocator;

devicedefender/source/DeviceDefender.cpp

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

@@ -37,100 +40,74 @@ namespace Aws
3740
OnTaskCancelledHandler &&onCancelled,
3841
void *cancellationUserdata) noexcept
3942
: OnTaskCancelled(std::move(onCancelled)), cancellationUserdata(cancellationUserdata),
40-
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_allocator(allocator), m_status(ReportTaskStatus::Ready), m_taskConfig{nullptr}, m_owningTask{nullptr},
44+
m_lastError(0), m_mqttConnection{mqttConnection}, m_eventLoopGroup(eventLoopGroup)
5445
{
55-
}
56-
57-
ReportTask::ReportTask(ReportTask &&toMove) noexcept
58-
: OnTaskCancelled(std::move(toMove.OnTaskCancelled)), cancellationUserdata(toMove.cancellationUserdata),
59-
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)
61-
{
62-
m_taskConfig.cancellation_userdata = this;
63-
toMove.OnTaskCancelled = nullptr;
64-
toMove.cancellationUserdata = nullptr;
65-
toMove.m_allocator = nullptr;
66-
toMove.m_status = ReportTaskStatus::Stopped;
67-
toMove.m_taskConfig = {0};
68-
toMove.m_owningTask = nullptr;
69-
toMove.m_lastError = AWS_ERROR_UNKNOWN;
70-
}
71-
72-
ReportTask &ReportTask::operator=(ReportTask &&toMove) noexcept
73-
{
74-
if (this != &toMove)
46+
(void)networkConnectionSamplePeriodSeconds;
47+
struct aws_byte_cursor thingNameCursor = Crt::ByteCursorFromString(thingName);
48+
int return_code =
49+
aws_iotdevice_defender_config_create(&m_taskConfig, allocator, &thingNameCursor, reportFormat);
50+
if (AWS_OP_SUCCESS == return_code)
7551
{
76-
this->~ReportTask();
77-
78-
OnTaskCancelled = std::move(toMove.OnTaskCancelled);
79-
cancellationUserdata = toMove.cancellationUserdata;
80-
m_allocator = toMove.m_allocator;
81-
m_status = toMove.m_status;
82-
m_taskConfig = std::move(toMove.m_taskConfig);
83-
m_taskConfig.cancellation_userdata = this;
84-
m_owningTask = toMove.m_owningTask;
85-
m_lastError = toMove.m_lastError;
86-
87-
toMove.OnTaskCancelled = nullptr;
88-
toMove.cancellationUserdata = nullptr;
89-
toMove.m_allocator = nullptr;
90-
toMove.m_status = ReportTaskStatus::Stopped;
91-
toMove.m_taskConfig = {0};
92-
toMove.m_owningTask = nullptr;
93-
toMove.m_lastError = AWS_ERROR_UNKNOWN;
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(
55+
m_taskConfig,
56+
aws_timestamp_convert(taskPeriodSeconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
57+
}
58+
else
59+
{
60+
m_lastError = aws_last_error();
9461
}
95-
96-
return *this;
9762
}
9863

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

10166
int ReportTask::StartTask() noexcept
10267
{
103-
if (this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped)
68+
int return_code = AWS_OP_ERR;
69+
if (m_taskConfig != nullptr && !m_lastError &&
70+
(this->GetStatus() == ReportTaskStatus::Ready || this->GetStatus() == ReportTaskStatus::Stopped))
10471
{
105-
106-
this->m_owningTask = aws_iotdevice_defender_v1_report_task(this->m_allocator, &this->m_taskConfig);
107-
108-
if (this->m_owningTask == nullptr)
72+
if (AWS_OP_SUCCESS != aws_iotdevice_defender_task_create(
73+
&m_owningTask,
74+
this->m_taskConfig,
75+
m_mqttConnection->GetUnderlyingConnection(),
76+
aws_event_loop_group_get_next_loop(m_eventLoopGroup.GetUnderlyingHandle())))
10977
{
11078
this->m_lastError = aws_last_error();
111-
aws_raise_error(this->m_lastError);
112-
return AWS_OP_ERR;
11379
}
11480
else
11581
{
11682
this->m_status = ReportTaskStatus::Running;
83+
return_code = AWS_OP_SUCCESS;
11784
}
11885
}
119-
return AWS_OP_SUCCESS;
86+
else
87+
{
88+
aws_raise_error(AWS_ERROR_INVALID_STATE);
89+
}
90+
return return_code;
12091
}
12192

12293
void ReportTask::StopTask() noexcept
12394
{
12495
if (this->GetStatus() == ReportTaskStatus::Running)
12596
{
126-
aws_iotdevice_defender_v1_stop_task(this->m_owningTask);
97+
aws_iotdevice_defender_task_clean_up(this->m_owningTask);
12798
this->m_owningTask = nullptr;
99+
m_status = ReportTaskStatus::Stopped;
128100
}
129101
}
130102

131103
ReportTask::~ReportTask()
132104
{
133105
StopTask();
106+
if (m_taskConfig)
107+
{
108+
aws_iotdevice_defender_config_clean_up(m_taskConfig);
109+
this->m_taskConfig = nullptr;
110+
}
134111
this->m_owningTask = nullptr;
135112
this->m_allocator = nullptr;
136113
this->OnTaskCancelled = nullptr;
@@ -183,10 +160,9 @@ namespace Aws
183160
return *this;
184161
}
185162

186-
ReportTask ReportTaskBuilder::Build() noexcept
163+
std::shared_ptr<ReportTask> ReportTaskBuilder::Build() noexcept
187164
{
188-
189-
return ReportTask(
165+
return std::shared_ptr<ReportTask>(new ReportTask(
190166
m_allocator,
191167
m_mqttConnection,
192168
m_thingName,
@@ -195,8 +171,8 @@ namespace Aws
195171
m_taskPeriodSeconds,
196172
m_networkConnectionSamplePeriodSeconds,
197173
static_cast<OnTaskCancelledHandler &&>(m_onCancelled),
198-
m_cancellationUserdata);
174+
m_cancellationUserdata));
199175
}
200176

201177
} // namespace Iotdevicedefenderv1
202-
} // namespace Aws
178+
} // namespace Aws

devicedefender/tests/DeviceDefenderTest.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
5+
#include "aws/common/error.h"
56
#include <aws/crt/Api.h>
67

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

64-
Aws::Iotdevicedefenderv1::ReportTask task = taskBuilder.Build();
65+
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();
6566

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

68-
task.StartTask();
69-
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task.GetStatus());
70-
task.StopTask();
69+
ASSERT_SUCCESS(task->StartTask());
70+
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task->GetStatus());
71+
ASSERT_FAILS(task->StartTask());
72+
ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE);
73+
task->StopTask();
74+
75+
ASSERT_TRUE(task->GetStatus() == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped);
7176

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

8287
ASSERT_FALSE(mqttClient);
8388

84-
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task.GetStatus());
89+
ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task->GetStatus());
8590
}
8691

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

132-
Aws::Iotdevicedefenderv1::ReportTask task = taskBuilder.Build();
137+
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();
133138

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

136-
ASSERT_INT_EQUALS(AWS_OP_ERR, task.StartTask());
137-
ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task.LastError());
141+
ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task->LastError());
142+
ASSERT_FAILS(task->StartTask());
143+
ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE);
138144

139145
mqttConnection->Disconnect();
140146
ASSERT_TRUE(mqttConnection);
@@ -145,4 +151,4 @@ static int s_TestDeviceDefenderFailedTest(Aws::Crt::Allocator *allocator, void *
145151
return AWS_ERROR_SUCCESS;
146152
}
147153

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

0 commit comments

Comments
 (0)