2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0.
4
4
*/
5
+ #include " aws/crt/Types.h"
6
+ #include " aws/iotdevice/device_defender.h"
5
7
#include < aws/common/clock.h>
6
8
#include < aws/iotdevicedefender/DeviceDefender.h>
7
9
@@ -38,33 +40,32 @@ namespace Aws
38
40
void *cancellationUserdata) noexcept
39
41
: OnTaskCancelled(std::move(onCancelled)), cancellationUserdata(cancellationUserdata),
40
42
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}
54
47
{
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
+ }
55
57
}
56
58
57
59
ReportTask::ReportTask (ReportTask &&toMove) noexcept
58
60
: OnTaskCancelled(std::move(toMove.OnTaskCancelled)), cancellationUserdata(toMove.cancellationUserdata),
59
61
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)
61
64
{
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 ;
66
68
toMove.m_status = ReportTaskStatus::Stopped;
67
- toMove.m_taskConfig = {0 };
68
69
toMove.m_owningTask = nullptr ;
69
70
toMove.m_lastError = AWS_ERROR_UNKNOWN;
70
71
}
@@ -79,16 +80,14 @@ namespace Aws
79
80
cancellationUserdata = toMove.cancellationUserdata ;
80
81
m_allocator = toMove.m_allocator ;
81
82
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 ;
84
84
m_owningTask = toMove.m_owningTask ;
85
85
m_lastError = toMove.m_lastError ;
86
86
87
87
toMove.OnTaskCancelled = nullptr ;
88
88
toMove.cancellationUserdata = nullptr ;
89
89
toMove.m_allocator = nullptr ;
90
90
toMove.m_status = ReportTaskStatus::Stopped;
91
- toMove.m_taskConfig = {0 };
92
91
toMove.m_owningTask = nullptr ;
93
92
toMove.m_lastError = AWS_ERROR_UNKNOWN;
94
93
}
@@ -100,10 +99,11 @@ namespace Aws
100
99
101
100
int ReportTask::StartTask () noexcept
102
101
{
103
- if (this ->GetStatus () == ReportTaskStatus::Ready || this ->GetStatus () == ReportTaskStatus::Stopped)
102
+ if (m_taskConfig != nullptr && (this ->GetStatus () == ReportTaskStatus::Ready || this ->GetStatus () == ReportTaskStatus::Stopped) )
104
103
{
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 ()));
107
107
108
108
if (this ->m_owningTask == nullptr )
109
109
{
@@ -123,15 +123,17 @@ namespace Aws
123
123
{
124
124
if (this ->GetStatus () == ReportTaskStatus::Running)
125
125
{
126
- aws_iotdevice_defender_v1_stop_task (this ->m_owningTask );
126
+ aws_iotdevice_defender_task_clean_up (this ->m_owningTask );
127
127
this ->m_owningTask = nullptr ;
128
128
}
129
129
}
130
130
131
131
ReportTask::~ReportTask ()
132
132
{
133
133
StopTask ();
134
+ aws_iotdevice_defender_config_clean_up (m_taskConfig);
134
135
this ->m_owningTask = nullptr ;
136
+ this ->m_taskConfig = nullptr ;
135
137
this ->m_allocator = nullptr ;
136
138
this ->OnTaskCancelled = nullptr ;
137
139
this ->cancellationUserdata = nullptr ;
@@ -199,4 +201,4 @@ namespace Aws
199
201
}
200
202
201
203
} // namespace Iotdevicedefenderv1
202
- } // namespace Aws
204
+ } // namespace Aws
0 commit comments