• 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 NETWORKSHARE_SUB_STATEMACHINE_H
17 #define NETWORKSHARE_SUB_STATEMACHINE_H
18 
19 #include <any>
20 #include <cstring>
21 #include <map>
22 #include <mutex>
23 #include <set>
24 #include <sstream>
25 
26 #include "dhcp_service_api.h"
27 #include "net_manager_constants.h"
28 #include "net_manager_ext_constants.h"
29 #include "networkshare_configuration.h"
30 #include "networkshare_constants.h"
31 #include "networkshare_hisysevent.h"
32 #include "networkshare_state_common.h"
33 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 class NetworkShareSubStateMachine : public std::enable_shared_from_this<NetworkShareSubStateMachine> {
37     using HandleFunc = int (NetworkShareSubStateMachine::*)(const std::any &messageObj);
38 
39 public:
40     NetworkShareSubStateMachine() = delete;
41     NetworkShareSubStateMachine(const std::string &ifaceName, const SharingIfaceType &interfaceType,
42                                 const std::shared_ptr<NetworkShareConfiguration> &configuration);
43     ~NetworkShareSubStateMachine() = default;
44 
45     /**
46      * get sub state machine share type
47      */
48     SharingIfaceType GetNetShareType() const;
49 
50     /**
51      * get sub state machine interface name
52      */
53     const std::string &GetInterfaceName() const;
54 
55     class SubStateMachineCallback {
56     public:
57         virtual void OnUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &paraSubStateMachine,
58                                             int state, int lastError) = 0;
59     };
60 
61     /**
62      * register callback
63      */
64     void RegisterSubSMCallback(const std::shared_ptr<SubStateMachineCallback> &callback);
65 
66     /**
67      * execute state switch
68      */
69     void SubSmStateSwitch(int newState);
70 
71     /**
72      * execute event
73      */
74     void SubSmEventHandle(int eventId, const std::any &messageObj);
75 
76     /**
77      * get down interface name
78      */
79     void GetDownIfaceName(std::string &downIface);
80 
81     /**
82      * get up interface name
83      */
84     void GetUpIfaceName(std::string &upIface);
85 
86 private:
87     void CreateInitStateTable();
88     void CreateSharedStateTable();
89     void InitStateEnter();
90     void SharedStateEnter();
91     void UnavailableStateEnter();
92     void InitStateExit();
93     void SharedStateExit();
94     void UnavailableStateExit();
95     int HandleInitSharingRequest(const std::any &messageObj);
96     int HandleInitInterfaceDown(const std::any &messageObj);
97     int HandleSharedUnrequest(const std::any &messageObj);
98     int HandleSharedInterfaceDown(const std::any &messageObj);
99     int HandleSharedConnectionChange(const std::any &messageObj);
100     int HandleSharedErrors(const std::any &messageObj);
101 
102     bool ConfigureShareDhcp(bool enabled);
103     bool RequestIpv4Address(std::shared_ptr<INetAddr> &netAddr);
104     bool StartDhcp(const std::shared_ptr<INetAddr> &netAddr);
105     bool StopDhcp();
106     void HandleConnectionChanged(const std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo);
107     void HandleConnection();
108     void RemoveRoutesToLocalNetwork();
109     void AddRoutesToLocalNetwork();
110     void CleanupUpstreamInterface();
111     bool HasChangeUpstreamIfaceSet(const std::string &newUpstreamIface);
112     bool GetWifiHotspotDhcpFlag();
113     bool GetBtDestinationAddr(std::string &addrStr);
114     bool GetWifiApDestinationAddr(std::string &addrStr);
115     bool GetUsbDestinationAddr(std::string &addrStr);
116     bool CheckConfig(std::string &endIp, std::string &mask);
117     bool FindDestinationAddr(std::string &destination);
118 
119 private:
120     struct SubSmStateTable {
121         int32_t event_;
122         int32_t curState_;
123         HandleFunc func_;
124         int32_t nextState_;
125     };
126     std::recursive_mutex mutex_;
127     std::string ifaceName_;
128     SharingIfaceType netShareType_;
129     int32_t lastError_ = NETMANAGER_EXT_SUCCESS;
130     std::string upstreamIfaceName_;
131     std::shared_ptr<SubStateMachineCallback> trackerCallback_ = nullptr;
132     std::unique_ptr<OHOS::Wifi::IDhcpService> dhcpService_ = nullptr;
133     std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr;
134     int32_t curState_ = SUBSTATE_INIT;
135     std::vector<SubSmStateTable> stateTable_;
136 };
137 } // namespace NetManagerStandard
138 } // namespace OHOS
139 #endif // NETWORKSHARE_SUB_STATEMACHINE_H
140