NuMaker connection with AWS IoT thru MQTT/HTTPS (Mbed OS 6)
Dependencies: MQTT
provision.cpp
00001 /* 00002 * Copyright (c) 2019 Nuvoton Technology Corporation 00003 * 00004 * SPDX-License-Identifier: Apache-2.0 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00019 #include "mbed.h" 00020 #include "mbedtls/config.h" 00021 #include "entropy_poll.h" 00022 //#include "psa/crypto.h" 00023 #if DEVICE_FLASH 00024 #include "kvstore_global_api.h" 00025 #include "KVStore.h" 00026 #include "TDBStore.h" 00027 #include "KVMap.h" 00028 #include "kv_config.h" 00029 #endif 00030 00031 #ifndef __STDC_FORMAT_MACROS 00032 #define __STDC_FORMAT_MACROS 00033 #endif 00034 #include <inttypes.h> 00035 00036 /* Simulate provision process for development 00037 * 00038 * 1. Reset kvstore 00039 * 2. Inject entropy seed (if no entropy source) 00040 * 3. Initialize user filesystem (if enabled) 00041 * 4. Mark the device as provisioned 00042 * 00043 * WARNING: For mass production, remove this file and run real provision process. 00044 */ 00045 00046 /* Check weak reference/definition at the link: 00047 * http://www.keil.com/support/man/docs/ARMLINK/armlink_pge1362065917715.htm */ 00048 00049 extern "C" { 00050 MBED_USED void provision(void); 00051 } 00052 00053 /* Stringize */ 00054 #define STR_EXPAND(tok) #tok 00055 #define STR(tok) STR_EXPAND(tok) 00056 00057 #define _GET_FILESYSTEM_concat(dev, ...) _get_filesystem_##dev(__VA_ARGS__) 00058 #define GET_FILESYSTEM(dev, ...) _GET_FILESYSTEM_concat(dev, __VA_ARGS__) 00059 00060 /* Key for the device provisioned */ 00061 #define KV_KEY_PROVISION "provision" 00062 00063 void provision(void) 00064 { 00065 #if DEVICE_FLASH 00066 00067 int kv_reset(const char *kvstore_path); 00068 00069 /* Initialize kvstore */ 00070 int kv_status = kv_init_storage_config(); 00071 if (kv_status != MBED_SUCCESS) { 00072 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "Initialize kvstore failed", kv_status); 00073 } 00074 00075 /* Get kvstore internal storage */ 00076 KVMap &kv_map = KVMap::get_instance(); 00077 KVStore *inner_store = kv_map.get_internal_kv_instance(NULL); 00078 if (inner_store == NULL) { 00079 MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "kvstore internal storage failed"); 00080 } 00081 00082 /* Check if the device has provisioned */ 00083 KVStore::info_t kv_info; 00084 kv_status = inner_store->get_info(KV_KEY_PROVISION, &kv_info); 00085 if (kv_status == MBED_SUCCESS) { 00086 do { 00087 /* Get KV_KEY_PROVISION key */ 00088 char buffer[4]; 00089 size_t actual_size = 0; 00090 int kv_status = inner_store->get(KV_KEY_PROVISION, buffer, sizeof(buffer), &actual_size); 00091 if (kv_status != MBED_SUCCESS) { 00092 printf("Get \'%s\' failed: %d\r\n", KV_KEY_PROVISION, kv_status); 00093 break; 00094 } 00095 /* Check KV_KEY_PROVISION key's value */ 00096 if (actual_size != 1 || buffer[0] != '1') { 00097 printf("\"%s\" not equal \"%s\"\r\n", KV_KEY_PROVISION, "1"); 00098 break; 00099 } 00100 00101 printf("The device has provisioned. Skip provision process\r\n"); 00102 return; 00103 } while (0); 00104 } else if (kv_status == MBED_ERROR_ITEM_NOT_FOUND) { 00105 /* Not provisioned yet */ 00106 printf("The device has not provisioned yet. Try to provision it...\r\n"); 00107 } else { 00108 printf("Get \'%s\' key failed: %d\r\n", KV_KEY_PROVISION, kv_status); 00109 } 00110 00111 /* Provision from here */ 00112 printf("Provision for development...\r\n"); 00113 00114 printf("Reset kvstore...\r\n"); 00115 00116 /* Reset kvstore for clean kvstore */ 00117 kv_status = kv_reset("/" STR(MBED_CONF_STORAGE_DEFAULT_KV) "/"); 00118 if (kv_status != MBED_SUCCESS) { 00119 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "kv_reset() failed", kv_status); 00120 } 00121 00122 printf("\rReset kvstore...OK\r\n"); 00123 00124 #if !DEVICE_TRNG && !TARGET_PSA && !TARGET_PSA_Target 00125 #if !defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 00126 /* Inject trivial seed for development */ 00127 00128 printf("Inject NV seed...\r\n"); 00129 00130 psa_status_t psa_status; 00131 uint8_t seed[SEED_SIZE] = { 0 }; 00132 00133 /* First inject seed, expect OK or seed has injected by some provision process */ 00134 psa_status = mbedtls_psa_inject_entropy(seed, sizeof(seed)); 00135 if (psa_status != PSA_SUCCESS && psa_status != PSA_ERROR_NOT_PERMITTED) { 00136 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "Inject entropy failed", psa_status); 00137 } 00138 00139 /* Second inject seed, expect seed has injected above or by some provision process */ 00140 psa_status = mbedtls_psa_inject_entropy(seed, sizeof(seed)); 00141 if (psa_status != PSA_ERROR_NOT_PERMITTED) { 00142 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "Re-jnject entropy expects PSA_ERROR_NOT_PERMITTED", psa_status); 00143 } 00144 00145 printf("\rInject NV seed...OK\r\n"); 00146 #endif /* !defined(MBEDTLS_ENTROPY_HARDWARE_ALT) */ 00147 #endif /* #if !DEVICE_TRNG && !TARGET_PSA && !TARGET_PSA_Target */ 00148 00149 /* Mark the device as provisioned */ 00150 kv_status = inner_store->set(KV_KEY_PROVISION, "1", 1, KVStore::WRITE_ONCE_FLAG); 00151 if (kv_status != MBED_SUCCESS) { 00152 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNKNOWN), "Mark the device as provisioned failed", kv_status); 00153 } 00154 00155 printf("Provision for development...OK\r\n"); 00156 00157 #endif /* #if DEVICE_FLASH */ 00158 }
Generated on Wed Mar 1 2023 03:55:05 by 1.7.2