joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform.c Source File

platform.c

00001 /*
00002  *  Platform abstraction layer
00003  *
00004  *  Copyright (C) 2006-2016, 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  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 #if !defined(MBEDTLS_CONFIG_FILE)
00023 #include "mbedtls/config.h"
00024 #else
00025 #include MBEDTLS_CONFIG_FILE
00026 #endif
00027 
00028 #if defined(MBEDTLS_PLATFORM_C)
00029 
00030 #include "mbedtls/platform.h"
00031 
00032 #if defined(MBEDTLS_PLATFORM_MEMORY)
00033 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
00034 static void *platform_calloc_uninit( size_t n, size_t size )
00035 {
00036     ((void) n);
00037     ((void) size);
00038     return( NULL );
00039 }
00040 
00041 #define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
00042 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
00043 
00044 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
00045 static void platform_free_uninit( void *ptr )
00046 {
00047     ((void) ptr);
00048 }
00049 
00050 #define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
00051 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
00052 
00053 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
00054 void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
00055 
00056 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
00057                               void (*free_func)( void * ) )
00058 {
00059     mbedtls_calloc = calloc_func;
00060     mbedtls_free = free_func;
00061     return( 0 );
00062 }
00063 #endif /* MBEDTLS_PLATFORM_MEMORY */
00064 
00065 #if defined(_WIN32)
00066 #include <stdarg.h>
00067 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
00068 {
00069     int ret;
00070     va_list argp;
00071 
00072     /* Avoid calling the invalid parameter handler by checking ourselves */
00073     if( s == NULL || n == 0 || fmt == NULL )
00074         return( -1 );
00075 
00076     va_start( argp, fmt );
00077 #if defined(_TRUNCATE)
00078     ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
00079 #else
00080     ret = _vsnprintf( s, n, fmt, argp );
00081     if( ret < 0 || (size_t) ret == n )
00082     {
00083         s[n-1] = '\0';
00084         ret = -1;
00085     }
00086 #endif
00087     va_end( argp );
00088 
00089     return( ret );
00090 }
00091 #endif
00092 
00093 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
00094 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
00095 /*
00096  * Make dummy function to prevent NULL pointer dereferences
00097  */
00098 static int platform_snprintf_uninit( char * s, size_t n,
00099                                      const char * format, ... )
00100 {
00101     ((void) s);
00102     ((void) n);
00103     ((void) format);
00104     return( 0 );
00105 }
00106 
00107 #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
00108 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
00109 
00110 int (*mbedtls_snprintf)( char * s, size_t n,
00111                           const char * format,
00112                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
00113 
00114 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
00115                                                  const char * format,
00116                                                  ... ) )
00117 {
00118     mbedtls_snprintf = snprintf_func;
00119     return( 0 );
00120 }
00121 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
00122 
00123 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
00124 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
00125 /*
00126  * Make dummy function to prevent NULL pointer dereferences
00127  */
00128 static int platform_printf_uninit( const char *format, ... )
00129 {
00130     ((void) format);
00131     return( 0 );
00132 }
00133 
00134 #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
00135 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
00136 
00137 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
00138 
00139 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
00140 {
00141     mbedtls_printf = printf_func;
00142     return( 0 );
00143 }
00144 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
00145 
00146 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
00147 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
00148 /*
00149  * Make dummy function to prevent NULL pointer dereferences
00150  */
00151 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
00152 {
00153     ((void) stream);
00154     ((void) format);
00155     return( 0 );
00156 }
00157 
00158 #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
00159 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
00160 
00161 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
00162                                         MBEDTLS_PLATFORM_STD_FPRINTF;
00163 
00164 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
00165 {
00166     mbedtls_fprintf = fprintf_func;
00167     return( 0 );
00168 }
00169 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
00170 
00171 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
00172 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
00173 /*
00174  * Make dummy function to prevent NULL pointer dereferences
00175  */
00176 static void platform_exit_uninit( int status )
00177 {
00178     ((void) status);
00179 }
00180 
00181 #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
00182 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
00183 
00184 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
00185 
00186 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
00187 {
00188     mbedtls_exit = exit_func;
00189     return( 0 );
00190 }
00191 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
00192 
00193 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
00194 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
00195 /*
00196  * Make dummy function to prevent NULL pointer dereferences
00197  */
00198 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
00199 {
00200     ((void) timer);
00201     return( 0 );
00202 }
00203 
00204 #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
00205 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
00206 
00207 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
00208 
00209 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
00210 {
00211     mbedtls_time = time_func;
00212     return( 0 );
00213 }
00214 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
00215 
00216 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00217 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
00218 /* Default implementations for the platform independent seed functions use
00219  * standard libc file functions to read from and write to a pre-defined filename
00220  */
00221 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
00222 {
00223     FILE *file;
00224     size_t n;
00225 
00226     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
00227         return -1;
00228 
00229     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
00230     {
00231         fclose( file );
00232         return -1;
00233     }
00234 
00235     fclose( file );
00236     return( n );
00237 }
00238 
00239 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
00240 {
00241     FILE *file;
00242     size_t n;
00243 
00244     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
00245         return -1;
00246 
00247     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
00248     {
00249         fclose( file );
00250         return -1;
00251     }
00252 
00253     fclose( file );
00254     return( n );
00255 }
00256 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
00257 
00258 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
00259 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
00260 /*
00261  * Make dummy function to prevent NULL pointer dereferences
00262  */
00263 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
00264 {
00265     ((void) buf);
00266     ((void) buf_len);
00267     return( -1 );
00268 }
00269 
00270 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
00271 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
00272 
00273 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
00274 /*
00275  * Make dummy function to prevent NULL pointer dereferences
00276  */
00277 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
00278 {
00279     ((void) buf);
00280     ((void) buf_len);
00281     return( -1 );
00282 }
00283 
00284 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
00285 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
00286 
00287 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
00288             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
00289 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
00290             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
00291 
00292 int mbedtls_platform_set_nv_seed(
00293         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
00294         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
00295 {
00296     mbedtls_nv_seed_read = nv_seed_read_func;
00297     mbedtls_nv_seed_write = nv_seed_write_func;
00298     return( 0 );
00299 }
00300 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
00301 #endif /* MBEDTLS_ENTROPY_NV_SEED */
00302 
00303 #endif /* MBEDTLS_PLATFORM_C */