leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mreporthandler.cpp@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:25fa8795676b | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:25fa8795676b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:25fa8795676b | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:25fa8795676b | 6 | * You may obtain a copy of the License at |
leothedragon | 0:25fa8795676b | 7 | * |
leothedragon | 0:25fa8795676b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:25fa8795676b | 9 | * |
leothedragon | 0:25fa8795676b | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:25fa8795676b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:25fa8795676b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:25fa8795676b | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:25fa8795676b | 14 | * limitations under the License. |
leothedragon | 0:25fa8795676b | 15 | */ |
leothedragon | 0:25fa8795676b | 16 | // Needed for PRIu64 on FreeRTOS |
leothedragon | 0:25fa8795676b | 17 | #include <stdio.h> |
leothedragon | 0:25fa8795676b | 18 | // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX |
leothedragon | 0:25fa8795676b | 19 | #ifndef __STDC_LIMIT_MACROS |
leothedragon | 0:25fa8795676b | 20 | #define __STDC_LIMIT_MACROS |
leothedragon | 0:25fa8795676b | 21 | #endif |
leothedragon | 0:25fa8795676b | 22 | |
leothedragon | 0:25fa8795676b | 23 | // Note: this macro is needed on armcc to get the the PRI*32 macros |
leothedragon | 0:25fa8795676b | 24 | // from inttypes.h in a C++ code. |
leothedragon | 0:25fa8795676b | 25 | #ifndef __STDC_FORMAT_MACROS |
leothedragon | 0:25fa8795676b | 26 | #define __STDC_FORMAT_MACROS |
leothedragon | 0:25fa8795676b | 27 | #endif |
leothedragon | 0:25fa8795676b | 28 | |
leothedragon | 0:25fa8795676b | 29 | #include "mbed-client/m2mreportobserver.h" |
leothedragon | 0:25fa8795676b | 30 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:25fa8795676b | 31 | #include "mbed-client/m2mtimer.h" |
leothedragon | 0:25fa8795676b | 32 | #include "include/m2mreporthandler.h" |
leothedragon | 0:25fa8795676b | 33 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:25fa8795676b | 34 | #include <string.h> |
leothedragon | 0:25fa8795676b | 35 | #include <stdlib.h> |
leothedragon | 0:25fa8795676b | 36 | |
leothedragon | 0:25fa8795676b | 37 | #define TRACE_GROUP "mClt" |
leothedragon | 0:25fa8795676b | 38 | |
leothedragon | 0:25fa8795676b | 39 | M2MReportHandler::M2MReportHandler(M2MReportObserver &observer, M2MBase::DataType type) |
leothedragon | 0:25fa8795676b | 40 | : _observer(observer), |
leothedragon | 0:25fa8795676b | 41 | _is_under_observation(false), |
leothedragon | 0:25fa8795676b | 42 | _observation_level(M2MBase::None), |
leothedragon | 0:25fa8795676b | 43 | _attribute_state(0), |
leothedragon | 0:25fa8795676b | 44 | _token_length(0), |
leothedragon | 0:25fa8795676b | 45 | _resource_type(type), |
leothedragon | 0:25fa8795676b | 46 | _notify(false), |
leothedragon | 0:25fa8795676b | 47 | _pmin_exceeded(false), |
leothedragon | 0:25fa8795676b | 48 | _pmax_exceeded(false), |
leothedragon | 0:25fa8795676b | 49 | _observation_number(0), |
leothedragon | 0:25fa8795676b | 50 | _pmin_timer(*this), |
leothedragon | 0:25fa8795676b | 51 | _pmax_timer(*this), |
leothedragon | 0:25fa8795676b | 52 | _token(NULL), |
leothedragon | 0:25fa8795676b | 53 | _pmax(-1.0f), |
leothedragon | 0:25fa8795676b | 54 | _pmin(1.0f), |
leothedragon | 0:25fa8795676b | 55 | _gt(0.0f), |
leothedragon | 0:25fa8795676b | 56 | _lt(0.0f), |
leothedragon | 0:25fa8795676b | 57 | _st(0.0f), |
leothedragon | 0:25fa8795676b | 58 | _notification_send_in_progress(false), |
leothedragon | 0:25fa8795676b | 59 | _notification_in_queue(false), |
leothedragon | 0:25fa8795676b | 60 | _blockwise_notify(false), |
leothedragon | 0:25fa8795676b | 61 | _pmin_quiet_period(false) |
leothedragon | 0:25fa8795676b | 62 | { |
leothedragon | 0:25fa8795676b | 63 | tr_debug("M2MReportHandler::M2MReportHandler()"); |
leothedragon | 0:25fa8795676b | 64 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 65 | _high_step.float_value = 0; |
leothedragon | 0:25fa8795676b | 66 | _low_step.float_value = 0; |
leothedragon | 0:25fa8795676b | 67 | _last_value.float_value = -1; |
leothedragon | 0:25fa8795676b | 68 | _current_value.float_value = 0; |
leothedragon | 0:25fa8795676b | 69 | } else { |
leothedragon | 0:25fa8795676b | 70 | _high_step.int_value = 0; |
leothedragon | 0:25fa8795676b | 71 | _low_step.int_value = 0; |
leothedragon | 0:25fa8795676b | 72 | _last_value.int_value = -1; |
leothedragon | 0:25fa8795676b | 73 | _current_value.int_value = 0; |
leothedragon | 0:25fa8795676b | 74 | } |
leothedragon | 0:25fa8795676b | 75 | } |
leothedragon | 0:25fa8795676b | 76 | |
leothedragon | 0:25fa8795676b | 77 | M2MReportHandler::~M2MReportHandler() |
leothedragon | 0:25fa8795676b | 78 | { |
leothedragon | 0:25fa8795676b | 79 | tr_debug("M2MReportHandler::~M2MReportHandler()"); |
leothedragon | 0:25fa8795676b | 80 | free(_token); |
leothedragon | 0:25fa8795676b | 81 | } |
leothedragon | 0:25fa8795676b | 82 | |
leothedragon | 0:25fa8795676b | 83 | void M2MReportHandler::set_under_observation(bool observed) |
leothedragon | 0:25fa8795676b | 84 | { |
leothedragon | 0:25fa8795676b | 85 | tr_debug("M2MReportHandler::set_under_observation(observed %d)", (int)observed); |
leothedragon | 0:25fa8795676b | 86 | |
leothedragon | 0:25fa8795676b | 87 | _is_under_observation = observed; |
leothedragon | 0:25fa8795676b | 88 | |
leothedragon | 0:25fa8795676b | 89 | stop_timers(); |
leothedragon | 0:25fa8795676b | 90 | if (observed) { |
leothedragon | 0:25fa8795676b | 91 | handle_timers(); |
leothedragon | 0:25fa8795676b | 92 | } |
leothedragon | 0:25fa8795676b | 93 | else { |
leothedragon | 0:25fa8795676b | 94 | set_default_values(); |
leothedragon | 0:25fa8795676b | 95 | } |
leothedragon | 0:25fa8795676b | 96 | } |
leothedragon | 0:25fa8795676b | 97 | |
leothedragon | 0:25fa8795676b | 98 | void M2MReportHandler::set_value_float(float value) |
leothedragon | 0:25fa8795676b | 99 | { |
leothedragon | 0:25fa8795676b | 100 | tr_debug("M2MReportHandler::set_value_float() - current %f, last %f", value, _last_value.float_value); |
leothedragon | 0:25fa8795676b | 101 | _current_value.float_value = value; |
leothedragon | 0:25fa8795676b | 102 | |
leothedragon | 0:25fa8795676b | 103 | if (_current_value.float_value != _last_value.float_value) { |
leothedragon | 0:25fa8795676b | 104 | send_value(); |
leothedragon | 0:25fa8795676b | 105 | _high_step.float_value = _last_value.float_value + _st; |
leothedragon | 0:25fa8795676b | 106 | _low_step.float_value = _last_value.float_value - _st; |
leothedragon | 0:25fa8795676b | 107 | } |
leothedragon | 0:25fa8795676b | 108 | } |
leothedragon | 0:25fa8795676b | 109 | |
leothedragon | 0:25fa8795676b | 110 | void M2MReportHandler::set_value_int(int64_t value) |
leothedragon | 0:25fa8795676b | 111 | { |
leothedragon | 0:25fa8795676b | 112 | tr_debug("M2MReportHandler::set_value_int() - current %" PRId64 ", last % " PRId64, value, _last_value.int_value); |
leothedragon | 0:25fa8795676b | 113 | _current_value.int_value = value; |
leothedragon | 0:25fa8795676b | 114 | |
leothedragon | 0:25fa8795676b | 115 | if (_current_value.int_value != _last_value.int_value) { |
leothedragon | 0:25fa8795676b | 116 | send_value(); |
leothedragon | 0:25fa8795676b | 117 | _high_step.int_value = _last_value.int_value + _st; |
leothedragon | 0:25fa8795676b | 118 | _low_step.int_value = _last_value.int_value - _st; |
leothedragon | 0:25fa8795676b | 119 | } |
leothedragon | 0:25fa8795676b | 120 | } |
leothedragon | 0:25fa8795676b | 121 | |
leothedragon | 0:25fa8795676b | 122 | void M2MReportHandler::set_notification_trigger(uint16_t obj_instance_id) |
leothedragon | 0:25fa8795676b | 123 | { |
leothedragon | 0:25fa8795676b | 124 | tr_debug("M2MReportHandler::set_notification_trigger(): %d", obj_instance_id); |
leothedragon | 0:25fa8795676b | 125 | // Add to array if not there yet |
leothedragon | 0:25fa8795676b | 126 | m2m::Vector<uint16_t>::const_iterator it; |
leothedragon | 0:25fa8795676b | 127 | it = _changed_instance_ids.begin(); |
leothedragon | 0:25fa8795676b | 128 | bool found = false; |
leothedragon | 0:25fa8795676b | 129 | for ( ; it != _changed_instance_ids.end(); it++) { |
leothedragon | 0:25fa8795676b | 130 | if ((*it) == obj_instance_id) { |
leothedragon | 0:25fa8795676b | 131 | found = true; |
leothedragon | 0:25fa8795676b | 132 | break; |
leothedragon | 0:25fa8795676b | 133 | } |
leothedragon | 0:25fa8795676b | 134 | } |
leothedragon | 0:25fa8795676b | 135 | if (!found) { |
leothedragon | 0:25fa8795676b | 136 | _changed_instance_ids.push_back(obj_instance_id); |
leothedragon | 0:25fa8795676b | 137 | } |
leothedragon | 0:25fa8795676b | 138 | |
leothedragon | 0:25fa8795676b | 139 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 140 | _current_value.float_value = 0; |
leothedragon | 0:25fa8795676b | 141 | _last_value.float_value = 1; |
leothedragon | 0:25fa8795676b | 142 | } else { |
leothedragon | 0:25fa8795676b | 143 | _current_value.int_value = 0; |
leothedragon | 0:25fa8795676b | 144 | _last_value.int_value = 1; |
leothedragon | 0:25fa8795676b | 145 | } |
leothedragon | 0:25fa8795676b | 146 | set_notification_in_queue(true); |
leothedragon | 0:25fa8795676b | 147 | schedule_report(); |
leothedragon | 0:25fa8795676b | 148 | } |
leothedragon | 0:25fa8795676b | 149 | |
leothedragon | 0:25fa8795676b | 150 | bool M2MReportHandler::parse_notification_attribute(const char *query, |
leothedragon | 0:25fa8795676b | 151 | M2MBase::BaseType type, |
leothedragon | 0:25fa8795676b | 152 | M2MResourceInstance::ResourceType resource_type) |
leothedragon | 0:25fa8795676b | 153 | { |
leothedragon | 0:25fa8795676b | 154 | tr_debug("M2MReportHandler::parse_notification_attribute(Query %s, Base type %d)", query, (int)type); |
leothedragon | 0:25fa8795676b | 155 | bool success = false; |
leothedragon | 0:25fa8795676b | 156 | const char* sep_pos = strchr(query, '&'); |
leothedragon | 0:25fa8795676b | 157 | const char* rest = query; |
leothedragon | 0:25fa8795676b | 158 | if( sep_pos != NULL ){ |
leothedragon | 0:25fa8795676b | 159 | char query_options[5][20]; |
leothedragon | 0:25fa8795676b | 160 | float pmin = _pmin; |
leothedragon | 0:25fa8795676b | 161 | float pmax = _pmax; |
leothedragon | 0:25fa8795676b | 162 | float lt = _lt; |
leothedragon | 0:25fa8795676b | 163 | float gt = _gt; |
leothedragon | 0:25fa8795676b | 164 | float st = _st; |
leothedragon | 0:25fa8795676b | 165 | high_step_t high = _high_step; |
leothedragon | 0:25fa8795676b | 166 | low_step_t low = _low_step; |
leothedragon | 0:25fa8795676b | 167 | uint8_t attr = _attribute_state; |
leothedragon | 0:25fa8795676b | 168 | |
leothedragon | 0:25fa8795676b | 169 | memset(query_options, 0, sizeof(query_options[0][0]) * 5 * 20); |
leothedragon | 0:25fa8795676b | 170 | uint8_t num_options = 0; |
leothedragon | 0:25fa8795676b | 171 | while( sep_pos != NULL && num_options < 5){ |
leothedragon | 0:25fa8795676b | 172 | size_t len = (size_t)(sep_pos-rest); |
leothedragon | 0:25fa8795676b | 173 | if( len > 19 ){ |
leothedragon | 0:25fa8795676b | 174 | len = 19; |
leothedragon | 0:25fa8795676b | 175 | } |
leothedragon | 0:25fa8795676b | 176 | memcpy(query_options[num_options], rest, len); |
leothedragon | 0:25fa8795676b | 177 | sep_pos++; |
leothedragon | 0:25fa8795676b | 178 | rest = sep_pos; |
leothedragon | 0:25fa8795676b | 179 | sep_pos = strchr(rest, '&'); |
leothedragon | 0:25fa8795676b | 180 | num_options++; |
leothedragon | 0:25fa8795676b | 181 | } |
leothedragon | 0:25fa8795676b | 182 | if( num_options < 5 && strlen(rest) > 0){ |
leothedragon | 0:25fa8795676b | 183 | size_t len = (size_t)strlen(rest); |
leothedragon | 0:25fa8795676b | 184 | if( len > 19 ){ |
leothedragon | 0:25fa8795676b | 185 | len = 19; |
leothedragon | 0:25fa8795676b | 186 | } |
leothedragon | 0:25fa8795676b | 187 | memcpy(query_options[num_options++], rest, len); |
leothedragon | 0:25fa8795676b | 188 | } |
leothedragon | 0:25fa8795676b | 189 | |
leothedragon | 0:25fa8795676b | 190 | for (int option = 0; option < num_options; option++) { |
leothedragon | 0:25fa8795676b | 191 | success = set_notification_attribute(query_options[option],type, resource_type); |
leothedragon | 0:25fa8795676b | 192 | if (!success) { |
leothedragon | 0:25fa8795676b | 193 | tr_error("M2MReportHandler::parse_notification_attribute - break"); |
leothedragon | 0:25fa8795676b | 194 | break; |
leothedragon | 0:25fa8795676b | 195 | } |
leothedragon | 0:25fa8795676b | 196 | } |
leothedragon | 0:25fa8795676b | 197 | |
leothedragon | 0:25fa8795676b | 198 | if(success) { |
leothedragon | 0:25fa8795676b | 199 | success = check_attribute_validity(); |
leothedragon | 0:25fa8795676b | 200 | } |
leothedragon | 0:25fa8795676b | 201 | else { |
leothedragon | 0:25fa8795676b | 202 | tr_debug("M2MReportHandler::parse_notification_attribute - not valid query"); |
leothedragon | 0:25fa8795676b | 203 | _pmin = pmin; |
leothedragon | 0:25fa8795676b | 204 | _pmax = pmax; |
leothedragon | 0:25fa8795676b | 205 | _st = st; |
leothedragon | 0:25fa8795676b | 206 | _lt = lt; |
leothedragon | 0:25fa8795676b | 207 | _gt = gt; |
leothedragon | 0:25fa8795676b | 208 | _high_step = high; |
leothedragon | 0:25fa8795676b | 209 | _low_step = low; |
leothedragon | 0:25fa8795676b | 210 | _attribute_state = attr; |
leothedragon | 0:25fa8795676b | 211 | } |
leothedragon | 0:25fa8795676b | 212 | } |
leothedragon | 0:25fa8795676b | 213 | else { |
leothedragon | 0:25fa8795676b | 214 | if(set_notification_attribute(query, type, resource_type)) { |
leothedragon | 0:25fa8795676b | 215 | success = check_attribute_validity(); |
leothedragon | 0:25fa8795676b | 216 | } |
leothedragon | 0:25fa8795676b | 217 | } |
leothedragon | 0:25fa8795676b | 218 | |
leothedragon | 0:25fa8795676b | 219 | return success; |
leothedragon | 0:25fa8795676b | 220 | } |
leothedragon | 0:25fa8795676b | 221 | |
leothedragon | 0:25fa8795676b | 222 | void M2MReportHandler::timer_expired(M2MTimerObserver::Type type) |
leothedragon | 0:25fa8795676b | 223 | { |
leothedragon | 0:25fa8795676b | 224 | switch(type) { |
leothedragon | 0:25fa8795676b | 225 | case M2MTimerObserver::PMinTimer: { |
leothedragon | 0:25fa8795676b | 226 | tr_debug("M2MReportHandler::timer_expired - PMIN"); |
leothedragon | 0:25fa8795676b | 227 | |
leothedragon | 0:25fa8795676b | 228 | _pmin_exceeded = true; |
leothedragon | 0:25fa8795676b | 229 | if (_notify || |
leothedragon | 0:25fa8795676b | 230 | (_pmin > 0 && (_attribute_state & M2MReportHandler::Pmax) != M2MReportHandler::Pmax)){ |
leothedragon | 0:25fa8795676b | 231 | report(); |
leothedragon | 0:25fa8795676b | 232 | } |
leothedragon | 0:25fa8795676b | 233 | |
leothedragon | 0:25fa8795676b | 234 | // If value hasn't changed since last expiration, next value change should send notification immediately |
leothedragon | 0:25fa8795676b | 235 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 236 | if (_current_value.float_value == _last_value.float_value) { |
leothedragon | 0:25fa8795676b | 237 | _pmin_quiet_period = true; |
leothedragon | 0:25fa8795676b | 238 | } |
leothedragon | 0:25fa8795676b | 239 | } else { |
leothedragon | 0:25fa8795676b | 240 | if (_current_value.int_value == _last_value.int_value) { |
leothedragon | 0:25fa8795676b | 241 | _pmin_quiet_period = true; |
leothedragon | 0:25fa8795676b | 242 | } |
leothedragon | 0:25fa8795676b | 243 | } |
leothedragon | 0:25fa8795676b | 244 | } |
leothedragon | 0:25fa8795676b | 245 | break; |
leothedragon | 0:25fa8795676b | 246 | case M2MTimerObserver::PMaxTimer: { |
leothedragon | 0:25fa8795676b | 247 | tr_debug("M2MReportHandler::timer_expired - PMAX"); |
leothedragon | 0:25fa8795676b | 248 | _pmax_exceeded = true; |
leothedragon | 0:25fa8795676b | 249 | if (_pmin_exceeded || |
leothedragon | 0:25fa8795676b | 250 | (_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin ) { |
leothedragon | 0:25fa8795676b | 251 | report(); |
leothedragon | 0:25fa8795676b | 252 | } |
leothedragon | 0:25fa8795676b | 253 | } |
leothedragon | 0:25fa8795676b | 254 | break; |
leothedragon | 0:25fa8795676b | 255 | default: |
leothedragon | 0:25fa8795676b | 256 | break; |
leothedragon | 0:25fa8795676b | 257 | } |
leothedragon | 0:25fa8795676b | 258 | } |
leothedragon | 0:25fa8795676b | 259 | |
leothedragon | 0:25fa8795676b | 260 | bool M2MReportHandler::set_notification_attribute(const char* option, |
leothedragon | 0:25fa8795676b | 261 | M2MBase::BaseType type, |
leothedragon | 0:25fa8795676b | 262 | M2MResourceInstance::ResourceType resource_type) |
leothedragon | 0:25fa8795676b | 263 | { |
leothedragon | 0:25fa8795676b | 264 | tr_debug("M2MReportHandler::set_notification_attribute()"); |
leothedragon | 0:25fa8795676b | 265 | bool success = false; |
leothedragon | 0:25fa8795676b | 266 | const int max_size = 20; |
leothedragon | 0:25fa8795676b | 267 | char attribute[max_size]; |
leothedragon | 0:25fa8795676b | 268 | char value[max_size]; |
leothedragon | 0:25fa8795676b | 269 | |
leothedragon | 0:25fa8795676b | 270 | const char* pos = strstr(option, EQUAL); |
leothedragon | 0:25fa8795676b | 271 | if (pos) { |
leothedragon | 0:25fa8795676b | 272 | size_t attr_len = pos - option; |
leothedragon | 0:25fa8795676b | 273 | // Skip the "=" mark |
leothedragon | 0:25fa8795676b | 274 | pos++; |
leothedragon | 0:25fa8795676b | 275 | size_t value_len = strlen(pos); |
leothedragon | 0:25fa8795676b | 276 | if (value_len && value_len < max_size && attr_len < max_size) { |
leothedragon | 0:25fa8795676b | 277 | memcpy(attribute, option, attr_len); |
leothedragon | 0:25fa8795676b | 278 | attribute[attr_len] = '\0'; |
leothedragon | 0:25fa8795676b | 279 | memcpy(value, pos, value_len); |
leothedragon | 0:25fa8795676b | 280 | value[value_len] = '\0'; |
leothedragon | 0:25fa8795676b | 281 | success = true; |
leothedragon | 0:25fa8795676b | 282 | } |
leothedragon | 0:25fa8795676b | 283 | } |
leothedragon | 0:25fa8795676b | 284 | |
leothedragon | 0:25fa8795676b | 285 | if (success) { |
leothedragon | 0:25fa8795676b | 286 | if (strcmp(attribute, PMIN) == 0) { |
leothedragon | 0:25fa8795676b | 287 | _pmin = atoi(value); |
leothedragon | 0:25fa8795676b | 288 | success = true; |
leothedragon | 0:25fa8795676b | 289 | _attribute_state |= M2MReportHandler::Pmin; |
leothedragon | 0:25fa8795676b | 290 | tr_info("M2MReportHandler::set_notification_attribute %s to %" PRId32, attribute, _pmin); |
leothedragon | 0:25fa8795676b | 291 | } |
leothedragon | 0:25fa8795676b | 292 | else if(strcmp(attribute, PMAX) == 0) { |
leothedragon | 0:25fa8795676b | 293 | _pmax = atoi(value); |
leothedragon | 0:25fa8795676b | 294 | success = true; |
leothedragon | 0:25fa8795676b | 295 | _attribute_state |= M2MReportHandler::Pmax; |
leothedragon | 0:25fa8795676b | 296 | tr_info("M2MReportHandler::set_notification_attribute %s to %" PRId32, attribute, _pmax); |
leothedragon | 0:25fa8795676b | 297 | } |
leothedragon | 0:25fa8795676b | 298 | else if(strcmp(attribute, GT) == 0 && |
leothedragon | 0:25fa8795676b | 299 | (M2MBase::Resource == type)){ |
leothedragon | 0:25fa8795676b | 300 | success = true; |
leothedragon | 0:25fa8795676b | 301 | _gt = atof(value); |
leothedragon | 0:25fa8795676b | 302 | _attribute_state |= M2MReportHandler::Gt; |
leothedragon | 0:25fa8795676b | 303 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _gt); |
leothedragon | 0:25fa8795676b | 304 | } |
leothedragon | 0:25fa8795676b | 305 | else if(strcmp(attribute, LT) == 0 && |
leothedragon | 0:25fa8795676b | 306 | (M2MBase::Resource == type)){ |
leothedragon | 0:25fa8795676b | 307 | success = true; |
leothedragon | 0:25fa8795676b | 308 | _lt = atof(value); |
leothedragon | 0:25fa8795676b | 309 | _attribute_state |= M2MReportHandler::Lt; |
leothedragon | 0:25fa8795676b | 310 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _lt); |
leothedragon | 0:25fa8795676b | 311 | } |
leothedragon | 0:25fa8795676b | 312 | else if((strcmp(attribute, ST_SIZE) == 0 || (strcmp(attribute, STP) == 0)) |
leothedragon | 0:25fa8795676b | 313 | && (M2MBase::Resource == type)){ |
leothedragon | 0:25fa8795676b | 314 | success = true; |
leothedragon | 0:25fa8795676b | 315 | _st = atof(value); |
leothedragon | 0:25fa8795676b | 316 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 317 | _high_step.float_value = _current_value.float_value + _st; |
leothedragon | 0:25fa8795676b | 318 | _low_step.float_value = _current_value.float_value - _st; |
leothedragon | 0:25fa8795676b | 319 | } else { |
leothedragon | 0:25fa8795676b | 320 | _high_step.int_value = _current_value.int_value + _st; |
leothedragon | 0:25fa8795676b | 321 | _low_step.int_value = _current_value.int_value - _st; |
leothedragon | 0:25fa8795676b | 322 | } |
leothedragon | 0:25fa8795676b | 323 | |
leothedragon | 0:25fa8795676b | 324 | _attribute_state |= M2MReportHandler::St; |
leothedragon | 0:25fa8795676b | 325 | tr_info("M2MReportHandler::set_notification_attribute %s to %f", attribute, _st); |
leothedragon | 0:25fa8795676b | 326 | } else { |
leothedragon | 0:25fa8795676b | 327 | tr_error("M2MReportHandler::set_notification_attribute - unknown write attribute!"); |
leothedragon | 0:25fa8795676b | 328 | success = false; |
leothedragon | 0:25fa8795676b | 329 | } |
leothedragon | 0:25fa8795676b | 330 | |
leothedragon | 0:25fa8795676b | 331 | // Return false if try to set gt,lt or st when the resource type is something else than numerical |
leothedragon | 0:25fa8795676b | 332 | if (success && |
leothedragon | 0:25fa8795676b | 333 | (resource_type != M2MResourceInstance::INTEGER && resource_type != M2MResourceInstance::FLOAT) && |
leothedragon | 0:25fa8795676b | 334 | ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
leothedragon | 0:25fa8795676b | 335 | (_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
leothedragon | 0:25fa8795676b | 336 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St)) { |
leothedragon | 0:25fa8795676b | 337 | tr_debug("M2MReportHandler::set_notification_attribute - not numerical resource"); |
leothedragon | 0:25fa8795676b | 338 | success = false; |
leothedragon | 0:25fa8795676b | 339 | } |
leothedragon | 0:25fa8795676b | 340 | } else { |
leothedragon | 0:25fa8795676b | 341 | tr_error("M2MReportHandler::set_notification_attribute - failed to parse query!"); |
leothedragon | 0:25fa8795676b | 342 | } |
leothedragon | 0:25fa8795676b | 343 | return success; |
leothedragon | 0:25fa8795676b | 344 | } |
leothedragon | 0:25fa8795676b | 345 | |
leothedragon | 0:25fa8795676b | 346 | void M2MReportHandler::schedule_report(bool in_queue) |
leothedragon | 0:25fa8795676b | 347 | { |
leothedragon | 0:25fa8795676b | 348 | tr_debug("M2MReportHandler::schedule_report()"); |
leothedragon | 0:25fa8795676b | 349 | _notify = true; |
leothedragon | 0:25fa8795676b | 350 | |
leothedragon | 0:25fa8795676b | 351 | if ((_attribute_state & M2MReportHandler::Pmin) != M2MReportHandler::Pmin || |
leothedragon | 0:25fa8795676b | 352 | _pmin_exceeded || |
leothedragon | 0:25fa8795676b | 353 | _pmin_quiet_period) { |
leothedragon | 0:25fa8795676b | 354 | report(in_queue); |
leothedragon | 0:25fa8795676b | 355 | } |
leothedragon | 0:25fa8795676b | 356 | } |
leothedragon | 0:25fa8795676b | 357 | |
leothedragon | 0:25fa8795676b | 358 | void M2MReportHandler::report(bool in_queue) |
leothedragon | 0:25fa8795676b | 359 | { |
leothedragon | 0:25fa8795676b | 360 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 361 | tr_debug("M2MReportHandler::report() - current %2f, last %2f, notify %d, queued %d", _current_value.float_value, _last_value.float_value, _notify, in_queue); |
leothedragon | 0:25fa8795676b | 362 | } else { |
leothedragon | 0:25fa8795676b | 363 | tr_debug("M2MReportHandler::report() - current %" PRId64 ", last % " PRId64 ", notify %d, queued %d", _current_value.int_value, _last_value.int_value, _notify, in_queue); |
leothedragon | 0:25fa8795676b | 364 | } |
leothedragon | 0:25fa8795676b | 365 | |
leothedragon | 0:25fa8795676b | 366 | bool value_changed = false; |
leothedragon | 0:25fa8795676b | 367 | |
leothedragon | 0:25fa8795676b | 368 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 369 | if (_current_value.float_value != _last_value.float_value) { |
leothedragon | 0:25fa8795676b | 370 | value_changed = true; |
leothedragon | 0:25fa8795676b | 371 | } |
leothedragon | 0:25fa8795676b | 372 | } else { |
leothedragon | 0:25fa8795676b | 373 | if (_current_value.int_value != _last_value.int_value) { |
leothedragon | 0:25fa8795676b | 374 | value_changed = true; |
leothedragon | 0:25fa8795676b | 375 | } |
leothedragon | 0:25fa8795676b | 376 | } |
leothedragon | 0:25fa8795676b | 377 | |
leothedragon | 0:25fa8795676b | 378 | if((value_changed && _notify) || in_queue) { |
leothedragon | 0:25fa8795676b | 379 | if (_pmin_exceeded) { |
leothedragon | 0:25fa8795676b | 380 | tr_debug("M2MReportHandler::report()- send with PMIN expiration"); |
leothedragon | 0:25fa8795676b | 381 | } else { |
leothedragon | 0:25fa8795676b | 382 | tr_debug("M2MReportHandler::report()- send with VALUE change"); |
leothedragon | 0:25fa8795676b | 383 | } |
leothedragon | 0:25fa8795676b | 384 | |
leothedragon | 0:25fa8795676b | 385 | _pmin_exceeded = false; |
leothedragon | 0:25fa8795676b | 386 | _pmax_exceeded = false; |
leothedragon | 0:25fa8795676b | 387 | _notify = false; |
leothedragon | 0:25fa8795676b | 388 | _pmin_quiet_period = false; |
leothedragon | 0:25fa8795676b | 389 | _observation_number++; |
leothedragon | 0:25fa8795676b | 390 | |
leothedragon | 0:25fa8795676b | 391 | if (_observation_number == 1) { |
leothedragon | 0:25fa8795676b | 392 | // Increment the observation number by 1 if it is already 1 because CoAP specification has reserved 1 for DEREGISTER notification |
leothedragon | 0:25fa8795676b | 393 | _observation_number++; |
leothedragon | 0:25fa8795676b | 394 | } |
leothedragon | 0:25fa8795676b | 395 | |
leothedragon | 0:25fa8795676b | 396 | if (_observer.observation_to_be_sent(_changed_instance_ids, observation_number())) { |
leothedragon | 0:25fa8795676b | 397 | _changed_instance_ids.clear(); |
leothedragon | 0:25fa8795676b | 398 | set_notification_send_in_progress(true); |
leothedragon | 0:25fa8795676b | 399 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 400 | _last_value.float_value = _current_value.float_value; |
leothedragon | 0:25fa8795676b | 401 | } else { |
leothedragon | 0:25fa8795676b | 402 | _last_value.int_value = _current_value.int_value; |
leothedragon | 0:25fa8795676b | 403 | } |
leothedragon | 0:25fa8795676b | 404 | } |
leothedragon | 0:25fa8795676b | 405 | |
leothedragon | 0:25fa8795676b | 406 | _pmax_timer.stop_timer(); |
leothedragon | 0:25fa8795676b | 407 | } |
leothedragon | 0:25fa8795676b | 408 | else { |
leothedragon | 0:25fa8795676b | 409 | if (_pmax_exceeded) { |
leothedragon | 0:25fa8795676b | 410 | tr_debug("M2MReportHandler::report()- send with PMAX expiration"); |
leothedragon | 0:25fa8795676b | 411 | _observation_number++; |
leothedragon | 0:25fa8795676b | 412 | |
leothedragon | 0:25fa8795676b | 413 | if (_observation_number == 1) { |
leothedragon | 0:25fa8795676b | 414 | // Increment the observation number by 1 if it is already 1 because CoAP specification has reserved 1 for DEREGISTER notification |
leothedragon | 0:25fa8795676b | 415 | _observation_number++; |
leothedragon | 0:25fa8795676b | 416 | } |
leothedragon | 0:25fa8795676b | 417 | |
leothedragon | 0:25fa8795676b | 418 | if (_observer.observation_to_be_sent(_changed_instance_ids, observation_number(), true)) { |
leothedragon | 0:25fa8795676b | 419 | _changed_instance_ids.clear(); |
leothedragon | 0:25fa8795676b | 420 | set_notification_send_in_progress(true); |
leothedragon | 0:25fa8795676b | 421 | } else { |
leothedragon | 0:25fa8795676b | 422 | set_notification_in_queue(true); |
leothedragon | 0:25fa8795676b | 423 | } |
leothedragon | 0:25fa8795676b | 424 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 425 | _last_value.float_value = _current_value.float_value; |
leothedragon | 0:25fa8795676b | 426 | } else { |
leothedragon | 0:25fa8795676b | 427 | _last_value.int_value = _current_value.int_value; |
leothedragon | 0:25fa8795676b | 428 | } |
leothedragon | 0:25fa8795676b | 429 | } |
leothedragon | 0:25fa8795676b | 430 | else { |
leothedragon | 0:25fa8795676b | 431 | tr_debug("M2MReportHandler::report()- no need to send"); |
leothedragon | 0:25fa8795676b | 432 | } |
leothedragon | 0:25fa8795676b | 433 | } |
leothedragon | 0:25fa8795676b | 434 | handle_timers(); |
leothedragon | 0:25fa8795676b | 435 | } |
leothedragon | 0:25fa8795676b | 436 | |
leothedragon | 0:25fa8795676b | 437 | void M2MReportHandler::handle_timers() |
leothedragon | 0:25fa8795676b | 438 | { |
leothedragon | 0:25fa8795676b | 439 | tr_debug("M2MReportHandler::handle_timers()"); |
leothedragon | 0:25fa8795676b | 440 | uint64_t time_interval = 0; |
leothedragon | 0:25fa8795676b | 441 | if ((_attribute_state & M2MReportHandler::Pmin) == M2MReportHandler::Pmin) { |
leothedragon | 0:25fa8795676b | 442 | if (_pmin == _pmax) { |
leothedragon | 0:25fa8795676b | 443 | _pmin_exceeded = true; |
leothedragon | 0:25fa8795676b | 444 | } else { |
leothedragon | 0:25fa8795676b | 445 | _pmin_exceeded = false; |
leothedragon | 0:25fa8795676b | 446 | time_interval = (uint64_t) ((uint64_t)_pmin * 1000); |
leothedragon | 0:25fa8795676b | 447 | tr_debug("M2MReportHandler::handle_timers() - Start PMIN interval: %d", (int)time_interval); |
leothedragon | 0:25fa8795676b | 448 | _pmin_timer.start_timer(time_interval, |
leothedragon | 0:25fa8795676b | 449 | M2MTimerObserver::PMinTimer, |
leothedragon | 0:25fa8795676b | 450 | true); |
leothedragon | 0:25fa8795676b | 451 | } |
leothedragon | 0:25fa8795676b | 452 | } |
leothedragon | 0:25fa8795676b | 453 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax) { |
leothedragon | 0:25fa8795676b | 454 | if (_pmax > 0) { |
leothedragon | 0:25fa8795676b | 455 | time_interval = (uint64_t) ((uint64_t)_pmax * 1000); |
leothedragon | 0:25fa8795676b | 456 | tr_debug("M2MReportHandler::handle_timers() - Start PMAX interval: %d", (int)time_interval); |
leothedragon | 0:25fa8795676b | 457 | _pmax_timer.start_timer(time_interval, |
leothedragon | 0:25fa8795676b | 458 | M2MTimerObserver::PMaxTimer, |
leothedragon | 0:25fa8795676b | 459 | true); |
leothedragon | 0:25fa8795676b | 460 | } |
leothedragon | 0:25fa8795676b | 461 | } |
leothedragon | 0:25fa8795676b | 462 | } |
leothedragon | 0:25fa8795676b | 463 | |
leothedragon | 0:25fa8795676b | 464 | bool M2MReportHandler::check_attribute_validity() const |
leothedragon | 0:25fa8795676b | 465 | { |
leothedragon | 0:25fa8795676b | 466 | bool success = true; |
leothedragon | 0:25fa8795676b | 467 | if ((_attribute_state & M2MReportHandler::Pmax) == M2MReportHandler::Pmax && |
leothedragon | 0:25fa8795676b | 468 | ((_pmax >= -1.0) && (_pmin > _pmax))) { |
leothedragon | 0:25fa8795676b | 469 | success = false; |
leothedragon | 0:25fa8795676b | 470 | } |
leothedragon | 0:25fa8795676b | 471 | float low = _lt + 2 * _st; |
leothedragon | 0:25fa8795676b | 472 | if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
leothedragon | 0:25fa8795676b | 473 | (low >= _gt)) { |
leothedragon | 0:25fa8795676b | 474 | success = false; |
leothedragon | 0:25fa8795676b | 475 | } |
leothedragon | 0:25fa8795676b | 476 | return success; |
leothedragon | 0:25fa8795676b | 477 | } |
leothedragon | 0:25fa8795676b | 478 | |
leothedragon | 0:25fa8795676b | 479 | void M2MReportHandler::stop_timers() |
leothedragon | 0:25fa8795676b | 480 | { |
leothedragon | 0:25fa8795676b | 481 | tr_debug("M2MReportHandler::stop_timers()"); |
leothedragon | 0:25fa8795676b | 482 | |
leothedragon | 0:25fa8795676b | 483 | _pmin_exceeded = false; |
leothedragon | 0:25fa8795676b | 484 | _pmin_timer.stop_timer(); |
leothedragon | 0:25fa8795676b | 485 | |
leothedragon | 0:25fa8795676b | 486 | _pmax_exceeded = false; |
leothedragon | 0:25fa8795676b | 487 | _pmax_timer.stop_timer(); |
leothedragon | 0:25fa8795676b | 488 | |
leothedragon | 0:25fa8795676b | 489 | tr_debug("M2MReportHandler::stop_timers() - out"); |
leothedragon | 0:25fa8795676b | 490 | } |
leothedragon | 0:25fa8795676b | 491 | |
leothedragon | 0:25fa8795676b | 492 | void M2MReportHandler::set_default_values() |
leothedragon | 0:25fa8795676b | 493 | { |
leothedragon | 0:25fa8795676b | 494 | tr_debug("M2MReportHandler::set_default_values"); |
leothedragon | 0:25fa8795676b | 495 | _pmax = -1.0; |
leothedragon | 0:25fa8795676b | 496 | _pmin = 1.0; |
leothedragon | 0:25fa8795676b | 497 | _gt = 0.0f; |
leothedragon | 0:25fa8795676b | 498 | _lt = 0.0f; |
leothedragon | 0:25fa8795676b | 499 | _st = 0.0f; |
leothedragon | 0:25fa8795676b | 500 | _pmin_exceeded = false; |
leothedragon | 0:25fa8795676b | 501 | _pmax_exceeded = false; |
leothedragon | 0:25fa8795676b | 502 | _attribute_state = 0; |
leothedragon | 0:25fa8795676b | 503 | _changed_instance_ids.clear(); |
leothedragon | 0:25fa8795676b | 504 | _notification_in_queue = false; |
leothedragon | 0:25fa8795676b | 505 | _notification_send_in_progress = false; |
leothedragon | 0:25fa8795676b | 506 | _pmin_quiet_period = false; |
leothedragon | 0:25fa8795676b | 507 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 508 | _high_step.float_value = 0.0f; |
leothedragon | 0:25fa8795676b | 509 | _low_step.float_value = 0.0f; |
leothedragon | 0:25fa8795676b | 510 | _last_value.float_value = -1.0f; |
leothedragon | 0:25fa8795676b | 511 | } else { |
leothedragon | 0:25fa8795676b | 512 | _high_step.int_value = 0; |
leothedragon | 0:25fa8795676b | 513 | _low_step.int_value = 0; |
leothedragon | 0:25fa8795676b | 514 | _last_value.int_value = -1; |
leothedragon | 0:25fa8795676b | 515 | } |
leothedragon | 0:25fa8795676b | 516 | } |
leothedragon | 0:25fa8795676b | 517 | |
leothedragon | 0:25fa8795676b | 518 | bool M2MReportHandler::check_threshold_values() const |
leothedragon | 0:25fa8795676b | 519 | { |
leothedragon | 0:25fa8795676b | 520 | tr_debug("M2MReportHandler::check_threshold_values"); |
leothedragon | 0:25fa8795676b | 521 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 522 | tr_debug("Current value: %f", _current_value.float_value); |
leothedragon | 0:25fa8795676b | 523 | tr_debug("Last value: %f", _last_value.float_value); |
leothedragon | 0:25fa8795676b | 524 | tr_debug("High step: %f", _high_step.float_value); |
leothedragon | 0:25fa8795676b | 525 | tr_debug("Low step: %f", _low_step.float_value); |
leothedragon | 0:25fa8795676b | 526 | } else { |
leothedragon | 0:25fa8795676b | 527 | tr_debug("Current value: %" PRId64, _current_value.int_value); |
leothedragon | 0:25fa8795676b | 528 | tr_debug("Last value: %" PRId64, _last_value.int_value); |
leothedragon | 0:25fa8795676b | 529 | tr_debug("High step: %" PRId64, _high_step.int_value); |
leothedragon | 0:25fa8795676b | 530 | tr_debug("Low step: %" PRId64, _low_step.int_value); |
leothedragon | 0:25fa8795676b | 531 | } |
leothedragon | 0:25fa8795676b | 532 | |
leothedragon | 0:25fa8795676b | 533 | tr_debug("Less than: %f", _lt); |
leothedragon | 0:25fa8795676b | 534 | tr_debug("Greater than: %f", _gt); |
leothedragon | 0:25fa8795676b | 535 | tr_debug("Step: %f", _st); |
leothedragon | 0:25fa8795676b | 536 | |
leothedragon | 0:25fa8795676b | 537 | bool can_send = check_gt_lt_params(); |
leothedragon | 0:25fa8795676b | 538 | if (can_send) { |
leothedragon | 0:25fa8795676b | 539 | if ((_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
leothedragon | 0:25fa8795676b | 540 | can_send = false; |
leothedragon | 0:25fa8795676b | 541 | |
leothedragon | 0:25fa8795676b | 542 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 543 | if (_current_value.float_value >= _high_step.float_value || |
leothedragon | 0:25fa8795676b | 544 | _current_value.float_value <= _low_step.float_value) { |
leothedragon | 0:25fa8795676b | 545 | can_send = true; |
leothedragon | 0:25fa8795676b | 546 | } |
leothedragon | 0:25fa8795676b | 547 | } else { |
leothedragon | 0:25fa8795676b | 548 | if ((_current_value.int_value >= _high_step.int_value || |
leothedragon | 0:25fa8795676b | 549 | _current_value.int_value <= _low_step.int_value)) { |
leothedragon | 0:25fa8795676b | 550 | can_send = true; |
leothedragon | 0:25fa8795676b | 551 | } |
leothedragon | 0:25fa8795676b | 552 | } |
leothedragon | 0:25fa8795676b | 553 | } |
leothedragon | 0:25fa8795676b | 554 | } |
leothedragon | 0:25fa8795676b | 555 | |
leothedragon | 0:25fa8795676b | 556 | tr_debug("M2MReportHandler::check_threshold_values - value can be sent = %d", (int)can_send); |
leothedragon | 0:25fa8795676b | 557 | return can_send; |
leothedragon | 0:25fa8795676b | 558 | } |
leothedragon | 0:25fa8795676b | 559 | |
leothedragon | 0:25fa8795676b | 560 | bool M2MReportHandler::check_gt_lt_params() const |
leothedragon | 0:25fa8795676b | 561 | { |
leothedragon | 0:25fa8795676b | 562 | tr_debug("M2MReportHandler::check_gt_lt_params"); |
leothedragon | 0:25fa8795676b | 563 | bool can_send = false; |
leothedragon | 0:25fa8795676b | 564 | // GT & LT set. |
leothedragon | 0:25fa8795676b | 565 | if ((_attribute_state & (M2MReportHandler::Lt | M2MReportHandler::Gt)) == |
leothedragon | 0:25fa8795676b | 566 | (M2MReportHandler::Lt | M2MReportHandler::Gt)) { |
leothedragon | 0:25fa8795676b | 567 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 568 | if (_current_value.float_value > _gt || _current_value.float_value < _lt) { |
leothedragon | 0:25fa8795676b | 569 | can_send = true; |
leothedragon | 0:25fa8795676b | 570 | } |
leothedragon | 0:25fa8795676b | 571 | } else { |
leothedragon | 0:25fa8795676b | 572 | if (_current_value.int_value > _gt || _current_value.int_value < _lt) { |
leothedragon | 0:25fa8795676b | 573 | can_send = true; |
leothedragon | 0:25fa8795676b | 574 | } |
leothedragon | 0:25fa8795676b | 575 | } |
leothedragon | 0:25fa8795676b | 576 | } |
leothedragon | 0:25fa8795676b | 577 | // Only LT |
leothedragon | 0:25fa8795676b | 578 | else if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt && |
leothedragon | 0:25fa8795676b | 579 | (_attribute_state & M2MReportHandler::Gt) == 0 ) { |
leothedragon | 0:25fa8795676b | 580 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 581 | if (_current_value.float_value < _lt) { |
leothedragon | 0:25fa8795676b | 582 | can_send = true; |
leothedragon | 0:25fa8795676b | 583 | } |
leothedragon | 0:25fa8795676b | 584 | } else { |
leothedragon | 0:25fa8795676b | 585 | if (_current_value.int_value < _lt) { |
leothedragon | 0:25fa8795676b | 586 | can_send = true; |
leothedragon | 0:25fa8795676b | 587 | } |
leothedragon | 0:25fa8795676b | 588 | } |
leothedragon | 0:25fa8795676b | 589 | |
leothedragon | 0:25fa8795676b | 590 | } |
leothedragon | 0:25fa8795676b | 591 | // Only GT |
leothedragon | 0:25fa8795676b | 592 | else if ((_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt && |
leothedragon | 0:25fa8795676b | 593 | (_attribute_state & M2MReportHandler::Lt) == 0 ) { |
leothedragon | 0:25fa8795676b | 594 | if (_resource_type == M2MBase::FLOAT) { |
leothedragon | 0:25fa8795676b | 595 | if (_current_value.float_value > _gt) { |
leothedragon | 0:25fa8795676b | 596 | can_send = true; |
leothedragon | 0:25fa8795676b | 597 | } |
leothedragon | 0:25fa8795676b | 598 | } else { |
leothedragon | 0:25fa8795676b | 599 | if (_current_value.int_value > _gt) { |
leothedragon | 0:25fa8795676b | 600 | can_send = true; |
leothedragon | 0:25fa8795676b | 601 | } |
leothedragon | 0:25fa8795676b | 602 | } |
leothedragon | 0:25fa8795676b | 603 | |
leothedragon | 0:25fa8795676b | 604 | } |
leothedragon | 0:25fa8795676b | 605 | // GT & LT not set. |
leothedragon | 0:25fa8795676b | 606 | else { |
leothedragon | 0:25fa8795676b | 607 | can_send = true; |
leothedragon | 0:25fa8795676b | 608 | } |
leothedragon | 0:25fa8795676b | 609 | tr_debug("M2MReportHandler::check_gt_lt_params - value in range = %d", (int)can_send); |
leothedragon | 0:25fa8795676b | 610 | return can_send; |
leothedragon | 0:25fa8795676b | 611 | } |
leothedragon | 0:25fa8795676b | 612 | |
leothedragon | 0:25fa8795676b | 613 | uint8_t M2MReportHandler::attribute_flags() const |
leothedragon | 0:25fa8795676b | 614 | { |
leothedragon | 0:25fa8795676b | 615 | return _attribute_state; |
leothedragon | 0:25fa8795676b | 616 | } |
leothedragon | 0:25fa8795676b | 617 | |
leothedragon | 0:25fa8795676b | 618 | void M2MReportHandler::set_observation_token(const uint8_t *token, const uint8_t length) |
leothedragon | 0:25fa8795676b | 619 | { |
leothedragon | 0:25fa8795676b | 620 | free(_token); |
leothedragon | 0:25fa8795676b | 621 | _token = NULL; |
leothedragon | 0:25fa8795676b | 622 | _token_length = 0; |
leothedragon | 0:25fa8795676b | 623 | |
leothedragon | 0:25fa8795676b | 624 | if( token != NULL && length > 0 ) { |
leothedragon | 0:25fa8795676b | 625 | _token = alloc_copy((uint8_t *)token, length); |
leothedragon | 0:25fa8795676b | 626 | if(_token) { |
leothedragon | 0:25fa8795676b | 627 | _token_length = length; |
leothedragon | 0:25fa8795676b | 628 | } |
leothedragon | 0:25fa8795676b | 629 | } |
leothedragon | 0:25fa8795676b | 630 | } |
leothedragon | 0:25fa8795676b | 631 | |
leothedragon | 0:25fa8795676b | 632 | void M2MReportHandler::get_observation_token(uint8_t *token, uint8_t &token_length) const |
leothedragon | 0:25fa8795676b | 633 | { |
leothedragon | 0:25fa8795676b | 634 | memcpy(token, _token, _token_length); |
leothedragon | 0:25fa8795676b | 635 | token_length = _token_length; |
leothedragon | 0:25fa8795676b | 636 | } |
leothedragon | 0:25fa8795676b | 637 | |
leothedragon | 0:25fa8795676b | 638 | uint16_t M2MReportHandler::observation_number() const |
leothedragon | 0:25fa8795676b | 639 | { |
leothedragon | 0:25fa8795676b | 640 | return _observation_number; |
leothedragon | 0:25fa8795676b | 641 | } |
leothedragon | 0:25fa8795676b | 642 | |
leothedragon | 0:25fa8795676b | 643 | void M2MReportHandler::add_observation_level(M2MBase::Observation obs_level) |
leothedragon | 0:25fa8795676b | 644 | { |
leothedragon | 0:25fa8795676b | 645 | _observation_level = (M2MBase::Observation)(_observation_level | obs_level); |
leothedragon | 0:25fa8795676b | 646 | } |
leothedragon | 0:25fa8795676b | 647 | |
leothedragon | 0:25fa8795676b | 648 | void M2MReportHandler::remove_observation_level(M2MBase::Observation obs_level) |
leothedragon | 0:25fa8795676b | 649 | { |
leothedragon | 0:25fa8795676b | 650 | _observation_level = (M2MBase::Observation)(_observation_level & ~obs_level); |
leothedragon | 0:25fa8795676b | 651 | } |
leothedragon | 0:25fa8795676b | 652 | |
leothedragon | 0:25fa8795676b | 653 | M2MBase::Observation M2MReportHandler::observation_level() const |
leothedragon | 0:25fa8795676b | 654 | { |
leothedragon | 0:25fa8795676b | 655 | return _observation_level; |
leothedragon | 0:25fa8795676b | 656 | } |
leothedragon | 0:25fa8795676b | 657 | |
leothedragon | 0:25fa8795676b | 658 | bool M2MReportHandler::is_under_observation() const |
leothedragon | 0:25fa8795676b | 659 | { |
leothedragon | 0:25fa8795676b | 660 | return _is_under_observation; |
leothedragon | 0:25fa8795676b | 661 | } |
leothedragon | 0:25fa8795676b | 662 | |
leothedragon | 0:25fa8795676b | 663 | uint8_t* M2MReportHandler::alloc_copy(const uint8_t* source, uint32_t size) |
leothedragon | 0:25fa8795676b | 664 | { |
leothedragon | 0:25fa8795676b | 665 | assert(source != NULL); |
leothedragon | 0:25fa8795676b | 666 | |
leothedragon | 0:25fa8795676b | 667 | uint8_t* result = (uint8_t*)malloc(size); |
leothedragon | 0:25fa8795676b | 668 | if (result) { |
leothedragon | 0:25fa8795676b | 669 | memcpy(result, source, size); |
leothedragon | 0:25fa8795676b | 670 | } |
leothedragon | 0:25fa8795676b | 671 | return result; |
leothedragon | 0:25fa8795676b | 672 | } |
leothedragon | 0:25fa8795676b | 673 | |
leothedragon | 0:25fa8795676b | 674 | void M2MReportHandler::set_notification_in_queue(bool to_queue) |
leothedragon | 0:25fa8795676b | 675 | { |
leothedragon | 0:25fa8795676b | 676 | _notification_in_queue = to_queue; |
leothedragon | 0:25fa8795676b | 677 | } |
leothedragon | 0:25fa8795676b | 678 | |
leothedragon | 0:25fa8795676b | 679 | bool M2MReportHandler::notification_in_queue() const |
leothedragon | 0:25fa8795676b | 680 | { |
leothedragon | 0:25fa8795676b | 681 | return _notification_in_queue; |
leothedragon | 0:25fa8795676b | 682 | } |
leothedragon | 0:25fa8795676b | 683 | |
leothedragon | 0:25fa8795676b | 684 | void M2MReportHandler::set_notification_send_in_progress(bool progress) |
leothedragon | 0:25fa8795676b | 685 | { |
leothedragon | 0:25fa8795676b | 686 | _notification_send_in_progress = progress; |
leothedragon | 0:25fa8795676b | 687 | } |
leothedragon | 0:25fa8795676b | 688 | |
leothedragon | 0:25fa8795676b | 689 | bool M2MReportHandler::notification_send_in_progress() const |
leothedragon | 0:25fa8795676b | 690 | { |
leothedragon | 0:25fa8795676b | 691 | return _notification_send_in_progress; |
leothedragon | 0:25fa8795676b | 692 | } |
leothedragon | 0:25fa8795676b | 693 | |
leothedragon | 0:25fa8795676b | 694 | void M2MReportHandler::set_blockwise_notify(bool blockwise_notify) |
leothedragon | 0:25fa8795676b | 695 | { |
leothedragon | 0:25fa8795676b | 696 | _blockwise_notify = blockwise_notify; |
leothedragon | 0:25fa8795676b | 697 | } |
leothedragon | 0:25fa8795676b | 698 | |
leothedragon | 0:25fa8795676b | 699 | bool M2MReportHandler::blockwise_notify() const |
leothedragon | 0:25fa8795676b | 700 | { |
leothedragon | 0:25fa8795676b | 701 | return _blockwise_notify; |
leothedragon | 0:25fa8795676b | 702 | } |
leothedragon | 0:25fa8795676b | 703 | |
leothedragon | 0:25fa8795676b | 704 | void M2MReportHandler::send_value() |
leothedragon | 0:25fa8795676b | 705 | { |
leothedragon | 0:25fa8795676b | 706 | tr_debug("M2MReportHandler::send_value() - new value"); |
leothedragon | 0:25fa8795676b | 707 | set_notification_in_queue(true); |
leothedragon | 0:25fa8795676b | 708 | if (check_threshold_values()) { |
leothedragon | 0:25fa8795676b | 709 | schedule_report(); |
leothedragon | 0:25fa8795676b | 710 | } else { |
leothedragon | 0:25fa8795676b | 711 | tr_debug("M2MReportHandler::send_value - value not in range"); |
leothedragon | 0:25fa8795676b | 712 | _notify = false; |
leothedragon | 0:25fa8795676b | 713 | if ((_attribute_state & M2MReportHandler::Lt) == M2MReportHandler::Lt || |
leothedragon | 0:25fa8795676b | 714 | (_attribute_state & M2MReportHandler::Gt) == M2MReportHandler::Gt || |
leothedragon | 0:25fa8795676b | 715 | (_attribute_state & M2MReportHandler::St) == M2MReportHandler::St) { |
leothedragon | 0:25fa8795676b | 716 | tr_debug("M2MReportHandler::send_value - stop pmin timer"); |
leothedragon | 0:25fa8795676b | 717 | _pmin_timer.stop_timer(); |
leothedragon | 0:25fa8795676b | 718 | _pmin_exceeded = true; |
leothedragon | 0:25fa8795676b | 719 | } |
leothedragon | 0:25fa8795676b | 720 | } |
leothedragon | 0:25fa8795676b | 721 | } |