1 /*
2  * Copyright (c) 2022-2024 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_ADAPTER
17 #define OHOS_SOFTBUS_ADAPTER
18 
19 #include <mutex>
20 #include <map>
21 #include <set>
22 
23 #include "transport/socket.h"
24 #include "transport/trans_type.h"
25 #include "single_instance.h"
26 
27 #include "dscreen_constants.h"
28 #include "dscreen_log.h"
29 #include "isoftbus_listener.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 typedef struct {
34     std::string sessionName;
35     std::string peerDevId;
36 } SessionInfo;
37 
38 class SoftbusAdapter {
39     DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter);
40 public:
41     int32_t CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
42         const std::string &peerDevId);
43     int32_t RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
44         const std::string &peerDevId);
45     int32_t OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName,
46         const std::string &peerDevId);
47     int32_t CloseSoftbusSession(const int32_t sessionId);
48     int32_t SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen) const;
49     int32_t SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext,
50         const StreamFrameInfo *param) const;
51     int32_t RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> &listener, const std::string &sessionName,
52         const std::string &peerDevId);
53     int32_t UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId);
54 
55     int32_t OnSoftbusSessionOpened(int32_t sessionId, PeerSocketInfo info);
56     void OnSoftbusSessionClosed(int32_t sessionId, ShutdownReason reason);
57     void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen);
58     void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
59         const StreamFrameInfo *frameInfo);
60     void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen) const;
61 
62 private:
63     SoftbusAdapter();
64     ~SoftbusAdapter();
65     std::shared_ptr<ISoftbusListener> &GetSoftbusListenerByName(int32_t sessionId);
66     std::shared_ptr<ISoftbusListener> &GetSoftbusListenerById(int32_t sessionId);
67 
68 private:
69     static const constexpr char *DSCREEN_LOG_TAG = "SoftbusAdapter";
70     std::mutex listenerMtx_;
71     std::mutex idMapMutex_;
72     std::mutex serverIdMapMutex_;
73 
74     ISocketListener sessListener_;
75     /* while can not find the listener in mapListeners_, return nullListener_ point to null ptr. */
76     std::map<int32_t, std::string> serverIdMap_;
77     std::map<int32_t, std::string> devId2SessIdMap_;
78     std::shared_ptr<ISoftbusListener> nullListener_;
79     std::map<std::string, std::shared_ptr<ISoftbusListener>> mapListeners_;
80     std::map<int32_t, std::shared_ptr<ISoftbusListener>> mapSessListeners_;
81 };
82 } // namespace DistributedHardware
83 } // namespace OHOS
84 #endif