1 /*
2  * Copyright (c) 2021-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 DISTRIBUTED_INPUT_SINK_TRANSPORT_H
17 #define DISTRIBUTED_INPUT_SINK_TRANSPORT_H
18 
19 #include <mutex>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "constants.h"
25 #include "event_handler.h"
26 #include "nlohmann/json.hpp"
27 
28 #include "dinput_sink_trans_callback.h"
29 #include "dinput_transbase_sink_callback.h"
30 #include "dinput_softbus_define.h"
31 #include "distributed_input_sink_switch.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 namespace DistributedInput {
36 class DistributedInputSinkTransport {
37 public:
38     static DistributedInputSinkTransport &GetInstance();
39     DistributedInputSinkTransport();
40     ~DistributedInputSinkTransport();
41 
42     class DInputTransbaseSinkListener : public DInputTransbaseSinkCallback {
43     public:
44         DInputTransbaseSinkListener(DistributedInputSinkTransport *transport);
45         virtual ~DInputTransbaseSinkListener();
46         void HandleSessionData(int32_t sessionId, const std::string &messageData) override;
47         void NotifySessionClosed(int32_t sessionId) override;
48 
49     private:
50         DistributedInputSinkTransport *sinkTransportObj_;
51     };
52 
53     int32_t Init();
54 
55     void RegistSinkRespCallback(std::shared_ptr<DInputSinkTransCallback> callback);
56     int32_t RespPrepareRemoteInput(const int32_t sessionId, std::string &smsg);
57     int32_t RespUnprepareRemoteInput(const int32_t sessionId, std::string &smsg);
58     int32_t RespStartRemoteInput(const int32_t sessionId, std::string &smsg);
59     int32_t RespStopRemoteInput(const int32_t sessionId, std::string &smsg);
60     int32_t RespLatency(const int32_t sessionId, std::string &smsg);
61     void SendKeyStateNodeMsg(const int32_t sessionId, const std::string &dhId, uint32_t type, const uint32_t btnCode,
62         int32_t value);
63     void SendKeyStateNodeMsgBatch(const int32_t sessionId, const std::vector<struct RawEvent> &events);
64 
65     class DInputSinkEventHandler : public AppExecFwk::EventHandler {
66     public:
67         explicit DInputSinkEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
68         ~DInputSinkEventHandler() override = default;
69 
70         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
71         void RecordEventLog(const std::shared_ptr<nlohmann::json> &events);
72     };
73 
74     std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> GetEventHandler();
75     void CloseAllSession();
76 
77 private:
78     int32_t SendMessage(int32_t sessionId, std::string &message);
79     void HandleData(int32_t sessionId, const std::string &message);
80     void HandleEventInner(int32_t sessionId, const nlohmann::json &recMsg);
81     void NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
82     void NotifyUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
83     void NotifyStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
84     void NotifyStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
85     void NotifyLatency(int32_t sessionId, const nlohmann::json &recMsg);
86     void NotifyStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
87     void NotifyStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
88 
89     void NotifyRelayPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
90     void NotifyRelayUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
91     void NotifyRelayStartDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
92     void NotifyRelayStopDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
93     void NotifyRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
94     void NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
95 
96     void RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value);
97     void RecordEventLog(const std::vector<struct RawEvent> &events);
98 
99     void DoSendMsgBatch(const int32_t sessionId, const std::vector<struct RawEvent> &events);
100 private:
101     std::string mySessionName_;
102     std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> eventHandler_;
103     std::shared_ptr<DistributedInputSinkTransport::DInputTransbaseSinkListener> statuslistener_;
104     std::shared_ptr<DInputSinkTransCallback> callback_;
105 };
106 } // namespace DistributedInput
107 } // namespace DistributedHardware
108 } // namespace OHOS
109 
110 #endif // DISTRIBUTED_INPUT_SINK_TRANSPORT_H
111