sandbox / mbed-client

Fork of mbed-client by Christopher Haster

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);
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 the bootstrap information is received from bootstrap server.
00187      * @param server_info, Server information received from bootstrap server.
00188      */
00189     void bootstrap_done_callback(sn_nsdl_oma_server_info_t *server_info);
00190 
00191     /**
00192      * @brief Callback when there is data received from server and needs to be processed.
00193      * @param data, data received from server.
00194      * @param data_size, data size received from server.
00195      * @param addres, address structure of the server.
00196      * @return true if successfully processed else false.
00197      */
00198     bool process_received_data(uint8_t *data,
00199                                uint16_t data_size,
00200                                sn_nsdl_addr_s *address);
00201 
00202     /**
00203      * @brief Stops all the timers in case there is any errors.
00204      */
00205     void stop_timers();
00206 
00207 protected: // from M2MTimerObserver
00208 
00209     virtual void timer_expired(M2MTimerObserver::Type type);
00210 
00211 protected: // from M2MObservationHandler
00212 
00213     virtual void observation_to_be_sent(M2MBase *object,
00214                                         uint16_t obs_number,
00215                                         m2m::Vector<uint16_t> changed_instance_ids,
00216                                         bool send_object = false);
00217 
00218     virtual void resource_to_be_deleted(const String &resource_name);
00219 
00220     virtual void value_updated(M2MBase *base, const String &object_name);
00221 
00222     virtual void remove_object(M2MBase *object);
00223 
00224     virtual void send_delayed_response(M2MBase *base);
00225 
00226 private:
00227 
00228     /**
00229     * @brief Initializes all the nsdl library component to be usable.
00230     * @return true if initialization is successful else false.
00231     */
00232     bool initialize();
00233 
00234     bool add_object_to_list(M2MObject *object);
00235 
00236     bool create_nsdl_object_structure(M2MObject *object);
00237 
00238     bool create_nsdl_object_instance_structure(M2MObjectInstance *object_instance);
00239 
00240     bool create_nsdl_resource_structure(M2MResource *resource,
00241                                         const String &object_name = "",
00242                                         bool multiple_instances = false);
00243 
00244     bool create_nsdl_resource(M2MBase *base, const String &name = "", bool publish_uri = true);
00245 
00246     String coap_to_string(uint8_t *coap_data_ptr,
00247                           int coap_data_ptr_length);
00248 
00249     void execute_nsdl_process_loop();
00250 
00251     uint64_t registration_time();
00252 
00253     M2MBase* find_resource(const String &object);
00254 
00255     M2MBase* find_resource(const M2MObject *object,
00256                            const String &object_instance);
00257 
00258     M2MBase* find_resource(const M2MObjectInstance *object_instance,
00259                            const String &resource_instance);
00260 
00261     M2MBase* find_resource(const M2MResource *resource,
00262                            const String &object_name,
00263                            const String &resource_instance);
00264 
00265     bool object_present(M2MObject * object) const;
00266 
00267     void clear_resource(sn_nsdl_resource_info_s *&resource);
00268 
00269     M2MInterface::Error interface_error(sn_coap_hdr_s *coap_header);
00270 
00271     void send_object_observation(M2MObject *object,
00272                                  uint16_t obs_number,
00273                                  m2m::Vector<uint16_t> changed_instance_ids,
00274                                  bool send_object);
00275 
00276     void send_object_instance_observation(M2MObjectInstance *object_instance,
00277                                           uint16_t obs_number);
00278 
00279     void send_resource_observation(M2MResource *resource, uint16_t obs_number);
00280 
00281     void build_observation_number(uint8_t *obs_number,
00282                                   uint8_t *obs_len,
00283                                   uint16_t number);
00284 
00285     void send_notification(uint8_t *token,
00286                            uint8_t  token_length,
00287                            uint8_t *value,
00288                            uint32_t value_length,
00289                            uint16_t observation,
00290                            uint32_t max_age,
00291                            uint8_t  coap_content_type,
00292                            const String  &uri_path);
00293 
00294 private:
00295 
00296     M2MNsdlObserver                   &_observer;
00297     M2MObjectList                      _object_list;
00298     M2MServer                         *_server;
00299     M2MTimer                          *_nsdl_exceution_timer;
00300     M2MTimer                          *_registration_timer;
00301     sn_nsdl_ep_parameters_s           *_endpoint;
00302     sn_nsdl_resource_info_s           *_resource;
00303     sn_nsdl_bs_ep_info_t               _bootstrap_endpoint;
00304     sn_nsdl_oma_device_t               _bootstrap_device_setup;
00305     sn_nsdl_addr_s                     _sn_nsdl_address;
00306     nsdl_s                            *_nsdl_handle;
00307     uint32_t                           _counter_for_nsdl;
00308     int32_t                            _register_id;
00309     int32_t                            _unregister_id;
00310     uint16_t                           _update_id;
00311     uint16_t                           _bootstrap_id;
00312 
00313 friend class Test_M2MNsdlInterface;
00314 
00315 };
00316 
00317 #endif // M2MNSDLINTERFACE_H