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