1 /*
2  * Copyright (c) 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  */
15 
16 #ifndef OHOS_SOFTBUS_CHANNEL_ADAPTER
17 #define OHOS_SOFTBUS_CHANNEL_ADAPTER
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 
23 #include "transport/socket.h"
24 #include "transport/trans_type.h"
25 #include "av_trans_types.h"
26 #include "single_instance.h"
27 #include "softbus_bus_center.h"
28 #include "softbus_common.h"
29 #include "av_trans_constants.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 class ISoftbusChannelListener {
34 public:
35     virtual ~ISoftbusChannelListener() = default;
36     virtual void OnChannelEvent(const AVTransEvent &event) = 0;
37     virtual void OnStreamReceived(const StreamData *data, const StreamData *ext) = 0;
38 };
39 
40 class SoftbusChannelAdapter {
41     DECLARE_SINGLE_INSTANCE_BASE(SoftbusChannelAdapter);
42 public:
43     int32_t CreateChannelServer(const std::string &pkgName, const std::string &sessName);
44     int32_t RemoveChannelServer(const std::string &pkgName, const std::string &sessName);
45 
46     int32_t OpenSoftbusChannel(const std::string &mySessName, const std::string &peerSessName,
47         const std::string &peerDevId);
48     int32_t CloseSoftbusChannel(const std::string &mySessName, const std::string &peerDevId);
49 
50     int32_t SendBytesData(const std::string &sessName, const std::string &peerDevId, const std::string &data);
51     int32_t SendStreamData(const std::string &sessName, const std::string &peerDevId, const StreamData *data,
52         const StreamData *ext);
53 
54     int32_t RegisterChannelListener(const std::string &sessName, const std::string &peerDevId,
55         ISoftbusChannelListener *listener);
56     int32_t UnRegisterChannelListener(const std::string &sessName, const std::string &peerDevId);
57 
58     int32_t StartDeviceTimeSync(const std::string &pkgName, const std::string &sessName,
59         const std::string &peerDevId);
60     int32_t StopDeviceTimeSync(const std::string &pkgName, const std::string &sessName,
61         const std::string &peerDevId);
62 
63     void SendChannelEvent(const std::string &sessName, const AVTransEvent event);
64 
65     int32_t OnSoftbusChannelOpened(std::string peerSessionName, int32_t sessionId,
66         std::string peerDevId, int32_t result);
67     void OnSoftbusChannelClosed(int32_t sessionId, ShutdownReason reason);
68     void OnSoftbusBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen);
69     void OnSoftbusTimeSyncResult(const TimeSyncResultInfo *info, int32_t result);
70     void OnSoftbusStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
71         const StreamFrameInfo *frameInfo);
72 
73 private:
74     SoftbusChannelAdapter();
75     ~SoftbusChannelAdapter();
76 
77     std::string GetSessionNameById(int32_t sessionId);
78     int32_t GetSessIdBySessName(const std::string &sessName, const std::string &peerDevId);
79     std::string GetPeerDevIdBySessId(int32_t sessionId);
80     std::string GetOwnerFromSessName(const std::string &sessName);
81     std::string TransName2PkgName(const std::string &ownerName);
82     std::string FindSessNameByPeerSessName(const std::string peerSessionName);
83     void SendEventChannelOPened(const std::string &mySessName, const std::string &peerDevId);
84 
85 private:
86     std::mutex timeSyncMtx_;
87     std::mutex idMapMutex_;
88     std::mutex listenerMtx_;
89     std::mutex serverMapMtx_;
90 
91     ISocketListener sessListener_;
92     std::map<std::string, int32_t> serverMap_;
93     std::set<std::string> timeSyncSessNames_;
94     std::map<std::string, int32_t> devId2SessIdMap_;
95     std::map<std::string, ISoftbusChannelListener *> listenerMap_;
96 };
97 } // namespace DistributedHardware
98 } // namespace OHOS
99 #endif // OHOS_SOFTBUS_CHANNEL_ADAPTER
100