1 /***************************************************************************/ /**
2  * @file
3  * @brief
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 #include "sl_si91x_host_interface.h"
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include "cmsis_os2.h"
35 #include <string.h>
36 extern osMutexId_t malloc_free_mutex;
37 sl_status_t sl_si91x_host_init_buffer_manager(void);
38 sl_status_t sl_si91x_host_deinit_buffer_manager(void);
39 sl_status_t sl_si91x_host_allocate_buffer(sl_wifi_buffer_t **buffer,
40                                           sl_wifi_buffer_type_t type,
41                                           uint32_t buffer_size,
42                                           uint32_t wait_duration_ms);
43 void *sl_si91x_host_get_buffer_data(sl_wifi_buffer_t *buffer, uint16_t offset, uint16_t *data_length);
44 void sl_si91x_host_free_buffer(sl_wifi_buffer_t *buffer);
45 
sl_si91x_host_init_buffer_manager(void)46 sl_status_t sl_si91x_host_init_buffer_manager(void)
47 {
48   if (malloc_free_mutex == NULL) {
49     malloc_free_mutex = osMutexNew(NULL);
50   }
51   return SL_STATUS_OK;
52 }
53 
sl_si91x_host_deinit_buffer_manager(void)54 sl_status_t sl_si91x_host_deinit_buffer_manager(void)
55 {
56   if (malloc_free_mutex != NULL) {
57     osMutexDelete(malloc_free_mutex);
58     malloc_free_mutex = NULL;
59   }
60   return SL_STATUS_OK;
61 }
62 
sl_si91x_host_allocate_buffer(sl_wifi_buffer_t ** buffer,sl_wifi_buffer_type_t type,uint32_t buffer_size,uint32_t wait_duration_ms)63 sl_status_t sl_si91x_host_allocate_buffer(sl_wifi_buffer_t **buffer,
64                                           sl_wifi_buffer_type_t type,
65                                           uint32_t buffer_size,
66                                           uint32_t wait_duration_ms)
67 {
68   (void)type;
69   osMutexAcquire(malloc_free_mutex, 0xFFFFFFFFUL);
70   uint32_t start         = osKernelGetTickCount();
71   sl_wifi_buffer_t *temp = NULL;
72   do {
73     temp = (sl_wifi_buffer_t *)malloc(buffer_size + sizeof(*temp));
74     if (temp != NULL) {
75       break;
76     } else {
77       osDelay(1);
78     }
79   } while ((osKernelGetTickCount() - start) < wait_duration_ms);
80 
81   osMutexRelease(malloc_free_mutex);
82   if (temp == NULL) {
83     return SL_STATUS_ALLOCATION_FAILED;
84   }
85   temp->length    = buffer_size;
86   temp->node.node = NULL;
87   *buffer         = temp;
88   return SL_STATUS_OK;
89 }
90 
sl_si91x_host_get_buffer_data(sl_wifi_buffer_t * buffer,uint16_t offset,uint16_t * data_length)91 void *sl_si91x_host_get_buffer_data(sl_wifi_buffer_t *buffer, uint16_t offset, uint16_t *data_length)
92 {
93   if (offset >= buffer->length) {
94     return NULL;
95   }
96   if (data_length) {
97     *data_length = (uint16_t)(buffer->length) - offset;
98   }
99   return (void *)&buffer->data[offset];
100 }
101 
sl_si91x_host_free_buffer(sl_wifi_buffer_t * buffer)102 void sl_si91x_host_free_buffer(sl_wifi_buffer_t *buffer)
103 {
104   if (buffer == NULL) {
105     return;
106   }
107   osMutexAcquire(malloc_free_mutex, 0xFFFFFFFFUL);
108   free((void *)buffer);
109   osMutexRelease(malloc_free_mutex);
110 }
111