1 /* 2 * Copyright (c) 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 #ifndef P2P_V1_PROCESSOR_H 16 #define P2P_V1_PROCESSOR_H 17 18 #include "common_timer_errors.h" 19 #include "timer.h" 20 21 #include "command/connect_command.h" 22 #include "command/disconnect_command.h" 23 #include "command/force_disconnect_command.h" 24 #include "command/negotiate_command.h" 25 #include "data/inner_link.h" 26 #include "entity/p2p_entity.h" 27 #include "processor/wifi_direct_processor.h" 28 #include "wifi_direct_executor.h" 29 30 namespace OHOS::SoftBus { 31 class P2pV1Processor : public WifiDirectProcessor { 32 public: 33 explicit P2pV1Processor(const std::string &remoteDeviceId); 34 ~P2pV1Processor() override; 35 36 [[noreturn]] void Run() override; 37 38 bool CanAcceptNegotiateDataAtState(WifiDirectCommand &command) override; 39 void HandleCommandAfterTerminate(WifiDirectCommand &command) override; 40 41 private: 42 static constexpr int P2P_VERSION = 2; 43 44 static constexpr int P2P_V1_WAITING_RESPONSE_TIME_MS = 10000; 45 static constexpr int P2P_V1_WAITING_REQUEST_TIME_MS = 10000; 46 static constexpr int P2P_V1_WAITING_AUTH_TIME_MS = 10000; 47 static constexpr int P2P_V1_WAITING_REUSE_RESPONSE_TIME_MS = 2000; 48 static constexpr int DISCONNECT_WAIT_POST_REQUEST_MS = 450; 49 static constexpr int TIMER_TIME = 200; 50 51 struct TimeoutEvent { }; 52 53 static int ErrorCodeToV1ProtocolCode(int reason); 54 static int ErrorCodeFromV1ProtocolCode(int reason); 55 56 using ProcessorState = void (P2pV1Processor::*)(); 57 static std::string GetStateName(ProcessorState state); 58 void SwitchState(ProcessorState state, int timeoutInMillis); 59 void AvailableState(); 60 void WaitingReqResponseState(); 61 void WaitingClientJoiningState(); 62 void WaitAuthHandShakeState(); 63 void WaitingRequestState(); 64 void WaitingReuseResponseState(); 65 66 void ProcessConnectCommand(std::shared_ptr<ConnectCommand> &command); 67 void ProcessDisconnectCommand(std::shared_ptr<DisconnectCommand> &command); 68 void ProcessForceDisconnectCommand(std::shared_ptr<ForceDisconnectCommand> &command); 69 70 void ProcessNegotiateCommandAtAvailableState(std::shared_ptr<NegotiateCommand> &command); 71 void ProcessNegotiateCommandAtWaitingReqResponseState(std::shared_ptr<NegotiateCommand> &command); 72 void ProcessNegotiateCommandAtWaitingRequestState(std::shared_ptr<NegotiateCommand> &command); 73 void ProcessNegotiateCommandAtWaitingReuseResponseState(std::shared_ptr<NegotiateCommand> &command); 74 void ProcessNegotiateCommandAtWaitingAuthHandShakeState(std::shared_ptr<NegotiateCommand> &command); 75 void ProcessNegotiateCommandAtWaitingClientJoiningState(std::shared_ptr<NegotiateCommand> &command); 76 int ProcessNegotiateCommandCommon(std::shared_ptr<NegotiateCommand> &command); 77 78 void ProcessAuthConnEvent(std::shared_ptr<AuthOpenEvent> &event); 79 80 void OnWaitReqResponseTimeoutEvent(); 81 void OnWaitReuseResponseTimeoutEvent(); 82 void OnWaitAuthHandShakeTimeoutEvent(); 83 void OnWaitRequestTimeoutEvent(); 84 int OnClientJoinEvent(std::shared_ptr<ClientJoinEvent> &event); 85 86 int CreateLink(); 87 int CreateLinkAsNone(); 88 int CreateLinkAsGo(); 89 int CreateLinkAsGc(); 90 91 int ProcessConnectRequest(std::shared_ptr<NegotiateCommand> &command); 92 int ProcessConnectRequestAsGo(std::shared_ptr<NegotiateCommand> &command, LinkInfo::LinkMode myRole); 93 int ProcessConnectRequestAsGc(std::shared_ptr<NegotiateCommand> &command, LinkInfo::LinkMode myRole); 94 int ProcessNoAvailableInterface(std::shared_ptr<NegotiateCommand> &command, LinkInfo::LinkMode myRole); 95 96 int ProcessConflictRequest(std::shared_ptr<NegotiateCommand> &command); 97 98 int ProcessReuseRequest(std::shared_ptr<NegotiateCommand> &command); 99 int ProcessReuseResponse(std::shared_ptr<NegotiateCommand> &command); 100 int ProcessDisconnectRequest(std::shared_ptr<NegotiateCommand> &command); 101 int ProcessForceDisconnectRequest(std::shared_ptr<NegotiateCommand> &command); 102 103 int ProcessGetInterfaceInfoRequest(std::shared_ptr<NegotiateCommand> &command); 104 105 int ProcessAuthHandShakeRequest(std::shared_ptr<NegotiateCommand> &command); 106 107 static int SendConnectRequestAsNone(const NegotiateChannel &channel, WifiDirectRole expectedRole); 108 static int SendConnectRequestAsGo(const NegotiateChannel &channel, const std::string &remoteMac); 109 static int SendConnectResponseAsGo(const NegotiateChannel &channel, const std::string &remoteMac); 110 static int SendConnectResponseAsNone(const NegotiateChannel &channel, const std::string &remoteMac); 111 static int SendReuseRequest(const NegotiateChannel &channel); 112 static int SendReuseResponse(const NegotiateChannel &channel, int32_t result); 113 static int SendDisconnectRequest(const NegotiateChannel &channel); 114 static int SendForceDisconnectRequest(const NegotiateChannel &channel); 115 static int SendInterfaceInfoResponse(const NegotiateChannel &channel); 116 static int SendNegotiateResult(const NegotiateChannel &channel, int32_t reason); 117 static int SendHandShakeMessage(const NegotiateChannel &channel); 118 119 int ProcessConnectResponseAtWaitingReqResponseState(std::shared_ptr<NegotiateCommand> &command); 120 int ProcessConnectResponseAtWaitingClientJoiningState(std::shared_ptr<NegotiateCommand> &command); 121 int ProcessConnectResponseAsGo(std::shared_ptr<NegotiateCommand> &command); 122 int ProcessConnectResponseAsNone(std::shared_ptr<NegotiateCommand> &command); 123 int ProcessConnectResponseWithGoInfoAsNone(std::shared_ptr<NegotiateCommand> &command); 124 int ProcessConnectResponseWithGcInfoAsNone(std::shared_ptr<NegotiateCommand> &command); 125 int ProcessConnectResponseAtWaitAuthHandShake(std::shared_ptr<NegotiateCommand> &command); 126 127 int UpdateWhenConnectSuccess(std::string groupConfig, const NegotiateMessage &msg); 128 129 int CreateGroup(const NegotiateMessage &msg); 130 int ConnectGroup(const NegotiateMessage &msg, const std::shared_ptr<NegotiateChannel> &channel); 131 static bool IsNeedDhcp(const std::string &gcIp, const std::string &groupConfig); 132 static int ChooseFrequency(int gcFreq, const std::vector<int> &gcChannels); 133 int DestroyGroup(); 134 135 int ReuseP2p(); 136 int ReuseLink(const std::shared_ptr<ConnectCommand> &command, InnerLink &innerLink); 137 std::string GetGoMac(LinkInfo::LinkMode role); 138 139 int StartAuthListening(const std::string &localIp); 140 int OpenAuthConnection(const NegotiateMessage &msg, const std::shared_ptr<NegotiateChannel> &channel); 141 int RemoveLink(const std::string &remoteDeviceId); 142 143 int GetFinalRoleWithPeerExpectedRole(WifiDirectRole myRole, WifiDirectRole peerRole, WifiDirectRole expectedRole, 144 const std::string &localGoMac, const std::string &remoteGoMac); 145 int GetFinalRoleAsGo(WifiDirectRole peerRole, WifiDirectRole expectedRole, const std::string &localGoMac, 146 const std::string &remoteGoMac); 147 int GetFinalRoleAsGc(WifiDirectRole peerRole, WifiDirectRole expectedRole, const std::string &localGoMac, 148 const std::string &remoteGoMac); 149 int GetFinalRoleAsNone(WifiDirectRole peerRole, WifiDirectRole expectedRole); 150 int GenerateSinkLink(WifiDirectSinkLink &sinkLink); 151 152 void CleanupIfNeed(int32_t ret, const std::string &remoteDeviceId); 153 void Exclusive(const std::string &remoteDeviceId); 154 void RemoveExclusive(); 155 156 void StartTimer(int timeoutInMillis); 157 void StopTimer(); 158 159 [[noreturn]] void Terminate(); 160 161 ProcessorState state_; 162 static std::map<std::string, ProcessorState> stateNameMapping; 163 bool canAcceptNegotiateData_ = true; 164 bool exclusive_ = false; 165 166 std::shared_ptr<ConnectCommand> connectCommand_; 167 std::string clientJoiningMac_; 168 169 bool active_; 170 bool hasRun_ = false; 171 172 Utils::Timer timer_; 173 uint32_t timerId_; 174 }; 175 } // namespace OHOS::SoftBus 176 #endif 177