• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2023 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  * Description: cast device data management
15  * Author: zhangge
16  * Create: 2022-10-15
17  */
18 
19 #ifndef CAST_DEVICE_DATA_MANAGE_H
20 #define CAST_DEVICE_DATA_MANAGE_H
21 
22 #include <memory>
23 #include <mutex>
24 #include <optional>
25 #include <string>
26 #include <vector>
27 
28 #include "cast_service_common.h"
29 #include "device_manager.h"
30 
31 using OHOS::DistributedHardware::DmDeviceInfo;
32 
33 namespace OHOS {
34 namespace CastEngine {
35 namespace CastEngineService {
36 enum class RemoteDeviceState {
37     UNKNOWN,
38     FOUND,
39     CONNECTING,
40     CONNECTED,
41     REMOTE_DEVICE_STATE_MAX
42 };
43 
44 const EXPORT std::array<std::string, static_cast<size_t>(RemoteDeviceState::REMOTE_DEVICE_STATE_MAX)>
45     REMOTE_DEVICE_STATE_STRING = { "UNKNOWN", "FOUND", "CONNECTING", "CONNECTED" };
46 
47 class CastDeviceDataManager final {
48 public:
49     static CastDeviceDataManager &GetInstance();
50 
51     ~CastDeviceDataManager() = default;
52 
53     bool AddDevice(const CastInnerRemoteDevice &device, const DmDeviceInfo &dmDeviceInfo);
54     bool HasDevice(const std::string &deviceId);
55     bool UpdateDevice(const CastInnerRemoteDevice &device);
56     void RemoveDevice(const std::string &deviceId);
57 
58     std::optional<CastInnerRemoteDevice> GetDeviceByDeviceId(const std::string &deviceId);
59     std::optional<CastInnerRemoteDevice> GetDeviceByTransId(int sessionId);
60     std::optional<DmDeviceInfo> GetDmDevice(const std::string &deviceId);
61 
62     bool SetDeviceTransId(const std::string &deviceId, int transportId);
63     int GetDeviceTransId(const std::string &deviceId);
64     int ResetDeviceTransId(const std::string &deviceId);
65 
66     bool SetDeviceRole(const std::string &deviceId, bool isSink);
67     std::optional<bool> GetDeviceRole(const std::string &deviceId);
68 
69     bool SetDeviceNetworkId(const std::string &deviceId, const std::string &networkId);
70     std::optional<std::string> GetDeviceNetworkId(const std::string &deviceId);
71 
72     bool SetDeviceIsActiveAuth(const std::string &deviceId, bool isActiveAuth);
73     std::optional<bool> GetDeviceIsActiveAuth(const std::string &deviceId);
74 
75     bool SetDeviceSessionKey(const std::string &deviceId, const uint8_t* sessionKey);
76     bool SetDeviceIp(const std::string &deviceId, const std::string &localIp, const std::string &remoteIp);
77     bool SetDeviceChannleType(const std::string &deviceId, const ChannelType &channelType);
78 
79     bool SetDeviceState(const std::string &deviceId, RemoteDeviceState state);
80     RemoteDeviceState GetDeviceState(const std::string &deviceId);
81     bool IsDeviceConnected(const std::string &deviceId);
82     bool IsDeviceConnecting(const std::string &deviceId);
83     bool IsDeviceUsed(const std::string &deviceId);
84     int GetSessionIdByDeviceId(const std::string &deviceId);
85     bool UpdateDeivceByDeviceId(const std::string &deviceId);
86     bool IsDoubleFrameDevice(const std::string &deviceId);
87     std::optional<std::string> GetDeviceNameByDeviceId(const std::string &deviceId);
88 
89 private:
90     struct DeviceInfoCollection {
91         CastInnerRemoteDevice device;
92         DmDeviceInfo wifiDeviceInfo;
93         DmDeviceInfo bleDeviceInfo;
94         RemoteDeviceState state{ RemoteDeviceState::UNKNOWN };
95         std::string networkId;
96         int localSessionId{ INVALID_ID };
97         int transportId{ INVALID_ID };
98         bool isActiveAuth{ false };
99         bool isSink{ false };
100     };
101 
102     CastDeviceDataManager() = default;
103 
104     std::vector<DeviceInfoCollection>::iterator GetDeviceLocked(const std::string &deviceId);
105     bool RemoveDeivceLocked(const std::string &deviceId);
106     RemoteDeviceState GetDeviceStateLocked(const std::string &deviceId);
107     bool HasDeviceLocked(const std::string &deviceId);
108 
109     std::mutex mutex_;
110     std::vector<DeviceInfoCollection> devices_;
111 };
112 } // namespace CastEngineService
113 } // namespace CastEngine
114 } // namespace OHOS
115 
116 #endif