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
0 commit comments