1 /*
2 * Copyright 2017-2018,2021 NXP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "phOsal_Adaptation.h"
18
19 #include <stdlib.h>
20 #include <cstdint>
21 #include <cstring>
22
23 /*
24 #define LOG_FUNCTION_ENTRY \
25 phOsal_LogFunctionEntry((const uint8_t*) \
26 "Adapt",(const uint8_t*)__FUNCTION__)
27 #define LOG_FUNCTION_EXIT \
28 phOsal_LogFunctionExit((const uint8_t*) \
29 "Adapt",(const uint8_t*)__FUNCTION__)
30 */
31
32 #define LOG_FUNCTION_ENTRY
33 #define LOG_FUNCTION_EXIT
34
35 sphOsalAdapt_Context_t gsOsalAdaptContext;
36
37 static void* phOsapAdapt_QueMemAllocCB(void*, uint32_t);
38 static signed int phOsalAdapt_QueMemFreeCB(void*, void*);
39
40 ADAPTSTATUS
phOsalAdapt_InitOsal(void * pvAppContext)41 phOsalAdapt_InitOsal(void* pvAppContext) {
42 ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
43 memset((uint8_t*)&gsOsalAdaptContext, 0, sizeof(sphOsalAdapt_Context_t));
44 psphOsalAdapt_Context_t pContext = &gsOsalAdaptContext;
45
46 phOsal_SetLogLevel(PHOSAL_LOGLEVEL_DATA_BUFFERS);
47
48 LOG_FUNCTION_ENTRY;
49
50 pContext->pvUpLayerContext = pvAppContext;
51
52 phOsal_LogDebug((const uint8_t*)"Adapt>Initializing OSAL...");
53
54 pContext->sOsalConfig.dwCallbackThreadId = 0L;
55 pContext->sOsalConfig.pContext = nullptr;
56 pContext->sOsalConfig.pLogFile = (uint8_t*)"phOSALLog";
57
58 dwAdaptStatus = phOsal_Init(&pContext->sOsalConfig);
59 if (ADAPTSTATUS_SUCCESS != dwAdaptStatus) {
60 phOsal_LogError((const uint8_t*)"Adapt>Osal Init Failed !");
61 return dwAdaptStatus;
62 }
63
64 phOsal_LogDebug((const uint8_t*)"Adapt>Creating Queue...");
65
66 pContext->sQueueCreatePrms.memHdl = nullptr;
67 pContext->sQueueCreatePrms.MemAllocCb = phOsapAdapt_QueMemAllocCB;
68 pContext->sQueueCreatePrms.MemFreeCb = phOsalAdapt_QueMemFreeCB;
69 pContext->sQueueCreatePrms.wQLength = 20;
70 pContext->sQueueCreatePrms.eOverwriteMode = PHOSAL_QUEUE_NO_OVERWRITE;
71
72 dwAdaptStatus = phOsal_QueueCreate(&pContext->pvOsalQueueHandle,
73 &pContext->sQueueCreatePrms);
74 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
75 phOsal_LogError((const uint8_t*)"Adapt>Queue Creation Failed !");
76 return dwAdaptStatus;
77 }
78
79 LOG_FUNCTION_EXIT;
80 return dwAdaptStatus;
81 }
82
83 ADAPTSTATUS
phOsalAdapt_DeinitOsal()84 phOsalAdapt_DeinitOsal() {
85 LOG_FUNCTION_ENTRY;
86 ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
87 psphOsalAdapt_Context_t pContext = &gsOsalAdaptContext;
88
89 dwAdaptStatus = phOsal_ThreadDelete(pContext->pvOsalTaskHandle);
90 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
91 phOsal_LogDebugString((const uint8_t*)"Adapt>Unable to Delete Thread",
92 (const uint8_t*)__FUNCTION__);
93 phOsal_LogErrorU32h((const uint8_t*)"Status = ", dwAdaptStatus);
94 return dwAdaptStatus;
95 }
96
97 dwAdaptStatus = phOsal_QueueDestroy(pContext->pvOsalQueueHandle);
98 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
99 phOsal_LogDebug((const uint8_t*)"Adapt>Unable to Delete Queue");
100 phOsal_LogErrorU32h((const uint8_t*)"Status = ", dwAdaptStatus);
101 return dwAdaptStatus;
102 }
103 LOG_FUNCTION_EXIT;
104 return dwAdaptStatus;
105 }
106
107 ADAPTSTATUS
phOsalAdapt_StartTask(void * threadFunc,void * pParams)108 phOsalAdapt_StartTask(void* threadFunc, void* pParams) {
109 LOG_FUNCTION_ENTRY;
110
111 ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
112
113 psphOsalAdapt_Context_t pContext = &gsOsalAdaptContext;
114
115 phOsal_LogDebug((const uint8_t*)"Adapt>Creating Thread...");
116
117 dwAdaptStatus =
118 phOsal_ThreadCreate(&pContext->pvOsalTaskHandle,
119 (pphOsal_ThreadFunction_t)threadFunc, pParams);
120 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
121 phOsal_LogError((const uint8_t*)"Adapt>Thread Creation Failed !");
122 phOsal_LogErrorU32h((const uint8_t*)"Status = ", dwAdaptStatus);
123 return dwAdaptStatus;
124 }
125
126 LOG_FUNCTION_EXIT;
127 return dwAdaptStatus;
128 }
129
130 ADAPTSTATUS
phOsalAdapt_StopTask(void * pvTaskHandle)131 phOsalAdapt_StopTask(__attribute__((unused)) void* pvTaskHandle) {
132 LOG_FUNCTION_ENTRY;
133 sQueueData_t* psQueueData = nullptr;
134 psQueueData = (sQueueData_t*)malloc(sizeof(sQueueData_t));
135 memset(psQueueData, 0, sizeof(sQueueData_t));
136 // send fake packet
137 // psQueueData->buffer = 0;
138 psQueueData->len = 0;
139 return phOsalAdapt_SendData(psQueueData);
140 LOG_FUNCTION_EXIT;
141 }
142
143 ADAPTSTATUS
phOsalAdapt_SendData(psQueueData_t psSendData)144 phOsalAdapt_SendData(psQueueData_t psSendData) {
145 LOG_FUNCTION_ENTRY;
146
147 ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
148
149 psphOsalAdapt_Context_t pContext = &gsOsalAdaptContext;
150
151 dwAdaptStatus = phOsal_QueuePush(pContext->pvOsalQueueHandle, psSendData, 0);
152 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
153 phOsal_LogErrorString((const uint8_t*)"Adapt>Unable to Push to Queue",
154 (const uint8_t*)__FUNCTION__);
155 phOsal_LogErrorU32h((const uint8_t*)"Status = ", dwAdaptStatus);
156 return dwAdaptStatus;
157 }
158
159 phOsal_LogDebug((const uint8_t*)"Adapt>Pushed Data");
160
161 LOG_FUNCTION_EXIT;
162 return dwAdaptStatus;
163 }
164
phOsalAdapt_GetData()165 psQueueData_t phOsalAdapt_GetData() {
166 LOG_FUNCTION_ENTRY;
167
168 ADAPTSTATUS dwAdaptStatus = ADAPTSTATUS_FAILED;
169
170 psQueueData_t psGetData;
171
172 psphOsalAdapt_Context_t pContext = &gsOsalAdaptContext;
173
174 phOsal_LogDebug((const uint8_t*)"Adapt>Waiting for Data");
175
176 dwAdaptStatus =
177 phOsal_QueuePull(pContext->pvOsalQueueHandle, (void**)&psGetData, 0);
178 if (dwAdaptStatus != ADAPTSTATUS_SUCCESS) {
179 phOsal_LogErrorString((const uint8_t*)"Adapt>Error QueuePull ",
180 (const uint8_t*)__FUNCTION__);
181 phOsal_LogErrorU32h((const uint8_t*)"Status = ", dwAdaptStatus);
182 return nullptr;
183 }
184
185 phOsal_LogDebug((const uint8_t*)"Adapt>Received Data");
186
187 LOG_FUNCTION_EXIT;
188 return psGetData;
189 }
190
phOsapAdapt_QueMemAllocCB(void * memHdl,uint32_t size)191 void* phOsapAdapt_QueMemAllocCB(__attribute__((unused)) void* memHdl,
192 uint32_t size) {
193 LOG_FUNCTION_ENTRY;
194 void* addr = malloc(size);
195 // phOsal_LogDebugU32h((const uint8_t*)"Adapt>Allocating mem Size", size);
196 // phOsal_LogDebugU32h((const uint8_t*)"Adapt>Memory Allocation Handle=",
197 // (size_t)memHdl); phOsal_LogDebugU32h((const uint8_t*)"Adapt>Addr=0x",
198 // (size_t)addr);
199 LOG_FUNCTION_EXIT;
200 return addr;
201 }
202
phOsalAdapt_QueMemFreeCB(void * memHdl,void * ptrToMem)203 signed int phOsalAdapt_QueMemFreeCB(__attribute__((unused)) void* memHdl,
204 void* ptrToMem) {
205 LOG_FUNCTION_ENTRY;
206 // phOsal_LogDebugU32h((const uint8_t*)"Adapt>Freeing mem Handle=",
207 // (size_t)memHdl); phOsal_LogDebugU32h((const uint8_t*)"Adapt>Freeing mem
208 // Addr=", (size_t)ptrToMem);
209 free(ptrToMem);
210 LOG_FUNCTION_EXIT;
211 return 0;
212 }
213