Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
simple-mbed-cloud-client/mbed-cloud-client/mbed-client-pal/Source/PAL-Impl/pal_init.c@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /******************************************************************************* |
leothedragon | 0:8f0bb79ddd48 | 2 | * Copyright 2016-2019 ARM Ltd. |
leothedragon | 0:8f0bb79ddd48 | 3 | * |
leothedragon | 0:8f0bb79ddd48 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
leothedragon | 0:8f0bb79ddd48 | 5 | * you may not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 6 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 7 | * |
leothedragon | 0:8f0bb79ddd48 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 9 | * |
leothedragon | 0:8f0bb79ddd48 | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
leothedragon | 0:8f0bb79ddd48 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 14 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 15 | *******************************************************************************/ |
leothedragon | 0:8f0bb79ddd48 | 16 | |
leothedragon | 0:8f0bb79ddd48 | 17 | |
leothedragon | 0:8f0bb79ddd48 | 18 | #include "pal.h" |
leothedragon | 0:8f0bb79ddd48 | 19 | #include "pal_plat_network.h" |
leothedragon | 0:8f0bb79ddd48 | 20 | #include "pal_plat_TLS.h" |
leothedragon | 0:8f0bb79ddd48 | 21 | #include "pal_plat_Crypto.h" |
leothedragon | 0:8f0bb79ddd48 | 22 | #include "pal_plat_drbg.h" |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "pal_macros.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | #include "sotp.h" |
leothedragon | 0:8f0bb79ddd48 | 25 | |
leothedragon | 0:8f0bb79ddd48 | 26 | #define TRACE_GROUP "PAL" |
leothedragon | 0:8f0bb79ddd48 | 27 | |
leothedragon | 0:8f0bb79ddd48 | 28 | //this variable must be a int32_t for using atomic increment |
leothedragon | 0:8f0bb79ddd48 | 29 | PAL_PRIVATE int32_t g_palIntialized = 0; |
leothedragon | 0:8f0bb79ddd48 | 30 | |
leothedragon | 0:8f0bb79ddd48 | 31 | PAL_PRIVATE void pal_modulesCleanup(void) |
leothedragon | 0:8f0bb79ddd48 | 32 | { |
leothedragon | 0:8f0bb79ddd48 | 33 | DEBUG_PRINT("Destroying modules\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 34 | pal_plat_socketsTerminate(NULL); |
leothedragon | 0:8f0bb79ddd48 | 35 | pal_plat_DRBGDestroy(); |
leothedragon | 0:8f0bb79ddd48 | 36 | #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT |
leothedragon | 0:8f0bb79ddd48 | 37 | sotp_deinit(); |
leothedragon | 0:8f0bb79ddd48 | 38 | #endif |
leothedragon | 0:8f0bb79ddd48 | 39 | pal_plat_cleanupCrypto(); |
leothedragon | 0:8f0bb79ddd48 | 40 | pal_cleanupTLS(); |
leothedragon | 0:8f0bb79ddd48 | 41 | pal_fsCleanup(); |
leothedragon | 0:8f0bb79ddd48 | 42 | #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT |
leothedragon | 0:8f0bb79ddd48 | 43 | #if PAL_USE_INTERNAL_FLASH |
leothedragon | 0:8f0bb79ddd48 | 44 | pal_internalFlashDeInit(); |
leothedragon | 0:8f0bb79ddd48 | 45 | #endif |
leothedragon | 0:8f0bb79ddd48 | 46 | #endif |
leothedragon | 0:8f0bb79ddd48 | 47 | pal_RTOSDestroy(); |
leothedragon | 0:8f0bb79ddd48 | 48 | } |
leothedragon | 0:8f0bb79ddd48 | 49 | |
leothedragon | 0:8f0bb79ddd48 | 50 | |
leothedragon | 0:8f0bb79ddd48 | 51 | |
leothedragon | 0:8f0bb79ddd48 | 52 | palStatus_t pal_init(void) |
leothedragon | 0:8f0bb79ddd48 | 53 | { |
leothedragon | 0:8f0bb79ddd48 | 54 | |
leothedragon | 0:8f0bb79ddd48 | 55 | palStatus_t status = PAL_SUCCESS; |
leothedragon | 0:8f0bb79ddd48 | 56 | sotp_result_e sotpStatus = SOTP_SUCCESS; |
leothedragon | 0:8f0bb79ddd48 | 57 | int32_t currentInitValue; |
leothedragon | 0:8f0bb79ddd48 | 58 | // get the return value of g_palIntialized+1 to save it locally |
leothedragon | 0:8f0bb79ddd48 | 59 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1); |
leothedragon | 0:8f0bb79ddd48 | 60 | // if increased for the 1st time |
leothedragon | 0:8f0bb79ddd48 | 61 | if (1 == currentInitValue) |
leothedragon | 0:8f0bb79ddd48 | 62 | { |
leothedragon | 0:8f0bb79ddd48 | 63 | DEBUG_PRINT("Init for the 1st time, initializing the modules\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 64 | status = pal_RTOSInitialize(NULL); |
leothedragon | 0:8f0bb79ddd48 | 65 | if (PAL_SUCCESS == status) |
leothedragon | 0:8f0bb79ddd48 | 66 | { |
leothedragon | 0:8f0bb79ddd48 | 67 | DEBUG_PRINT("Network init\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 68 | status = pal_plat_socketsInit(NULL); |
leothedragon | 0:8f0bb79ddd48 | 69 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 70 | { |
leothedragon | 0:8f0bb79ddd48 | 71 | DEBUG_PRINT("init of network module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 72 | } |
leothedragon | 0:8f0bb79ddd48 | 73 | else //socket init succeeded |
leothedragon | 0:8f0bb79ddd48 | 74 | { |
leothedragon | 0:8f0bb79ddd48 | 75 | DEBUG_PRINT("TLS init\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 76 | status = pal_initTLSLibrary(); |
leothedragon | 0:8f0bb79ddd48 | 77 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 78 | { |
leothedragon | 0:8f0bb79ddd48 | 79 | DEBUG_PRINT("init of tls module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 80 | } |
leothedragon | 0:8f0bb79ddd48 | 81 | else |
leothedragon | 0:8f0bb79ddd48 | 82 | { |
leothedragon | 0:8f0bb79ddd48 | 83 | DEBUG_PRINT("Crypto init\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 84 | status = pal_plat_initCrypto(); |
leothedragon | 0:8f0bb79ddd48 | 85 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 86 | { |
leothedragon | 0:8f0bb79ddd48 | 87 | DEBUG_PRINT("init of crypto module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 88 | } |
leothedragon | 0:8f0bb79ddd48 | 89 | else |
leothedragon | 0:8f0bb79ddd48 | 90 | { |
leothedragon | 0:8f0bb79ddd48 | 91 | DEBUG_PRINT("Internal Flash init\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 92 | |
leothedragon | 0:8f0bb79ddd48 | 93 | #if PAL_USE_INTERNAL_FLASH |
leothedragon | 0:8f0bb79ddd48 | 94 | status = pal_internalFlashInit(); |
leothedragon | 0:8f0bb79ddd48 | 95 | #endif |
leothedragon | 0:8f0bb79ddd48 | 96 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 97 | { |
leothedragon | 0:8f0bb79ddd48 | 98 | DEBUG_PRINT("init of Internal Flash module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 99 | } |
leothedragon | 0:8f0bb79ddd48 | 100 | |
leothedragon | 0:8f0bb79ddd48 | 101 | else |
leothedragon | 0:8f0bb79ddd48 | 102 | { |
leothedragon | 0:8f0bb79ddd48 | 103 | #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT |
leothedragon | 0:8f0bb79ddd48 | 104 | DEBUG_PRINT("SOTP init\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 105 | |
leothedragon | 0:8f0bb79ddd48 | 106 | sotpStatus = sotp_init(); |
leothedragon | 0:8f0bb79ddd48 | 107 | #endif |
leothedragon | 0:8f0bb79ddd48 | 108 | if (SOTP_SUCCESS != sotpStatus) |
leothedragon | 0:8f0bb79ddd48 | 109 | { |
leothedragon | 0:8f0bb79ddd48 | 110 | DEBUG_PRINT("init of SOTP module has failed with status %" PRIx32 "\r\n", (int32_t)sotpStatus); |
leothedragon | 0:8f0bb79ddd48 | 111 | status = PAL_ERR_INIT_SOTP_FAILED; |
leothedragon | 0:8f0bb79ddd48 | 112 | } |
leothedragon | 0:8f0bb79ddd48 | 113 | if (PAL_SUCCESS == status) |
leothedragon | 0:8f0bb79ddd48 | 114 | { |
leothedragon | 0:8f0bb79ddd48 | 115 | status = pal_plat_DRBGInit(); |
leothedragon | 0:8f0bb79ddd48 | 116 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 117 | { |
leothedragon | 0:8f0bb79ddd48 | 118 | DEBUG_PRINT("init of DRBG module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 119 | } |
leothedragon | 0:8f0bb79ddd48 | 120 | } |
leothedragon | 0:8f0bb79ddd48 | 121 | } |
leothedragon | 0:8f0bb79ddd48 | 122 | } |
leothedragon | 0:8f0bb79ddd48 | 123 | } |
leothedragon | 0:8f0bb79ddd48 | 124 | } |
leothedragon | 0:8f0bb79ddd48 | 125 | } |
leothedragon | 0:8f0bb79ddd48 | 126 | else |
leothedragon | 0:8f0bb79ddd48 | 127 | { |
leothedragon | 0:8f0bb79ddd48 | 128 | DEBUG_PRINT("init of RTOS module has failed with status %" PRIx32 "\r\n",status); |
leothedragon | 0:8f0bb79ddd48 | 129 | } |
leothedragon | 0:8f0bb79ddd48 | 130 | |
leothedragon | 0:8f0bb79ddd48 | 131 | // if failed decrease the value of g_palIntialized |
leothedragon | 0:8f0bb79ddd48 | 132 | if (PAL_SUCCESS != status) |
leothedragon | 0:8f0bb79ddd48 | 133 | { |
leothedragon | 0:8f0bb79ddd48 | 134 | #if PAL_CLEANUP_ON_INIT_FAILURE |
leothedragon | 0:8f0bb79ddd48 | 135 | pal_modulesCleanup(); |
leothedragon | 0:8f0bb79ddd48 | 136 | pal_osAtomicIncrement(&g_palIntialized, -1); |
leothedragon | 0:8f0bb79ddd48 | 137 | #endif |
leothedragon | 0:8f0bb79ddd48 | 138 | PAL_LOG_ERR("\nInit failed\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 139 | } |
leothedragon | 0:8f0bb79ddd48 | 140 | } |
leothedragon | 0:8f0bb79ddd48 | 141 | |
leothedragon | 0:8f0bb79ddd48 | 142 | DEBUG_PRINT("FINISH PAL INIT\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 143 | return status; |
leothedragon | 0:8f0bb79ddd48 | 144 | } |
leothedragon | 0:8f0bb79ddd48 | 145 | |
leothedragon | 0:8f0bb79ddd48 | 146 | |
leothedragon | 0:8f0bb79ddd48 | 147 | int32_t pal_destroy(void) |
leothedragon | 0:8f0bb79ddd48 | 148 | { |
leothedragon | 0:8f0bb79ddd48 | 149 | int32_t currentInitValue; |
leothedragon | 0:8f0bb79ddd48 | 150 | // get the current value of g_palIntialized locally |
leothedragon | 0:8f0bb79ddd48 | 151 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized, 0); |
leothedragon | 0:8f0bb79ddd48 | 152 | if(currentInitValue != 0) |
leothedragon | 0:8f0bb79ddd48 | 153 | { |
leothedragon | 0:8f0bb79ddd48 | 154 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1); |
leothedragon | 0:8f0bb79ddd48 | 155 | if (0 == currentInitValue) |
leothedragon | 0:8f0bb79ddd48 | 156 | { |
leothedragon | 0:8f0bb79ddd48 | 157 | pal_modulesCleanup(); |
leothedragon | 0:8f0bb79ddd48 | 158 | } |
leothedragon | 0:8f0bb79ddd48 | 159 | } |
leothedragon | 0:8f0bb79ddd48 | 160 | |
leothedragon | 0:8f0bb79ddd48 | 161 | return currentInitValue; |
leothedragon | 0:8f0bb79ddd48 | 162 | } |
leothedragon | 0:8f0bb79ddd48 | 163 | |
leothedragon | 0:8f0bb79ddd48 | 164 |