Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mreporthandler.h Source File

m2mreporthandler.h

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef M2MREPORTHANDLER_H
00017 #define M2MREPORTHANDLER_H
00018 
00019 // Support for std args
00020 #include <stdint.h>
00021 #include "mbed-client/m2mconfig.h"
00022 #include "mbed-client/m2mbase.h"
00023 #include "mbed-client/m2mtimerobserver.h"
00024 #include "mbed-client/m2mresourceinstance.h"
00025 #include "mbed-client/m2mvector.h"
00026 #include "mbed-client/m2mtimer.h"
00027 
00028 //FORWARD DECLARATION
00029 class M2MReportObserver;
00030 class M2MTimer;
00031 class M2MResourceInstance;
00032 
00033 /**
00034  *  @brief M2MReportHandler.
00035  *  This class is handles all the observation related operations.
00036  */
00037 class M2MReportHandler: public M2MTimerObserver
00038 {
00039 private:
00040     // Prevents the use of assignment operator by accident.
00041     M2MReportHandler& operator=( const M2MReportHandler& /*other*/ );
00042 
00043 public:
00044 
00045     M2MReportHandler(M2MReportObserver &observer);
00046 
00047 public:
00048 
00049     /**
00050      * Enum defining which write attributes are set.
00051     */
00052     enum {
00053         Cancel = 1,
00054         Pmin = 2,
00055         Pmax = 4,
00056         Lt = 8,
00057         Gt = 16,
00058         St = 32
00059     };
00060 
00061     /**
00062      * Destructor
00063      */
00064     virtual ~M2MReportHandler();
00065 
00066     /**
00067      * @brief Sets that object is under observation.
00068      * @param Value for the observation.
00069      * @param handler, Handler object for sending
00070      * observation callbacks.
00071      */
00072     void set_under_observation(bool observed);
00073 
00074     /**
00075      * @brief Sets the value of the given resource.
00076      * @param value, Value of the observed resource.
00077      */
00078     void set_value(float value);
00079 
00080     /**
00081      * @brief Sets notification trigger.
00082      * @param obj_instance_id, Object instance id that has changed
00083      */
00084     void set_notification_trigger(uint16_t obj_instance_id = 0);
00085 
00086     /**
00087      * @brief Parses the received query for notification
00088      * attribute.
00089      * @param query Query to be parsed for attributes.
00090      * @param type Type of the Base Object.
00091      * @param resource_type Type of the Resource.
00092      * @return true if required attributes are present else false.
00093      */
00094     bool parse_notification_attribute(const char *query,
00095                                               M2MBase::BaseType type,
00096                                               M2MResourceInstance::ResourceType resource_type = M2MResourceInstance::OPAQUE);
00097 
00098     /**
00099     * @brief Set back to default values.
00100     */
00101     void set_default_values();
00102 
00103     /**
00104      * @brief Return write attribute flags.
00105      */
00106     uint8_t attribute_flags() const;
00107 
00108     /**
00109      * \brief Sets the observation token value.
00110      * \param token A pointer to the token of the resource.
00111      * \param length The length of the token pointer.
00112      */
00113     void set_observation_token(const uint8_t *token, const uint8_t length);
00114 
00115     /**
00116      * \brief Provides a copy of the observation token of the object.
00117      * \param value[OUT] A pointer to the value of the token.
00118      * \param value_length[OUT] The length of the token pointer.
00119      */
00120     void get_observation_token(uint8_t *token, uint8_t &token_length) const;
00121 
00122     /**
00123      * \brief Returns the observation number.
00124      * \return The observation number of the object.
00125      */
00126     uint16_t observation_number() const;
00127 
00128     /**
00129      * \brief Adds the observation level for the object.
00130      * \param observation_level The level of observation.
00131      */
00132     void add_observation_level(M2MBase::Observation obs_level);
00133 
00134     /**
00135      * \brief Removes the observation level for the object.
00136      * \param observation_level The level of observation.
00137      */
00138     void remove_observation_level(M2MBase::Observation obs_level);
00139 
00140     /**
00141      * \brief Returns the observation level of the object.
00142      * \return The observation level of the object.
00143      */
00144     M2MBase::Observation observation_level() const;
00145 
00146     /**
00147      * @brief Returns whether this resource is under observation or not.
00148      * @return True if the resource is under observation, else false,
00149      */
00150     bool is_under_observation() const;
00151 
00152 protected : // from M2MTimerObserver
00153 
00154     virtual void timer_expired(M2MTimerObserver::Type type =
00155                                M2MTimerObserver::Notdefined);
00156 
00157 private:
00158 
00159 
00160 
00161     bool set_notification_attribute(const char* option,
00162             M2MBase::BaseType type,
00163             M2MResourceInstance::ResourceType resource_type);
00164 
00165     /**
00166      * @brief Schedule a report, if the pmin is exceeded
00167      * then report immediately else store the state to be
00168      * reported once the time fires.
00169      */
00170     void schedule_report();
00171 
00172     /**
00173     * @brief Reports a sample that satisfies the reporting criteria.
00174     */
00175     void report();
00176 
00177     /**
00178     * @brief Manage timers for pmin and pmax.
00179     */
00180     void handle_timers();
00181 
00182     /**
00183     * @brief Check whether notification params can be accepted.
00184     */
00185     bool check_attribute_validity() const;
00186 
00187     /**
00188     * @brief Stop pmin & pmax timers.
00189     */
00190     void stop_timers();
00191 
00192     /**
00193      * @brief Check if current value match threshold values.
00194      * @return True if notify can be send otherwise false.
00195      */
00196     bool check_threshold_values() const;
00197 
00198     /**
00199      * @brief Check whether current value matches with GT & LT.
00200      * @return True if current value match with GT or LT values.
00201      */
00202     bool check_gt_lt_params() const;
00203 
00204     /**
00205      * \brief Allocate (size + 1) amount of memory, copy size bytes into
00206      * it and add zero termination.
00207      * \param source The source string to copy, may not be NULL.
00208      * \param size The size of memory to be reserved.
00209     */
00210     static uint8_t* alloc_string_copy(const uint8_t* source, uint32_t size);
00211 
00212 private:
00213     M2MReportObserver           &_observer;
00214     bool                        _is_under_observation : 1;
00215     M2MBase::Observation        _observation_level : 4;
00216     uint8_t                     _attribute_state;
00217     unsigned                    _token_length : 8;
00218     bool                        _notify;
00219     bool                        _pmin_exceeded;
00220     bool                        _pmax_exceeded;
00221     unsigned                    _observation_number : 24;
00222     M2MTimer                    _pmin_timer;
00223     M2MTimer                    _pmax_timer;
00224     uint8_t                     *_token;
00225     int32_t                     _pmax;
00226     int32_t                     _pmin;
00227     float                       _current_value;
00228     float                       _gt;
00229     float                       _lt;
00230     float                       _st;
00231     float                       _high_step;
00232     float                       _low_step;
00233     float                       _last_value;
00234     m2m::Vector<uint16_t>       _changed_instance_ids;
00235 
00236 friend class Test_M2MReportHandler;
00237 
00238 };
00239 
00240 #endif // M2MREPORTHANDLER_H