Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform_alt.cpp Source File

platform_alt.cpp

00001  /*
00002   *  platform_alt.c
00003   *
00004   *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00005   *  SPDX-License-Identifier: Apache-2.0
00006   *
00007   *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008   *  not use this file except in compliance with the License.
00009   *  You may obtain a copy of the License at
00010   *
00011   *  http://www.apache.org/licenses/LICENSE-2.0
00012   *
00013   *  Unless required by applicable law or agreed to in writing, software
00014   *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015   *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   *  See the License for the specific language governing permissions and
00017   *  limitations under the License.
00018   *
00019   */
00020 
00021 #include "mbedtls/platform.h"
00022 #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
00023 #include "platform/SingletonPtr.h"
00024 #include "platform/PlatformMutex.h"
00025 
00026 mbedtls_platform_context plat_ctx = { { 0 } };
00027 extern SingletonPtr<PlatformMutex>  mbedtls_mutex;
00028 
00029 int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
00030 {
00031     int ret = 0;
00032     mbedtls_mutex->lock();
00033     ++plat_ctx.reference_count;
00034 
00035     if( plat_ctx.reference_count == 1 )
00036     {
00037         /* call platform specific code to setup crypto driver */
00038         ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx );
00039     }
00040     mbedtls_mutex->unlock();
00041     return ( ret );
00042 }
00043 
00044 void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx )
00045 {
00046     mbedtls_mutex->lock();
00047     --plat_ctx.reference_count;
00048     if( plat_ctx.reference_count < 1 )
00049     {
00050         /* call platform specific code to terminate crypto driver */
00051         crypto_platform_terminate( &plat_ctx.platform_impl_ctx );
00052         plat_ctx.reference_count = 0;
00053     }
00054     mbedtls_mutex->unlock();
00055 }
00056 
00057 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/