Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
mbed-client/source/m2mreporthandler.cpp@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | /* |
ram54288 | 0:a7a43371b306 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
ram54288 | 0:a7a43371b306 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:a7a43371b306 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:a7a43371b306 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 7 | * |
ram54288 | 0:a7a43371b306 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 9 | * |
ram54288 | 0:a7a43371b306 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:a7a43371b306 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 14 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 15 | */ |
ram54288 | 0:a7a43371b306 | 16 | #include "mbed-client/m2mreportobserver.h" |
ram54288 | 0:a7a43371b306 | 17 | #include "mbed-client/m2mconstants.h" |
ram54288 | 0:a7a43371b306 | 18 | #include "mbed-client/m2mtimer.h" |
ram54288 | 0:a7a43371b306 | 19 | #include "include/m2mreporthandler.h" |
ram54288 | 0:a7a43371b306 | 20 | #include "mbed-trace/mbed_trace.h" |
ram54288 | 0:a7a43371b306 | 21 | #include <string.h> |
ram54288 | 0:a7a43371b306 | 22 | #include <stdlib.h> |
ram54288 | 0:a7a43371b306 | 23 | |
ram54288 | 0:a7a43371b306 | 24 | #define TRACE_GROUP "mClt" |
ram54288 | 0:a7a43371b306 | 25 | |
ram54288 | 0:a7a43371b306 | 26 | M2MReportHandler::M2MReportHandler(M2MReportObserver &observer) |
ram54288 | 0:a7a43371b306 | 27 | : _observer(observer), |
ram54288 | 0:a7a43371b306 | 28 | _attribute_state(0), |
ram54288 | 0:a7a43371b306 | 29 | _notify(false), |
ram54288 | 0:a7a43371b306 | 30 | _pmin_exceeded(false), |
ram54288 | 0:a7a43371b306 | 31 | _pmax_exceeded(false), |
ram54288 | 0:a7a43371b306 | 32 | _pmin_timer(*this), |
ram54288 | 0:a7a43371b306 | 33 | _pmax_timer(*this), |
ram54288 | 0:a7a43371b306 | 34 | _pmax(-1.0f), |
ram54288 | 0:a7a43371b306 | 35 | _pmin(1.0f), |
ram54288 | 0:a7a43371b306 | 36 | _current_value(0.0f), |
ram54288 | 0:a7a43371b306 | 37 | _gt(0.0f), |
ram54288 | 0:a7a43371b306 | 38 | _lt(0.0f), |
ram54288 | 0:a7a43371b306 | 39 | _st(0.0f), |
ram54288 | 0:a7a43371b306 | 40 | _high_step(0.0f), |
ram54288 | 0:a7a43371b306 | 41 | _low_step(0.0f), |
ram54288 | 0:a7a43371b306 | 42 | _last_value(-1.0f) |
ram54288 | 0:a7a43371b306 | 43 | { |
ram54288 | 0:a7a43371b306 | 44 | tr_debug("M2MReportHandler::M2MReportHandler()"); |
ram54288 | 0:a7a43371b306 | 45 | } |
ram54288 | 0:a7a43371b306 | 46 | |
ram54288 | 0:a7a43371b306 | 47 | M2MReportHandler::~M2MReportHandler() |
ram54288 | 0:a7a43371b306 | 48 | { |
ram54288 | 0:a7a43371b306 | 49 | tr_debug("M2MReportHandler::~M2MReportHandler()"); |
ram54288 | 0:a7a43371b306 | 50 | } |
ram54288 | 0:a7a43371b306 | 51 | |
ram54288 | 0:a7a43371b306 | 52 | void M2MReportHandler::set_under_observation(bool observed) |
ram54288 | 0:a7a43371b306 | 53 | { |
ram54288 | 0:a7a43371b306 | 54 | tr_debug("M2MReportHandler::set_under_observation(observed %d)", (int)observed); |
ram54288 | 0:a7a43371b306 | 55 | stop_timers(); |
ram54288 | 0:a7a43371b306 | 56 | if(observed) { |
ram54288 | 0:a7a43371b306 | 57 | handle_timers(); |
ram54288 | 0:a7a43371b306 | 58 | } |
ram54288 | 0:a7a43371b306 | 59 | else { |
ram54288 | 0:a7a43371b306 | 60 | set_default_values(); |
ram54288 | 0:a7a43371b306 | 61 | } |
ram54288 | 0:a7a43371b306 | 62 | } |
ram54288 | 0:a7a43371b306 | 63 | |
ram54288 | 0:a7a43371b306 | 64 | void M2MReportHandler::set_value(float value) |
ram54288 | 0:a7a43371b306 | 65 | { |
ram54288 | 0:a7a43371b306 | 66 | tr_debug("M2MReportHandler::set_value() - current %f, last %f", value, _last_value); |
ram54288 | 0:a7a43371b306 | 67 | _current_value = value; |
ram54288 | 0:a7a43371b306 | 68 | if(_current_value != _last_value) { |
ram54288 | 0:a7a43371b306 | 69 | tr_debug("M2MReportHandler::set_value() - UNDER OBSERVATION"); |
ram54288 | 0:a7a43371b306 | 70 | if (check_threshold_values()) { |
ram54288 | 0:a7a43371b306 | 71 | schedule_report(); |
ram54288 | 0:a7a43371b306 | 72 | } |
ram54288 | 0:a7a43371b306 | 73 | else { |
ram54288 | 0:a7a43371b306 | 74 | tr_debug("M2MReportHandler::set_value - value not in range"); |
ram54288 | 0:a7a43371b306 | 75 | _notify = false; |
ram54288 | 0:a7a43371b306 | 76 | _last_value = _current_value; |
ram54288 | 0:a7a43371b306 | 77 | if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
ram54288 | 0:a7a43371b306 | 78 | (_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
ram54288 | 0:a7a43371b306 | 79 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
ram54288 | 0:a7a43371b306 | 80 | tr_debug("M2MReportHandler::set_value - stop pmin timer"); |
ram54288 | 0:a7a43371b306 | 81 | _pmin_timer.stop_timer(); |
ram54288 | 0:a7a43371b306 | 82 | _pmin_exceeded = true; |
ram54288 | 0:a7a43371b306 | 83 | } |
ram54288 | 0:a7a43371b306 | 84 | } |
ram54288 | 0:a7a43371b306 | 85 | _high_step = _current_value + _st; |
ram54288 | 0:a7a43371b306 | 86 | _low_step = _current_value - _st; |
ram54288 | 0:a7a43371b306 | 87 | } |
ram54288 | 0:a7a43371b306 | 88 | } |
ram54288 | 0:a7a43371b306 | 89 | |
ram54288 | 0:a7a43371b306 | 90 | void M2MReportHandler::set_notification_trigger(uint16_t obj_instance_id) |
ram54288 | 0:a7a43371b306 | 91 | { |
ram54288 | 0:a7a43371b306 | 92 | tr_debug("M2MReportHandler::set_notification_trigger(): %d", obj_instance_id); |
ram54288 | 0:a7a43371b306 | 93 | // Add to array if not there yet |
ram54288 | 0:a7a43371b306 | 94 | m2m::Vector<uint16_t>::const_iterator it; |
ram54288 | 0:a7a43371b306 | 95 | it = _changed_instance_ids.begin(); |
ram54288 | 0:a7a43371b306 | 96 | bool found = false; |
ram54288 | 0:a7a43371b306 | 97 | for ( ; it != _changed_instance_ids.end(); it++) { |
ram54288 | 0:a7a43371b306 | 98 | if ((*it) == obj_instance_id) { |
ram54288 | 0:a7a43371b306 | 99 | found = true; |
ram54288 | 0:a7a43371b306 | 100 | break; |
ram54288 | 0:a7a43371b306 | 101 | } |
ram54288 | 0:a7a43371b306 | 102 | } |
ram54288 | 0:a7a43371b306 | 103 | if (!found) { |
ram54288 | 0:a7a43371b306 | 104 | _changed_instance_ids.push_back(obj_instance_id); |
ram54288 | 0:a7a43371b306 | 105 | } |
ram54288 | 0:a7a43371b306 | 106 | |
ram54288 | 0:a7a43371b306 | 107 | _current_value = 0.0f; |
ram54288 | 0:a7a43371b306 | 108 | _last_value = 1.0f; |
ram54288 | 0:a7a43371b306 | 109 | schedule_report(); |
ram54288 | 0:a7a43371b306 | 110 | } |
ram54288 | 0:a7a43371b306 | 111 | |
ram54288 | 0:a7a43371b306 | 112 | bool M2MReportHandler::parse_notification_attribute(const char *query, |
ram54288 | 0:a7a43371b306 | 113 | M2MBase::BaseType type, |
ram54288 | 0:a7a43371b306 | 114 | M2MResourceInstance::ResourceType resource_type) |
ram54288 | 0:a7a43371b306 | 115 | { |
ram54288 | 0:a7a43371b306 | 116 | tr_debug("M2MReportHandler::parse_notification_attribute(Query %s, Base type %d)", query, (int)type); |
ram54288 | 0:a7a43371b306 | 117 | bool success = false; |
ram54288 | 0:a7a43371b306 | 118 | const char* sep_pos = strchr(query, '&'); |
ram54288 | 0:a7a43371b306 | 119 | const char* rest = query; |
ram54288 | 0:a7a43371b306 | 120 | if( sep_pos != NULL ){ |
ram54288 | 0:a7a43371b306 | 121 | char query_options[5][20]; |
ram54288 | 0:a7a43371b306 | 122 | float pmin = _pmin; |
ram54288 | 0:a7a43371b306 | 123 | float pmax = _pmax; |
ram54288 | 0:a7a43371b306 | 124 | float lt = _lt; |
ram54288 | 0:a7a43371b306 | 125 | float gt = _gt; |
ram54288 | 0:a7a43371b306 | 126 | float st = _st; |
ram54288 | 0:a7a43371b306 | 127 | float high = _high_step; |
ram54288 | 0:a7a43371b306 | 128 | float low = _low_step; |
ram54288 | 0:a7a43371b306 | 129 | uint8_t attr = _attribute_state; |
ram54288 | 0:a7a43371b306 | 130 | |
ram54288 | 0:a7a43371b306 | 131 | memset(query_options, 0, sizeof(query_options[0][0]) * 5 * 20); |
ram54288 | 0:a7a43371b306 | 132 | uint8_t num_options = 0; |
ram54288 | 0:a7a43371b306 | 133 | while( sep_pos != NULL && num_options < 5){ |
ram54288 | 0:a7a43371b306 | 134 | size_t len = (size_t)(sep_pos-rest); |
ram54288 | 0:a7a43371b306 | 135 | if( len > 19 ){ |
ram54288 | 0:a7a43371b306 | 136 | len = 19; |
ram54288 | 0:a7a43371b306 | 137 | } |
ram54288 | 0:a7a43371b306 | 138 | memcpy(query_options[num_options], rest, len); |
ram54288 | 0:a7a43371b306 | 139 | sep_pos++; |
ram54288 | 0:a7a43371b306 | 140 | rest = sep_pos; |
ram54288 | 0:a7a43371b306 | 141 | sep_pos = strchr(rest, '&'); |
ram54288 | 0:a7a43371b306 | 142 | num_options++; |
ram54288 | 0:a7a43371b306 | 143 | } |
ram54288 | 0:a7a43371b306 | 144 | if( num_options < 5 && strlen(rest) > 0){ |
ram54288 | 0:a7a43371b306 | 145 | size_t len = (size_t)strlen(rest); |
ram54288 | 0:a7a43371b306 | 146 | if( len > 19 ){ |
ram54288 | 0:a7a43371b306 | 147 | len = 19; |
ram54288 | 0:a7a43371b306 | 148 | } |
ram54288 | 0:a7a43371b306 | 149 | memcpy(query_options[num_options++], rest, len); |
ram54288 | 0:a7a43371b306 | 150 | } |
ram54288 | 0:a7a43371b306 | 151 | |
ram54288 | 0:a7a43371b306 | 152 | for (int option = 0; option < num_options; option++) { |
ram54288 | 0:a7a43371b306 | 153 | success = set_notification_attribute(query_options[option],type, resource_type); |
ram54288 | 0:a7a43371b306 | 154 | if (!success) { |
ram54288 | 0:a7a43371b306 | 155 | tr_debug("M2MReportHandler::parse_notification_attribute - break"); |
ram54288 | 0:a7a43371b306 | 156 | break; |
ram54288 | 0:a7a43371b306 | 157 | } |
ram54288 | 0:a7a43371b306 | 158 | } |
ram54288 | 0:a7a43371b306 | 159 | |
ram54288 | 0:a7a43371b306 | 160 | if(success) { |
ram54288 | 0:a7a43371b306 | 161 | success = check_attribute_validity(); |
ram54288 | 0:a7a43371b306 | 162 | } |
ram54288 | 0:a7a43371b306 | 163 | else { |
ram54288 | 0:a7a43371b306 | 164 | tr_debug("M2MReportHandler::parse_notification_attribute - not valid query"); |
ram54288 | 0:a7a43371b306 | 165 | _pmin = pmin; |
ram54288 | 0:a7a43371b306 | 166 | _pmax = pmax; |
ram54288 | 0:a7a43371b306 | 167 | _st = st; |
ram54288 | 0:a7a43371b306 | 168 | _lt = lt; |
ram54288 | 0:a7a43371b306 | 169 | _gt = gt; |
ram54288 | 0:a7a43371b306 | 170 | _high_step = high; |
ram54288 | 0:a7a43371b306 | 171 | _low_step = low; |
ram54288 | 0:a7a43371b306 | 172 | _attribute_state = attr; |
ram54288 | 0:a7a43371b306 | 173 | } |
ram54288 | 0:a7a43371b306 | 174 | } |
ram54288 | 0:a7a43371b306 | 175 | else { |
ram54288 | 0:a7a43371b306 | 176 | if(set_notification_attribute(query, type, resource_type)) { |
ram54288 | 0:a7a43371b306 | 177 | success = check_attribute_validity(); |
ram54288 | 0:a7a43371b306 | 178 | } |
ram54288 | 0:a7a43371b306 | 179 | } |
ram54288 | 0:a7a43371b306 | 180 | |
ram54288 | 0:a7a43371b306 | 181 | return success; |
ram54288 | 0:a7a43371b306 | 182 | } |
ram54288 | 0:a7a43371b306 | 183 | |
ram54288 | 0:a7a43371b306 | 184 | void M2MReportHandler::timer_expired(M2MTimerObserver::Type type) |
ram54288 | 0:a7a43371b306 | 185 | { |
ram54288 | 0:a7a43371b306 | 186 | switch(type) { |
ram54288 | 0:a7a43371b306 | 187 | case M2MTimerObserver::PMinTimer: { |
ram54288 | 0:a7a43371b306 | 188 | tr_debug("M2MReportHandler::timer_expired - PMIN"); |
ram54288 | 0:a7a43371b306 | 189 | if (_notify || |
ram54288 | 0:a7a43371b306 | 190 | (_pmin > 0 && |
ram54288 | 0:a7a43371b306 | 191 | (_attribute_state & M2MReportHandler::Pmax) != M2MReportHandler::Pmax)){ |
ram54288 | 0:a7a43371b306 | 192 | report(); |
ram54288 | 0:a7a43371b306 | 193 | } |
ram54288 | 0:a7a43371b306 | 194 | else{ |
ram54288 | 0:a7a43371b306 | 195 | _pmin_exceeded = true; |
ram54288 | 0:a7a43371b306 | 196 | } |
ram54288 | 0:a7a43371b306 | 197 | } |
ram54288 | 0:a7a43371b306 | 198 | break; |
ram54288 | 0:a7a43371b306 | 199 | case M2MTimerObserver::PMaxTimer: { |
ram54288 | 0:a7a43371b306 | 200 | tr_debug("M2MReportHandler::timer_expired - PMAX"); |
ram54288 | 0:a7a43371b306 | 201 | _pmax_exceeded = true; |
ram54288 | 0:a7a43371b306 | 202 | if (_pmin_exceeded || |
ram54288 | 0:a7a43371b306 | 203 | (_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin ) { |
ram54288 | 0:a7a43371b306 | 204 | report(); |
ram54288 | 0:a7a43371b306 | 205 | } |
ram54288 | 0:a7a43371b306 | 206 | } |
ram54288 | 0:a7a43371b306 | 207 | break; |
ram54288 | 0:a7a43371b306 | 208 | default: |
ram54288 | 0:a7a43371b306 | 209 | break; |
ram54288 | 0:a7a43371b306 | 210 | } |
ram54288 | 0:a7a43371b306 | 211 | } |
ram54288 | 0:a7a43371b306 | 212 | |
ram54288 | 0:a7a43371b306 | 213 | bool M2MReportHandler::set_notification_attribute(const char* option, |
ram54288 | 0:a7a43371b306 | 214 | M2MBase::BaseType type, |
ram54288 | 0:a7a43371b306 | 215 | M2MResourceInstance::ResourceType resource_type) |
ram54288 | 0:a7a43371b306 | 216 | { |
ram54288 | 0:a7a43371b306 | 217 | tr_debug("M2MReportHandler::set_notification_attribute()"); |
ram54288 | 0:a7a43371b306 | 218 | bool success = false; |
ram54288 | 0:a7a43371b306 | 219 | char attribute[20]; |
ram54288 | 0:a7a43371b306 | 220 | char value[20]; |
ram54288 | 0:a7a43371b306 | 221 | memset(&attribute, 0, 20); |
ram54288 | 0:a7a43371b306 | 222 | memset(&value, 0, 20); |
ram54288 | 0:a7a43371b306 | 223 | |
ram54288 | 0:a7a43371b306 | 224 | const char* pos = strstr(option, EQUAL); |
ram54288 | 0:a7a43371b306 | 225 | if( pos != NULL ){ |
ram54288 | 0:a7a43371b306 | 226 | memcpy(attribute, option, (size_t)(pos-option)); |
ram54288 | 0:a7a43371b306 | 227 | pos++; |
ram54288 | 0:a7a43371b306 | 228 | memcpy(value, pos, strlen(pos)); |
ram54288 | 0:a7a43371b306 | 229 | }else{ |
ram54288 | 0:a7a43371b306 | 230 | memcpy(attribute, option, (size_t)strlen(option) + 1); |
ram54288 | 0:a7a43371b306 | 231 | } |
ram54288 | 0:a7a43371b306 | 232 | |
ram54288 | 0:a7a43371b306 | 233 | if (strlen(value)) { |
ram54288 | 0:a7a43371b306 | 234 | if (strcmp(attribute, PMIN) == 0) { |
ram54288 | 0:a7a43371b306 | 235 | _pmin = atoi(value); |
ram54288 | 0:a7a43371b306 | 236 | success = true; |
ram54288 | 0:a7a43371b306 | 237 | _attribute_state |= M2MReportHandler::Pmin; |
ram54288 | 0:a7a43371b306 | 238 | tr_debug("M2MReportHandler::set_notification_attribute %s to %d", attribute, _pmin); |
ram54288 | 0:a7a43371b306 | 239 | } |
ram54288 | 0:a7a43371b306 | 240 | else if(strcmp(attribute, PMAX) == 0) { |
ram54288 | 0:a7a43371b306 | 241 | _pmax = atoi(value); |
ram54288 | 0:a7a43371b306 | 242 | success = true; |
ram54288 | 0:a7a43371b306 | 243 | _attribute_state |= M2MReportHandler::Pmax; |
ram54288 | 0:a7a43371b306 | 244 | tr_debug("M2MReportHandler::set_notification_attribute %s to %d", attribute, _pmax); |
ram54288 | 0:a7a43371b306 | 245 | } |
ram54288 | 0:a7a43371b306 | 246 | else if(strcmp(attribute, GT) == 0 && |
ram54288 | 0:a7a43371b306 | 247 | (M2MBase::Resource == type)){ |
ram54288 | 0:a7a43371b306 | 248 | _gt = atof(value); |
ram54288 | 0:a7a43371b306 | 249 | success = true; |
ram54288 | 0:a7a43371b306 | 250 | _attribute_state |= M2MReportHandler::Gt; |
ram54288 | 0:a7a43371b306 | 251 | tr_debug("M2MReportHandler::set_notification_attribute %s to %f", attribute, _gt); |
ram54288 | 0:a7a43371b306 | 252 | } |
ram54288 | 0:a7a43371b306 | 253 | else if(strcmp(attribute, LT) == 0 && |
ram54288 | 0:a7a43371b306 | 254 | (M2MBase::Resource == type)){ |
ram54288 | 0:a7a43371b306 | 255 | _lt = atof(value); |
ram54288 | 0:a7a43371b306 | 256 | success = true; |
ram54288 | 0:a7a43371b306 | 257 | _attribute_state |= M2MReportHandler::Lt; |
ram54288 | 0:a7a43371b306 | 258 | tr_debug("M2MReportHandler::set_notification_attribute %s to %f", attribute, _lt); |
ram54288 | 0:a7a43371b306 | 259 | } |
ram54288 | 0:a7a43371b306 | 260 | else if((strcmp(attribute, ST_SIZE) == 0 || (strcmp(attribute, STP) == 0)) |
ram54288 | 0:a7a43371b306 | 261 | && (M2MBase::Resource == type)){ |
ram54288 | 0:a7a43371b306 | 262 | _st = atof(value); |
ram54288 | 0:a7a43371b306 | 263 | success = true; |
ram54288 | 0:a7a43371b306 | 264 | _high_step = _current_value + _st; |
ram54288 | 0:a7a43371b306 | 265 | _low_step = _current_value - _st; |
ram54288 | 0:a7a43371b306 | 266 | _attribute_state |= M2MReportHandler::St; |
ram54288 | 0:a7a43371b306 | 267 | tr_debug("M2MReportHandler::set_notification_attribute %s to %f", attribute, _st); |
ram54288 | 0:a7a43371b306 | 268 | } |
ram54288 | 0:a7a43371b306 | 269 | // Return false if try to set gt,lt or st when the resource type is something else than numerical |
ram54288 | 0:a7a43371b306 | 270 | if ((resource_type != M2MResourceInstance::INTEGER && |
ram54288 | 0:a7a43371b306 | 271 | resource_type != M2MResourceInstance::FLOAT) && |
ram54288 | 0:a7a43371b306 | 272 | ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
ram54288 | 0:a7a43371b306 | 273 | (_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
ram54288 | 0:a7a43371b306 | 274 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St)) { |
ram54288 | 0:a7a43371b306 | 275 | tr_debug("M2MReportHandler::set_notification_attribute - not numerical resource"); |
ram54288 | 0:a7a43371b306 | 276 | success = false; |
ram54288 | 0:a7a43371b306 | 277 | } |
ram54288 | 0:a7a43371b306 | 278 | } |
ram54288 | 0:a7a43371b306 | 279 | return success; |
ram54288 | 0:a7a43371b306 | 280 | } |
ram54288 | 0:a7a43371b306 | 281 | |
ram54288 | 0:a7a43371b306 | 282 | void M2MReportHandler::schedule_report() |
ram54288 | 0:a7a43371b306 | 283 | { |
ram54288 | 0:a7a43371b306 | 284 | tr_debug("M2MReportHandler::schedule_report()"); |
ram54288 | 0:a7a43371b306 | 285 | _notify = true; |
ram54288 | 0:a7a43371b306 | 286 | if ((_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin || |
ram54288 | 0:a7a43371b306 | 287 | _pmin_exceeded) { |
ram54288 | 0:a7a43371b306 | 288 | report(); |
ram54288 | 0:a7a43371b306 | 289 | } |
ram54288 | 0:a7a43371b306 | 290 | } |
ram54288 | 0:a7a43371b306 | 291 | |
ram54288 | 0:a7a43371b306 | 292 | void M2MReportHandler::report() |
ram54288 | 0:a7a43371b306 | 293 | { |
ram54288 | 0:a7a43371b306 | 294 | tr_debug("M2MReportHandler::report()"); |
ram54288 | 0:a7a43371b306 | 295 | if(_current_value != _last_value && _notify) { |
ram54288 | 0:a7a43371b306 | 296 | tr_debug("M2MReportHandler::report()- send with PMIN"); |
ram54288 | 0:a7a43371b306 | 297 | _pmin_exceeded = false; |
ram54288 | 0:a7a43371b306 | 298 | _pmax_exceeded = false; |
ram54288 | 0:a7a43371b306 | 299 | _notify = false; |
ram54288 | 0:a7a43371b306 | 300 | _observer.observation_to_be_sent(_changed_instance_ids); |
ram54288 | 0:a7a43371b306 | 301 | _changed_instance_ids.clear(); |
ram54288 | 0:a7a43371b306 | 302 | _pmax_timer.stop_timer(); |
ram54288 | 0:a7a43371b306 | 303 | } |
ram54288 | 0:a7a43371b306 | 304 | else { |
ram54288 | 0:a7a43371b306 | 305 | if (_pmax_exceeded) { |
ram54288 | 0:a7a43371b306 | 306 | tr_debug("M2MReportHandler::report()- send with PMAX"); |
ram54288 | 0:a7a43371b306 | 307 | _observer.observation_to_be_sent(_changed_instance_ids, true); |
ram54288 | 0:a7a43371b306 | 308 | _changed_instance_ids.clear(); |
ram54288 | 0:a7a43371b306 | 309 | } |
ram54288 | 0:a7a43371b306 | 310 | else { |
ram54288 | 0:a7a43371b306 | 311 | tr_debug("M2MReportHandler::report()- no need to send"); |
ram54288 | 0:a7a43371b306 | 312 | } |
ram54288 | 0:a7a43371b306 | 313 | } |
ram54288 | 0:a7a43371b306 | 314 | handle_timers(); |
ram54288 | 0:a7a43371b306 | 315 | _last_value = _current_value; |
ram54288 | 0:a7a43371b306 | 316 | } |
ram54288 | 0:a7a43371b306 | 317 | |
ram54288 | 0:a7a43371b306 | 318 | void M2MReportHandler::handle_timers() |
ram54288 | 0:a7a43371b306 | 319 | { |
ram54288 | 0:a7a43371b306 | 320 | tr_debug("M2MReportHandler::handle_timers()"); |
ram54288 | 0:a7a43371b306 | 321 | uint64_t time_interval = 0; |
ram54288 | 0:a7a43371b306 | 322 | if ((_attribute_state & M2MReportHandler::Pmin) == M2MReportHandler::Pmin) { |
ram54288 | 0:a7a43371b306 | 323 | if (_pmin == _pmax) { |
ram54288 | 0:a7a43371b306 | 324 | _pmin_exceeded = true; |
ram54288 | 0:a7a43371b306 | 325 | } else { |
ram54288 | 0:a7a43371b306 | 326 | _pmin_exceeded = false; |
ram54288 | 0:a7a43371b306 | 327 | time_interval = (uint64_t) ((uint64_t)_pmin * 1000); |
ram54288 | 0:a7a43371b306 | 328 | tr_debug("M2MReportHandler::handle_timers() - Start PMIN interval: %d", (int)time_interval); |
ram54288 | 0:a7a43371b306 | 329 | _pmin_timer.start_timer(time_interval, |
ram54288 | 0:a7a43371b306 | 330 | M2MTimerObserver::PMinTimer, |
ram54288 | 0:a7a43371b306 | 331 | true); |
ram54288 | 0:a7a43371b306 | 332 | } |
ram54288 | 0:a7a43371b306 | 333 | } |
ram54288 | 0:a7a43371b306 | 334 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax) { |
ram54288 | 0:a7a43371b306 | 335 | if (_pmax > 0) { |
ram54288 | 0:a7a43371b306 | 336 | time_interval = (uint64_t) ((uint64_t)_pmax * 1000); |
ram54288 | 0:a7a43371b306 | 337 | tr_debug("M2MReportHandler::handle_timers() - Start PMAX interval: %d", (int)time_interval); |
ram54288 | 0:a7a43371b306 | 338 | _pmax_timer.start_timer(time_interval, |
ram54288 | 0:a7a43371b306 | 339 | M2MTimerObserver::PMaxTimer, |
ram54288 | 0:a7a43371b306 | 340 | true); |
ram54288 | 0:a7a43371b306 | 341 | } |
ram54288 | 0:a7a43371b306 | 342 | } |
ram54288 | 0:a7a43371b306 | 343 | } |
ram54288 | 0:a7a43371b306 | 344 | |
ram54288 | 0:a7a43371b306 | 345 | bool M2MReportHandler::check_attribute_validity() |
ram54288 | 0:a7a43371b306 | 346 | { |
ram54288 | 0:a7a43371b306 | 347 | bool success = true; |
ram54288 | 0:a7a43371b306 | 348 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax && |
ram54288 | 0:a7a43371b306 | 349 | ((_pmax >= -1.0) && (_pmin > _pmax))) { |
ram54288 | 0:a7a43371b306 | 350 | success = false; |
ram54288 | 0:a7a43371b306 | 351 | } |
ram54288 | 0:a7a43371b306 | 352 | float low = _lt + 2 * _st; |
ram54288 | 0:a7a43371b306 | 353 | if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
ram54288 | 0:a7a43371b306 | 354 | (low >= _gt)) { |
ram54288 | 0:a7a43371b306 | 355 | success = false; |
ram54288 | 0:a7a43371b306 | 356 | } |
ram54288 | 0:a7a43371b306 | 357 | return success; |
ram54288 | 0:a7a43371b306 | 358 | } |
ram54288 | 0:a7a43371b306 | 359 | |
ram54288 | 0:a7a43371b306 | 360 | void M2MReportHandler::stop_timers() |
ram54288 | 0:a7a43371b306 | 361 | { |
ram54288 | 0:a7a43371b306 | 362 | tr_debug("M2MReportHandler::stop_timers()"); |
ram54288 | 0:a7a43371b306 | 363 | |
ram54288 | 0:a7a43371b306 | 364 | _pmin_exceeded = false; |
ram54288 | 0:a7a43371b306 | 365 | _pmin_timer.stop_timer(); |
ram54288 | 0:a7a43371b306 | 366 | |
ram54288 | 0:a7a43371b306 | 367 | _pmax_exceeded = false; |
ram54288 | 0:a7a43371b306 | 368 | _pmax_timer.stop_timer(); |
ram54288 | 0:a7a43371b306 | 369 | |
ram54288 | 0:a7a43371b306 | 370 | tr_debug("M2MReportHandler::stop_timers() - out"); |
ram54288 | 0:a7a43371b306 | 371 | } |
ram54288 | 0:a7a43371b306 | 372 | |
ram54288 | 0:a7a43371b306 | 373 | void M2MReportHandler::set_default_values() |
ram54288 | 0:a7a43371b306 | 374 | { |
ram54288 | 0:a7a43371b306 | 375 | tr_debug("M2MReportHandler::set_default_values"); |
ram54288 | 0:a7a43371b306 | 376 | _pmax = -1.0; |
ram54288 | 0:a7a43371b306 | 377 | _pmin = 1.0; |
ram54288 | 0:a7a43371b306 | 378 | _gt = 0.0f; |
ram54288 | 0:a7a43371b306 | 379 | _lt = 0.0f; |
ram54288 | 0:a7a43371b306 | 380 | _st = 0.0f; |
ram54288 | 0:a7a43371b306 | 381 | _high_step = 0.0f; |
ram54288 | 0:a7a43371b306 | 382 | _low_step = 0.0f; |
ram54288 | 0:a7a43371b306 | 383 | _pmin_exceeded = false; |
ram54288 | 0:a7a43371b306 | 384 | _pmax_exceeded = false; |
ram54288 | 0:a7a43371b306 | 385 | _last_value = -1.0f; |
ram54288 | 0:a7a43371b306 | 386 | _attribute_state = 0; |
ram54288 | 0:a7a43371b306 | 387 | _changed_instance_ids.clear(); |
ram54288 | 0:a7a43371b306 | 388 | } |
ram54288 | 0:a7a43371b306 | 389 | |
ram54288 | 0:a7a43371b306 | 390 | bool M2MReportHandler::check_threshold_values() |
ram54288 | 0:a7a43371b306 | 391 | { |
ram54288 | 0:a7a43371b306 | 392 | tr_debug("M2MReportHandler::check_threshold_values"); |
ram54288 | 0:a7a43371b306 | 393 | tr_debug("Current value: %f", _current_value); |
ram54288 | 0:a7a43371b306 | 394 | tr_debug("High step: %f", _high_step); |
ram54288 | 0:a7a43371b306 | 395 | tr_debug("Low step: %f", _low_step); |
ram54288 | 0:a7a43371b306 | 396 | tr_debug("Less than: %f", _lt); |
ram54288 | 0:a7a43371b306 | 397 | tr_debug("Greater than: %f", _gt); |
ram54288 | 0:a7a43371b306 | 398 | tr_debug("Step: %f", _st); |
ram54288 | 0:a7a43371b306 | 399 | bool can_send = false; |
ram54288 | 0:a7a43371b306 | 400 | // Check step condition |
ram54288 | 0:a7a43371b306 | 401 | if ((_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
ram54288 | 0:a7a43371b306 | 402 | if ((_current_value >= _high_step || |
ram54288 | 0:a7a43371b306 | 403 | _current_value <= _low_step)) { |
ram54288 | 0:a7a43371b306 | 404 | can_send = true; |
ram54288 | 0:a7a43371b306 | 405 | } |
ram54288 | 0:a7a43371b306 | 406 | else { |
ram54288 | 0:a7a43371b306 | 407 | if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
ram54288 | 0:a7a43371b306 | 408 | (_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt ) { |
ram54288 | 0:a7a43371b306 | 409 | can_send = check_gt_lt_params(); |
ram54288 | 0:a7a43371b306 | 410 | } |
ram54288 | 0:a7a43371b306 | 411 | else { |
ram54288 | 0:a7a43371b306 | 412 | can_send = false; |
ram54288 | 0:a7a43371b306 | 413 | } |
ram54288 | 0:a7a43371b306 | 414 | } |
ram54288 | 0:a7a43371b306 | 415 | } |
ram54288 | 0:a7a43371b306 | 416 | else { |
ram54288 | 0:a7a43371b306 | 417 | can_send = check_gt_lt_params(); |
ram54288 | 0:a7a43371b306 | 418 | } |
ram54288 | 0:a7a43371b306 | 419 | tr_debug("M2MReportHandler::check_threshold_values - value in range = %d", (int)can_send); |
ram54288 | 0:a7a43371b306 | 420 | return can_send; |
ram54288 | 0:a7a43371b306 | 421 | } |
ram54288 | 0:a7a43371b306 | 422 | |
ram54288 | 0:a7a43371b306 | 423 | bool M2MReportHandler::check_gt_lt_params() |
ram54288 | 0:a7a43371b306 | 424 | { |
ram54288 | 0:a7a43371b306 | 425 | tr_debug("M2MReportHandler::check_gt_lt_params"); |
ram54288 | 0:a7a43371b306 | 426 | bool can_send = false; |
ram54288 | 0:a7a43371b306 | 427 | // GT & LT set. |
ram54288 | 0:a7a43371b306 | 428 | if ((_attribute_state & (M2MReportHandler::Lt | M2MReportHandler::Gt)) |
ram54288 | 0:a7a43371b306 | 429 | == (M2MReportHandler::Lt | M2MReportHandler::Gt)) { |
ram54288 | 0:a7a43371b306 | 430 | if (_current_value > _gt || _current_value < _lt) { |
ram54288 | 0:a7a43371b306 | 431 | can_send = true; |
ram54288 | 0:a7a43371b306 | 432 | } |
ram54288 | 0:a7a43371b306 | 433 | else { |
ram54288 | 0:a7a43371b306 | 434 | can_send = false; |
ram54288 | 0:a7a43371b306 | 435 | } |
ram54288 | 0:a7a43371b306 | 436 | } |
ram54288 | 0:a7a43371b306 | 437 | // Only LT |
ram54288 | 0:a7a43371b306 | 438 | else if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt && |
ram54288 | 0:a7a43371b306 | 439 | (_attribute_state & M2MReportHandler::Gt) == 0 ) { |
ram54288 | 0:a7a43371b306 | 440 | if (_current_value < _lt) { |
ram54288 | 0:a7a43371b306 | 441 | can_send = true; |
ram54288 | 0:a7a43371b306 | 442 | } |
ram54288 | 0:a7a43371b306 | 443 | else { |
ram54288 | 0:a7a43371b306 | 444 | can_send = false; |
ram54288 | 0:a7a43371b306 | 445 | } |
ram54288 | 0:a7a43371b306 | 446 | } |
ram54288 | 0:a7a43371b306 | 447 | // Only GT |
ram54288 | 0:a7a43371b306 | 448 | else if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
ram54288 | 0:a7a43371b306 | 449 | (_attribute_state & M2MReportHandler::Lt) == 0 ) { |
ram54288 | 0:a7a43371b306 | 450 | if (_current_value > _gt) { |
ram54288 | 0:a7a43371b306 | 451 | can_send = true; |
ram54288 | 0:a7a43371b306 | 452 | } |
ram54288 | 0:a7a43371b306 | 453 | else { |
ram54288 | 0:a7a43371b306 | 454 | can_send = false; |
ram54288 | 0:a7a43371b306 | 455 | } |
ram54288 | 0:a7a43371b306 | 456 | } |
ram54288 | 0:a7a43371b306 | 457 | // GT & LT not set. |
ram54288 | 0:a7a43371b306 | 458 | else { |
ram54288 | 0:a7a43371b306 | 459 | can_send = true; |
ram54288 | 0:a7a43371b306 | 460 | } |
ram54288 | 0:a7a43371b306 | 461 | tr_debug("M2MReportHandler::check_gt_lt_params - value in range = %d", (int)can_send); |
ram54288 | 0:a7a43371b306 | 462 | return can_send; |
ram54288 | 0:a7a43371b306 | 463 | } |
ram54288 | 0:a7a43371b306 | 464 | |
ram54288 | 0:a7a43371b306 | 465 | uint8_t M2MReportHandler::attribute_flags() |
ram54288 | 0:a7a43371b306 | 466 | { |
ram54288 | 0:a7a43371b306 | 467 | return _attribute_state; |
ram54288 | 0:a7a43371b306 | 468 | } |