1 /* 2 * Copyright (c) 2024-2025 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 OHOS_DSCHED_COLLAB_MANAGER_H 17 #define OHOS_DSCHED_COLLAB_MANAGER_H 18 19 #include <atomic> 20 #include <map> 21 #include <string> 22 23 #include "dsched_collab.h" 24 #include "dsched_data_buffer.h" 25 #include "idata_listener.h" 26 #include "inner_socket.h" 27 #include "iremote_object.h" 28 #include "single_instance.h" 29 #include "want.h" 30 31 namespace OHOS { 32 namespace DistributedSchedule { 33 namespace { 34 constexpr int32_t COLLAB_TIMEOUT = 20000; 35 constexpr int32_t BACKGROUND_TIMEOUT = 5000; 36 } 37 38 typedef enum { 39 ACCEPT = 0, 40 REJECT = 1, 41 ON_COLLABORATE_ERR = 10, 42 } CollaborateResult; 43 44 class DSchedCollabManager { 45 DECLARE_SINGLE_INSTANCE_BASE(DSchedCollabManager); 46 public: 47 explicit DSchedCollabManager(); 48 ~DSchedCollabManager(); 49 50 int32_t GetSinkCollabVersion(DSchedCollabInfo &info); 51 int32_t CollabMission(DSchedCollabInfo &info); 52 int32_t NotifyStartAbilityResult(const std::string& collabToken, const int32_t &result, 53 const int32_t &sinkPid, const int32_t &sinkUid, const int32_t &sinkAccessTokenId); 54 int32_t NotifySinkPrepareResult(const std::string &collabToken, const int32_t &result, 55 const int32_t &collabSessionId, const std::string &socketName, const sptr<IRemoteObject> &clientCB); 56 int32_t NotifySinkRejectReason(const std::string& collabToken, const std::string& reason); 57 int32_t NotifyAbilityDied(const std::string &bundleName, const int32_t &pid); 58 int32_t NotifySessionClose(const std::string &collabToken); 59 int32_t CleanUpSession(const std::string &collabToken); 60 int32_t CheckCollabRelation(const CollabInfo *sourceInfo, const CollabInfo *sinkInfo); 61 int32_t ReleaseAbilityLink(const std::string &bundleName, const int32_t &pid); 62 int32_t CancleReleaseAbilityLink(const std::string &bundleName, const int32_t &pid); 63 void NotifyWifiOpen(); 64 bool GetWifiStatus(); 65 66 void Init(); 67 void UnInit(); 68 void NotifyAllConnectDecision(std::string peerDeviceId, bool isSupport); 69 void OnDataRecv(int32_t softbusSessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer); 70 void OnShutdown(int32_t socket, bool isSelfCalled); 71 72 std::shared_ptr<DSchedCollab> GetDSchedCollabByTokenId(const std::string &tokenId); 73 74 private: 75 void StartEvent(); 76 void HandleGetSinkCollabVersion(const DSchedCollabInfo &info); 77 void HandleCollabPrepareResult(const std::string &collabToken, const int32_t &result, 78 const int32_t &collabSessionId, const std::string &socketName, const sptr<IRemoteObject> &clientCB); 79 int32_t HandleCloseSessions(const std::string &bundleName, const int32_t &pid); 80 void HandleReleaseAbilityLink(const std::string &bundleName, const int32_t &pid, const std::string &collabToken); 81 void HandleDataRecv(const int32_t &softbusSessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer); 82 void NotifyDataRecv(const int32_t &softbusSessionId, int32_t command, const std::string& jsonStr, 83 std::shared_ptr<DSchedDataBuffer> dataBuffer, const std::string& collabToken); 84 void WaitAllConnectDecision(const std::string &peerDeviceId, const std::shared_ptr<DSchedCollab> &dCollab); 85 void SetTimeOut(const std::string &collabToken, int32_t timeout); 86 void RemoveTimeout(const std::string &collabToken); 87 bool IsSessionExists(const DSchedCollabInfo &info); 88 std::string GenerateCollabToken(const std::string &sourceDeviceId); 89 int32_t CheckSrcCollabRelation(const CollabInfo *sourceInfo, const DSchedCollabInfo *collabInfo); 90 int32_t CheckSinkCollabRelation(const CollabInfo *sinkInfo, const DSchedCollabInfo *collabInfo); 91 int32_t ConvertCollaborateResult(int32_t result); 92 93 class SoftbusListener : public IDataListener { 94 void OnBind(int32_t socket, PeerSocketInfo info); 95 void OnShutdown(int32_t socket, bool isSelfCalled); 96 void OnDataRecv(int32_t socket, std::shared_ptr<DSchedDataBuffer> dataBuffer); 97 }; 98 99 private: 100 std::thread eventThread_; 101 std::condition_variable eventCon_; 102 std::mutex eventMutex_; 103 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 104 std::shared_ptr<DSchedCollabManager::SoftbusListener> softbusListener_; 105 std::map<std::string, std::shared_ptr<DSchedCollab>> collabs_; 106 std::mutex collabMutex_; 107 108 #ifdef DMSFWK_ALL_CONNECT_MGR 109 std::mutex connectDecisionMutex_; 110 std::condition_variable connectDecisionCond_; 111 std::map<std::string, std::atomic<bool>> peerConnectDecision_; 112 static constexpr int32_t CONNECT_DECISION_TIME_OUT = 10; 113 #endif 114 }; 115 } // namespace DistributedSchedule 116 } // namespace OHOS 117 #endif // OHOS_DSCHED_COLLAB_MANAGER_H