AWS IoT Device SDK C++ v2  1.32.6
AWS IoT Device SDK C++ v2
DeviceDefender.h
Go to the documentation of this file.
1 #pragma once
2 
8 
12 
13 #include <aws/iotdevice/device_defender.h>
14 
15 namespace Aws
16 {
17  namespace Crt
18  {
19 
20  }
21 
22  namespace Iotdevicedefenderv1
23  {
24 
25  class ReportTask;
26  class ReportTaskBuilder;
27 
31  using OnTaskCancelledHandler = std::function<void(void *)>;
32 
33  using ReportFormat = aws_iotdevice_defender_report_format;
34 
35  using CustomMetricNumberFunction = std::function<int(double *)>;
36  using CustomMetricNumberListFunction = std::function<int(Crt::Vector<double> *)>;
39 
44 
48  enum class ReportTaskStatus
49  {
50  Ready = 0,
51  Running = 1,
52  Stopped = 2,
53  };
54 
59  {
60  friend ReportTaskBuilder;
61 
62  public:
63  ~ReportTask();
64  ReportTask(const ReportTask &) = delete;
65  ReportTask &operator=(const ReportTask &) = delete;
66 
70  void StopTask() noexcept;
71 
75  int StartTask() noexcept;
76 
80  ReportTaskStatus GetStatus() noexcept;
81 
82  OnTaskCancelledHandler OnTaskCancelled;
83 
84  void *cancellationUserdata;
85 
89  int LastError() const noexcept { return m_lastError; }
90 
98  void RegisterCustomMetricNumber(
99  const Crt::String metricName,
100  CustomMetricNumberFunction metricFunc) noexcept;
101 
110  void RegisterCustomMetricNumberList(
111  const Crt::String metricName,
112  CustomMetricNumberListFunction metricFunc) noexcept;
113 
123  void RegisterCustomMetricStringList(
124  const Crt::String metricName,
125  CustomMetricStringListFunction metricFunc) noexcept;
126 
135  void RegisterCustomMetricIpAddressList(
136  const Crt::String metricName,
137  CustomMetricIpListFunction metricFunc) noexcept;
138 
139  private:
140  Crt::Allocator *m_allocator;
141  ReportTaskStatus m_status;
142  aws_iotdevice_defender_task_config *m_taskConfig;
143  aws_iotdevice_defender_task *m_owningTask;
144  int m_lastError;
145  std::shared_ptr<Crt::Mqtt::MqttConnection> m_mqttConnection;
146  Crt::Io::EventLoopGroup &m_eventLoopGroup;
147 
148  ReportTask(
149  Crt::Allocator *allocator,
150  std::shared_ptr<Crt::Mqtt::MqttConnection> mqttConnection,
151  const Crt::String &thingName,
152  Crt::Io::EventLoopGroup &eventLoopGroup,
153  ReportFormat reportFormat,
154  uint32_t taskPeriodSeconds,
155  uint32_t networkConnectionSamplePeriodSeconds,
156  OnTaskCancelledHandler &&onCancelled = NULL,
157  void *cancellationUserdata = nullptr) noexcept;
158 
159  static void s_onDefenderV1TaskCancelled(void *userData);
160 
161  // Holds all of the custom metrics created for this task. These are pointers that will be
162  // automatically created and cleaned by ReportTask when it is destroyed.
163  Crt::Vector<std::shared_ptr<CustomMetricBase>> storedCustomMetrics;
164  };
165 
170  {
171  public:
173  Crt::Allocator *allocator,
174  std::shared_ptr<Crt::Mqtt::MqttConnection> mqttConnection,
175  Crt::Io::EventLoopGroup &eventLoopGroup,
176  const Crt::String &thingName);
177 
179  Crt::Allocator *allocator,
180  std::shared_ptr<Crt::Mqtt5::Mqtt5Client> mqtt5Client,
181  Crt::Io::EventLoopGroup &eventLoopGroup,
182  const Crt::String &thingName);
183 
187  ReportTaskBuilder &WithReportFormat(ReportFormat reportFormat) noexcept;
188 
192  ReportTaskBuilder &WithTaskPeriodSeconds(uint32_t taskPeriodSeconds) noexcept;
193 
197  ReportTaskBuilder &WithNetworkConnectionSamplePeriodSeconds(
198  uint32_t networkConnectionSamplePeriodSeconds) noexcept;
199 
203  ReportTaskBuilder &WithTaskCancelledHandler(OnTaskCancelledHandler &&onCancelled) noexcept;
204 
208  ReportTaskBuilder &WithTaskCancellationUserData(void *cancellationUserdata) noexcept;
209 
213  std::shared_ptr<ReportTask> Build() noexcept;
214 
215  private:
216  Crt::Allocator *m_allocator;
217  std::shared_ptr<Crt::Mqtt::MqttConnection> m_mqttConnection;
218  Crt::String m_thingName;
219  Crt::Io::EventLoopGroup &m_eventLoopGroup;
220  ReportFormat m_reportFormat;
221  uint32_t m_taskPeriodSeconds;
222  uint32_t m_networkConnectionSamplePeriodSeconds;
223  OnTaskCancelledHandler m_onCancelled;
224  void *m_cancellationUserdata;
225  };
226 
227  } // namespace Iotdevicedefenderv1
228 } // namespace Aws
Aws::Iotdevicedefenderv1::ReportTaskBuilder
Definition: DeviceDefender.h:170
Aws::Iotdevicedefenderv1::CustomMetricBase
Definition: DeviceDefender.cpp:24
EventLoopGroup.h
Exports.h
Aws::Iotdevicedefenderv1::CustomMetricStringListFunction
std::function< int(Crt::Vector< Crt::String > *)> CustomMetricStringListFunction
Definition: DeviceDefender.h:37
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:413
Aws::Iotdevicedefenderv1::ReportTask::operator=
ReportTask & operator=(const ReportTask &)=delete
Aws::Iotdevicedefenderv1::ReportTask
Definition: DeviceDefender.h:59
Aws::Iotdevicedefenderv1::OnTaskCancelledHandler
std::function< void(void *)> OnTaskCancelledHandler
Definition: DeviceDefender.h:31
Aws::Iotdevicedefenderv1::ReportTask::ReportTask
ReportTask(const ReportTask &)=delete
Mqtt5Client.h
Aws::Iotdevicedefenderv1::ReportFormat
aws_iotdevice_defender_report_format ReportFormat
Definition: DeviceDefender.h:33
Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready
@ Ready
Aws::Iotdevicedefenderv1::CustomMetricNumberFunction
std::function< int(double *)> CustomMetricNumberFunction
Definition: DeviceDefender.h:35
AWS_IOTDEVICEDEFENDER_API
#define AWS_IOTDEVICEDEFENDER_API
Definition: Exports.h:19
Aws::Iotdevicedefenderv1::ReportTaskStatus
ReportTaskStatus
Definition: DeviceDefender.h:49
Aws::Iotdevicedefenderv1::CustomMetricIpListFunction
std::function< int(Crt::Vector< Crt::String > *)> CustomMetricIpListFunction
Definition: DeviceDefender.h:38
Aws::Iotdevicedefenderv1::CustomMetricNumberListFunction
std::function< int(Crt::Vector< double > *)> CustomMetricNumberListFunction
Definition: DeviceDefender.h:36
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:53
std
Definition: StringView.h:851
Aws
Definition: Allocator.h:11
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
MqttClient.h
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
Aws::Crt::Io::EventLoopGroup
Definition: EventLoopGroup.h:33