• 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 OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H
17 #define OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H
18 
19 #include "continuation_manager/notifier_info.h"
20 #include "distributed_ability_manager_stub.h"
21 #include "dms_dumper.h"
22 #include "dms_notifier.h"
23 #include "event_handler.h"
24 #include "single_instance.h"
25 #include "system_ability.h"
26 
27 namespace OHOS {
28 namespace DistributedSchedule {
29 class DistributedAbilityManagerService : public SystemAbility, public DistributedAbilityManagerStub,
30     public DmsNotifier, public DmsDumper {
31     DECLARE_SYSTEM_ABILITY(DistributedAbilityManagerService);
32 
33 public:
34     DistributedAbilityManagerService(int32_t systemAbilityId, bool runOnCreate);
35     ~DistributedAbilityManagerService() = default;
36     void OnStart() override;
37     void OnStop() override;
38     int32_t SendRequestToImpl(uint32_t code, MessageParcel& data, MessageParcel& reply,
39         MessageOption& option) override;
40     bool IsDistributedSchedLoaded() override;
41     void DeviceOnlineNotify(const std::string& deviceId) override;
42     void DeviceOfflineNotify(const std::string& deviceId) override;
43     void ProcessNotifierDied(const sptr<IRemoteObject>& notifier) override;
44     void ScheduleStartDeviceManager(const sptr<IRemoteObject>& appProxy, int32_t token,
45         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr) override;
46     int32_t OnDeviceConnect(int32_t token, const std::vector<ContinuationResult>& continuationResults) override;
47     int32_t OnDeviceDisconnect(int32_t token, const std::vector<ContinuationResult>& continuationResults) override;
48     int32_t OnDeviceCancel() override;
49 
50     int32_t Register(
51         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token) override;
52     int32_t Unregister(int32_t token) override;
53     int32_t RegisterDeviceSelectionCallback(
54         int32_t token, const std::string& cbType, const sptr<IRemoteObject>& notifier) override;
55     int32_t UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType) override;
56     int32_t UpdateConnectStatus(int32_t token, const std::string& deviceId,
57         const DeviceConnectStatus& deviceConnectStatus) override;
58     int32_t StartDeviceManager(
59         int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr) override;
60     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
61     bool ProcessDistributedSchedDump(const std::vector<std::string>& args, std::string& result) override;
62 private:
63     bool IsExceededRegisterMaxNum(uint32_t accessToken);
64     bool IsContinuationModeValid(ContinuationMode continuationMode);
65     bool IsConnectStatusValid(DeviceConnectStatus deviceConnectStatus);
66     bool IsTokenRegistered(uint32_t accessToken, int32_t token);
67     bool IsNotifierRegistered(int32_t token);
68     bool IsNotifierRegisteredLocked(int32_t token, const std::string& cbType);
69     bool QueryTokenByNotifier(const sptr<IRemoteObject>& notifier, int32_t& token);
70     bool HandleDeviceConnect(const sptr<IRemoteObject>& notifier,
71         const std::vector<ContinuationResult>& continuationResults);
72     bool HandleDeviceDisconnect(const sptr<IRemoteObject>& notifier,
73         const std::vector<ContinuationResult>& continuationResults);
74     int32_t ConnectAbility(const sptr<DmsNotifier>& dmsNotifier, int32_t token,
75         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
76     int32_t DisconnectAbility();
77     bool HandleDisconnectAbility();
78     void HandleNotifierDied(const sptr<IRemoteObject>& notifier);
79     void HandleStartDeviceManager(int32_t token,
80         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr);
81     void HandleUpdateConnectStatus(int32_t token, std::string deviceId,
82         const DeviceConnectStatus& deviceConnectStatus);
83 
84     bool InitDmsImplFunc();
85     using LibHandle = void*;
86     LibHandle dmsImplHandle_ = nullptr;
87     bool InitFunc();
88 
89     using OnStartFunc = void(*)();
90     using OnRemoteRequestFunc = int32_t(*)(uint32_t code, MessageParcel& data, MessageParcel& reply,
91         MessageOption& option);
92     using DeviceOnlineNotifyFunc = void(*)(const std::string& deviceId);
93     using DeviceOfflineNotifyFunc = void(*)(const std::string& deviceId);
94     using ConnectAbilityFunc = int32_t(*)(const sptr<DmsNotifier>& dmsNotifier, int32_t token,
95         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
96     using DisconnectAbilityFunc = int32_t(*)();
97     using DistributedSchedDumpFunc = bool(*)(const std::vector<std::string>& args, std::string& result);
98     OnRemoteRequestFunc onRemoteRequestFunc_ = nullptr;
99     DeviceOnlineNotifyFunc deviceOnlineNotifyFunc_ = nullptr;
100     DeviceOfflineNotifyFunc deviceOfflineNotifyFunc_ = nullptr;
101     ConnectAbilityFunc connectAbilityFunc_ = nullptr;
102     DisconnectAbilityFunc disconnectAbilityFunc_ = nullptr;
103     DistributedSchedDumpFunc distributedSchedDumpFunc_ = nullptr;
104 
105     std::atomic_bool isLoaded_ = false;
106     std::mutex libLoadLock_;
107     std::mutex tokenMutex_;
108     std::atomic<int32_t> token_ {0};
109     std::mutex tokenMapMutex_;
110     std::map<uint32_t, std::vector<int32_t>> tokenMap_;
111     std::mutex callbackMapMutex_;
112     std::map<int32_t, std::unique_ptr<NotifierInfo>> callbackMap_;
113     sptr<IRemoteObject::DeathRecipient> notifierDeathRecipient_;
114     std::mutex appProxyMutex_;
115     sptr<IRemoteObject> appProxy_;
116     std::shared_ptr<AppExecFwk::EventHandler> continuationHandler_;
117 };
118 } // namespace DistributedSchedule
119 } // namespace OHOS
120 #endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H
121