1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CALLBACKS_H
17 #define CALLBACKS_H
18 #include "fillpinc.h"
19 #include "fillp_os.h"
20 
21 #ifdef FILLP_MAC
22 #include <mach/task.h>
23 #include <mach/semaphore.h>
24 #include <mach/sync_policy.h>
25 #include <mach/mach_init.h>
26 #include <mach/clock.h>
27 #include <mach/mach.h>
28 #include <mach/mach_time.h>
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #define FILLP_CONST_2    2
36 #define FILLP_CONST_100  100
37 #define FILLP_CONST_1K   1000
38 #define FILLP_CONST_10K  10000
39 #define FILLP_CONST_1M   1000000
40 
41 #if defined(FILLP_LINUX) && defined(FILLP_MAC)
42 
43 typedef uint64_t FillpSysArchTime;
44 
45 #elif defined(FILLP_LINUX)
46 
47 typedef struct timespec FillpSysArchTime;
48 
49 #elif defined(FILLP_WIN32)
50 
51 typedef struct FillpSysArchTimeSt {
52     LARGE_INTEGER time;
53 } FillpSysArchTime;
54 
55 #endif
56 
57 
58 void *FillpMemCalloc(IN FILLP_UINT32 nitems, IN FILLP_UINT32 size);
59 
60 /*******************************************************************************
61     Adption     : FillpMemAlloc
62 
63     Description : Adp Adption if user has not registered the callback for malloc
64 
65     Input         :
66                     size: Requested size to be allocated
67 
68     Output       :None
69 
70     Return       : FILLP_NULL_PTR
71  *******************************************************************************/
72 void  *FillpMemAlloc(IN FILLP_UINT32 size);
73 
74 
75 /*******************************************************************************
76     Adption     : FillpMemFree
77 
78     Description : Adp Adption if user has not registered the callback for free
79 
80     Input         :
81                     addr                   : Base address of memory to be freed
82 
83     Output       :None
84 
85     Return       : FILLP_FAILURE
86  *******************************************************************************/
87 void FillpMemFree(IN void *addr);
88 
89 void *FillpMemChr(IN FILLP_CONST void *s, IN FILLP_INT c, IN FILLP_SIZE_T n);
90 
91 /*******************************************************************************
92     Adption     : FillpStrLen
93 
94     Description : Adp function if user has not registered the callback for strlen
95 
96     Input         :
97                     pSrc                    : String
98 
99     Output       :None
100 
101     Return       : FILLP_NULL_PTR
102  *******************************************************************************/
103 FILLP_UINT32 FillpStrLen(IN FILLP_CHAR *str);
104 
105 /*******************************************************************************
106     Function     : FillpAdpSelect
107 
108     Description : Adp function if user has not registered the callback for select
109  *******************************************************************************/
110 FILLP_INT  FillpSelect(
111     IN FILLP_INT maxFd, /* fd value to be selected */
112     IN void *rdFds, /* fd for read */
113     IN void *wrFds, /* fd for write */
114     IO void *exceptFds, /* fd for errors */
115     IN void *timeout); /* max time for select to wait */
116 
117 
118 void FillpFuncFdClr(
119     IN FILLP_UINT sockFd, /* socket fd */
120     IN FT_FD_SET clrFdSet);
121 
122 void FillpFuncFdSet(
123     IN FILLP_UINT sockFd, /* socket fd */
124     IN FT_FD_SET setFd);
125 
126 FILLP_INT FillpFuncFdIsSet(
127     IN FILLP_INT sockFd, /* socket fd */
128     IN FT_FD_SET issetFd);
129 
130 
131 FT_FD_SET FillpFuncCreateFdSet(void);
132 
133 void FillpFuncDestroyFdSet(IN FT_FD_SET destroyFdSet);
134 
135 FILLP_INT32 FillpFuncCopyFdSet(IO FT_FD_SET dstFdSet, IN FT_FD_SET srcFdSet);
136 
137 
138 /*******************************************************************************
139     Function     : FillpRand
140 
141     Description : Adp function if user has not registered the callback for rand
142  *******************************************************************************/
143 FILLP_UINT32 FillpRand(IN void);
144 
145 
146 /*******************************************************************************
147     Function     : FillpCreateThread
148 
149     Description : Adp function if user has not registered the callback for rand
150  *******************************************************************************/
151 FILLP_INT FillpCreateThread(IN void *param, IO void *threadId);
152 
153 
154 /*******************************************************************************
155     Function     : FillpSysArchInit
156 
157     Description : Adp function if user has not registered the callback for  initializing the
158                        use of the Winsock DLL by a process
159  *******************************************************************************/
160 FILLP_INT FillpSysArchInit(IN void);
161 
162 /*******************************************************************************
163     Function     : FillpAdpSysArcGetCurTime
164 
165     Description : Adp function if user has not registered the callback for getting system current time
166  *******************************************************************************/
167 FILLP_LLONG FillpSysArchGetCurTimeLonglong(IN void);
168 
169 /*******************************************************************************
170     Function     : FillpSysArchAtomicInc
171 
172     Description : Adp function if user has not registered the callback for increment the value
173  *******************************************************************************/
174 FILLP_INT FillpSysArchAtomicInc(IO SysArchAtomic *var, FILLP_INT val);
175 
176 /*******************************************************************************
177     Function     : FillpSysArchAtomicIncAndTest
178 
179     Description : Adp function if user has not registered the callback for increment and test the value
180  *******************************************************************************/
181 FILLP_BOOL FillpSysArchAtomicIncAndTest(IO SysArchAtomic *var);
182 
183 /*******************************************************************************
184     Function     : FillpSysArchAtomicDec
185 
186     Description : Adp function if user has not registered the callback for decrement the value
187  *******************************************************************************/
188 FILLP_INT FillpSysArchAtomicDec(IO SysArchAtomic *var, FILLP_INT val);
189 
190 /*******************************************************************************
191     Function     : FillpSysArchAtomicDecAndTest
192 
193     Description : Adp function if user has not registered the callback for decrement and test the value
194  *******************************************************************************/
195 FILLP_BOOL FillpSysArchAtomicDecAndTest(IO SysArchAtomic *var);
196 
197 /*******************************************************************************
198     Function     : FillpSysArchAtomicDec
199 
200     Description : Adp function if user has not registered the callback for read the value
201  *******************************************************************************/
202 FILLP_INT FillpSysArchAtomicRead(IO SysArchAtomic *var);
203 
204 /*******************************************************************************
205     Function     : FillpSysArchAtomicSet
206 
207     Description : Adp function if user has not registered the callback for automatic exchange the value
208  *******************************************************************************/
209 FILLP_INT FillpSysArchAtomicSet(IN SysArchAtomic *var, FILLP_INT newValue);
210 
211 /*******************************************************************************
212     Function     : FillpAdpSysArchCompAndSwap
213 
214     Description : Adp function if user has not registered the callback for compare and swap a value
215  *******************************************************************************/
216 FILLP_INT FillpSysArchCompAndWwap(IO volatile FILLP_ULONG *sem,
217     IN volatile FILLP_ULONG oldValue,
218     IN volatile FILLP_ULONG exchange);
219 
220 
221 /*******************************************************************************
222     Function     : FillpSysSleepMs
223 
224     Description : Adp function if user has not registered the callback for sleep
225  *******************************************************************************/
226 void FillpSysSleepMs(IN FILLP_UINT time); /* In Millseconds */
227 
228 FILLP_INT FillpSysSleepUs(IN FILLP_UINT time); /* In seconds */
229 
230 
231 /*******************************************************************************
232     Function     : FillpAdpRtePause
233 
234     Description : Adp function if user has not registered the callback for pause
235  *******************************************************************************/
236 void FillpAdpRtePause(void);
237 
238 
239 /*******************************************************************************
240     Function     : FillpSemTryWait
241 
242     Description : Adp function if user has not registered the callback for semaphore try wait
243  *******************************************************************************/
244 FILLP_INT FillpSemTryWait(IN SYS_ARCH_SEM *sem);
245 
246 /*******************************************************************************
247     Function     : FillpSemWait
248 
249     Description : Adp function if user has not registered the callback for semaphore wait
250  *******************************************************************************/
251 FILLP_INT FillpSemWait(IN SYS_ARCH_SEM *sem);
252 
253 FILLP_INT FillpRwSemTryRdWait(IN SYS_ARCH_RW_SEM *sem);
254 
255 FILLP_INT FillpRwSemTryWrWait(IN SYS_ARCH_RW_SEM *sem);
256 
257 
258 /*******************************************************************************
259     Function     : FillpRwSemRdPost
260 
261     Description : Adp function if user has not registered the callback for semaphore post
262  *******************************************************************************/
263 FILLP_INT FillpRwSemRdPost(IN SYS_ARCH_RW_SEM *sem);
264 
265 /*******************************************************************************
266     Function     : FillpRwSemWrPost
267 
268     Description : Adp function if user has not registered the callback for semaphore post
269 
270  *******************************************************************************/
271 FILLP_INT FillpRwSemWrPost(IN SYS_ARCH_RW_SEM *sem);
272 
273 /*******************************************************************************
274     Function     : FillpRwSemDestroy
275 
276     Description : Adp function if user has not registered the callback for semaphore destroy
277  *******************************************************************************/
278 FILLP_INT FillpRwSemDestroy(IN SYS_ARCH_RW_SEM *sem);
279 
280 /*******************************************************************************
281     Function     : FillpSemPost
282 
283     Description : Adp function if user has not registered the callback for semaphore post
284  *******************************************************************************/
285 FILLP_INT FillpSemPost(IN SYS_ARCH_SEM *sem);
286 
287 /*******************************************************************************
288     Function     : FillpSemDestroy
289 
290     Description : Adp function if user has not registered the callback for semaphore destroy
291  *******************************************************************************/
292 FILLP_INT FillpSemDestroy(IN SYS_ARCH_SEM *sem);
293 
294 
295 /*******************************************************************************
296     Function     : FillpFuncCreateSocket
297 
298     Description : Adp function if user has not registered the Create socket callback
299  *******************************************************************************/
300 FILLP_INT32 FillpFuncCreateSocket(
301     IN FILLP_INT32 domain, /* the address family */
302     IN FILLP_INT32 type, /* new socket */
303     IN FILLP_INT32 protocol); /* protocol to be used */
304 
305 
306 /*******************************************************************************
307     Function     : FillpFuncBindSocket
308 
309     Description : Adp function if user has not registered the Bind socket callback function
310 
311  *******************************************************************************/
312 FILLP_INT32 FillpFuncBindSocket(
313     IN FILLP_INT32 sockFd, /* socket fd */
314     IN FILLP_CONST void *myAddr, /* bind addr */
315     IN FILLP_INT32 addrLen); /* addr length */
316 
317 
318 FILLP_INT FillpFuncIoCtlSocket(
319     FILLP_INT fd,
320     FILLP_INT type,
321     FILLP_ULONG *parg);
322 
323 
324 FILLP_INT32 FillpFuncGetSockName(
325     IN FILLP_INT32 sockFd, /* socket fd */
326     IN void *myAddr, /* bind addr */
327     IN void *addrLen); /* addr length */
328 
329 
330 FILLP_INT32 FillpFuncConnectSocket(
331     IN FILLP_INT32 sockFd, /* socket fd */
332     IN FILLP_CONST void *myAddr, /* bind addr */
333     IN FILLP_INT32 addrLen); /* addr length */
334 
335 
336 /*******************************************************************************
337     Function     : FillpFuncCloseSocket
338 
339     Description : Adp function if user has not registered the close socket callback function
340  *******************************************************************************/
341 FILLP_INT32 FillpFuncCloseSocket(IN FILLP_INT32 sockFd);
342 
343 
344 /*******************************************************************************
345     Function     : FillpFuncSendTo
346 
347     Description : Adp function if user has not registered the sendto callback function
348  *******************************************************************************/
349 FILLP_INT FillpFuncSendTo(
350     IN FILLP_INT sockFd,
351     IN const void *buf,
352     IN FILLP_SIZE_T len,
353     IN FILLP_INT flags,
354     IN const void *to,
355     IN FILLP_SIZE_T toLen);
356 
357 
358 /*******************************************************************************
359     Function     : FillpFuncRecvFrom
360 
361     Description : Adp function if user has not registered the receive from callback function
362  *******************************************************************************/
363 FILLP_INT  FillpFuncRecvFrom(
364     IN FILLP_INT sockFd,
365     OUT void *buf,
366     IN FILLP_SIZE_T len,
367     IN FILLP_INT flags,
368     OUT void *from,
369     IO FILLP_SIZE_T *fromLen);
370 
371 
372 FILLP_INT FillpFuncSend(
373     IN FILLP_INT sockFd, /* Connection fd */
374     IN const void *buffer, /* buffer to hold data to be sent */
375     IN FILLP_INT len, /* no of bytes to be sent */
376     IN FILLP_INT flags); /* flags to tell the status */
377 
378 
379 /* callback for SYS_ARCH_SEM_CLOSE */
380 FILLP_INT FillpSysArchSemClose(SYS_ARCH_SEM *sem);
381 
382 /* callback for SYS_ARCH_SEM_WAIT_TIMEOUT */
383 FILLP_INT FillpSysArchSemWaitTimeout(SYS_ARCH_SEM *sem, FILLP_SLONG timeout);
384 
385 
386 /*******************************************************************************
387     Function     : FillpRegLibSysFunc
388 
389     Description : FILLP user invokes this function to register the system interface functions to be used by FILLP stack.
390                   The function pointers of all system functions defined by FILLP_SYS_APPLIB_CALLBACK_FUNC_ST are passed
391                   to FILLP stack to be registered and used during the run time of the stack.
392                   The user is expected to pass a valid function pointer. Passing NULL as any of the function pointer
393                   results in failure. This function should be called before initializing the stack.
394 
395     Input         : pstAdpAppLibSysFunc: Pointer to system interface callback function structure
396                     (FILLP_SYS_APPLIB_CALLBACK_FUNC_ST).
397 
398 
399     Output       :None
400 
401     Return       : Fillp_SUCCESS - Success
402                        ERROR CODES    - Failure
403 *******************************************************************************/
404 
405 void FillpRegLibSysFunc(IN void);
406 
407 
408 void FillpSysOsDeinit(IN void);
409 
410 FILLP_INT FillpFuncFcntl(
411     IN FILLP_INT fd, /* connection fd */
412     IN FILLP_INT cmd, /* command to perform on socket */
413     IN FILLP_INT val); /* arguments for socket */
414 
415 
416 FILLP_INT FillpFuncSetSockOpt(
417     IN FILLP_INT sockFd,
418     IN FILLP_INT level,
419     IN FILLP_INT optName,
420     IN FILLP_CONST void *optVal,
421     IN FILLP_INT optLen);
422 
423 
424 FILLP_INT FillpFuncGetSockOpt(
425     IN FILLP_INT sockFd,
426     IN FILLP_INT level,
427     IN FILLP_INT optName,
428     IO void *optVal,
429     IO FILLP_INT *optLen);
430 
431 FILLP_INT  FillpArchInitSem(
432     IO SYS_ARCH_SEM  *sem,
433     IN FILLP_ULONG value);
434 
435 void FillpSysArchCompilerBarrier(void);
436 
437 
438 FILLP_LLONG FillpSysAdaptArchGetCurTimeLonglong();
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 
445 #endif  /* CALLBACKS_H */
446 
447