Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /*
bryantaylor 0:eafc3fd41f75 2 * Threading abstraction layer
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
bryantaylor 0:eafc3fd41f75 5 * SPDX-License-Identifier: Apache-2.0
bryantaylor 0:eafc3fd41f75 6 *
bryantaylor 0:eafc3fd41f75 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
bryantaylor 0:eafc3fd41f75 8 * not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 9 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 10 *
bryantaylor 0:eafc3fd41f75 11 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 12 *
bryantaylor 0:eafc3fd41f75 13 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
bryantaylor 0:eafc3fd41f75 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 16 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 17 * limitations under the License.
bryantaylor 0:eafc3fd41f75 18 *
bryantaylor 0:eafc3fd41f75 19 * This file is part of mbed TLS (https://tls.mbed.org)
bryantaylor 0:eafc3fd41f75 20 */
bryantaylor 0:eafc3fd41f75 21
bryantaylor 0:eafc3fd41f75 22 #if !defined(MBEDTLS_CONFIG_FILE)
bryantaylor 0:eafc3fd41f75 23 #include "mbedtls/config.h"
bryantaylor 0:eafc3fd41f75 24 #else
bryantaylor 0:eafc3fd41f75 25 #include MBEDTLS_CONFIG_FILE
bryantaylor 0:eafc3fd41f75 26 #endif
bryantaylor 0:eafc3fd41f75 27
bryantaylor 0:eafc3fd41f75 28 #if defined(MBEDTLS_THREADING_C)
bryantaylor 0:eafc3fd41f75 29
bryantaylor 0:eafc3fd41f75 30 #include "mbedtls/threading.h"
bryantaylor 0:eafc3fd41f75 31
bryantaylor 0:eafc3fd41f75 32 #if defined(MBEDTLS_THREADING_PTHREAD)
bryantaylor 0:eafc3fd41f75 33 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 34 {
bryantaylor 0:eafc3fd41f75 35 if( mutex == NULL )
bryantaylor 0:eafc3fd41f75 36 return;
bryantaylor 0:eafc3fd41f75 37
bryantaylor 0:eafc3fd41f75 38 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
bryantaylor 0:eafc3fd41f75 39 }
bryantaylor 0:eafc3fd41f75 40
bryantaylor 0:eafc3fd41f75 41 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 42 {
bryantaylor 0:eafc3fd41f75 43 if( mutex == NULL )
bryantaylor 0:eafc3fd41f75 44 return;
bryantaylor 0:eafc3fd41f75 45
bryantaylor 0:eafc3fd41f75 46 (void) pthread_mutex_destroy( &mutex->mutex );
bryantaylor 0:eafc3fd41f75 47 }
bryantaylor 0:eafc3fd41f75 48
bryantaylor 0:eafc3fd41f75 49 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 50 {
bryantaylor 0:eafc3fd41f75 51 if( mutex == NULL || ! mutex->is_valid )
bryantaylor 0:eafc3fd41f75 52 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 53
bryantaylor 0:eafc3fd41f75 54 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
bryantaylor 0:eafc3fd41f75 55 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
bryantaylor 0:eafc3fd41f75 56
bryantaylor 0:eafc3fd41f75 57 return( 0 );
bryantaylor 0:eafc3fd41f75 58 }
bryantaylor 0:eafc3fd41f75 59
bryantaylor 0:eafc3fd41f75 60 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 61 {
bryantaylor 0:eafc3fd41f75 62 if( mutex == NULL || ! mutex->is_valid )
bryantaylor 0:eafc3fd41f75 63 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 64
bryantaylor 0:eafc3fd41f75 65 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
bryantaylor 0:eafc3fd41f75 66 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
bryantaylor 0:eafc3fd41f75 67
bryantaylor 0:eafc3fd41f75 68 return( 0 );
bryantaylor 0:eafc3fd41f75 69 }
bryantaylor 0:eafc3fd41f75 70
bryantaylor 0:eafc3fd41f75 71 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
bryantaylor 0:eafc3fd41f75 72 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
bryantaylor 0:eafc3fd41f75 73 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
bryantaylor 0:eafc3fd41f75 74 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
bryantaylor 0:eafc3fd41f75 75
bryantaylor 0:eafc3fd41f75 76 /*
bryantaylor 0:eafc3fd41f75 77 * With phtreads we can statically initialize mutexes
bryantaylor 0:eafc3fd41f75 78 */
bryantaylor 0:eafc3fd41f75 79 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
bryantaylor 0:eafc3fd41f75 80
bryantaylor 0:eafc3fd41f75 81 #endif /* MBEDTLS_THREADING_PTHREAD */
bryantaylor 0:eafc3fd41f75 82
bryantaylor 0:eafc3fd41f75 83 #if defined(MBEDTLS_THREADING_ALT)
bryantaylor 0:eafc3fd41f75 84 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 85 {
bryantaylor 0:eafc3fd41f75 86 ((void) mutex );
bryantaylor 0:eafc3fd41f75 87 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 88 }
bryantaylor 0:eafc3fd41f75 89 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
bryantaylor 0:eafc3fd41f75 90 {
bryantaylor 0:eafc3fd41f75 91 ((void) mutex );
bryantaylor 0:eafc3fd41f75 92 return;
bryantaylor 0:eafc3fd41f75 93 }
bryantaylor 0:eafc3fd41f75 94
bryantaylor 0:eafc3fd41f75 95 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
bryantaylor 0:eafc3fd41f75 96 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
bryantaylor 0:eafc3fd41f75 97 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
bryantaylor 0:eafc3fd41f75 98 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
bryantaylor 0:eafc3fd41f75 99
bryantaylor 0:eafc3fd41f75 100 /*
bryantaylor 0:eafc3fd41f75 101 * Set functions pointers and initialize global mutexes
bryantaylor 0:eafc3fd41f75 102 */
bryantaylor 0:eafc3fd41f75 103 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
bryantaylor 0:eafc3fd41f75 104 void (*mutex_free)( mbedtls_threading_mutex_t * ),
bryantaylor 0:eafc3fd41f75 105 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
bryantaylor 0:eafc3fd41f75 106 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
bryantaylor 0:eafc3fd41f75 107 {
bryantaylor 0:eafc3fd41f75 108 mbedtls_mutex_init = mutex_init;
bryantaylor 0:eafc3fd41f75 109 mbedtls_mutex_free = mutex_free;
bryantaylor 0:eafc3fd41f75 110 mbedtls_mutex_lock = mutex_lock;
bryantaylor 0:eafc3fd41f75 111 mbedtls_mutex_unlock = mutex_unlock;
bryantaylor 0:eafc3fd41f75 112
bryantaylor 0:eafc3fd41f75 113 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
bryantaylor 0:eafc3fd41f75 114 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
bryantaylor 0:eafc3fd41f75 115 }
bryantaylor 0:eafc3fd41f75 116
bryantaylor 0:eafc3fd41f75 117 /*
bryantaylor 0:eafc3fd41f75 118 * Free global mutexes
bryantaylor 0:eafc3fd41f75 119 */
bryantaylor 0:eafc3fd41f75 120 void mbedtls_threading_free_alt( void )
bryantaylor 0:eafc3fd41f75 121 {
bryantaylor 0:eafc3fd41f75 122 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
bryantaylor 0:eafc3fd41f75 123 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
bryantaylor 0:eafc3fd41f75 124 }
bryantaylor 0:eafc3fd41f75 125 #endif /* MBEDTLS_THREADING_ALT */
bryantaylor 0:eafc3fd41f75 126
bryantaylor 0:eafc3fd41f75 127 /*
bryantaylor 0:eafc3fd41f75 128 * Define global mutexes
bryantaylor 0:eafc3fd41f75 129 */
bryantaylor 0:eafc3fd41f75 130 #ifndef MUTEX_INIT
bryantaylor 0:eafc3fd41f75 131 #define MUTEX_INIT
bryantaylor 0:eafc3fd41f75 132 #endif
bryantaylor 0:eafc3fd41f75 133 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
bryantaylor 0:eafc3fd41f75 134 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
bryantaylor 0:eafc3fd41f75 135
bryantaylor 0:eafc3fd41f75 136 #endif /* MBEDTLS_THREADING_C */