• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 LNN_HEARTBEAT_UTILS_H
17 #define LNN_HEARTBEAT_UTILS_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 #include "data_level_inner.h"
23 #include "softbus_common.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define HB_INVALID_TYPE_ID         (-1)
30 #define HB_SHORT_UUID_LEN          2
31 #define HB_SHORT_UDID_HASH_LEN     8
32 #define HB_SHORT_UDID_HASH_HEX_LEN 16
33 #define HB_SHORT_ACCOUNT_HASH_LEN  2
34 #define HB_FSM_NAME_LEN            32
35 
36 #define HB_TIME_FACTOR_TWO_HUNDRED_MS         (200LL)
37 #define HB_TIME_FACTOR                        (1000LL)
38 #define HB_START_DELAY_LEN                    (10 * HB_TIME_FACTOR)
39 #define HB_CLOUD_SYNC_DELAY_LEN               (13 * HB_TIME_FACTOR)
40 #define HB_SEND_ONCE_LEN                      (10 * HB_TIME_FACTOR)
41 #define HB_SEND_RELAY_LEN                     (1 * HB_TIME_FACTOR)
42 #define HB_CHECK_DELAY_LEN                    HB_SEND_ONCE_LEN
43 #define HB_CHECK_OFFLINE_TOLERANCE_LEN        HB_SEND_ONCE_LEN
44 #define HB_NOTIFY_DEV_LOST_DELAY_LEN          (2 * HB_TIME_FACTOR + 2 * HB_SEND_ONCE_LEN)
45 #define HB_NOTIFY_MASTER_NODE_DELAY_LEN       (2 * HB_TIME_FACTOR + HB_SEND_ONCE_LEN)
46 #define HB_REPEAD_RECV_THRESHOLD              (1 * HB_TIME_FACTOR)
47 #define HB_REPEAD_JOIN_LNN_THRESHOLD          (2 * HB_TIME_FACTOR)
48 #define HB_REPEAD_RECV_THRESHOLD_MULTI_DEVICE (3 * HB_TIME_FACTOR)
49 #define HB_OFFLINE_TIME                       (5 * 60 * HB_TIME_FACTOR + 2 * HB_SEND_ONCE_LEN)
50 #define HB_SCREEN_ON_COAP_TIME                (3 * HB_TIME_FACTOR)
51 #define HB_RESTART_LEN                        (3 * HB_TIME_FACTOR)
52 #define HB_PERIOD_DUMP_LOCAL_INFO_LEN         (5 * 60 * HB_TIME_FACTOR)
53 #define HB_SEND_RELAY_LEN_ONCE                (3 * HB_TIME_FACTOR)
54 #define HB_OFFLINE_PERIOD                     2
55 
56 #define HB_SEND_EACH_SEPARATELY_LEN (2 * HB_TIME_FACTOR) // Split and send a single heartbeat
57 #define HB_SEND_SEPARATELY_CNT      (HB_SEND_ONCE_LEN / HB_SEND_EACH_SEPARATELY_LEN)
58 
59 #define HB_MAX_TYPE_COUNT         6
60 #define HB_MULTI_DEVICE_THRESHOLD 8
61 
62 // heartbeat type
63 typedef uint32_t LnnHeartbeatType;
64 #define HEARTBEAT_TYPE_MIN       (0x1L)
65 #define HEARTBEAT_TYPE_UDP       HEARTBEAT_TYPE_MIN
66 #define HEARTBEAT_TYPE_BLE_V0    (0x1L << 1)
67 #define HEARTBEAT_TYPE_BLE_V1    (0x1L << 2)
68 #define HEARTBEAT_TYPE_TCP_FLUSH (0x1L << 3)
69 #define HEARTBEAT_TYPE_BLE_V3    (0x1L << 4)
70 #define HEARTBEAT_TYPE_BLE_V4    (0x1L << 5) // for heartbeat to lowpower
71 #define HEARTBEAT_TYPE_MAX       (0x1L << 6)
72 
73 #define NORMAL_STRATEGY               1
74 #define HIGH_PERFORMANCE_STRATEGY     2
75 #define ONCE_STRATEGY                 3
76 #define SUSPEND_STRATEGY              4
77 #define LOW_CONTINUOUS_ADVERTISE      7
78 #define ADJUST_INTERVAL_STRATEGY      8
79 #define REQUEST_DISABLE_BLE_DISCOVERY 100
80 #define REQUEST_ENABLE_BLE_DISCOVERY  101
81 
82 #define MIN_DISABLE_BLE_DISCOVERY_TIME 1000
83 #define MAX_DISABLE_BLE_DISCOVERY_TIME 15000
84 
85 #define BT_ADDR_LEN         6
86 #define BT_MAC_HASH_LEN     8
87 #define BT_MAC_HASH_STR_LEN 17
88 
89 #define CHECK_TRUSTED_RELATION_TIME 5000
90 
91 #define HB_ADV_RANDOM_TIME_50  50
92 #define HB_ADV_RANDOM_TIME_100 100
93 #define HB_ADV_RANDOM_TIME_200 200
94 #define HB_ADV_RANDOM_TIME_300 300
95 #define HB_ADV_RANDOM_TIME_500 500
96 #define HB_ADV_RANDOM_TIME_600 600
97 #define HB_ADV_RANDOM_TIME_1000 1000
98 
99 typedef enum {
100     STRATEGY_HB_SEND_SINGLE = 0,
101     STRATEGY_HB_SEND_FIXED_PERIOD,
102     STRATEGY_HB_SEND_ADJUSTABLE_PERIOD,
103     STRATEGY_HB_RECV_SINGLE = 3,
104     STRATEGY_HB_RECV_REMOVE_REPEAT,
105 } LnnHeartbeatStrategyType;
106 
107 typedef enum {
108     UPDATE_HB_INFO_MIN = 0,
109     UPDATE_HB_ACCOUNT_INFO,
110     UPDATE_HB_NETWORK_INFO,
111     UPDATE_SCREEN_STATE_INFO,
112     UPDATE_BT_STATE_OPEN_INFO,
113     UPDATE_BT_STATE_CLOSE_INFO,
114     UPDATE_BR_TURN_ON_INFO,
115     UPDATE_HB_MAX_INFO,
116 } LnnHeartbeatUpdateInfoType;
117 
118 typedef struct {
119     uint8_t capabiltiy;
120     int16_t stateVersion;
121     uint16_t staticLength;
122     uint16_t staticLevel;
123     uint16_t switchLength;
124     uint32_t switchLevel;
125     int32_t preferChannel;
126     uint8_t shortUuid[HB_SHORT_UUID_LEN];
127     bool isScreenOn;
128 } HbRespData;
129 
130 typedef enum {
131     BIT_SUPPORT_DIRECT_TRIGGER = 0,
132     BIT_SUPPORT_SCREEN_STATUS = 1,
133 } HeartbeatCapability;
134 
135 #define STATE_VERSION_INVALID (-1)
136 #define ENABLE_COC_CAP        (1 << 0)
137 #define P2P_GO                (1 << 1)
138 #define P2P_GC                (1 << 2)
139 #define ENABLE_WIFI_CAP       (1 << 3)
140 #define DISABLE_BR_CAP        (1 << 4)
141 #define BLE_TRIGGER_HML       (1 << 5)
142 
143 typedef struct {
144     int32_t (*onDataLevelChanged)(const char *networkId, const DataLevelInfo *dataLevelInfo);
145 } IDataLevelChangeCallback;
146 
147 typedef bool (*VisitHbTypeCb)(LnnHeartbeatType *typeSet, LnnHeartbeatType eachType, void *data);
148 bool LnnVisitHbTypeSet(VisitHbTypeCb callback, LnnHeartbeatType *typeSet, void *data);
149 
150 LnnHeartbeatType LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType);
151 ConnectionAddrType LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type);
152 int32_t LnnConvertHbTypeToId(LnnHeartbeatType type);
153 bool LnnHasActiveConnection(const char *networkId, ConnectionAddrType addrType);
154 bool LnnCheckSupportedHbType(LnnHeartbeatType *srcType, LnnHeartbeatType *dstType);
155 int32_t LnnGetShortAccountHash(uint8_t *accountHash, uint32_t len);
156 int32_t LnnGenerateHexStringHash(const unsigned char *str, char *hashStr, uint32_t len);
157 int32_t LnnGenerateBtMacHash(const char *btMac, int32_t brMacLen, char *brMacHash, int32_t hashLen);
158 bool LnnIsSupportBurstFeature(const char *networkId);
159 bool LnnIsLocalSupportBurstFeature(void);
160 void LnnDumpLocalBasicInfo(void);
161 void LnnDumpOnlineDeviceInfo(void);
162 uint32_t GenerateRandomNumForHb(uint32_t randMin, uint32_t randMax);
163 bool LnnIsMultiDeviceOnline(void);
164 bool LnnIsSupportHeartbeatCap(uint32_t hbCapacity, HeartbeatCapability capaBit);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 #endif /* LNN_HEARTBEAT_UTILS_H */
170