Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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 typedef union current_value_u {
00034     float   float_value;
00035     int64_t int_value;
00036 } current_value_t;
00037 
00038 typedef union last_value_u {
00039     float   float_value;
00040     int64_t int_value;
00041 } last_value_t;
00042 
00043 typedef union high_step_u {
00044     float   float_value;
00045     int64_t int_value;
00046 } high_step_t;
00047 
00048 typedef union low_step_u {
00049     float   float_value;
00050     int64_t int_value;
00051 } low_step_t;
00052 
00053 /**
00054  *  @brief M2MReportHandler.
00055  *  This class is handles all the observation related operations.
00056  */
00057 class M2MReportHandler: public M2MTimerObserver
00058 {
00059 private:
00060     // Prevents the use of assignment operator by accident.
00061     M2MReportHandler& operator=( const M2MReportHandler& /*other*/ );
00062 
00063 public:
00064 
00065     M2MReportHandler(M2MReportObserver &observer, M2MBase::DataType type);
00066 
00067 public:
00068 
00069     /**
00070      * Enum defining which write attributes are set.
00071     */
00072     enum {
00073         Cancel = 1,
00074         Pmin = 2,
00075         Pmax = 4,
00076         Lt = 8,
00077         Gt = 16,
00078         St = 32
00079     };
00080 
00081     /**
00082      * Destructor
00083      */
00084     virtual ~M2MReportHandler();
00085 
00086     /**
00087      * @brief Sets that object is under observation.
00088      * @param Value for the observation.
00089      * @param handler, Handler object for sending
00090      * observation callbacks.
00091      */
00092     void set_under_observation(bool observed);
00093 
00094     /**
00095      * @brief Sets the float value of the given resource.
00096      * @param value, Value of the observed resource.
00097      */
00098     void set_value_float(float value);
00099 
00100     /**
00101      * @brief Sets the integer value of the given resource.
00102      * @param value, Value of the observed resource.
00103      */
00104     void set_value_int(int64_t value);
00105 
00106     /**
00107      * @brief Sets notification trigger.
00108      * @param obj_instance_id, Object instance id that has changed
00109      */
00110     void set_notification_trigger(uint16_t obj_instance_id = 0);
00111 
00112     /**
00113      * @brief Parses the received query for notification
00114      * attribute.
00115      * @param query Query to be parsed for attributes.
00116      * @param type Type of the Base Object.
00117      * @param resource_type Type of the Resource.
00118      * @return true if required attributes are present else false.
00119      */
00120     bool parse_notification_attribute(const char *query,
00121                                               M2MBase::BaseType type,
00122                                               M2MResourceInstance::ResourceType resource_type = M2MResourceInstance::OPAQUE);
00123 
00124     /**
00125     * @brief Set back to default values.
00126     */
00127     void set_default_values();
00128 
00129     /**
00130      * @brief Return write attribute flags.
00131      */
00132     uint8_t attribute_flags() const;
00133 
00134     /**
00135      * \brief Sets the observation token value.
00136      * \param token A pointer to the token of the resource.
00137      * \param length The length of the token pointer.
00138      */
00139     void set_observation_token(const uint8_t *token, const uint8_t length);
00140 
00141     /**
00142      * \brief Provides a copy of the observation token of the object.
00143      * \param value[OUT] A pointer to the value of the token.
00144      * \param value_length[OUT] The length of the token pointer.
00145      */
00146     void get_observation_token(uint8_t *token, uint8_t &token_length) const;
00147 
00148     /**
00149      * \brief Returns the observation number.
00150      * \return The observation number of the object.
00151      */
00152     uint16_t observation_number() const;
00153 
00154     /**
00155      * \brief Adds the observation level for the object.
00156      * \param observation_level The level of observation.
00157      */
00158     void add_observation_level(M2MBase::Observation obs_level);
00159 
00160     /**
00161      * \brief Removes the observation level for the object.
00162      * \param observation_level The level of observation.
00163      */
00164     void remove_observation_level(M2MBase::Observation obs_level);
00165 
00166     /**
00167      * \brief Returns the observation level of the object.
00168      * \return The observation level of the object.
00169      */
00170     M2MBase::Observation observation_level() const;
00171 
00172     /**
00173      * @brief Returns whether this resource is under observation or not.
00174      * @return True if the resource is under observation, else false,
00175      */
00176     bool is_under_observation() const;
00177 
00178     /**
00179      * @brief Schedule a report, if the pmin is exceeded
00180      * report immediately, otherwise store the state to be
00181      * reported once the time fires.
00182      *
00183      * @param in_queue If the message is queued message then it must be send even if
00184      * current and last values are the same.
00185      */
00186     void schedule_report(bool in_queue = false);
00187 
00188     /**
00189      * @brief Set flag that new notification needs to be send.
00190      *
00191      * @param to_queue If True then notification is marked to be send
00192      */
00193     void set_notification_in_queue(bool to_queue);
00194 
00195     /**
00196      * @brief Returns whether notification needs to be send or not.
00197      *
00198      * @return Is notification sending needed or not.
00199      */
00200     bool notification_in_queue() const;
00201 
00202     /**
00203      * @brief Set flag that new notification needs to be send.
00204      *
00205      * @param to_queue If True then notification is marked to be send
00206      */
00207     void set_notification_send_in_progress(bool progress);
00208 
00209     /**
00210      * @brief Returns whether notification send is in progress or not.
00211      *
00212      * @return Is notification sending ongoing or not.
00213      */
00214     bool notification_send_in_progress() const;
00215 
00216     /**
00217      * @brief Sets whether notification will be sent using blockwise or not.
00218      *
00219      * @param blockwise_notify If True then notification is sent using blockwise.
00220      */
00221     void set_blockwise_notify(bool blockwise_notify);
00222 
00223     /**
00224      * @brief Returns whether notification is sent using blockwise or not.
00225      *
00226      * @return Is notification sent using blockwise.
00227      */
00228     bool blockwise_notify() const;
00229 
00230 protected : // from M2MTimerObserver
00231 
00232     virtual void timer_expired(M2MTimerObserver::Type type =
00233                                M2MTimerObserver::Notdefined);
00234 
00235 private:
00236 
00237     bool set_notification_attribute(const char* option,
00238             M2MBase::BaseType type,
00239             M2MResourceInstance::ResourceType resource_type);
00240 
00241     /**
00242     * @brief Reports a sample that satisfies the reporting criteria.
00243     *
00244     * @param in_queue If the message is queued message then it must be send even
00245     * current and last values are the same.
00246     */
00247     void report(bool in_queue = false);
00248 
00249     /**
00250     * @brief Manage timers for pmin and pmax.
00251     */
00252     void handle_timers();
00253 
00254     /**
00255     * @brief Check whether notification params can be accepted.
00256     */
00257     bool check_attribute_validity() const;
00258 
00259     /**
00260     * @brief Stop pmin & pmax timers.
00261     */
00262     void stop_timers();
00263 
00264     /**
00265      * @brief Check if current value match threshold values.
00266      * @return True if notify can be send otherwise false.
00267      */
00268     bool check_threshold_values() const;
00269 
00270     /**
00271      * @brief Check whether current value matches with GT & LT.
00272      * @return True if current value match with GT or LT values.
00273      */
00274     bool check_gt_lt_params() const;
00275 
00276     /**
00277      * \brief Allocate size amount of memory, copy size bytes into it
00278      * \param source The source data to copy, may not be NULL.
00279      * \param size The size of memory to be reserved.
00280     */
00281     static uint8_t* alloc_copy(const uint8_t* source, uint32_t size);
00282 
00283     /**
00284      * \brief New value is ready to be sent.
00285     */
00286     void send_value();
00287 
00288 private:
00289     M2MReportObserver           &_observer;
00290     bool                        _is_under_observation : 1;
00291     M2MBase::Observation        _observation_level : 3;
00292     uint8_t                     _attribute_state;
00293     unsigned                    _token_length : 8;
00294     M2MBase::DataType           _resource_type : 3;
00295     bool                        _notify : 1;
00296     bool                        _pmin_exceeded : 1;
00297     bool                        _pmax_exceeded : 1;
00298     unsigned                    _observation_number : 24;
00299     M2MTimer                    _pmin_timer;
00300     M2MTimer                    _pmax_timer;
00301     uint8_t                     *_token;
00302     int32_t                     _pmax;
00303     int32_t                     _pmin;
00304     current_value_t             _current_value;
00305     high_step_t                 _high_step;
00306     low_step_t                  _low_step;
00307     last_value_t                _last_value;
00308     float                       _gt;
00309     float                       _lt;
00310     float                       _st;
00311     m2m::Vector<uint16_t>       _changed_instance_ids;
00312     bool                        _notification_send_in_progress : 1;
00313     bool                        _notification_in_queue : 1;
00314     bool                        _blockwise_notify : 1;
00315     bool                        _pmin_quiet_period : 1;
00316 
00317 friend class Test_M2MReportHandler;
00318 
00319 };
00320 
00321 #endif // M2MREPORTHANDLER_H