Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

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