Skip to content

Commit

Permalink
Restyle PSA Mutex patch. (#195)
Browse files Browse the repository at this point in the history
Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
restyled-io[bot] and restyled-commits authored May 2, 2024
1 parent f6ad14d commit 4017291
Show file tree
Hide file tree
Showing 2 changed files with 973 additions and 1,010 deletions.
181 changes: 91 additions & 90 deletions platform/security/sl_component/sl_mbedtls_support/inc/threading_alt.h
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
/**************************************************************************/ /**
* @file
* @brief Threading primitive implementation for mbed TLS
*******************************************************************************
* # License
* <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/
* @file
* @brief Threading primitive implementation for mbed TLS
*******************************************************************************
* # License
* <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
******************************************************************************/

#ifndef THREADING_ALT_H
#define THREADING_ALT_H

/***************************************************************************//**
* \addtogroup sl_crypto
* \{
******************************************************************************/
/***************************************************************************/ /**
* \addtogroup sl_crypto
* \{
******************************************************************************/

/***************************************************************************/ /**
* \addtogroup sl_crypto_threading Threading Primitives
* \brief Threading primitive implementation for mbed TLS
*
* This file contains the glue logic between the mbed TLS threading API
* and CMSIS RTOS2 API.
*
* In order to enable support for Micrium OS backend
* the user must make sure SL_CATALOG_MICRIUMOS_KERNEL_PRESENT is defined.
* In order to enable support for FreeRTOS backend the user must make sure
* SL_CATALOG_FREERTOS_KERNEL_PRESENT is defined.
*
* Applications created using Simplicity Studio 5 the sl_component_catalog.h
* file will define one of the above in order to declare the presence
* of a specific RTOS.
*
* \note
* In order to use the Silicon Labs Hardware Acceleration plugins in
* multi-threaded applications, select
* <b>Mbed TLS support for EFM32/EFR32 crypto acceleration</b> component.
*
* \{
******************************************************************************/

/***************************************************************************//**
* \addtogroup sl_crypto_threading Threading Primitives
* \brief Threading primitive implementation for mbed TLS
*
* This file contains the glue logic between the mbed TLS threading API
* and CMSIS RTOS2 API.
*
* In order to enable support for Micrium OS backend
* the user must make sure SL_CATALOG_MICRIUMOS_KERNEL_PRESENT is defined.
* In order to enable support for FreeRTOS backend the user must make sure
* SL_CATALOG_FREERTOS_KERNEL_PRESENT is defined.
*
* Applications created using Simplicity Studio 5 the sl_component_catalog.h
* file will define one of the above in order to declare the presence
* of a specific RTOS.
*
* \note
* In order to use the Silicon Labs Hardware Acceleration plugins in
* multi-threaded applications, select
* <b>Mbed TLS support for EFM32/EFR32 crypto acceleration</b> component.
*
* \{
******************************************************************************/

#include <stdbool.h>
#include "mbedtls/threading.h"
#include <stdbool.h>

#if defined(MBEDTLS_THREADING_ALT) && defined(MBEDTLS_THREADING_C)

#if defined(SL_COMPONENT_CATALOG_PRESENT)
#include "sl_component_catalog.h"
#include "sl_component_catalog.h"
#endif

#if defined(SL_CATALOG_MICRIUMOS_KERNEL_PRESENT) || defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT)
#if defined(SL_CATALOG_MICRIUMOS_KERNEL_PRESENT) || \
defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT)

#include "cmsis_os2.h"
#include "sl_assert.h"

#if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT)
#include "FreeRTOSConfig.h"
#if (configSUPPORT_STATIC_ALLOCATION == 1)
#include "FreeRTOS.h" // StaticSemaphore_t
#endif
#include "FreeRTOSConfig.h"
#if (configSUPPORT_STATIC_ALLOCATION == 1)
#include "FreeRTOS.h" // StaticSemaphore_t
#endif
#endif

