• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 OHOS_DISTRIBUTEDSCHEDULE_DMSLITE_UTILS_H
17 #define OHOS_DISTRIBUTEDSCHEDULE_DMSLITE_UTILS_H
18 
19 #include <stdbool.h>
20 #ifdef WEARABLE_PRODUCT
21 #include "ohos_mem_pool.h"
22 #endif
23 
24 #define PACKET_MARSHALL_HELPER(type, fieldType, field) \
25     do { \
26         bool ret = Marshall##type((field), (fieldType)); \
27         if (!ret) { \
28             HILOGE("%{public}s marshall value failed!", __func__); \
29             CleanBuild(); \
30             return -1; \
31         } \
32     } while (0)
33 
34 #define RAWDATA_MARSHALL_HELPER(type, fieldType, field, length) \
35     do { \
36         bool ret = Marshall##type((field), (fieldType), (length)); \
37         if (!ret) { \
38             HILOGE("%{public}s marshall value failed!", __func__); \
39             CleanBuild(); \
40             return -1; \
41         } \
42     } while (0)
43 
IsBigEndian()44 static inline bool IsBigEndian()
45 {
46     union {
47         uint16_t a;
48         uint8_t b;
49     } c;
50     c.a = 1;
51     return (c.b == 0);
52 }
53 
54 #ifdef WEARABLE_PRODUCT
55 #define DMS_ALLOC(size) OhosMalloc(MEM_TYPE_APPFMK_LSRAM, size)
56 #define DMS_FREE(a) \
57     do { \
58         if ((a) != NULL) { \
59             (void) OhosFree((void *)(a)); \
60             (a) = NULL; \
61         } \
62     } while (0)
63 #else
64 #define DMS_ALLOC(size) malloc(size)
65 #define DMS_FREE(a) \
66     do { \
67         if ((a) != NULL) { \
68             (void) free((void *)(a)); \
69             (a) = NULL; \
70         } \
71     } while (0)
72 #endif
73 
74 /*
75  * convert u16 data from Big Endian to Little Endian
76  * dataIn: pointer to start of u16 data
77  * dataOut: the converted u16 data
78  */
Convert16DataBig2Little(const uint8_t * dataIn,uint16_t * dataOut)79 static inline void Convert16DataBig2Little(const uint8_t *dataIn, uint16_t *dataOut)
80 {
81     *dataOut  = ((uint16_t)(*dataIn++) << 8);
82     *dataOut |=  (uint16_t)(*dataIn);
83 }
84 
85 /*
86  * convert u32 data from Big Endian to Little Endian
87  * dataIn: pointer to start of u32 data
88  * dataOut: the converted u32 data
89  */
Convert32DataBig2Little(const uint8_t * dataIn,uint32_t * dataOut)90 static inline void Convert32DataBig2Little(const uint8_t *dataIn, uint32_t *dataOut)
91 {
92     *dataOut  = ((uint32_t)(*dataIn++) << 24);
93     *dataOut |= ((uint32_t)(*dataIn++) << 16);
94     *dataOut |= ((uint32_t)(*dataIn++) << 8);
95     *dataOut |=  (uint32_t)(*dataIn);
96 }
97 
98 /*
99  * convert u64 data from Big Endian to Little Endian
100  * dataIn: pointer to start of u64 data
101  * dataOut: the converted u64 data
102  */
Convert64DataBig2Little(const uint8_t * dataIn,uint64_t * dataOut)103 static inline void Convert64DataBig2Little(const uint8_t *dataIn, uint64_t *dataOut)
104 {
105     *dataOut  = ((uint64_t)(*dataIn++) << 56);
106     *dataOut |= ((uint64_t)(*dataIn++) << 48);
107     *dataOut |= ((uint64_t)(*dataIn++) << 40);
108     *dataOut |= ((uint64_t)(*dataIn++) << 32);
109     *dataOut |= ((uint64_t)(*dataIn++) << 24);
110     *dataOut |= ((uint64_t)(*dataIn++) << 16);
111     *dataOut |= ((uint64_t)(*dataIn++) << 8);
112     *dataOut |=  (uint64_t)(*dataIn);
113 }
114 
115 /*
116  * convert u16 data from Little Endian to Big Endian
117  * dataIn: pointer of the u16 data
118  * dataOut: the converted u16 data
119  */
Convert16DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)120 static inline void Convert16DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
121 {
122     *dataOut++ = *(dataIn + 1);
123     *dataOut   = *(dataIn);
124 }
125 
126 /*
127  * convert u32 data from Little Endian to Big Endian
128  * dataIn: pointer of the u32 data
129  * dataOut: the converted u32 data
130  */
Convert32DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)131 static inline void Convert32DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
132 {
133     *dataOut++ = *(dataIn + 3);
134     *dataOut++ = *(dataIn + 2);
135     *dataOut++ = *(dataIn + 1);
136     *dataOut   = *(dataIn);
137 }
138 
139 /*
140  * convert u64 data from Little Endian to Big Endian
141  * dataIn: pointer of the u64 data
142  * dataOut: the converted u64 data
143  */
Convert64DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)144 static inline void Convert64DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
145 {
146     *dataOut++ = *(dataIn + 7);
147     *dataOut++ = *(dataIn + 6);
148     *dataOut++ = *(dataIn + 5);
149     *dataOut++ = *(dataIn + 4);
150     *dataOut++ = *(dataIn + 3);
151     *dataOut++ = *(dataIn + 2);
152     *dataOut++ = *(dataIn + 1);
153     *dataOut   = *(dataIn);
154 }
155 #endif // OHOS_DISTRIBUTEDSCHEDULE_DMSLITE_UTILS_H