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 "data_level_inner.h" 20 #include "softbus_common.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 #define HB_INVALID_TYPE_ID (-1) 27 #define HB_SHORT_UUID_LEN 2 28 #define USERID_CHECKSUM_LEN 4 29 #define USERID_LEN 4 30 #define HB_ADV_POWER_LEN 1 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_SEND_DIRECT_LEN_ONCE (5 * HB_TIME_FACTOR) 55 #define HB_OFFLINE_PERIOD 2 56 57 #define HB_SEND_EACH_SEPARATELY_LEN (2 * HB_TIME_FACTOR) // Split and send a single heartbeat 58 #define HB_SEND_SEPARATELY_CNT (HB_SEND_ONCE_LEN / HB_SEND_EACH_SEPARATELY_LEN) 59 60 #define HB_MAX_TYPE_COUNT 6 61 #define HB_MULTI_DEVICE_THRESHOLD 8 62 63 // heartbeat type 64 typedef uint32_t LnnHeartbeatType; 65 #define HEARTBEAT_TYPE_MIN (0x1L) 66 #define HEARTBEAT_TYPE_UDP HEARTBEAT_TYPE_MIN 67 #define HEARTBEAT_TYPE_BLE_V0 (0x1L << 1) 68 #define HEARTBEAT_TYPE_BLE_V1 (0x1L << 2) 69 #define HEARTBEAT_TYPE_TCP_FLUSH (0x1L << 3) 70 #define HEARTBEAT_TYPE_BLE_V3 (0x1L << 4) 71 #define HEARTBEAT_TYPE_BLE_V4 (0x1L << 5) // for heartbeat to lowpower 72 #define HEARTBEAT_TYPE_MAX (0x1L << 6) 73 #define HEARTBEAT_TYPE_INVALID 0xFFFF 74 75 #define NORMAL_STRATEGY 1 76 #define HIGH_PERFORMANCE_STRATEGY 2 77 #define ONCE_STRATEGY 3 78 #define SUSPEND_STRATEGY 4 79 #define LOW_CONTINUOUS_ADVERTISE 7 80 #define ADJUST_INTERVAL_STRATEGY 8 81 #define REQUEST_DISABLE_BLE_DISCOVERY 100 82 #define REQUEST_ENABLE_BLE_DISCOVERY 101 83 #define SAME_ACCOUNT_REQUEST_DISABLE_BLE_DISCOVERY 102 84 #define SAME_ACCOUNT_REQUEST_ENABLE_BLE_DISCOVERY 103 85 86 #define MIN_DISABLE_BLE_DISCOVERY_TIME 1000 87 #define MAX_DISABLE_BLE_DISCOVERY_TIME 15000 88 89 #define BT_ADDR_LEN 6 90 #define BT_MAC_HASH_LEN 8 91 #define BT_MAC_HASH_STR_LEN 17 92 93 #define CHECK_TRUSTED_RELATION_TIME 5000 94 95 #define HB_ADV_RANDOM_TIME_50 50 96 #define HB_ADV_RANDOM_TIME_100 100 97 #define HB_ADV_RANDOM_TIME_200 200 98 #define HB_ADV_RANDOM_TIME_300 300 99 #define HB_ADV_RANDOM_TIME_500 500 100 #define HB_ADV_RANDOM_TIME_600 600 101 #define HB_ADV_RANDOM_TIME_1000 1000 102 103 #define HB_USER_SWITCH_CALLER_ID "HEARTBEAT_USER_SWITCH_CALLER_ID" 104 105 typedef enum { 106 STRATEGY_HB_SEND_SINGLE = 0, 107 STRATEGY_HB_SEND_FIXED_PERIOD, 108 STRATEGY_HB_SEND_ADJUSTABLE_PERIOD, 109 STRATEGY_HB_RECV_SINGLE = 3, 110 STRATEGY_HB_RECV_REMOVE_REPEAT, 111 STRATEGY_HB_SEND_DIRECT, 112 } LnnHeartbeatStrategyType; 113 114 typedef enum { 115 UPDATE_HB_INFO_MIN = 0, 116 UPDATE_HB_ACCOUNT_INFO, 117 UPDATE_HB_NETWORK_INFO, 118 UPDATE_SCREEN_STATE_INFO, 119 UPDATE_BT_STATE_OPEN_INFO, 120 UPDATE_BT_STATE_CLOSE_INFO, 121 UPDATE_BR_TURN_ON_INFO, 122 UPDATE_HB_MAX_INFO, 123 } LnnHeartbeatUpdateInfoType; 124 125 typedef struct { 126 uint8_t capabiltiy; 127 uint8_t shortUuid[HB_SHORT_UUID_LEN]; 128 uint8_t userIdCheckSum[USERID_CHECKSUM_LEN]; 129 uint8_t advUserId[USERID_LEN]; 130 uint8_t hbVersion; 131 bool isScreenOn; 132 int16_t stateVersion; 133 uint16_t staticLength; 134 uint16_t staticLevel; 135 uint16_t switchLength; 136 uint32_t switchLevel; 137 int32_t preferChannel; 138 int8_t advPower; 139 } HbRespData; 140 141 typedef enum { 142 BIT_SUPPORT_DIRECT_TRIGGER = 0, 143 BIT_SUPPORT_SCREEN_STATUS = 1, 144 } HeartbeatCapability; 145 146 #define STATE_VERSION_INVALID (-1) 147 #define ENABLE_COC_CAP (1 << 0) 148 #define P2P_GO (1 << 1) 149 #define P2P_GC (1 << 2) 150 #define ENABLE_WIFI_CAP (1 << 3) 151 #define DISABLE_BR_CAP (1 << 4) 152 #define BLE_TRIGGER_HML (1 << 5) 153 154 typedef struct { 155 int32_t (*onDataLevelChanged)(const char *networkId, const DataLevelInfo *dataLevelInfo); 156 } IDataLevelChangeCallback; 157 158 typedef struct { 159 void (*onRangeDone)(const BleRangeInnerInfo *info); 160 } IBleRangeInnerCallback; 161 162 typedef bool (*VisitHbTypeCb)(LnnHeartbeatType *typeSet, LnnHeartbeatType eachType, void *data); 163 bool LnnVisitHbTypeSet(VisitHbTypeCb callback, LnnHeartbeatType *typeSet, void *data); 164 165 LnnHeartbeatType LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType); 166 ConnectionAddrType LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type); 167 int32_t LnnConvertHbTypeToId(LnnHeartbeatType type); 168 bool LnnHasActiveConnection(const char *networkId, ConnectionAddrType addrType); 169 bool LnnCheckSupportedHbType(LnnHeartbeatType *srcType, LnnHeartbeatType *dstType); 170 int32_t LnnGetShortAccountHash(uint8_t *accountHash, uint32_t len); 171 int32_t LnnGenerateHexStringHash(const unsigned char *str, char *hashStr, uint32_t len); 172 int32_t LnnGenerateBtMacHash(const char *btMac, int32_t brMacLen, char *brMacHash, int32_t hashLen); 173 bool LnnIsSupportBurstFeature(const char *networkId); 174 bool LnnIsLocalSupportBurstFeature(void); 175 void LnnDumpLocalBasicInfo(void); 176 void LnnDumpOnlineDeviceInfo(void); 177 uint32_t GenerateRandomNumForHb(uint32_t randMin, uint32_t randMax); 178 bool LnnIsMultiDeviceOnline(void); 179 bool LnnIsSupportHeartbeatCap(uint32_t hbCapacity, HeartbeatCapability capaBit); 180 181 #ifdef __cplusplus 182 } 183 #endif 184 #endif /* LNN_HEARTBEAT_UTILS_H */ 185