A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:a7a43371b306 1 /*
ram54288 0:a7a43371b306 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
ram54288 0:a7a43371b306 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:a7a43371b306 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:a7a43371b306 5 * not use this file except in compliance with the License.
ram54288 0:a7a43371b306 6 * You may obtain a copy of the License at
ram54288 0:a7a43371b306 7 *
ram54288 0:a7a43371b306 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:a7a43371b306 9 *
ram54288 0:a7a43371b306 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:a7a43371b306 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:a7a43371b306 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:a7a43371b306 13 * See the License for the specific language governing permissions and
ram54288 0:a7a43371b306 14 * limitations under the License.
ram54288 0:a7a43371b306 15 */
ram54288 0:a7a43371b306 16 #ifndef M2MREPORTHANDLER_H
ram54288 0:a7a43371b306 17 #define M2MREPORTHANDLER_H
ram54288 0:a7a43371b306 18
ram54288 0:a7a43371b306 19 // Support for std args
ram54288 0:a7a43371b306 20 #include <stdint.h>
ram54288 0:a7a43371b306 21 #include "mbed-client/m2mconfig.h"
ram54288 0:a7a43371b306 22 #include "mbed-client/m2mbase.h"
ram54288 0:a7a43371b306 23 #include "mbed-client/m2mtimerobserver.h"
ram54288 0:a7a43371b306 24 #include "mbed-client/m2mresourceinstance.h"
ram54288 0:a7a43371b306 25 #include "mbed-client/m2mvector.h"
ram54288 0:a7a43371b306 26 #include "mbed-client/m2mtimer.h"
ram54288 0:a7a43371b306 27
ram54288 0:a7a43371b306 28 //FORWARD DECLARATION
ram54288 0:a7a43371b306 29 class M2MReportObserver;
ram54288 0:a7a43371b306 30 class M2MTimer;
ram54288 0:a7a43371b306 31 class M2MResourceInstance;
ram54288 0:a7a43371b306 32
ram54288 0:a7a43371b306 33 /**
ram54288 0:a7a43371b306 34 * @brief M2MReportHandler.
ram54288 0:a7a43371b306 35 * This class is handles all the observation related operations.
ram54288 0:a7a43371b306 36 */
ram54288 0:a7a43371b306 37 class M2MReportHandler: public M2MTimerObserver
ram54288 0:a7a43371b306 38 {
ram54288 0:a7a43371b306 39 private:
ram54288 0:a7a43371b306 40 // Prevents the use of assignment operator by accident.
ram54288 0:a7a43371b306 41 M2MReportHandler& operator=( const M2MReportHandler& /*other*/ );
ram54288 0:a7a43371b306 42
ram54288 0:a7a43371b306 43 public:
ram54288 0:a7a43371b306 44
ram54288 0:a7a43371b306 45 M2MReportHandler(M2MReportObserver &observer);
ram54288 0:a7a43371b306 46
ram54288 0:a7a43371b306 47 public:
ram54288 0:a7a43371b306 48
ram54288 0:a7a43371b306 49 /**
ram54288 0:a7a43371b306 50 * Enum defining which write attributes are set.
ram54288 0:a7a43371b306 51 */
ram54288 0:a7a43371b306 52 enum {
ram54288 0:a7a43371b306 53 Cancel = 1,
ram54288 0:a7a43371b306 54 Pmin = 2,
ram54288 0:a7a43371b306 55 Pmax = 4,
ram54288 0:a7a43371b306 56 Lt = 8,
ram54288 0:a7a43371b306 57 Gt = 16,
ram54288 0:a7a43371b306 58 St = 32
ram54288 0:a7a43371b306 59 };
ram54288 0:a7a43371b306 60
ram54288 0:a7a43371b306 61 /**
ram54288 0:a7a43371b306 62 * Destructor
ram54288 0:a7a43371b306 63 */
ram54288 0:a7a43371b306 64 virtual ~M2MReportHandler();
ram54288 0:a7a43371b306 65
ram54288 0:a7a43371b306 66 /**
ram54288 0:a7a43371b306 67 * @brief Sets that object is under observation.
ram54288 0:a7a43371b306 68 * @param Value for the observation.
ram54288 0:a7a43371b306 69 * @param handler, Handler object for sending
ram54288 0:a7a43371b306 70 * observation callbacks.
ram54288 0:a7a43371b306 71 */
ram54288 0:a7a43371b306 72 void set_under_observation(bool observed);
ram54288 0:a7a43371b306 73
ram54288 0:a7a43371b306 74 /**
ram54288 0:a7a43371b306 75 * @brief Sets the value of the given resource.
ram54288 0:a7a43371b306 76 * @param value, Value of the observed resource.
ram54288 0:a7a43371b306 77 */
ram54288 0:a7a43371b306 78 void set_value(float value);
ram54288 0:a7a43371b306 79
ram54288 0:a7a43371b306 80 /**
ram54288 0:a7a43371b306 81 * @brief Sets notification trigger.
ram54288 0:a7a43371b306 82 * @param obj_instance_id, Object instance id that has changed
ram54288 0:a7a43371b306 83 */
ram54288 0:a7a43371b306 84 void set_notification_trigger(uint16_t obj_instance_id = 0);
ram54288 0:a7a43371b306 85
ram54288 0:a7a43371b306 86 /**
ram54288 0:a7a43371b306 87 * @brief Parses the received query for notification
ram54288 0:a7a43371b306 88 * attribute.
ram54288 0:a7a43371b306 89 * @param query Query to be parsed for attributes.
ram54288 0:a7a43371b306 90 * @param type Type of the Base Object.
ram54288 0:a7a43371b306 91 * @param resource_type Type of the Resource.
ram54288 0:a7a43371b306 92 * @return true if required attributes are present else false.
ram54288 0:a7a43371b306 93 */
ram54288 0:a7a43371b306 94 bool parse_notification_attribute(const char *query,
ram54288 0:a7a43371b306 95 M2MBase::BaseType type,
ram54288 0:a7a43371b306 96 M2MResourceInstance::ResourceType resource_type = M2MResourceInstance::OPAQUE);
ram54288 0:a7a43371b306 97
ram54288 0:a7a43371b306 98 /**
ram54288 0:a7a43371b306 99 * @brief Set back to default values.
ram54288 0:a7a43371b306 100 */
ram54288 0:a7a43371b306 101 void set_default_values();
ram54288 0:a7a43371b306 102
ram54288 0:a7a43371b306 103 /**
ram54288 0:a7a43371b306 104 * @brief Return write attribute flags.
ram54288 0:a7a43371b306 105 */
ram54288 0:a7a43371b306 106 uint8_t attribute_flags();
ram54288 0:a7a43371b306 107
ram54288 0:a7a43371b306 108 protected : // from M2MTimerObserver
ram54288 0:a7a43371b306 109
ram54288 0:a7a43371b306 110 virtual void timer_expired(M2MTimerObserver::Type type =
ram54288 0:a7a43371b306 111 M2MTimerObserver::Notdefined);
ram54288 0:a7a43371b306 112
ram54288 0:a7a43371b306 113 private:
ram54288 0:a7a43371b306 114
ram54288 0:a7a43371b306 115
ram54288 0:a7a43371b306 116
ram54288 0:a7a43371b306 117 bool set_notification_attribute(const char* option,
ram54288 0:a7a43371b306 118 M2MBase::BaseType type,
ram54288 0:a7a43371b306 119 M2MResourceInstance::ResourceType resource_type);
ram54288 0:a7a43371b306 120
ram54288 0:a7a43371b306 121 /**
ram54288 0:a7a43371b306 122 * @brief Schedule a report, if the pmin is exceeded
ram54288 0:a7a43371b306 123 * then report immediately else store the state to be
ram54288 0:a7a43371b306 124 * reported once the time fires.
ram54288 0:a7a43371b306 125 */
ram54288 0:a7a43371b306 126 void schedule_report();
ram54288 0:a7a43371b306 127
ram54288 0:a7a43371b306 128 /**
ram54288 0:a7a43371b306 129 * @brief Reports a sample that satisfies the reporting criteria.
ram54288 0:a7a43371b306 130 */
ram54288 0:a7a43371b306 131 void report();
ram54288 0:a7a43371b306 132
ram54288 0:a7a43371b306 133 /**
ram54288 0:a7a43371b306 134 * @brief Manage timers for pmin and pmax.
ram54288 0:a7a43371b306 135 */
ram54288 0:a7a43371b306 136 void handle_timers();
ram54288 0:a7a43371b306 137
ram54288 0:a7a43371b306 138 /**
ram54288 0:a7a43371b306 139 * @brief Check whether notification params can be accepted.
ram54288 0:a7a43371b306 140 */
ram54288 0:a7a43371b306 141 bool check_attribute_validity();
ram54288 0:a7a43371b306 142
ram54288 0:a7a43371b306 143 /**
ram54288 0:a7a43371b306 144 * @brief Stop pmin & pmax timers.
ram54288 0:a7a43371b306 145 */
ram54288 0:a7a43371b306 146 void stop_timers();
ram54288 0:a7a43371b306 147
ram54288 0:a7a43371b306 148 /**
ram54288 0:a7a43371b306 149 * @brief Check if current value match threshold values.
ram54288 0:a7a43371b306 150 * @return True if notify can be send otherwise false.
ram54288 0:a7a43371b306 151 */
ram54288 0:a7a43371b306 152 bool check_threshold_values();
ram54288 0:a7a43371b306 153
ram54288 0:a7a43371b306 154 /**
ram54288 0:a7a43371b306 155 * @brief Check whether current value matches with GT & LT.
ram54288 0:a7a43371b306 156 * @return True if current value match with GT or LT values.
ram54288 0:a7a43371b306 157 */
ram54288 0:a7a43371b306 158 bool check_gt_lt_params();
ram54288 0:a7a43371b306 159
ram54288 0:a7a43371b306 160 private:
ram54288 0:a7a43371b306 161 M2MReportObserver &_observer;
ram54288 0:a7a43371b306 162 uint8_t _attribute_state;
ram54288 0:a7a43371b306 163 bool _notify;
ram54288 0:a7a43371b306 164 bool _pmin_exceeded;
ram54288 0:a7a43371b306 165 bool _pmax_exceeded;
ram54288 0:a7a43371b306 166 M2MTimer _pmin_timer;
ram54288 0:a7a43371b306 167 M2MTimer _pmax_timer;
ram54288 0:a7a43371b306 168 int32_t _pmax;
ram54288 0:a7a43371b306 169 int32_t _pmin;
ram54288 0:a7a43371b306 170 float _current_value;
ram54288 0:a7a43371b306 171 float _gt;
ram54288 0:a7a43371b306 172 float _lt;
ram54288 0:a7a43371b306 173 float _st;
ram54288 0:a7a43371b306 174 float _high_step;
ram54288 0:a7a43371b306 175 float _low_step;
ram54288 0:a7a43371b306 176 float _last_value;
ram54288 0:a7a43371b306 177 m2m::Vector<uint16_t> _changed_instance_ids;
ram54288 0:a7a43371b306 178
ram54288 0:a7a43371b306 179 friend class Test_M2MReportHandler;
ram54288 0:a7a43371b306 180
ram54288 0:a7a43371b306 181 };
ram54288 0:a7a43371b306 182
ram54288 0:a7a43371b306 183 #endif // M2MREPORTHANDLER_H