Mistake on this page? Email us
MbedCloudClient.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // Copyright 2016-2019 ARM Ltd.
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // ----------------------------------------------------------------------------
18 
19 
20 #ifndef __MBED_CLOUD_CLIENT_H__
21 #define __MBED_CLOUD_CLIENT_H__
22 
23 #include "include/ServiceClient.h"
24 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
25 #include "est_defs.h"
26 #endif // !MBED_CLIENT_DISABLE_EST_FEATURE
28 
29 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
30 // This overly long path is needed to have build compatibility with previous
31 // version. A #include can't just point to a file which is not in application's
32 // include path and we should not force application to add every internal directory
33 // of MCC to their paths.
34 // On next phase, the cmake is used to publish the API paths via
35 // target_include_directories(), but that requires a bit more cleanups.
36 #include "certificate-enrollment-client/certificate-enrollment-client/ce_defs.h"
37 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
38 
39 #if MBED_CLOUD_CLIENT_STL_API
40 #include <map>
41 #include <string>
42 #include <vector>
43 #endif
44 
45 #if MBED_CLOUD_CLIENT_STD_NAMESPACE_POLLUTION
46 // We should not really pollute application's namespace with std:: by having this in
47 // a public header file.
48 // But as as removal of the next line may break existing applications, which build due to this
49 // leakage, we need to maintain the old behavior for a while and just allow one to remove it.
50 using namespace std;
51 #endif
52 
53 class SimpleM2MResourceBase;
54 
62 
63 public:
64 
71  virtual void value_updated(M2MBase *base, M2MBase::BaseType type) = 0;
72 };
73 
74 
75 
85 class MbedCloudClient : public ServiceClientCallback {
86 
87 public:
88 
93  typedef enum {
94  // Range 0x30 - 0x3FF reserved for Connector error.
95  ConnectErrorNone = M2MInterface::ErrorNone,
96 
97  // Not used.
98  ConnectAlreadyExists = M2MInterface::AlreadyExists,
99 
100  // Bootstrap failed.
101  // Client recovers automatically.
102  ConnectBootstrapFailed = M2MInterface::BootstrapFailed,
103 
104  // Security object is not valid or server rejects the registration.
105  // No internal recovery mechanism. Actions needed on the application side.
106  ConnectInvalidParameters = M2MInterface::InvalidParameters,
107 
108  // Cannot unregister as client is not registered.
109  // No internal recovery mechanism. Actions needed on the application side.
110  ConnectNotRegistered = M2MInterface::NotRegistered,
111 
112  // Registration has timed out.
113  // Client recovers automatically.
114  ConnectTimeout = M2MInterface::Timeout,
115 
116  // Socket level operation error.
117  // Client recovers automatically.
118  ConnectNetworkError = M2MInterface::NetworkError,
119 
120  // Failed to parse an incoming CoAP message.
121  // Client will continue working, no actions needed.
122  ConnectResponseParseFailed = M2MInterface::ResponseParseFailed,
123 
124  // Unknown CoAP level error.
125  // Client recovers automatically.
126  ConnectUnknownError = M2MInterface::UnknownError,
127 
128  // Memory allocation has failed.
129  // No internal recovery mechanism. Actions needed on the application side.
130  ConnectMemoryConnectFail = M2MInterface::MemoryFail,
131 
132  // API call is not allowed for now.
133  // Application should try again later.
134  ConnectNotAllowed = M2MInterface::NotAllowed,
135 
136  // Failed to initialize secure connection or DTLS/TLS handshake failed.
137  // Client recovers automatically.
138  ConnectSecureConnectionFailed = M2MInterface::SecureConnectionFailed,
139 
140  // DNS resolving has failed.
141  // Client recovers automatically.
142  ConnectDnsResolvingFailed = M2MInterface::DnsResolvingFailed,
143 
144  // Not used.
145  ConnectorFailedToStoreCredentials = M2MInterface::FailedToStoreCredentials,
146 
147  // Failed to read credentials from storage.
148  // No internal recovery mechanism. Actions needed on the application side.
149  ConnectorFailedToReadCredentials = M2MInterface::FailedToReadCredentials,
150 
151  // Not used.
152  ConnectorInvalidCredentials,
153 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
154  UpdateWarningNoActionRequired = UpdateClient::WarningBase, // Range reserved for Update Error from 0x0400 - 0x04FF
155  UpdateWarningCertificateNotFound = UpdateClient::WarningCertificateNotFound,
156  UpdateWarningIdentityNotFound = UpdateClient::WarningIdentityNotFound,
157  UpdateWarningVendorMismatch = UpdateClient::WarningVendorMismatch,
158  UpdateWarningClassMismatch = UpdateClient::WarningClassMismatch,
159  UpdateWarningDeviceMismatch = UpdateClient::WarningDeviceMismatch,
160  UpdateWarningCertificateInvalid = UpdateClient::WarningCertificateInvalid,
161  UpdateWarningSignatureInvalid = UpdateClient::WarningSignatureInvalid,
162  UpdateWarningBadKeytable = UpdateClient::WarningBadKeytable,
163  UpdateWarningURINotFound = UpdateClient::WarningURINotFound,
164  UpdateWarningRollbackProtection = UpdateClient::WarningRollbackProtection,
165  UpdateWarningAuthorizationRejected = UpdateClient::WarningAuthorizationRejected,
166  UpdateWarningAuthorizationUnavailable = UpdateClient::WarningAuthorizationUnavailable,
167  UpdateWarningUnknown = UpdateClient::WarningUnknown,
168  UpdateCertificateInsertion = UpdateClient::WarningCertificateInsertion,
169  UpdateErrorUserActionRequired = UpdateClient::ErrorBase,
170  UpdateErrorWriteToStorage = UpdateClient::ErrorWriteToStorage,
171  UpdateErrorInvalidHash = UpdateClient::ErrorInvalidHash,
172  UpdateErrorConnection = UpdateClient::ErrorConnection,
173  UpdateFatalRebootRequired,
174 #endif
175 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
176  // Certificate Enrollment error 0x0500 - 0x05ff. Defined in ce_status.h
177  EnrollmentErrorBase = CE_STATUS_RANGE_BASE,
178  EnrollmentErrorEnd = CE_STATUS_RANGE_END
179 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
180  }Error;
181 
182 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
183 
186  enum {
187  UpdateRequestInvalid = UpdateClient::RequestInvalid,
188  UpdateRequestDownload = UpdateClient::RequestDownload,
189  UpdateRequestInstall = UpdateClient::RequestInstall
190  };
191 #endif
192 
196  MbedCloudClient();
197 
210  MbedCloudClient(void(*on_registered_cb)(void),
211  void(*on_unregistered_cb)(void),
212  void(*on_error_cb)(int)
213 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
214  ,void(*update_authorize_cb)(int32_t request) = NULL,
215  void(*update_progress_cb)(uint32_t progress, uint32_t total) = NULL
216 #endif
217  );
218 
222  virtual ~MbedCloudClient();
223 
232  void add_objects(const M2MObjectList& object_list);
233 
242  void add_objects(const M2MBaseList& base_list);
243 
244  void remove_object(M2MBase *object);
245 
253  void set_update_callback(MbedCloudClientCallback *callback);
254 
269  bool init();
270 
283  bool setup(void* iface);
284 
291  void on_registered(void(*fn)(void));
292 
299  template<typename T>
300  void on_registered(T *object, void (T::*member)(void));
301 
309  void on_error(void(*fn)(int));
310 
318  template<typename T>
319  void on_error(T *object, void (T::*member)(int));
320 
327  void on_unregistered(void(*fn)(void));
328 
335  template<typename T>
336  void on_unregistered(T *object, void (T::*member)(void));
337 
344  void on_registration_updated(void(*fn)(void));
345 
353  template<typename T>
354  void on_registration_updated(T *object, void (T::*member)(void));
355 
363  void keep_alive() m2m_deprecated;
364 
371  void register_update();
372 
377  void close();
378 
383  const ConnectorClientEndpointInfo *endpoint_info() const;
384 
391  void set_queue_sleep_handler(callback_handler handler);
392 
400  void set_random_number_callback(random_number_cb callback);
401 
411  void set_entropy_callback(entropy_cb callback);
412 
413 #if MBED_CLOUD_CLIENT_STL_API
414 
424  bool set_device_resource_value(M2MDevice::DeviceResource resource,
425  const std::string &value) m2m_deprecated;
426 #endif
427 
428 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
429 
433  void set_update_authorize_handler(void (*handler)(int32_t request));
434 
439  void set_update_authorize_priority_handler(void (*handler)(int32_t request, uint64_t priority));
440 
445  void update_authorize(int32_t request);
446 
452  void update_reject(int32_t request, int32_t reason);
453 
458  void set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total));
459 #endif
460 
465  const char *error_description() const;
466 
476  void send_get_request(DownloadType type,
477  const char *uri,
478  const size_t offset,
479  get_data_cb data_cb,
480  get_data_error_cb error_cb,
481  void *context);
482 
483 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
484 
493  ce_status_e certificate_renew(const char *cert_name);
494 
500  void on_certificate_renewal(cert_renewal_cb_f user_cb);
501 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
502 
503 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
504 
509  const M2MBaseList *get_object_list() const;
510 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
511 
520  void pause();
521 
529  void resume(void *iface);
530 
531 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
532 
542  est_status_e est_request_enrollment(const char *cert_name,
543  const size_t cert_name_length,
544  uint8_t *csr,
545  const size_t csr_length,
546  est_enrollment_result_cb result_cb,
547  void *context) const;
548 
555  void est_free_cert_chain_context(cert_chain_context_s *context) const;
556 #endif // !MBED_CLIENT_DISABLE_EST_FEATURE
557 
558 protected: // from ServiceClientCallback
559 
565  virtual void complete(ServiceClientCallbackStatus status);
566 
572  virtual void error(int error, const char *reason);
573 
580  virtual void value_updated(M2MBase *base, M2MBase::BaseType type);
581 
582 private:
583 
590 #if MBED_CLOUD_CLIENT_STL_API
591  void register_update_callback(string route, SimpleM2MResourceBase* resource) m2m_deprecated;
592 #endif
593 
594 private:
595 
596  ServiceClient _client;
597  MbedCloudClientCallback *_value_callback;
598  M2MBaseList _object_list;
599  FP0<void> _on_registered;
600  FP0<void> _on_unregistered;
601  FP0<void> _on_registration_updated;
602  FP1<void,int> _on_error;
603  const char *_error_description;
604  bool _init_done;
605 
606 #if MBED_CLOUD_CLIENT_STL_API
607  // This API and functionality is being phased out, as it is wasting resources by
608  // duplicating data and harming portability by requiring an STL implementation.
609  map<string, M2MObject*> _objects;
610  map<string, M2MResource*> _resources;
611  map<string, SimpleM2MResourceBase*> _update_values;
612 
613 friend class SimpleM2MResourceBase;
614 
615 #endif // MBED_CLOUD_CLIENT_STL_API
616 };
617 
618 template<typename T>
619 void MbedCloudClient::on_registered(T *object, void (T::*member)(void))
620 {
621  FP0<void> fp(object, member);
622  _on_registered = fp;
623 }
624 
625 template<typename T>
626 void MbedCloudClient::on_error(T *object, void (T::*member)(int))
627 {
628  FP1<void, int> fp(object, member);
629  _on_error = fp;
630 }
631 
632 template<typename T>
633 void MbedCloudClient::on_unregistered(T *object, void (T::*member)(void))
634 {
635  FP0<void> fp(object, member);
636  _on_unregistered = fp;
637 }
638 
639 template<typename T>
640 void MbedCloudClient::on_registration_updated(T *object, void (T::*member)(void))
641 {
642  FP0<void> fp(object, member);
643  _on_registration_updated = fp;
644 }
645 #endif // __MBED_CLOUD_CLIENT_H__
Configuration options (set of defines and values).
Error
An enum defining different kinds of errors that can occur during various client operations.
Definition: MbedCloudClient.h:93
void on_registration_updated(void(*fn)(void))
Set the callback function that is called when Device Management Client registration is updated succes...
void on_error(void(*fn)(int))
Set the callback function that is called when there is any error occuring in the client functionality...
MbedCloudClientCallback A callback class for informing updated Object and Resource value from the LwM...
Definition: MbedCloudClient.h:61
Definition: MbedCloudClient.h:85
Definition: est_defs.h:46
void on_unregistered(void(*fn)(void))
Set the callback function that is called when Device Management Client is unregistered successfully f...
void on_registered(void(*fn)(void))
Set the callback function that is called when Device Management Client is registered successfully to ...