Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-cloud-client/mbed-client/source/include/m2mreporthandler.h@0:276e7a263c35, 2018-07-02 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Jul 02 06:30:39 2018 +0000
- Revision:
- 0:276e7a263c35
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MACRUM | 0:276e7a263c35 | 1 | /* |
| MACRUM | 0:276e7a263c35 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
| MACRUM | 0:276e7a263c35 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| MACRUM | 0:276e7a263c35 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| MACRUM | 0:276e7a263c35 | 5 | * not use this file except in compliance with the License. |
| MACRUM | 0:276e7a263c35 | 6 | * You may obtain a copy of the License at |
| MACRUM | 0:276e7a263c35 | 7 | * |
| MACRUM | 0:276e7a263c35 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| MACRUM | 0:276e7a263c35 | 9 | * |
| MACRUM | 0:276e7a263c35 | 10 | * Unless required by applicable law or agreed to in writing, software |
| MACRUM | 0:276e7a263c35 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| MACRUM | 0:276e7a263c35 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| MACRUM | 0:276e7a263c35 | 13 | * See the License for the specific language governing permissions and |
| MACRUM | 0:276e7a263c35 | 14 | * limitations under the License. |
| MACRUM | 0:276e7a263c35 | 15 | */ |
| MACRUM | 0:276e7a263c35 | 16 | #ifndef M2MREPORTHANDLER_H |
| MACRUM | 0:276e7a263c35 | 17 | #define M2MREPORTHANDLER_H |
| MACRUM | 0:276e7a263c35 | 18 | |
| MACRUM | 0:276e7a263c35 | 19 | // Support for std args |
| MACRUM | 0:276e7a263c35 | 20 | #include <stdint.h> |
| MACRUM | 0:276e7a263c35 | 21 | #include "mbed-client/m2mconfig.h" |
| MACRUM | 0:276e7a263c35 | 22 | #include "mbed-client/m2mbase.h" |
| MACRUM | 0:276e7a263c35 | 23 | #include "mbed-client/m2mtimerobserver.h" |
| MACRUM | 0:276e7a263c35 | 24 | #include "mbed-client/m2mresourceinstance.h" |
| MACRUM | 0:276e7a263c35 | 25 | #include "mbed-client/m2mvector.h" |
| MACRUM | 0:276e7a263c35 | 26 | #include "mbed-client/m2mtimer.h" |
| MACRUM | 0:276e7a263c35 | 27 | |
| MACRUM | 0:276e7a263c35 | 28 | //FORWARD DECLARATION |
| MACRUM | 0:276e7a263c35 | 29 | class M2MReportObserver; |
| MACRUM | 0:276e7a263c35 | 30 | class M2MTimer; |
| MACRUM | 0:276e7a263c35 | 31 | class M2MResourceInstance; |
| MACRUM | 0:276e7a263c35 | 32 | |
| MACRUM | 0:276e7a263c35 | 33 | /** |
| MACRUM | 0:276e7a263c35 | 34 | * @brief M2MReportHandler. |
| MACRUM | 0:276e7a263c35 | 35 | * This class is handles all the observation related operations. |
| MACRUM | 0:276e7a263c35 | 36 | */ |
| MACRUM | 0:276e7a263c35 | 37 | class M2MReportHandler: public M2MTimerObserver |
| MACRUM | 0:276e7a263c35 | 38 | { |
| MACRUM | 0:276e7a263c35 | 39 | private: |
| MACRUM | 0:276e7a263c35 | 40 | // Prevents the use of assignment operator by accident. |
| MACRUM | 0:276e7a263c35 | 41 | M2MReportHandler& operator=( const M2MReportHandler& /*other*/ ); |
| MACRUM | 0:276e7a263c35 | 42 | |
| MACRUM | 0:276e7a263c35 | 43 | public: |
| MACRUM | 0:276e7a263c35 | 44 | |
| MACRUM | 0:276e7a263c35 | 45 | M2MReportHandler(M2MReportObserver &observer); |
| MACRUM | 0:276e7a263c35 | 46 | |
| MACRUM | 0:276e7a263c35 | 47 | public: |
| MACRUM | 0:276e7a263c35 | 48 | |
| MACRUM | 0:276e7a263c35 | 49 | /** |
| MACRUM | 0:276e7a263c35 | 50 | * Enum defining which write attributes are set. |
| MACRUM | 0:276e7a263c35 | 51 | */ |
| MACRUM | 0:276e7a263c35 | 52 | enum { |
| MACRUM | 0:276e7a263c35 | 53 | Cancel = 1, |
| MACRUM | 0:276e7a263c35 | 54 | Pmin = 2, |
| MACRUM | 0:276e7a263c35 | 55 | Pmax = 4, |
| MACRUM | 0:276e7a263c35 | 56 | Lt = 8, |
| MACRUM | 0:276e7a263c35 | 57 | Gt = 16, |
| MACRUM | 0:276e7a263c35 | 58 | St = 32 |
| MACRUM | 0:276e7a263c35 | 59 | }; |
| MACRUM | 0:276e7a263c35 | 60 | |
| MACRUM | 0:276e7a263c35 | 61 | /** |
| MACRUM | 0:276e7a263c35 | 62 | * Destructor |
| MACRUM | 0:276e7a263c35 | 63 | */ |
| MACRUM | 0:276e7a263c35 | 64 | virtual ~M2MReportHandler(); |
| MACRUM | 0:276e7a263c35 | 65 | |
| MACRUM | 0:276e7a263c35 | 66 | /** |
| MACRUM | 0:276e7a263c35 | 67 | * @brief Sets that object is under observation. |
| MACRUM | 0:276e7a263c35 | 68 | * @param Value for the observation. |
| MACRUM | 0:276e7a263c35 | 69 | * @param handler, Handler object for sending |
| MACRUM | 0:276e7a263c35 | 70 | * observation callbacks. |
| MACRUM | 0:276e7a263c35 | 71 | */ |
| MACRUM | 0:276e7a263c35 | 72 | void set_under_observation(bool observed); |
| MACRUM | 0:276e7a263c35 | 73 | |
| MACRUM | 0:276e7a263c35 | 74 | /** |
| MACRUM | 0:276e7a263c35 | 75 | * @brief Sets the value of the given resource. |
| MACRUM | 0:276e7a263c35 | 76 | * @param value, Value of the observed resource. |
| MACRUM | 0:276e7a263c35 | 77 | */ |
| MACRUM | 0:276e7a263c35 | 78 | void set_value(float value); |
| MACRUM | 0:276e7a263c35 | 79 | |
| MACRUM | 0:276e7a263c35 | 80 | /** |
| MACRUM | 0:276e7a263c35 | 81 | * @brief Sets notification trigger. |
| MACRUM | 0:276e7a263c35 | 82 | * @param obj_instance_id, Object instance id that has changed |
| MACRUM | 0:276e7a263c35 | 83 | */ |
| MACRUM | 0:276e7a263c35 | 84 | void set_notification_trigger(uint16_t obj_instance_id = 0); |
| MACRUM | 0:276e7a263c35 | 85 | |
| MACRUM | 0:276e7a263c35 | 86 | /** |
| MACRUM | 0:276e7a263c35 | 87 | * @brief Parses the received query for notification |
| MACRUM | 0:276e7a263c35 | 88 | * attribute. |
| MACRUM | 0:276e7a263c35 | 89 | * @param query Query to be parsed for attributes. |
| MACRUM | 0:276e7a263c35 | 90 | * @param type Type of the Base Object. |
| MACRUM | 0:276e7a263c35 | 91 | * @param resource_type Type of the Resource. |
| MACRUM | 0:276e7a263c35 | 92 | * @return true if required attributes are present else false. |
| MACRUM | 0:276e7a263c35 | 93 | */ |
| MACRUM | 0:276e7a263c35 | 94 | bool parse_notification_attribute(const char *query, |
| MACRUM | 0:276e7a263c35 | 95 | M2MBase::BaseType type, |
| MACRUM | 0:276e7a263c35 | 96 | M2MResourceInstance::ResourceType resource_type = M2MResourceInstance::OPAQUE); |
| MACRUM | 0:276e7a263c35 | 97 | |
| MACRUM | 0:276e7a263c35 | 98 | /** |
| MACRUM | 0:276e7a263c35 | 99 | * @brief Set back to default values. |
| MACRUM | 0:276e7a263c35 | 100 | */ |
| MACRUM | 0:276e7a263c35 | 101 | void set_default_values(); |
| MACRUM | 0:276e7a263c35 | 102 | |
| MACRUM | 0:276e7a263c35 | 103 | /** |
| MACRUM | 0:276e7a263c35 | 104 | * @brief Return write attribute flags. |
| MACRUM | 0:276e7a263c35 | 105 | */ |
| MACRUM | 0:276e7a263c35 | 106 | uint8_t attribute_flags() const; |
| MACRUM | 0:276e7a263c35 | 107 | |
| MACRUM | 0:276e7a263c35 | 108 | /** |
| MACRUM | 0:276e7a263c35 | 109 | * \brief Sets the observation token value. |
| MACRUM | 0:276e7a263c35 | 110 | * \param token A pointer to the token of the resource. |
| MACRUM | 0:276e7a263c35 | 111 | * \param length The length of the token pointer. |
| MACRUM | 0:276e7a263c35 | 112 | */ |
| MACRUM | 0:276e7a263c35 | 113 | void set_observation_token(const uint8_t *token, const uint8_t length); |
| MACRUM | 0:276e7a263c35 | 114 | |
| MACRUM | 0:276e7a263c35 | 115 | /** |
| MACRUM | 0:276e7a263c35 | 116 | * \brief Provides a copy of the observation token of the object. |
| MACRUM | 0:276e7a263c35 | 117 | * \param value[OUT] A pointer to the value of the token. |
| MACRUM | 0:276e7a263c35 | 118 | * \param value_length[OUT] The length of the token pointer. |
| MACRUM | 0:276e7a263c35 | 119 | */ |
| MACRUM | 0:276e7a263c35 | 120 | void get_observation_token(uint8_t *token, uint8_t &token_length) const; |
| MACRUM | 0:276e7a263c35 | 121 | |
| MACRUM | 0:276e7a263c35 | 122 | /** |
| MACRUM | 0:276e7a263c35 | 123 | * \brief Returns the observation number. |
| MACRUM | 0:276e7a263c35 | 124 | * \return The observation number of the object. |
| MACRUM | 0:276e7a263c35 | 125 | */ |
| MACRUM | 0:276e7a263c35 | 126 | uint16_t observation_number() const; |
| MACRUM | 0:276e7a263c35 | 127 | |
| MACRUM | 0:276e7a263c35 | 128 | /** |
| MACRUM | 0:276e7a263c35 | 129 | * \brief Adds the observation level for the object. |
| MACRUM | 0:276e7a263c35 | 130 | * \param observation_level The level of observation. |
| MACRUM | 0:276e7a263c35 | 131 | */ |
| MACRUM | 0:276e7a263c35 | 132 | void add_observation_level(M2MBase::Observation obs_level); |
| MACRUM | 0:276e7a263c35 | 133 | |
| MACRUM | 0:276e7a263c35 | 134 | /** |
| MACRUM | 0:276e7a263c35 | 135 | * \brief Removes the observation level for the object. |
| MACRUM | 0:276e7a263c35 | 136 | * \param observation_level The level of observation. |
| MACRUM | 0:276e7a263c35 | 137 | */ |
| MACRUM | 0:276e7a263c35 | 138 | void remove_observation_level(M2MBase::Observation obs_level); |
| MACRUM | 0:276e7a263c35 | 139 | |
| MACRUM | 0:276e7a263c35 | 140 | /** |
| MACRUM | 0:276e7a263c35 | 141 | * \brief Returns the observation level of the object. |
| MACRUM | 0:276e7a263c35 | 142 | * \return The observation level of the object. |
| MACRUM | 0:276e7a263c35 | 143 | */ |
| MACRUM | 0:276e7a263c35 | 144 | M2MBase::Observation observation_level() const; |
| MACRUM | 0:276e7a263c35 | 145 | |
| MACRUM | 0:276e7a263c35 | 146 | /** |
| MACRUM | 0:276e7a263c35 | 147 | * @brief Returns whether this resource is under observation or not. |
| MACRUM | 0:276e7a263c35 | 148 | * @return True if the resource is under observation, else false, |
| MACRUM | 0:276e7a263c35 | 149 | */ |
| MACRUM | 0:276e7a263c35 | 150 | bool is_under_observation() const; |
| MACRUM | 0:276e7a263c35 | 151 | |
| MACRUM | 0:276e7a263c35 | 152 | protected : // from M2MTimerObserver |
| MACRUM | 0:276e7a263c35 | 153 | |
| MACRUM | 0:276e7a263c35 | 154 | virtual void timer_expired(M2MTimerObserver::Type type = |
| MACRUM | 0:276e7a263c35 | 155 | M2MTimerObserver::Notdefined); |
| MACRUM | 0:276e7a263c35 | 156 | |
| MACRUM | 0:276e7a263c35 | 157 | private: |
| MACRUM | 0:276e7a263c35 | 158 | |
| MACRUM | 0:276e7a263c35 | 159 | |
| MACRUM | 0:276e7a263c35 | 160 | |
| MACRUM | 0:276e7a263c35 | 161 | bool set_notification_attribute(const char* option, |
| MACRUM | 0:276e7a263c35 | 162 | M2MBase::BaseType type, |
| MACRUM | 0:276e7a263c35 | 163 | M2MResourceInstance::ResourceType resource_type); |
| MACRUM | 0:276e7a263c35 | 164 | |
| MACRUM | 0:276e7a263c35 | 165 | /** |
| MACRUM | 0:276e7a263c35 | 166 | * @brief Schedule a report, if the pmin is exceeded |
| MACRUM | 0:276e7a263c35 | 167 | * then report immediately else store the state to be |
| MACRUM | 0:276e7a263c35 | 168 | * reported once the time fires. |
| MACRUM | 0:276e7a263c35 | 169 | */ |
| MACRUM | 0:276e7a263c35 | 170 | void schedule_report(); |
| MACRUM | 0:276e7a263c35 | 171 | |
| MACRUM | 0:276e7a263c35 | 172 | /** |
| MACRUM | 0:276e7a263c35 | 173 | * @brief Reports a sample that satisfies the reporting criteria. |
| MACRUM | 0:276e7a263c35 | 174 | */ |
| MACRUM | 0:276e7a263c35 | 175 | void report(); |
| MACRUM | 0:276e7a263c35 | 176 | |
| MACRUM | 0:276e7a263c35 | 177 | /** |
| MACRUM | 0:276e7a263c35 | 178 | * @brief Manage timers for pmin and pmax. |
| MACRUM | 0:276e7a263c35 | 179 | */ |
| MACRUM | 0:276e7a263c35 | 180 | void handle_timers(); |
| MACRUM | 0:276e7a263c35 | 181 | |
| MACRUM | 0:276e7a263c35 | 182 | /** |
| MACRUM | 0:276e7a263c35 | 183 | * @brief Check whether notification params can be accepted. |
| MACRUM | 0:276e7a263c35 | 184 | */ |
| MACRUM | 0:276e7a263c35 | 185 | bool check_attribute_validity() const; |
| MACRUM | 0:276e7a263c35 | 186 | |
| MACRUM | 0:276e7a263c35 | 187 | /** |
| MACRUM | 0:276e7a263c35 | 188 | * @brief Stop pmin & pmax timers. |
| MACRUM | 0:276e7a263c35 | 189 | */ |
| MACRUM | 0:276e7a263c35 | 190 | void stop_timers(); |
| MACRUM | 0:276e7a263c35 | 191 | |
| MACRUM | 0:276e7a263c35 | 192 | /** |
| MACRUM | 0:276e7a263c35 | 193 | * @brief Check if current value match threshold values. |
| MACRUM | 0:276e7a263c35 | 194 | * @return True if notify can be send otherwise false. |
| MACRUM | 0:276e7a263c35 | 195 | */ |
| MACRUM | 0:276e7a263c35 | 196 | bool check_threshold_values() const; |
| MACRUM | 0:276e7a263c35 | 197 | |
| MACRUM | 0:276e7a263c35 | 198 | /** |
| MACRUM | 0:276e7a263c35 | 199 | * @brief Check whether current value matches with GT & LT. |
| MACRUM | 0:276e7a263c35 | 200 | * @return True if current value match with GT or LT values. |
| MACRUM | 0:276e7a263c35 | 201 | */ |
| MACRUM | 0:276e7a263c35 | 202 | bool check_gt_lt_params() const; |
| MACRUM | 0:276e7a263c35 | 203 | |
| MACRUM | 0:276e7a263c35 | 204 | /** |
| MACRUM | 0:276e7a263c35 | 205 | * \brief Allocate (size + 1) amount of memory, copy size bytes into |
| MACRUM | 0:276e7a263c35 | 206 | * it and add zero termination. |
| MACRUM | 0:276e7a263c35 | 207 | * \param source The source string to copy, may not be NULL. |
| MACRUM | 0:276e7a263c35 | 208 | * \param size The size of memory to be reserved. |
| MACRUM | 0:276e7a263c35 | 209 | */ |
| MACRUM | 0:276e7a263c35 | 210 | static uint8_t* alloc_string_copy(const uint8_t* source, uint32_t size); |
| MACRUM | 0:276e7a263c35 | 211 | |
| MACRUM | 0:276e7a263c35 | 212 | private: |
| MACRUM | 0:276e7a263c35 | 213 | M2MReportObserver &_observer; |
| MACRUM | 0:276e7a263c35 | 214 | bool _is_under_observation : 1; |
| MACRUM | 0:276e7a263c35 | 215 | M2MBase::Observation _observation_level : 4; |
| MACRUM | 0:276e7a263c35 | 216 | uint8_t _attribute_state; |
| MACRUM | 0:276e7a263c35 | 217 | unsigned _token_length : 8; |
| MACRUM | 0:276e7a263c35 | 218 | bool _notify; |
| MACRUM | 0:276e7a263c35 | 219 | bool _pmin_exceeded; |
| MACRUM | 0:276e7a263c35 | 220 | bool _pmax_exceeded; |
| MACRUM | 0:276e7a263c35 | 221 | unsigned _observation_number : 24; |
| MACRUM | 0:276e7a263c35 | 222 | M2MTimer _pmin_timer; |
| MACRUM | 0:276e7a263c35 | 223 | M2MTimer _pmax_timer; |
| MACRUM | 0:276e7a263c35 | 224 | uint8_t *_token; |
| MACRUM | 0:276e7a263c35 | 225 | int32_t _pmax; |
| MACRUM | 0:276e7a263c35 | 226 | int32_t _pmin; |
| MACRUM | 0:276e7a263c35 | 227 | float _current_value; |
| MACRUM | 0:276e7a263c35 | 228 | float _gt; |
| MACRUM | 0:276e7a263c35 | 229 | float _lt; |
| MACRUM | 0:276e7a263c35 | 230 | float _st; |
| MACRUM | 0:276e7a263c35 | 231 | float _high_step; |
| MACRUM | 0:276e7a263c35 | 232 | float _low_step; |
| MACRUM | 0:276e7a263c35 | 233 | float _last_value; |
| MACRUM | 0:276e7a263c35 | 234 | m2m::Vector<uint16_t> _changed_instance_ids; |
| MACRUM | 0:276e7a263c35 | 235 | |
| MACRUM | 0:276e7a263c35 | 236 | friend class Test_M2MReportHandler; |
| MACRUM | 0:276e7a263c35 | 237 | |
| MACRUM | 0:276e7a263c35 | 238 | }; |
| MACRUM | 0:276e7a263c35 | 239 | |
| MACRUM | 0:276e7a263c35 | 240 | #endif // M2MREPORTHANDLER_H |