• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "callbacks.h"
17 
18 #ifdef FILLP_LINUX
19 
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE
22 #endif
23 
24 #ifndef __USE_GNU
25 #define __USE_GNU
26 #endif
27 #include <stdarg.h>
28 #include <time.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include "semaphore.h"
36 #include <pthread.h>
37 #ifndef FILLP_MAC
38 #include <sys/prctl.h>
39 #endif
40 #include <sched.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <sys/time.h>
44 #include <errno.h>
45 #if defined(FILLP_LW_LITEOS)
46 #include "los_sem.h"
47 #include "los_typedef.h"
48 #include "los_memory.h"
49 #include "los_atomic.h"
50 #include "los_task.h"
51 #include "lwip/sockets.h"
52 #else
53 #include <sys/socket.h>
54 #include <sys/mman.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #endif
58 #include <math.h>
59 #include "securec.h"
60 #include "securectype.h"
61 
62 #else
63 
64 #include <WinSock2.h>
65 #include <windows.h>
66 #include <WinBase.h>
67 #include <Ws2tcpip.h>
68 
69 #include <stdlib.h>
70 #include <stdio.h>
71 #include <string.h>
72 
73 #include <process.h>
74 #include <windows.h>
75 #include <math.h>
76 
77 #include <tchar.h>
78 #include "securec.h"
79 #include "securectype.h"
80 
81 
82 #ifdef DLL_SUPPORT
83 #ifdef DLL_IMPLEMENT
84 #define DLL_API __declspec(dllexport)
85 #else
86 #define DLL_API __declspec(dllimport)
87 #endif
88 #else
89 #define DLL_API
90 
91 #endif
92 
93 #define FILLP_STDCALL __stdcall
94 
95 
96 #ifndef _WINDOWS
97 #ifdef _WIN32
98 #define _WINDOWS
99 #else
100 
101 #ifdef _WIN64
102 #define _WINDOWS
103 #endif
104 
105 #endif
106 #endif
107 
108 
109 #endif
110 
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 
115 #ifdef FILLP_WIN32
116 LARGE_INTEGER g_fillpBasePerformanceFrequency;
117 #endif
118 
119 #ifdef FILLP_MAC
120 mach_port_t g_fillpMacSelf;
121 clock_serv_t g_sclock;
122 static mach_timebase_info_data_t g_macTimeBaseInfo;
123 #endif
124 
125 
126 #ifdef FILLP_LINUX
127 #if defined(FILLP_LW_LITEOS)
FillpSysAdptArchAtomicInc(SysArchAtomic * v,FILLP_INT val)128 static FILLP_INT FillpSysAdptArchAtomicInc(SysArchAtomic *v, FILLP_INT val)
129 {
130     return atomic_add_return(val, v);
131 }
132 
FillpSysAdptArchAtomicIncAndTest(SysArchAtomic * v)133 static FILLP_BOOL FillpSysAdptArchAtomicIncAndTest(SysArchAtomic *v)
134 {
135     return atomic_inc_return(v) == 0;
136 }
137 
FillpSysAdptArchAtomicDec(SysArchAtomic * v,FILLP_INT val)138 static FILLP_INT FillpSysAdptArchAtomicDec(SysArchAtomic *v, FILLP_INT val)
139 {
140     return atomic_add_return(-val, v);
141 }
142 
FillpSysAdptArchAtomicDecAndTest(SysArchAtomic * v)143 static FILLP_BOOL FillpSysAdptArchAtomicDecAndTest(SysArchAtomic *v)
144 {
145     return (FILLP_BOOL)atomic_dec_and_test(v);
146 }
147 
FillpSysAdptArchAtomicRead(SysArchAtomic * v)148 static FILLP_INT FillpSysAdptArchAtomicRead(SysArchAtomic *v)
149 {
150     return atomic_read(v);
151 }
152 
FillpSysAdptArchAtomicSet(SysArchAtomic * target,FILLP_INT newValue)153 static FILLP_INT FillpSysAdptArchAtomicSet(SysArchAtomic *target, FILLP_INT newValue)
154 {
155     return atomic_set(target, newValue);
156 }
157 #else
FillpSysAdptArchAtomicInc(SysArchAtomic * v,FILLP_INT val)158 static FILLP_INT FillpSysAdptArchAtomicInc(SysArchAtomic *v, FILLP_INT val)
159 {
160     return __sync_add_and_fetch(&v->counter, val);
161 }
162 
FillpSysAdptArchAtomicIncAndTest(SysArchAtomic * v)163 static FILLP_BOOL FillpSysAdptArchAtomicIncAndTest(SysArchAtomic *v)
164 {
165     return __sync_add_and_fetch(&v->counter, 1) == 0;
166 }
167 
FillpSysAdptArchAtomicDec(SysArchAtomic * v,FILLP_INT val)168 static FILLP_INT FillpSysAdptArchAtomicDec(SysArchAtomic *v, FILLP_INT val)
169 {
170     return __sync_sub_and_fetch(&v->counter, val);
171 }
172 
FillpSysAdptArchAtomicDecAndTest(SysArchAtomic * v)173 static FILLP_BOOL FillpSysAdptArchAtomicDecAndTest(SysArchAtomic *v)
174 {
175     return __sync_sub_and_fetch(&v->counter, 1) == 0;
176 }
177 
FillpSysAdptArchAtomicRead(SysArchAtomic * v)178 static FILLP_INT FillpSysAdptArchAtomicRead(SysArchAtomic *v)
179 {
180     return v->counter;
181 }
182 
FillpSysAdptArchAtomicSet(SysArchAtomic * target,FILLP_INT newValue)183 static FILLP_INT FillpSysAdptArchAtomicSet(SysArchAtomic *target, FILLP_INT newValue)
184 {
185     return target->counter = newValue;
186 }
187 #endif
188 
189 #else
190 
FillpSysAdptArchAtomicInc(SysArchAtomic * v,long value)191 static FILLP_INT FillpSysAdptArchAtomicInc(SysArchAtomic *v, long value)
192 {
193     return InterlockedExchangeAdd((LONG volatile *)(uintptr_t)v, value) + value;
194 }
195 
FillpSysAdptArchAtomicIncAndTest(SysArchAtomic * v)196 static FILLP_BOOL FillpSysAdptArchAtomicIncAndTest(SysArchAtomic *v)
197 {
198     return InterlockedIncrement((LONG volatile *)(uintptr_t)v) == 0;
199 }
200 
FillpSysAdptArchAtomicDec(SysArchAtomic * v,long value)201 static FILLP_INT FillpSysAdptArchAtomicDec(SysArchAtomic *v, long value)
202 {
203     return InterlockedExchangeAdd((LONG volatile *)(uintptr_t)v, (-value)) - value;
204 }
205 
FillpSysAdptArchAtomicDecAndTest(SysArchAtomic * v)206 static FILLP_BOOL FillpSysAdptArchAtomicDecAndTest(SysArchAtomic *v)
207 {
208     return InterlockedDecrement((LONG volatile *)(uintptr_t)v) == 0;
209 }
210 
FillpSysAdptArchAtomicRead(SysArchAtomic * v)211 static SysArchAtomic FillpSysAdptArchAtomicRead(SysArchAtomic *v)
212 {
213     return *v;
214 }
215 
FillpSysAdptArchAtomicSet(SysArchAtomic * target,IN FILLP_INT value)216 static FILLP_INT FillpSysAdptArchAtomicSet(SysArchAtomic *target, IN FILLP_INT value)
217 {
218     return InterlockedExchange((LONG volatile *)(uintptr_t)target, value);
219 }
220 
221 #endif
222 
223 
224 /*******************************************************************************
225     Adption     : FillpMemCalloc
226 
227     Description : Adp Adption if user has not registered the callback for malloc
228 
229     Input         :
230                     nitems                   : Partition number
231                     size: Requested size to be allocated
232 
233     Output       :None
234 
235     Return       : FILLP_NULL_PTR
236  *******************************************************************************/
FillpMemCalloc(IN FILLP_UINT32 nitems,IN FILLP_UINT32 size)237 void *FillpMemCalloc(IN FILLP_UINT32 nitems, IN FILLP_UINT32 size)
238 {
239     void *ptr;
240 
241     ptr = calloc((size_t)nitems, (size_t)size);
242     return ptr;
243 }
244 
FillpMemAlloc(IN FILLP_UINT32 size)245 void *FillpMemAlloc(IN FILLP_UINT32 size)
246 {
247     void *ptr;
248 
249     ptr = malloc((size_t)size);
250     return ptr;
251 }
252 
253 /*******************************************************************************
254     Adption     : FillpMemFree
255 
256     Description : Adp Adption if user has not registered the callback for free
257 
258     Input         :
259                     addr                   : Base address of memory to be freed
260 
261     Output       :None
262 
263     Return       : FILLP_FAILURE
264  *******************************************************************************/
FillpMemFree(IN void * addr)265 void FillpMemFree(IN void *addr)
266 {
267     if (addr != FILLP_NULL_PTR) {
268         free(addr);
269     }
270 
271     return;
272 }
273 
FillpMemChr(IN FILLP_CONST void * s,IN FILLP_INT c,IN FILLP_SIZE_T n)274 void *FillpMemChr(IN FILLP_CONST void *s, IN FILLP_INT c, IN FILLP_SIZE_T n)
275 {
276     if (s == FILLP_NULL_PTR) {
277         return FILLP_NULL_PTR;
278     }
279 
280     return (memchr(s, c, n));
281 }
282 
283 /*******************************************************************************
284     Adption     : FillpStrLen
285 
286     Description : Adp function if user has not registered the callback for strlen
287 
288     Input         :
289                     pSrc                    : String
290 
291     Output       :None
292 
293     Return       : FILLP_NULL_PTR
294  *******************************************************************************/
FillpStrLen(IN FILLP_CHAR * str)295 FILLP_UINT32 FillpStrLen(IN FILLP_CHAR *str)
296 {
297     if (str == FILLP_NULL_PTR) {
298         return 0;
299     }
300     return (FILLP_UINT32)strlen(str);
301 }
302 
303 
304 /*******************************************************************************
305     Function     : FillpAdpSelect
306 
307     Description : Adp function if user has not registered the callback for select
308 
309     Input         :
310 
311     Output       :
312 
313     Return       :
314  *******************************************************************************/
FillpSelect(IN FILLP_INT maxFd,IN void * rdFds,IN void * wrFds,IO void * exceptFds,IN void * timeout)315 FILLP_INT FillpSelect(
316     IN FILLP_INT maxFd, /* fd value to be selected */
317     IN void *rdFds, /* fd for read */
318     IN void *wrFds, /* fd for write */
319     IO void *exceptFds, /* fd for errors */
320     IN void *timeout) /* max time for select to wait */
321 {
322 #if defined(FILLP_LW_LITEOS)
323     return lwip_select(maxFd, rdFds, wrFds, exceptFds, timeout);
324 #else
325     return select(maxFd, rdFds, wrFds, exceptFds, timeout);
326 #endif
327 }
328 
329 
FillpFuncFdClr(IN FILLP_UINT sockFd,IN FT_FD_SET clrFdSet)330 void FillpFuncFdClr(
331     IN FILLP_UINT sockFd, /* socket fd */
332     IN FT_FD_SET clrFdSet)
333 {
334     FD_CLR((int)sockFd, (fd_set *)clrFdSet);
335 }
336 
FillpFuncFdSet(IN FILLP_UINT sockFd,IN FT_FD_SET setFd)337 void FillpFuncFdSet(
338     IN FILLP_UINT sockFd, /* socket fd */
339     IN FT_FD_SET setFd)
340 {
341     FD_SET((int)sockFd, (fd_set *)setFd);
342 }
343 
FillpFuncFdIsSet(IN FILLP_INT sockFd,IN FT_FD_SET isSetFd)344 FILLP_INT FillpFuncFdIsSet(
345     IN FILLP_INT sockFd, /* socket fd */
346     IN FT_FD_SET isSetFd)
347 {
348     return FD_ISSET(sockFd, (fd_set *)isSetFd);
349 }
350 
FillpFuncCreateFdSet(void)351 FT_FD_SET FillpFuncCreateFdSet(void)
352 {
353     return FillpMemCalloc(sizeof(fd_set), 1);
354 }
355 
FillpFuncDestroyFdSet(IN FT_FD_SET destroyFdSet)356 void FillpFuncDestroyFdSet(IN FT_FD_SET destroyFdSet)
357 {
358     FillpMemFree(destroyFdSet);
359     return;
360 }
361 
FillpFuncCopyFdSet(IO FT_FD_SET dstFdSet,IN FT_FD_SET srcFdSet)362 FILLP_INT32 FillpFuncCopyFdSet(IO FT_FD_SET dstFdSet, IN FT_FD_SET srcFdSet)
363 {
364     if ((dstFdSet == FILLP_NULL_PTR) || (srcFdSet == FILLP_NULL_PTR)) {
365         return -1;
366     }
367 
368     return memcpy_s(dstFdSet, sizeof(fd_set), srcFdSet, sizeof(fd_set));
369 }
370 
371 
372 /*******************************************************************************
373     Function     : FillpRand
374 
375     Description : Adp function if user has not registered the callback for rand
376 
377  *******************************************************************************/
FillpRand(IN void)378 FILLP_UINT32 FillpRand(IN void)
379 {
380     return (FILLP_UINT32)rand();
381 }
382 
383 #ifdef FILLP_LINUX
FillpThreadFun(void * param)384 static void *FillpThreadFun(void *param)
385 {
386     struct ThreadParam *threadParam = (struct ThreadParam *)param;
387     threadParam->func(threadParam->param);
388 
389     return 0;
390 }
391 #else
392 
FillpThreadFun(void * param)393 static unsigned int FILLP_STDCALL FillpThreadFun(void *param)
394 {
395     struct ThreadParam *threadParam = (struct ThreadParam *)param;
396     threadParam->func(threadParam->param);
397 
398     return 0;
399 }
400 
401 
402 #endif
403 
404 /*******************************************************************************
405     Function     : FillpCreateThread
406 
407     Description : Adp function if user has not registered the callback for create thread
408 
409  *******************************************************************************/
FillpCreateThread(IN void * param,IO void * threadId)410 FILLP_INT FillpCreateThread(IN void *param, IO void *threadId)
411 {
412 #ifdef FILLP_LINUX
413 
414     return pthread_create((pthread_t *)threadId, FILLP_NULL_PTR, FillpThreadFun, (void *)param);
415 #else /* for Windows */
416 
417     _beginthreadex(FILLP_NULL_PTR, 0, FillpThreadFun, param, 0, threadId);
418     return 0;
419 
420 #endif
421 }
422 
423 
424 /*******************************************************************************
425     Function     : FillpSysArchInit
426 
427     Description : Adp function if user has not registered the callback for  initializing the
428                        use of the Winsock DLL by a process
429 
430  *******************************************************************************/
431 
432 #if defined(FILLP_LINUX) && defined(FILLP_MAC)
433 
434 #define FillpSysArchGetCurTime(time) (*(time) = mach_absolute_time())
435 
FillpSysArchTimeToLonglong(FillpSysArchTime * time)436 FILLP_LLONG FillpSysArchTimeToLonglong(FillpSysArchTime *time)
437 {
438     if (g_macTimeBaseInfo.denom == 0) {
439         return 0;
440     }
441     FILLP_LLONG l_time = (((*time) * g_macTimeBaseInfo.numer) / (g_macTimeBaseInfo.denom * FILLP_CONST_1K));
442     return l_time;
443 }
444 
445 #elif defined(FILLP_LINUX)
446 
447 #define FillpSysArchGetCurTime(time) (void)clock_gettime(CLOCK_MONOTONIC, time)
448 
FillpSysArchTimeToLonglong(FillpSysArchTime * ptime)449 FILLP_LLONG FillpSysArchTimeToLonglong(FillpSysArchTime *ptime)
450 {
451     FILLP_LLONG l_time = ((FILLP_LLONG)ptime->tv_sec) * FILLP_CONST_1M + (ptime->tv_nsec / FILLP_CONST_1K);
452     return l_time;
453 }
454 
455 
456 #elif defined(FILLP_WIN32)
457 
FillpSysArchInitTime()458 void FillpSysArchInitTime()
459 {
460     (void)QueryPerformanceFrequency(&g_fillpBasePerformanceFrequency);
461     g_fillpBasePerformanceFrequency.QuadPart /= FILLP_CONST_10K;
462 }
463 
FillpSysArchGetCurTime(FillpSysArchTime * timeValue)464 void FillpSysArchGetCurTime(FillpSysArchTime *timeValue)
465 {
466     /* Windows 2000 and later. ---------------------------------- */
467     QueryPerformanceCounter(&(timeValue->time));
468     return;
469 }
470 
FillpSysArchTimeToLonglong(FillpSysArchTime * timeValue)471 FILLP_LLONG FillpSysArchTimeToLonglong(FillpSysArchTime *timeValue)
472 {
473     if (g_fillpBasePerformanceFrequency.QuadPart == 0) {
474         return 0;
475     }
476 
477     return ((FILLP_LLONG)(timeValue->time.QuadPart * FILLP_CONST_100) / g_fillpBasePerformanceFrequency.QuadPart);
478 }
479 
480 #endif
481 
482 
FillpSysAdaptArchGetCurTimeLonglong()483 FILLP_LLONG FillpSysAdaptArchGetCurTimeLonglong()
484 {
485     FillpSysArchTime timeValue;
486     FillpSysArchGetCurTime(&timeValue);
487     return FillpSysArchTimeToLonglong(&timeValue);
488 }
489 
FillpSysArchInit(IN void)490 FILLP_INT FillpSysArchInit(IN void)
491 {
492 #ifdef FILLP_LINUX
493 
494     return ERR_OK;
495 
496 #else /* for Windows */
497 
498     WSADATA wsaData;
499     WORD sockVersion = MAKEWORD(FILLP_CONST_2, FILLP_CONST_2);
500     if (WSAStartup(sockVersion, &wsaData) != 0) {
501         return ERR_FAILURE;
502     }
503     FillpSysArchInitTime();
504     return ERR_OK;
505 
506 #endif
507 }
508 
509 /*******************************************************************************
510     Function     : FillpAdpSysArcGetCurTime
511 
512     Description : Adp function if user has not registered the callback for getting system current time
513  *******************************************************************************/
FillpSysArchGetCurTimeLonglong(IN void)514 FILLP_LLONG FillpSysArchGetCurTimeLonglong(IN void)
515 {
516     return FillpSysAdaptArchGetCurTimeLonglong();
517 }
518 
519 
520 /*******************************************************************************
521     Function     : FillpSysArchAtomicInc
522 
523     Description : Adp function if user has not registered the callback for increment the value
524  *******************************************************************************/
FillpSysArchAtomicInc(IO SysArchAtomic * var,FILLP_INT val)525 FILLP_INT FillpSysArchAtomicInc(IO SysArchAtomic *var, FILLP_INT val)
526 {
527     return FillpSysAdptArchAtomicInc(var, val);
528 }
529 
530 /*******************************************************************************
531     Function     : FillpSysArchAtomicIncAndTest
532 
533     Description : Adp function if user has not registered the callback for increment and test the value
534  *******************************************************************************/
FillpSysArchAtomicIncAndTest(IO SysArchAtomic * var)535 FILLP_BOOL FillpSysArchAtomicIncAndTest(IO SysArchAtomic *var)
536 {
537     return FillpSysAdptArchAtomicIncAndTest(var);
538 }
539 
540 /*******************************************************************************
541     Function     : FillpSysArchAtomicDec
542 
543     Description : Adp function if user has not registered the callback for decrement the value
544  *******************************************************************************/
FillpSysArchAtomicDec(IO SysArchAtomic * var,FILLP_INT val)545 FILLP_INT FillpSysArchAtomicDec(IO SysArchAtomic *var, FILLP_INT val)
546 {
547     return FillpSysAdptArchAtomicDec(var, val);
548 }
549 
550 /*******************************************************************************
551     Function     : FillpSysArchAtomicDecAndTest
552 
553     Description : Adp function if user has not registered the callback for decrement and test the value
554  *******************************************************************************/
FillpSysArchAtomicDecAndTest(IO SysArchAtomic * var)555 FILLP_BOOL FillpSysArchAtomicDecAndTest(IO SysArchAtomic *var)
556 {
557     return FillpSysAdptArchAtomicDecAndTest(var);
558 }
559 
560 /*******************************************************************************
561     Function     : FillpSysArchAtomicDec
562 
563     Description : Adp function if user has not registered the callback for read the value
564  *******************************************************************************/
FillpSysArchAtomicRead(IO SysArchAtomic * var)565 FILLP_INT FillpSysArchAtomicRead(IO SysArchAtomic *var)
566 {
567     return FillpSysAdptArchAtomicRead(var);
568 }
569 
570 /*******************************************************************************
571     Function     : FillpSysArchAtomicSet
572 
573     Description : Adp function if user has not registered the callback for automatic exchange the value
574  *******************************************************************************/
FillpSysArchAtomicSet(IN SysArchAtomic * var,FILLP_INT newValue)575 FILLP_INT FillpSysArchAtomicSet(IN SysArchAtomic *var, FILLP_INT newValue)
576 {
577     return FillpSysAdptArchAtomicSet(var, newValue);
578 }
579 
580 /*******************************************************************************
581     Function     : FillpSysArchCompAndWwap
582 
583     Description : Adp function if user has not registered the callback for compare and swap a value
584  *******************************************************************************/
FillpSysArchCompAndWwap(IO volatile FILLP_ULONG * sem,IN volatile FILLP_ULONG oldValue,IN volatile FILLP_ULONG exchange)585 FILLP_INT FillpSysArchCompAndWwap(
586     IO volatile FILLP_ULONG *sem,
587     IN volatile FILLP_ULONG oldValue,
588     IN volatile FILLP_ULONG exchange)
589 {
590 #ifdef FILLP_LINUX
591     return (FILLP_INT)__sync_bool_compare_and_swap(sem, (FILLP_LLONG)(FILLP_ULLONG)oldValue,
592         (FILLP_LLONG)(FILLP_ULLONG)exchange);
593 #else /* for Windows */
594     return ((InterlockedCompareExchange((LONG volatile *)sem, (LONG)exchange, (LONG)oldValue)) == (LONG)oldValue);
595 #endif
596 }
597 
598 /*******************************************************************************
599     Function     : FillpSysSleepMs
600 
601     Description : Adp function if user has not registered the callback for sleep
602  *******************************************************************************/
603 #ifdef FILLP_LINUX
604 /* use nanosleep as usleep is Obsolete by CC */
605 #define FILLP_ADP_SLEEP_MS(_ms) do  { \
606     struct timespec delay; \
607     delay.tv_sec = (_ms) / FILLP_CONST_1K; \
608     delay.tv_nsec = ((_ms) % FILLP_CONST_1K) * FILLP_CONST_1K * FILLP_CONST_1K; \
609     (void) nanosleep (&delay, FILLP_NULL_PTR); \
610 } while (0)
611 
612 #else
613 
614 #define FILLP_ADP_SLEEP_MS(a) Sleep(a)
615 
616 #endif
617 
FillpSysSleepMs(IN FILLP_UINT timeValue)618 void FillpSysSleepMs(IN FILLP_UINT timeValue) /* In Millseconds */
619 {
620     FILLP_ADP_SLEEP_MS(timeValue);
621     return;
622 }
623 
FillpSysSleepUs(IN FILLP_UINT timeValue)624 FILLP_INT FillpSysSleepUs(IN FILLP_UINT timeValue) /* In micro seconds */
625 {
626 #ifdef FILLP_LINUX
627     struct timespec tmsp;
628     tmsp.tv_sec = timeValue / FILLP_CONST_1M;
629     tmsp.tv_nsec = (timeValue % FILLP_CONST_1M) * FILLP_CONST_1K;
630     return nanosleep(&tmsp, FILLP_NULL_PTR);
631 #else
632     FILLP_UNUSED_PARA(timeValue);
633     return 0;
634 #endif
635 }
636 
637 
638 /*******************************************************************************
639     Function     : FillpAdpRtePause
640 
641     Description : Adp function if user has not registered the callback for pause
642  *******************************************************************************/
FillpAdpRtePause(void)643 void FillpAdpRtePause(void)
644 {
645 #ifdef FILLP_LINUX
646     (void)sleep(0);
647 #endif
648 }
649 
650 #ifdef FILLP_LINUX
651 
652 #define FILLP_ADAPT_SYS_ARCH_SEM_COND_INIT(sem, value) do { \
653     pthread_condattr_t attr; \
654     (void)pthread_condattr_init(&attr); \
655     (void)pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); \
656     (void)pthread_cond_init(&(sem)->cond, &attr); \
657     (void)pthread_condattr_destroy(&attr); \
658     (sem)->counter = (FILLP_INT)(value); \
659 } while (0)
660 
661 #define FILLP_ADAPT_SYS_ARCH_SEM_INIT(sem, value, ret) do { \
662     if (pthread_mutex_init(&(sem)->mutex, FILLP_NULL_PTR) == 0) { \
663         FILLP_ADAPT_SYS_ARCH_SEM_COND_INIT(sem, value); \
664         (ret) = FILLP_OK; \
665     } else { \
666         (ret) = ERR_NULLPTR; \
667     } \
668 } while (0)
669 
670 #else
671 
672 #define FILLP_ADAPT_SYS_ARCH_SEM_INIT(sem, value, ret) do {                           \
673         HANDLE tempSem = CreateSemaphore(FILLP_NULL_PTR, (FILLP_SLONG)(value),        \
674             0xffffff, FILLP_NULL_PTR);                                                \
675         if (tempSem == FILLP_NULL_PTR) {                                              \
676             (ret) = ERR_NULLPTR;                                                      \
677         } else {                                                                      \
678             *(sem) = tempSem;                                                         \
679             (ret) = FILLP_OK;                                                         \
680         }                                                                             \
681     } while (0)
682 
683 #endif
684 
685 /*******************************************************************************
686     Function     : FillpArchInitSem
687 
688     Description : Adp function if user has not registered the callback for semaphore init
689  *******************************************************************************/
FillpArchInitSem(IO SYS_ARCH_SEM * sem,IN FILLP_ULONG value)690 FILLP_INT FillpArchInitSem(IO SYS_ARCH_SEM *sem, IN FILLP_ULONG value)
691 {
692 #ifndef FILLP_MAC
693     FILLP_INT ret;
694     FILLP_ADAPT_SYS_ARCH_SEM_INIT(sem, value, ret);
695     return ret;
696 #endif
697 
698 #ifdef FILLP_MAC
699     kern_return_t ret;
700     ret = semaphore_create(g_fillpMacSelf, sem, SYNC_POLICY_FIFO, value);
701 
702     return ret;
703 #endif
704 }
705 
706 #ifdef FILLP_LINUX
707 #define FILLP_ADAPT_SYS_ARCH_SEM_TRYWAIT(sem) sem_trywait(sem)
708 
709 #define FILLP_ADAPT_SYS_ARCH_SEM_WAIT(sem) sem_wait(sem)
710 
711 #define FILLP_ADAPT_SYS_ARCH_SEM_POST(sem) sem_post(sem)
712 
713 #define FILLP_ADAPT_SYS_ARCH_SEM_DESTROY(sem) sem_destroy(sem)
714 
715 #if defined(FILLP_LW_LITEOS)
FillpAdaptSysArchRwsemInit(SYS_ARCH_RW_SEM * sem)716 staic FillpErrorType FillpAdaptSysArchRwsemInit(SYS_ARCH_RW_SEM *sem)
717 {
718     int ret = pthread_mutex_init(&sem->readMutex, FILLP_NULL_PTR);
719     if (ret != 0) {
720         return ERR_PARAM;
721     }
722     ret = pthread_mutex_init(&sem->writeMutex, FILLP_NULL_PTR);
723     if (ret != 0) {
724         (void)pthread_mutex_destroy(&sem->readMutex);
725         return ERR_PARAM;
726     }
727     sem->readCount = 0;
728     return ERR_OK;
729 }
730 
FillpAdaptSysArchRwsemTryrdwait(SYS_ARCH_RW_SEM * sem)731 static FillpErrorType FillpAdaptSysArchRwsemTryrdwait(SYS_ARCH_RW_SEM *sem)
732 {
733     int ret = pthread_mutex_trylock(&sem->readMutex);
734     if (ret != 0) {
735         return ERR_EAGAIN;
736     }
737     sem->readCount++;
738     if (sem->readCount == 1) {
739         ret = pthread_mutex_trylock(&sem->writeMutex);
740         if (ret != 0) {
741             sem->readCount--;
742             (void)pthread_mutex_unlock(&sem->readMutex);
743             return ERR_EAGAIN;
744         }
745     }
746     (void)pthread_mutex_unlock(&sem->readMutex);
747     return ERR_OK;
748 }
749 
FillpAdaptSysArchRwsemRdPost(SYS_ARCH_RW_SEM * sem)750 static FillpErrorType FillpAdaptSysArchRwsemRdPost(SYS_ARCH_RW_SEM *sem)
751 {
752     int ret = pthread_mutex_lock(&sem->readMutex);
753     if (ret != 0) {
754         return ERR_PARAM;
755     }
756     sem->readCount--;
757     if (sem->readCount == 0) {
758         (void)pthread_mutex_unlock(&sem->writeMutex);
759     }
760     (void)pthread_mutex_unlock(&sem->readMutex);
761     return ERR_OK;
762 }
763 
FillpAdaptSysArchRwsemTrywrwait(SYS_ARCH_RW_SEM * sem)764 static FillpErrorType FillpAdaptSysArchRwsemTrywrwait(SYS_ARCH_RW_SEM *sem)
765 {
766     int ret = pthread_mutex_trylock(&sem->writeMutex);
767     if (ret != 0) {
768         return ERR_EAGAIN;
769     }
770     return ERR_OK;
771 }
772 
FillpAdaptSysArchRwsemWrPost(SYS_ARCH_RW_SEM * sem)773 static FillpErrorType FillpAdaptSysArchRwsemWrPost(SYS_ARCH_RW_SEM *sem)
774 {
775     (void)pthread_mutex_unlock(&sem->writeMutex);
776     return ERR_OK;
777 }
778 
FillpAdaptSysArchRwsemDestroy(SYS_ARCH_RW_SEM * sem)779 static FillpErrorType FillpAdaptSysArchRwsemDestroy(SYS_ARCH_RW_SEM *sem)
780 {
781     (void)pthread_mutex_destroy(&sem->readMutex);
782     (void)pthread_mutex_destroy(&sem->writeMutex);
783     sem->readCount = 0;
784     return ERR_OK;
785 }
786 
787 #else /* FILLP_LW_LITEOS */
788 #define FillpAdaptSysArchRwsemInit(sem) pthread_rwlock_init((sem), FILLP_NULL_PTR)
789 
790 #define FillpAdaptSysArchRwsemTryrdwait(sem) pthread_rwlock_tryrdlock(sem)
791 
792 #define FillpAdaptSysArchRwsemTrywrwait(sem) pthread_rwlock_trywrlock(sem)
793 
794 #define FillpAdaptSysArchRwsemRdPost(sem) pthread_rwlock_unlock(sem)
795 
796 #define FillpAdaptSysArchRwsemWrPost(sem) pthread_rwlock_unlock(sem) // The same with rdpost in linux
797 
798 #define FillpAdaptSysArchRwsemDestroy(sem) pthread_rwlock_destroy(sem)
799 #endif /* FILLP_LW_LITEOS */
800 
801 #else /* FILLP_LINUX */
802 
803 #define FILLP_ADAPT_SYS_ARCH_SEM_WAIT(sem)      WaitForSingleObject(*(sem), 0xffffff)
804 
805 #define FILLP_ADAPT_SYS_ARCH_SEM_TRYWAIT(sem)   WaitForSingleObject(*(sem), 0)
806 
807 #define FILLP_ADAPT_SYS_ARCH_SEM_POST(sem)      ReleaseSemaphore(*(sem), 1, FILLP_NULL_PTR)
808 
809 #define FILLP_ADAPT_SYS_ARCH_SEM_DESTROY(sem)   CloseHandle(*(sem))
810 
811 
812 #define SYS_ARCH_RWSEM_RD_LOCK_STEP 0x2
813 #define SYS_ARCH_RWSEM_WR_LOCK_FLAG 0x01
814 
815 #define CAS(sem, oldValue, newValue) FillpSysArchCompAndWwap((sem), (oldValue), (newValue))
816 
FillpAdaptSysArchRwsemInit(SYS_ARCH_RW_SEM * sem)817 static FillpErrorType FillpAdaptSysArchRwsemInit(SYS_ARCH_RW_SEM *sem)
818 {
819     sem->sem = 0x0;
820     return ERR_OK;
821 }
822 
FillpAdaptSysArchRwsemTryrdwait(SYS_ARCH_RW_SEM * sem)823 static FillpErrorType FillpAdaptSysArchRwsemTryrdwait(SYS_ARCH_RW_SEM *sem)
824 {
825     FILLP_ULONG oldValue;
826     FILLP_ULONG nextValue;
827 
828     do {
829         oldValue = sem->sem;
830         if (oldValue & SYS_ARCH_RWSEM_WR_LOCK_FLAG) { // Write lock
831             return ERR_FAILURE;
832         }
833         nextValue = oldValue + SYS_ARCH_RWSEM_RD_LOCK_STEP;
834     } while (!CAS(&sem->sem, oldValue, nextValue));
835     return ERR_OK;
836 }
837 
838 
FillpAdaptSysArchRwsemTrywrwait(SYS_ARCH_RW_SEM * sem)839 static FillpErrorType FillpAdaptSysArchRwsemTrywrwait(SYS_ARCH_RW_SEM *sem)
840 {
841     FILLP_ULONG oldValue;
842     FILLP_ULONG nextValue;
843 
844     do {
845         oldValue = sem->sem;
846         if (oldValue != 0x0) {
847             return ERR_FAILURE;
848         }
849         nextValue = oldValue | SYS_ARCH_RWSEM_WR_LOCK_FLAG;
850     } while (!CAS(&sem->sem, oldValue, nextValue));
851 
852     return ERR_OK;
853 }
854 
FillpAdaptSysArchRwsemRdPost(SYS_ARCH_RW_SEM * sem)855 static FillpErrorType FillpAdaptSysArchRwsemRdPost(SYS_ARCH_RW_SEM *sem)
856 {
857     FILLP_ULONG oldValue;
858     FILLP_ULONG nextValue;
859 
860     do {
861         oldValue = sem->sem;
862         nextValue = oldValue - SYS_ARCH_RWSEM_RD_LOCK_STEP;
863     } while (!CAS(&sem->sem, oldValue, nextValue));
864 
865     return ERR_OK;
866 }
867 
FillpAdaptSysArchRwsemWrPost(SYS_ARCH_RW_SEM * sem)868 static FillpErrorType FillpAdaptSysArchRwsemWrPost(SYS_ARCH_RW_SEM *sem)
869 {
870     FILLP_ULONG oldValue;
871     FILLP_ULONG nextValue;
872 
873     do {
874         oldValue = sem->sem;
875         nextValue = oldValue & (~SYS_ARCH_RWSEM_WR_LOCK_FLAG);
876     } while (!CAS(&sem->sem, oldValue, nextValue));
877 
878     return ERR_OK;
879 }
880 
FillpAdaptSysArchRwsemDestroy(SYS_ARCH_RW_SEM * sem)881 static FillpErrorType FillpAdaptSysArchRwsemDestroy(SYS_ARCH_RW_SEM *sem)
882 {
883     if (sem->sem != 0x0) {
884         return ERR_FAILURE;
885     }
886 
887     return ERR_OK;
888 }
889 
890 #endif /* FILLP_LINUX */
891 
892 /*******************************************************************************
893     Function     : FillpSemTryWait
894 
895     Description : Adp function if user has not registered the callback for semaphore try wait
896  *******************************************************************************/
FillpSemTryWait(IN SYS_ARCH_SEM * sem)897 FILLP_INT FillpSemTryWait(IN SYS_ARCH_SEM *sem)
898 {
899 #ifndef FILLP_MAC
900 #ifdef FILLP_LINUX
901     FILLP_INT ret;
902 
903     if (pthread_mutex_trylock(&sem->mutex) != 0) {
904         return -1;
905     }
906     if (sem->counter > 0) {
907         sem->counter--;
908         ret = 0;
909     } else {
910         ret = -1;
911     }
912     (void)pthread_mutex_unlock(&sem->mutex);
913     return ret;
914 #else
915     return (FILLP_INT)FILLP_ADAPT_SYS_ARCH_SEM_TRYWAIT(sem);
916 #endif
917 #else
918     /* sem try wait is not implemented in MAC, so simulate it using sem timeout
919          Below timeout value is arrive by doing the throughput test in 2 mthods:
920          a) Test by registering normal semaphore_wait
921          b) Test by registering semaphore_timedwait with timeout value
922     */
923     mach_timespec_t mts;
924     FILLP_SLONG timeout = 1;
925     mts.tv_sec = 0;
926     mts.tv_nsec = 0;
927     clock_get_time(g_sclock, &mts);
928 
929     mts.tv_sec += timeout / FILLP_CONST_1K;
930     mts.tv_nsec += (timeout % FILLP_CONST_1K) * FILLP_CONST_1M;
931 
932     return semaphore_timedwait(*sem, mts);
933 
934 #endif
935 }
936 
937 /*******************************************************************************
938     Function     : FillpSemWait
939 
940     Description : Adp function if user has not registered the callback for semaphore wait
941  *******************************************************************************/
FillpSemWait(IN SYS_ARCH_SEM * sem)942 FILLP_INT FillpSemWait(IN SYS_ARCH_SEM *sem)
943 {
944 #ifndef FILLP_MAC
945 #ifdef FILLP_LINUX
946     if (pthread_mutex_lock(&sem->mutex) != 0) {
947         return -1;
948     }
949     while (sem->counter <= 0) {
950         if (pthread_cond_wait(&sem->cond, &sem->mutex) != 0) {
951             (void)pthread_mutex_unlock(&sem->mutex);
952             return -1;
953         }
954     }
955     sem->counter--;
956     (void)pthread_mutex_unlock(&sem->mutex);
957     return 0;
958 #else
959     return (FILLP_INT)FILLP_ADAPT_SYS_ARCH_SEM_WAIT(sem);
960 #endif
961 #else
962     kern_return_t ret;
963     ret = semaphore_wait(*sem);
964     return ret;
965 #endif
966 }
967 
968 
969 /*******************************************************************************
970     Function     : FillpSemPost
971 
972     Description : Adp function if user has not registered the callback for semaphore post
973  *******************************************************************************/
FillpSemPost(IN SYS_ARCH_SEM * sem)974 FILLP_INT FillpSemPost(IN SYS_ARCH_SEM *sem)
975 {
976 #ifndef FILLP_MAC
977 #ifdef FILLP_LINUX
978     if (pthread_mutex_lock(&sem->mutex) != 0) {
979         return -1;
980     }
981     sem->counter++;
982     (void)pthread_cond_signal(&sem->cond);
983     (void)pthread_mutex_unlock(&sem->mutex);
984     return 0;
985 #else
986     return (FILLP_ADAPT_SYS_ARCH_SEM_POST(sem) == 0);
987 #endif
988 #else
989     kern_return_t ret;
990     ret = semaphore_signal(*sem);
991     return ret;
992 #endif
993 }
994 
995 /*******************************************************************************
996     Function     : FillpSemDestroy
997 
998     Description : Adp function if user has not registered the callback for semaphore destroy
999  *******************************************************************************/
FillpSemDestroy(IN SYS_ARCH_SEM * sem)1000 FILLP_INT FillpSemDestroy(IN SYS_ARCH_SEM *sem)
1001 {
1002 #ifndef FILLP_MAC
1003 #ifdef FILLP_LINUX
1004     FILLP_INT ret = pthread_mutex_lock(&(sem->mutex));
1005     if (ret != 0) {
1006         return -1;
1007     }
1008 
1009     if (pthread_cond_destroy(&(sem->cond)) != 0) {
1010         ret = -1;
1011     }
1012     if (pthread_mutex_unlock(&(sem->mutex)) != 0) {
1013         ret = -1;
1014     }
1015     if (pthread_mutex_destroy(&(sem->mutex)) != 0) {
1016         ret = -1;
1017     }
1018     return ret;
1019 #else
1020     return FILLP_ADAPT_SYS_ARCH_SEM_DESTROY(sem);
1021 #endif
1022 #else
1023     kern_return_t ret;
1024     ret = semaphore_destroy(g_fillpMacSelf, *sem);
1025     return ret;
1026 #endif
1027 }
1028 
FillpArchInitRwSem(IO SYS_ARCH_RW_SEM * sem)1029 static FILLP_INT FillpArchInitRwSem(IO SYS_ARCH_RW_SEM *sem)
1030 {
1031     return FillpAdaptSysArchRwsemInit(sem);
1032 }
1033 
1034 
FillpRwSemTryRdWait(IN SYS_ARCH_RW_SEM * sem)1035 FILLP_INT FillpRwSemTryRdWait(IN SYS_ARCH_RW_SEM *sem)
1036 {
1037     return FillpAdaptSysArchRwsemTryrdwait(sem);
1038 }
1039 
FillpRwSemTryWrWait(IN SYS_ARCH_RW_SEM * sem)1040 FILLP_INT FillpRwSemTryWrWait(IN SYS_ARCH_RW_SEM *sem)
1041 {
1042     return FillpAdaptSysArchRwsemTrywrwait(sem);
1043 }
1044 
1045 /*******************************************************************************
1046     Function     : FillpRwSemDestroy
1047 
1048     Description : Adp function if user has not registered the callback for semaphore destroy
1049  *******************************************************************************/
FillpRwSemDestroy(IN SYS_ARCH_RW_SEM * sem)1050 FILLP_INT FillpRwSemDestroy(IN SYS_ARCH_RW_SEM *sem)
1051 {
1052     return FillpAdaptSysArchRwsemDestroy(sem);
1053 }
1054 
1055 /*******************************************************************************
1056     Function     : FillpRwSemWrPost
1057 
1058     Description : Adp function if user has not registered the callback for semaphore post
1059  *******************************************************************************/
FillpRwSemWrPost(IN SYS_ARCH_RW_SEM * sem)1060 FILLP_INT FillpRwSemWrPost(IN SYS_ARCH_RW_SEM *sem)
1061 {
1062     return FillpAdaptSysArchRwsemWrPost(sem);
1063 }
1064 
1065 /*******************************************************************************
1066     Function     : FillpRwSemRdPost
1067 
1068     Description : Adp function if user has not registered the callback for semaphore post
1069  *******************************************************************************/
FillpRwSemRdPost(IN SYS_ARCH_RW_SEM * sem)1070 FILLP_INT FillpRwSemRdPost(IN SYS_ARCH_RW_SEM *sem)
1071 {
1072     return FillpAdaptSysArchRwsemRdPost(sem);
1073 }
1074 
1075 
1076 /*******************************************************************************
1077     Function     : FillpFuncCreateSocket
1078 
1079     Description : Adp function if user has not registered the Create socket callback
1080  *******************************************************************************/
FillpFuncCreateSocket(IN FILLP_INT32 domain,IN FILLP_INT32 type,IN FILLP_INT32 protocol)1081 FILLP_INT32  FillpFuncCreateSocket(
1082     IN FILLP_INT32 domain,   /* the address family */
1083     IN FILLP_INT32 type,     /* new socket */
1084     IN FILLP_INT32 protocol) /* protocol to be used */
1085 {
1086     return (FILLP_INT32)socket((int)domain, (int)type, (int)protocol);
1087 }
1088 
1089 /*******************************************************************************
1090     Function     : FillpFuncBindSocket
1091 
1092     Description : Adp function if user has not registered the Bind socket callback function
1093  *******************************************************************************/
FillpFuncBindSocket(IN FILLP_INT32 sockFd,IN FILLP_CONST void * myAddr,IN FILLP_INT32 addrLen)1094 FILLP_INT32 FillpFuncBindSocket(
1095     IN FILLP_INT32 sockFd, /* socket fd */
1096     IN FILLP_CONST void *myAddr, /* bind addr */
1097     IN FILLP_INT32 addrLen) /* addr length */
1098 {
1099     return (FILLP_INT32)bind(sockFd, myAddr, (socklen_t)addrLen);
1100 }
1101 
FillpFuncConnectSocket(IN FILLP_INT32 sockFd,IN FILLP_CONST void * myAddr,IN FILLP_INT32 addrLen)1102 FILLP_INT32 FillpFuncConnectSocket(
1103     IN FILLP_INT32 sockFd,       /* socket fd */
1104     IN FILLP_CONST void *myAddr, /* bind addr */
1105     IN FILLP_INT32 addrLen)      /* addr length */
1106 {
1107     return connect(sockFd, myAddr, (socklen_t)addrLen);
1108 }
1109 
FillpFuncGetSockName(IN FILLP_INT32 sockFd,IN void * myAddr,IN void * addrLen)1110 FILLP_INT32 FillpFuncGetSockName(
1111     IN FILLP_INT32 sockFd, /* socket fd */
1112     IN void *myAddr,       /* bind addr */
1113     IN void *addrLen)      /* addr length */
1114 {
1115 #ifdef FILLP_WIN32
1116     return getsockname(sockFd, (struct sockaddr *)myAddr, (int *)addrLen);
1117 #else
1118     return getsockname(sockFd, (struct sockaddr *)myAddr, (socklen_t *)addrLen);
1119 #endif
1120 }
1121 
1122 
FillpFuncIoCtlSocket(FILLP_INT fd,FILLP_INT type,FILLP_ULONG * parg)1123 FILLP_INT FillpFuncIoCtlSocket(FILLP_INT fd, FILLP_INT type, FILLP_ULONG *parg)
1124 {
1125 #ifdef FILLP_WIN32
1126     return ioctlsocket(fd, type, (FILLP_ULONG *)parg);
1127 #else
1128     return ioctl(fd, (FILLP_ULONG)type, parg);
1129 
1130 #endif
1131 }
1132 
1133 
FillpFuncFcntl(IN FILLP_INT fd,IN FILLP_INT cmd,IN FILLP_INT val)1134 FILLP_INT FillpFuncFcntl(
1135     IN FILLP_INT fd,  /* connection fd */
1136     IN FILLP_INT cmd, /* command to perform on socket */
1137     IN FILLP_INT val) /* arguments for socket */
1138 {
1139 #if defined(FILLP_LW_LITEOS)
1140     return lwip_fcntl(fd, cmd, val);
1141 #elif defined(FILLP_WIN32)
1142     FILLP_UNUSED_PARA(fd);
1143     FILLP_UNUSED_PARA(cmd);
1144     FILLP_UNUSED_PARA(val);
1145     return 0;
1146 #else
1147     return fcntl(fd, cmd, val);
1148 #endif
1149 }
1150 
FillpFuncSetSockOpt(IN FILLP_INT sockFd,IN FILLP_INT level,IN FILLP_INT optName,IN FILLP_CONST void * optVal,IN FILLP_INT optLen)1151 FILLP_INT FillpFuncSetSockOpt(IN FILLP_INT sockFd, IN FILLP_INT level, IN FILLP_INT optName,
1152     IN FILLP_CONST void *optVal, IN FILLP_INT optLen)
1153 {
1154     return setsockopt(sockFd, level, optName, optVal, (socklen_t)optLen);
1155 }
1156 
FillpFuncGetSockOpt(IN FILLP_INT sockFd,IN FILLP_INT level,IN FILLP_INT optName,IO void * optVal,IO FILLP_INT * optLen)1157 FILLP_INT FillpFuncGetSockOpt(IN FILLP_INT sockFd, IN FILLP_INT level, IN FILLP_INT optName, IO void *optVal,
1158     IO FILLP_INT *optLen)
1159 {
1160 #ifdef FILLP_WIN32
1161     return getsockopt(sockFd, level, optName, (char *)optVal, (int *)optLen);
1162 #else
1163     return getsockopt(sockFd, level, optName, optVal, (socklen_t *)optLen);
1164 #endif
1165 }
1166 
1167 
1168 /*******************************************************************************
1169     Function     : FillpFuncCloseSocket
1170 
1171     Description : Adp function if user has not registered the close socket callback function
1172  *******************************************************************************/
FillpFuncCloseSocket(IN FILLP_INT32 sockFd)1173 FILLP_INT32 FillpFuncCloseSocket(IN FILLP_INT32 sockFd)
1174 {
1175 #ifdef FILLP_LINUX
1176 
1177     return close(sockFd);
1178 
1179 #else /* For Windows */
1180 
1181     return closesocket(sockFd);
1182 
1183 #endif
1184 }
1185 
1186 /*******************************************************************************
1187     Function     : FillpFuncSendTo
1188 
1189     Description : Adp function if user has not registered the sendto callback function
1190  *******************************************************************************/
FillpFuncSendTo(IN FILLP_INT sockFd,IN const void * buf,IN FILLP_SIZE_T len,IN FILLP_INT flags,IN const void * to,IN FILLP_SIZE_T toLen)1191 FILLP_INT FillpFuncSendTo(IN FILLP_INT sockFd, IN const void *buf, IN FILLP_SIZE_T len, IN FILLP_INT flags,
1192     IN const void *to, IN FILLP_SIZE_T toLen)
1193 {
1194 #ifdef FILLP_WIN64
1195     return (FILLP_INT)sendto(sockFd, buf, (FILLP_INT)len, flags, to, (socklen_t)toLen);
1196 #else
1197     return (FILLP_INT)sendto(sockFd, buf, len, flags, to, (socklen_t)toLen);
1198 #endif
1199 }
1200 
1201 /*******************************************************************************
1202     Function     : FillpFuncRecvFrom
1203     Description  : Adp function if user has not registered the receive from callback function
1204  *******************************************************************************/
FillpFuncRecvFrom(IN FILLP_INT sockFd,OUT void * buf,IN FILLP_SIZE_T len,IN FILLP_INT flags,OUT void * from,IO FILLP_SIZE_T * fromLen)1205 FILLP_INT FillpFuncRecvFrom(IN FILLP_INT sockFd, OUT void *buf, IN FILLP_SIZE_T len, IN FILLP_INT flags,
1206     OUT void *from, IO FILLP_SIZE_T *fromLen)
1207 {
1208 #ifdef FILLP_WIN32
1209 #ifdef FILLP_WIN64
1210     return (FILLP_INT)recvfrom(sockFd, buf, (int)len, flags, (struct sockaddr *)from, (int *)fromLen);
1211 #else
1212     return (FILLP_INT)recvfrom(sockFd, buf, len, flags, (struct sockaddr *)from, (int *)fromLen);
1213 #endif
1214 #else
1215     return (FILLP_INT)recvfrom(sockFd, buf, len, flags, from, (socklen_t *)fromLen);
1216 #endif
1217 }
1218 
FillpFuncSend(IN FILLP_INT sockFd,IN const void * buffer,IN FILLP_INT len,IN FILLP_INT flags)1219 FILLP_INT FillpFuncSend(
1220     IN FILLP_INT sockFd,   /* Connection fd */
1221     IN const void *buffer, /* buffer to hold data to be sent */
1222     IN FILLP_INT len,      /* no of bytes to be sent */
1223     IN FILLP_INT flags)    /* flags to tell the status */
1224 {
1225     return (FILLP_INT)send(sockFd, buffer, (FILLP_SIZE_T)(unsigned int)len, flags);
1226 }
1227 
1228 #ifdef FILLP_LINUX
FillpAdaptSysArchSemClose(SYS_ARCH_SEM * sem)1229 FILLP_INT FillpAdaptSysArchSemClose(SYS_ARCH_SEM *sem)
1230 {
1231 #ifndef FILLP_MAC
1232     return FillpSemDestroy(sem);
1233 #endif
1234 
1235     /* Once inited with semaphore_create() in callback sysArchSemInit, it will be destroyed
1236         semaphore_destroy() in sysArchSemDestroy. here is no semaphore_close() in MAC
1237     */
1238 #ifdef FILLP_MAC
1239     FILLP_UNUSED_PARA(sem);
1240     return FILLP_SUCCESS;
1241 #endif
1242 }
1243 
1244 #else
1245 
FillpAdaptSysArchSemClose(SYS_ARCH_SEM * sem)1246 FILLP_INT FillpAdaptSysArchSemClose(SYS_ARCH_SEM *sem)
1247 {
1248     return CloseHandle(*sem);
1249 }
1250 
1251 #endif
1252 
1253 /* callback for sys_arch_named_sem_close */
FillpSysArchSemClose(SYS_ARCH_SEM * sem)1254 FILLP_INT FillpSysArchSemClose(SYS_ARCH_SEM *sem)
1255 {
1256     return FillpAdaptSysArchSemClose(sem);
1257 }
1258 
1259 #ifdef FILLP_LINUX
FillpAdaptSysArchSemWaitTimeout(SYS_ARCH_SEM * sem,FILLP_SLONG timeout)1260 static FILLP_INT FillpAdaptSysArchSemWaitTimeout(SYS_ARCH_SEM *sem, FILLP_SLONG timeout)
1261 {
1262 #ifndef FILLP_MAC
1263     FILLP_LLONG start;
1264     FILLP_LLONG end;
1265     FillpSysArchTime timeValue;
1266 
1267     (void)clock_gettime(CLOCK_MONOTONIC, &timeValue);
1268     start = FillpSysArchTimeToLonglong(&timeValue);
1269     end = start + (timeout * FILLP_CONST_1K);
1270     timeValue.tv_sec = (time_t)(end / FILLP_CONST_1M);
1271     timeValue.tv_nsec = (long)((end % FILLP_CONST_1M) * FILLP_CONST_1K);
1272 
1273     if (pthread_mutex_lock(&sem->mutex) != 0) {
1274         return -1;
1275     }
1276     while (sem->counter <= 0) {
1277         if (pthread_cond_timedwait(&sem->cond, &sem->mutex, &timeValue) != 0) {
1278             (void)pthread_mutex_unlock(&sem->mutex);
1279             return -1;
1280         }
1281     }
1282     sem->counter--;
1283     (void)pthread_mutex_unlock(&sem->mutex);
1284     return 0;
1285 
1286 #endif
1287 
1288 #ifdef FILLP_MAC
1289     mach_timespec_t mts;
1290     mts.tv_sec = 0;
1291     mts.tv_nsec = 0;
1292     clock_get_time(g_sclock, &mts);
1293 
1294     mts.tv_sec += timeout / FILLP_CONST_1K;
1295     mts.tv_nsec += (timeout % FILLP_CONST_1K) * FILLP_CONST_1M;
1296 
1297     return semaphore_timedwait(*sem, mts);
1298 #endif
1299 }
1300 
1301 #else
1302 
FillpAdaptSysArchSemWaitTimeout(SYS_ARCH_SEM * sem,FILLP_SLONG timeout)1303 FILLP_INT FillpAdaptSysArchSemWaitTimeout(SYS_ARCH_SEM *sem, FILLP_SLONG timeout)
1304 {
1305     DWORD ret = WaitForSingleObject(*sem, timeout);
1306     if (ret == WAIT_TIMEOUT) {
1307         return ERR_OK;
1308     } else {
1309         return ERR_COMM;
1310     }
1311 }
1312 
FillpAdaptSysArchSemTryWait(SYS_ARCH_SEM * sem,FILLP_SLONG timeout)1313 FILLP_INT FillpAdaptSysArchSemTryWait(SYS_ARCH_SEM *sem, FILLP_SLONG timeout)
1314 {
1315     return WaitForSingleObject(*sem, timeout);
1316 }
1317 
1318 #endif
1319 
1320 /* callback for sys_arch_named_sem_wait_timeout */
FillpSysArchSemWaitTimeout(SYS_ARCH_SEM * sem,FILLP_SLONG timeout)1321 FILLP_INT FillpSysArchSemWaitTimeout(SYS_ARCH_SEM *sem, FILLP_SLONG timeout)
1322 {
1323     return FillpAdaptSysArchSemWaitTimeout(sem, timeout);
1324 }
1325 
FillpSysArchSchedYield(void)1326 static FILLP_INT FillpSysArchSchedYield(void)
1327 {
1328 #ifdef FILLP_LINUX
1329 #if defined(FILLP_LW_LITEOS)
1330     return LOS_TaskYield();
1331 #else
1332     return sched_yield();
1333 #endif
1334 #else
1335     return FILLP_SUCCESS;
1336 #endif
1337 }
1338 
1339 #if defined(FILLP_MAC)
FillpSysOsInit(IN void)1340 void FillpSysOsInit(IN void)
1341 {
1342     g_fillpMacSelf = mach_task_self();
1343     host_get_clock_service(g_fillpMacSelf, SYSTEM_CLOCK, &g_sclock);
1344     (void)mach_timebase_info(&g_macTimeBaseInfo);
1345     return;
1346 }
1347 #elif defined(FILLP_WIN32)
FillpSysOsInit(IN void)1348 void FillpSysOsInit(IN void)
1349 {
1350     FillpSysArchInitTime();
1351     return;
1352 }
1353 #else
1354 #define FillpSysOsInit()
1355 #endif
1356 
1357 #if defined(FILLP_MAC)
FillpSysOsDeinit(IN void)1358 void FillpSysOsDeinit(IN void)
1359 {
1360     mach_port_deallocate(g_fillpMacSelf, g_sclock);
1361     return;
1362 }
1363 #else
FillpSysOsDeinit(IN void)1364 void FillpSysOsDeinit(IN void)
1365 {
1366     return;
1367 }
1368 #endif
1369 
FillpRegBasicFun(void)1370 static void FillpRegBasicFun(void)
1371 {
1372     /* Basic Os function Registration start */
1373     g_fillpOsBasicLibFun.memCalloc = FillpMemCalloc;
1374     g_fillpOsBasicLibFun.memAlloc = FillpMemAlloc;
1375     g_fillpOsBasicLibFun.memFree = FillpMemFree;
1376     g_fillpOsBasicLibFun.memChr = FillpMemChr;
1377     g_fillpOsBasicLibFun.fillpStrLen = FillpStrLen;
1378     g_fillpOsBasicLibFun.fillpRand = FillpRand;
1379     g_fillpOsBasicLibFun.fillpCreateThread = FillpCreateThread;
1380     g_fillpOsBasicLibFun.sysArcInit = FillpSysArchInit;
1381     g_fillpOsBasicLibFun.sysArcGetCurTimeLongLong = FillpSysArchGetCurTimeLonglong;
1382     g_fillpOsBasicLibFun.sysArchAtomicInc = FillpSysArchAtomicInc;
1383     g_fillpOsBasicLibFun.sysArchAtomicIncAndTest = FillpSysArchAtomicIncAndTest;
1384     g_fillpOsBasicLibFun.sysArchAtomicDec = FillpSysArchAtomicDec;
1385     g_fillpOsBasicLibFun.sysArchAtomicDecAndTest = FillpSysArchAtomicDecAndTest;
1386     g_fillpOsBasicLibFun.sysArchAtomicRead = FillpSysArchAtomicRead;
1387     g_fillpOsBasicLibFun.sysArchAtomicSet = FillpSysArchAtomicSet;
1388     g_fillpOsBasicLibFun.sysArchCompAndSwap = FillpSysArchCompAndWwap;
1389     g_fillpOsBasicLibFun.sysSleepMs = FillpSysSleepMs;
1390     g_fillpOsBasicLibFun.sysUsleep = FillpSysSleepUs;
1391     g_fillpOsBasicLibFun.rtePause = FillpAdpRtePause;
1392     /* product MUST register for this, there is no default callback function for this */
1393     g_fillpOsBasicLibFun.cryptoRand = FILLP_NULL_PTR;
1394 
1395     /* Semaphore function Registration start */
1396     g_fillpOsSemLibFun.sysArchSemClose = FillpSysArchSemClose;
1397     g_fillpOsSemLibFun.sysArchSemInit = FillpArchInitSem;
1398     g_fillpOsSemLibFun.sysArchSemTryWait = FillpSemTryWait;
1399     g_fillpOsSemLibFun.sysArchSemWait = FillpSemWait;
1400     g_fillpOsSemLibFun.sysArchSemPost = FillpSemPost;
1401     g_fillpOsSemLibFun.sysArchSemDestroy = FillpSemDestroy;
1402     g_fillpOsSemLibFun.sysArchSemWaitTimeout = FillpSysArchSemWaitTimeout;
1403     g_fillpOsSemLibFun.sysArchRWSemInit = FillpArchInitRwSem;
1404     g_fillpOsSemLibFun.sysArchRWSemTryRDWait = FillpRwSemTryRdWait;
1405     g_fillpOsSemLibFun.sysArchRWSemTryWRWait = FillpRwSemTryWrWait;
1406     g_fillpOsSemLibFun.sysArchRWSemRDPost = FillpRwSemRdPost;
1407     g_fillpOsSemLibFun.sysArchRWSemWRPost = FillpRwSemWrPost;
1408     g_fillpOsSemLibFun.sysArchRWSemDestroy = FillpRwSemDestroy;
1409     g_fillpOsSemLibFun.sysArchSchedYield = FillpSysArchSchedYield;
1410 }
1411 
1412 /*******************************************************************************
1413     Function     : FillpRegAdpLibSysFunc
1414 
1415     Des            : Adaptor function
1416     Input         : None
1417 
1418 
1419     Output       :None
1420 
1421     Return       : Fillp_SUCCESS - Success
1422                        ERROR CODES    - Failure
1423  *******************************************************************************/
FillpRegLibSysFunc(IN void)1424 void FillpRegLibSysFunc(IN void)
1425 {
1426     FillpSysOsInit();
1427 
1428     FillpRegBasicFun();
1429 
1430     /* Socket function registration Start */
1431     g_fillpOsSocketLibFun.socketCallbackFunc = FillpFuncCreateSocket;
1432     g_fillpOsSocketLibFun.select = FillpSelect;
1433     g_fillpOsSocketLibFun.bindCallbackFunc = FillpFuncBindSocket;
1434     g_fillpOsSocketLibFun.closeSocketCallbackFunc = FillpFuncCloseSocket;
1435     g_fillpOsSocketLibFun.recvFromCallbackFunc = FillpFuncRecvFrom;
1436     g_fillpOsSocketLibFun.sendtoCallbackFunc = FillpFuncSendTo;
1437     g_fillpOsSocketLibFun.ioctl = FillpFuncIoCtlSocket;
1438     g_fillpOsSocketLibFun.fcntl = FillpFuncFcntl;
1439     g_fillpOsSocketLibFun.setSockOpt = FillpFuncSetSockOpt;
1440     g_fillpOsSocketLibFun.getSockOpt = FillpFuncGetSockOpt;
1441     g_fillpOsSocketLibFun.sendCallbackFunc = FillpFuncSend;
1442     g_fillpOsSocketLibFun.getSockNameCallbackFunc = FillpFuncGetSockName;
1443     g_fillpOsSocketLibFun.connectCallbackFunc = FillpFuncConnectSocket;
1444     g_fillpOsSocketLibFun.fillpFuncFdClr = FillpFuncFdClr;
1445     g_fillpOsSocketLibFun.fillpFuncFdSet = FillpFuncFdSet;
1446     g_fillpOsSocketLibFun.fillpFuncFdIsSet = FillpFuncFdIsSet;
1447     g_fillpOsSocketLibFun.fillpFuncCreateFdSet = FillpFuncCreateFdSet;
1448     g_fillpOsSocketLibFun.fillpFuncDestroyFdSet = FillpFuncDestroyFdSet;
1449     g_fillpOsSocketLibFun.fillpFuncCopyFdSet = FillpFuncCopyFdSet;
1450 
1451     /* Other function registration Start */
1452     g_fillpAppCbkFun.fillpSockCloseCbkFunc = FILLP_NULL_PTR;
1453 }
1454 
1455 #ifdef __cplusplus
1456 }
1457 #endif
1458