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