1 /*
2  * Copyright (c) 2021-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_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H
17 #define OHOS_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H
18 
19 #include <unordered_set>
20 
21 #include "distributed_device_profile_stub.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "single_instance.h"
25 #include "system_ability.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace DeviceProfile {
30 class DistributedDeviceProfileService : public SystemAbility, public DistributedDeviceProfileStub {
31     DECLARE_SYSTEM_ABILITY(DistributedDeviceProfileService);
32     DECLARE_SINGLE_INSTANCE_BASE(DistributedDeviceProfileService);
33 
34 public:
35     DistributedDeviceProfileService();
36     ~DistributedDeviceProfileService() = default;
37 
38     int32_t PutDeviceProfile(const ServiceCharacteristicProfile& profile) override;
39     int32_t DeleteDeviceProfile(const std::string& serviceId) override;
40     int32_t GetDeviceProfile(const std::string& udid, const std::string& serviceId,
41         ServiceCharacteristicProfile& profile) override;
42     int32_t SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
43         const sptr<IRemoteObject>& profileEventNotifier,
44         std::list<ProfileEvent>& failedEvents) override;
45     int32_t UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
46         const sptr<IRemoteObject>& profileEventNotifier,
47         std::list<ProfileEvent>& failedEvents) override;
48     int32_t SyncDeviceProfile(const SyncOptions& syncOptions,
49         const sptr<IRemoteObject>& profileEventNotifier) override;
50     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
51     void DelayUnloadTask() override;
52     void DeviceOnline();
53 
54 protected:
55     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
56     void OnStop() override;
57     int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override;
58     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
59 
60 private:
61     bool Init();
62     bool DoBusinessInit();
63 private:
64     std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
65     std::mutex unloadMutex_;
66     std::atomic<bool> isOnline_ {false};
67     std::mutex depSaIdsMtx_;
68     std::unordered_set<int32_t> depSaIds_ {
69         SOFTBUS_SERVER_SA_ID,
70         DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID,
71         DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID
72     };
73 };
74 } // namespace DeviceProfile
75 } // namespace OHOS
76 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H