• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_WFD_SINK_SCENE_H
17 #define OHOS_SHARING_WFD_SINK_SCENE_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include "agent/agent_def.h"
23 #include "data_ability_observer_stub.h"
24 #include "dm_constants.h"
25 #include "i_wifi_device_callback.h"
26 #include "interaction/device_kit/dm_kit.h"
27 #include "interaction/scene/base_scene.h"
28 #include "system_ability_status_change_stub.h"
29 #include "sink/data_share_helper.h"
30 #include "utils/utils.h"
31 #include "wfd_def.h"
32 #include "wfd_msg.h"
33 #include "sink/wfd_trust_list_manager.h"
34 #include "wifi_device.h"
35 #include "wifi_p2p.h"
36 #include "common/sharing_sink_hisysevent.h"
37 
38 namespace OHOS {
39 namespace Sharing {
40 
41 class WfdSinkScene final : public BaseScene,
42                            public std::enable_shared_from_this<WfdSinkScene> {
43 public:
44     WfdSinkScene();
45     ~WfdSinkScene();
46 
47 public:
48     class WfdP2pCallback : public Wifi::IWifiP2pCallback {
49     public:
AsObject()50         sptr<IRemoteObject> AsObject() override { return nullptr; }
51 
52     public:
WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent)53         explicit WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent) : parent_(parent) {}
54 
55         void OnP2pStateChanged(int32_t state) override;
56         void OnP2pPersistentGroupsChanged(void) override;
57         void OnP2pDiscoveryChanged(bool isChange) override;
58         void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override;
59         void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override;
60         void OnP2pThisDeviceChanged(const Wifi::WifiP2pDevice &device) override;
61         void OnP2pConnectionChanged(const Wifi::WifiP2pLinkedInfo &info) override;
62         void OnConfigChanged(Wifi::CfgType type, char *data, int32_t dataLen) override;
63         void OnP2pPeersChanged(const std::vector<Wifi::WifiP2pDevice> &device) override;
64         void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override;
65         void OnP2pActionResult(Wifi::P2pActionCallback action, Wifi::ErrCode code) override;
66         void OnP2pServicesChanged(const std::vector<Wifi::WifiP2pServiceInfo> &srvInfo) override;
67         void OnP2pChrErrCodeReport(const int errCode) override;
68         bool GetErrorCode(Wifi::ErrCode errorCode, SinkErrorCode &sharingError);
69 
70     private:
71         std::weak_ptr<WfdSinkScene> parent_;
72         WfdTrustListManager wfdTrustListManager_;
73         static std::unordered_map<Wifi::ErrCode, SinkErrorCode> wifiErrorMapping_;
74     };
75 
76     class WifiCallback : public Wifi::IWifiDeviceCallBack {
77     public:
AsObject()78         sptr<IRemoteObject> AsObject() override { return nullptr; }
79 
80     public:
WifiCallback(std::weak_ptr<WfdSinkScene> parent)81         explicit WifiCallback(std::weak_ptr<WfdSinkScene> parent) : parent_(parent) {}
82 
83         void OnWifiStateChanged(int state) override;
84         void OnWifiConnectionChanged(int state, const OHOS::Wifi::WifiLinkedInfo &info) override;
85         void OnWifiRssiChanged(int rssi) override;
86         void OnWifiWpsStateChanged(int state, const std::string &pinCode) override;
87         void OnStreamChanged(int direction) override;
88         void OnDeviceConfigChanged(OHOS::Wifi::ConfigChange value) override;
89 
90     private:
91         std::weak_ptr<WfdSinkScene> parent_;
92     };
93 
94     friend class WfdP2pCallback;
95 
96     class WfdSystemAbilityListener : public SystemAbilityStatusChangeStub {
97     public:
WfdSystemAbilityListener(std::weak_ptr<WfdSinkScene> scene)98         explicit WfdSystemAbilityListener(std::weak_ptr<WfdSinkScene> scene) : scene_(scene) {}
99 
100         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
101         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102 
103     private:
104         std::weak_ptr<WfdSinkScene> scene_;
105     };
106 
107     class DeviceNameObserver : public AAFwk::DataAbilityObserverStub {
108     public:
DeviceNameObserver(std::weak_ptr<WfdSinkScene> scene)109         explicit DeviceNameObserver(std::weak_ptr<WfdSinkScene> scene) : scene_(scene) {}
110         void OnChange() override;
111 
112     private:
113         std::weak_ptr<WfdSinkScene> scene_;
114     };
115 
116 protected:
SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> & adapter)117     void SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> &adapter) final
118     {
119         SHARING_LOGD("trace.");
120         ipcAdapter_ = adapter;
121         auto ipcAdapter = ipcAdapter_.lock();
122         if (ipcAdapter) {
123             ipcAdapter->SetListener(shared_from_this());
124         }
125     }
126 
127 protected:
128     void Release();
129 
130     // impl BaseScene
131     void Initialize() final;
132     void OnRemoteDied() final;
133     void OnInnerEvent(SharingEvent &event) final;
134     void OnInnerError(uint32_t contextId, uint32_t agentId, SharingErrorCode errorCode,
135                       std::string message = "wfd inner error") final;
136     void OnInnerError(std::string deviceId, SharingErrorCode errorCode, std::string message = "wfd inner error");
137     void OnInnerDestroy(uint32_t contextId, uint32_t agentId, AgentType agentType) final;
138 
139     // interactionMgr -> interaction -> scene directly
140     void OnDomainMsg(std::shared_ptr<BaseDomainMsg> &msg) final;
141 
142     // impl IpcMsgAdapterListener
143     void OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) final;
144 
145 private:
146     static void SetWifiScene(uint32_t scene);
147     void InitP2pName();
148     void RegisterP2pListener();
149     void RegisterWifiStatusChangeListener();
150 
151     void WfdP2pStop();
152     void WfdP2pStart();
153 
154     void ErrorCodeFiltering(int32_t &code);
155     void P2pRemoveClient(ConnectionInfo &connectionInfo);
156 
157     void OnP2pPeerDisconnected(const std::string &mac);
158     void OnP2pPeerConnected(ConnectionInfo &connectionInfo);
159     void OnConnectionChanged(ConnectionInfo &connectionInfo);
160     void OnP2pPeerDisconnected(ConnectionInfo &connectionInfo);
161 
162     void OnDecoderDied(ConnectionInfo &connectionInfo);
163     void OnDecoderAccelerationDone(ConnectionInfo &connectionInfo);
164 
165     void OnWifiAbilityResume();
166     void OnWifiAbilityDied();
167     void RegisterWfdAbilityListener();
168     void UnRegisterWfdAbilityListener();
169     void RegisterDevNameObserver();
170     void UnRegisterDevNameObserver();
171 
172     int32_t HandleStop(std::shared_ptr<WfdSinkStopReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
173     int32_t HandleStart(std::shared_ptr<WfdSinkStartReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
174 
175     int32_t HandleSetSceneType(std::shared_ptr<SetSceneTypeReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
176     int32_t HandleSetMediaFormat(std::shared_ptr<SetMediaFormatReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
177     int32_t HandleAppendSurface(std::shared_ptr<WfdAppendSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
178     int32_t HandleRemoveSurface(std::shared_ptr<WfdRemoveSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
179 
180     int32_t HandleMute(std::shared_ptr<MuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
181     int32_t HandlePlay(std::shared_ptr<WfdPlayReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
182     int32_t HandleUnMute(std::shared_ptr<UnMuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
183     int32_t HandlePause(std::shared_ptr<WfdPauseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
184     int32_t HandleClose(std::shared_ptr<WfdCloseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply);
185     int32_t HandleGetConfig(std::shared_ptr<GetSinkConfigReq> &msg, std::shared_ptr<GetSinkConfigRsp> &reply);
186 
187     int32_t HandleGetBoundDevices(std::shared_ptr<WfdGetBoundDevicesReq> &msg,
188                                   std::shared_ptr<WfdGetBoundDevicesRsp> &reply);
189     int32_t HandleDeleteBoundDevice(std::shared_ptr<WfdDeleteBoundDeviceReq> &msg,
190                                     std::shared_ptr<WfdCommonRsp> &reply);
191     void FillAndReportDeviceInfo(const ConnectionInfo &connectionInfo);
192 
193 private:
194     std::atomic_bool isSinkRunning_ = false;
195     std::atomic_bool isInitialized_ = false;
196 
197     int32_t ctrlPort_ = DEFAULT_WFD_CTRLPORT;
198     int32_t surfaceMaximum_ = SURFACE_MAX_NUMBER;
199     int32_t accessDevMaximum_ = ACCESS_DEV_MAX_NUMBER;
200     int32_t foregroundMaximum_ = FOREGROUND_SURFACE_MAX_NUMBER;
201 
202     std::string localIp_;
203     ConnectionInfo currentConnectDev_;
204     std::mutex localIpMutex_;
205     std::mutex currentConnectDevMutex_;
206     std::mutex mutex_;
207     std::shared_ptr<Wifi::WifiP2p> p2pInstance_;
208     std::map<uint64_t, std::shared_ptr<DevSurfaceItem>> devSurfaceItemMap_;
209     std::unordered_map<std::string, std::shared_ptr<ConnectionInfo>> devConnectionMap_;
210 
211     CodecId audioCodecId_ = CodecId::CODEC_AAC;
212     CodecId videoCodecId_ = CodecId::CODEC_H264;
213     AudioFormat audioFormatId_ = AudioFormat::AUDIO_NONE;
214     VideoFormat videoFormatId_ = VideoFormat::VIDEO_NONE;
215     WfdParamsInfo wfdParamsInfo_;
216     WfdTrustListManager wfdTrustListManager_;
217 
218     sptr<AAFwk::IDataAbilityObserver> deviceNameObserver_ = nullptr;
219     sptr<ISystemAbilityStatusChange> sysAbilityListener_ = nullptr;
220 };
221 
222 } // namespace Sharing
223 } // namespace OHOS
224 #endif
225