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 SOFTBUS_UTILS_H
17 #define SOFTBUS_UTILS_H
18 
19 #include "softbus_def.h"
20 
21 #ifdef __cplusplus
22 #if __cplusplus
23 extern "C" {
24 #endif
25 #endif
26 
27 #define HEXIFY_UNIT_LEN 2
28 #define UN_HEXIFY_LEN(len) ((len) / HEXIFY_UNIT_LEN)
29 #define HEXIFY_LEN(len) ((len) * HEXIFY_UNIT_LEN + 1)
30 #define HEX_MAX_NUM 16
31 #define DEC_MAX_NUM 10
32 #define HEX_MAX_BIT_NUM 4
33 #define MAX_QUERY_LEN 64
34 #define INT64_TO_STR_MAX_LEN 21
35 
36 #define TIMER_TIMEOUT 1000 // 1s
37 #define BT_MAC_NO_COLON_LEN 13
38 
39 #define MAC_DELIMITER ':'
40 #define IP_DELIMITER '.'
41 #define ID_DELIMITER ' '
42 
43 typedef void (*TimerFunCallback)(void);
44 
45 typedef enum {
46     SOFTBUS_CONN_TIMER_FUN,
47     SOFTBUS_AUTHEN_TIMER_FUN,
48     SOFTBUS_SESSION_TIMER_FUN,
49     SOFTBUS_PROXYCHANNEL_TIMER_FUN,
50     SOFTBUS_PROXYSLICE_TIMER_FUN,
51     SOFTBUS_TCP_DIRECTCHANNEL_TIMER_FUN,
52     SOFTBUS_UDP_CHANNEL_TIMER_FUN,
53     SOFTBUS_TIME_SYNC_TIMER_FUN,
54     SOFTBUS_PROXY_SENDFILE_TIMER_FUN,
55     SOFTBUS_NIP_NODE_AGING_TIMER_FUN,
56     SOFTBUS_TRNAS_IDLE_TIMEOUT_TIMER_FUN,
57     SOFTBUS_TRNAS_REQUEST_TIMEOUT_TIMER_FUN,
58     SOFTBUS_MAX_TIMER_FUN_NUM
59 } SoftBusTimerFunEnum;
60 
61 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback);
62 
63 int32_t SoftBusTimerInit(void);
64 
65 void SoftBusTimerDeInit(void);
66 SoftBusList *CreateSoftBusList(void);
67 
68 void DestroySoftBusList(SoftBusList *list);
69 
70 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf, uint32_t inLen);
71 
72 int32_t ConvertBtMacToStrNoColon(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
73     uint32_t binMacLen);
74 
75 int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf, uint32_t inLen);
76 
77 int32_t ConvertBytesToUpperCaseHexString(char *outBuf, uint32_t outBufLen,
78     const unsigned char *inBuf, uint32_t inLen);
79 
80 int32_t GenerateRandomStr(char *str, uint32_t size);
81 
82 bool IsValidString(const char *input, uint32_t maxLen);
83 
84 int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac, uint32_t binMacLen);
85 
86 int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen);
87 
88 int32_t ConvertReverseBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen);
89 
90 int32_t ConvertBtMacToU64(const char *strMac, uint32_t strMacLen, uint64_t *u64Mac);
91 
92 int32_t ConvertU64MacToStr(uint64_t u64Mac, char *strMac, uint32_t strMacLen);
93 
94 bool Int64ToString(int64_t src, char *buf, uint32_t bufLen);
95 int32_t StrCmpIgnoreCase(const char *str1, const char *str2);
96 
97 int32_t StringToUpperCase(const char *str, char *buf, int32_t size);
98 
99 int32_t StringToLowerCase(const char *str, char *buf, int32_t size);
100 
101 void SetSignalingMsgSwitchOn(void);
102 void SetSignalingMsgSwitchOff(void);
103 bool GetSignalingMsgSwitch(void);
104 
105 /**
106  * @brief Intercept signaling messages of specified length and print.
107  * @param[in] distinguish Distinguish the sending and receiving of ble, COAP and P2P signaling messages.
108  * @param[in] data Signaling message.
109  * @param[in] dataLen Length of the signaling message.
110  * @param[in] module softbus log module.
111  */
112 void SignalingMsgPrint(const char *distinguish, unsigned char *data, unsigned char dataLen, uint32_t module);
113 
114 void DataMasking(const char *data, uint32_t length, char delimiter, char *container);
115 int32_t GenerateStrHashAndConvertToHexString(const unsigned char *str, uint32_t len, unsigned char *hashStr,
116     uint32_t hashStrLen);
117 #ifdef __cplusplus
118 #if __cplusplus
119 }
120 #endif /* __cplusplus */
121 #endif /* __cplusplus */
122 
123 #endif /* SOFTBUS_UTILS_H */