1
Revision 0:d193d40d4fa1, committed 2018-03-21
- Comitter:
- group-STM32F031
- Date:
- Wed Mar 21 18:13:41 2018 +0000
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,2 @@ +# simple-mbed-cloud-client +Simple interface for Mbed Cloud CLient
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_cloud_client_resource.cpp Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,65 @@ +#include "mbed_cloud_client_resource.h" +#include "simple-mbed-cloud-client.h" + +unsigned int m2m_method_to_index(M2MMethod::M2MMethod method) { + switch (method) { + case M2MMethod::GET: + return 0; + break; + + case M2MMethod::PUT: + return 1; + break; + + case M2MMethod::POST: + return 2; + break; + + case M2MMethod::DELETE: + return 3; + break; + } +} + +MbedCloudClientResource::MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name) +: client(client) { + this->path = path; + this->name = name; +} + +void MbedCloudClientResource::observable(bool observable) { + this->isObservable = observable; +} + +void MbedCloudClientResource::methods(unsigned int methodMask) { + this->methodMask = methodMask; +} + +void MbedCloudClientResource::attach(M2MMethod::M2MMethod method, void *callback) { + this->callbacks[m2m_method_to_index(method)] = callback; +} + +void MbedCloudClientResource::attach_notification(M2MMethod::M2MMethod method, void *callback) { + this->notification_callbacks[m2m_method_to_index(method)] = callback; +} + +void MbedCloudClientResource::detatch(M2MMethod::M2MMethod method) { + this->callbacks[m2m_method_to_index(method)] = NULL; +} + +void MbedCloudClientResource::detatch_notification(M2MMethod::M2MMethod method) { + this->notification_callbacks[m2m_method_to_index(method)] = NULL; +} + +void MbedCloudClientResource::set_value(int value) { + // TODO +} + +void MbedCloudClientResource::set_value(char *value) { + // TODO +} + +char* MbedCloudClientResource::get_value() { + // TODO + return (char*)"test"; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_cloud_client_resource.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,47 @@ +#ifndef MBED_CLOUD_CLIENT_RESOURCE_H +#define MBED_CLOUD_CLIENT_RESOURCE_H + +#include "simple-mbed-cloud-client.h" +#include "mbed-client/m2mstring.h" + +#define NUM_M2M_METHODS 4 + +namespace M2MMethod { + +enum M2MMethod { + GET = 0x01, + PUT = 0x02, + POST = 0x04, + DELETE = 0x08 +}; + +}; + +class SimpleMbedCloudClient; + +class MbedCloudClientResource { + public: + MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name); + + void observable(bool observable); + void methods(unsigned int methodMask); + void attach(M2MMethod::M2MMethod method, void *callback); + void attach_notification(M2MMethod::M2MMethod method, void *callback); + void detatch(M2MMethod::M2MMethod method); + void detatch_notification(M2MMethod::M2MMethod method); + void set_value(int value); + void set_value(char *value); + char* get_value(); + + private: + SimpleMbedCloudClient *client; + String path; + String name; + bool isObservable; + unsigned int methodMask; + + void *callbacks[NUM_M2M_METHODS]; + void *notification_callbacks[NUM_M2M_METHODS]; +}; + +#endif // MBED_CLOUD_CLIENT_RESOURCE_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_cloud_client_user_config.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,48 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + + +#ifndef MBED_CLOUD_CLIENT_USER_CONFIG_H +#define MBED_CLOUD_CLIENT_USER_CONFIG_H + +#define MBED_CLOUD_CLIENT_ENDPOINT_TYPE "default" +#define MBED_CLOUD_CLIENT_TRANSPORT_MODE_TCP +#define MBED_CLOUD_CLIENT_LIFETIME 3600 + +#define MBED_CLOUD_CLIENT_SUPPORT_UPDATE +#define SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 1024 + +/* set flag to enable update support in mbed Cloud client */ +#define MBED_CLOUD_CLIENT_SUPPORT_UPDATE + +/* set download buffer size in bytes (min. 1024 bytes) */ + +/* Use larger buffers in Linux */ +#ifdef __linux__ +#define MBED_CLOUD_CLIENT_UPDATE_BUFFER (2 * 1024 * 1024) +#else +#define MBED_CLOUD_CLIENT_UPDATE_BUFFER 2048 +#endif + +/* Developer flags for Update feature */ +#if MBED_CONF_APP_DEVELOPER_MODE == 1 + #define MBED_CLOUD_DEV_UPDATE_CERT + #define MBED_CLOUD_DEV_UPDATE_ID +#endif /* MBED_CONF_APP_DEVELOPER_MODE */ + +#endif /* MBED_CLOUD_CLIENT_USER_CONFIG_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memory_tests.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,33 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#ifndef __MEMORY_TESTS_H__ +#define __MEMORY_TESTS_H__ + +#include "mbed-client/m2minterface.h" +#include "mbed.h" + +// A function for creating a batch of resources for memory consumption purposes. +void m2mobject_test_set(M2MObjectList& object_list); + +// Print into serial the m2m object sizes and heap allocation sizes. +void m2mobject_stats(); + +void heap_stats(); + +#endif // !__MEMORY_TESTS_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pal_plat_rot_insecure.cpp Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,53 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + + +#include "pal_plat_rtos.h" + +#define PAL_DEVICE_KEY_SIZE_IN_BYTES 16 + +//THIS CODE IS FOR TESTING PURPOSES ONLY. DO NOT USE IN PRODUCTION ENVIRONMENTS. REPLACE WITH A PROPER IMPLEMENTATION BEFORE USE +palStatus_t __attribute__((weak)) pal_plat_osGetRoT128Bit(uint8_t *keyBuf, size_t keyLenBytes) +{ +#if defined (__CC_ARM) /* ARM compiler. */ + #warning("PAL_INSECURE- You are using insecure Root Of Trust implementation, DO NOT USE IN PRODUCTION ENVIRONMENTS. REPLACE WITH A PROPER IMPLEMENTATION BEFORE USE") +#else + #pragma message ("You are using insecure Root Of Trust implementation, DO NOT USE IN PRODUCTION ENVIRONMENTS. REPLACE WITH A PROPER IMPLEMENTATION BEFORE USE") +#endif + static bool runOnce = true; + if (runOnce) { + + PAL_LOG(WARN, "You are using insecure Root Of Trust implementation"); + runOnce = false; + } + + if (keyLenBytes < PAL_DEVICE_KEY_SIZE_IN_BYTES) { + return PAL_ERR_BUFFER_TOO_SMALL; + } + + if (NULL == keyBuf) { + return PAL_ERR_NULL_POINTER; + } + + for (int i=0; i < PAL_DEVICE_KEY_SIZE_IN_BYTES; i++) { + keyBuf[i] = i; + } + + return PAL_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resource.cpp Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,84 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#include "mbed-cloud-client/MbedCloudClient.h" +#include "m2mresource.h" +#include "mbed-client/m2minterface.h" +#include <stdio.h> +#include <string.h> + +M2MResource* add_resource(M2MObjectList *list, uint16_t object_id, uint16_t instance_id, + uint16_t resource_id, const char *resource_type, M2MResourceInstance::ResourceType data_type, + M2MBase::Operation allowed, const char *value, bool observable, void *cb, + void *notification_status_cb) +{ + M2MObject *object = NULL; + M2MObjectInstance* object_instance = NULL; + M2MResource* resource = NULL; + char name[6]; + + //check if object already exists. + if (!list->empty()) { + M2MObjectList::const_iterator it; + it = list->begin(); + for ( ; it != list->end(); it++ ) { + if ((*it)->name_id() == object_id) { + object = (*it); + break; + } + } + } + //Create new object if needed. + if (!object) { + snprintf(name, 6, "%d", object_id); + object = M2MInterfaceFactory::create_object(name); + list->push_back(object); + } else { + //check if instance already exists. + object_instance = object->object_instance(instance_id); + } + //Create new instance if needed. + if (!object_instance) { + object_instance = object->create_object_instance(instance_id); + } + //create the recource. + snprintf(name, 6, "%d", resource_id); + resource = object_instance->create_dynamic_resource(name, resource_type, data_type, observable); + //Set value if available. + if (value) { + resource->set_value((const unsigned char*)value, strlen(value)); + } + //Set allowed operations for accessing the resource. + resource->set_operation(allowed); + if (observable) { + resource->set_notification_delivery_status_cb( + (void(*)(const M2MBase&, + const NoticationDeliveryStatus, + void*))notification_status_cb, NULL); + } + + //Set callback of PUT or POST operation is enabled. + //NOTE: This function does not support setting them both. + if(allowed & M2MResourceInstance::PUT_ALLOWED) { + resource->set_value_updated_function((void(*)(const char*))cb); + } else if (allowed & M2MResourceInstance::POST_ALLOWED){ + resource->set_execute_function((void(*)(void*))cb); + } + + return resource; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resource.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,65 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + + +#ifndef RESOURCE_H +#define RESOURCE_H + + +/** + * \brief Helper function for creating different kind of resources. + * The path of the resource will be "object_id/instance_id/resource_id" + * For example if object_id = 1, instance_id = 2, resource_id = 3 + * the path would be 1/2/3 + * + * \param list Pointer to the object list, + * contains objects to be registered to the server. + * \param object_id Name of the object in integer format. + * \param instance_id Name of the instance in integer format. + * \param resource_id Name of the resource in integer format. + * \param resource_type Resource type name. + * \param data_type Data type of the resource value. + * \param allowed Methods allowed for accessing this resource. + * \param value Resource value as a null terminated string. + * May be set as NULL. + * \param observable Resource set observable if true. + * \param cb Function pointer to either: + * value_updated_callback2 if allowed & GET_PUT_ALLOWED + * OR + * execute_callback_2 in if allowed & POST_ALLOWED. + * In other cases this parameter is ignored. + * + * NOTE: This function is not designed to support setting both + * GET_PUT_ALLOWED and POST_ALLOWED for parameter allowed + * at the same time. + * \param notification_status_cb Function pointer to notification_delivery_status_cb + * if resource is set to be observable. + */ +M2MResource* add_resource(M2MObjectList *list, + uint16_t object_id, + uint16_t instance_id, + uint16_t resource_id, + const char *resource_type, + M2MResourceInstance::ResourceType data_type, + M2MBase::Operation allowed, + const char *value, + bool observable, + void *cb, + void *notification_status_cb); + +#endif //RESOURCE_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simple-mbed-cloud-client.cpp Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,252 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#include <stdio.h> +#include "simple-mbed-cloud-client.h" +#include "mbed-cloud-client/MbedCloudClient.h" +#include "m2mdevice.h" +#include "setup.h" +#include "m2mresource.h" +#include "mbed-client/m2minterface.h" +#include "key_config_manager.h" +#include "resource.h" +#include "mbed-client/m2mvector.h" +#include "mbed_cloud_client_resource.h" + +#ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE +#include MBED_CLOUD_CLIENT_USER_CONFIG_FILE +#endif + +#ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE +#include "update_ui_example.h" +#endif + +#ifdef MBED_HEAP_STATS_ENABLED +#include "memory_tests.h" +#endif + + +SimpleMbedCloudClient::SimpleMbedCloudClient() : + _registered(false), + _register_called(false){ +} + +SimpleMbedCloudClient::~SimpleMbedCloudClient() { + for (unsigned int i = 0; _resources.size(); i++) { + delete _resources[i]; + } +} + +bool SimpleMbedCloudClient::call_register() { + + _cloud_client.on_registered(this, &SimpleMbedCloudClient::client_registered); + _cloud_client.on_unregistered(this, &SimpleMbedCloudClient::client_unregistered); + _cloud_client.on_error(this, &SimpleMbedCloudClient::error); + + if (init_connection()) { + printf("Network initialized, connecting...\n"); + bool setup = _cloud_client.setup(get_network_interface()); + _register_called = true; + if (!setup) { + printf("Client setup failed\n"); + return false; + } + } else { + printf("Failed to initialize connection\n"); + return false; + } + +#ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE + /* Set callback functions for authorizing updates and monitoring progress. + Code is implemented in update_ui_example.cpp + Both callbacks are completely optional. If no authorization callback + is set, the update process will procede immediately in each step. + */ + update_ui_set_cloud_client(&_cloud_client); + _cloud_client.set_update_authorize_handler(update_authorize); + _cloud_client.set_update_progress_handler(update_progress); +#endif + return true; +} + +void SimpleMbedCloudClient::close() { + _cloud_client.close(); +} + +void SimpleMbedCloudClient::register_update() { + _cloud_client.register_update(); +} + +void SimpleMbedCloudClient::client_registered() { + _registered = true; + printf("\nClient registered\n\n"); + static const ConnectorClientEndpointInfo* endpoint = NULL; + if (endpoint == NULL) { + endpoint = _cloud_client.endpoint_info(); + if (endpoint) { + +#if MBED_CONF_APP_DEVELOPER_MODE == 1 + printf("Endpoint Name: %s\r\n", endpoint->internal_endpoint_name.c_str()); +#else + printf("Endpoint Name: %s\r\n", endpoint->endpoint_name.c_str()); +#endif + printf("Device Id: %s\r\n", endpoint->internal_endpoint_name.c_str()); + } + } +#ifdef MBED_HEAP_STATS_ENABLED + heap_stats(); +#endif +} + +void SimpleMbedCloudClient::client_unregistered() { + _registered = false; + _register_called = false; + printf("\nClient unregistered - Exiting application\n\n"); +#ifdef MBED_HEAP_STATS_ENABLED + heap_stats(); +#endif +} + +void SimpleMbedCloudClient::error(int error_code) { + const char *error; + switch(error_code) { + case MbedCloudClient::ConnectErrorNone: + error = "MbedCloudClient::ConnectErrorNone"; + break; + case MbedCloudClient::ConnectAlreadyExists: + error = "MbedCloudClient::ConnectAlreadyExists"; + break; + case MbedCloudClient::ConnectBootstrapFailed: + error = "MbedCloudClient::ConnectBootstrapFailed"; + break; + case MbedCloudClient::ConnectInvalidParameters: + error = "MbedCloudClient::ConnectInvalidParameters"; + break; + case MbedCloudClient::ConnectNotRegistered: + error = "MbedCloudClient::ConnectNotRegistered"; + break; + case MbedCloudClient::ConnectTimeout: + error = "MbedCloudClient::ConnectTimeout"; + break; + case MbedCloudClient::ConnectNetworkError: + error = "MbedCloudClient::ConnectNetworkError"; + break; + case MbedCloudClient::ConnectResponseParseFailed: + error = "MbedCloudClient::ConnectResponseParseFailed"; + break; + case MbedCloudClient::ConnectUnknownError: + error = "MbedCloudClient::ConnectUnknownError"; + break; + case MbedCloudClient::ConnectMemoryConnectFail: + error = "MbedCloudClient::ConnectMemoryConnectFail"; + break; + case MbedCloudClient::ConnectNotAllowed: + error = "MbedCloudClient::ConnectNotAllowed"; + break; + case MbedCloudClient::ConnectSecureConnectionFailed: + error = "MbedCloudClient::ConnectSecureConnectionFailed"; + break; + case MbedCloudClient::ConnectDnsResolvingFailed: + error = "MbedCloudClient::ConnectDnsResolvingFailed"; + break; +#ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE + case MbedCloudClient::UpdateWarningCertificateNotFound: + error = "MbedCloudClient::UpdateWarningCertificateNotFound"; + break; + case MbedCloudClient::UpdateWarningIdentityNotFound: + error = "MbedCloudClient::UpdateWarningIdentityNotFound"; + break; + case MbedCloudClient::UpdateWarningCertificateInvalid: + error = "MbedCloudClient::UpdateWarningCertificateInvalid"; + break; + case MbedCloudClient::UpdateWarningSignatureInvalid: + error = "MbedCloudClient::UpdateWarningSignatureInvalid"; + break; + case MbedCloudClient::UpdateWarningVendorMismatch: + error = "MbedCloudClient::UpdateWarningVendorMismatch"; + break; + case MbedCloudClient::UpdateWarningClassMismatch: + error = "MbedCloudClient::UpdateWarningClassMismatch"; + break; + case MbedCloudClient::UpdateWarningDeviceMismatch: + error = "MbedCloudClient::UpdateWarningDeviceMismatch"; + break; + case MbedCloudClient::UpdateWarningURINotFound: + error = "MbedCloudClient::UpdateWarningURINotFound"; + break; + case MbedCloudClient::UpdateWarningRollbackProtection: + error = "MbedCloudClient::UpdateWarningRollbackProtection"; + break; + case MbedCloudClient::UpdateWarningUnknown: + error = "MbedCloudClient::UpdateWarningUnknown"; + break; + case MbedCloudClient::UpdateErrorWriteToStorage: + error = "MbedCloudClient::UpdateErrorWriteToStorage"; + break; + case MbedCloudClient::UpdateErrorInvalidHash: + error = "MbedCloudClient::UpdateErrorInvalidHash"; + break; +#endif + default: + error = "UNKNOWN"; + } + printf("\nError occurred : %s\r\n", error); + printf("Error code : %d\r\n\n", error_code); + printf("Error details : %s\r\n\n",_cloud_client.error_description()); +} + +bool SimpleMbedCloudClient::is_client_registered() { + return _registered; +} + +bool SimpleMbedCloudClient::is_register_called() { + return _register_called; +} + +void SimpleMbedCloudClient::register_and_connect() { + + _cloud_client.add_objects(_obj_list); + + // Start registering to the cloud. + call_register(); + + // Print memory statistics if the MBED_HEAP_STATS_ENABLED is defined. + #ifdef MBED_HEAP_STATS_ENABLED + printf("Register being called\r\n"); + heap_stats(); + #endif +} + +MbedCloudClient& SimpleMbedCloudClient::get_cloud_client() { + return _cloud_client; +} + +MbedCloudClientResource* SimpleMbedCloudClient::create_resource(const char *path, const char *name) { + MbedCloudClientResource *resource = new MbedCloudClientResource(this, path, name); + _resources.push_back(resource); + return resource; +} + +M2MResource* SimpleMbedCloudClient::add_cloud_resource(uint16_t object_id, uint16_t instance_id, + uint16_t resource_id, const char *resource_type, + M2MResourceInstance::ResourceType data_type, + M2MBase::Operation allowed, const char *value, + bool observable, void *cb, void *notification_status_cb) { + return add_resource(&_obj_list, object_id, instance_id, resource_id, resource_type, data_type, + allowed, value, observable, cb, notification_status_cb); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simple-mbed-cloud-client.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,64 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + + +#ifndef SIMPLEMBEDCLOUDCLIENT_H +#define SIMPLEMBEDCLOUDCLIENT_H +#include <stdio.h> +#include "mbed-cloud-client/MbedCloudClient.h" +#include "m2mdevice.h" +#include "m2mresource.h" +#include "mbed-client/m2minterface.h" +#include "mbed-client/m2mvector.h" +#include "mbed_cloud_client_resource.h" + +class MbedCloudClientResource; + +class SimpleMbedCloudClient { + +public: + + SimpleMbedCloudClient(); + ~SimpleMbedCloudClient(); + + bool call_register(); + void close(); + void register_update(); + void client_registered(); + void client_unregistered(); + void error(int error_code); + bool is_client_registered(); + bool is_register_called(); + void register_and_connect(); + MbedCloudClient& get_cloud_client(); + MbedCloudClientResource* create_resource(const char *path, const char *name); + M2MResource* add_cloud_resource(uint16_t object_id, uint16_t instance_id, + uint16_t resource_id, const char *resource_type, + M2MResourceInstance::ResourceType data_type, + M2MBase::Operation allowed, const char *value, + bool observable, void *cb, void *notification_status_cb); + +private: + M2MObjectList _obj_list; + MbedCloudClient _cloud_client; + bool _registered; + bool _register_called; + Vector<MbedCloudClientResource*> _resources; +}; + +#endif // SIMPLEMBEDCLOUDCLIENT_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/update_default_resources.c Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,41 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE +#include MBED_CLOUD_CLIENT_USER_CONFIG_FILE +#endif + +#include <stdint.h> + +#ifdef MBED_CLOUD_DEV_UPDATE_ID +const uint8_t arm_uc_vendor_id[16] = { "dev_manufacturer" }; +const uint16_t arm_uc_vendor_id_size = sizeof(arm_uc_vendor_id); + +const uint8_t arm_uc_class_id[16] = { "dev_model_number" }; +const uint16_t arm_uc_class_id_size = sizeof(arm_uc_class_id); +#endif + +#ifdef MBED_CLOUD_DEV_UPDATE_CERT +const uint8_t arm_uc_default_fingerprint[32] = { 0 }; +const uint16_t arm_uc_default_fingerprint_size = + sizeof(arm_uc_default_fingerprint); + +const uint8_t arm_uc_default_certificate[1] = { 0 }; +const uint16_t arm_uc_default_certificate_size = + sizeof(arm_uc_default_certificate); +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/update_ui_example.cpp Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,141 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#include "update_ui_example.h" + +#ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE + +#include <stdio.h> +#include <stdint.h> + +static MbedCloudClient* _client; + +#ifdef ARM_UPDATE_CLIENT_VERSION_VALUE +#if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000 +void update_ui_set_cloud_client(MbedCloudClient* client) +{ + _client = client; +} + +void update_authorize(int32_t request) +{ + switch (request) + { + /* Cloud Client wishes to download new firmware. This can have a negative + impact on the performance of the rest of the system. + + The user application is supposed to pause performance sensitive tasks + before authorizing the download. + + Note: the authorization call can be postponed and called later. + This doesn't affect the performance of the Cloud Client. + */ + case MbedCloudClient::UpdateRequestDownload: + printf("Firmware download requested\r\n"); + printf("Authorization granted\r\n"); + _client->update_authorize(MbedCloudClient::UpdateRequestDownload); + + break; + + /* Cloud Client wishes to reboot and apply the new firmware. + + The user application is supposed to save all current work before rebooting. + + Note: the authorization call can be postponed and called later. + This doesn't affect the performance of the Cloud Client. + */ + case MbedCloudClient::UpdateRequestInstall: + printf("Firmware install requested\r\n"); + printf("Authorization granted\r\n"); + _client->update_authorize(MbedCloudClient::UpdateRequestInstall); + break; + + default: + printf("Error - unknown request\r\n"); + break; + } +} +#endif +#endif + +void update_progress(uint32_t progress, uint32_t total) +{ + uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total); + +/* only show progress bar if debug trace is disabled */ +#if !defined(MBED_CONF_MBED_TRACE_ENABLE) \ + && !ARM_UC_ALL_TRACE_ENABLE \ + && !ARM_UC_HUB_TRACE_ENABLE + + printf("\rDownloading: ["); + for (uint8_t index = 0; index < 50; index++) + { + if (index < percent / 2) + { + printf("+"); + } + else if (index == percent / 2) + { + static uint8_t old_max = 0; + static uint8_t counter = 0; + + if (index == old_max) + { + counter++; + } + else + { + old_max = index; + counter = 0; + } + + switch (counter % 4) + { + case 0: + printf("/"); + break; + case 1: + printf("-"); + break; + case 2: + printf("\\"); + break; + case 3: + default: + printf("|"); + break; + } + } + else + { + printf(" "); + } + } + printf("] %d %%", percent); + fflush(stdout); +#else + printf("Downloading: %d %%\r\n", percent); +#endif + + if (progress == total) + { + printf("\r\nDownload completed\r\n"); + } +} + +#endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/update_ui_example.h Wed Mar 21 18:13:41 2018 +0000 @@ -0,0 +1,52 @@ +// ---------------------------------------------------------------------------- +// Copyright 2016-2017 ARM Ltd. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------- + +#ifndef UPDATE_UI_EXAMPLE_H +#define UPDATE_UI_EXAMPLE_H + +#include "mbed-cloud-client/MbedCloudClient.h" + +#ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE + +#ifdef ARM_UPDATE_CLIENT_VERSION_VALUE +#if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000 +/** + * @brief Function for authorizing firmware downloads and reboots. + * @param request The request under consideration. + */ +void update_authorize(int32_t request); +#endif +#endif + +/** + * @brief Callback function for reporting the firmware download progress. + * @param progress Received bytes. + * @param total Total amount of bytes to be received. + */ +void update_progress(uint32_t progress, uint32_t total); + +/** + * @brief Set the cloud client instance for the update UI to use + * @param[in] client pointer to the cloud client instance + */ +void update_ui_set_cloud_client(MbedCloudClient* client); + + +#endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE + +#endif // UPDATE_UI_EXAMPLE_H