Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
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_plat_cleanupCrypto (); 00036 pal_cleanupTLS(); 00037 pal_fsCleanup(); 00038 #if PAL_USE_INTERNAL_FLASH 00039 pal_internalFlashDeInit(); 00040 #endif 00041 pal_RTOSDestroy(); 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", (int32_t)sotpStatus); 00101 status = PAL_ERR_NOT_INITIALIZED ; 00102 } 00103 if (PAL_SUCCESS == status) 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 } 00116 else 00117 { 00118 DEBUG_PRINT("init of RTOS module has failed with status %" PRIx32 "\r\n",status); 00119 } 00120 00121 // if failed decrease the value of g_palIntialized 00122 if (PAL_SUCCESS != status) 00123 { 00124 #if PAL_CLEANUP_ON_INIT_FAILURE 00125 pal_modulesCleanup(); 00126 pal_osAtomicIncrement(&g_palIntialized, -1); 00127 #endif 00128 PAL_LOG(ERR,"\nInit failed\r\n"); 00129 } 00130 } 00131 00132 DEBUG_PRINT("FINISH PAL INIT\r\n"); 00133 return status; 00134 } 00135 00136 00137 int32_t pal_destroy(void) 00138 { 00139 int32_t currentInitValue; 00140 // get the current value of g_palIntialized locally 00141 currentInitValue = pal_osAtomicIncrement(&g_palIntialized, 0); 00142 if(currentInitValue != 0) 00143 { 00144 currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1); 00145 if (0 == currentInitValue) 00146 { 00147 pal_modulesCleanup(); 00148 } 00149 } 00150 return currentInitValue; 00151 } 00152 00153
Generated on Tue Jul 12 2022 19:12:14 by 1.7.2