#ifdef __cplusplus
Expand All @@ -87,29 +88,30 @@ extern "C" {

#define SL_THREADING_ALT

#define MUTEX_INIT = { 0 }
#define MUTEX_INIT = {0}

/// SE manager mutex definition for CMSIS RTOS2.
typedef struct mbedtls_threading_mutex {
#if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT) && (configSUPPORT_STATIC_ALLOCATION == 1)
#if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT) && \
(configSUPPORT_STATIC_ALLOCATION == 1)
StaticSemaphore_t static_sem_object;
#endif
osMutexAttr_t mutex_attr;
osMutexId_t mutex_ID;
osMutexId_t mutex_ID;
} mbedtls_threading_mutex_t;

/**
* \brief Initialize a given mutex
*
* \param mutex Pointer to the mutex needing initialization
*/
static inline void THREADING_InitMutex(mbedtls_threading_mutex_t *mutex)
{
static inline void THREADING_InitMutex(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL) {
return;
}

#if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT) && (configSUPPORT_STATIC_ALLOCATION == 1)
#if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT) && \
(configSUPPORT_STATIC_ALLOCATION == 1)
mutex->mutex_attr.cb_mem = &mutex->static_sem_object;
mutex->mutex_attr.cb_size = sizeof(mutex->static_sem_object);
#endif
Expand All @@ -124,8 +126,7 @@ static inline void THREADING_InitMutex(mbedtls_threading_mutex_t *mutex)
*
* \param mutex Pointer to the mutex being freed
*/
static inline void THREADING_FreeMutex(mbedtls_threading_mutex_t *mutex)
{
static inline void THREADING_FreeMutex(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL) {
return;
}
Expand All @@ -141,8 +142,8 @@ static inline void THREADING_FreeMutex(mbedtls_threading_mutex_t *mutex)
*
* \return RTOS_ERR_NONE on success, error code otherwise.
*/
static inline int THREADING_TakeMutexBlocking(mbedtls_threading_mutex_t *mutex)
{
static inline int
THREADING_TakeMutexBlocking(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL) {
return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
}
Expand All @@ -159,10 +160,11 @@ static inline int THREADING_TakeMutexBlocking(mbedtls_threading_mutex_t *mutex)
*
* \param mutex Pointer to the mutex being tested
*
* \return RTOS_ERR_NONE on success (= mutex successfully owned), error code otherwise.
* \return RTOS_ERR_NONE on success (= mutex successfully owned), error
* code otherwise.
*/
static inline int THREADING_TakeMutexNonBlocking(mbedtls_threading_mutex_t *mutex)
{
static inline int
THREADING_TakeMutexNonBlocking(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL) {
return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
}
Expand All @@ -181,8 +183,7 @@ static inline int THREADING_TakeMutexNonBlocking(mbedtls_threading_mutex_t *mute
*
* \return RTOS_ERR_NONE on success, error code otherwise.
*/
static inline int THREADING_GiveMutex(mbedtls_threading_mutex_t *mutex)
{
static inline int THREADING_GiveMutex(mbedtls_threading_mutex_t *mutex) {
if (mutex == NULL) {
return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
}
Expand All @@ -198,27 +199,27 @@ static inline int THREADING_GiveMutex(mbedtls_threading_mutex_t *mutex)
}
#endif

#endif // SL_CATALOG_MICRIUMOS_KERNEL_PRESENT || SL_CATALOG_FREERTOS_KERNEL_PRESENT
#endif // SL_CATALOG_MICRIUMOS_KERNEL_PRESENT ||
// SL_CATALOG_FREERTOS_KERNEL_PRESENT

#ifdef __cplusplus
extern "C" {
#endif

/* Forward declaration of threading_set_alt */
void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
void (*mutex_free)(mbedtls_threading_mutex_t *),
int (*mutex_lock)(mbedtls_threading_mutex_t *),
int (*mutex_unlock)(mbedtls_threading_mutex_t *) );
void mbedtls_threading_set_alt(
void (*mutex_init)(mbedtls_threading_mutex_t *),
void (*mutex_free)(mbedtls_threading_mutex_t *),
int (*mutex_lock)(mbedtls_threading_mutex_t *),
int (*mutex_unlock)(mbedtls_threading_mutex_t *));

/**
* \brief Helper function for setting up the mbed TLS threading subsystem
* \brief Helper function for setting up the mbed TLS threading
* subsystem
*/
static inline void THREADING_setup(void)
{
mbedtls_threading_set_alt(&THREADING_InitMutex,
&THREADING_FreeMutex,
&THREADING_TakeMutexBlocking,
&THREADING_GiveMutex);
static inline void THREADING_setup(void) {
mbedtls_threading_set_alt(&THREADING_InitMutex, &THREADING_FreeMutex,
&THREADING_TakeMutexBlocking, &THREADING_GiveMutex);
}

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 4017291

Please sign in to comment.