1 /*
2 * Copyright (C) 2023 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_WIFI_HDI_COMMON_H
17 #define OHOS_WIFI_HDI_COMMON_H
18
19 #include "wifi_hdi_define.h"
20 #include "wifi_hdi_struct.h"
21
22 typedef unsigned char u8;
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #ifndef HDI_BIT
29 #define HDI_BIT(x) (1U << (x))
30 #endif
31
32 #define MAC_UINT_SIZE 6
33 #define MAC_STRING_SIZE 17
34 #define HDI_CHECK_ELEMENT(e, input, len) \
35 for ((e) = (const struct HdiElem *) (input); \
36 (const uint8_t *) (input) + (len) - (const uint8_t *)(e) >= \
37 (int) sizeof(*(e)) && \
38 (const uint8_t *) (input) + (len) - (const uint8_t *)(e) >= \
39 (int) sizeof(*(e)) + (e)->datalen; \
40 (e) = (const struct HdiElem *) ((e)->data + (e)->datalen))
41
42 #define HDI_CHECK_ELEMENT_BY_ID(e, inputid, input, len) \
43 HDI_CHECK_ELEMENT(e, input, len) \
44 if ((e)->id == (inputid))
45
46 #define HDI_CHECK_ELEMENT_BY_EXTID(e, extid, input, len) \
47 HDI_CHECK_ELEMENT(e, input, len) \
48 if ((e)->id == HDI_EID_EXTENSION && \
49 (e)->datalen > 0 && \
50 (e)->data[0] == (extid))
51
52 #define HDI_HANDLE_CIPHER_INFO(ret, start, end, delim, format) \
53 ret = HdiTxtPrintf(pos, (end) - (pos), format, \
54 pos == (start) ? "" : (delim)); \
55 if (HdiCheckError((end) - pos, ret)) \
56 return -1; \
57 pos += ret; \
58
59 #define HDI_HANDLE_CIPHER_POS_INFO(flag, ret, pos, end, delim, format) \
60 if (flag) { \
61 ret = HdiTxtPrintf(pos, (end) - (pos), format, \
62 pos == (start) ? "" : (delim)); \
63 if (HdiCheckError((end) - (pos), ret)) \
64 return pos; \
65 pos += ret; \
66 } \
67
HdiCheckCompleted(const struct HdiElem * e,const uint8_t * data,size_t datalen)68 static inline int HdiCheckCompleted(const struct HdiElem *e,
69 const uint8_t *data, size_t datalen)
70 {
71 return (const uint8_t *)e == data + datalen;
72 }
73
74 #define HDI_GET_RSN_ID(a) HdiGetBe32((const uint8_t *) (a))
75
76 #define HDI_BIT_MULTI(a, b, c, d) \
77 ((((uint32_t) (a)) << 24) | (((uint32_t) (b)) << 16) | (((uint32_t) (c)) << 8) | \
78 (uint32_t) (d))
79
80 const uint8_t* HdiGetIe(const uint8_t *ies, size_t len, uint8_t eid);
81
82 const uint8_t* HdiBssGetIe(const uint8_t *ies, size_t len, uint8_t ie);
83
84 const uint8_t* HdiBssGetVendorIe(const uint8_t *ies, size_t len, uint32_t vendorType);
85
86 const uint8_t* HdiBssGetVendorBeacon(const uint8_t *ies, size_t len, size_t beaconIeLen, uint32_t vendorType);
87
HdiCheckError(size_t size,int res)88 static inline int HdiCheckError(size_t size, int res)
89 {
90 return res < 0 || (unsigned int) res >= size;
91 }
92
HdiGetBe24(const uint8_t * a)93 static inline uint32_t HdiGetBe24(const uint8_t *a)
94 {
95 return (a[HDI_POS_ZERO] << HDI_MOVE_SIXTEEN) | (a[HDI_POS_FIRST] << HDI_MOVE_EIGHT) | a[HDI_POS_SECOND];
96 }
97
HdiGetBe32(const uint8_t * a)98 static inline uint32_t HdiGetBe32(const uint8_t *a)
99 {
100 return ((uint32_t) a[HDI_POS_ZERO] << HDI_MOVE_TF) | (a[HDI_POS_FIRST] << HDI_MOVE_SIXTEEN) |
101 (a[HDI_POS_SECOND] << HDI_MOVE_EIGHT) | a[HDI_POS_THIRD];
102 }
103
HdiGetBe16(const uint8_t * a)104 static inline uint16_t HdiGetBe16(const uint8_t *a)
105 {
106 return (a[HDI_POS_FIRST] << HDI_MOVE_EIGHT) | a[HDI_POS_ZERO];
107 }
108
109 size_t PrintfDecode(u8 *buf, size_t maxlen, const char *str);
110
111 int HdiTxtPrintf(char *str, size_t size, const char *format, ...);
112
113 const uint8_t* HdiBssGetIeExt(const uint8_t *ies, size_t len, uint8_t ext);
114
115 void HdiBufEncode(char *txt, size_t maxlen, const uint8_t *data, size_t len);
116
117 const char* HdiSSid2Txt(const uint8_t *ssid, size_t ssidLen);
118
119 char* HdiGetWifiCategoryTxt(char *pos, char* end, const struct HdiElems *elems);
120
121 char* HdiGetIeTxt(char *pos, char *end, const char *proto,
122 const uint8_t *ie, size_t ieLen);
123
124 int8_t IsValidHexCharAndConvert(char c);
125
126 int CheckMacIsValid(const char *macStr);
127
128 void StrSafeCopy(char *dst, unsigned len, const char *src);
129
130 char* HdiGetWapiTxt(char *pos, char *end, const uint8_t *ie);
131
132 #ifdef __cplusplus
133 }
134 #endif
135 #endif