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