Timothy Beight / Mbed 2 deprecated 6_songs-from-the-cloud

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

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 "include/nsdllinker.h"
00025 
00026 //FORWARD DECLARARTION
00027 class M2MSecurity;
00028 class M2MObject;
00029 class M2MObjectInstance;
00030 class M2MResource;
00031 class M2MResourceInstance;
00032 class M2MNsdlObserver;
00033 class M2MBase;
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 
00215     virtual void resource_to_be_deleted(const String &resource_name);
00216 
00217     virtual void value_updated(M2MBase *base, const String &object_name);
00218 
00219     virtual void remove_object(M2MBase *object);
00220 
00221 private:
00222 
00223     /**
00224     * @brief Initializes all the nsdl library component to be usable.
00225     * @return true if initialization is successful else false.
00226     */
00227     bool initialize();
00228 
00229     bool add_object_to_list(M2MObject *object);
00230 
00231     bool create_nsdl_object_structure(M2MObject *object);
00232 
00233     bool create_nsdl_object_instance_structure(M2MObjectInstance *object_instance);
00234 
00235     bool create_nsdl_resource_structure(M2MResource *resource,
00236                                         const String &object_name = "",
00237                                         bool multiple_instances = false);
00238 
00239     bool create_nsdl_resource(M2MBase *base, const String &name = "");
00240 
00241     String coap_to_string(uint8_t *coap_data_ptr,
00242                           int coap_data_ptr_length);
00243 
00244     void execute_nsdl_process_loop();
00245 
00246     uint64_t registration_time();
00247 
00248     M2MBase* find_resource(const String &object);
00249 
00250     M2MBase* find_resource(const M2MObject *object,
00251                            const String &object_instance);
00252 
00253     M2MBase* find_resource(const M2MObjectInstance *object_instance,
00254                            const String &resource_instance);
00255 
00256     M2MBase* find_resource(const M2MResource *resource,
00257                            const String &object_name,
00258                            const String &resource_instance);
00259 
00260     bool object_present(M2MObject * object) const;
00261 
00262     void clear_resource(sn_nsdl_resource_info_s *&resource);
00263 
00264     M2MInterface::Error interface_error(sn_coap_hdr_s *coap_header);
00265 
00266     void send_object_observation(M2MObject *object);
00267 
00268     void send_object_instance_observation(M2MObjectInstance *object_instance);
00269 
00270     void send_resource_observation(M2MResource *resource);
00271 
00272 private:
00273 
00274     M2MNsdlObserver                   &_observer;
00275     M2MObjectList                      _object_list;
00276     M2MServer                         *_server;
00277     M2MTimer                          *_nsdl_exceution_timer;
00278     M2MTimer                          *_registration_timer;
00279     sn_nsdl_ep_parameters_s           *_endpoint;
00280     sn_nsdl_resource_info_s           *_resource;
00281     sn_nsdl_bs_ep_info_t               _bootstrap_endpoint;
00282     sn_nsdl_oma_device_t               _bootstrap_device_setup;
00283     sn_nsdl_addr_s                     _sn_nsdl_address;
00284     nsdl_s                            *_nsdl_handle;
00285     uint32_t                           _counter_for_nsdl;
00286     uint16_t                           _register_id;
00287     uint16_t                           _unregister_id;
00288     uint16_t                           _update_id;
00289     uint16_t                           _bootstrap_id;
00290 
00291 friend class Test_M2MNsdlInterface;
00292 
00293 };
00294 
00295 #endif // M2MNSDLINTERFACE_H