1 /*
2  * Copyright (C) 2021 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 SYS_UTIL_H
17 #define SYS_UTIL_H
18 
19 #include "nstackx_common_header.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #ifndef htobe64
26 #define PP_HTON64(x) ((((x) & (uint64_t)0x00000000000000ffULL) << 56) | \
27                       (((x) & (uint64_t)0xff00000000000000ULL) >> 56) | \
28                       (((x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
29                       (((x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
30                       (((x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
31                       (((x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
32                       (((x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
33                       (((x) & (uint64_t)0x000000ff00000000ULL) >>  8))
34 #if BYTE_ORDER == BIG_ENDIAN
35 #define htobe64(x) (x)
36 #define htole64(x) PP_HTON64(x)
37 #define be64toh(x) (x)
38 #define le64toh(x) PP_HTON64(x)
39 #else
40 #define htobe64(x) PP_HTON64(x)
41 #define htole64(x) (x)
42 #define be64toh(x) PP_HTON64(x)
43 #define le64toh(x) (x)
44 #endif /* BYTE_ORDER == BIG_ENDIAN */
45 #endif /* htobe64 */
46 
47 #define PATH_SEPARATOR '/'
48 #define INVALID_TID (pthread_t)(-1)
49 
50 #ifdef LWIP_LITEOS_A_COMPAT
51 #ifndef atomic_t
52 typedef uint64_t atomic_t;
53 #endif
54 
55 #define NSTACKX_ATOM_FETCH(ptr) (*ptr)
56 #define NSTACKX_ATOM_SET(ptr, i) ((*ptr) = (i))
57 #define NSTACKX_ATOM_FETCH_INC(ptr) __sync_fetch_and_add((ptr), 1)
58 #define NSTACKX_ATOM_FETCH_DEC(ptr) __sync_fetch_and_sub((ptr), 1)
59 #define NSTACKX_ATOM_ADD_RETURN(ptr, i) __sync_add_and_fetch((ptr), i)
60 #define NSTACKX_ATOM_FETCH_ADD(ptr, val) __sync_fetch_and_add((ptr), (val))
61 #define NSTACKX_ATOM_FETCH_SUB(ptr, val) __sync_fetch_and_sub((ptr), (val))
62 
63 #else /* LWIP_LITEOS_A_COMPAT */
64 #define NSTACKX_ATOM_FETCH(ptr) atomic_read(ptr)
65 #define NSTACKX_ATOM_SET(ptr, i) atomic_set(ptr, i)
66 #define NSTACKX_ATOM_FETCH_INC(ptr) atomic_inc(ptr)
67 #define NSTACKX_ATOM_FETCH_DEC(ptr) atomic_dec(ptr)
68 #define NSTACKX_ATOM_ADD_RETURN(ptr, i) atomic_add_return(i, ptr)
69 #define NSTACKX_ATOM_FETCH_ADD(ptr, val) atomic_add((val), (ptr))
70 #define NSTACKX_ATOM_FETCH_SUB(ptr, val) atomic_sub((val), (ptr))
71 #endif /* LWIP_LITEOS_A_COMPAT */
72 
GetErrno(void)73 static inline int32_t GetErrno(void)
74 {
75     return errno;
76 }
77 
78 #define CloseSocketInner CloseDesc
79 #define gettid() (pid_t)pthread_self()
80 NSTACKX_EXPORT void CloseDesc(int32_t desc);
81 NSTACKX_EXPORT int32_t GetInterfaceList(struct ifconf *ifc, struct ifreq *buf, uint32_t size);
82 NSTACKX_EXPORT int32_t GetInterfaceIP(int32_t fd, struct ifreq *interface);
83 NSTACKX_EXPORT int32_t GetTargetInterface(const struct sockaddr_in *dstAddr, struct ifreq *localDev);
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif // NSTACKX_UTIL_H
90