Mistake on this page? Email us
MbedCloudClient.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // Copyright 2016-2021 Pelion.
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 
25 #include "include/ServiceClient.h"
26 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
27 #include "est_defs.h"
28 #endif // !MBED_CLIENT_DISABLE_EST_FEATURE
30 
31 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
32 // This overly long path is needed to have build compatibility with previous
33 // version. A #include can't just point to a file which is not in application's
34 // include path and we should not force application to add every internal directory
35 // of MCC to their paths.
36 // On next phase, the cmake is used to publish the API paths via
37 // target_include_directories(), but that requires a bit more cleanups.
38 #include "certificate-enrollment-client/certificate-enrollment-client/ce_defs.h"
39 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
40 
41 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
42 #include "ds_status.h"
43 #endif // MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
44 
45 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
46 #include "multicast.h"
47 #endif // MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
48 
56 
57 public:
58 
65  virtual void value_updated(M2MBase *base, M2MBase::BaseType type) = 0;
66 };
67 
68 
69 
89 class MbedCloudClient : public ServiceClientCallback {
90 
91 public:
92 
99  typedef enum {
101  ConnectErrorNone = M2MInterface::ErrorNone,
102 
104  ConnectAlreadyExists = M2MInterface::AlreadyExists,
105 
108  ConnectBootstrapFailed = M2MInterface::BootstrapFailed,
109 
112  ConnectInvalidParameters = M2MInterface::InvalidParameters,
113 
116  ConnectNotRegistered = M2MInterface::NotRegistered,
117 
120  ConnectTimeout = M2MInterface::Timeout,
121 
124  ConnectNetworkError = M2MInterface::NetworkError,
125 
128  ConnectResponseParseFailed = M2MInterface::ResponseParseFailed,
129 
132  ConnectUnknownError = M2MInterface::UnknownError,
133 
136  ConnectMemoryConnectFail = M2MInterface::MemoryFail,
137 
140  ConnectNotAllowed = M2MInterface::NotAllowed,
141 
144  ConnectSecureConnectionFailed = M2MInterface::SecureConnectionFailed,
145 
148  ConnectDnsResolvingFailed = M2MInterface::DnsResolvingFailed,
149 
151  ConnectorFailedToStoreCredentials = M2MInterface::FailedToStoreCredentials,
152 
155  ConnectorFailedToReadCredentials = M2MInterface::FailedToReadCredentials,
156 
158  ConnectorInvalidCredentials = M2MInterface::InvalidCertificates,
159 
162  ConnectorUnregistrationFailed = M2MInterface::UnregistrationFailed,
163 
164 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
165  UpdateWarningNoActionRequired = UpdateClient::WarningBase, // Range reserved for Update Error from 0x0400 - 0x04FF
166  UpdateWarningCertificateNotFound = UpdateClient::WarningCertificateNotFound,
167  UpdateWarningIdentityNotFound = UpdateClient::WarningIdentityNotFound,
168  UpdateWarningVendorMismatch = UpdateClient::WarningVendorMismatch,
169  UpdateWarningClassMismatch = UpdateClient::WarningClassMismatch,
170  UpdateWarningDeviceMismatch = UpdateClient::WarningDeviceMismatch,
171  UpdateWarningCertificateInvalid = UpdateClient::WarningCertificateInvalid,
172  UpdateWarningSignatureInvalid = UpdateClient::WarningSignatureInvalid,
173  UpdateWarningBadKeytable = UpdateClient::WarningBadKeytable,
174  UpdateWarningURINotFound = UpdateClient::WarningURINotFound,
175  UpdateWarningRollbackProtection = UpdateClient::WarningRollbackProtection,
176  UpdateWarningAuthorizationRejected = UpdateClient::WarningAuthorizationRejected,
177  UpdateWarningAuthorizationUnavailable = UpdateClient::WarningAuthorizationUnavailable,
178  UpdateWarningUnknown = UpdateClient::WarningUnknown,
179  UpdateCertificateInsertion = UpdateClient::WarningCertificateInsertion,
180  UpdateErrorUserActionRequired = UpdateClient::ErrorBase,
181  UpdateErrorWriteToStorage = UpdateClient::ErrorWriteToStorage,
182  UpdateErrorInvalidHash = UpdateClient::ErrorInvalidHash,
183  UpdateErrorConnection = UpdateClient::ErrorConnection,
184  UpdateFatalRebootRequired,
185 #endif
186 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
187  EnrollmentErrorBase = CE_STATUS_RANGE_BASE,
190  EnrollmentErrorEnd = CE_STATUS_RANGE_END,
191 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
192 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
193  DeviceSentryErrorBase = DS_STATUS_RANGE_BASE,
196  DeviceSentryErrorEnd = DS_STATUS_RANGE_END,
197 #endif // MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
198 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
199  MulticastErrorBase = MULTICAST_STATUS_RANGE_BASE,
201  MulticastErrorEnd = MULTICAST_STATUS_RANGE_END
202 #endif // MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
203 
204  } Error;
205 
206 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
207 
210  enum {
211  UpdateRequestInvalid = UpdateClient::RequestInvalid,
212  UpdateRequestDownload = UpdateClient::RequestDownload,
213  UpdateRequestInstall = UpdateClient::RequestInstall
214  };
215 #endif
216 
221  typedef enum {
222  Unregistered = 0,
223  Registered,
224  RegistrationUpdated,
225  AlertMode,
226  Paused,
227  Sleep
228  } Status;
229 
233  MbedCloudClient();
234 
248  MbedCloudClient(void(*on_registered_cb)(void),
249  void(*on_unregistered_cb)(void),
250  void(*on_error_cb)(int)
251 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
252  , void(*update_authorize_cb)(int32_t request) = NULL,
253  void(*update_progress_cb)(uint32_t progress, uint32_t total) = NULL
254 #endif
255  ) m2m_deprecated;
256 
266  MbedCloudClient(void(*on_status_changed_cb)(int),
267  void(*on_error_cb)(int)
268 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
269  , void(*update_authorize_cb)(int32_t request) = NULL,
270  void(*update_progress_cb)(uint32_t progress, uint32_t total) = NULL
271 #endif
272  );
273 
277  virtual ~MbedCloudClient();
278 
287  void add_objects(const M2MObjectList &object_list);
288 
297  void add_objects(const M2MBaseList &base_list);
298 
299  void remove_object(M2MBase *object);
300 
310  void set_update_callback(MbedCloudClientCallback *callback);
311 
326  bool init();
327 
341  bool setup(void *iface, bool full_register = false);
342 
350  void on_registered(void(*fn)(void)) m2m_deprecated;
351 
359  template<typename T>
360  void on_registered(T *object, void (T::*member)(void)) m2m_deprecated;
361 
369  void on_error(void(*fn)(int));
370 
378  template<typename T>
379  void on_error(T *object, void (T::*member)(int));
380 
387  void on_status_changed(void(*fn)(int));
388 
396  template<typename T>
397  void on_status_changed(T *object, void (T::*member)(int));
398 
406  void on_unregistered(void(*fn)(void)) m2m_deprecated;
407 
415  template<typename T>
416  void on_unregistered(T *object, void (T::*member)(void)) m2m_deprecated;
417 
425  void on_registration_updated(void(*fn)(void)) m2m_deprecated;
426 
435  template<typename T>
436  void on_registration_updated(T *object, void (T::*member)(void)) m2m_deprecated;
437 
445  void keep_alive() m2m_deprecated;
446 
453  void register_update();
454 
459  void close();
460 
465  const ConnectorClientEndpointInfo *endpoint_info() const;
466 
471  M2MInterface *get_m2m_interface();
472 
479  void set_queue_sleep_handler(callback_handler handler) m2m_deprecated;
480 
488  void set_random_number_callback(random_number_cb callback);
489 
499  void set_entropy_callback(entropy_cb callback);
500 
501 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
502 
506  void set_update_authorize_handler(void (*handler)(int32_t request)) __attribute__((deprecated("Use set_update_authorize_priority_handler instead")));
507 
512  void set_update_authorize_priority_handler(void (*handler)(int32_t request, uint64_t priority));
513 
518  void update_authorize(int32_t request);
519 
525  void update_reject(int32_t request, int32_t reason);
526 
531  void set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total));
532 #endif
533 
534 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
535 
541  void on_external_update(void(*fn)(uint32_t start_address, uint32_t firmware_size));
542 #endif
543 
548  const char *error_description() const;
549 
559  void send_get_request(DownloadType type,
560  const char *uri,
561  const size_t offset,
562  get_data_cb data_cb,
563  get_data_error_cb error_cb,
564  void *context);
565 
566 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
567 
576  ce_status_e certificate_renew(const char *cert_name);
577 
583  void on_certificate_renewal(cert_renewal_cb_f user_cb);
584 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
585 
586 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
587 
592  const M2MBaseList *get_object_list() const;
593 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
594 
603  void pause();
604 
612  void resume(void *iface);
613 
620  void alert();
621 
622 #if defined (MBED_CLIENT_DYNAMIC_LOGGING_BUFFER_SIZE) && (MBED_CLIENT_DYNAMIC_LOGGING_BUFFER_SIZE > 0)
623 
629  void set_dynamic_logging_state(bool state, bool stopped_by_update);
630 #endif // MBED_CLIENT_DYNAMIC_LOGGING_BUFFER_SIZE
631 
632 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
633 
643  est_status_e est_request_enrollment(const char *cert_name,
644  const size_t cert_name_length,
645  uint8_t *csr,
646  const size_t csr_length,
647  est_enrollment_result_cb result_cb,
648  void *context) const;
649 
656  void est_free_cert_chain_context(cert_chain_context_s *context) const;
657 #endif // !MBED_CLIENT_DISABLE_EST_FEATURE
658 
659 protected: // from ServiceClientCallback
660 
666  virtual void complete(ServiceClientCallbackStatus status);
667 
673  virtual void error(int error, const char *reason);
674 
681  virtual void value_updated(M2MBase *base, M2MBase::BaseType type);
682 
683 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
684 
689  virtual void external_update(uint32_t start_address, uint32_t firmware_size);
690 #endif
691 
692 private:
693 
694  ServiceClient _client;
695  MbedCloudClientCallback *_value_callback;
696  M2MBaseList _object_list;
697  FP0<void> _on_registered;
698  FP0<void> _on_unregistered;
699  FP0<void> _on_registration_updated;
700  FP1<void, int> _on_error;
701  FP1<void, int> _on_status_changed;
702  const char *_error_description;
703  bool _init_done;
704 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
705  FP2<void, uint32_t, uint32_t> _on_external_update;
706 #endif
707 
708 };
709 
710 template<typename T>
711 void MbedCloudClient::on_registered(T *object, void (T::*member)(void))
712 {
713  FP0<void> fp(object, member);
714  _on_registered = fp;
715 }
716 
717 template<typename T>
718 void MbedCloudClient::on_error(T *object, void (T::*member)(int))
719 {
720  FP1<void, int> fp(object, member);
721  _on_error = fp;
722 }
723 
724 template<typename T>
725 void MbedCloudClient::on_unregistered(T *object, void (T::*member)(void))
726 {
727  FP0<void> fp(object, member);
728  _on_unregistered = fp;
729 }
730 
731 template<typename T>
732 void MbedCloudClient::on_registration_updated(T *object, void (T::*member)(void))
733 {
734  FP0<void> fp(object, member);
735  _on_registration_updated = fp;
736 }
737 
738 template<typename T>
739 void MbedCloudClient::on_status_changed(T *object, void (T::*member)(int))
740 {
741  FP1<void, int> fp(object, member);
742  _on_status_changed = fp;
743 }
744 #endif // __MBED_CLOUD_CLIENT_H__
void on_unregistered(void(*fn)(void)) m2m_deprecated
Set the callback function that is called when Device Management Client is unregistered successfully f...
Configuration options (set of defines and values).
Definitions for certificate chain structures and Enrollment over Secure Transport (EST) callback...
void(* est_enrollment_result_cb)(est_enrollment_result_e result, struct cert_chain_context_s *cert_chain, void *context)
When the enrollment result has been handled by the callback, the free_cert_chain_context function mus...
Definition: est_defs.h:76
virtual void value_updated(M2MBase *base, M2MBase::BaseType type)=0
A callback indicating that the value of the Resource Object is updated by the LwM2M Device Management...
void on_status_changed(void(*fn)(int))
Set the callback function that is called when the client status change. The status code can be mapped...
Error
An enum defining different kinds of errors that can occur during various client operations.
Definition: MbedCloudClient.h:99
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:55
Definition: MbedCloudClient.h:89
Definition: est_defs.h:51
void on_registration_updated(void(*fn)(void)) m2m_deprecated
Set the callback function that is called when Device Management Client registration is updated succes...
void on_registered(void(*fn)(void)) m2m_deprecated
Set the callback function that is called when Device Management Client is registered successfully to ...
Status
An enum defining different statuses that can occur during various client operations.
Definition: MbedCloudClient.h:221