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 
16 #ifndef OHOS_DSCHED_ALL_CONNECT_MANAGER_H
17 #define OHOS_DSCHED_ALL_CONNECT_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <queue>
22 #include <string>
23 
24 #include "service_collaboration_manager_capi.h"
25 #include "single_instance.h"
26 
27 namespace OHOS {
28 namespace DistributedSchedule {
29 class DSchedAllConnectManager {
30 DECLARE_SINGLE_INSTANCE_BASE(DSchedAllConnectManager);
31 public:
32     int32_t InitAllConnectManager();
33     int32_t UninitAllConnectManager();
34     int32_t PublishServiceState(const std::string &peerNetworkId, const std::string &extraInfo,
35         ServiceCollaborationManagerBussinessStatus state);
36     int32_t ApplyAdvanceResource(const std::string &peerNetworkId,
37         ServiceCollaborationManager_ResourceRequestInfoSets reqInfoSets);
38     void GetResourceRequest(ServiceCollaborationManager_ResourceRequestInfoSets &reqInfoSets);
39 
40 private:
41     DSchedAllConnectManager() = default;
42     ~DSchedAllConnectManager() = default;
43     int32_t GetServiceCollaborationManagerProxy();
44     int32_t RegistLifecycleCallback();
45     int32_t UnregistLifecycleCallback();
46     int32_t WaitAllConnectApplyCb(const std::string &peerNetworkId);
47     void NotifyAllConnectDecision(const std::string &peerNetworkId, bool isSupport);
48 
49     static int32_t OnStop(const char *peerNetworkId);
50     static int32_t ApplyResult(int32_t errorcode, int32_t result, const char *reason);
51 
52 private:
53     static constexpr int32_t DSCHED_QOS_TYPE_MIN_BW = 40 * 1024 * 1024;
54     static constexpr int32_t DSCHED_QOS_TYPE_MAX_LATENCY = 6000;
55     static constexpr int32_t DSCHED_QOS_TYPE_MIN_LATENCY = 1000;
56     static ServiceCollaborationManager_HardwareRequestInfo locReqInfo_;
57     static ServiceCollaborationManager_HardwareRequestInfo rmtReqInfo_;
58     static ServiceCollaborationManager_CommunicationRequestInfo communicationRequest_;
59     static std::queue<std::string> peerConnectCbQueue_;
60     const char *DMS_BIND_MGR_SRV_NAME = "TaskContinue";
61 
62     std::mutex allConnectMgrLock_;
63     void *dllHandle_ = nullptr;
64     ServiceCollaborationManager_API allConnectMgrApi_ = {
65         .ServiceCollaborationManager_PublishServiceState = nullptr,
66         .ServiceCollaborationManager_ApplyAdvancedResource = nullptr,
67         .ServiceCollaborationManager_RegisterLifecycleCallback = nullptr,
68         .ServiceCollaborationManager_UnRegisterLifecycleCallback = nullptr,
69     };
70 
71     std::mutex connectDecisionMutex_;
72     std::condition_variable connectDecisionCond_;
73     std::map<std::string, std::atomic<bool>> peerConnectDecision_;
74 };
75 } // namespace DistributedSchedule
76 } // namespace OHOS
77 #endif // OHOS_DSCHED_ALL_CONNECT_MANAGER_H
78