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