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/m2mreporthandler.cpp@2:bf2124b482f9, 2018-07-02 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Jul 02 08:06:37 2018 +0000
- Revision:
- 2:bf2124b482f9
- Parent:
- 0:276e7a263c35
Update library
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 | |
MACRUM | 0:276e7a263c35 | 17 | // fixup the compilation on ARMCC for PRId32 |
MACRUM | 0:276e7a263c35 | 18 | #define __STDC_FORMAT_MACROS |
MACRUM | 0:276e7a263c35 | 19 | #include <inttypes.h> |
MACRUM | 0:276e7a263c35 | 20 | |
MACRUM | 0:276e7a263c35 | 21 | #include "mbed-client/m2mreportobserver.h" |
MACRUM | 0:276e7a263c35 | 22 | #include "mbed-client/m2mconstants.h" |
MACRUM | 0:276e7a263c35 | 23 | #include "mbed-client/m2mtimer.h" |
MACRUM | 0:276e7a263c35 | 24 | #include "include/m2mreporthandler.h" |
MACRUM | 0:276e7a263c35 | 25 | #include "mbed-trace/mbed_trace.h" |
MACRUM | 0:276e7a263c35 | 26 | #include <string.h> |
MACRUM | 0:276e7a263c35 | 27 | #include <stdlib.h> |
MACRUM | 0:276e7a263c35 | 28 | |
MACRUM | 0:276e7a263c35 | 29 | #define TRACE_GROUP "mClt" |
MACRUM | 0:276e7a263c35 | 30 | |
MACRUM | 0:276e7a263c35 | 31 | M2MReportHandler::M2MReportHandler(M2MReportObserver &observer) |
MACRUM | 0:276e7a263c35 | 32 | : _observer(observer), |
MACRUM | 0:276e7a263c35 | 33 | _is_under_observation(false), |
MACRUM | 0:276e7a263c35 | 34 | _observation_level(M2MBase::None), |
MACRUM | 0:276e7a263c35 | 35 | _attribute_state(0), |
MACRUM | 0:276e7a263c35 | 36 | _token_length(0), |
MACRUM | 0:276e7a263c35 | 37 | _notify(false), |
MACRUM | 0:276e7a263c35 | 38 | _pmin_exceeded(false), |
MACRUM | 0:276e7a263c35 | 39 | _pmax_exceeded(false), |
MACRUM | 0:276e7a263c35 | 40 | _observation_number(0), |
MACRUM | 0:276e7a263c35 | 41 | _pmin_timer(*this), |
MACRUM | 0:276e7a263c35 | 42 | _pmax_timer(*this), |
MACRUM | 0:276e7a263c35 | 43 | _token(NULL), |
MACRUM | 0:276e7a263c35 | 44 | _pmax(-1.0f), |
MACRUM | 0:276e7a263c35 | 45 | _pmin(1.0f), |
MACRUM | 0:276e7a263c35 | 46 | _current_value(0.0f), |
MACRUM | 0:276e7a263c35 | 47 | _gt(0.0f), |
MACRUM | 0:276e7a263c35 | 48 | _lt(0.0f), |
MACRUM | 0:276e7a263c35 | 49 | _st(0.0f), |
MACRUM | 0:276e7a263c35 | 50 | _high_step(0.0f), |
MACRUM | 0:276e7a263c35 | 51 | _low_step(0.0f), |
MACRUM | 0:276e7a263c35 | 52 | _last_value(-1.0f) |
MACRUM | 0:276e7a263c35 | 53 | { |
MACRUM | 0:276e7a263c35 | 54 | tr_debug("M2MReportHandler::M2MReportHandler()"); |
MACRUM | 0:276e7a263c35 | 55 | } |
MACRUM | 0:276e7a263c35 | 56 | |
MACRUM | 0:276e7a263c35 | 57 | M2MReportHandler::~M2MReportHandler() |
MACRUM | 0:276e7a263c35 | 58 | { |
MACRUM | 0:276e7a263c35 | 59 | tr_debug("M2MReportHandler::~M2MReportHandler()"); |
MACRUM | 0:276e7a263c35 | 60 | free(_token); |
MACRUM | 0:276e7a263c35 | 61 | } |
MACRUM | 0:276e7a263c35 | 62 | |
MACRUM | 0:276e7a263c35 | 63 | void M2MReportHandler::set_under_observation(bool observed) |
MACRUM | 0:276e7a263c35 | 64 | { |
MACRUM | 0:276e7a263c35 | 65 | tr_debug("M2MReportHandler::set_under_observation(observed %d)", (int)observed); |
MACRUM | 0:276e7a263c35 | 66 | |
MACRUM | 0:276e7a263c35 | 67 | _is_under_observation = observed; |
MACRUM | 0:276e7a263c35 | 68 | |
MACRUM | 0:276e7a263c35 | 69 | stop_timers(); |
MACRUM | 0:276e7a263c35 | 70 | if(observed) { |
MACRUM | 0:276e7a263c35 | 71 | handle_timers(); |
MACRUM | 0:276e7a263c35 | 72 | } |
MACRUM | 0:276e7a263c35 | 73 | else { |
MACRUM | 0:276e7a263c35 | 74 | set_default_values(); |
MACRUM | 0:276e7a263c35 | 75 | } |
MACRUM | 0:276e7a263c35 | 76 | } |
MACRUM | 0:276e7a263c35 | 77 | |
MACRUM | 0:276e7a263c35 | 78 | void M2MReportHandler::set_value(float value) |
MACRUM | 0:276e7a263c35 | 79 | { |
MACRUM | 0:276e7a263c35 | 80 | tr_debug("M2MReportHandler::set_value() - current %f, last %f", value, _last_value); |
MACRUM | 0:276e7a263c35 | 81 | _current_value = value; |
MACRUM | 0:276e7a263c35 | 82 | if(_current_value != _last_value) { |
MACRUM | 0:276e7a263c35 | 83 | tr_debug("M2MReportHandler::set_value() - UNDER OBSERVATION"); |
MACRUM | 0:276e7a263c35 | 84 | if (check_threshold_values()) { |
MACRUM | 0:276e7a263c35 | 85 | schedule_report(); |
MACRUM | 0:276e7a263c35 | 86 | } |
MACRUM | 0:276e7a263c35 | 87 | else { |
MACRUM | 0:276e7a263c35 | 88 | tr_debug("M2MReportHandler::set_value - value not in range"); |
MACRUM | 0:276e7a263c35 | 89 | _notify = false; |
MACRUM | 0:276e7a263c35 | 90 | _last_value = _current_value; |
MACRUM | 0:276e7a263c35 | 91 | if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
MACRUM | 0:276e7a263c35 | 92 | (_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
MACRUM | 0:276e7a263c35 | 93 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
MACRUM | 0:276e7a263c35 | 94 | tr_debug("M2MReportHandler::set_value - stop pmin timer"); |
MACRUM | 0:276e7a263c35 | 95 | _pmin_timer.stop_timer(); |
MACRUM | 0:276e7a263c35 | 96 | _pmin_exceeded = true; |
MACRUM | 0:276e7a263c35 | 97 | } |
MACRUM | 0:276e7a263c35 | 98 | } |
MACRUM | 0:276e7a263c35 | 99 | _high_step = _current_value + _st; |
MACRUM | 0:276e7a263c35 | 100 | _low_step = _current_value - _st; |
MACRUM | 0:276e7a263c35 | 101 | } |
MACRUM | 0:276e7a263c35 | 102 | } |
MACRUM | 0:276e7a263c35 | 103 | |
MACRUM | 0:276e7a263c35 | 104 | void M2MReportHandler::set_notification_trigger(uint16_t obj_instance_id) |
MACRUM | 0:276e7a263c35 | 105 | { |
MACRUM | 0:276e7a263c35 | 106 | tr_debug("M2MReportHandler::set_notification_trigger(): %d", obj_instance_id); |
MACRUM | 0:276e7a263c35 | 107 | // Add to array if not there yet |
MACRUM | 0:276e7a263c35 | 108 | m2m::Vector<uint16_t>::const_iterator it; |
MACRUM | 0:276e7a263c35 | 109 | it = _changed_instance_ids.begin(); |
MACRUM | 0:276e7a263c35 | 110 | bool found = false; |
MACRUM | 0:276e7a263c35 | 111 | for ( ; it != _changed_instance_ids.end(); it++) { |
MACRUM | 0:276e7a263c35 | 112 | if ((*it) == obj_instance_id) { |
MACRUM | 0:276e7a263c35 | 113 | found = true; |
MACRUM | 0:276e7a263c35 | 114 | break; |
MACRUM | 0:276e7a263c35 | 115 | } |
MACRUM | 0:276e7a263c35 | 116 | } |
MACRUM | 0:276e7a263c35 | 117 | if (!found) { |
MACRUM | 0:276e7a263c35 | 118 | _changed_instance_ids.push_back(obj_instance_id); |
MACRUM | 0:276e7a263c35 | 119 | } |
MACRUM | 0:276e7a263c35 | 120 | |
MACRUM | 0:276e7a263c35 | 121 | _current_value = 0.0f; |
MACRUM | 0:276e7a263c35 | 122 | _last_value = 1.0f; |
MACRUM | 0:276e7a263c35 | 123 | schedule_report(); |
MACRUM | 0:276e7a263c35 | 124 | } |
MACRUM | 0:276e7a263c35 | 125 | |
MACRUM | 0:276e7a263c35 | 126 | bool M2MReportHandler::parse_notification_attribute(const char *query, |
MACRUM | 0:276e7a263c35 | 127 | M2MBase::BaseType type, |
MACRUM | 0:276e7a263c35 | 128 | M2MResourceInstance::ResourceType resource_type) |
MACRUM | 0:276e7a263c35 | 129 | { |
MACRUM | 0:276e7a263c35 | 130 | tr_debug("M2MReportHandler::parse_notification_attribute(Query %s, Base type %d)", query, (int)type); |
MACRUM | 0:276e7a263c35 | 131 | bool success = false; |
MACRUM | 0:276e7a263c35 | 132 | const char* sep_pos = strchr(query, '&'); |
MACRUM | 0:276e7a263c35 | 133 | const char* rest = query; |
MACRUM | 0:276e7a263c35 | 134 | if( sep_pos != NULL ){ |
MACRUM | 0:276e7a263c35 | 135 | char query_options[5][20]; |
MACRUM | 0:276e7a263c35 | 136 | float pmin = _pmin; |
MACRUM | 0:276e7a263c35 | 137 | float pmax = _pmax; |
MACRUM | 0:276e7a263c35 | 138 | float lt = _lt; |
MACRUM | 0:276e7a263c35 | 139 | float gt = _gt; |
MACRUM | 0:276e7a263c35 | 140 | float st = _st; |
MACRUM | 0:276e7a263c35 | 141 | float high = _high_step; |
MACRUM | 0:276e7a263c35 | 142 | float low = _low_step; |
MACRUM | 0:276e7a263c35 | 143 | uint8_t attr = _attribute_state; |
MACRUM | 0:276e7a263c35 | 144 | |
MACRUM | 0:276e7a263c35 | 145 | memset(query_options, 0, sizeof(query_options[0][0]) * 5 * 20); |
MACRUM | 0:276e7a263c35 | 146 | uint8_t num_options = 0; |
MACRUM | 0:276e7a263c35 | 147 | while( sep_pos != NULL && num_options < 5){ |
MACRUM | 0:276e7a263c35 | 148 | size_t len = (size_t)(sep_pos-rest); |
MACRUM | 0:276e7a263c35 | 149 | if( len > 19 ){ |
MACRUM | 0:276e7a263c35 | 150 | len = 19; |
MACRUM | 0:276e7a263c35 | 151 | } |
MACRUM | 0:276e7a263c35 | 152 | memcpy(query_options[num_options], rest, len); |
MACRUM | 0:276e7a263c35 | 153 | sep_pos++; |
MACRUM | 0:276e7a263c35 | 154 | rest = sep_pos; |
MACRUM | 0:276e7a263c35 | 155 | sep_pos = strchr(rest, '&'); |
MACRUM | 0:276e7a263c35 | 156 | num_options++; |
MACRUM | 0:276e7a263c35 | 157 | } |
MACRUM | 0:276e7a263c35 | 158 | if( num_options < 5 && strlen(rest) > 0){ |
MACRUM | 0:276e7a263c35 | 159 | size_t len = (size_t)strlen(rest); |
MACRUM | 0:276e7a263c35 | 160 | if( len > 19 ){ |
MACRUM | 0:276e7a263c35 | 161 | len = 19; |
MACRUM | 0:276e7a263c35 | 162 | } |
MACRUM | 0:276e7a263c35 | 163 | memcpy(query_options[num_options++], rest, len); |
MACRUM | 0:276e7a263c35 | 164 | } |
MACRUM | 0:276e7a263c35 | 165 | |
MACRUM | 0:276e7a263c35 | 166 | for (int option = 0; option < num_options; option++) { |
MACRUM | 0:276e7a263c35 | 167 | success = set_notification_attribute(query_options[option],type, resource_type); |
MACRUM | 0:276e7a263c35 | 168 | if (!success) { |
MACRUM | 0:276e7a263c35 | 169 | tr_debug("M2MReportHandler::parse_notification_attribute - break"); |
MACRUM | 0:276e7a263c35 | 170 | break; |
MACRUM | 0:276e7a263c35 | 171 | } |
MACRUM | 0:276e7a263c35 | 172 | } |
MACRUM | 0:276e7a263c35 | 173 | |
MACRUM | 0:276e7a263c35 | 174 | if(success) { |
MACRUM | 0:276e7a263c35 | 175 | success = check_attribute_validity(); |
MACRUM | 0:276e7a263c35 | 176 | } |
MACRUM | 0:276e7a263c35 | 177 | else { |
MACRUM | 0:276e7a263c35 | 178 | tr_debug("M2MReportHandler::parse_notification_attribute - not valid query"); |
MACRUM | 0:276e7a263c35 | 179 | _pmin = pmin; |
MACRUM | 0:276e7a263c35 | 180 | _pmax = pmax; |
MACRUM | 0:276e7a263c35 | 181 | _st = st; |
MACRUM | 0:276e7a263c35 | 182 | _lt = lt; |
MACRUM | 0:276e7a263c35 | 183 | _gt = gt; |
MACRUM | 0:276e7a263c35 | 184 | _high_step = high; |
MACRUM | 0:276e7a263c35 | 185 | _low_step = low; |
MACRUM | 0:276e7a263c35 | 186 | _attribute_state = attr; |
MACRUM | 0:276e7a263c35 | 187 | } |
MACRUM | 0:276e7a263c35 | 188 | } |
MACRUM | 0:276e7a263c35 | 189 | else { |
MACRUM | 0:276e7a263c35 | 190 | if(set_notification_attribute(query, type, resource_type)) { |
MACRUM | 0:276e7a263c35 | 191 | success = check_attribute_validity(); |
MACRUM | 0:276e7a263c35 | 192 | } |
MACRUM | 0:276e7a263c35 | 193 | } |
MACRUM | 0:276e7a263c35 | 194 | |
MACRUM | 0:276e7a263c35 | 195 | return success; |
MACRUM | 0:276e7a263c35 | 196 | } |
MACRUM | 0:276e7a263c35 | 197 | |
MACRUM | 0:276e7a263c35 | 198 | void M2MReportHandler::timer_expired(M2MTimerObserver::Type type) |
MACRUM | 0:276e7a263c35 | 199 | { |
MACRUM | 0:276e7a263c35 | 200 | switch(type) { |
MACRUM | 0:276e7a263c35 | 201 | case M2MTimerObserver::PMinTimer: { |
MACRUM | 0:276e7a263c35 | 202 | tr_debug("M2MReportHandler::timer_expired - PMIN"); |
MACRUM | 0:276e7a263c35 | 203 | _pmin_exceeded = true; |
MACRUM | 0:276e7a263c35 | 204 | if (_notify || |
MACRUM | 0:276e7a263c35 | 205 | (_pmin > 0 && |
MACRUM | 0:276e7a263c35 | 206 | (_attribute_state & M2MReportHandler::Pmax) != M2MReportHandler::Pmax)){ |
MACRUM | 0:276e7a263c35 | 207 | report(); |
MACRUM | 0:276e7a263c35 | 208 | } |
MACRUM | 0:276e7a263c35 | 209 | } |
MACRUM | 0:276e7a263c35 | 210 | break; |
MACRUM | 0:276e7a263c35 | 211 | case M2MTimerObserver::PMaxTimer: { |
MACRUM | 0:276e7a263c35 | 212 | tr_debug("M2MReportHandler::timer_expired - PMAX"); |
MACRUM | 0:276e7a263c35 | 213 | _pmax_exceeded = true; |
MACRUM | 0:276e7a263c35 | 214 | if (_pmin_exceeded || |
MACRUM | 0:276e7a263c35 | 215 | (_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin ) { |
MACRUM | 0:276e7a263c35 | 216 | report(); |
MACRUM | 0:276e7a263c35 | 217 | } |
MACRUM | 0:276e7a263c35 | 218 | } |
MACRUM | 0:276e7a263c35 | 219 | break; |
MACRUM | 0:276e7a263c35 | 220 | default: |
MACRUM | 0:276e7a263c35 | 221 | break; |
MACRUM | 0:276e7a263c35 | 222 | } |
MACRUM | 0:276e7a263c35 | 223 | } |
MACRUM | 0:276e7a263c35 | 224 | |
MACRUM | 0:276e7a263c35 | 225 | bool M2MReportHandler::set_notification_attribute(const char* option, |
MACRUM | 0:276e7a263c35 | 226 | M2MBase::BaseType type, |
MACRUM | 0:276e7a263c35 | 227 | M2MResourceInstance::ResourceType resource_type) |
MACRUM | 0:276e7a263c35 | 228 | { |
MACRUM | 0:276e7a263c35 | 229 | tr_debug("M2MReportHandler::set_notification_attribute()"); |
MACRUM | 0:276e7a263c35 | 230 | bool success = false; |
MACRUM | 0:276e7a263c35 | 231 | char attribute[20]; |
MACRUM | 0:276e7a263c35 | 232 | char value[20]; |
MACRUM | 0:276e7a263c35 | 233 | memset(&attribute, 0, 20); |
MACRUM | 0:276e7a263c35 | 234 | memset(&value, 0, 20); |
MACRUM | 0:276e7a263c35 | 235 | |
MACRUM | 0:276e7a263c35 | 236 | const char* pos = strstr(option, EQUAL); |
MACRUM | 0:276e7a263c35 | 237 | if( pos != NULL ){ |
MACRUM | 0:276e7a263c35 | 238 | memcpy(attribute, option, (size_t)(pos-option)); |
MACRUM | 0:276e7a263c35 | 239 | pos++; |
MACRUM | 0:276e7a263c35 | 240 | memcpy(value, pos, strlen(pos)); |
MACRUM | 0:276e7a263c35 | 241 | }else{ |
MACRUM | 0:276e7a263c35 | 242 | memcpy(attribute, option, (size_t)strlen(option) + 1); |
MACRUM | 0:276e7a263c35 | 243 | } |
MACRUM | 0:276e7a263c35 | 244 | |
MACRUM | 0:276e7a263c35 | 245 | if (strlen(value)) { |
MACRUM | 0:276e7a263c35 | 246 | if (strcmp(attribute, PMIN) == 0) { |
MACRUM | 0:276e7a263c35 | 247 | _pmin = atoi(value); |
MACRUM | 0:276e7a263c35 | 248 | success = true; |
MACRUM | 0:276e7a263c35 | 249 | _attribute_state |= M2MReportHandler::Pmin; |
MACRUM | 0:276e7a263c35 | 250 | tr_info("M2MReportHandler::set_notification_attribute %s to %" PRId32, attribute, _pmin); |
MACRUM | 0:276e7a263c35 | 251 | } |
MACRUM | 0:276e7a263c35 | 252 | else if(strcmp(attribute, PMAX) == 0) { |
MACRUM | 0:276e7a263c35 | 253 | _pmax = atoi(value); |
MACRUM | 0:276e7a263c35 | 254 | success = true; |
MACRUM | 0:276e7a263c35 | 255 | _attribute_state |= M2MReportHandler::Pmax; |
MACRUM | 0:276e7a263c35 | 256 | tr_info("M2MReportHandler::set_notification_attribute %s to %" PRId32, attribute, _pmax); |
MACRUM | 0:276e7a263c35 | 257 | } |
MACRUM | 0:276e7a263c35 | 258 | else if(strcmp(attribute, GT) == 0 && |
MACRUM | 0:276e7a263c35 | 259 | (M2MBase::Resource == type)){ |
MACRUM | 0:276e7a263c35 | 260 | _gt = atof(value); |
MACRUM | 0:276e7a263c35 | 261 | success = true; |
MACRUM | 0:276e7a263c35 | 262 | _attribute_state |= M2MReportHandler::Gt; |
MACRUM | 0:276e7a263c35 | 263 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _gt); |
MACRUM | 0:276e7a263c35 | 264 | } |
MACRUM | 0:276e7a263c35 | 265 | else if(strcmp(attribute, LT) == 0 && |
MACRUM | 0:276e7a263c35 | 266 | (M2MBase::Resource == type)){ |
MACRUM | 0:276e7a263c35 | 267 | _lt = atof(value); |
MACRUM | 0:276e7a263c35 | 268 | success = true; |
MACRUM | 0:276e7a263c35 | 269 | _attribute_state |= M2MReportHandler::Lt; |
MACRUM | 0:276e7a263c35 | 270 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _lt); |
MACRUM | 0:276e7a263c35 | 271 | } |
MACRUM | 0:276e7a263c35 | 272 | else if((strcmp(attribute, ST_SIZE) == 0 || (strcmp(attribute, STP) == 0)) |
MACRUM | 0:276e7a263c35 | 273 | && (M2MBase::Resource == type)){ |
MACRUM | 0:276e7a263c35 | 274 | _st = atof(value); |
MACRUM | 0:276e7a263c35 | 275 | success = true; |
MACRUM | 0:276e7a263c35 | 276 | _high_step = _current_value + _st; |
MACRUM | 0:276e7a263c35 | 277 | _low_step = _current_value - _st; |
MACRUM | 0:276e7a263c35 | 278 | _attribute_state |= M2MReportHandler::St; |
MACRUM | 0:276e7a263c35 | 279 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _st); |
MACRUM | 0:276e7a263c35 | 280 | } |
MACRUM | 0:276e7a263c35 | 281 | // Return false if try to set gt,lt or st when the resource type is something else than numerical |
MACRUM | 0:276e7a263c35 | 282 | if ((resource_type != M2MResourceInstance::INTEGER && |
MACRUM | 0:276e7a263c35 | 283 | resource_type != M2MResourceInstance::FLOAT) && |
MACRUM | 0:276e7a263c35 | 284 | ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
MACRUM | 0:276e7a263c35 | 285 | (_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
MACRUM | 0:276e7a263c35 | 286 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St)) { |
MACRUM | 0:276e7a263c35 | 287 | tr_debug("M2MReportHandler::set_notification_attribute - not numerical resource"); |
MACRUM | 0:276e7a263c35 | 288 | success = false; |
MACRUM | 0:276e7a263c35 | 289 | } |
MACRUM | 0:276e7a263c35 | 290 | } |
MACRUM | 0:276e7a263c35 | 291 | return success; |
MACRUM | 0:276e7a263c35 | 292 | } |
MACRUM | 0:276e7a263c35 | 293 | |
MACRUM | 0:276e7a263c35 | 294 | void M2MReportHandler::schedule_report() |
MACRUM | 0:276e7a263c35 | 295 | { |
MACRUM | 0:276e7a263c35 | 296 | tr_debug("M2MReportHandler::schedule_report()"); |
MACRUM | 0:276e7a263c35 | 297 | _notify = true; |
MACRUM | 0:276e7a263c35 | 298 | if ((_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin || |
MACRUM | 0:276e7a263c35 | 299 | _pmin_exceeded) { |
MACRUM | 0:276e7a263c35 | 300 | report(); |
MACRUM | 0:276e7a263c35 | 301 | } |
MACRUM | 0:276e7a263c35 | 302 | } |
MACRUM | 0:276e7a263c35 | 303 | |
MACRUM | 0:276e7a263c35 | 304 | void M2MReportHandler::report() |
MACRUM | 0:276e7a263c35 | 305 | { |
MACRUM | 0:276e7a263c35 | 306 | tr_debug("M2MReportHandler::report()"); |
MACRUM | 0:276e7a263c35 | 307 | if(_current_value != _last_value && _notify) { |
MACRUM | 0:276e7a263c35 | 308 | if (_pmin_exceeded) { |
MACRUM | 0:276e7a263c35 | 309 | tr_debug("M2MReportHandler::report()- send with PMIN expiration"); |
MACRUM | 0:276e7a263c35 | 310 | } else { |
MACRUM | 0:276e7a263c35 | 311 | tr_debug("M2MReportHandler::report()- send with VALUE change"); |
MACRUM | 0:276e7a263c35 | 312 | } |
MACRUM | 0:276e7a263c35 | 313 | _pmin_exceeded = false; |
MACRUM | 0:276e7a263c35 | 314 | _pmax_exceeded = false; |
MACRUM | 0:276e7a263c35 | 315 | _notify = false; |
MACRUM | 0:276e7a263c35 | 316 | _observation_number++; |
MACRUM | 0:276e7a263c35 | 317 | if(_observation_number == 1) { |
MACRUM | 0:276e7a263c35 | 318 | // Increment the observation number by 1 if it is already 1 because CoAP specification has reserved 1 for DEREGISTER notification |
MACRUM | 0:276e7a263c35 | 319 | _observation_number++; |
MACRUM | 0:276e7a263c35 | 320 | } |
MACRUM | 0:276e7a263c35 | 321 | _observer.observation_to_be_sent(_changed_instance_ids, observation_number()); |
MACRUM | 0:276e7a263c35 | 322 | _changed_instance_ids.clear(); |
MACRUM | 0:276e7a263c35 | 323 | _pmax_timer.stop_timer(); |
MACRUM | 0:276e7a263c35 | 324 | } |
MACRUM | 0:276e7a263c35 | 325 | else { |
MACRUM | 0:276e7a263c35 | 326 | if (_pmax_exceeded) { |
MACRUM | 0:276e7a263c35 | 327 | tr_debug("M2MReportHandler::report()- send with PMAX expiration"); |
MACRUM | 0:276e7a263c35 | 328 | _observation_number++; |
MACRUM | 0:276e7a263c35 | 329 | if(_observation_number == 1) { |
MACRUM | 0:276e7a263c35 | 330 | // Increment the observation number by 1 if it is already 1 because CoAP specification has reserved 1 for DEREGISTER notification |
MACRUM | 0:276e7a263c35 | 331 | _observation_number++; |
MACRUM | 0:276e7a263c35 | 332 | } |
MACRUM | 0:276e7a263c35 | 333 | _observer.observation_to_be_sent(_changed_instance_ids, observation_number(),true); |
MACRUM | 0:276e7a263c35 | 334 | _changed_instance_ids.clear(); |
MACRUM | 0:276e7a263c35 | 335 | } |
MACRUM | 0:276e7a263c35 | 336 | else { |
MACRUM | 0:276e7a263c35 | 337 | tr_debug("M2MReportHandler::report()- no need to send"); |
MACRUM | 0:276e7a263c35 | 338 | } |
MACRUM | 0:276e7a263c35 | 339 | } |
MACRUM | 0:276e7a263c35 | 340 | handle_timers(); |
MACRUM | 0:276e7a263c35 | 341 | _last_value = _current_value; |
MACRUM | 0:276e7a263c35 | 342 | } |
MACRUM | 0:276e7a263c35 | 343 | |
MACRUM | 0:276e7a263c35 | 344 | void M2MReportHandler::handle_timers() |
MACRUM | 0:276e7a263c35 | 345 | { |
MACRUM | 0:276e7a263c35 | 346 | tr_debug("M2MReportHandler::handle_timers()"); |
MACRUM | 0:276e7a263c35 | 347 | uint64_t time_interval = 0; |
MACRUM | 0:276e7a263c35 | 348 | if ((_attribute_state & M2MReportHandler::Pmin) == M2MReportHandler::Pmin) { |
MACRUM | 0:276e7a263c35 | 349 | if (_pmin == _pmax) { |
MACRUM | 0:276e7a263c35 | 350 | _pmin_exceeded = true; |
MACRUM | 0:276e7a263c35 | 351 | } else { |
MACRUM | 0:276e7a263c35 | 352 | _pmin_exceeded = false; |
MACRUM | 0:276e7a263c35 | 353 | time_interval = (uint64_t) ((uint64_t)_pmin * 1000); |
MACRUM | 0:276e7a263c35 | 354 | tr_debug("M2MReportHandler::handle_timers() - Start PMIN interval: %d", (int)time_interval); |
MACRUM | 0:276e7a263c35 | 355 | _pmin_timer.start_timer(time_interval, |
MACRUM | 0:276e7a263c35 | 356 | M2MTimerObserver::PMinTimer, |
MACRUM | 0:276e7a263c35 | 357 | true); |
MACRUM | 0:276e7a263c35 | 358 | } |
MACRUM | 0:276e7a263c35 | 359 | } |
MACRUM | 0:276e7a263c35 | 360 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax) { |
MACRUM | 0:276e7a263c35 | 361 | if (_pmax > 0) { |
MACRUM | 0:276e7a263c35 | 362 | time_interval = (uint64_t) ((uint64_t)_pmax * 1000); |
MACRUM | 0:276e7a263c35 | 363 | tr_debug("M2MReportHandler::handle_timers() - Start PMAX interval: %d", (int)time_interval); |
MACRUM | 0:276e7a263c35 | 364 | _pmax_timer.start_timer(time_interval, |
MACRUM | 0:276e7a263c35 | 365 | M2MTimerObserver::PMaxTimer, |
MACRUM | 0:276e7a263c35 | 366 | true); |
MACRUM | 0:276e7a263c35 | 367 | } |
MACRUM | 0:276e7a263c35 | 368 | } |
MACRUM | 0:276e7a263c35 | 369 | } |
MACRUM | 0:276e7a263c35 | 370 | |
MACRUM | 0:276e7a263c35 | 371 | bool M2MReportHandler::check_attribute_validity() const |
MACRUM | 0:276e7a263c35 | 372 | { |
MACRUM | 0:276e7a263c35 | 373 | bool success = true; |
MACRUM | 0:276e7a263c35 | 374 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax && |
MACRUM | 0:276e7a263c35 | 375 | ((_pmax >= -1.0) && (_pmin > _pmax))) { |
MACRUM | 0:276e7a263c35 | 376 | success = false; |
MACRUM | 0:276e7a263c35 | 377 | } |
MACRUM | 0:276e7a263c35 | 378 | float low = _lt + 2 * _st; |
MACRUM | 0:276e7a263c35 | 379 | if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
MACRUM | 0:276e7a263c35 | 380 | (low >= _gt)) { |
MACRUM | 0:276e7a263c35 | 381 | success = false; |
MACRUM | 0:276e7a263c35 | 382 | } |
MACRUM | 0:276e7a263c35 | 383 | return success; |
MACRUM | 0:276e7a263c35 | 384 | } |
MACRUM | 0:276e7a263c35 | 385 | |
MACRUM | 0:276e7a263c35 | 386 | void M2MReportHandler::stop_timers() |
MACRUM | 0:276e7a263c35 | 387 | { |
MACRUM | 0:276e7a263c35 | 388 | tr_debug("M2MReportHandler::stop_timers()"); |
MACRUM | 0:276e7a263c35 | 389 | |
MACRUM | 0:276e7a263c35 | 390 | _pmin_exceeded = false; |
MACRUM | 0:276e7a263c35 | 391 | _pmin_timer.stop_timer(); |
MACRUM | 0:276e7a263c35 | 392 | |
MACRUM | 0:276e7a263c35 | 393 | _pmax_exceeded = false; |
MACRUM | 0:276e7a263c35 | 394 | _pmax_timer.stop_timer(); |
MACRUM | 0:276e7a263c35 | 395 | |
MACRUM | 0:276e7a263c35 | 396 | tr_debug("M2MReportHandler::stop_timers() - out"); |
MACRUM | 0:276e7a263c35 | 397 | } |
MACRUM | 0:276e7a263c35 | 398 | |
MACRUM | 0:276e7a263c35 | 399 | void M2MReportHandler::set_default_values() |
MACRUM | 0:276e7a263c35 | 400 | { |
MACRUM | 0:276e7a263c35 | 401 | tr_debug("M2MReportHandler::set_default_values"); |
MACRUM | 0:276e7a263c35 | 402 | _pmax = -1.0; |
MACRUM | 0:276e7a263c35 | 403 | _pmin = 1.0; |
MACRUM | 0:276e7a263c35 | 404 | _gt = 0.0f; |
MACRUM | 0:276e7a263c35 | 405 | _lt = 0.0f; |
MACRUM | 0:276e7a263c35 | 406 | _st = 0.0f; |
MACRUM | 0:276e7a263c35 | 407 | _high_step = 0.0f; |
MACRUM | 0:276e7a263c35 | 408 | _low_step = 0.0f; |
MACRUM | 0:276e7a263c35 | 409 | _pmin_exceeded = false; |
MACRUM | 0:276e7a263c35 | 410 | _pmax_exceeded = false; |
MACRUM | 0:276e7a263c35 | 411 | _last_value = -1.0f; |
MACRUM | 0:276e7a263c35 | 412 | _attribute_state = 0; |
MACRUM | 0:276e7a263c35 | 413 | _changed_instance_ids.clear(); |
MACRUM | 0:276e7a263c35 | 414 | } |
MACRUM | 0:276e7a263c35 | 415 | |
MACRUM | 0:276e7a263c35 | 416 | bool M2MReportHandler::check_threshold_values() const |
MACRUM | 0:276e7a263c35 | 417 | { |
MACRUM | 0:276e7a263c35 | 418 | tr_debug("M2MReportHandler::check_threshold_values"); |
MACRUM | 0:276e7a263c35 | 419 | tr_debug("Current value: %f", _current_value); |
MACRUM | 0:276e7a263c35 | 420 | tr_debug("High step: %f", _high_step); |
MACRUM | 0:276e7a263c35 | 421 | tr_debug("Low step: %f", _low_step); |
MACRUM | 0:276e7a263c35 | 422 | tr_debug("Less than: %f", _lt); |
MACRUM | 0:276e7a263c35 | 423 | tr_debug("Greater than: %f", _gt); |
MACRUM | 0:276e7a263c35 | 424 | tr_debug("Step: %f", _st); |
MACRUM | 0:276e7a263c35 | 425 | bool can_send = false; |
MACRUM | 0:276e7a263c35 | 426 | // Check step condition |
MACRUM | 0:276e7a263c35 | 427 | if ((_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
MACRUM | 0:276e7a263c35 | 428 | if ((_current_value >= _high_step || |
MACRUM | 0:276e7a263c35 | 429 | _current_value <= _low_step)) { |
MACRUM | 0:276e7a263c35 | 430 | can_send = true; |
MACRUM | 0:276e7a263c35 | 431 | } |
MACRUM | 0:276e7a263c35 | 432 | else { |
MACRUM | 0:276e7a263c35 | 433 | if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
MACRUM | 0:276e7a263c35 | 434 | (_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt ) { |
MACRUM | 0:276e7a263c35 | 435 | can_send = check_gt_lt_params(); |
MACRUM | 0:276e7a263c35 | 436 | } |
MACRUM | 0:276e7a263c35 | 437 | else { |
MACRUM | 0:276e7a263c35 | 438 | can_send = false; |
MACRUM | 0:276e7a263c35 | 439 | } |
MACRUM | 0:276e7a263c35 | 440 | } |
MACRUM | 0:276e7a263c35 | 441 | } |
MACRUM | 0:276e7a263c35 | 442 | else { |
MACRUM | 0:276e7a263c35 | 443 | can_send = check_gt_lt_params(); |
MACRUM | 0:276e7a263c35 | 444 | } |
MACRUM | 0:276e7a263c35 | 445 | tr_debug("M2MReportHandler::check_threshold_values - value in range = %d", (int)can_send); |
MACRUM | 0:276e7a263c35 | 446 | return can_send; |
MACRUM | 0:276e7a263c35 | 447 | } |
MACRUM | 0:276e7a263c35 | 448 | |
MACRUM | 0:276e7a263c35 | 449 | bool M2MReportHandler::check_gt_lt_params() const |
MACRUM | 0:276e7a263c35 | 450 | { |
MACRUM | 0:276e7a263c35 | 451 | tr_debug("M2MReportHandler::check_gt_lt_params"); |
MACRUM | 0:276e7a263c35 | 452 | bool can_send = false; |
MACRUM | 0:276e7a263c35 | 453 | // GT & LT set. |
MACRUM | 0:276e7a263c35 | 454 | if ((_attribute_state & (M2MReportHandler::Lt | M2MReportHandler::Gt)) |
MACRUM | 0:276e7a263c35 | 455 | == (M2MReportHandler::Lt | M2MReportHandler::Gt)) { |
MACRUM | 0:276e7a263c35 | 456 | if (_current_value > _gt || _current_value < _lt) { |
MACRUM | 0:276e7a263c35 | 457 | can_send = true; |
MACRUM | 0:276e7a263c35 | 458 | } |
MACRUM | 0:276e7a263c35 | 459 | else { |
MACRUM | 0:276e7a263c35 | 460 | can_send = false; |
MACRUM | 0:276e7a263c35 | 461 | } |
MACRUM | 0:276e7a263c35 | 462 | } |
MACRUM | 0:276e7a263c35 | 463 | // Only LT |
MACRUM | 0:276e7a263c35 | 464 | else if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt && |
MACRUM | 0:276e7a263c35 | 465 | (_attribute_state & M2MReportHandler::Gt) == 0 ) { |
MACRUM | 0:276e7a263c35 | 466 | if (_current_value < _lt) { |
MACRUM | 0:276e7a263c35 | 467 | can_send = true; |
MACRUM | 0:276e7a263c35 | 468 | } |
MACRUM | 0:276e7a263c35 | 469 | else { |
MACRUM | 0:276e7a263c35 | 470 | can_send = false; |
MACRUM | 0:276e7a263c35 | 471 | } |
MACRUM | 0:276e7a263c35 | 472 | } |
MACRUM | 0:276e7a263c35 | 473 | // Only GT |
MACRUM | 0:276e7a263c35 | 474 | else if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
MACRUM | 0:276e7a263c35 | 475 | (_attribute_state & M2MReportHandler::Lt) == 0 ) { |
MACRUM | 0:276e7a263c35 | 476 | if (_current_value > _gt) { |
MACRUM | 0:276e7a263c35 | 477 | can_send = true; |
MACRUM | 0:276e7a263c35 | 478 | } |
MACRUM | 0:276e7a263c35 | 479 | else { |
MACRUM | 0:276e7a263c35 | 480 | can_send = false; |
MACRUM | 0:276e7a263c35 | 481 | } |
MACRUM | 0:276e7a263c35 | 482 | } |
MACRUM | 0:276e7a263c35 | 483 | // GT & LT not set. |
MACRUM | 0:276e7a263c35 | 484 | else { |
MACRUM | 0:276e7a263c35 | 485 | can_send = true; |
MACRUM | 0:276e7a263c35 | 486 | } |
MACRUM | 0:276e7a263c35 | 487 | tr_debug("M2MReportHandler::check_gt_lt_params - value in range = %d", (int)can_send); |
MACRUM | 0:276e7a263c35 | 488 | return can_send; |
MACRUM | 0:276e7a263c35 | 489 | } |
MACRUM | 0:276e7a263c35 | 490 | |
MACRUM | 0:276e7a263c35 | 491 | uint8_t M2MReportHandler::attribute_flags() const |
MACRUM | 0:276e7a263c35 | 492 | { |
MACRUM | 0:276e7a263c35 | 493 | return _attribute_state; |
MACRUM | 0:276e7a263c35 | 494 | } |
MACRUM | 0:276e7a263c35 | 495 | |
MACRUM | 0:276e7a263c35 | 496 | void M2MReportHandler::set_observation_token(const uint8_t *token, const uint8_t length) |
MACRUM | 0:276e7a263c35 | 497 | { |
MACRUM | 0:276e7a263c35 | 498 | free(_token); |
MACRUM | 0:276e7a263c35 | 499 | _token = NULL; |
MACRUM | 0:276e7a263c35 | 500 | _token_length = 0; |
MACRUM | 0:276e7a263c35 | 501 | |
MACRUM | 0:276e7a263c35 | 502 | if( token != NULL && length > 0 ) { |
MACRUM | 0:276e7a263c35 | 503 | _token = alloc_string_copy((uint8_t *)token, length); |
MACRUM | 0:276e7a263c35 | 504 | if(_token) { |
MACRUM | 0:276e7a263c35 | 505 | _token_length = length; |
MACRUM | 0:276e7a263c35 | 506 | } |
MACRUM | 0:276e7a263c35 | 507 | } |
MACRUM | 0:276e7a263c35 | 508 | } |
MACRUM | 0:276e7a263c35 | 509 | |
MACRUM | 0:276e7a263c35 | 510 | void M2MReportHandler::get_observation_token(uint8_t *token, uint8_t &token_length) const |
MACRUM | 0:276e7a263c35 | 511 | { |
MACRUM | 0:276e7a263c35 | 512 | memcpy(token, _token, _token_length); |
MACRUM | 0:276e7a263c35 | 513 | token_length = _token_length; |
MACRUM | 0:276e7a263c35 | 514 | } |
MACRUM | 0:276e7a263c35 | 515 | |
MACRUM | 0:276e7a263c35 | 516 | uint16_t M2MReportHandler::observation_number() const |
MACRUM | 0:276e7a263c35 | 517 | { |
MACRUM | 0:276e7a263c35 | 518 | return _observation_number; |
MACRUM | 0:276e7a263c35 | 519 | } |
MACRUM | 0:276e7a263c35 | 520 | |
MACRUM | 0:276e7a263c35 | 521 | void M2MReportHandler::add_observation_level(M2MBase::Observation obs_level) |
MACRUM | 0:276e7a263c35 | 522 | { |
MACRUM | 0:276e7a263c35 | 523 | _observation_level = (M2MBase::Observation)(_observation_level | obs_level); |
MACRUM | 0:276e7a263c35 | 524 | } |
MACRUM | 0:276e7a263c35 | 525 | |
MACRUM | 0:276e7a263c35 | 526 | void M2MReportHandler::remove_observation_level(M2MBase::Observation obs_level) |
MACRUM | 0:276e7a263c35 | 527 | { |
MACRUM | 0:276e7a263c35 | 528 | _observation_level = (M2MBase::Observation)(_observation_level & ~obs_level); |
MACRUM | 0:276e7a263c35 | 529 | } |
MACRUM | 0:276e7a263c35 | 530 | |
MACRUM | 0:276e7a263c35 | 531 | M2MBase::Observation M2MReportHandler::observation_level() const |
MACRUM | 0:276e7a263c35 | 532 | { |
MACRUM | 0:276e7a263c35 | 533 | return _observation_level; |
MACRUM | 0:276e7a263c35 | 534 | } |
MACRUM | 0:276e7a263c35 | 535 | |
MACRUM | 0:276e7a263c35 | 536 | bool M2MReportHandler::is_under_observation() const |
MACRUM | 0:276e7a263c35 | 537 | { |
MACRUM | 0:276e7a263c35 | 538 | return _is_under_observation; |
MACRUM | 0:276e7a263c35 | 539 | } |
MACRUM | 0:276e7a263c35 | 540 | |
MACRUM | 0:276e7a263c35 | 541 | uint8_t* M2MReportHandler::alloc_string_copy(const uint8_t* source, uint32_t size) |
MACRUM | 0:276e7a263c35 | 542 | { |
MACRUM | 0:276e7a263c35 | 543 | assert(source != NULL); |
MACRUM | 0:276e7a263c35 | 544 | |
MACRUM | 0:276e7a263c35 | 545 | uint8_t* result = (uint8_t*)malloc(size + 1); |
MACRUM | 0:276e7a263c35 | 546 | if (result) { |
MACRUM | 0:276e7a263c35 | 547 | memcpy(result, source, size); |
MACRUM | 0:276e7a263c35 | 548 | result[size] = '\0'; |
MACRUM | 0:276e7a263c35 | 549 | } |
MACRUM | 0:276e7a263c35 | 550 | return result; |
MACRUM | 0:276e7a263c35 | 551 | } |