• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "softbus_common.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define HB_INVALID_TYPE_ID (-1)
29 #define HB_SHORT_UDID_HASH_LEN 8
30 #define HB_SHORT_UDID_HASH_HEX_LEN 16
31 #define HB_SHORT_ACCOUNT_HASH_LEN 2
32 #define HB_FSM_NAME_LEN 32
33 
34 #define HB_TIME_FACTOR (1000LL)
35 #define HB_START_DELAY_LEN (10 * HB_TIME_FACTOR)
36 #define HB_SEND_ONCE_LEN (10 * HB_TIME_FACTOR)
37 #define HB_SEND_RELAY_LEN (2 * HB_TIME_FACTOR)
38 #define HB_CHECK_DELAY_LEN HB_SEND_ONCE_LEN
39 #define HB_CHECK_OFFLINE_TOLERANCE_LEN HB_SEND_ONCE_LEN
40 #define HB_NOTIFY_DEV_LOST_DELAY_LEN (2 * HB_TIME_FACTOR + 2 * HB_SEND_ONCE_LEN)
41 #define HB_REPEAD_RECV_THRESHOLD (1 * HB_TIME_FACTOR)
42 #define HB_REPEAD_JOIN_LNN_THRESHOLD (2 * HB_TIME_FACTOR)
43 #define HB_OFFLINE_TIME (5 * 60 * HB_TIME_FACTOR + 2 * HB_SEND_ONCE_LEN)
44 #define HB_SCREEN_ON_COAP_TIME (3 * HB_TIME_FACTOR)
45 #define HB_RESTART_LEN (3 * HB_TIME_FACTOR)
46 #define HB_OFFLINE_PERIOD 2
47 
48 #define HB_SEND_EACH_SEPARATELY_LEN (2 * HB_TIME_FACTOR) // Split and send a single heartbeat
49 #define HB_SEND_SEPARATELY_CNT (HB_SEND_ONCE_LEN / HB_SEND_EACH_SEPARATELY_LEN)
50 
51 #define HB_MAX_TYPE_COUNT 5
52 
53 // heartbeat type
54 #define HEARTBEAT_TYPE_MIN              (0x1L)
55 #define HEARTBEAT_TYPE_UDP              HEARTBEAT_TYPE_MIN
56 #define HEARTBEAT_TYPE_BLE_V0           (0x1L << 1)
57 #define HEARTBEAT_TYPE_BLE_V1           (0x1L << 2)
58 #define HEARTBEAT_TYPE_TCP_FLUSH        (0x1L << 3)
59 #define HEARTBEAT_TYPE_BLE_V3           (0x1L << 4)
60 #define HEARTBEAT_TYPE_MAX              (0x1L << 5)
61 
62 #define NORMAL_STRATEGY 1
63 #define HIGH_PERFORMANCE_STRATEGY 2
64 #define ONCE_STRATEGY 3
65 #define SUSPEND_STRATEGY 4
66 #define LOW_CONTINUOUS_ADVERTISE 7
67 #define ADJUST_INTERVAL_STRATEGY 8
68 #define REQUEST_DISABLE_BLE_DISCOVERY 100
69 #define REQUEST_ENABLE_BLE_DISCOVERY  101
70 
71 #define MIN_DISABLE_BLE_DISCOVERY_TIME 1000
72 #define MAX_DISABLE_BLE_DISCOVERY_TIME 15000
73 
74 #define BT_ADDR_LEN 6
75 #define BT_MAC_HASH_LEN 8
76 #define BT_MAC_HASH_STR_LEN 17
77 
78 #define CHECK_TRUSTED_RELATION_TIME 5000
79 
80 typedef uint32_t LnnHeartbeatType;
81 
82 typedef enum {
83     STRATEGY_HB_SEND_SINGLE = 0,
84     STRATEGY_HB_SEND_FIXED_PERIOD,
85     STRATEGY_HB_SEND_ADJUSTABLE_PERIOD,
86     STRATEGY_HB_RECV_SINGLE = 3,
87     STRATEGY_HB_RECV_REMOVE_REPEAT,
88 } LnnHeartbeatStrategyType;
89 
90 typedef enum {
91     UPDATE_HB_INFO_MIN = 0,
92     UPDATE_HB_ACCOUNT_INFO,
93     UPDATE_HB_NETWORK_INFO,
94     UPDATE_SCREEN_STATE_INFO,
95     UPDATE_BT_STATE_OPEN_INFO,
96     UPDATE_BT_STATE_CLOSE_INFO,
97     UPDATE_HB_MAX_INFO,
98 } LnnHeartbeatUpdateInfoType;
99 
100 typedef struct {
101     uint8_t capabiltiy;
102     int16_t stateVersion;
103 } HbRespData;
104 #define STATE_VERSION_INVALID (-1)
105 #define ENABLE_COC_CAP (1 << 0)
106 #define P2P_GO (1 << 1)
107 #define P2P_GC (1 << 2)
108 #define ENABLE_WIFI_CAP (1 << 3)
109 
110 typedef bool (*VisitHbTypeCb)(LnnHeartbeatType *typeSet, LnnHeartbeatType eachType, void *data);
111 bool LnnVisitHbTypeSet(VisitHbTypeCb callback, LnnHeartbeatType *typeSet, void *data);
112 
113 LnnHeartbeatType LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType);
114 ConnectionAddrType LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type);
115 int32_t LnnConvertHbTypeToId(LnnHeartbeatType type);
116 bool LnnHasActiveConnection(const char *networkId, ConnectionAddrType addrType);
117 bool LnnCheckSupportedHbType(LnnHeartbeatType *srcType, LnnHeartbeatType *dstType);
118 int32_t LnnGenerateHexStringHash(const unsigned char *str, char *hashStr, uint32_t len);
119 int32_t LnnGenerateBtMacHash(const char *btMac, int32_t brMacLen, char *brMacHash, int32_t hashLen);
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 #endif /* LNN_HEARTBEAT_UTILS_H */
125