• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "session.h"
24 #include "single_instance.h"
25 
26 #include "dscreen_constants.h"
27 #include "dscreen_log.h"
28 #include "isoftbus_listener.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 typedef struct {
33     std::string sessionName;
34     std::string peerDevId;
35 } SessionInfo;
36 
37 class SoftbusAdapter {
38     DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter);
39 public:
40     int32_t CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
41         const std::string &peerDevId);
42     int32_t RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName,
43         const std::string &peerDevId);
44     int32_t OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName,
45         const std::string &peerDevId) const;
46     int32_t CloseSoftbusSession(const int32_t sessionId);
47     int32_t SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen) const;
48     int32_t SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext,
49         const StreamFrameInfo *param) const;
50     int32_t RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> &listener, const std::string &sessionName,
51         const std::string &peerDevId);
52     int32_t UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId);
53 
54     int32_t OnSoftbusSessionOpened(int32_t sessionId, int32_t result);
55     void OnSoftbusSessionClosed(int32_t sessionId);
56     void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen);
57     void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
58         const StreamFrameInfo *frameInfo);
59     void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen) const;
60     void OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList) 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 *LOG_TAG = "SoftbusAdapter";
70     std::mutex listenerMtx_;
71     std::mutex sessInfoMtx_;
72     std::mutex sessSetMtx_;
73 
74     ISessionListener sessListener_;
75     /* while can not find the listener in mapListeners_, return nullListener_ point to null ptr. */
76     std::shared_ptr<ISoftbusListener> nullListener_;
77     std::map<int32_t, SessionInfo> mapSessionInfos_;
78     std::map<std::string, std::set<std::string>> mapSessionSet_;
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