Simple interface for Mbed Cloud Client
Embed:
(wiki syntax)
Show/hide line numbers
pal_init.c
00001 /******************************************************************************* 00002 * Copyright 2016, 2017 ARM Ltd. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 *******************************************************************************/ 00016 00017 00018 #include "pal.h" 00019 #include "pal_plat_network.h" 00020 #include "pal_plat_TLS.h" 00021 #include "pal_plat_Crypto.h" 00022 #include "pal_macros.h" 00023 #include "sotp.h" 00024 00025 00026 //this variable must be a int32_t for using atomic increment 00027 PAL_PRIVATE int32_t g_palIntialized = 0; 00028 00029 00030 PAL_PRIVATE void pal_modulesCleanup(void) 00031 { 00032 DEBUG_PRINT("Destroying modules\r\n"); 00033 pal_plat_socketsTerminate (NULL); 00034 sotp_deinit(); 00035 pal_RTOSDestroy(); 00036 pal_plat_cleanupCrypto (); 00037 pal_cleanupTLS(); 00038 pal_fsCleanup(); 00039 #if PAL_USE_INTERNAL_FLASH 00040 pal_internalFlashDeInit(); 00041 #endif 00042 } 00043 00044 00045 00046 palStatus_t pal_init(void) 00047 { 00048 00049 palStatus_t status = PAL_SUCCESS; 00050 sotp_result_e sotpStatus = SOTP_SUCCESS; 00051 int32_t currentInitValue; 00052 // get the return value of g_palIntialized+1 to save it locally 00053 currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1); 00054 // if increased for the 1st time 00055 if (1 == currentInitValue) 00056 { 00057 DEBUG_PRINT("\nInit for the 1st time, initializing the modules\r\n"); 00058 status = pal_RTOSInitialize(NULL); 00059 if (PAL_SUCCESS == status) 00060 { 00061 DEBUG_PRINT("Network init\r\n"); 00062 status = pal_plat_socketsInit(NULL); 00063 if (PAL_SUCCESS != status) 00064 { 00065 DEBUG_PRINT("init of network module has failed with status %" PRIx32 "\r\n",status); 00066 } 00067 else //socket init succeeded 00068 { 00069 DEBUG_PRINT("TLS init\r\n"); 00070 status = pal_initTLSLibrary(); 00071 if (PAL_SUCCESS != status) 00072 { 00073 DEBUG_PRINT("init of tls module has failed with status %" PRIx32 "\r\n",status); 00074 } 00075 else 00076 { 00077 DEBUG_PRINT("Crypto init\r\n"); 00078 status = pal_plat_initCrypto (); 00079 if (PAL_SUCCESS != status) 00080 { 00081 DEBUG_PRINT("init of crypto module has failed with status %" PRIx32 "\r\n",status); 00082 } 00083 else 00084 { 00085 DEBUG_PRINT("Internal Flash init\r\n"); 00086 #if PAL_USE_INTERNAL_FLASH 00087 status = pal_internalFlashInit(); 00088 #endif 00089 if (PAL_SUCCESS != status) 00090 { 00091 DEBUG_PRINT("init of Internal Flash module has failed with status %" PRIx32 "\r\n",status); 00092 } 00093 00094 else 00095 { 00096 DEBUG_PRINT("SOTP init\r\n"); 00097 sotpStatus = sotp_init(); 00098 if (SOTP_SUCCESS != sotpStatus) 00099 { 00100 DEBUG_PRINT("init of SOTP module has failed with status %" PRIx32 "\r\n",status); 00101 } 00102 00103 else 00104 { 00105 status = pal_initTime(); 00106 if (PAL_SUCCESS != status) 00107 { 00108 DEBUG_PRINT("init of Time module has failed with status %" PRIx32 "\r\n",status); 00109 } 00110 } 00111 } 00112 } 00113 } 00114 } 00115 if (PAL_SUCCESS != status) 00116 { 00117 DEBUG_PRINT("init of Time module has failed with status %" PRIx32 "\r\n",status); 00118 } 00119 } 00120 else 00121 { 00122 DEBUG_PRINT("init of RTOS module has failed with status %" PRIx32 "\r\n",status); 00123 } 00124 00125 // if failed decrease the value of g_palIntialized 00126 if (PAL_SUCCESS != status) 00127 { 00128 #if PAL_CLEANUP_ON_INIT_FAILURE 00129 pal_modulesCleanup(); 00130 pal_osAtomicIncrement(&g_palIntialized, -1); 00131 #endif 00132 PAL_LOG(ERR,"\nInit failed\r\n"); 00133 } 00134 } 00135 00136 00137 DEBUG_PRINT("FINISH PAL INIT\r\n"); 00138 return status; 00139 } 00140 00141 00142 int32_t pal_destroy(void) 00143 { 00144 int32_t currentInitValue; 00145 // get the current value of g_palIntialized locally 00146 currentInitValue = pal_osAtomicIncrement(&g_palIntialized, 0); 00147 if(currentInitValue != 0) 00148 { 00149 currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1); 00150 if (0 == currentInitValue) 00151 { 00152 pal_modulesCleanup(); 00153 } 00154 } 00155 return currentInitValue; 00156 } 00157 00158
Generated on Tue Jul 12 2022 19:01:36 by 1.7.2