1 /* 2 * Copyright (c) 2022-2023 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 COORDINATION_SOFTBUS_ADAPTER_H 17 #define COORDINATION_SOFTBUS_ADAPTER_H 18 19 #include <condition_variable> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 25 #include "nocopyable.h" 26 #include "parcel.h" 27 #include "socket.h" 28 29 #include "coordination_util.h" 30 #include "json_parser.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class CoordinationSoftbusAdapter { 36 public: 37 virtual ~CoordinationSoftbusAdapter(); 38 39 int32_t StartRemoteCoordination(const std::string &localNetworkId, 40 const std::string &remoteNetworkId, bool checkButtonDown); 41 int32_t StartRemoteCoordinationResult(const std::string &remoteNetworkId, bool isSuccess, 42 const std::string &startDeviceDhid, int32_t xPercent, int32_t yPercent); 43 int32_t StopRemoteCoordination(const std::string &remoteNetworkId, bool isUnchained); 44 int32_t StopRemoteCoordinationResult(const std::string &remoteNetworkId, bool isSuccess); 45 int32_t StartCoordinationOtherResult(const std::string &originNetworkId, const std::string &remoteNetworkId); 46 int32_t Init(); 47 void Release(); 48 int32_t OpenInputSoftbus(const std::string &remoteNetworkId); 49 void CloseInputSoftbus(const std::string &remoteNetworkId); 50 int32_t OnBind(int32_t socket, PeerSocketInfo info); 51 void OnShutdown(int32_t socket, ShutdownReason reason); 52 void OnBytes(int32_t socket, const void* data, uint32_t dataLen); 53 void RegisterRecvFunc(std::function<void(void*, uint32_t)> callback); 54 int32_t SendData(const std::string &networkId, Parcel &parcel); 55 static std::shared_ptr<CoordinationSoftbusAdapter> GetInstance(); 56 int32_t NotifyUnchainedResult(const std::string &localNetworkId, 57 const std::string &remoteNetworkId, bool isSuccess); 58 int32_t NotifyFilterAdded(const std::string &remoteNetworkId); 59 void ConfigTcpAlive(int32_t socket); 60 61 private: 62 int32_t ChkAndCpyStr(char* dest, uint32_t destLen, const std::string &src); 63 CoordinationSoftbusAdapter() = default; 64 DISALLOW_COPY_AND_MOVE(CoordinationSoftbusAdapter); 65 std::string FindDevice(int32_t socket); 66 int32_t SendMsg(int32_t socket, const std::string &message); 67 bool CheckDeviceSessionState(const std::string &remoteNetworkId); 68 void HandleSessionData(int32_t socket, const std::string &messageData); 69 void HandleCoordinationSessionData(int32_t sessionId, const JsonParser &parser); 70 int32_t WaitSessionOpend(const std::string &remoteNetworkId, int32_t sessionId); 71 void ResponseNotifyFilterAdded(); 72 int32_t InitSocket(SocketInfo info, int32_t socketType, int32_t &socket); 73 74 std::map<std::string, int32_t> sessionDevs_; 75 std::mutex operationMutex_; 76 std::string localSessionName_; 77 std::condition_variable openSessionWaitCond_; 78 std::function<void(void*, uint32_t)> onRecvDataCallback_; 79 int32_t socketFd_ { -1 }; 80 }; 81 } // namespace DeviceStatus 82 } // namespace Msdp 83 } // namespace OHOS 84 #define COOR_SOFTBUS_ADAPTER CoordinationSoftbusAdapter::GetInstance() 85 #endif // COORDINATION_SOFTBUS_ADAPTER_H