joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mnsdlinterface.h Source File

m2mnsdlinterface.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 M2MNSDLINTERFACE_H
00017 #define M2MNSDLINTERFACE_H
00018 
00019 #include "mbed-client/m2mvector.h"
00020 #include "mbed-client/m2mconfig.h"
00021 #include "mbed-client/m2minterface.h"
00022 #include "mbed-client/m2mtimerobserver.h"
00023 #include "mbed-client/m2mobservationhandler.h"
00024 #include "mbed-client/m2mbase.h"
00025 #include "include/nsdllinker.h"
00026 
00027 //FORWARD DECLARARTION
00028 class M2MSecurity;
00029 class M2MObject;
00030 class M2MObjectInstance;
00031 class M2MResource;
00032 class M2MResourceInstance;
00033 class M2MNsdlObserver;
00034 class M2MServer;
00035 class M2MTimer;
00036 
00037 typedef Vector<M2MObject *> M2MObjectList;
00038 
00039 /**
00040  * @brief M2MNsdlInterface
00041  * Class which interacts between mbed Client C++ Library and mbed-client-c library.
00042  */
00043 class M2MNsdlInterface : public M2MTimerObserver,
00044                          public M2MObservationHandler
00045 {
00046 private:
00047     // Prevents the use of assignment operator by accident.
00048     M2MNsdlInterface& operator=( const M2MNsdlInterface& /*other*/ );
00049 
00050     // Prevents the use of copy constructor by accident
00051     M2MNsdlInterface( const M2MNsdlInterface& /*other*/ );
00052 
00053 public:
00054     /**
00055     * @brief Constructor
00056     * @param observer, Observer to pass the event callbacks from nsdl library.
00057     */
00058     M2MNsdlInterface(M2MNsdlObserver &observer);
00059 
00060     /**
00061      * @brief Destructor
00062      */
00063     virtual ~M2MNsdlInterface();
00064 
00065     /**
00066      * @brief Creates endpoint object for the nsdl stack.
00067      * @param endpoint_name, Endpoint name of the client.
00068      * @param endpoint_type, Endpoint type of the client.
00069      * @param life_time, Life time of the client in seconds
00070      * @param domain, Domain of the client.
00071      * @param mode, Binding mode of the client, default is UDP
00072      * @param context_address, Context address default is empty.
00073     */
00074     void create_endpoint(const String &endpoint_name,
00075                          const String &endpoint_type,
00076                          const int32_t life_time,
00077                          const String &domain,
00078                          const uint8_t mode,
00079                          const String &context_address);
00080 
00081     /**
00082      * @brief Deletes the endpoint.
00083     */
00084     void delete_endpoint();
00085 
00086     /**
00087      * @brief Creates the NSDL structure for the registered objectlist.
00088      * @param object_list, List of objects to be registered.
00089      * @return true if structure created successfully else false.
00090     */
00091     bool create_nsdl_list_structure(const M2MObjectList &object_list);
00092 
00093     /**
00094      * @brief Removed the NSDL resource for the given resource.
00095      * @param resource_name, Resource name to be removed.
00096      * @return true if removed successfully else false.
00097     */
00098     bool delete_nsdl_resource(const String &resource_name);
00099 
00100     /**
00101      * @brief Creates the bootstrap object.
00102      * @param address Bootstrap address.
00103      * @return true if created and sent successfully else false.
00104     */
00105     bool create_bootstrap_resource(sn_nsdl_addr_s *address, const String &bootstrap_endpoint_name);
00106 
00107     /**
00108      * @brief Sends the register message to the server.
00109      * @param address M2MServer address.
00110      * @param port M2MServer port.
00111      * @param address_type IP Address type.
00112      * @return  true if register sent successfully else false.
00113     */
00114     bool send_register_message(uint8_t* address,
00115                                const uint16_t port,
00116                                sn_nsdl_addr_type_e address_type);
00117 
00118     /**
00119      * @brief Sends the update registration message to the server.
00120      * @param lifetime, Updated lifetime value in seconds.
00121      * @return  true if sent successfully else false.
00122      *
00123     */
00124     bool send_update_registration(const uint32_t lifetime = 0);
00125 
00126     /**
00127      * @brief Sends unregister message to the server.
00128      * @return  true if unregister sent successfully else false.
00129     */
00130     bool send_unregister_message();
00131 
00132     /**
00133      * @brief Memory Allocation required for libCoap.
00134      * @param size, Size of memory to be reserved.
00135     */
00136     void* memory_alloc(uint16_t size);
00137 
00138     /**
00139      * @brief Memory free functions required for libCoap
00140      * @param ptr, Object whose memory needs to be freed.
00141     */
00142     void memory_free(void *ptr);
00143 
00144     /**
00145     * @brief Callback from nsdl library to inform the data is ready
00146     * to be sent to server.
00147     * @param nsdl_handle, Handler for the nsdl structure for this endpoint
00148     * @param protocol, Protocol format of the data
00149     * @param data, Data to be sent.
00150     * @param data_len, Size of the data to be sent
00151     * @param address, server address where data has to be sent.
00152     * @return 1 if successful else 0.
00153     */
00154     uint8_t send_to_server_callback(struct nsdl_s * nsdl_handle,
00155                                     sn_nsdl_capab_e protocol,
00156                                     uint8_t *data,
00157                                     uint16_t data_len,
00158                                     sn_nsdl_addr_s *address);
00159 
00160     /**
00161     * @brief Callback from nsdl library to inform the data which is
00162     * received from server for the client has been converted to coap message.
00163     * @param nsdl_handle, Handler for the nsdl structure for this endpoint
00164     * @param coap_header, Coap message formed from data.
00165     * @param address, Server address from where the data is received.
00166     * @return 1 if successful else 0.
00167     */
00168     uint8_t received_from_server_callback(struct nsdl_s * nsdl_handle,
00169                                           sn_coap_hdr_s *coap_header,
00170                                           sn_nsdl_addr_s *address);
00171 
00172     /**
00173     * @brief Callback from nsdl library to inform the data which is
00174     * received from server for the resources has been converted to coap message.
00175     * @param nsdl_handle, Handler for the nsdl resource structure for this endpoint..
00176     * @param coap_header, Coap message formed from data.
00177     * @param address, Server address from where the data is received.
00178     * @param nsdl_capab, Protocol for the message, currently only coap is supported.
00179     * @return 1 if successful else 0.
00180     */
00181     uint8_t resource_callback(struct nsdl_s *nsdl_handle, sn_coap_hdr_s *coap,
00182                                sn_nsdl_addr_s *address,
00183                                sn_nsdl_capab_e nsdl_capab);
00184 
00185     /**
00186      * @brief Callback when there is data received from server and needs to be processed.
00187      * @param data, data received from server.
00188      * @param data_size, data size received from server.
00189      * @param addres, address structure of the server.
00190      * @return true if successfully processed else false.
00191      */
00192     bool process_received_data(uint8_t *data,
00193                                uint16_t data_size,
00194                                sn_nsdl_addr_s *address);
00195 
00196     /**
00197      * @brief Stops all the timers in case there is any errors.
00198      */
00199     void stop_timers();
00200 
00201     /**
00202      * @brief Returns nsdl handle.
00203      * @return ndsl handle
00204      */
00205     nsdl_s* get_nsdl_handle();
00206 
00207     /**
00208      * @brief Get endpoint name
00209      * @return endpoint name
00210      */
00211     const String& endpoint_name() const;
00212 
00213 protected: // from M2MTimerObserver
00214 
00215     virtual void timer_expired(M2MTimerObserver::Type type);
00216 
00217 protected: // from M2MObservationHandler
00218 
00219     virtual void observation_to_be_sent(M2MBase *object,
00220                                         uint16_t obs_number,
00221                                         m2m::Vector<uint16_t> changed_instance_ids,
00222                                         bool send_object = false);
00223 
00224     virtual void resource_to_be_deleted(const String &resource_name);
00225 
00226     virtual void value_updated(M2MBase *base, const String &object_name);
00227 
00228     virtual void remove_object(M2MBase *object);
00229 
00230     virtual void send_delayed_response(M2MBase *base);
00231 
00232 private:
00233 
00234     /**
00235     * @brief Initializes all the nsdl library component to be usable.
00236     * @return true if initialization is successful else false.
00237     */
00238     bool initialize();
00239 
00240     bool add_object_to_list(M2MObject *object);
00241 
00242     bool create_nsdl_object_structure(M2MObject *object);
00243 
00244     bool create_nsdl_object_instance_structure(M2MObjectInstance *object_instance);
00245 
00246     bool create_nsdl_resource_structure(M2MResource *resource,
00247                                         const String &object_name = "",
00248                                         bool multiple_instances = false);
00249 
00250     bool create_nsdl_resource(M2MBase *base, const String &name = "", bool publish_uri = true);
00251 
00252     String coap_to_string(uint8_t *coap_data_ptr,
00253                           int coap_data_ptr_length);
00254 
00255     void execute_nsdl_process_loop();
00256 
00257     uint64_t registration_time();
00258 
00259     M2MBase* find_resource(const String &object);
00260 
00261     M2MBase* find_resource(const M2MObject *object,
00262                            const String &object_instance);
00263 
00264     M2MBase* find_resource(const M2MObjectInstance *object_instance,
00265                            const String &resource_instance);
00266 
00267     M2MBase* find_resource(const M2MResource *resource,
00268                            const String &object_name,
00269                            const String &resource_instance);
00270 
00271     bool object_present(M2MObject * object) const;
00272 
00273     void clear_resource(sn_nsdl_resource_info_s *&resource);
00274 
00275     M2MInterface::Error interface_error(sn_coap_hdr_s *coap_header);
00276 
00277     void send_object_observation(M2MObject *object,
00278                                  uint16_t obs_number,
00279                                  m2m::Vector<uint16_t> changed_instance_ids,
00280                                  bool send_object);
00281 
00282     void send_object_instance_observation(M2MObjectInstance *object_instance,
00283                                           uint16_t obs_number);
00284 
00285     void send_resource_observation(M2MResource *resource, uint16_t obs_number);
00286 
00287     void build_observation_number(uint8_t *obs_number,
00288                                   uint8_t *obs_len,
00289                                   uint16_t number);
00290 
00291     void send_notification(uint8_t *token,
00292                            uint8_t  token_length,
00293                            uint8_t *value,
00294                            uint32_t value_length,
00295                            uint16_t observation,
00296                            uint32_t max_age,
00297                            uint8_t  coap_content_type,
00298                            const String  &uri_path);
00299 
00300     /**
00301      * @brief Allocate (size + 1) amount of memory, copy size bytes into
00302      * it and add zero termination.
00303      * @param source Source string to copy, may not be NULL.
00304      * @param size The size of memory to be reserved.
00305     */
00306     uint8_t* alloc_string_copy(const uint8_t* source, uint16_t size);
00307 
00308     /**
00309      * @brief Utility method to convert given lifetime int to ascii
00310      * and allocate a buffer for it and set it to _endpoint->lifetime_ptr.
00311      * @param lifetime A new value for lifetime.
00312     */
00313     void set_endpoint_lifetime_buffer(int lifetime);
00314 
00315     /**
00316      * @brief Handle incoming bootstrap PUT message.
00317      * @param coap_header, Received CoAP message
00318      * @param address, Server address
00319     */
00320     void handle_bootstrap_put_message(sn_coap_hdr_s *coap_header, sn_nsdl_addr_s *address);
00321 
00322     /**
00323      * @brief Handle bootstrap finished message.
00324      * @param coap_header, Received CoAP message
00325      * @param address, Server address
00326     */
00327     void handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);
00328 
00329     /**
00330      * @brief Handle bootstrap delete message.
00331      * @param coap_header, Received CoAP message
00332      * @param address, Server address
00333     */
00334     void handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);
00335 
00336     /**
00337      * @brief Parse bootstrap TLV message.
00338      * @param coap_header, Received CoAP message
00339      * @return True if parsing was succesful else false
00340     */
00341     bool parse_bootstrap_message(sn_coap_hdr_s *coap_header, bool is_security_object);
00342 
00343     /**
00344      * @brief Parse bootstrap TLV message.
00345      * @param coap_header, Received CoAP message
00346      * @return True if parsing was succesful else false
00347     */
00348     bool validate_security_object();
00349 
00350     /**
00351      * @brief Handle bootstrap errors.
00352     */
00353     void handle_bootstrap_error();
00354 
00355 private:
00356 
00357     M2MNsdlObserver                   &_observer;
00358     M2MObjectList                      _object_list;
00359     M2MServer                         *_server; // Not owned
00360     M2MSecurity                       *_security; // Not owned
00361     M2MTimer                          *_nsdl_exceution_timer;
00362     M2MTimer                          *_registration_timer;
00363     sn_nsdl_ep_parameters_s           *_endpoint;
00364     sn_nsdl_resource_info_s           *_resource;
00365     sn_nsdl_addr_s                     _sn_nsdl_address;
00366     nsdl_s                            *_nsdl_handle;
00367     uint32_t                           _counter_for_nsdl;
00368     uint16_t                           _bootstrap_id;
00369     bool                               _register_ongoing;
00370     bool                               _unregister_ongoing;
00371     bool                               _update_register_ongoing;
00372     String                             _endpoint_name;
00373 
00374 friend class Test_M2MNsdlInterface;
00375 
00376 };
00377 
00378 #endif // M2MNSDLINTERFACE_H
00379