mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Platform abstraction layer
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 5 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 8 * not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 *
elessair 0:f269e3021894 19 * This file is part of mbed TLS (https://tls.mbed.org)
elessair 0:f269e3021894 20 */
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #if !defined(MBEDTLS_CONFIG_FILE)
elessair 0:f269e3021894 23 #include "mbedtls/config.h"
elessair 0:f269e3021894 24 #else
elessair 0:f269e3021894 25 #include MBEDTLS_CONFIG_FILE
elessair 0:f269e3021894 26 #endif
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 #if defined(MBEDTLS_PLATFORM_C)
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 #include "mbedtls/platform.h"
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 #if defined(MBEDTLS_PLATFORM_MEMORY)
elessair 0:f269e3021894 33 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
elessair 0:f269e3021894 34 static void *platform_calloc_uninit( size_t n, size_t size )
elessair 0:f269e3021894 35 {
elessair 0:f269e3021894 36 ((void) n);
elessair 0:f269e3021894 37 ((void) size);
elessair 0:f269e3021894 38 return( NULL );
elessair 0:f269e3021894 39 }
elessair 0:f269e3021894 40
elessair 0:f269e3021894 41 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
elessair 0:f269e3021894 42 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
elessair 0:f269e3021894 45 static void platform_free_uninit( void *ptr )
elessair 0:f269e3021894 46 {
elessair 0:f269e3021894 47 ((void) ptr);
elessair 0:f269e3021894 48 }
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
elessair 0:f269e3021894 51 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
elessair 0:f269e3021894 54 void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
elessair 0:f269e3021894 57 void (*free_func)( void * ) )
elessair 0:f269e3021894 58 {
elessair 0:f269e3021894 59 mbedtls_calloc = calloc_func;
elessair 0:f269e3021894 60 mbedtls_free = free_func;
elessair 0:f269e3021894 61 return( 0 );
elessair 0:f269e3021894 62 }
elessair 0:f269e3021894 63 #endif /* MBEDTLS_PLATFORM_MEMORY */
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 #if defined(_WIN32)
elessair 0:f269e3021894 66 #include <stdarg.h>
elessair 0:f269e3021894 67 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
elessair 0:f269e3021894 68 {
elessair 0:f269e3021894 69 int ret;
elessair 0:f269e3021894 70 va_list argp;
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 /* Avoid calling the invalid parameter handler by checking ourselves */
elessair 0:f269e3021894 73 if( s == NULL || n == 0 || fmt == NULL )
elessair 0:f269e3021894 74 return( -1 );
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 va_start( argp, fmt );
elessair 0:f269e3021894 77 #if defined(_TRUNCATE)
elessair 0:f269e3021894 78 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
elessair 0:f269e3021894 79 #else
elessair 0:f269e3021894 80 ret = _vsnprintf( s, n, fmt, argp );
elessair 0:f269e3021894 81 if( ret < 0 || (size_t) ret == n )
elessair 0:f269e3021894 82 {
elessair 0:f269e3021894 83 s[n-1] = '\0';
elessair 0:f269e3021894 84 ret = -1;
elessair 0:f269e3021894 85 }
elessair 0:f269e3021894 86 #endif
elessair 0:f269e3021894 87 va_end( argp );
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 return( ret );
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91 #endif
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
elessair 0:f269e3021894 94 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
elessair 0:f269e3021894 95 /*
elessair 0:f269e3021894 96 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 97 */
elessair 0:f269e3021894 98 static int platform_snprintf_uninit( char * s, size_t n,
elessair 0:f269e3021894 99 const char * format, ... )
elessair 0:f269e3021894 100 {
elessair 0:f269e3021894 101 ((void) s);
elessair 0:f269e3021894 102 ((void) n);
elessair 0:f269e3021894 103 ((void) format);
elessair 0:f269e3021894 104 return( 0 );
elessair 0:f269e3021894 105 }
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
elessair 0:f269e3021894 108 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 int (*mbedtls_snprintf)( char * s, size_t n,
elessair 0:f269e3021894 111 const char * format,
elessair 0:f269e3021894 112 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
elessair 0:f269e3021894 113
elessair 0:f269e3021894 114 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
elessair 0:f269e3021894 115 const char * format,
elessair 0:f269e3021894 116 ... ) )
elessair 0:f269e3021894 117 {
elessair 0:f269e3021894 118 mbedtls_snprintf = snprintf_func;
elessair 0:f269e3021894 119 return( 0 );
elessair 0:f269e3021894 120 }
elessair 0:f269e3021894 121 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
elessair 0:f269e3021894 124 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
elessair 0:f269e3021894 125 /*
elessair 0:f269e3021894 126 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 127 */
elessair 0:f269e3021894 128 static int platform_printf_uninit( const char *format, ... )
elessair 0:f269e3021894 129 {
elessair 0:f269e3021894 130 ((void) format);
elessair 0:f269e3021894 131 return( 0 );
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
elessair 0:f269e3021894 135 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
elessair 0:f269e3021894 138
elessair 0:f269e3021894 139 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
elessair 0:f269e3021894 140 {
elessair 0:f269e3021894 141 mbedtls_printf = printf_func;
elessair 0:f269e3021894 142 return( 0 );
elessair 0:f269e3021894 143 }
elessair 0:f269e3021894 144 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
elessair 0:f269e3021894 145
elessair 0:f269e3021894 146 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
elessair 0:f269e3021894 147 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
elessair 0:f269e3021894 148 /*
elessair 0:f269e3021894 149 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 150 */
elessair 0:f269e3021894 151 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
elessair 0:f269e3021894 152 {
elessair 0:f269e3021894 153 ((void) stream);
elessair 0:f269e3021894 154 ((void) format);
elessair 0:f269e3021894 155 return( 0 );
elessair 0:f269e3021894 156 }
elessair 0:f269e3021894 157
elessair 0:f269e3021894 158 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
elessair 0:f269e3021894 159 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
elessair 0:f269e3021894 162 MBEDTLS_PLATFORM_STD_FPRINTF;
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
elessair 0:f269e3021894 165 {
elessair 0:f269e3021894 166 mbedtls_fprintf = fprintf_func;
elessair 0:f269e3021894 167 return( 0 );
elessair 0:f269e3021894 168 }
elessair 0:f269e3021894 169 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
elessair 0:f269e3021894 170
elessair 0:f269e3021894 171 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
elessair 0:f269e3021894 172 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
elessair 0:f269e3021894 173 /*
elessair 0:f269e3021894 174 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 175 */
elessair 0:f269e3021894 176 static void platform_exit_uninit( int status )
elessair 0:f269e3021894 177 {
elessair 0:f269e3021894 178 ((void) status);
elessair 0:f269e3021894 179 }
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
elessair 0:f269e3021894 182 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
elessair 0:f269e3021894 183
elessair 0:f269e3021894 184 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
elessair 0:f269e3021894 187 {
elessair 0:f269e3021894 188 mbedtls_exit = exit_func;
elessair 0:f269e3021894 189 return( 0 );
elessair 0:f269e3021894 190 }
elessair 0:f269e3021894 191 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
elessair 0:f269e3021894 192
elessair 0:f269e3021894 193 #if defined(MBEDTLS_HAVE_TIME)
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
elessair 0:f269e3021894 196 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
elessair 0:f269e3021894 197 /*
elessair 0:f269e3021894 198 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 199 */
elessair 0:f269e3021894 200 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
elessair 0:f269e3021894 201 {
elessair 0:f269e3021894 202 ((void) timer);
elessair 0:f269e3021894 203 return( 0 );
elessair 0:f269e3021894 204 }
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
elessair 0:f269e3021894 207 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
elessair 0:f269e3021894 212 {
elessair 0:f269e3021894 213 mbedtls_time = time_func;
elessair 0:f269e3021894 214 return( 0 );
elessair 0:f269e3021894 215 }
elessair 0:f269e3021894 216 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
elessair 0:f269e3021894 217
elessair 0:f269e3021894 218 #endif /* MBEDTLS_HAVE_TIME */
elessair 0:f269e3021894 219
elessair 0:f269e3021894 220 #if defined(MBEDTLS_ENTROPY_NV_SEED)
elessair 0:f269e3021894 221 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
elessair 0:f269e3021894 222 /* Default implementations for the platform independent seed functions use
elessair 0:f269e3021894 223 * standard libc file functions to read from and write to a pre-defined filename
elessair 0:f269e3021894 224 */
elessair 0:f269e3021894 225 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
elessair 0:f269e3021894 226 {
elessair 0:f269e3021894 227 FILE *file;
elessair 0:f269e3021894 228 size_t n;
elessair 0:f269e3021894 229
elessair 0:f269e3021894 230 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
elessair 0:f269e3021894 231 return -1;
elessair 0:f269e3021894 232
elessair 0:f269e3021894 233 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
elessair 0:f269e3021894 234 {
elessair 0:f269e3021894 235 fclose( file );
elessair 0:f269e3021894 236 return -1;
elessair 0:f269e3021894 237 }
elessair 0:f269e3021894 238
elessair 0:f269e3021894 239 fclose( file );
elessair 0:f269e3021894 240 return( n );
elessair 0:f269e3021894 241 }
elessair 0:f269e3021894 242
elessair 0:f269e3021894 243 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
elessair 0:f269e3021894 244 {
elessair 0:f269e3021894 245 FILE *file;
elessair 0:f269e3021894 246 size_t n;
elessair 0:f269e3021894 247
elessair 0:f269e3021894 248 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
elessair 0:f269e3021894 249 return -1;
elessair 0:f269e3021894 250
elessair 0:f269e3021894 251 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
elessair 0:f269e3021894 252 {
elessair 0:f269e3021894 253 fclose( file );
elessair 0:f269e3021894 254 return -1;
elessair 0:f269e3021894 255 }
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 fclose( file );
elessair 0:f269e3021894 258 return( n );
elessair 0:f269e3021894 259 }
elessair 0:f269e3021894 260 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
elessair 0:f269e3021894 261
elessair 0:f269e3021894 262 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
elessair 0:f269e3021894 263 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
elessair 0:f269e3021894 264 /*
elessair 0:f269e3021894 265 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 266 */
elessair 0:f269e3021894 267 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
elessair 0:f269e3021894 268 {
elessair 0:f269e3021894 269 ((void) buf);
elessair 0:f269e3021894 270 ((void) buf_len);
elessair 0:f269e3021894 271 return( -1 );
elessair 0:f269e3021894 272 }
elessair 0:f269e3021894 273
elessair 0:f269e3021894 274 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
elessair 0:f269e3021894 275 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
elessair 0:f269e3021894 278 /*
elessair 0:f269e3021894 279 * Make dummy function to prevent NULL pointer dereferences
elessair 0:f269e3021894 280 */
elessair 0:f269e3021894 281 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
elessair 0:f269e3021894 282 {
elessair 0:f269e3021894 283 ((void) buf);
elessair 0:f269e3021894 284 ((void) buf_len);
elessair 0:f269e3021894 285 return( -1 );
elessair 0:f269e3021894 286 }
elessair 0:f269e3021894 287
elessair 0:f269e3021894 288 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
elessair 0:f269e3021894 289 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
elessair 0:f269e3021894 290
elessair 0:f269e3021894 291 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
elessair 0:f269e3021894 292 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
elessair 0:f269e3021894 293 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
elessair 0:f269e3021894 294 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
elessair 0:f269e3021894 295
elessair 0:f269e3021894 296 int mbedtls_platform_set_nv_seed(
elessair 0:f269e3021894 297 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
elessair 0:f269e3021894 298 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
elessair 0:f269e3021894 299 {
elessair 0:f269e3021894 300 mbedtls_nv_seed_read = nv_seed_read_func;
elessair 0:f269e3021894 301 mbedtls_nv_seed_write = nv_seed_write_func;
elessair 0:f269e3021894 302 return( 0 );
elessair 0:f269e3021894 303 }
elessair 0:f269e3021894 304 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
elessair 0:f269e3021894 305 #endif /* MBEDTLS_ENTROPY_NV_SEED */
elessair 0:f269e3021894 306
elessair 0:f269e3021894 307 #endif /* MBEDTLS_PLATFORM_C */