leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #ifndef M2MREPORTHANDLER_H
leothedragon 0:8f0bb79ddd48 17 #define M2MREPORTHANDLER_H
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 // Support for std args
leothedragon 0:8f0bb79ddd48 20 #include <stdint.h>
leothedragon 0:8f0bb79ddd48 21 #include "mbed-client/m2mconfig.h"
leothedragon 0:8f0bb79ddd48 22 #include "mbed-client/m2mbase.h"
leothedragon 0:8f0bb79ddd48 23 #include "mbed-client/m2mtimerobserver.h"
leothedragon 0:8f0bb79ddd48 24 #include "mbed-client/m2mresourceinstance.h"
leothedragon 0:8f0bb79ddd48 25 #include "mbed-client/m2mvector.h"
leothedragon 0:8f0bb79ddd48 26 #include "mbed-client/m2mtimer.h"
leothedragon 0:8f0bb79ddd48 27
leothedragon 0:8f0bb79ddd48 28 //FORWARD DECLARATION
leothedragon 0:8f0bb79ddd48 29 class M2MReportObserver;
leothedragon 0:8f0bb79ddd48 30 class M2MTimer;
leothedragon 0:8f0bb79ddd48 31 class M2MResourceInstance;
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 typedef union current_value_u {
leothedragon 0:8f0bb79ddd48 34 float float_value;
leothedragon 0:8f0bb79ddd48 35 int64_t int_value;
leothedragon 0:8f0bb79ddd48 36 } current_value_t;
leothedragon 0:8f0bb79ddd48 37
leothedragon 0:8f0bb79ddd48 38 typedef union last_value_u {
leothedragon 0:8f0bb79ddd48 39 float float_value;
leothedragon 0:8f0bb79ddd48 40 int64_t int_value;
leothedragon 0:8f0bb79ddd48 41 } last_value_t;
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 typedef union high_step_u {
leothedragon 0:8f0bb79ddd48 44 float float_value;
leothedragon 0:8f0bb79ddd48 45 int64_t int_value;
leothedragon 0:8f0bb79ddd48 46 } high_step_t;
leothedragon 0:8f0bb79ddd48 47
leothedragon 0:8f0bb79ddd48 48 typedef union low_step_u {
leothedragon 0:8f0bb79ddd48 49 float float_value;
leothedragon 0:8f0bb79ddd48 50 int64_t int_value;
leothedragon 0:8f0bb79ddd48 51 } low_step_t;
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 /**
leothedragon 0:8f0bb79ddd48 54 * @brief M2MReportHandler.
leothedragon 0:8f0bb79ddd48 55 * This class is handles all the observation related operations.
leothedragon 0:8f0bb79ddd48 56 */
leothedragon 0:8f0bb79ddd48 57 class M2MReportHandler: public M2MTimerObserver
leothedragon 0:8f0bb79ddd48 58 {
leothedragon 0:8f0bb79ddd48 59 private:
leothedragon 0:8f0bb79ddd48 60 // Prevents the use of assignment operator by accident.
leothedragon 0:8f0bb79ddd48 61 M2MReportHandler& operator=( const M2MReportHandler& /*other*/ );
leothedragon 0:8f0bb79ddd48 62
leothedragon 0:8f0bb79ddd48 63 public:
leothedragon 0:8f0bb79ddd48 64
leothedragon 0:8f0bb79ddd48 65 M2MReportHandler(M2MReportObserver &observer, M2MBase::DataType type);
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 public:
leothedragon 0:8f0bb79ddd48 68
leothedragon 0:8f0bb79ddd48 69 /**
leothedragon 0:8f0bb79ddd48 70 * Enum defining which write attributes are set.
leothedragon 0:8f0bb79ddd48 71 */
leothedragon 0:8f0bb79ddd48 72 enum {
leothedragon 0:8f0bb79ddd48 73 Cancel = 1,
leothedragon 0:8f0bb79ddd48 74 Pmin = 2,
leothedragon 0:8f0bb79ddd48 75 Pmax = 4,
leothedragon 0:8f0bb79ddd48 76 Lt = 8,
leothedragon 0:8f0bb79ddd48 77 Gt = 16,
leothedragon 0:8f0bb79ddd48 78 St = 32
leothedragon 0:8f0bb79ddd48 79 };
leothedragon 0:8f0bb79ddd48 80
leothedragon 0:8f0bb79ddd48 81 /**
leothedragon 0:8f0bb79ddd48 82 * Destructor
leothedragon 0:8f0bb79ddd48 83 */
leothedragon 0:8f0bb79ddd48 84 virtual ~M2MReportHandler();
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 /**
leothedragon 0:8f0bb79ddd48 87 * @brief Sets that object is under observation.
leothedragon 0:8f0bb79ddd48 88 * @param Value for the observation.
leothedragon 0:8f0bb79ddd48 89 * @param handler, Handler object for sending
leothedragon 0:8f0bb79ddd48 90 * observation callbacks.
leothedragon 0:8f0bb79ddd48 91 */
leothedragon 0:8f0bb79ddd48 92 void set_under_observation(bool observed);
leothedragon 0:8f0bb79ddd48 93
leothedragon 0:8f0bb79ddd48 94 /**
leothedragon 0:8f0bb79ddd48 95 * @brief Sets the float value of the given resource.
leothedragon 0:8f0bb79ddd48 96 * @param value, Value of the observed resource.
leothedragon 0:8f0bb79ddd48 97 */
leothedragon 0:8f0bb79ddd48 98 void set_value_float(float value);
leothedragon 0:8f0bb79ddd48 99
leothedragon 0:8f0bb79ddd48 100 /**
leothedragon 0:8f0bb79ddd48 101 * @brief Sets the integer value of the given resource.
leothedragon 0:8f0bb79ddd48 102 * @param value, Value of the observed resource.
leothedragon 0:8f0bb79ddd48 103 */
leothedragon 0:8f0bb79ddd48 104 void set_value_int(int64_t value);
leothedragon 0:8f0bb79ddd48 105
leothedragon 0:8f0bb79ddd48 106 /**
leothedragon 0:8f0bb79ddd48 107 * @brief Sets notification trigger.
leothedragon 0:8f0bb79ddd48 108 * @param obj_instance_id, Object instance id that has changed
leothedragon 0:8f0bb79ddd48 109 */
leothedragon 0:8f0bb79ddd48 110 void set_notification_trigger(uint16_t obj_instance_id = 0);
leothedragon 0:8f0bb79ddd48 111
leothedragon 0:8f0bb79ddd48 112 /**
leothedragon 0:8f0bb79ddd48 113 * @brief Parses the received query for notification
leothedragon 0:8f0bb79ddd48 114 * attribute.
leothedragon 0:8f0bb79ddd48 115 * @param query Query to be parsed for attributes.
leothedragon 0:8f0bb79ddd48 116 * @param type Type of the Base Object.
leothedragon 0:8f0bb79ddd48 117 * @param resource_type Type of the Resource.
leothedragon 0:8f0bb79ddd48 118 * @return true if required attributes are present else false.
leothedragon 0:8f0bb79ddd48 119 */
leothedragon 0:8f0bb79ddd48 120 bool parse_notification_attribute(const char *query,
leothedragon 0:8f0bb79ddd48 121 M2MBase::BaseType type,
leothedragon 0:8f0bb79ddd48 122 M2MResourceInstance::ResourceType resource_type = M2MResourceInstance::OPAQUE);
leothedragon 0:8f0bb79ddd48 123
leothedragon 0:8f0bb79ddd48 124 /**
leothedragon 0:8f0bb79ddd48 125 * @brief Set back to default values.
leothedragon 0:8f0bb79ddd48 126 */
leothedragon 0:8f0bb79ddd48 127 void set_default_values();
leothedragon 0:8f0bb79ddd48 128
leothedragon 0:8f0bb79ddd48 129 /**
leothedragon 0:8f0bb79ddd48 130 * @brief Return write attribute flags.
leothedragon 0:8f0bb79ddd48 131 */
leothedragon 0:8f0bb79ddd48 132 uint8_t attribute_flags() const;
leothedragon 0:8f0bb79ddd48 133
leothedragon 0:8f0bb79ddd48 134 /**
leothedragon 0:8f0bb79ddd48 135 * \brief Sets the observation token value.
leothedragon 0:8f0bb79ddd48 136 * \param token A pointer to the token of the resource.
leothedragon 0:8f0bb79ddd48 137 * \param length The length of the token pointer.
leothedragon 0:8f0bb79ddd48 138 */
leothedragon 0:8f0bb79ddd48 139 void set_observation_token(const uint8_t *token, const uint8_t length);
leothedragon 0:8f0bb79ddd48 140
leothedragon 0:8f0bb79ddd48 141 /**
leothedragon 0:8f0bb79ddd48 142 * \brief Provides a copy of the observation token of the object.
leothedragon 0:8f0bb79ddd48 143 * \param value[OUT] A pointer to the value of the token.
leothedragon 0:8f0bb79ddd48 144 * \param value_length[OUT] The length of the token pointer.
leothedragon 0:8f0bb79ddd48 145 */
leothedragon 0:8f0bb79ddd48 146 void get_observation_token(uint8_t *token, uint8_t &token_length) const;
leothedragon 0:8f0bb79ddd48 147
leothedragon 0:8f0bb79ddd48 148 /**
leothedragon 0:8f0bb79ddd48 149 * \brief Returns the observation number.
leothedragon 0:8f0bb79ddd48 150 * \return The observation number of the object.
leothedragon 0:8f0bb79ddd48 151 */
leothedragon 0:8f0bb79ddd48 152 uint16_t observation_number() const;
leothedragon 0:8f0bb79ddd48 153
leothedragon 0:8f0bb79ddd48 154 /**
leothedragon 0:8f0bb79ddd48 155 * \brief Adds the observation level for the object.
leothedragon 0:8f0bb79ddd48 156 * \param observation_level The level of observation.
leothedragon 0:8f0bb79ddd48 157 */
leothedragon 0:8f0bb79ddd48 158 void add_observation_level(M2MBase::Observation obs_level);
leothedragon 0:8f0bb79ddd48 159
leothedragon 0:8f0bb79ddd48 160 /**
leothedragon 0:8f0bb79ddd48 161 * \brief Removes the observation level for the object.
leothedragon 0:8f0bb79ddd48 162 * \param observation_level The level of observation.
leothedragon 0:8f0bb79ddd48 163 */
leothedragon 0:8f0bb79ddd48 164 void remove_observation_level(M2MBase::Observation obs_level);
leothedragon 0:8f0bb79ddd48 165
leothedragon 0:8f0bb79ddd48 166 /**
leothedragon 0:8f0bb79ddd48 167 * \brief Returns the observation level of the object.
leothedragon 0:8f0bb79ddd48 168 * \return The observation level of the object.
leothedragon 0:8f0bb79ddd48 169 */
leothedragon 0:8f0bb79ddd48 170 M2MBase::Observation observation_level() const;
leothedragon 0:8f0bb79ddd48 171
leothedragon 0:8f0bb79ddd48 172 /**
leothedragon 0:8f0bb79ddd48 173 * @brief Returns whether this resource is under observation or not.
leothedragon 0:8f0bb79ddd48 174 * @return True if the resource is under observation, else false,
leothedragon 0:8f0bb79ddd48 175 */
leothedragon 0:8f0bb79ddd48 176 bool is_under_observation() const;
leothedragon 0:8f0bb79ddd48 177
leothedragon 0:8f0bb79ddd48 178 /**
leothedragon 0:8f0bb79ddd48 179 * @brief Schedule a report, if the pmin is exceeded
leothedragon 0:8f0bb79ddd48 180 * report immediately, otherwise store the state to be
leothedragon 0:8f0bb79ddd48 181 * reported once the time fires.
leothedragon 0:8f0bb79ddd48 182 *
leothedragon 0:8f0bb79ddd48 183 * @param in_queue If the message is queued message then it must be send even if
leothedragon 0:8f0bb79ddd48 184 * current and last values are the same.
leothedragon 0:8f0bb79ddd48 185 */
leothedragon 0:8f0bb79ddd48 186 void schedule_report(bool in_queue = false);
leothedragon 0:8f0bb79ddd48 187
leothedragon 0:8f0bb79ddd48 188 /**
leothedragon 0:8f0bb79ddd48 189 * @brief Set flag that new notification needs to be send.
leothedragon 0:8f0bb79ddd48 190 *
leothedragon 0:8f0bb79ddd48 191 * @param to_queue If True then notification is marked to be send
leothedragon 0:8f0bb79ddd48 192 */
leothedragon 0:8f0bb79ddd48 193 void set_notification_in_queue(bool to_queue);
leothedragon 0:8f0bb79ddd48 194
leothedragon 0:8f0bb79ddd48 195 /**
leothedragon 0:8f0bb79ddd48 196 * @brief Returns whether notification needs to be send or not.
leothedragon 0:8f0bb79ddd48 197 *
leothedragon 0:8f0bb79ddd48 198 * @return Is notification sending needed or not.
leothedragon 0:8f0bb79ddd48 199 */
leothedragon 0:8f0bb79ddd48 200 bool notification_in_queue() const;
leothedragon 0:8f0bb79ddd48 201
leothedragon 0:8f0bb79ddd48 202 /**
leothedragon 0:8f0bb79ddd48 203 * @brief Set flag that new notification needs to be send.
leothedragon 0:8f0bb79ddd48 204 *
leothedragon 0:8f0bb79ddd48 205 * @param to_queue If True then notification is marked to be send
leothedragon 0:8f0bb79ddd48 206 */
leothedragon 0:8f0bb79ddd48 207 void set_notification_send_in_progress(bool progress);
leothedragon 0:8f0bb79ddd48 208
leothedragon 0:8f0bb79ddd48 209 /**
leothedragon 0:8f0bb79ddd48 210 * @brief Returns whether notification send is in progress or not.
leothedragon 0:8f0bb79ddd48 211 *
leothedragon 0:8f0bb79ddd48 212 * @return Is notification sending ongoing or not.
leothedragon 0:8f0bb79ddd48 213 */
leothedragon 0:8f0bb79ddd48 214 bool notification_send_in_progress() const;
leothedragon 0:8f0bb79ddd48 215
leothedragon 0:8f0bb79ddd48 216 /**
leothedragon 0:8f0bb79ddd48 217 * @brief Sets whether notification will be sent using blockwise or not.
leothedragon 0:8f0bb79ddd48 218 *
leothedragon 0:8f0bb79ddd48 219 * @param blockwise_notify If True then notification is sent using blockwise.
leothedragon 0:8f0bb79ddd48 220 */
leothedragon 0:8f0bb79ddd48 221 void set_blockwise_notify(bool blockwise_notify);
leothedragon 0:8f0bb79ddd48 222
leothedragon 0:8f0bb79ddd48 223 /**
leothedragon 0:8f0bb79ddd48 224 * @brief Returns whether notification is sent using blockwise or not.
leothedragon 0:8f0bb79ddd48 225 *
leothedragon 0:8f0bb79ddd48 226 * @return Is notification sent using blockwise.
leothedragon 0:8f0bb79ddd48 227 */
leothedragon 0:8f0bb79ddd48 228 bool blockwise_notify() const;
leothedragon 0:8f0bb79ddd48 229
leothedragon 0:8f0bb79ddd48 230 protected : // from M2MTimerObserver
leothedragon 0:8f0bb79ddd48 231
leothedragon 0:8f0bb79ddd48 232 virtual void timer_expired(M2MTimerObserver::Type type =
leothedragon 0:8f0bb79ddd48 233 M2MTimerObserver::Notdefined);
leothedragon 0:8f0bb79ddd48 234
leothedragon 0:8f0bb79ddd48 235 private:
leothedragon 0:8f0bb79ddd48 236
leothedragon 0:8f0bb79ddd48 237 bool set_notification_attribute(const char* option,
leothedragon 0:8f0bb79ddd48 238 M2MBase::BaseType type,
leothedragon 0:8f0bb79ddd48 239 M2MResourceInstance::ResourceType resource_type);
leothedragon 0:8f0bb79ddd48 240
leothedragon 0:8f0bb79ddd48 241 /**
leothedragon 0:8f0bb79ddd48 242 * @brief Reports a sample that satisfies the reporting criteria.
leothedragon 0:8f0bb79ddd48 243 *
leothedragon 0:8f0bb79ddd48 244 * @param in_queue If the message is queued message then it must be send even
leothedragon 0:8f0bb79ddd48 245 * current and last values are the same.
leothedragon 0:8f0bb79ddd48 246 */
leothedragon 0:8f0bb79ddd48 247 void report(bool in_queue = false);
leothedragon 0:8f0bb79ddd48 248
leothedragon 0:8f0bb79ddd48 249 /**
leothedragon 0:8f0bb79ddd48 250 * @brief Manage timers for pmin and pmax.
leothedragon 0:8f0bb79ddd48 251 */
leothedragon 0:8f0bb79ddd48 252 void handle_timers();
leothedragon 0:8f0bb79ddd48 253
leothedragon 0:8f0bb79ddd48 254 /**
leothedragon 0:8f0bb79ddd48 255 * @brief Check whether notification params can be accepted.
leothedragon 0:8f0bb79ddd48 256 */
leothedragon 0:8f0bb79ddd48 257 bool check_attribute_validity() const;
leothedragon 0:8f0bb79ddd48 258
leothedragon 0:8f0bb79ddd48 259 /**
leothedragon 0:8f0bb79ddd48 260 * @brief Stop pmin & pmax timers.
leothedragon 0:8f0bb79ddd48 261 */
leothedragon 0:8f0bb79ddd48 262 void stop_timers();
leothedragon 0:8f0bb79ddd48 263
leothedragon 0:8f0bb79ddd48 264 /**
leothedragon 0:8f0bb79ddd48 265 * @brief Check if current value match threshold values.
leothedragon 0:8f0bb79ddd48 266 * @return True if notify can be send otherwise false.
leothedragon 0:8f0bb79ddd48 267 */
leothedragon 0:8f0bb79ddd48 268 bool check_threshold_values() const;
leothedragon 0:8f0bb79ddd48 269
leothedragon 0:8f0bb79ddd48 270 /**
leothedragon 0:8f0bb79ddd48 271 * @brief Check whether current value matches with GT & LT.
leothedragon 0:8f0bb79ddd48 272 * @return True if current value match with GT or LT values.
leothedragon 0:8f0bb79ddd48 273 */
leothedragon 0:8f0bb79ddd48 274 bool check_gt_lt_params() const;
leothedragon 0:8f0bb79ddd48 275
leothedragon 0:8f0bb79ddd48 276 /**
leothedragon 0:8f0bb79ddd48 277 * \brief Allocate size amount of memory, copy size bytes into it
leothedragon 0:8f0bb79ddd48 278 * \param source The source data to copy, may not be NULL.
leothedragon 0:8f0bb79ddd48 279 * \param size The size of memory to be reserved.
leothedragon 0:8f0bb79ddd48 280 */
leothedragon 0:8f0bb79ddd48 281 static uint8_t* alloc_copy(const uint8_t* source, uint32_t size);
leothedragon 0:8f0bb79ddd48 282
leothedragon 0:8f0bb79ddd48 283 /**
leothedragon 0:8f0bb79ddd48 284 * \brief New value is ready to be sent.
leothedragon 0:8f0bb79ddd48 285 */
leothedragon 0:8f0bb79ddd48 286 void send_value();
leothedragon 0:8f0bb79ddd48 287
leothedragon 0:8f0bb79ddd48 288 private:
leothedragon 0:8f0bb79ddd48 289 M2MReportObserver &_observer;
leothedragon 0:8f0bb79ddd48 290 bool _is_under_observation : 1;
leothedragon 0:8f0bb79ddd48 291 M2MBase::Observation _observation_level : 3;
leothedragon 0:8f0bb79ddd48 292 uint8_t _attribute_state;
leothedragon 0:8f0bb79ddd48 293 unsigned _token_length : 8;
leothedragon 0:8f0bb79ddd48 294 M2MBase::DataType _resource_type : 3;
leothedragon 0:8f0bb79ddd48 295 bool _notify : 1;
leothedragon 0:8f0bb79ddd48 296 bool _pmin_exceeded : 1;
leothedragon 0:8f0bb79ddd48 297 bool _pmax_exceeded : 1;
leothedragon 0:8f0bb79ddd48 298 unsigned _observation_number : 24;
leothedragon 0:8f0bb79ddd48 299 M2MTimer _pmin_timer;
leothedragon 0:8f0bb79ddd48 300 M2MTimer _pmax_timer;
leothedragon 0:8f0bb79ddd48 301 uint8_t *_token;
leothedragon 0:8f0bb79ddd48 302 int32_t _pmax;
leothedragon 0:8f0bb79ddd48 303 int32_t _pmin;
leothedragon 0:8f0bb79ddd48 304 current_value_t _current_value;
leothedragon 0:8f0bb79ddd48 305 high_step_t _high_step;
leothedragon 0:8f0bb79ddd48 306 low_step_t _low_step;
leothedragon 0:8f0bb79ddd48 307 last_value_t _last_value;
leothedragon 0:8f0bb79ddd48 308 float _gt;
leothedragon 0:8f0bb79ddd48 309 float _lt;
leothedragon 0:8f0bb79ddd48 310 float _st;
leothedragon 0:8f0bb79ddd48 311 m2m::Vector<uint16_t> _changed_instance_ids;
leothedragon 0:8f0bb79ddd48 312 bool _notification_send_in_progress : 1;
leothedragon 0:8f0bb79ddd48 313 bool _notification_in_queue : 1;
leothedragon 0:8f0bb79ddd48 314 bool _blockwise_notify : 1;
leothedragon 0:8f0bb79ddd48 315 bool _pmin_quiet_period : 1;
leothedragon 0:8f0bb79ddd48 316
leothedragon 0:8f0bb79ddd48 317 friend class Test_M2MReportHandler;
leothedragon 0:8f0bb79ddd48 318
leothedragon 0:8f0bb79ddd48 319 };
leothedragon 0:8f0bb79ddd48 320
leothedragon 0:8f0bb79ddd48 321 #endif // M2MREPORTHANDLER_H