Ram Gandikota / Mbed OS ABCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_init.c Source File

pal_init.c

00001 /*
00002 * Copyright (c) 2016 ARM Limited. All rights reserved.
00003 * SPDX-License-Identifier: Apache-2.0
00004 * Licensed under the Apache License, Version 2.0 (the License); you may
00005 * 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, WITHOUT
00012 * 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_rtos.h"
00020 #include "pal_plat_network.h"
00021 #include "pal_macros.h"
00022 
00023 //this variable must be a int32_t for using atomic increment
00024 static int32_t g_palIntialized = 0;
00025 
00026 
00027 palStatus_t pal_init()
00028 {
00029 
00030     palStatus_t status = PAL_SUCCESS;
00031     int32_t currentInitValue;
00032     //  get the return value of g_palIntialized+1 to save it locally
00033     currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1);
00034     // if increased for the 1st time
00035     if (1 == currentInitValue)
00036     {
00037         DEBUG_PRINT("Init for the 1st time, initializing the modules\r\n");
00038         status = pal_plat_RTOSInitialize(NULL);
00039         if (PAL_SUCCESS == status)
00040         {
00041 
00042             status = pal_plat_socketsInit(NULL);
00043             if (PAL_SUCCESS != status)
00044             {
00045                 DEBUG_PRINT("init of network module has failed with status %d\r\n",status);
00046             }
00047         }
00048         else
00049         {
00050             DEBUG_PRINT("init of RTOS module has failed with status %d\r\n",status);
00051         }
00052     }
00053     // if failed decrees the value of g_palIntialized
00054     if (PAL_SUCCESS != status)
00055     {
00056         pal_plat_socketsTerminate(NULL);
00057         pal_plat_RTOSDestroy();
00058         pal_osAtomicIncrement(&g_palIntialized, -1);
00059     }
00060     return status;
00061 }
00062 
00063 
00064 void pal_destroy()
00065 {
00066     int32_t currentInitValue;
00067     // get the current value of g_palIntialized locally
00068     currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1);
00069     if (0 == currentInitValue)
00070     {
00071         DEBUG_PRINT("Destroying modules\r\n");
00072         pal_plat_RTOSDestroy();
00073         pal_plat_socketsTerminate(NULL);
00074     }
00075 }