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