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/common/error.h"
6
+ #include " aws/crt/Types.h"
7
+ #include " aws/iotdevice/device_defender.h"
5
8
#include < aws/common/clock.h>
6
9
#include < aws/iotdevicedefender/DeviceDefender.h>
7
10
@@ -37,100 +40,74 @@ namespace Aws
37
40
OnTaskCancelledHandler &&onCancelled,
38
41
void *cancellationUserdata) noexcept
39
42
: 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)
54
45
{
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)
75
51
{
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 ();
94
61
}
95
-
96
- return *this ;
97
62
}
98
63
99
64
ReportTaskStatus ReportTask::GetStatus () noexcept { return this ->m_status ; }
100
65
101
66
int ReportTask::StartTask () noexcept
102
67
{
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))
104
71
{
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 ())))
109
77
{
110
78
this ->m_lastError = aws_last_error ();
111
- aws_raise_error (this ->m_lastError );
112
- return AWS_OP_ERR;
113
79
}
114
80
else
115
81
{
116
82
this ->m_status = ReportTaskStatus::Running;
83
+ return_code = AWS_OP_SUCCESS;
117
84
}
118
85
}
119
- return AWS_OP_SUCCESS;
86
+ else
87
+ {
88
+ aws_raise_error (AWS_ERROR_INVALID_STATE);
89
+ }
90
+ return return_code;
120
91
}
121
92
122
93
void ReportTask::StopTask () noexcept
123
94
{
124
95
if (this ->GetStatus () == ReportTaskStatus::Running)
125
96
{
126
- aws_iotdevice_defender_v1_stop_task (this ->m_owningTask );
97
+ aws_iotdevice_defender_task_clean_up (this ->m_owningTask );
127
98
this ->m_owningTask = nullptr ;
99
+ m_status = ReportTaskStatus::Stopped;
128
100
}
129
101
}
130
102
131
103
ReportTask::~ReportTask ()
132
104
{
133
105
StopTask ();
106
+ if (m_taskConfig)
107
+ {
108
+ aws_iotdevice_defender_config_clean_up (m_taskConfig);
109
+ this ->m_taskConfig = nullptr ;
110
+ }
134
111
this ->m_owningTask = nullptr ;
135
112
this ->m_allocator = nullptr ;
136
113
this ->OnTaskCancelled = nullptr ;
@@ -183,10 +160,9 @@ namespace Aws
183
160
return *this ;
184
161
}
185
162
186
- ReportTask ReportTaskBuilder::Build () noexcept
163
+ std::shared_ptr< ReportTask> ReportTaskBuilder::Build () noexcept
187
164
{
188
-
189
- return ReportTask (
165
+ return std::shared_ptr<ReportTask>(new ReportTask (
190
166
m_allocator,
191
167
m_mqttConnection,
192
168
m_thingName,
@@ -195,8 +171,8 @@ namespace Aws
195
171
m_taskPeriodSeconds,
196
172
m_networkConnectionSamplePeriodSeconds,
197
173
static_cast <OnTaskCancelledHandler &&>(m_onCancelled),
198
- m_cancellationUserdata);
174
+ m_cancellationUserdata)) ;
199
175
}
200
176
201
177
} // namespace Iotdevicedefenderv1
202
- } // namespace Aws
178
+ } // namespace Aws
0 commit comments