67b20a3b1a819a149561bcd0f518d41095d580c3
[platform/upstream/connectedhomeip.git] / examples / platform / k32w / app / support / FreeRtosMbedtlsMutex.c
1 /*
2  *
3  *    Copyright (c) 2020 Google LLC.
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  *
18  */
19
20 #include "FreeRtosMbedtlsMutex.h"
21
22 #include "FreeRTOS.h"
23 #include "semphr.h"
24
25 #include <assert.h>
26
27 static inline void mutex_init(mbedtls_threading_mutex_t * p_mutex)
28 {
29     assert(p_mutex != NULL);
30     *p_mutex = xSemaphoreCreateMutex();
31     assert(*p_mutex != NULL);
32 }
33
34 static inline void mutex_free(mbedtls_threading_mutex_t * p_mutex)
35 {
36     assert(p_mutex != NULL);
37     assert(*p_mutex != NULL);
38     vSemaphoreDelete(*p_mutex);
39 }
40
41 static inline int mutex_lock(mbedtls_threading_mutex_t * p_mutex)
42 {
43     assert(p_mutex != NULL);
44     assert(*p_mutex != NULL);
45     return xSemaphoreTake(*p_mutex, portMAX_DELAY) != pdTRUE;
46 }
47
48 static inline int mutex_unlock(mbedtls_threading_mutex_t * p_mutex)
49 {
50     assert(p_mutex != NULL);
51     assert(*p_mutex != NULL);
52     return xSemaphoreGive(*p_mutex) != pdTRUE;
53 }
54
55 void freertos_mbedtls_mutex_init(void)
56 {
57     mbedtls_threading_set_alt(mutex_init, mutex_free, mutex_lock, mutex_unlock);
58 }
59
60 void freertos_mbedtls_mutex_free(void)
61 {
62     mbedtls_threading_free_alt();
63 }