NuMaker Pelion Device Management example

Fork of mbed-os-example-pelion by cc li

Committer:
ccli8
Date:
Tue Apr 14 16:01:24 2020 +0800
Revision:
11:d147172e94b5
Parent:
5:ae686808e015
Child:
16:75a4830fc40a
Synchronize to mainstream

1. Update main.cpp
2. Update mbed-cloud-client.lib to 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ccli8 0:f78ec4a22e67 1 // ----------------------------------------------------------------------------
ccli8 11:d147172e94b5 2 // Copyright 2016-2020 ARM Ltd.
ccli8 0:f78ec4a22e67 3 //
ccli8 0:f78ec4a22e67 4 // SPDX-License-Identifier: Apache-2.0
ccli8 0:f78ec4a22e67 5 //
ccli8 0:f78ec4a22e67 6 // Licensed under the Apache License, Version 2.0 (the "License");
ccli8 0:f78ec4a22e67 7 // you may not use this file except in compliance with the License.
ccli8 0:f78ec4a22e67 8 // You may obtain a copy of the License at
ccli8 0:f78ec4a22e67 9 //
ccli8 0:f78ec4a22e67 10 // http://www.apache.org/licenses/LICENSE-2.0
ccli8 0:f78ec4a22e67 11 //
ccli8 0:f78ec4a22e67 12 // Unless required by applicable law or agreed to in writing, software
ccli8 0:f78ec4a22e67 13 // distributed under the License is distributed on an "AS IS" BASIS,
ccli8 0:f78ec4a22e67 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ccli8 0:f78ec4a22e67 15 // See the License for the specific language governing permissions and
ccli8 0:f78ec4a22e67 16 // limitations under the License.
ccli8 0:f78ec4a22e67 17 // ----------------------------------------------------------------------------
ccli8 0:f78ec4a22e67 18 #ifndef MBED_TEST_MODE
ccli8 0:f78ec4a22e67 19 #include "mbed.h"
ccli8 0:f78ec4a22e67 20 #include "kv_config.h"
ccli8 0:f78ec4a22e67 21 #include "mbed-cloud-client/MbedCloudClient.h" // Required for new MbedCloudClient()
ccli8 0:f78ec4a22e67 22 #include "factory_configurator_client.h" // Required for fcc_* functions and FCC_* defines
ccli8 0:f78ec4a22e67 23 #include "m2mresource.h" // Required for M2MResource
ccli8 11:d147172e94b5 24 #include "key_config_manager.h" // Required for kcm_factory_reset
ccli8 0:f78ec4a22e67 25
ccli8 0:f78ec4a22e67 26 #include "mbed-trace/mbed_trace.h" // Required for mbed_trace_*
ccli8 0:f78ec4a22e67 27
ccli8 0:f78ec4a22e67 28 // Pointers to the resources that will be created in main_application().
ccli8 0:f78ec4a22e67 29 static MbedCloudClient *cloud_client;
ccli8 0:f78ec4a22e67 30 static bool cloud_client_running = true;
ccli8 0:f78ec4a22e67 31 static NetworkInterface *network = NULL;
ccli8 11:d147172e94b5 32 static int error_count = 0;
ccli8 0:f78ec4a22e67 33
ccli8 0:f78ec4a22e67 34 // Fake entropy needed for non-TRNG boards. Suitable only for demo devices.
ccli8 0:f78ec4a22e67 35 const uint8_t MBED_CLOUD_DEV_ENTROPY[] = { 0xf6, 0xd6, 0xc0, 0x09, 0x9e, 0x6e, 0xf2, 0x37, 0xdc, 0x29, 0x88, 0xf1, 0x57, 0x32, 0x7d, 0xde, 0xac, 0xb3, 0x99, 0x8c, 0xb9, 0x11, 0x35, 0x18, 0xeb, 0x48, 0x29, 0x03, 0x6a, 0x94, 0x6d, 0xe8, 0x40, 0xc0, 0x28, 0xcc, 0xe4, 0x04, 0xc3, 0x1f, 0x4b, 0xc2, 0xe0, 0x68, 0xa0, 0x93, 0xe6, 0x3a };
ccli8 11:d147172e94b5 36 const int MAX_ERROR_COUNT = 5;
ccli8 0:f78ec4a22e67 37
ccli8 0:f78ec4a22e67 38 static M2MResource* m2m_get_res;
ccli8 0:f78ec4a22e67 39 static M2MResource* m2m_put_res;
ccli8 0:f78ec4a22e67 40 static M2MResource* m2m_post_res;
ccli8 0:f78ec4a22e67 41 static M2MResource* m2m_deregister_res;
ccli8 11:d147172e94b5 42 static M2MResource* m2m_factory_reset_res;
ccli8 11:d147172e94b5 43 static SocketAddress sa;
ccli8 11:d147172e94b5 44
ccli8 11:d147172e94b5 45 EventQueue queue(32 * EVENTS_EVENT_SIZE);
ccli8 11:d147172e94b5 46 Thread t;
ccli8 11:d147172e94b5 47 Mutex value_increment_mutex;
ccli8 0:f78ec4a22e67 48
ccli8 0:f78ec4a22e67 49 void print_client_ids(void)
ccli8 0:f78ec4a22e67 50 {
ccli8 0:f78ec4a22e67 51 printf("Account ID: %s\n", cloud_client->endpoint_info()->account_id.c_str());
ccli8 0:f78ec4a22e67 52 printf("Endpoint name: %s\n", cloud_client->endpoint_info()->internal_endpoint_name.c_str());
ccli8 11:d147172e94b5 53 printf("Device ID: %s\n\n", cloud_client->endpoint_info()->endpoint_name.c_str());
ccli8 0:f78ec4a22e67 54 }
ccli8 0:f78ec4a22e67 55
ccli8 11:d147172e94b5 56 void value_increment(void)
ccli8 0:f78ec4a22e67 57 {
ccli8 11:d147172e94b5 58 value_increment_mutex.lock();
ccli8 0:f78ec4a22e67 59 m2m_get_res->set_value(m2m_get_res->get_value_int() + 1);
ccli8 0:f78ec4a22e67 60 printf("Counter %" PRIu64 "\n", m2m_get_res->get_value_int());
ccli8 11:d147172e94b5 61 value_increment_mutex.unlock();
ccli8 0:f78ec4a22e67 62 }
ccli8 0:f78ec4a22e67 63
ccli8 11:d147172e94b5 64 void get_res_update(const char* /*object_name*/)
ccli8 11:d147172e94b5 65 {
ccli8 11:d147172e94b5 66 printf("Counter resource set to %d\n", (int)m2m_get_res->get_value_int());
ccli8 11:d147172e94b5 67 }
ccli8 11:d147172e94b5 68
ccli8 11:d147172e94b5 69 void put_res_update(const char* /*object_name*/)
ccli8 0:f78ec4a22e67 70 {
ccli8 0:f78ec4a22e67 71 printf("PUT update %d\n", (int)m2m_put_res->get_value_int());
ccli8 0:f78ec4a22e67 72 }
ccli8 0:f78ec4a22e67 73
ccli8 0:f78ec4a22e67 74 void execute_post(void* /*arguments*/)
ccli8 0:f78ec4a22e67 75 {
ccli8 0:f78ec4a22e67 76 printf("POST executed\n");
ccli8 0:f78ec4a22e67 77 }
ccli8 0:f78ec4a22e67 78
ccli8 0:f78ec4a22e67 79 void deregister_client(void)
ccli8 0:f78ec4a22e67 80 {
ccli8 0:f78ec4a22e67 81 printf("Unregistering and disconnecting from the network.\n");
ccli8 0:f78ec4a22e67 82 cloud_client->close();
ccli8 0:f78ec4a22e67 83 }
ccli8 0:f78ec4a22e67 84
ccli8 0:f78ec4a22e67 85 void deregister(void* /*arguments*/)
ccli8 0:f78ec4a22e67 86 {
ccli8 0:f78ec4a22e67 87 printf("POST deregister executed\n");
ccli8 0:f78ec4a22e67 88 m2m_deregister_res->send_delayed_post_response();
ccli8 0:f78ec4a22e67 89
ccli8 0:f78ec4a22e67 90 deregister_client();
ccli8 0:f78ec4a22e67 91 }
ccli8 0:f78ec4a22e67 92
ccli8 0:f78ec4a22e67 93 void client_registered(void)
ccli8 0:f78ec4a22e67 94 {
ccli8 0:f78ec4a22e67 95 printf("Client registered.\n");
ccli8 0:f78ec4a22e67 96 print_client_ids();
ccli8 11:d147172e94b5 97 error_count = 0;
ccli8 11:d147172e94b5 98 }
ccli8 11:d147172e94b5 99
ccli8 11:d147172e94b5 100 void client_registration_updated(void)
ccli8 11:d147172e94b5 101 {
ccli8 11:d147172e94b5 102 printf("Client registration updated.\n");
ccli8 11:d147172e94b5 103 error_count = 0;
ccli8 0:f78ec4a22e67 104 }
ccli8 0:f78ec4a22e67 105
ccli8 0:f78ec4a22e67 106 void client_unregistered(void)
ccli8 0:f78ec4a22e67 107 {
ccli8 0:f78ec4a22e67 108 printf("Client unregistered.\n");
ccli8 0:f78ec4a22e67 109 (void) network->disconnect();
ccli8 0:f78ec4a22e67 110 cloud_client_running = false;
ccli8 0:f78ec4a22e67 111 }
ccli8 0:f78ec4a22e67 112
ccli8 11:d147172e94b5 113 void factory_reset(void*)
ccli8 11:d147172e94b5 114 {
ccli8 11:d147172e94b5 115 printf("POST factory reset executed\n");
ccli8 11:d147172e94b5 116 m2m_factory_reset_res->send_delayed_post_response();
ccli8 11:d147172e94b5 117
ccli8 11:d147172e94b5 118 kcm_factory_reset();
ccli8 11:d147172e94b5 119 }
ccli8 11:d147172e94b5 120
ccli8 0:f78ec4a22e67 121 void client_error(int err)
ccli8 0:f78ec4a22e67 122 {
ccli8 0:f78ec4a22e67 123 printf("client_error(%d) -> %s\n", err, cloud_client->error_description());
ccli8 11:d147172e94b5 124 if (err == MbedCloudClient::ConnectNetworkError ||
ccli8 11:d147172e94b5 125 err == MbedCloudClient::ConnectDnsResolvingFailed ||
ccli8 11:d147172e94b5 126 err == MbedCloudClient::ConnectSecureConnectionFailed) {
ccli8 11:d147172e94b5 127 if(++error_count == MAX_ERROR_COUNT) {
ccli8 11:d147172e94b5 128 printf("Max error count %d reached, rebooting.\n\n", MAX_ERROR_COUNT);
ccli8 11:d147172e94b5 129 ThisThread::sleep_for(1*1000);
ccli8 11:d147172e94b5 130 NVIC_SystemReset();
ccli8 11:d147172e94b5 131 }
ccli8 11:d147172e94b5 132 }
ccli8 0:f78ec4a22e67 133 }
ccli8 0:f78ec4a22e67 134
ccli8 0:f78ec4a22e67 135 void update_progress(uint32_t progress, uint32_t total)
ccli8 0:f78ec4a22e67 136 {
ccli8 0:f78ec4a22e67 137 uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total);
ccli8 0:f78ec4a22e67 138 printf("Update progress = %" PRIu8 "%%\n", percent);
ccli8 0:f78ec4a22e67 139 }
ccli8 0:f78ec4a22e67 140
ccli8 11:d147172e94b5 141 void flush_stdin_buffer(void)
ccli8 11:d147172e94b5 142 {
ccli8 11:d147172e94b5 143 FileHandle *debug_console = mbed::mbed_file_handle(STDIN_FILENO);
ccli8 11:d147172e94b5 144 while(debug_console->readable()) {
ccli8 11:d147172e94b5 145 char buffer[1];
ccli8 11:d147172e94b5 146 debug_console->read(buffer, 1);
ccli8 11:d147172e94b5 147 }
ccli8 11:d147172e94b5 148 }
ccli8 11:d147172e94b5 149
ccli8 5:ae686808e015 150 extern "C" MBED_WEAK void dispatch_host_command(int);
ccli8 5:ae686808e015 151
ccli8 0:f78ec4a22e67 152 int main(void)
ccli8 0:f78ec4a22e67 153 {
ccli8 0:f78ec4a22e67 154 int status;
ccli8 0:f78ec4a22e67 155
ccli8 0:f78ec4a22e67 156 status = mbed_trace_init();
ccli8 0:f78ec4a22e67 157 if (status != 0) {
ccli8 0:f78ec4a22e67 158 printf("mbed_trace_init() failed with %d\n", status);
ccli8 0:f78ec4a22e67 159 return -1;
ccli8 0:f78ec4a22e67 160 }
ccli8 0:f78ec4a22e67 161
ccli8 0:f78ec4a22e67 162 // Mount default kvstore
ccli8 0:f78ec4a22e67 163 printf("Application ready\n");
ccli8 0:f78ec4a22e67 164 status = kv_init_storage_config();
ccli8 0:f78ec4a22e67 165 if (status != MBED_SUCCESS) {
ccli8 0:f78ec4a22e67 166 printf("kv_init_storage_config() - failed, status %d\n", status);
ccli8 0:f78ec4a22e67 167 return -1;
ccli8 0:f78ec4a22e67 168 }
ccli8 0:f78ec4a22e67 169
ccli8 0:f78ec4a22e67 170 // Connect with NetworkInterface
ccli8 0:f78ec4a22e67 171 printf("Connect to network\n");
ccli8 0:f78ec4a22e67 172 network = NetworkInterface::get_default_instance();
ccli8 0:f78ec4a22e67 173 if (network == NULL) {
ccli8 0:f78ec4a22e67 174 printf("Failed to get default NetworkInterface\n");
ccli8 0:f78ec4a22e67 175 return -1;
ccli8 0:f78ec4a22e67 176 }
ccli8 0:f78ec4a22e67 177 status = network->connect();
ccli8 0:f78ec4a22e67 178 if (status != NSAPI_ERROR_OK) {
ccli8 0:f78ec4a22e67 179 printf("NetworkInterface failed to connect with %d\n", status);
ccli8 0:f78ec4a22e67 180 return -1;
ccli8 0:f78ec4a22e67 181 }
ccli8 11:d147172e94b5 182 status = network->get_ip_address(&sa);
ccli8 11:d147172e94b5 183 if (status!=0) {
ccli8 11:d147172e94b5 184 printf("get_ip_address failed with %d\n", status);
ccli8 11:d147172e94b5 185 return -2;
ccli8 11:d147172e94b5 186 }
ccli8 11:d147172e94b5 187 printf("Network initialized, connected with IP %s\n\n", sa.get_ip_address());
ccli8 0:f78ec4a22e67 188
ccli8 0:f78ec4a22e67 189 // Run developer flow
ccli8 0:f78ec4a22e67 190 printf("Start developer flow\n");
ccli8 0:f78ec4a22e67 191 status = fcc_init();
ccli8 0:f78ec4a22e67 192 if (status != FCC_STATUS_SUCCESS) {
ccli8 0:f78ec4a22e67 193 printf("fcc_init() failed with %d\n", status);
ccli8 0:f78ec4a22e67 194 return -1;
ccli8 0:f78ec4a22e67 195 }
ccli8 0:f78ec4a22e67 196
ccli8 0:f78ec4a22e67 197 // Inject hardcoded entropy for the device. Suitable only for demo devices.
ccli8 0:f78ec4a22e67 198 (void) fcc_entropy_set(MBED_CLOUD_DEV_ENTROPY, sizeof(MBED_CLOUD_DEV_ENTROPY));
ccli8 0:f78ec4a22e67 199 status = fcc_developer_flow();
ccli8 0:f78ec4a22e67 200 if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_KCM_FILE_EXIST_ERROR && status != FCC_STATUS_CA_ERROR) {
ccli8 0:f78ec4a22e67 201 printf("fcc_developer_flow() failed with %d\n", status);
ccli8 0:f78ec4a22e67 202 return -1;
ccli8 0:f78ec4a22e67 203 }
ccli8 0:f78ec4a22e67 204
ccli8 11:d147172e94b5 205 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
ccli8 11:d147172e94b5 206 cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error, NULL, update_progress);
ccli8 11:d147172e94b5 207 #else
ccli8 11:d147172e94b5 208 cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error);
ccli8 11:d147172e94b5 209 #endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE
ccli8 11:d147172e94b5 210
ccli8 11:d147172e94b5 211 // Initialize client
ccli8 11:d147172e94b5 212 cloud_client->init();
ccli8 11:d147172e94b5 213
ccli8 0:f78ec4a22e67 214 printf("Create resources\n");
ccli8 0:f78ec4a22e67 215 M2MObjectList m2m_obj_list;
ccli8 0:f78ec4a22e67 216
ccli8 0:f78ec4a22e67 217 // GET resource 3200/0/5501
ccli8 11:d147172e94b5 218 // PUT also allowed for resetting the resource
ccli8 11:d147172e94b5 219 m2m_get_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3200, 0, 5501, M2MResourceInstance::INTEGER, M2MBase::GET_PUT_ALLOWED);
ccli8 0:f78ec4a22e67 220 if (m2m_get_res->set_value(0) != true) {
ccli8 0:f78ec4a22e67 221 printf("m2m_get_res->set_value() failed\n");
ccli8 0:f78ec4a22e67 222 return -1;
ccli8 0:f78ec4a22e67 223 }
ccli8 11:d147172e94b5 224 if (m2m_get_res->set_value_updated_function(get_res_update) != true) {
ccli8 11:d147172e94b5 225 printf("m2m_get_res->set_value_updated_function() failed\n");
ccli8 11:d147172e94b5 226 return -1;
ccli8 11:d147172e94b5 227 }
ccli8 0:f78ec4a22e67 228
ccli8 0:f78ec4a22e67 229 // PUT resource 3201/0/5853
ccli8 0:f78ec4a22e67 230 m2m_put_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5853, M2MResourceInstance::INTEGER, M2MBase::GET_PUT_ALLOWED);
ccli8 0:f78ec4a22e67 231 if (m2m_put_res->set_value(0) != true) {
ccli8 11:d147172e94b5 232 printf("m2m_put_res->set_value() failed\n");
ccli8 0:f78ec4a22e67 233 return -1;
ccli8 0:f78ec4a22e67 234 }
ccli8 11:d147172e94b5 235 if (m2m_put_res->set_value_updated_function(put_res_update) != true) {
ccli8 0:f78ec4a22e67 236 printf("m2m_put_res->set_value_updated_function() failed\n");
ccli8 0:f78ec4a22e67 237 return -1;
ccli8 0:f78ec4a22e67 238 }
ccli8 0:f78ec4a22e67 239
ccli8 0:f78ec4a22e67 240 // POST resource 3201/0/5850
ccli8 0:f78ec4a22e67 241 m2m_post_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5850, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED);
ccli8 0:f78ec4a22e67 242 if (m2m_post_res->set_execute_function(execute_post) != true) {
ccli8 0:f78ec4a22e67 243 printf("m2m_post_res->set_execute_function() failed\n");
ccli8 0:f78ec4a22e67 244 return -1;
ccli8 0:f78ec4a22e67 245 }
ccli8 0:f78ec4a22e67 246
ccli8 0:f78ec4a22e67 247 // POST resource 5000/0/1 to trigger deregister.
ccli8 0:f78ec4a22e67 248 m2m_deregister_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 5000, 0, 1, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED);
ccli8 0:f78ec4a22e67 249
ccli8 0:f78ec4a22e67 250 // Use delayed response
ccli8 0:f78ec4a22e67 251 m2m_deregister_res->set_delayed_response(true);
ccli8 0:f78ec4a22e67 252
ccli8 0:f78ec4a22e67 253 if (m2m_deregister_res->set_execute_function(deregister) != true) {
ccli8 0:f78ec4a22e67 254 printf("m2m_post_res->set_execute_function() failed\n");
ccli8 0:f78ec4a22e67 255 return -1;
ccli8 0:f78ec4a22e67 256 }
ccli8 0:f78ec4a22e67 257
ccli8 11:d147172e94b5 258 // optional Device resource for running factory reset for the device. Path of this resource will be: 3/0/5.
ccli8 11:d147172e94b5 259 m2m_factory_reset_res = M2MInterfaceFactory::create_device()->create_resource(M2MDevice::FactoryReset);
ccli8 11:d147172e94b5 260 if (m2m_factory_reset_res) {
ccli8 11:d147172e94b5 261 m2m_factory_reset_res->set_execute_function(factory_reset);
ccli8 11:d147172e94b5 262 }
ccli8 11:d147172e94b5 263
ccli8 0:f78ec4a22e67 264 printf("Register Pelion Device Management Client\n\n");
ccli8 11:d147172e94b5 265
ccli8 11:d147172e94b5 266 cloud_client->on_registration_updated(client_registration_updated);
ccli8 11:d147172e94b5 267
ccli8 0:f78ec4a22e67 268 cloud_client->add_objects(m2m_obj_list);
ccli8 11:d147172e94b5 269 cloud_client->setup(network);
ccli8 11:d147172e94b5 270
ccli8 11:d147172e94b5 271 t.start(callback(&queue, &EventQueue::dispatch_forever));
ccli8 11:d147172e94b5 272 queue.call_every(5000, value_increment);
ccli8 11:d147172e94b5 273
ccli8 11:d147172e94b5 274 // Flush the stdin buffer before reading from it
ccli8 11:d147172e94b5 275 flush_stdin_buffer();
ccli8 0:f78ec4a22e67 276
ccli8 0:f78ec4a22e67 277 while(cloud_client_running) {
ccli8 0:f78ec4a22e67 278 int in_char = getchar();
ccli8 0:f78ec4a22e67 279 if (in_char == 'i') {
ccli8 0:f78ec4a22e67 280 print_client_ids(); // When 'i' is pressed, print endpoint info
ccli8 0:f78ec4a22e67 281 continue;
ccli8 0:f78ec4a22e67 282 } else if (in_char == 'r') {
ccli8 0:f78ec4a22e67 283 (void) fcc_storage_delete(); // When 'r' is pressed, erase storage and reboot the board.
ccli8 0:f78ec4a22e67 284 printf("Storage erased, rebooting the device.\n\n");
ccli8 11:d147172e94b5 285 ThisThread::sleep_for(1*1000);
ccli8 0:f78ec4a22e67 286 NVIC_SystemReset();
ccli8 5:ae686808e015 287 } else if (dispatch_host_command && in_char != 0x03) {
ccli8 5:ae686808e015 288 /* Intercept other host commands */
ccli8 5:ae686808e015 289 dispatch_host_command(in_char);
ccli8 5:ae686808e015 290 continue;
ccli8 0:f78ec4a22e67 291 } else if (in_char > 0 && in_char != 0x03) { // Ctrl+C is 0x03 in Mbed OS and Linux returns negative number
ccli8 11:d147172e94b5 292 value_increment(); // Simulate button press
ccli8 0:f78ec4a22e67 293 continue;
ccli8 0:f78ec4a22e67 294 }
ccli8 0:f78ec4a22e67 295 deregister_client();
ccli8 0:f78ec4a22e67 296 break;
ccli8 0:f78ec4a22e67 297 }
ccli8 0:f78ec4a22e67 298 return 0;
ccli8 0:f78ec4a22e67 299 }
ccli8 0:f78ec4a22e67 300
ccli8 0:f78ec4a22e67 301 #endif /* MBED_TEST_MODE */