1 /*
2  * Copyright (c) 2022-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 DISTRIBUTED_INPUT_TRANSPORT_BASE_H
17 #define DISTRIBUTED_INPUT_TRANSPORT_BASE_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <map>
22 #include <mutex>
23 #include <set>
24 #include <string>
25 #include <thread>
26 #include <vector>
27 
28 #include "constants.h"
29 #include "event_handler.h"
30 #include "nlohmann/json.hpp"
31 #include "securec.h"
32 #include "single_instance.h"
33 
34 #include "socket.h"
35 #include "softbus_bus_center.h"
36 
37 #include "dinput_sink_manager_callback.h"
38 #include "dinput_source_manager_callback.h"
39 #include "dinput_transbase_source_callback.h"
40 #include "dinput_transbase_sink_callback.h"
41 #include "i_session_state_callback.h"
42 
43 namespace OHOS {
44 namespace DistributedHardware {
45 namespace DistributedInput {
46 class DistributedInputTransportBase {
47     DECLARE_SINGLE_INSTANCE_BASE(DistributedInputTransportBase);
48 public:
49     int32_t Init();
50     int32_t StartSession(const std::string &remoteDevId);
51     void StopSession(const std::string &remoteDevId);
52     void StopAllSession();
53 
54     void RegisterSrcHandleSessionCallback(std::shared_ptr<DInputTransbaseSourceCallback> callback);
55     void RegisterSinkHandleSessionCallback(std::shared_ptr<DInputTransbaseSinkCallback> callback);
56     void RegisterSourceManagerCallback(std::shared_ptr<DInputSourceManagerCallback> callback);
57     void RegisterSinkManagerCallback(std::shared_ptr<DInputSinkManagerCallback> callback);
58     void RegisterSessionStateCb(sptr<ISessionStateCallback> callback);
59     void UnregisterSessionStateCb();
60     int32_t OnSessionOpened(int32_t sessionId, const PeerSocketInfo &info);
61     void OnSessionClosed(int32_t sessionId, ShutdownReason reason);
62     void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen);
63 
64     int32_t GetCurrentSessionId();
65     int32_t CountSession(const std::string &remoteDevId);
66     void EraseSessionId(const std::string &remoteDevId);
67     int32_t GetSessionIdByDevId(const std::string &srcId);
68     std::string GetDevIdBySessionId(int32_t sessionId);
69     int32_t SendMsg(int32_t sessionId, std::string &message);
70 
71 private:
72     DistributedInputTransportBase() = default;
73     ~DistributedInputTransportBase();
74     int32_t CheckDeviceSessionState(const std::string &remoteDevId);
75     bool CheckRecivedData(const std::string &message);
76     void HandleSession(int32_t sessionId, const std::string &message);
77     void Release();
78     void RunSessionStateCallback(const std::string &remoteDevId, const uint32_t sessionState);
79 
80     int32_t CreateServerSocket();
81     int32_t CreateClientSocket(const std::string &remoteDevId);
82 
83 private:
84     std::atomic<bool> isSessSerCreateFlag_ = false;
85     std::atomic<int32_t> localServerSocket_;
86     std::mutex sessSerOperMutex_;
87     std::mutex operationMutex_;
88     std::string remoteDeviceId_;
89     std::map<std::string, int32_t> remoteDevSessionMap_;
90     std::map<std::string, bool> channelStatusMap_;
91     std::string localSessionName_ = "";
92     int32_t sessionId_ = 0;
93 
94     std::shared_ptr<DInputTransbaseSourceCallback> srcCallback_;
95     std::shared_ptr<DInputTransbaseSinkCallback> sinkCallback_;
96     std::shared_ptr<DInputSourceManagerCallback> srcMgrCallback_;
97     std::shared_ptr<DInputSinkManagerCallback> sinkMgrCallback_;
98     sptr<ISessionStateCallback> SessionStateCallback_;
99 };
100 
101 } // namespace DistributedInput
102 } // namespace DistributedHardware
103 } // namespace OHOS
104 
105 #endif // DISTRIBUTED_INPUT_TRANSPORT_BASE_H