1 /*
2  * Copyright (c) 2021 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 
17 #ifndef LNN_CONNECTION_FSM_H
18 #define LNN_CONNECTION_FSM_H
19 
20 #include <stdint.h>
21 
22 #include "auth_interface.h"
23 #include "common_list.h"
24 #include "lnn_node_info.h"
25 #include "lnn_state_machine.h"
26 #include "softbus_bus_center.h"
27 #include "softbus_hisysevt_bus_center.h"
28 #include "lnn_net_builder.h"
29 
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif
34 #endif
35 
36 #define LNN_CONNECTION_FSM_NAME_LEN 32
37 
38 #define LNN_CONN_INFO_FLAG_JOIN_REQUEST 0x01
39 #define LNN_CONN_INFO_FLAG_JOIN_AUTO 0x02
40 #define LNN_CONN_INFO_FLAG_JOIN_PASSIVE 0x04
41 #define LNN_CONN_INFO_FLAG_LEAVE_REQUEST 0x08
42 #define LNN_CONN_INFO_FLAG_LEAVE_AUTO 0x10
43 #define LNN_CONN_INFO_FLAG_LEAVE_PASSIVE 0x20
44 #define LNN_CONN_INFO_FLAG_INITIATE_ONLINE 0x40
45 #define LNN_CONN_INFO_FLAG_ONLINE 0x80
46 
47 #define LNN_CONN_INFO_FLAG_JOIN_ACTIVE (LNN_CONN_INFO_FLAG_JOIN_REQUEST | LNN_CONN_INFO_FLAG_JOIN_AUTO)
48 #define LNN_CONN_INFO_FLAG_JOIN (LNN_CONN_INFO_FLAG_JOIN_ACTIVE | LNN_CONN_INFO_FLAG_JOIN_PASSIVE)
49 
50 #define LNN_CONN_INFO_FLAG_LEAVE_ACTIVE (LNN_CONN_INFO_FLAG_LEAVE_REQUEST | LNN_CONN_INFO_FLAG_LEAVE_AUTO)
51 #define LNN_CONN_INFO_FLAG_LEAVE (LNN_CONN_INFO_FLAG_LEAVE_ACTIVE | LNN_CONN_INFO_FLAG_LEAVE_PASSIVE)
52 
53 typedef struct {
54     /* clean invalid addr type */
55     ConnectionAddrType addrType;
56     /* record newer connection networkId */
57     char networkId[NETWORK_ID_BUF_LEN];
58 } LnnInvalidCleanInfo;
59 
60 typedef struct {
61     ConnectionAddr addr;
62     NodeInfo *nodeInfo;
63     NodeInfo *dupInfo;
64     char peerNetworkId[NETWORK_ID_BUF_LEN];
65     LnnInvalidCleanInfo *cleanInfo;
66     AuthHandle authHandle;
67     uint32_t requestId;
68     uint32_t flag;
69     SoftBusVersion version;
70     LnnDfxDeviceInfoReport infoReport;
71 } LnnConntionInfo;
72 
73 struct tagLnnConnectionFsm;
74 
75 typedef void (*LnnConnectionFsmStopCallback)(struct tagLnnConnectionFsm *connFsm);
76 
77 typedef struct tagLnnConnectionFsm {
78     ListNode node;
79     uint16_t id;
80 
81     char fsmName[LNN_CONNECTION_FSM_NAME_LEN];
82     char pkgName[PKG_NAME_SIZE_MAX];
83     FsmStateMachine fsm;
84     LnnConntionInfo connInfo;
85     LnnConnectionFsmStopCallback stopCallback;
86     bool isDead;
87     bool isNeedConnect;
88     LnnStatisticData statisticData;
89 } LnnConnectionFsm;
90 
91 LnnConnectionFsm *LnnCreateConnectionFsm(const ConnectionAddr *target, const char *pkgName, bool isNeedConnect);
92 void LnnDestroyConnectionFsm(LnnConnectionFsm *connFsm);
93 
94 int32_t LnnStartConnectionFsm(LnnConnectionFsm *connFsm);
95 int32_t LnnStopConnectionFsm(LnnConnectionFsm *connFsm, LnnConnectionFsmStopCallback callback);
96 bool LnnIsNeedCleanConnectionFsm(const NodeInfo *nodeInfo, ConnectionAddrType type);
97 
98 int32_t LnnSendJoinRequestToConnFsm(LnnConnectionFsm *connFsm);
99 int32_t LnnSendAuthResultMsgToConnFsm(LnnConnectionFsm *connFsm, int32_t retCode);
100 int32_t LnnSendNotTrustedToConnFsm(LnnConnectionFsm *connFsm);
101 int32_t LnnSendDisconnectMsgToConnFsm(LnnConnectionFsm *connFsm);
102 int32_t LnnSendLeaveRequestToConnFsm(LnnConnectionFsm *connFsm);
103 int32_t LnnSendSyncOfflineFinishToConnFsm(LnnConnectionFsm *connFsm);
104 int32_t LnnSendNewNetworkOnlineToConnFsm(LnnConnectionFsm *connFsm);
105 bool CheckRemoteBasicInfoChanged(const NodeInfo *newNodeInfo);
106 
107 #ifdef __cplusplus
108 #if __cplusplus
109 }
110 #endif /* __cplusplus */
111 #endif /* __cplusplus */
112 
113 #endif /* LNN_CONNECTION_FSM_H */