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 
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 
49 #if MBED_CLOUD_CLIENT_STL_API
50 #include <map>
51 #include <string>
52 #include <vector>
53 #endif
54 
55 #if MBED_CLOUD_CLIENT_STD_NAMESPACE_POLLUTION
56 // We should not really pollute application's namespace with std:: by having this in
57 // a public header file.
58 // But as as removal of the next line may break existing applications, which build due to this
59 // leakage, we need to maintain the old behavior for a while and just allow one to remove it.
60 using namespace std;
61 #endif
62 
63 class SimpleM2MResourceBase;
64 
72 
73 public:
74 
81  virtual void value_updated(M2MBase *base, M2MBase::BaseType type) = 0;
82 };
83 
84 
85 
105 class MbedCloudClient : public ServiceClientCallback {
106 
107 public:
108 
115  typedef enum {
117  ConnectErrorNone = M2MInterface::ErrorNone,
118 
120  ConnectAlreadyExists = M2MInterface::AlreadyExists,
121 
124  ConnectBootstrapFailed = M2MInterface::BootstrapFailed,
125 
128  ConnectInvalidParameters = M2MInterface::InvalidParameters,
129 
132  ConnectNotRegistered = M2MInterface::NotRegistered,
133 
136  ConnectTimeout = M2MInterface::Timeout,
137 
140  ConnectNetworkError = M2MInterface::NetworkError,
141 
144  ConnectResponseParseFailed = M2MInterface::ResponseParseFailed,
145 
148  ConnectUnknownError = M2MInterface::UnknownError,
149 
152  ConnectMemoryConnectFail = M2MInterface::MemoryFail,
153 
156  ConnectNotAllowed = M2MInterface::NotAllowed,
157 
160  ConnectSecureConnectionFailed = M2MInterface::SecureConnectionFailed,
161 
164  ConnectDnsResolvingFailed = M2MInterface::DnsResolvingFailed,
165 
167  ConnectorFailedToStoreCredentials = M2MInterface::FailedToStoreCredentials,
168 
171  ConnectorFailedToReadCredentials = M2MInterface::FailedToReadCredentials,
172 
175 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
176  UpdateWarningNoActionRequired = UpdateClient::WarningBase, // Range reserved for Update Error from 0x0400 - 0x04FF
177  UpdateWarningCertificateNotFound = UpdateClient::WarningCertificateNotFound,
178  UpdateWarningIdentityNotFound = UpdateClient::WarningIdentityNotFound,
179  UpdateWarningVendorMismatch = UpdateClient::WarningVendorMismatch,
180  UpdateWarningClassMismatch = UpdateClient::WarningClassMismatch,
181  UpdateWarningDeviceMismatch = UpdateClient::WarningDeviceMismatch,
182  UpdateWarningCertificateInvalid = UpdateClient::WarningCertificateInvalid,
183  UpdateWarningSignatureInvalid = UpdateClient::WarningSignatureInvalid,
184  UpdateWarningBadKeytable = UpdateClient::WarningBadKeytable,
185  UpdateWarningURINotFound = UpdateClient::WarningURINotFound,
186  UpdateWarningRollbackProtection = UpdateClient::WarningRollbackProtection,
187  UpdateWarningAuthorizationRejected = UpdateClient::WarningAuthorizationRejected,
188  UpdateWarningAuthorizationUnavailable = UpdateClient::WarningAuthorizationUnavailable,
189  UpdateWarningUnknown = UpdateClient::WarningUnknown,
190  UpdateCertificateInsertion = UpdateClient::WarningCertificateInsertion,
191  UpdateErrorUserActionRequired = UpdateClient::ErrorBase,
192  UpdateErrorWriteToStorage = UpdateClient::ErrorWriteToStorage,
193  UpdateErrorInvalidHash = UpdateClient::ErrorInvalidHash,
194  UpdateErrorConnection = UpdateClient::ErrorConnection,
195  UpdateFatalRebootRequired,
196 #endif
197 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
198  EnrollmentErrorBase = CE_STATUS_RANGE_BASE,
201  EnrollmentErrorEnd = CE_STATUS_RANGE_END,
202 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
203 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
204  DeviceSentryErrorBase = DS_STATUS_RANGE_BASE,
207  DeviceSentryErrorEnd = DS_STATUS_RANGE_END,
208 #endif // MBED_CONF_MBED_CLOUD_CLIENT_ENABLE_DEVICE_SENTRY
209 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
210  MulticastErrorBase = MULTICAST_STATUS_RANGE_BASE,
212  MulticastErrorEnd = MULTICAST_STATUS_RANGE_END
213 #endif // MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
214 
215  }Error;
216 
217 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
218 
221  enum {
222  UpdateRequestInvalid = UpdateClient::RequestInvalid,
223  UpdateRequestDownload = UpdateClient::RequestDownload,
224  UpdateRequestInstall = UpdateClient::RequestInstall
225  };
226 #endif
227 
231  MbedCloudClient();
232 
245  MbedCloudClient(void(*on_registered_cb)(void),
246  void(*on_unregistered_cb)(void),
247  void(*on_error_cb)(int)
248 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
249  ,void(*update_authorize_cb)(int32_t request) = NULL,
250  void(*update_progress_cb)(uint32_t progress, uint32_t total) = NULL
251 #endif
252  );
253 
257  virtual ~MbedCloudClient();
258 
267  void add_objects(const M2MObjectList& object_list);
268 
277  void add_objects(const M2MBaseList& base_list);
278 
279  void remove_object(M2MBase *object);
280 
288  void set_update_callback(MbedCloudClientCallback *callback);
289 
304  bool init();
305 
318  bool setup(void* iface);
319 
326  void on_registered(void(*fn)(void));
327 
334  template<typename T>
335  void on_registered(T *object, void (T::*member)(void));
336 
344  void on_error(void(*fn)(int));
345 
353  template<typename T>
354  void on_error(T *object, void (T::*member)(int));
355 
362  void on_unregistered(void(*fn)(void));
363 
370  template<typename T>
371  void on_unregistered(T *object, void (T::*member)(void));
372 
379  void on_registration_updated(void(*fn)(void));
380 
388  template<typename T>
389  void on_registration_updated(T *object, void (T::*member)(void));
390 
398  void keep_alive() m2m_deprecated;
399 
406  void register_update();
407 
412  void close();
413 
418  const ConnectorClientEndpointInfo *endpoint_info() const;
419 
420 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
421 
425  M2MInterface *get_m2m_interface();
426 #endif
427 
434  void set_queue_sleep_handler(callback_handler handler);
435 
443  void set_random_number_callback(random_number_cb callback);
444 
454  void set_entropy_callback(entropy_cb callback);
455 
456 #if MBED_CLOUD_CLIENT_STL_API
457 
467  bool set_device_resource_value(M2MDevice::DeviceResource resource,
468  const std::string &value) m2m_deprecated;
469 #endif
470 
471 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
472 
476  void set_update_authorize_handler(void (*handler)(int32_t request)) __attribute__((deprecated("Use set_update_authorize_priority_handler instead")));
477 
482  void set_update_authorize_priority_handler(void (*handler)(int32_t request, uint64_t priority));
483 
488  void update_authorize(int32_t request);
489 
495  void update_reject(int32_t request, int32_t reason);
496 
501  void set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total));
502 #endif
503 
504 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
505 
511  void on_external_update(void(*fn)(uint32_t start_address, uint32_t firmware_size));
512 #endif
513 
518  const char *error_description() const;
519 
529  void send_get_request(DownloadType type,
530  const char *uri,
531  const size_t offset,
532  get_data_cb data_cb,
533  get_data_error_cb error_cb,
534  void *context);
535 
536 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
537 
546  ce_status_e certificate_renew(const char *cert_name);
547 
553  void on_certificate_renewal(cert_renewal_cb_f user_cb);
554 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
555 
556 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
557 
562  const M2MBaseList *get_object_list() const;
563 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
564 
573  void pause();
574 
582  void resume(void *iface);
583 
584 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
585 
595  est_status_e est_request_enrollment(const char *cert_name,
596  const size_t cert_name_length,
597  uint8_t *csr,
598  const size_t csr_length,
599  est_enrollment_result_cb result_cb,
600  void *context) const;
601 
608  void est_free_cert_chain_context(cert_chain_context_s *context) const;
609 #endif // !MBED_CLIENT_DISABLE_EST_FEATURE
610 
611 protected: // from ServiceClientCallback
612 
618  virtual void complete(ServiceClientCallbackStatus status);
619 
625  virtual void error(int error, const char *reason);
626 
633  virtual void value_updated(M2MBase *base, M2MBase::BaseType type);
634 
635 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
636 
641  virtual void external_update(uint32_t start_address, uint32_t firmware_size);
642 #endif
643 
644 private:
645 
652 #if MBED_CLOUD_CLIENT_STL_API
653  void register_update_callback(string route, SimpleM2MResourceBase* resource) m2m_deprecated;
654 #endif
655 
656 private:
657 
658  ServiceClient _client;
659  MbedCloudClientCallback *_value_callback;
660  M2MBaseList _object_list;
661  FP0<void> _on_registered;
662  FP0<void> _on_unregistered;
663  FP0<void> _on_registration_updated;
664  FP1<void,int> _on_error;
665  const char *_error_description;
666  bool _init_done;
667 #ifdef MBED_CLOUD_CLIENT_SUPPORT_MULTICAST_UPDATE
668  FP2<void, uint32_t, uint32_t> _on_external_update;
669 #endif
670 
671 #if MBED_CLOUD_CLIENT_STL_API
672  // This API and functionality is being phased out, as it is wasting resources by
673  // duplicating data and harming portability by requiring an STL implementation.
674  map<string, M2MObject*> _objects;
675  map<string, M2MResource*> _resources;
676  map<string, SimpleM2MResourceBase*> _update_values;
677 
678 friend class SimpleM2MResourceBase;
679 
680 #endif // MBED_CLOUD_CLIENT_STL_API
681 };
682 
683 template<typename T>
684 void MbedCloudClient::on_registered(T *object, void (T::*member)(void))
685 {
686  FP0<void> fp(object, member);
687  _on_registered = fp;
688 }
689 
690 template<typename T>
691 void MbedCloudClient::on_error(T *object, void (T::*member)(int))
692 {
693  FP1<void, int> fp(object, member);
694  _on_error = fp;
695 }
696 
697 template<typename T>
698 void MbedCloudClient::on_unregistered(T *object, void (T::*member)(void))
699 {
700  FP0<void> fp(object, member);
701  _on_unregistered = fp;
702 }
703 
704 template<typename T>
705 void MbedCloudClient::on_registration_updated(T *object, void (T::*member)(void))
706 {
707  FP0<void> fp(object, member);
708  _on_registration_updated = fp;
709 }
710 #endif // __MBED_CLOUD_CLIENT_H__
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
Error
An enum defining different kinds of errors that can occur during various client operations.
Definition: MbedCloudClient.h:115
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:71
Definition: MbedCloudClient.h:105
Definition: est_defs.h:51
void on_unregistered(void(*fn)(void))
Set the callback function that is called when Device Management Client is unregistered successfully f...
Not used.
Definition: MbedCloudClient.h:174
void on_registered(void(*fn)(void))
Set the callback function that is called when Device Management Client is registered successfully to ...