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 #define HEARTBEAT_TYPE_INVALID 0xFFFF 73 74 #define NORMAL_STRATEGY 1 75 #define HIGH_PERFORMANCE_STRATEGY 2 76 #define ONCE_STRATEGY 3 77 #define SUSPEND_STRATEGY 4 78 #define LOW_CONTINUOUS_ADVERTISE 7 79 #define ADJUST_INTERVAL_STRATEGY 8 80 #define REQUEST_DISABLE_BLE_DISCOVERY 100 81 #define REQUEST_ENABLE_BLE_DISCOVERY 101 82 83 #define MIN_DISABLE_BLE_DISCOVERY_TIME 1000 84 #define MAX_DISABLE_BLE_DISCOVERY_TIME 15000 85 86 #define BT_ADDR_LEN 6 87 #define BT_MAC_HASH_LEN 8 88 #define BT_MAC_HASH_STR_LEN 17 89 90 #define CHECK_TRUSTED_RELATION_TIME 5000 91 92 #define HB_ADV_RANDOM_TIME_50 50 93 #define HB_ADV_RANDOM_TIME_100 100 94 #define HB_ADV_RANDOM_TIME_200 200 95 #define HB_ADV_RANDOM_TIME_300 300 96 #define HB_ADV_RANDOM_TIME_500 500 97 #define HB_ADV_RANDOM_TIME_600 600 98 #define HB_ADV_RANDOM_TIME_1000 1000 99 100 typedef enum { 101 STRATEGY_HB_SEND_SINGLE = 0, 102 STRATEGY_HB_SEND_FIXED_PERIOD, 103 STRATEGY_HB_SEND_ADJUSTABLE_PERIOD, 104 STRATEGY_HB_RECV_SINGLE = 3, 105 STRATEGY_HB_RECV_REMOVE_REPEAT, 106 } LnnHeartbeatStrategyType; 107 108 typedef enum { 109 UPDATE_HB_INFO_MIN = 0, 110 UPDATE_HB_ACCOUNT_INFO, 111 UPDATE_HB_NETWORK_INFO, 112 UPDATE_SCREEN_STATE_INFO, 113 UPDATE_BT_STATE_OPEN_INFO, 114 UPDATE_BT_STATE_CLOSE_INFO, 115 UPDATE_BR_TURN_ON_INFO, 116 UPDATE_HB_MAX_INFO, 117 } LnnHeartbeatUpdateInfoType; 118 119 typedef struct { 120 uint8_t capabiltiy; 121 int16_t stateVersion; 122 uint16_t staticLength; 123 uint16_t staticLevel; 124 uint16_t switchLength; 125 uint32_t switchLevel; 126 int32_t preferChannel; 127 uint8_t shortUuid[HB_SHORT_UUID_LEN]; 128 bool isScreenOn; 129 } HbRespData; 130 131 typedef enum { 132 BIT_SUPPORT_DIRECT_TRIGGER = 0, 133 BIT_SUPPORT_SCREEN_STATUS = 1, 134 } HeartbeatCapability; 135 136 #define STATE_VERSION_INVALID (-1) 137 #define ENABLE_COC_CAP (1 << 0) 138 #define P2P_GO (1 << 1) 139 #define P2P_GC (1 << 2) 140 #define ENABLE_WIFI_CAP (1 << 3) 141 #define DISABLE_BR_CAP (1 << 4) 142 #define BLE_TRIGGER_HML (1 << 5) 143 144 typedef struct { 145 int32_t (*onDataLevelChanged)(const char *networkId, const DataLevelInfo *dataLevelInfo); 146 } IDataLevelChangeCallback; 147 148 typedef bool (*VisitHbTypeCb)(LnnHeartbeatType *typeSet, LnnHeartbeatType eachType, void *data); 149 bool LnnVisitHbTypeSet(VisitHbTypeCb callback, LnnHeartbeatType *typeSet, void *data); 150 151 LnnHeartbeatType LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType); 152 ConnectionAddrType LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type); 153 int32_t LnnConvertHbTypeToId(LnnHeartbeatType type); 154 bool LnnHasActiveConnection(const char *networkId, ConnectionAddrType addrType); 155 bool LnnCheckSupportedHbType(LnnHeartbeatType *srcType, LnnHeartbeatType *dstType); 156 int32_t LnnGetShortAccountHash(uint8_t *accountHash, uint32_t len); 157 int32_t LnnGenerateHexStringHash(const unsigned char *str, char *hashStr, uint32_t len); 158 int32_t LnnGenerateBtMacHash(const char *btMac, int32_t brMacLen, char *brMacHash, int32_t hashLen); 159 bool LnnIsSupportBurstFeature(const char *networkId); 160 bool LnnIsLocalSupportBurstFeature(void); 161 void LnnDumpLocalBasicInfo(void); 162 void LnnDumpOnlineDeviceInfo(void); 163 uint32_t GenerateRandomNumForHb(uint32_t randMin, uint32_t randMax); 164 bool LnnIsMultiDeviceOnline(void); 165 bool LnnIsSupportHeartbeatCap(uint32_t hbCapacity, HeartbeatCapability capaBit); 166 167 #ifdef __cplusplus 168 } 169 #endif 170 #endif /* LNN_HEARTBEAT_UTILS_H */ 171