Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Fri Mar 20 20:15:18 2020 +0000
Revision:
2:990c985a69ae
Parent:
0:977e87915078
Update to work with P2Scan runtime

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16 #ifndef M2MNSDLINTERFACE_H
vithyat 0:977e87915078 17 #define M2MNSDLINTERFACE_H
vithyat 0:977e87915078 18
vithyat 0:977e87915078 19 #include "ns_list.h"
vithyat 0:977e87915078 20 #include "mbed-client/m2mvector.h"
vithyat 0:977e87915078 21 #include "mbed-client/m2mconfig.h"
vithyat 0:977e87915078 22 #include "mbed-client/m2minterface.h"
vithyat 0:977e87915078 23 #include "mbed-client/m2mtimerobserver.h"
vithyat 0:977e87915078 24 #include "mbed-client/m2mobservationhandler.h"
vithyat 0:977e87915078 25 #include "mbed-client/m2mtimer.h"
vithyat 0:977e87915078 26 #include "mbed-client/m2mbase.h"
vithyat 0:977e87915078 27 #include "mbed-client/m2mserver.h"
vithyat 0:977e87915078 28 #include "include/nsdllinker.h"
vithyat 0:977e87915078 29 #include "eventOS_event.h"
vithyat 0:977e87915078 30
vithyat 0:977e87915078 31 //FORWARD DECLARARTION
vithyat 0:977e87915078 32 class M2MSecurity;
vithyat 0:977e87915078 33 class M2MObject;
vithyat 0:977e87915078 34 class M2MObjectInstance;
vithyat 0:977e87915078 35 class M2MResource;
vithyat 0:977e87915078 36 class M2MResourceInstance;
vithyat 0:977e87915078 37 class M2MNsdlObserver;
vithyat 0:977e87915078 38 class M2MServer;
vithyat 0:977e87915078 39 class M2MConnectionHandler;
vithyat 0:977e87915078 40 class M2MNotificationHandler;
vithyat 0:977e87915078 41
vithyat 0:977e87915078 42 const int UNDEFINED_MSG_ID = -1;
vithyat 0:977e87915078 43
vithyat 0:977e87915078 44 /**
vithyat 0:977e87915078 45 * @brief M2MNsdlInterface
vithyat 0:977e87915078 46 * Class which interacts between mbed Client C++ Library and mbed-client-c library.
vithyat 0:977e87915078 47 */
vithyat 0:977e87915078 48 class M2MNsdlInterface : public M2MTimerObserver,
vithyat 0:977e87915078 49 public M2MObservationHandler
vithyat 0:977e87915078 50 {
vithyat 0:977e87915078 51 private:
vithyat 0:977e87915078 52 // Prevents the use of assignment operator by accident.
vithyat 0:977e87915078 53 M2MNsdlInterface& operator=( const M2MNsdlInterface& /*other*/ );
vithyat 0:977e87915078 54
vithyat 0:977e87915078 55 // Prevents the use of copy constructor by accident
vithyat 0:977e87915078 56 M2MNsdlInterface( const M2MNsdlInterface& /*other*/ );
vithyat 0:977e87915078 57
vithyat 0:977e87915078 58 public:
vithyat 0:977e87915078 59
vithyat 0:977e87915078 60 struct request_context_s {
vithyat 0:977e87915078 61 request_data_cb on_request_data_cb;
vithyat 0:977e87915078 62 request_error_cb on_request_error_cb;
vithyat 0:977e87915078 63 size_t received_size;
vithyat 0:977e87915078 64 uint32_t msg_token;
vithyat 0:977e87915078 65 char *uri_path;
vithyat 0:977e87915078 66 void *context;
vithyat 0:977e87915078 67 bool async_req;
vithyat 0:977e87915078 68 sn_coap_msg_code_e msg_code;
vithyat 0:977e87915078 69 bool resend;
vithyat 0:977e87915078 70 DownloadType download_type;
vithyat 0:977e87915078 71 ns_list_link_t link;
vithyat 0:977e87915078 72 };
vithyat 0:977e87915078 73
vithyat 0:977e87915078 74 struct nsdl_coap_data_s {
vithyat 0:977e87915078 75 nsdl_s *nsdl_handle;
vithyat 0:977e87915078 76 sn_coap_hdr_s *received_coap_header;
vithyat 0:977e87915078 77 sn_nsdl_addr_s address;
vithyat 0:977e87915078 78 };
vithyat 0:977e87915078 79
vithyat 0:977e87915078 80 struct coap_response_s {
vithyat 0:977e87915078 81 char *uri_path;
vithyat 0:977e87915078 82 int32_t msg_id;
vithyat 0:977e87915078 83 M2MBase::MessageType type;
vithyat 0:977e87915078 84 bool blockwise_used;
vithyat 0:977e87915078 85 ns_list_link_t link;
vithyat 0:977e87915078 86 };
vithyat 0:977e87915078 87
vithyat 0:977e87915078 88 typedef NS_LIST_HEAD(request_context_s, link) request_context_list_t;
vithyat 0:977e87915078 89
vithyat 0:977e87915078 90 typedef NS_LIST_HEAD(coap_response_s, link) response_list_t;
vithyat 0:977e87915078 91
vithyat 0:977e87915078 92 /**
vithyat 0:977e87915078 93 * @brief Constructor
vithyat 0:977e87915078 94 * @param observer, Observer to pass the event callbacks from nsdl library.
vithyat 0:977e87915078 95 */
vithyat 0:977e87915078 96 M2MNsdlInterface(M2MNsdlObserver &observer, M2MConnectionHandler &connection_handler);
vithyat 0:977e87915078 97
vithyat 0:977e87915078 98 /**
vithyat 0:977e87915078 99 * @brief Destructor
vithyat 0:977e87915078 100 */
vithyat 0:977e87915078 101 virtual ~M2MNsdlInterface();
vithyat 0:977e87915078 102
vithyat 0:977e87915078 103 /**
vithyat 0:977e87915078 104 * @brief Creates endpoint object for the nsdl stack.
vithyat 0:977e87915078 105 * @param endpoint_name, Endpoint name of the client.
vithyat 0:977e87915078 106 * @param endpoint_type, Endpoint type of the client.
vithyat 0:977e87915078 107 * @param life_time, Life time of the client in seconds
vithyat 0:977e87915078 108 * @param domain, Domain of the client.
vithyat 0:977e87915078 109 * @param mode, Binding mode of the client, default is UDP
vithyat 0:977e87915078 110 * @param context_address, Context address default is empty.
vithyat 0:977e87915078 111 */
vithyat 0:977e87915078 112 void create_endpoint(const String &endpoint_name,
vithyat 0:977e87915078 113 const String &endpoint_type,
vithyat 0:977e87915078 114 const int32_t life_time,
vithyat 0:977e87915078 115 const String &domain,
vithyat 0:977e87915078 116 const uint8_t mode,
vithyat 0:977e87915078 117 const String &context_address);
vithyat 0:977e87915078 118
vithyat 0:977e87915078 119 /**
vithyat 0:977e87915078 120 * @brief Deletes the endpoint.
vithyat 0:977e87915078 121 */
vithyat 0:977e87915078 122 void delete_endpoint();
vithyat 0:977e87915078 123
vithyat 0:977e87915078 124 /**
vithyat 0:977e87915078 125 * @brief Updates endpoint name.
vithyat 0:977e87915078 126 */
vithyat 0:977e87915078 127 void update_endpoint(const String &name);
vithyat 0:977e87915078 128
vithyat 0:977e87915078 129 /**
vithyat 0:977e87915078 130 * @brief Updates domain.
vithyat 0:977e87915078 131 */
vithyat 0:977e87915078 132 void update_domain(const String &domain);
vithyat 0:977e87915078 133
vithyat 0:977e87915078 134 /**
vithyat 0:977e87915078 135 * @brief Creates the NSDL structure for the registered objectlist.
vithyat 0:977e87915078 136 * @param list, List of objects implementing the M2MBase interface to be registered.
vithyat 0:977e87915078 137 * @return true if structure created successfully else false.
vithyat 0:977e87915078 138 */
vithyat 0:977e87915078 139 bool create_nsdl_list_structure(const M2MBaseList &list);
vithyat 0:977e87915078 140
vithyat 0:977e87915078 141 /**
vithyat 0:977e87915078 142 * @brief Removed the NSDL resource for the given resource.
vithyat 0:977e87915078 143 * @param base, Resource to be removed.
vithyat 0:977e87915078 144 * @return true if removed successfully else false.
vithyat 0:977e87915078 145 */
vithyat 0:977e87915078 146 bool remove_nsdl_resource(M2MBase *base);
vithyat 0:977e87915078 147
vithyat 0:977e87915078 148 /**
vithyat 0:977e87915078 149 * @brief Creates the bootstrap object.
vithyat 0:977e87915078 150 * @param address Bootstrap address.
vithyat 0:977e87915078 151 * @return true if created and sent successfully else false.
vithyat 0:977e87915078 152 */
vithyat 0:977e87915078 153 bool create_bootstrap_resource(sn_nsdl_addr_s *address);
vithyat 0:977e87915078 154
vithyat 0:977e87915078 155 /**
vithyat 0:977e87915078 156 * @brief Sets the register message to the server.
vithyat 0:977e87915078 157 * @param address M2MServer address.
vithyat 0:977e87915078 158 * @param address_length M2MServer address length.
vithyat 0:977e87915078 159 * @param port M2MServer port.
vithyat 0:977e87915078 160 * @param address_type IP Address type.
vithyat 0:977e87915078 161 */
vithyat 0:977e87915078 162 void set_server_address(uint8_t* address,
vithyat 0:977e87915078 163 uint8_t address_length,
vithyat 0:977e87915078 164 const uint16_t port,
vithyat 0:977e87915078 165 sn_nsdl_addr_type_e address_type);
vithyat 0:977e87915078 166 /**
vithyat 0:977e87915078 167 * @brief Sends the register message to the server.
vithyat 0:977e87915078 168 * @return true if register sent successfully else false.
vithyat 0:977e87915078 169 */
vithyat 0:977e87915078 170 bool send_register_message();
vithyat 0:977e87915078 171
vithyat 0:977e87915078 172 /**
vithyat 0:977e87915078 173 * @brief Sends the CoAP request to the server.
vithyat 0:977e87915078 174 * @type Download type.
vithyat 0:977e87915078 175 * @uri Uri path to the data.
vithyat 0:977e87915078 176 * @msg_code CoAP message code of request to send.
vithyat 0:977e87915078 177 * @offset Data offset.
vithyat 0:977e87915078 178 * @async In async mode application must call this API again with the updated offset.
vithyat 0:977e87915078 179 * If set to false then client will automatically download the whole package.
vithyat 0:977e87915078 180 * @token The token to use for the request, 0 value will generate new token.
vithyat 0:977e87915078 181 * @payload_len Length of payload buffer.
vithyat 0:977e87915078 182 * @payload_ptr Pointer to payload buffer.
vithyat 0:977e87915078 183 * @request_data_cb Callback which is triggered once there is data available.
vithyat 0:977e87915078 184 * @request_error_cb Callback which is trigged in case of any error.
vithyat 0:977e87915078 185 * @context Application context.
vithyat 0:977e87915078 186 */
vithyat 0:977e87915078 187 void send_request(DownloadType type,
vithyat 0:977e87915078 188 const char *uri,
vithyat 0:977e87915078 189 const sn_coap_msg_code_e msg_code,
vithyat 0:977e87915078 190 const size_t offset,
vithyat 0:977e87915078 191 const bool async,
vithyat 0:977e87915078 192 uint32_t token,
vithyat 0:977e87915078 193 const uint16_t payload_len,
vithyat 0:977e87915078 194 uint8_t *payload_ptr,
vithyat 0:977e87915078 195 request_data_cb data_cb,
vithyat 0:977e87915078 196 request_error_cb error_cb,
vithyat 0:977e87915078 197 void *context);
vithyat 0:977e87915078 198
vithyat 0:977e87915078 199 /**
vithyat 0:977e87915078 200 * @brief Sends the update registration message to the server.
vithyat 0:977e87915078 201 * @param lifetime, Updated lifetime value in seconds.
vithyat 0:977e87915078 202 * @return true if sent successfully else false.
vithyat 0:977e87915078 203 *
vithyat 0:977e87915078 204 */
vithyat 0:977e87915078 205 bool send_update_registration(const uint32_t lifetime = 0);
vithyat 0:977e87915078 206
vithyat 0:977e87915078 207 /**
vithyat 0:977e87915078 208 * @brief Sends unregister message to the server.
vithyat 0:977e87915078 209 * @return true if unregister sent successfully else false.
vithyat 0:977e87915078 210 */
vithyat 0:977e87915078 211 bool send_unregister_message();
vithyat 0:977e87915078 212
vithyat 0:977e87915078 213 /**
vithyat 0:977e87915078 214 * @brief Memory Allocation required for libCoap.
vithyat 0:977e87915078 215 * @param size, Size of memory to be reserved.
vithyat 0:977e87915078 216 */
vithyat 0:977e87915078 217 static void* memory_alloc(uint32_t size);
vithyat 0:977e87915078 218
vithyat 0:977e87915078 219 /**
vithyat 0:977e87915078 220 * @brief Memory free functions required for libCoap
vithyat 0:977e87915078 221 * @param ptr, Object whose memory needs to be freed.
vithyat 0:977e87915078 222 */
vithyat 0:977e87915078 223 static void memory_free(void *ptr);
vithyat 0:977e87915078 224
vithyat 0:977e87915078 225 /**
vithyat 0:977e87915078 226 * @brief Callback from nsdl library to inform the data is ready
vithyat 0:977e87915078 227 * to be sent to server.
vithyat 0:977e87915078 228 * @param nsdl_handle, Handler for the nsdl structure for this endpoint
vithyat 0:977e87915078 229 * @param protocol, Protocol format of the data
vithyat 0:977e87915078 230 * @param data, Data to be sent.
vithyat 0:977e87915078 231 * @param data_len, Size of the data to be sent
vithyat 0:977e87915078 232 * @param address, server address where data has to be sent.
vithyat 0:977e87915078 233 * @return 1 if successful else 0.
vithyat 0:977e87915078 234 */
vithyat 0:977e87915078 235 uint8_t send_to_server_callback(struct nsdl_s * nsdl_handle,
vithyat 0:977e87915078 236 sn_nsdl_capab_e protocol,
vithyat 0:977e87915078 237 uint8_t *data,
vithyat 0:977e87915078 238 uint16_t data_len,
vithyat 0:977e87915078 239 sn_nsdl_addr_s *address);
vithyat 0:977e87915078 240
vithyat 0:977e87915078 241 /**
vithyat 0:977e87915078 242 * @brief Callback from nsdl library to inform the data which is
vithyat 0:977e87915078 243 * received from server for the client has been converted to coap message.
vithyat 0:977e87915078 244 * @param nsdl_handle, Handler for the nsdl structure for this endpoint
vithyat 0:977e87915078 245 * @param coap_header, Coap message formed from data.
vithyat 0:977e87915078 246 * @param address, Server address from where the data is received.
vithyat 0:977e87915078 247 * @return 1 if successful else 0.
vithyat 0:977e87915078 248 */
vithyat 0:977e87915078 249 uint8_t received_from_server_callback(struct nsdl_s * nsdl_handle,
vithyat 0:977e87915078 250 sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 251 sn_nsdl_addr_s *address);
vithyat 0:977e87915078 252
vithyat 0:977e87915078 253 /**
vithyat 0:977e87915078 254 * @brief Callback from nsdl library to inform the data which is
vithyat 0:977e87915078 255 * received from server for the resources has been converted to coap message.
vithyat 0:977e87915078 256 * @param nsdl_handle, Handler for the nsdl resource structure for this endpoint..
vithyat 0:977e87915078 257 * @param coap_header, Coap message formed from data.
vithyat 0:977e87915078 258 * @param address, Server address from where the data is received.
vithyat 0:977e87915078 259 * @param nsdl_capab, Protocol for the message, currently only coap is supported.
vithyat 0:977e87915078 260 * @return 1 if successful else 0.
vithyat 0:977e87915078 261 */
vithyat 0:977e87915078 262 uint8_t resource_callback(struct nsdl_s *nsdl_handle, sn_coap_hdr_s *coap,
vithyat 0:977e87915078 263 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 264 sn_nsdl_capab_e nsdl_capab);
vithyat 0:977e87915078 265
vithyat 0:977e87915078 266 /**
vithyat 0:977e87915078 267 * @brief Callback from event loop for handling CoAP messages received from server for the resources
vithyat 0:977e87915078 268 * that has been converted to coap message.
vithyat 0:977e87915078 269 * @param coap_header, Coap message formed from data.
vithyat 0:977e87915078 270 * @param address, Server address from where the data is received.
vithyat 0:977e87915078 271 * @return 0 if successful else 1.
vithyat 0:977e87915078 272 */
vithyat 0:977e87915078 273 uint8_t resource_callback_handle_event(sn_coap_hdr_s *coap,
vithyat 0:977e87915078 274 sn_nsdl_addr_s *address);
vithyat 0:977e87915078 275
vithyat 0:977e87915078 276
vithyat 0:977e87915078 277 /**
vithyat 0:977e87915078 278 * @brief Callback when there is data received from server and needs to be processed.
vithyat 0:977e87915078 279 * @param data, data received from server.
vithyat 0:977e87915078 280 * @param data_size, data size received from server.
vithyat 0:977e87915078 281 * @param addres, address structure of the server.
vithyat 0:977e87915078 282 * @return true if successfully processed else false.
vithyat 0:977e87915078 283 */
vithyat 0:977e87915078 284 bool process_received_data(uint8_t *data,
vithyat 0:977e87915078 285 uint16_t data_size,
vithyat 0:977e87915078 286 sn_nsdl_addr_s *address);
vithyat 0:977e87915078 287
vithyat 0:977e87915078 288 /**
vithyat 0:977e87915078 289 * @brief Stops all the timers in case there is any errors.
vithyat 0:977e87915078 290 */
vithyat 0:977e87915078 291 void stop_timers();
vithyat 0:977e87915078 292
vithyat 0:977e87915078 293 /**
vithyat 0:977e87915078 294 * @brief Returns nsdl handle.
vithyat 0:977e87915078 295 * @return ndsl handle
vithyat 0:977e87915078 296 */
vithyat 0:977e87915078 297 nsdl_s* get_nsdl_handle() const;
vithyat 0:977e87915078 298
vithyat 0:977e87915078 299 /**
vithyat 0:977e87915078 300 * @brief Get endpoint name
vithyat 0:977e87915078 301 * @return endpoint name
vithyat 0:977e87915078 302 */
vithyat 0:977e87915078 303 const String& endpoint_name() const;
vithyat 0:977e87915078 304
vithyat 0:977e87915078 305 /**
vithyat 0:977e87915078 306 * @brief Get internal endpoint name
vithyat 0:977e87915078 307 * @return internal endpoint name
vithyat 0:977e87915078 308 */
vithyat 0:977e87915078 309 const String internal_endpoint_name() const;
vithyat 0:977e87915078 310
vithyat 0:977e87915078 311 /**
vithyat 0:977e87915078 312 * @brief Set server address
vithyat 0:977e87915078 313 * @param server_address, Bootstrap or M2M server address.
vithyat 0:977e87915078 314 */
vithyat 0:977e87915078 315 void set_server_address(const char *server_address);
vithyat 0:977e87915078 316
vithyat 0:977e87915078 317 /**
vithyat 0:977e87915078 318 * @brief Remove an object from the list kept by the NSDLInteface.
vithyat 0:977e87915078 319 * Does not call delete on the object.
vithyat 0:977e87915078 320 */
vithyat 0:977e87915078 321 bool remove_object_from_list(M2MBase *base);
vithyat 0:977e87915078 322
vithyat 0:977e87915078 323 /*
vithyat 0:977e87915078 324 * @brief Get NSDL timer.
vithyat 0:977e87915078 325 * @return NSDL execution timer.
vithyat 0:977e87915078 326 */
vithyat 0:977e87915078 327 M2MTimer &get_nsdl_execution_timer();
vithyat 0:977e87915078 328
vithyat 0:977e87915078 329 /**
vithyat 0:977e87915078 330 * @brief Get unregister state.
vithyat 0:977e87915078 331 * @return Is unregistration ongoing.
vithyat 0:977e87915078 332 */
vithyat 0:977e87915078 333 bool is_unregister_ongoing() const;
vithyat 0:977e87915078 334
vithyat 0:977e87915078 335 /**
vithyat 0:977e87915078 336 * @brief Get update register state.
vithyat 0:977e87915078 337 * @return Is updare registration ongoing.
vithyat 0:977e87915078 338 */
vithyat 0:977e87915078 339 bool is_update_register_ongoing() const;
vithyat 0:977e87915078 340
vithyat 0:977e87915078 341 /**
vithyat 0:977e87915078 342 * @brief Starts the NSDL execution timer.
vithyat 0:977e87915078 343 */
vithyat 0:977e87915078 344 void start_nsdl_execution_timer();
vithyat 0:977e87915078 345
vithyat 0:977e87915078 346 /**
vithyat 0:977e87915078 347 * @brief Returns security object.
vithyat 0:977e87915078 348 * @return M2MSecurity object, contains lwm2m server information.
vithyat 0:977e87915078 349 */
vithyat 0:977e87915078 350 M2MSecurity* get_security_object();
vithyat 0:977e87915078 351
vithyat 0:977e87915078 352 /**
vithyat 0:977e87915078 353 * @brief Returns auto-observation token.
vithyat 0:977e87915078 354 * @param path, Resource path, used for searching the right object.
vithyat 0:977e87915078 355 * @param token[OUT], Token data.
vithyat 0:977e87915078 356 * @return Length of the token if found otherwise 0.
vithyat 0:977e87915078 357 */
vithyat 0:977e87915078 358 uint8_t find_auto_obs_token(const char *path, uint8_t *token) const;
vithyat 0:977e87915078 359
vithyat 0:977e87915078 360 /**
vithyat 0:977e87915078 361 * @brief Set custom uri query paramaters used in LWM2M registration.
vithyat 0:977e87915078 362 * @uri_query_params Uri query params. Parameters must be in key-value pair format:
vithyat 0:977e87915078 363 * "a=100&b=200". Maximum length can be up to 64 bytes.
vithyat 0:977e87915078 364 * @return False if maximum length exceeded otherwise True.
vithyat 0:977e87915078 365 */
vithyat 0:977e87915078 366 bool set_uri_query_parameters(const char *uri_query_params);
vithyat 0:977e87915078 367
vithyat 0:977e87915078 368 /**
vithyat 0:977e87915078 369 * @brief Clears the sent blockwise message list in CoAP library.
vithyat 0:977e87915078 370 */
vithyat 0:977e87915078 371 void clear_sent_blockwise_messages();
vithyat 0:977e87915078 372
vithyat 0:977e87915078 373 /**
vithyat 0:977e87915078 374 * @brief Clears the received blockwise message list in CoAP library.
vithyat 0:977e87915078 375 */
vithyat 0:977e87915078 376 void clear_received_blockwise_messages();
vithyat 0:977e87915078 377
vithyat 0:977e87915078 378 /**
vithyat 0:977e87915078 379 * @brief Send next notification message.
vithyat 0:977e87915078 380 */
vithyat 0:977e87915078 381 void send_next_notification(bool clear_token);
vithyat 0:977e87915078 382
vithyat 0:977e87915078 383 /**
vithyat 0:977e87915078 384 * @brief Store the "BS finished" response id.
vithyat 0:977e87915078 385 * @param msg_id Response id.
vithyat 0:977e87915078 386 */
vithyat 0:977e87915078 387 void store_bs_finished_response_id(uint16_t msg_id);
vithyat 0:977e87915078 388
vithyat 0:977e87915078 389 /**
vithyat 0:977e87915078 390 * @brief Store the registration state.
vithyat 0:977e87915078 391 * @param registered Registered to lwm2m server or not.
vithyat 0:977e87915078 392 */
vithyat 0:977e87915078 393 void set_registration_status(bool registered);
vithyat 0:977e87915078 394
vithyat 0:977e87915078 395 /**
vithyat 0:977e87915078 396 * @brief Handle incoming bootstrap PUT message.
vithyat 0:977e87915078 397 * @param coap_header, Received CoAP message
vithyat 0:977e87915078 398 * @param address, Server address
vithyat 0:977e87915078 399 */
vithyat 0:977e87915078 400 void handle_bootstrap_put_message(sn_coap_hdr_s *coap_header, sn_nsdl_addr_s *address);
vithyat 0:977e87915078 401
vithyat 0:977e87915078 402 /**
vithyat 0:977e87915078 403 * @brief Handle bootstrap finish acknowledgement.
vithyat 0:977e87915078 404 */
vithyat 0:977e87915078 405 void handle_bootstrap_finish_ack(uint16_t msg_id);
vithyat 0:977e87915078 406
vithyat 0:977e87915078 407 /**
vithyat 0:977e87915078 408 * @brief Returns total retransmission time
vithyat 0:977e87915078 409 * @resend_count Resend count
vithyat 0:977e87915078 410 * @return Total retransmission time
vithyat 0:977e87915078 411 */
vithyat 0:977e87915078 412 uint32_t total_retransmission_time(uint32_t resend_count);
vithyat 0:977e87915078 413
vithyat 0:977e87915078 414 /**
vithyat 0:977e87915078 415 * @brief Returns CoAP retransmission count
vithyat 0:977e87915078 416 * @return CoAP retransmission count
vithyat 0:977e87915078 417 */
vithyat 0:977e87915078 418 uint8_t get_resend_count();
vithyat 0:977e87915078 419
vithyat 0:977e87915078 420 /**
vithyat 0:977e87915078 421 * @brief Mark request to be resend again after network break
vithyat 0:977e87915078 422 * @param token, Message token
vithyat 0:977e87915078 423 * @param token_len, Message token length
vithyat 0:977e87915078 424 */
vithyat 0:977e87915078 425 void set_request_context_to_be_resend(uint8_t *token, uint8_t token_len);
vithyat 0:977e87915078 426
vithyat 0:977e87915078 427 /**
vithyat 0:977e87915078 428 * @brief Create a new time when to send CoAP ping.
vithyat 0:977e87915078 429 */
vithyat 0:977e87915078 430 void calculate_new_coap_ping_send_time();
vithyat 0:977e87915078 431
vithyat 0:977e87915078 432 protected: // from M2MTimerObserver
vithyat 0:977e87915078 433
vithyat 0:977e87915078 434 virtual void timer_expired(M2MTimerObserver::Type type);
vithyat 0:977e87915078 435
vithyat 0:977e87915078 436 protected: // from M2MObservationHandler
vithyat 0:977e87915078 437
vithyat 0:977e87915078 438 virtual bool observation_to_be_sent(M2MBase *object,
vithyat 0:977e87915078 439 uint16_t obs_number,
vithyat 0:977e87915078 440 const m2m::Vector<uint16_t> &changed_instance_ids,
vithyat 0:977e87915078 441 bool send_object = false);
vithyat 0:977e87915078 442
vithyat 0:977e87915078 443 virtual void resource_to_be_deleted(M2MBase* base);
vithyat 0:977e87915078 444
vithyat 0:977e87915078 445 virtual void value_updated(M2MBase *base);
vithyat 0:977e87915078 446
vithyat 0:977e87915078 447 virtual void remove_object(M2MBase *object);
vithyat 0:977e87915078 448 #ifndef DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 449 virtual void send_delayed_response(M2MBase *base);
vithyat 0:977e87915078 450 #endif //DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 451
vithyat 0:977e87915078 452 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 453 virtual void send_asynchronous_response(M2MBase *base,
vithyat 0:977e87915078 454 const uint8_t *payload,
vithyat 0:977e87915078 455 size_t payload_len,
vithyat 0:977e87915078 456 const uint8_t* token,
vithyat 0:977e87915078 457 const uint8_t token_len,
vithyat 0:977e87915078 458 coap_response_code_e code);
vithyat 0:977e87915078 459 #endif //ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 460
vithyat 0:977e87915078 461 private:
vithyat 0:977e87915078 462
vithyat 0:977e87915078 463 /**
vithyat 0:977e87915078 464 * Enum defining an LWM2M object type.
vithyat 0:977e87915078 465 */
vithyat 0:977e87915078 466 typedef enum {
vithyat 0:977e87915078 467 SECURITY = 0x00,
vithyat 0:977e87915078 468 SERVER = 0x01,
vithyat 0:977e87915078 469 DEVICE = 0x02
vithyat 0:977e87915078 470 }ObjectType;
vithyat 0:977e87915078 471
vithyat 0:977e87915078 472 /**
vithyat 0:977e87915078 473 * @brief Initializes all the nsdl library component to be usable.
vithyat 0:977e87915078 474 * @return true if initialization is successful else false.
vithyat 0:977e87915078 475 */
vithyat 0:977e87915078 476 bool initialize();
vithyat 0:977e87915078 477
vithyat 0:977e87915078 478 bool add_object_to_list(M2MBase *base);
vithyat 0:977e87915078 479
vithyat 0:977e87915078 480 bool create_nsdl_structure(M2MBase *base);
vithyat 0:977e87915078 481
vithyat 0:977e87915078 482 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 483 bool create_nsdl_endpoint_structure(M2MEndpoint *endpoint);
vithyat 0:977e87915078 484 #endif
vithyat 0:977e87915078 485
vithyat 0:977e87915078 486 bool create_nsdl_object_structure(M2MObject *object);
vithyat 0:977e87915078 487
vithyat 0:977e87915078 488 bool create_nsdl_object_instance_structure(M2MObjectInstance *object_instance);
vithyat 0:977e87915078 489
vithyat 0:977e87915078 490 bool create_nsdl_resource_structure(M2MResource *resource,
vithyat 0:977e87915078 491 bool multiple_instances = false);
vithyat 0:977e87915078 492
vithyat 0:977e87915078 493 bool create_nsdl_resource(M2MBase *base);
vithyat 0:977e87915078 494
vithyat 0:977e87915078 495 static String coap_to_string(const uint8_t *coap_data_ptr,
vithyat 0:977e87915078 496 int coap_data_ptr_length);
vithyat 0:977e87915078 497
vithyat 0:977e87915078 498 void execute_nsdl_process_loop();
vithyat 0:977e87915078 499
vithyat 0:977e87915078 500 uint64_t registration_time() const;
vithyat 0:977e87915078 501
vithyat 0:977e87915078 502 M2MBase* find_resource(const String &object) const;
vithyat 0:977e87915078 503
vithyat 0:977e87915078 504 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 505 M2MBase* find_resource(const M2MEndpoint *endpoint,
vithyat 0:977e87915078 506 const String &object_name) const;
vithyat 0:977e87915078 507 #endif
vithyat 0:977e87915078 508
vithyat 0:977e87915078 509 M2MBase* find_resource(const M2MObject *object,
vithyat 0:977e87915078 510 const String &object_instance) const;
vithyat 0:977e87915078 511
vithyat 0:977e87915078 512 M2MBase* find_resource(const M2MObjectInstance *object_instance,
vithyat 0:977e87915078 513 const String &resource_instance) const;
vithyat 0:977e87915078 514
vithyat 0:977e87915078 515 M2MBase* find_resource(const M2MResource *resource,
vithyat 0:977e87915078 516 const String &object_name,
vithyat 0:977e87915078 517 const String &resource_instance) const;
vithyat 0:977e87915078 518
vithyat 0:977e87915078 519 bool object_present(M2MBase *base) const;
vithyat 0:977e87915078 520
vithyat 0:977e87915078 521 int object_index(M2MBase *base) const;
vithyat 0:977e87915078 522
vithyat 0:977e87915078 523 static M2MInterface::Error interface_error(const sn_coap_hdr_s &coap_header);
vithyat 0:977e87915078 524
vithyat 0:977e87915078 525 void send_object_observation(M2MObject *object,
vithyat 0:977e87915078 526 uint16_t obs_number,
vithyat 0:977e87915078 527 const m2m::Vector<uint16_t> &changed_instance_ids,
vithyat 0:977e87915078 528 bool send_object);
vithyat 0:977e87915078 529
vithyat 0:977e87915078 530 void send_object_instance_observation(M2MObjectInstance *object_instance,
vithyat 0:977e87915078 531 uint16_t obs_number);
vithyat 0:977e87915078 532
vithyat 0:977e87915078 533 void send_resource_observation(M2MResource *resource, uint16_t obs_number);
vithyat 0:977e87915078 534
vithyat 0:977e87915078 535
vithyat 0:977e87915078 536
vithyat 0:977e87915078 537 /**
vithyat 0:977e87915078 538 * @brief Allocate (size + 1) amount of memory, copy size bytes into
vithyat 0:977e87915078 539 * it and add zero termination.
vithyat 0:977e87915078 540 * @param source Source string to copy, may not be NULL.
vithyat 0:977e87915078 541 * @param size The size of memory to be reserved.
vithyat 0:977e87915078 542 */
vithyat 0:977e87915078 543 static uint8_t* alloc_string_copy(const uint8_t* source, uint16_t size);
vithyat 0:977e87915078 544
vithyat 0:977e87915078 545 /**
vithyat 0:977e87915078 546 * @brief Utility method to convert given lifetime int to ascii
vithyat 0:977e87915078 547 * and allocate a buffer for it and set it to _endpoint->lifetime_ptr.
vithyat 0:977e87915078 548 * @param lifetime A new value for lifetime.
vithyat 0:977e87915078 549 */
vithyat 0:977e87915078 550 void set_endpoint_lifetime_buffer(int lifetime);
vithyat 0:977e87915078 551
vithyat 0:977e87915078 552 /**
vithyat 0:977e87915078 553 * @brief Handle bootstrap finished message.
vithyat 0:977e87915078 554 * @param coap_header, Received CoAP message
vithyat 0:977e87915078 555 * @param address, Server address
vithyat 0:977e87915078 556 */
vithyat 0:977e87915078 557 void handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);
vithyat 0:977e87915078 558
vithyat 0:977e87915078 559 /**
vithyat 0:977e87915078 560 * @brief Handle bootstrap delete message.
vithyat 0:977e87915078 561 * @param coap_header, Received CoAP message
vithyat 0:977e87915078 562 * @param address, Server address
vithyat 0:977e87915078 563 */
vithyat 0:977e87915078 564 void handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address);
vithyat 0:977e87915078 565
vithyat 0:977e87915078 566 /**
vithyat 0:977e87915078 567 * @brief Parse bootstrap TLV message.
vithyat 0:977e87915078 568 * @param coap_header, Received CoAP message
vithyat 0:977e87915078 569 * @return True if parsing was succesful else false
vithyat 0:977e87915078 570 */
vithyat 0:977e87915078 571 bool parse_bootstrap_message(sn_coap_hdr_s *coap_header, M2MNsdlInterface::ObjectType lwm2m_object_type);
vithyat 0:977e87915078 572
vithyat 0:977e87915078 573 /**
vithyat 0:977e87915078 574 * @brief Parse bootstrap TLV message.
vithyat 0:977e87915078 575 * @param coap_header, Received CoAP message
vithyat 0:977e87915078 576 * @return True if parsing was succesful else false
vithyat 0:977e87915078 577 */
vithyat 0:977e87915078 578 bool validate_security_object();
vithyat 0:977e87915078 579
vithyat 0:977e87915078 580 /**
vithyat 0:977e87915078 581 * @brief Handle bootstrap errors.
vithyat 0:977e87915078 582 * @param reason, Reason for Bootstrap failure.
vithyat 0:977e87915078 583 * @param wait, True if need to wait that ACK has been sent.
vithyat 0:977e87915078 584 * False if reconnection can start immediately.
vithyat 0:977e87915078 585 */
vithyat 0:977e87915078 586 void handle_bootstrap_error(const char *reason, bool wait);
vithyat 0:977e87915078 587
vithyat 0:977e87915078 588 /**
vithyat 0:977e87915078 589 * @brief Handle different coap errors.
vithyat 0:977e87915078 590 * @param coap_header, CoAP structure.
vithyat 0:977e87915078 591 * @return Error reason.
vithyat 0:977e87915078 592 */
vithyat 0:977e87915078 593 static const char *coap_error(const sn_coap_hdr_s &coap_header);
vithyat 0:977e87915078 594
vithyat 0:977e87915078 595 /**
vithyat 0:977e87915078 596 * @brief Claim
vithyat 0:977e87915078 597 */
vithyat 0:977e87915078 598 void claim_mutex();
vithyat 0:977e87915078 599
vithyat 0:977e87915078 600 /**
vithyat 0:977e87915078 601 * @brief Release
vithyat 0:977e87915078 602 */
vithyat 0:977e87915078 603 void release_mutex();
vithyat 0:977e87915078 604
vithyat 0:977e87915078 605 /**
vithyat 0:977e87915078 606 * @brief Change operation mode of every resource.
vithyat 0:977e87915078 607 * @param object, Object to be updated.
vithyat 0:977e87915078 608 * @return operation, New operation mode.
vithyat 0:977e87915078 609 */
vithyat 0:977e87915078 610 void change_operation_mode(M2MObject *object, M2MBase::Operation operation);
vithyat 0:977e87915078 611
vithyat 0:977e87915078 612 /**
vithyat 0:977e87915078 613 * @brief Parse URI query parameters and pass those to nsdl-c.
vithyat 0:977e87915078 614 * @return True if parsing success otherwise False
vithyat 0:977e87915078 615 */
vithyat 0:977e87915078 616 bool parse_and_send_uri_query_parameters();
vithyat 0:977e87915078 617
vithyat 0:977e87915078 618 /**
vithyat 0:977e87915078 619 * @brief Callback function that triggers the registration update call.
vithyat 0:977e87915078 620 * @param argument, Arguments part of the POST request.
vithyat 0:977e87915078 621 */
vithyat 0:977e87915078 622 void update_trigger_callback(void *argument);
vithyat 0:977e87915078 623
vithyat 0:977e87915078 624 bool lifetime_value_changed() const;
vithyat 0:977e87915078 625
vithyat 0:977e87915078 626 void execute_notification_delivery_status_cb(M2MBase* object, int32_t msgid);
vithyat 0:977e87915078 627
vithyat 0:977e87915078 628 bool is_response_to_request(const sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 629 struct request_context_s &get_data);
vithyat 0:977e87915078 630
vithyat 0:977e87915078 631 void free_request_context_list(const sn_coap_hdr_s *coap_header, bool call_error_cb, request_error_t error_code = FAILED_TO_SEND_MSG);
vithyat 0:977e87915078 632
vithyat 0:977e87915078 633 void free_response_list();
vithyat 0:977e87915078 634
vithyat 0:977e87915078 635 void remove_item_from_response_list(const char* uri_path, const int32_t msg_id);
vithyat 0:977e87915078 636
vithyat 0:977e87915078 637 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 638 void remove_items_from_response_list_for_uri(const char* uri_path);
vithyat 0:977e87915078 639 #endif
vithyat 0:977e87915078 640 /**
vithyat 0:977e87915078 641 * @brief Send next notification for object, return true if notification sent, false
vithyat 0:977e87915078 642 * if no notification to send or send already in progress.
vithyat 0:977e87915078 643 * @param object, M2MObject whose next notification should be sent
vithyat 0:977e87915078 644 * @param clear_token, Flag to indicate whether observation token should be cleared.
vithyat 0:977e87915078 645 * @return True if notification sent, false otherwise or if send already in progress
vithyat 0:977e87915078 646 */
vithyat 0:977e87915078 647 bool send_next_notification_for_object(M2MObject& object, bool clear_token);
vithyat 0:977e87915078 648
vithyat 0:977e87915078 649 static char* parse_uri_query_parameters(char* uri);
vithyat 0:977e87915078 650
vithyat 0:977e87915078 651 void send_coap_ping();
vithyat 0:977e87915078 652
vithyat 0:977e87915078 653 void send_empty_ack(const sn_coap_hdr_s *header, sn_nsdl_addr_s *address);
vithyat 0:977e87915078 654
vithyat 0:977e87915078 655 struct M2MNsdlInterface::nsdl_coap_data_s* create_coap_event_data(sn_coap_hdr_s *received_coap_header,
vithyat 0:977e87915078 656 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 657 struct nsdl_s *nsdl_handle,
vithyat 0:977e87915078 658 uint8_t coap_msg_code = COAP_MSG_CODE_EMPTY);
vithyat 0:977e87915078 659
vithyat 0:977e87915078 660 void handle_register_response(const sn_coap_hdr_s *coap_header);
vithyat 0:977e87915078 661
vithyat 0:977e87915078 662 void handle_unregister_response(const sn_coap_hdr_s *coap_header);
vithyat 0:977e87915078 663
vithyat 0:977e87915078 664 void handle_register_update_response(const sn_coap_hdr_s *coap_header);
vithyat 0:977e87915078 665
vithyat 0:977e87915078 666 void handle_request_response(const sn_coap_hdr_s *coap_header, struct request_context_s *request_context);
vithyat 0:977e87915078 667
vithyat 0:977e87915078 668 void handle_bootstrap_response(const sn_coap_hdr_s *coap_header);
vithyat 0:977e87915078 669
vithyat 0:977e87915078 670 void handle_message_delivered(M2MBase *base, const M2MBase::MessageType type);
vithyat 0:977e87915078 671
vithyat 0:977e87915078 672 void handle_empty_ack(const sn_coap_hdr_s *coap_header, bool is_bootstrap_msg);
vithyat 0:977e87915078 673
vithyat 0:977e87915078 674 bool handle_post_response(sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 675 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 676 sn_coap_hdr_s *&coap_response,
vithyat 0:977e87915078 677 M2MObjectInstance *&obj_instance,
vithyat 0:977e87915078 678 bool is_bootstrap_msg);
vithyat 0:977e87915078 679
vithyat 0:977e87915078 680 void set_retransmission_parameters();
vithyat 0:977e87915078 681
vithyat 0:977e87915078 682 void send_pending_request();
vithyat 0:977e87915078 683
vithyat 0:977e87915078 684 void store_to_response_list(const char *uri, int32_t msg_id, M2MBase::MessageType type);
vithyat 0:977e87915078 685
vithyat 0:977e87915078 686 struct coap_response_s* find_response(int32_t msg_id);
vithyat 0:977e87915078 687
vithyat 0:977e87915078 688 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 689 struct coap_response_s* find_delayed_response(const char* uri_path,
vithyat 0:977e87915078 690 const M2MBase::MessageType type,
vithyat 0:977e87915078 691 int32_t message_id = UNDEFINED_MSG_ID);
vithyat 0:977e87915078 692
vithyat 0:977e87915078 693 bool handle_delayed_response_store(const char* uri_path,
vithyat 0:977e87915078 694 sn_coap_hdr_s* received_coap,
vithyat 0:977e87915078 695 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 696 const M2MBase::MessageType message_type);
vithyat 0:977e87915078 697 #endif
vithyat 0:977e87915078 698
vithyat 0:977e87915078 699 void failed_to_send_request(request_context_s *request, const sn_coap_hdr_s *coap_header);
vithyat 0:977e87915078 700
vithyat 0:977e87915078 701 bool coap_ping_in_process() const;
vithyat 0:977e87915078 702
vithyat 0:977e87915078 703 void remove_ping_from_response_list();
vithyat 0:977e87915078 704
vithyat 0:977e87915078 705 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 706 static M2MBase::Operation operation_for_message_code(sn_coap_msg_code_e code);
vithyat 0:977e87915078 707 #endif // ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 708
vithyat 0:977e87915078 709 private:
vithyat 0:977e87915078 710 M2MNsdlObserver &_observer;
vithyat 0:977e87915078 711 M2MBaseList _base_list;
vithyat 0:977e87915078 712 sn_nsdl_ep_parameters_s *_endpoint;
vithyat 0:977e87915078 713 nsdl_s *_nsdl_handle;
vithyat 0:977e87915078 714 M2MSecurity *_security; // Not owned
vithyat 0:977e87915078 715 M2MServer *_server;
vithyat 0:977e87915078 716 M2MTimer _nsdl_execution_timer;
vithyat 0:977e87915078 717 M2MTimer _registration_timer;
vithyat 0:977e87915078 718 M2MConnectionHandler &_connection_handler;
vithyat 0:977e87915078 719 String _endpoint_name;
vithyat 0:977e87915078 720 String _internal_endpoint_name;
vithyat 0:977e87915078 721 uint32_t _counter_for_nsdl;
vithyat 0:977e87915078 722 uint32_t _next_coap_ping_send_time;
vithyat 0:977e87915078 723 char *_server_address; // BS or M2M address
vithyat 0:977e87915078 724 request_context_list_t _request_context_list;
vithyat 0:977e87915078 725 response_list_t _response_list;
vithyat 0:977e87915078 726 char *_custom_uri_query_params;
vithyat 0:977e87915078 727 M2MNotificationHandler *_notification_handler;
vithyat 0:977e87915078 728 arm_event_storage_t _event;
vithyat 0:977e87915078 729 uint16_t _auto_obs_token;
vithyat 0:977e87915078 730 uint16_t _bootstrap_id;
vithyat 0:977e87915078 731 static int8_t _tasklet_id;
vithyat 0:977e87915078 732 uint8_t _binding_mode;
vithyat 0:977e87915078 733 bool _identity_accepted;
vithyat 0:977e87915078 734 bool _nsdl_execution_timer_running;
vithyat 0:977e87915078 735 bool _notification_send_ongoing;
vithyat 0:977e87915078 736 bool _registered;
vithyat 0:977e87915078 737 bool _bootstrap_finish_ack_received;
vithyat 0:977e87915078 738 M2MTimer _download_retry_timer;
vithyat 0:977e87915078 739 uint64_t _download_retry_time;
vithyat 0:977e87915078 740
vithyat 0:977e87915078 741 friend class Test_M2MNsdlInterface;
vithyat 0:977e87915078 742
vithyat 0:977e87915078 743 };
vithyat 0:977e87915078 744
vithyat 0:977e87915078 745 #endif // M2MNSDLINTERFACE_H
vithyat 0:977e87915078 746