Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_init.c Source File

pal_init.c

00001 /*******************************************************************************
00002  * Copyright 2016-2019 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_plat_drbg.h"
00023 #include "pal_macros.h"
00024 #include "sotp.h"
00025 
00026 #define TRACE_GROUP "PAL"
00027 
00028 //this variable must be a int32_t for using atomic increment
00029 PAL_PRIVATE int32_t g_palIntialized = 0;
00030 
00031 PAL_PRIVATE void pal_modulesCleanup(void)
00032 {
00033     DEBUG_PRINT("Destroying modules\r\n");
00034     pal_plat_socketsTerminate(NULL);
00035     pal_plat_DRBGDestroy();
00036 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00037     sotp_deinit();
00038 #endif
00039     pal_plat_cleanupCrypto();
00040     pal_cleanupTLS();
00041     pal_fsCleanup();
00042 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00043     #if PAL_USE_INTERNAL_FLASH
00044         pal_internalFlashDeInit();
00045     #endif
00046 #endif
00047     pal_RTOSDestroy();
00048 }
00049 
00050 
00051 
00052 palStatus_t pal_init(void)
00053 {
00054 
00055     palStatus_t status = PAL_SUCCESS;
00056     sotp_result_e sotpStatus = SOTP_SUCCESS;
00057     int32_t currentInitValue;
00058     //  get the return value of g_palIntialized+1 to save it locally
00059     currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1);
00060     // if increased for the 1st time
00061     if (1 == currentInitValue)
00062     {
00063         DEBUG_PRINT("Init for the 1st time, initializing the modules\r\n");
00064         status = pal_RTOSInitialize(NULL);
00065         if (PAL_SUCCESS == status)
00066         {
00067             DEBUG_PRINT("Network init\r\n");
00068             status = pal_plat_socketsInit(NULL);
00069             if (PAL_SUCCESS != status)
00070             {
00071                 DEBUG_PRINT("init of network module has failed with status %" PRIx32 "\r\n",status);
00072             }
00073             else //socket init succeeded
00074             {
00075                 DEBUG_PRINT("TLS init\r\n");
00076                 status = pal_initTLSLibrary();
00077                 if (PAL_SUCCESS != status)
00078                 {
00079                     DEBUG_PRINT("init of tls module has failed with status %" PRIx32 "\r\n",status);
00080                 }
00081                 else
00082                 {
00083                     DEBUG_PRINT("Crypto init\r\n");
00084                     status = pal_plat_initCrypto();
00085                     if (PAL_SUCCESS != status)
00086                     {
00087                         DEBUG_PRINT("init of crypto module has failed with status %" PRIx32 "\r\n",status);
00088                     }
00089                     else
00090                     {
00091                         DEBUG_PRINT("Internal Flash init\r\n");
00092 
00093                         #if PAL_USE_INTERNAL_FLASH
00094                             status = pal_internalFlashInit();
00095                         #endif
00096                         if (PAL_SUCCESS != status)
00097                         {
00098                             DEBUG_PRINT("init of Internal Flash module has failed with status %" PRIx32 "\r\n",status);
00099                         }
00100 
00101                         else
00102                         {
00103 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00104                             DEBUG_PRINT("SOTP init\r\n");
00105 
00106                             sotpStatus = sotp_init();
00107 #endif
00108                             if (SOTP_SUCCESS != sotpStatus)
00109                             {
00110                                 DEBUG_PRINT("init of SOTP module has failed with status %" PRIx32 "\r\n", (int32_t)sotpStatus);
00111                                 status = PAL_ERR_INIT_SOTP_FAILED ;
00112                             }
00113                             if (PAL_SUCCESS == status)
00114                             {
00115                                 status = pal_plat_DRBGInit();
00116                                 if (PAL_SUCCESS != status)
00117                                 {
00118                                     DEBUG_PRINT("init of DRBG module has failed with status %" PRIx32 "\r\n",status);
00119                                 }
00120                             }
00121                         }
00122                     }
00123                 }
00124             }
00125         }
00126         else
00127         {
00128             DEBUG_PRINT("init of RTOS module has failed with status %" PRIx32 "\r\n",status);
00129         }
00130 
00131         // if failed decrease the value of g_palIntialized
00132         if (PAL_SUCCESS != status)
00133         {
00134 #if PAL_CLEANUP_ON_INIT_FAILURE           
00135             pal_modulesCleanup();
00136             pal_osAtomicIncrement(&g_palIntialized, -1);
00137 #endif
00138             PAL_LOG_ERR("\nInit failed\r\n");
00139         }
00140     }
00141 
00142     DEBUG_PRINT("FINISH PAL INIT\r\n");
00143     return status;
00144 }
00145 
00146 
00147 int32_t  pal_destroy(void)
00148 {
00149     int32_t currentInitValue;
00150     // get the current value of g_palIntialized locally
00151     currentInitValue = pal_osAtomicIncrement(&g_palIntialized, 0);
00152     if(currentInitValue != 0)
00153     {
00154         currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1);
00155         if (0 == currentInitValue)
00156         {
00157             pal_modulesCleanup();
00158         }
00159     }
00160 
00161     return currentInitValue;
00162 }
00163 
00164