Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wc_port.c Source File

wc_port.c

00001 /* port.c
00002  *
00003  * Copyright (C) 2006-2014 wolfSSL Inc.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * CyaSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023     #include <config.h>
00024 #endif
00025 
00026 #include <cyassl/ctaocrypt/settings.h>
00027 #include <cyassl/ctaocrypt/types.h>
00028 #include <cyassl/ctaocrypt/error-crypt.h>
00029 
00030 
00031 #ifdef _MSC_VER
00032     /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
00033     #pragma warning(disable: 4996)
00034 #endif
00035 
00036 
00037 
00038 #ifdef SINGLE_THREADED
00039 
00040 int InitMutex(CyaSSL_Mutex* m)
00041 {
00042     (void)m;
00043     return 0;
00044 }
00045 
00046 
00047 int FreeMutex(CyaSSL_Mutex *m)
00048 {
00049     (void)m;
00050     return 0;
00051 }
00052 
00053 
00054 int LockMutex(CyaSSL_Mutex *m)
00055 {
00056     (void)m;
00057     return 0;
00058 }
00059 
00060 
00061 int UnLockMutex(CyaSSL_Mutex *m)
00062 {
00063     (void)m;
00064     return 0;
00065 }
00066 
00067 #else /* MULTI_THREAD */
00068 
00069     #if defined(FREERTOS)
00070 
00071         int InitMutex(CyaSSL_Mutex* m)
00072         {
00073             int iReturn;
00074 
00075             *m = ( CyaSSL_Mutex ) xSemaphoreCreateMutex();
00076             if( *m != NULL )
00077                 iReturn = 0;
00078             else
00079                 iReturn = BAD_MUTEX_E;
00080 
00081             return iReturn;
00082         }
00083 
00084         int FreeMutex(CyaSSL_Mutex* m)
00085         {
00086             vSemaphoreDelete( *m );
00087             return 0;
00088         }
00089 
00090         int LockMutex(CyaSSL_Mutex* m)
00091         {
00092             /* Assume an infinite block, or should there be zero block? */
00093             xSemaphoreTake( *m, portMAX_DELAY );
00094             return 0;
00095         }
00096 
00097         int UnLockMutex(CyaSSL_Mutex* m)
00098         {
00099             xSemaphoreGive( *m );
00100             return 0;
00101         }
00102 
00103     #elif defined(CYASSL_SAFERTOS)
00104 
00105         int InitMutex(CyaSSL_Mutex* m)
00106         {
00107             vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
00108             if (m->mutex == NULL)
00109                 return BAD_MUTEX_E;
00110 
00111             return 0;
00112         }
00113 
00114         int FreeMutex(CyaSSL_Mutex* m)
00115         {
00116             (void)m;
00117             return 0;
00118         }
00119 
00120         int LockMutex(CyaSSL_Mutex* m)
00121         {
00122             /* Assume an infinite block */
00123             xSemaphoreTake(m->mutex, portMAX_DELAY);
00124             return 0;
00125         }
00126 
00127         int UnLockMutex(CyaSSL_Mutex* m)
00128         {
00129             xSemaphoreGive(m->mutex);
00130             return 0;
00131         }
00132 
00133 
00134     #elif defined(USE_WINDOWS_API)
00135 
00136         int InitMutex(CyaSSL_Mutex* m)
00137         {
00138             InitializeCriticalSection(m);
00139             return 0;
00140         }
00141 
00142 
00143         int FreeMutex(CyaSSL_Mutex* m)
00144         {
00145             DeleteCriticalSection(m);
00146             return 0;
00147         }
00148 
00149 
00150         int LockMutex(CyaSSL_Mutex* m)
00151         {
00152             EnterCriticalSection(m);
00153             return 0;
00154         }
00155 
00156 
00157         int UnLockMutex(CyaSSL_Mutex* m)
00158         {
00159             LeaveCriticalSection(m);
00160             return 0;
00161         }
00162 
00163     #elif defined(CYASSL_PTHREADS)
00164 
00165         int InitMutex(CyaSSL_Mutex* m)
00166         {
00167             if (pthread_mutex_init(m, 0) == 0)
00168                 return 0;
00169             else
00170                 return BAD_MUTEX_E;
00171         }
00172 
00173 
00174         int FreeMutex(CyaSSL_Mutex* m)
00175         {
00176             if (pthread_mutex_destroy(m) == 0)
00177                 return 0;
00178             else
00179                 return BAD_MUTEX_E;
00180         }
00181 
00182 
00183         int LockMutex(CyaSSL_Mutex* m)
00184         {
00185             if (pthread_mutex_lock(m) == 0)
00186                 return 0;
00187             else
00188                 return BAD_MUTEX_E;
00189         }
00190 
00191 
00192         int UnLockMutex(CyaSSL_Mutex* m)
00193         {
00194             if (pthread_mutex_unlock(m) == 0)
00195                 return 0;
00196             else
00197                 return BAD_MUTEX_E;
00198         }
00199 
00200     #elif defined(THREADX)
00201 
00202         int InitMutex(CyaSSL_Mutex* m)
00203         {
00204             if (tx_mutex_create(m, "CyaSSL Mutex", TX_NO_INHERIT) == 0)
00205                 return 0;
00206             else
00207                 return BAD_MUTEX_E;
00208         }
00209 
00210 
00211         int FreeMutex(CyaSSL_Mutex* m)
00212         {
00213             if (tx_mutex_delete(m) == 0)
00214                 return 0;
00215             else
00216                 return BAD_MUTEX_E;
00217         }
00218 
00219 
00220         int LockMutex(CyaSSL_Mutex* m)
00221         {
00222             if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
00223                 return 0;
00224             else
00225                 return BAD_MUTEX_E;
00226         }
00227 
00228 
00229         int UnLockMutex(CyaSSL_Mutex* m)
00230         {
00231             if (tx_mutex_put(m) == 0)
00232                 return 0;
00233             else
00234                 return BAD_MUTEX_E;
00235         }
00236 
00237     #elif defined(MICRIUM)
00238 
00239         int InitMutex(CyaSSL_Mutex* m)
00240         {
00241             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00242                 if (NetSecure_OS_MutexCreate(m) == 0)
00243                     return 0;
00244                 else
00245                     return BAD_MUTEX_E;
00246             #else
00247                 return 0;
00248             #endif
00249         }
00250 
00251 
00252         int FreeMutex(CyaSSL_Mutex* m)
00253         {
00254             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00255                 if (NetSecure_OS_FreeMutex(m) == 0)
00256                     return 0;
00257                 else
00258                     return BAD_MUTEX_E;
00259             #else
00260                 return 0;
00261             #endif
00262         }
00263 
00264 
00265         int LockMutex(CyaSSL_Mutex* m)
00266         {
00267             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00268                 if (NetSecure_OS_LockMutex(m) == 0)
00269                     return 0;
00270                 else
00271                     return BAD_MUTEX_E;
00272             #else
00273                 return 0;
00274             #endif
00275         }
00276 
00277 
00278         int UnLockMutex(CyaSSL_Mutex* m)
00279         {
00280             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00281                 if (NetSecure_OS_UnLockMutex(m) == 0)
00282                     return 0;
00283                 else
00284                     return BAD_MUTEX_E;
00285             #else
00286                 return 0;
00287             #endif
00288 
00289         }
00290 
00291     #elif defined(EBSNET)
00292 
00293         int InitMutex(CyaSSL_Mutex* m)
00294         {
00295             if (rtp_sig_mutex_alloc(m, "CyaSSL Mutex") == -1)
00296                 return BAD_MUTEX_E;
00297             else
00298                 return 0;
00299         }
00300 
00301         int FreeMutex(CyaSSL_Mutex* m)
00302         {
00303             rtp_sig_mutex_free(*m);
00304             return 0;
00305         }
00306 
00307         int LockMutex(CyaSSL_Mutex* m)
00308         {
00309             if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
00310                 return 0;
00311             else
00312                 return BAD_MUTEX_E;
00313         }
00314 
00315         int UnLockMutex(CyaSSL_Mutex* m)
00316         {
00317             rtp_sig_mutex_release(*m);
00318             return 0;
00319         }
00320 
00321     #elif defined(FREESCALE_MQX)
00322 
00323         int InitMutex(CyaSSL_Mutex* m)
00324         {
00325             if (_mutex_init(m, NULL) == MQX_EOK)
00326                 return 0;
00327             else
00328                 return BAD_MUTEX_E;
00329         }
00330 
00331         int FreeMutex(CyaSSL_Mutex* m)
00332         {
00333             if (_mutex_destroy(m) == MQX_EOK)
00334                 return 0;
00335             else
00336                 return BAD_MUTEX_E;
00337         }
00338 
00339         int LockMutex(CyaSSL_Mutex* m)
00340         {
00341             if (_mutex_lock(m) == MQX_EOK)
00342                 return 0;
00343             else
00344                 return BAD_MUTEX_E;
00345         }
00346 
00347         int UnLockMutex(CyaSSL_Mutex* m)
00348         {
00349             if (_mutex_unlock(m) == MQX_EOK)
00350                 return 0;
00351             else
00352                 return BAD_MUTEX_E;
00353         }
00354 
00355     #elif defined (CYASSL_TIRTOS)
00356 
00357         int InitMutex(CyaSSL_Mutex* m)
00358         {
00359            Semaphore_Params params;
00360 
00361            Semaphore_Params_init(&params);
00362            params.mode = Semaphore_Mode_BINARY;
00363 
00364            *m = Semaphore_create(1, &params, NULL);
00365 
00366            return 0;
00367         }
00368 
00369         int FreeMutex(CyaSSL_Mutex* m)
00370         {
00371             Semaphore_delete(m);
00372 
00373             return 0;
00374         }
00375 
00376         int LockMutex(CyaSSL_Mutex* m)
00377         {
00378             Semaphore_pend(*m, BIOS_WAIT_FOREVER);
00379 
00380             return 0;
00381         }
00382 
00383         int UnLockMutex(CyaSSL_Mutex* m)
00384         {
00385             Semaphore_post(*m);
00386 
00387             return 0;
00388         }
00389 
00390     #elif defined(CYASSL_MDK_ARM)|| defined(CYASSL_CMSIS_RTOS)
00391     
00392         #if defined(CYASSL_CMSIS_RTOS)
00393             #include "cmsis_os.h"
00394             #define CMSIS_NMUTEX 10
00395             osMutexDef(CyaSSL_mt0) ;  osMutexDef(CyaSSL_mt1) ;  osMutexDef(CyaSSL_mt2) ;
00396             osMutexDef(CyaSSL_mt3) ;  osMutexDef(CyaSSL_mt4) ;  osMutexDef(CyaSSL_mt5) ;  
00397             osMutexDef(CyaSSL_mt6) ;  osMutexDef(CyaSSL_mt7) ;  osMutexDef(CyaSSL_mt8) ;  
00398             osMutexDef(CyaSSL_mt9) ;  
00399             
00400             static const osMutexDef_t *CMSIS_mutex[] = { osMutex(CyaSSL_mt0),   
00401                 osMutex(CyaSSL_mt1),    osMutex(CyaSSL_mt2),   osMutex(CyaSSL_mt3),    
00402                 osMutex(CyaSSL_mt4),    osMutex(CyaSSL_mt5),   osMutex(CyaSSL_mt6),
00403                 osMutex(CyaSSL_mt7),    osMutex(CyaSSL_mt8),    osMutex(CyaSSL_mt9) } ;                 
00404             
00405             static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0} ;
00406 
00407             int InitMutex(CyaSSL_Mutex* m)
00408             {
00409                 int i ;
00410                 for (i=0; i<CMSIS_NMUTEX; i++) {
00411                     if(CMSIS_mutexID[i] == 0) {
00412                         CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]) ;
00413                         (*m) = CMSIS_mutexID[i] ;
00414                     return 0 ;
00415                     }
00416                 }
00417                 return -1 ;
00418             }
00419 
00420             int FreeMutex(CyaSSL_Mutex* m)
00421             {
00422                 int i ;
00423                 osMutexDelete   (*m) ;
00424                 for (i=0; i<CMSIS_NMUTEX; i++) {
00425                     if(CMSIS_mutexID[i] == (*m)) {
00426                         CMSIS_mutexID[i] = 0 ;
00427                         return(0) ;
00428                     }
00429                 }
00430                 return(-1) ;
00431             }
00432             
00433             int LockMutex(CyaSSL_Mutex* m)
00434             {
00435                 osMutexWait(*m, osWaitForever) ;
00436                 return(0) ;
00437             }
00438 
00439             int UnLockMutex(CyaSSL_Mutex* m)
00440             {
00441                 osMutexRelease (*m);
00442                 return 0;
00443             }
00444         #else
00445 
00446         int InitMutex(CyaSSL_Mutex* m)
00447         {
00448             os_mut_init (m); 
00449             return 0;
00450         }
00451 
00452         int FreeMutex(CyaSSL_Mutex* m)
00453         {
00454             return(0) ;
00455         }
00456 
00457         int LockMutex(CyaSSL_Mutex* m)
00458         {
00459             os_mut_wait (m, 0xffff);
00460             return(0) ;
00461         }
00462 
00463         int UnLockMutex(CyaSSL_Mutex* m)
00464         {
00465             os_mut_release (m);
00466             return 0;
00467         }
00468         #endif
00469     #endif /* USE_WINDOWS_API */
00470 #endif /* SINGLE_THREADED */
00471