Toyomasa Watarai
/
Mbed-example-WS-W27
simple-mbed-cloud-client/mbed-cloud-client/mbed-client-pal/Source/PAL-Impl/pal_init.c@0:119624335925, 2018-06-30 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Jun 30 01:40:30 2018 +0000
- Revision:
- 0:119624335925
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:119624335925 | 1 | /* |
MACRUM | 0:119624335925 | 2 | * Copyright (c) 2016 ARM Limited. All rights reserved. |
MACRUM | 0:119624335925 | 3 | * SPDX-License-Identifier: Apache-2.0 |
MACRUM | 0:119624335925 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
MACRUM | 0:119624335925 | 5 | * not use this file except in compliance with the License. |
MACRUM | 0:119624335925 | 6 | * You may obtain a copy of the License at |
MACRUM | 0:119624335925 | 7 | * |
MACRUM | 0:119624335925 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:119624335925 | 9 | * |
MACRUM | 0:119624335925 | 10 | * Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:119624335925 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
MACRUM | 0:119624335925 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:119624335925 | 13 | * See the License for the specific language governing permissions and |
MACRUM | 0:119624335925 | 14 | * limitations under the License. |
MACRUM | 0:119624335925 | 15 | */ |
MACRUM | 0:119624335925 | 16 | |
MACRUM | 0:119624335925 | 17 | |
MACRUM | 0:119624335925 | 18 | #include "pal.h" |
MACRUM | 0:119624335925 | 19 | #include "pal_plat_network.h" |
MACRUM | 0:119624335925 | 20 | #include "pal_plat_TLS.h" |
MACRUM | 0:119624335925 | 21 | #include "pal_plat_Crypto.h" |
MACRUM | 0:119624335925 | 22 | #include "pal_macros.h" |
MACRUM | 0:119624335925 | 23 | |
MACRUM | 0:119624335925 | 24 | |
MACRUM | 0:119624335925 | 25 | |
MACRUM | 0:119624335925 | 26 | //this variable must be a int32_t for using atomic increment |
MACRUM | 0:119624335925 | 27 | PAL_PRIVATE int32_t g_palIntialized = 0; |
MACRUM | 0:119624335925 | 28 | |
MACRUM | 0:119624335925 | 29 | |
MACRUM | 0:119624335925 | 30 | PAL_PRIVATE void pal_modulesCleanup(void) |
MACRUM | 0:119624335925 | 31 | { |
MACRUM | 0:119624335925 | 32 | DEBUG_PRINT("Destroying modules\r\n"); |
MACRUM | 0:119624335925 | 33 | pal_plat_cleanupTLS(); |
MACRUM | 0:119624335925 | 34 | pal_plat_socketsTerminate(NULL); |
MACRUM | 0:119624335925 | 35 | pal_RTOSDestroy(); |
MACRUM | 0:119624335925 | 36 | pal_plat_cleanupCrypto(); |
MACRUM | 0:119624335925 | 37 | pal_internalFlashDeInit(); |
MACRUM | 0:119624335925 | 38 | } |
MACRUM | 0:119624335925 | 39 | |
MACRUM | 0:119624335925 | 40 | |
MACRUM | 0:119624335925 | 41 | palStatus_t pal_init(void) |
MACRUM | 0:119624335925 | 42 | { |
MACRUM | 0:119624335925 | 43 | |
MACRUM | 0:119624335925 | 44 | palStatus_t status = PAL_SUCCESS; |
MACRUM | 0:119624335925 | 45 | int32_t currentInitValue; |
MACRUM | 0:119624335925 | 46 | // get the return value of g_palIntialized+1 to save it locally |
MACRUM | 0:119624335925 | 47 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized,1); |
MACRUM | 0:119624335925 | 48 | // if increased for the 1st time |
MACRUM | 0:119624335925 | 49 | if (1 == currentInitValue) |
MACRUM | 0:119624335925 | 50 | { |
MACRUM | 0:119624335925 | 51 | DEBUG_PRINT("\nInit for the 1st time, initializing the modules\r\n"); |
MACRUM | 0:119624335925 | 52 | status = pal_RTOSInitialize(NULL); |
MACRUM | 0:119624335925 | 53 | if (PAL_SUCCESS == status) |
MACRUM | 0:119624335925 | 54 | { |
MACRUM | 0:119624335925 | 55 | DEBUG_PRINT("\n1. Network init\r\n"); |
MACRUM | 0:119624335925 | 56 | status = pal_plat_socketsInit(NULL); |
MACRUM | 0:119624335925 | 57 | if (PAL_SUCCESS != status) |
MACRUM | 0:119624335925 | 58 | { |
MACRUM | 0:119624335925 | 59 | DEBUG_PRINT("init of network module has failed with status %" PRIu32 "\r\n",status); |
MACRUM | 0:119624335925 | 60 | } |
MACRUM | 0:119624335925 | 61 | else //socket init succeeded |
MACRUM | 0:119624335925 | 62 | { |
MACRUM | 0:119624335925 | 63 | DEBUG_PRINT("\n2. TLS init\r\n"); |
MACRUM | 0:119624335925 | 64 | status = pal_plat_initTLSLibrary(); |
MACRUM | 0:119624335925 | 65 | if (PAL_SUCCESS != status) |
MACRUM | 0:119624335925 | 66 | { |
MACRUM | 0:119624335925 | 67 | DEBUG_PRINT("init of tls module has failed with status %" PRIu32 "\r\n",status); |
MACRUM | 0:119624335925 | 68 | } |
MACRUM | 0:119624335925 | 69 | else |
MACRUM | 0:119624335925 | 70 | { |
MACRUM | 0:119624335925 | 71 | DEBUG_PRINT("\n3. Crypto init\r\n"); |
MACRUM | 0:119624335925 | 72 | status = pal_plat_initCrypto(); |
MACRUM | 0:119624335925 | 73 | if (PAL_SUCCESS != status) |
MACRUM | 0:119624335925 | 74 | { |
MACRUM | 0:119624335925 | 75 | DEBUG_PRINT("init of crypto module has failed with status %" PRIu32 "\r\n",status); |
MACRUM | 0:119624335925 | 76 | } |
MACRUM | 0:119624335925 | 77 | else |
MACRUM | 0:119624335925 | 78 | { |
MACRUM | 0:119624335925 | 79 | DEBUG_PRINT("\n4. Internal Flash init\r\n"); |
MACRUM | 0:119624335925 | 80 | status = pal_internalFlashInit(); |
MACRUM | 0:119624335925 | 81 | if (PAL_SUCCESS != status) |
MACRUM | 0:119624335925 | 82 | { |
MACRUM | 0:119624335925 | 83 | DEBUG_PRINT("init of Internal Flash module has failed with status %" PRIu32 "\r\n",status); |
MACRUM | 0:119624335925 | 84 | } |
MACRUM | 0:119624335925 | 85 | } |
MACRUM | 0:119624335925 | 86 | } |
MACRUM | 0:119624335925 | 87 | } |
MACRUM | 0:119624335925 | 88 | } |
MACRUM | 0:119624335925 | 89 | else |
MACRUM | 0:119624335925 | 90 | { |
MACRUM | 0:119624335925 | 91 | DEBUG_PRINT("init of RTOS module has failed with status %" PRIu32 "\r\n",status); |
MACRUM | 0:119624335925 | 92 | } |
MACRUM | 0:119624335925 | 93 | |
MACRUM | 0:119624335925 | 94 | // if failed decrease the value of g_palIntialized |
MACRUM | 0:119624335925 | 95 | if (PAL_SUCCESS != status) |
MACRUM | 0:119624335925 | 96 | { |
MACRUM | 0:119624335925 | 97 | pal_modulesCleanup(); |
MACRUM | 0:119624335925 | 98 | pal_osAtomicIncrement(&g_palIntialized, -1); |
MACRUM | 0:119624335925 | 99 | PAL_LOG(ERR,"\nInit failed\r\n"); |
MACRUM | 0:119624335925 | 100 | } |
MACRUM | 0:119624335925 | 101 | } |
MACRUM | 0:119624335925 | 102 | |
MACRUM | 0:119624335925 | 103 | |
MACRUM | 0:119624335925 | 104 | return status; |
MACRUM | 0:119624335925 | 105 | } |
MACRUM | 0:119624335925 | 106 | |
MACRUM | 0:119624335925 | 107 | |
MACRUM | 0:119624335925 | 108 | int32_t pal_destroy(void) |
MACRUM | 0:119624335925 | 109 | { |
MACRUM | 0:119624335925 | 110 | int32_t currentInitValue; |
MACRUM | 0:119624335925 | 111 | // get the current value of g_palIntialized locally |
MACRUM | 0:119624335925 | 112 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized, 0); |
MACRUM | 0:119624335925 | 113 | if(currentInitValue != 0) |
MACRUM | 0:119624335925 | 114 | { |
MACRUM | 0:119624335925 | 115 | currentInitValue = pal_osAtomicIncrement(&g_palIntialized, -1); |
MACRUM | 0:119624335925 | 116 | if (0 == currentInitValue) |
MACRUM | 0:119624335925 | 117 | { |
MACRUM | 0:119624335925 | 118 | pal_modulesCleanup(); |
MACRUM | 0:119624335925 | 119 | } |
MACRUM | 0:119624335925 | 120 | } |
MACRUM | 0:119624335925 | 121 | return currentInitValue; |
MACRUM | 0:119624335925 | 122 | } |