• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 #define TRANS_CAPABILITY_TLV_OFFSET 0
39 #define TRANS_CHANNEL_INNER_ENCRYPT_OFFSET 1
40 #define TRANS_CHANNEL_INNER_ENCRYPT (1u << TRANS_CHANNEL_INNER_ENCRYPT_OFFSET) /* bit1 */
41 #define TRANS_CHANNEL_CAPABILITY 0x03 /* bit0 & bit1 */
42 
43 #define MAC_DELIMITER ':'
44 #define IP_DELIMITER '.'
45 #define ID_DELIMITER ' '
46 
47 typedef void (*TimerFunCallback)(void);
48 
49 typedef enum {
50     SOFTBUS_CONN_TIMER_FUN,
51     SOFTBUS_AUTHEN_TIMER_FUN,
52     SOFTBUS_SESSION_TIMER_FUN,
53     SOFTBUS_PROXYCHANNEL_TIMER_FUN,
54     SOFTBUS_PROXYSLICE_TIMER_FUN,
55     SOFTBUS_TCP_DIRECTCHANNEL_TIMER_FUN,
56     SOFTBUS_UDP_CHANNEL_TIMER_FUN,
57     SOFTBUS_TIME_SYNC_TIMER_FUN,
58     SOFTBUS_PROXY_SENDFILE_TIMER_FUN,
59     SOFTBUS_NIP_NODE_AGING_TIMER_FUN,
60     SOFTBUS_TRNAS_IDLE_TIMEOUT_TIMER_FUN,
61     SOFTBUS_TRNAS_REQUEST_TIMEOUT_TIMER_FUN,
62     SOFTBUS_TRANS_ASYNC_SENDBYTES_TIMER_FUN,
63     SOFTBUS_DDOS_TIMER_FUN,
64     SOFTBUS_MAX_TIMER_FUN_NUM
65 } SoftBusTimerFunEnum;
66 
67 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback);
68 
69 int32_t SoftBusTimerInit(void);
70 
71 void SoftBusTimerDeInit(void);
72 SoftBusList *CreateSoftBusList(void);
73 
74 void DestroySoftBusList(SoftBusList *list);
75 
76 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf, uint32_t inLen);
77 
78 int32_t ConvertBtMacToStrNoColon(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
79     uint32_t binMacLen);
80 
81 int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf, uint32_t inLen);
82 
83 int32_t ConvertBytesToUpperCaseHexString(char *outBuf, uint32_t outBufLen,
84     const unsigned char *inBuf, uint32_t inLen);
85 
86 int32_t GenerateRandomStr(char *str, uint32_t size);
87 
88 bool IsValidString(const char *input, uint32_t maxLen);
89 
90 int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac, uint32_t binMacLen);
91 
92 int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen);
93 
94 int32_t ConvertReverseBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen);
95 
96 int32_t ConvertBtMacToU64(const char *strMac, uint32_t strMacLen, uint64_t *u64Mac);
97 
98 int32_t ConvertU64MacToStr(uint64_t u64Mac, char *strMac, uint32_t strMacLen);
99 
100 bool Int64ToString(int64_t src, char *buf, uint32_t bufLen);
101 int32_t StrCmpIgnoreCase(const char *str1, const char *str2);
102 
103 int32_t StringToUpperCase(const char *str, char *buf, int32_t size);
104 
105 int32_t StringToLowerCase(const char *str, char *buf, int32_t size);
106 
107 int32_t WriteInt32ToBuf(uint8_t *buf, uint32_t dataLen, int32_t *offSet, int32_t data);
108 
109 int32_t WriteUint8ToBuf(uint8_t *buf, uint32_t dataLen, int32_t *offSet, uint8_t data);
110 
111 int32_t ReadInt32FromBuf(uint8_t *buf, uint32_t dataLen, int32_t *offSet, int32_t *data);
112 
113 int32_t ReadUint8FromBuf(uint8_t *buf, uint32_t dataLen, int32_t *offSet, uint8_t *data);
114 
115 int32_t CalculateMbsTruncateSize(const char *multiByteStr, uint32_t capacity, uint32_t *truncatedSize);
116 
117 void SetSignalingMsgSwitchOn(void);
118 void SetSignalingMsgSwitchOff(void);
119 bool GetSignalingMsgSwitch(void);
120 
121 /**
122  * @brief Intercept signaling messages of specified length and print.
123  * @param[in] distinguish Distinguish the sending and receiving of ble, COAP and P2P signaling messages.
124  * @param[in] data Signaling message.
125  * @param[in] dataLen Length of the signaling message.
126  * @param[in] module softbus log module.
127  */
128 void SignalingMsgPrint(const char *distinguish, unsigned char *data, unsigned char dataLen, uint32_t module);
129 
130 void DataMasking(const char *data, uint32_t length, char delimiter, char *container);
131 int32_t GenerateStrHashAndConvertToHexString(const unsigned char *str, uint32_t len, unsigned char *hashStr,
132     uint32_t hashStrLen);
133 
134 void EnableCapabilityBit(uint32_t *value, uint32_t offSet);
135 
136 bool GetCapabilityBit(uint32_t *value, uint32_t offSet);
137 #ifdef __cplusplus
138 #if __cplusplus
139 }
140 #endif /* __cplusplus */
141 #endif /* __cplusplus */
142 
143 #endif /* SOFTBUS_UTILS_H */