• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_HARDWARE_DHCONTEXT_H
17 #define OHOS_DISTRIBUTED_HARDWARE_DHCONTEXT_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <string>
22 #include <unordered_set>
23 #include <shared_mutex>
24 
25 #include "power_mgr_client.h"
26 #include "power_state_callback_stub.h"
27 
28 #include "device_type.h"
29 #include "event_handler.h"
30 #include "single_instance.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 class DHContext {
35 DECLARE_SINGLE_INSTANCE_BASE(DHContext);
36 public:
37     DHContext();
38     ~DHContext();
39     const DeviceInfo& GetDeviceInfo();
40 
41     /* Save online device UUID and networkId when devices online */
42     void AddOnlineDevice(const std::string &uuid, const std::string &networkId);
43     void RemoveOnlineDevice(const std::string &uuid);
44     bool IsDeviceOnline(const std::string &uuid);
45     size_t GetOnlineCount();
46     std::string GetNetworkIdByUUID(const std::string &uuid);
47     std::string GetUUIDByNetworkId(const std::string &networkId);
48 
49     /* DeviceId is which is hashed by sha256 */
50     std::string GetUUIDByDeviceId(const std::string &deviceId);
51 
52     class CommonEventHandler : public AppExecFwk::EventHandler {
53         public:
54             CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
55             ~CommonEventHandler() override = default;
56 
57             bool PostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0);
58             void RemoveTask(const std::string &name);
59     };
60     std::shared_ptr<DHContext::CommonEventHandler> GetEventHandler();
61 
62     bool IsSleeping();
63     void SetIsSleeping(bool isSleeping);
64 
65 private:
66     class DHFWKPowerStateCallback : public OHOS::PowerMgr::PowerStateCallbackStub {
67     public:
68         void OnPowerStateChanged(OHOS::PowerMgr::PowerState state) override;
69     };
70     void RegisterPowerStateLinstener();
71 
72 private:
73     DeviceInfo devInfo_ { "", "", "", 0 };
74     std::mutex devMutex_;
75 
76     /* Save online device uuid and networkId */
77     std::unordered_map<std::string, std::string> onlineDeviceMap_ = {};
78 
79     /* Save online device hashed uuid and uuid */
80     std::unordered_map<std::string, std::string> deviceIdUUIDMap_ = {};
81     std::shared_mutex onlineDevMutex_;
82 
83     std::shared_ptr<DHContext::CommonEventHandler> eventHandler_;
84     /* true for system in sleeping, false for NOT in sleeping */
85     std::atomic<bool> isSleeping_ = false;
86 };
87 } // namespace DistributedHardware
88 } // namespace OHOS
89 #endif
90