1 /*
2  * Copyright (C) 2021 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_IPC_DBINDER_REMOTE_LISTENER_H
17 #define OHOS_IPC_DBINDER_REMOTE_LISTENER_H
18 
19 #include <string>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <map>
23 #include <mutex>
24 
25 #include "dbinder_service.h"
26 #include "dbinder_softbus_client.h"
27 
28 namespace OHOS {
29 struct DeviceLock {
30     std::mutex mutex;
31 };
32 
33 class DBinderRemoteListener {
34 public:
35     DBinderRemoteListener();
36     ~DBinderRemoteListener();
37 
38     static void ServerOnBind(int32_t socket, PeerSocketInfo info);
39     static void ServerOnShutdown(int32_t socket, ShutdownReason reason);
40     static void ClientOnBind(int32_t socket, PeerSocketInfo info);
41     static void ClientOnShutdown(int32_t socket, ShutdownReason reason);
42     static void OnBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
43     static void EraseDeviceLock(const std::string &networkId);
44 
45     bool StartListener();
46     bool StopListener();
47     bool SendDataToRemote(const std::string &networkId, const struct DHandleEntryTxRx *msg);
48     bool SendDataReply(const std::string &networkId, const struct DHandleEntryTxRx *msg);
49     bool ShutdownSocket(const std::string &networkId);
50 
51 private:
52     DISALLOW_COPY_AND_MOVE(DBinderRemoteListener);
53 
54     std::shared_ptr<DeviceLock> QueryOrNewDeviceLock(const std::string &networkId);
55     void ClearDeviceLock();
56 
57     int32_t CreateClientSocket(const std::string &peerNetworkId);
58     static int32_t GetPeerSocketId(const std::string &peerNetworkId);
59 
60     const std::string DBINDER_SERVER_PKG_NAME = "DBinderBus";
61     const std::string OWN_SESSION_NAME = "DBinderService";
62     const std::string PEER_SESSION_NAME = "DBinderService";
63 
64     static constexpr QosTV QOS_TV[] = {
65         { .qos = QOS_TYPE_MIN_BW, .value = RPC_QOS_MIN_BW },
66         { .qos = QOS_TYPE_MAX_LATENCY, .value = RPC_QOS_MAX_LATENCY },
67         { .qos = QOS_TYPE_MIN_LATENCY, .value = RPC_QOS_MIN_LATENCY },
68         { .qos = QOS_TYPE_MAX_IDLE_TIMEOUT, .value = RPC_QOS_MAX_IDLE_TIME }
69     };
70     static constexpr uint32_t QOS_COUNT = static_cast<uint32_t>(sizeof(QOS_TV) / sizeof(QosTV));
71 
72     int32_t listenSocketId_ = SOCKET_ID_INVALID;
73     ISocketListener clientListener_ {};
74     ISocketListener serverListener_ {};
75 
76     static inline std::mutex deviceMutex_;
77     static inline std::mutex clientSocketMutex_;
78     static inline std::mutex serverSocketMutex_;
79     static inline std::map<std::string, std::shared_ptr<DeviceLock>> deviceLockMap_ {};
80     static inline std::map<std::string, int32_t> clientSocketInfos_ {};
81     static inline std::map<std::string, int32_t> serverSocketInfos_ {};
82 };
83 } // namespace OHOS
84 #endif // OHOS_IPC_DBINDER_REMOTE_LISTENER_H
85