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 static const uint32_t ONE_BITE = 1;
75 static const uint32_t TWO_BITE = 2;
76 static const uint32_t THREE_BITE = 3;
77 static const uint32_t FOUR_BITE = 4;
78 static const uint32_t FIVE_BITE = 5;
79 static const uint32_t SIX_BITE = 6;
80 static const uint32_t SEVEN_BITE = 7;
81 
82 static const uint32_t BITS_NUM_OF_PER_BITE = 8;
83 static const uint32_t BITS_NUM_OF_TWO_BITE = BITS_NUM_OF_PER_BITE * TWO_BITE;
84 static const uint32_t BITS_NUM_OF_THREE_BITE = BITS_NUM_OF_PER_BITE * THREE_BITE;
85 static const uint32_t BITS_NUM_OF_FOUR_BITE = BITS_NUM_OF_PER_BITE * FOUR_BITE;
86 static const uint32_t BITS_NUM_OF_FIVE_BITE = BITS_NUM_OF_PER_BITE * FIVE_BITE;
87 static const uint32_t BITS_NUM_OF_SIX_BITE = BITS_NUM_OF_PER_BITE * SIX_BITE;
88 static const uint32_t BITS_NUM_OF_SEVEN_BITE = BITS_NUM_OF_PER_BITE * SEVEN_BITE;
89 
90 /*
91  * convert u16 data from Big Endian to Little Endian
92  * dataIn: pointer to start of u16 data
93  * dataOut: the converted u16 data
94  */
Convert16DataBig2Little(const uint8_t * dataIn,uint16_t * dataOut)95 static inline void Convert16DataBig2Little(const uint8_t *dataIn, uint16_t *dataOut)
96 {
97     *dataOut  = ((uint16_t)(*dataIn++) << BITS_NUM_OF_PER_BITE);
98     *dataOut |=  (uint16_t)(*dataIn);
99 }
100 
101 /*
102  * convert u32 data from Big Endian to Little Endian
103  * dataIn: pointer to start of u32 data
104  * dataOut: the converted u32 data
105  */
Convert32DataBig2Little(const uint8_t * dataIn,uint32_t * dataOut)106 static inline void Convert32DataBig2Little(const uint8_t *dataIn, uint32_t *dataOut)
107 {
108     *dataOut  = ((uint32_t)(*dataIn++) << BITS_NUM_OF_THREE_BITE);
109     *dataOut |= ((uint32_t)(*dataIn++) << BITS_NUM_OF_TWO_BITE);
110     *dataOut |= ((uint32_t)(*dataIn++) << BITS_NUM_OF_PER_BITE);
111     *dataOut |=  (uint32_t)(*dataIn);
112 }
113 
114 /*
115  * convert u64 data from Big Endian to Little Endian
116  * dataIn: pointer to start of u64 data
117  * dataOut: the converted u64 data
118  */
Convert64DataBig2Little(const uint8_t * dataIn,uint64_t * dataOut)119 static inline void Convert64DataBig2Little(const uint8_t *dataIn, uint64_t *dataOut)
120 {
121     *dataOut  = ((uint64_t)(*dataIn++) << BITS_NUM_OF_SEVEN_BITE);
122     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_SIX_BITE);
123     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_FIVE_BITE);
124     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_FOUR_BITE);
125     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_THREE_BITE);
126     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_TWO_BITE);
127     *dataOut |= ((uint64_t)(*dataIn++) << BITS_NUM_OF_PER_BITE);
128     *dataOut |=  (uint64_t)(*dataIn);
129 }
130 
131 /*
132  * convert u16 data from Little Endian to Big Endian
133  * dataIn: pointer of the u16 data
134  * dataOut: the converted u16 data
135  */
Convert16DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)136 static inline void Convert16DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
137 {
138     *dataOut++ = *(dataIn + ONE_BITE);
139     *dataOut   = *(dataIn);
140 }
141 
142 /*
143  * convert u32 data from Little Endian to Big Endian
144  * dataIn: pointer of the u32 data
145  * dataOut: the converted u32 data
146  */
Convert32DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)147 static inline void Convert32DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
148 {
149     *dataOut++ = *(dataIn + THREE_BITE);
150     *dataOut++ = *(dataIn + TWO_BITE);
151     *dataOut++ = *(dataIn + ONE_BITE);
152     *dataOut   = *(dataIn);
153 }
154 
155 /*
156  * convert u64 data from Little Endian to Big Endian
157  * dataIn: pointer of the u64 data
158  * dataOut: the converted u64 data
159  */
Convert64DataLittle2Big(const uint8_t * dataIn,uint8_t * dataOut)160 static inline void Convert64DataLittle2Big(const uint8_t *dataIn, uint8_t *dataOut)
161 {
162     *dataOut++ = *(dataIn + SEVEN_BITE);
163     *dataOut++ = *(dataIn + SIX_BITE);
164     *dataOut++ = *(dataIn + FIVE_BITE);
165     *dataOut++ = *(dataIn + FOUR_BITE);
166     *dataOut++ = *(dataIn + THREE_BITE);
167     *dataOut++ = *(dataIn + TWO_BITE);
168     *dataOut++ = *(dataIn + ONE_BITE);
169     *dataOut   = *(dataIn);
170 }
171 #endif // OHOS_DISTRIBUTEDSCHEDULE_DMSLITE_UTILS_H