• 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 <memory>
20 #include <string>
21 #include <unordered_set>
22 #include <shared_mutex>
23 
24 #include "device_type.h"
25 #include "event_bus.h"
26 #include "single_instance.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 class DHContext {
31 DECLARE_SINGLE_INSTANCE_BASE(DHContext);
32 public:
33     DHContext();
34     ~DHContext();
35     std::shared_ptr<EventBus> GetEventBus();
36     const DeviceInfo& GetDeviceInfo();
37 
38     /* Save online device UUID and networkId when devices online */
39     void AddOnlineDevice(const std::string &uuid, const std::string &networkId);
40     void RemoveOnlineDevice(const std::string &uuid);
41     bool IsDeviceOnline(const std::string &uuid);
42     size_t GetOnlineCount();
43     std::string GetNetworkIdByUUID(const std::string &uuid);
44     std::string GetUUIDByNetworkId(const std::string &networkId);
45 
46     /* DeviceId is which is hashed by sha256 */
47     std::string GetUUIDByDeviceId(const std::string &deviceId);
48 
49 private:
50     std::shared_ptr<EventBus> eventBus_;
51     DeviceInfo devInfo_ { "", "", "", 0 };
52     std::mutex devMutex_;
53 
54     /* Save online device uuid and networkId */
55     std::unordered_map<std::string, std::string> onlineDeviceMap_;
56 
57     /* Save online device hashed uuid and uuid */
58     std::unordered_map<std::string, std::string> deviceIdUUIDMap_;
59     std::shared_mutex onlineDevMutex_;
60 };
61 }
62 }
63 #endif