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 82 bool IsDeviceConnected(const std::string &deviceId); 83 bool IsDeviceConnecting(const std::string &deviceId); 84 bool IsDeviceUsed(const std::string &deviceId); 85 86 int GetSessionIdByDeviceId(const std::string &deviceId); 87 int GetCastSessionIdByDeviceId(const std::string &deviceId); 88 bool UpdateDeviceByDeviceId(const std::string &deviceId); 89 std::pair<std::string, std::string> GetDeviceNameByDeviceId(const std::string &deviceId); 90 91 bool RemoveDeviceInfo(std::string deviceId, bool isWifi); 92 bool SetDeviceNotFresh(const std::string &deviceId); 93 bool IsDoubleFrameDevice(const std::string &deviceId); 94 95 private: 96 struct DeviceInfoCollection { 97 CastInnerRemoteDevice device; 98 DmDeviceInfo wifiDeviceInfo; 99 DmDeviceInfo bleDeviceInfo; 100 RemoteDeviceState state{ RemoteDeviceState::UNKNOWN }; 101 std::string networkId; 102 int localSessionId{ INVALID_ID }; 103 int transportId{ INVALID_ID }; 104 bool isActiveAuth{ false }; 105 bool isSink{ false }; 106 }; 107 108 CastDeviceDataManager() = default; 109 110 std::vector<DeviceInfoCollection>::iterator GetDeviceLocked(const std::string &deviceId); 111 bool RemoveDeviceLocked(const std::string &deviceId); 112 RemoteDeviceState GetDeviceStateLocked(const std::string &deviceId); 113 bool HasDeviceLocked(const std::string &deviceId); 114 115 std::mutex mutex_; 116 std::vector<DeviceInfoCollection> devices_; 117 }; 118 } // namespace CastEngineService 119 } // namespace CastEngine 120 } // namespace OHOS 121 122 #endif