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