• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_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, const uint32_t btnCode);
62 
63     class DInputSinkEventHandler : public AppExecFwk::EventHandler {
64     public:
65         explicit DInputSinkEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
66         ~DInputSinkEventHandler() override = default;
67 
68         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
69         void RecordEventLog(const std::shared_ptr<nlohmann::json> &events);
70     };
71 
72     std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> GetEventHandler();
73     void CloseAllSession();
74 
75 private:
76     int32_t SendMessage(int32_t sessionId, std::string &message);
77     void HandleData(int32_t sessionId, const std::string &message);
78     void RegRespFunMap();
79     void NotifyPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
80     void NotifyUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
81     void NotifyStartRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
82     void NotifyStopRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
83     void NotifyLatency(int32_t sessionId, const nlohmann::json &recMsg);
84     void NotifyStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
85     void NotifyStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg);
86 
87     void NotifyRelayPrepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
88     void NotifyRelayUnprepareRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
89     void NotifyRelayStartDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
90     void NotifyRelayStopDhidRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
91     void NotifyRelayStartTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
92     void NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg);
93 private:
94     std::string mySessionName_;
95     std::shared_ptr<DistributedInputSinkTransport::DInputSinkEventHandler> eventHandler_;
96     std::shared_ptr<DistributedInputSinkTransport::DInputTransbaseSinkListener> statuslistener_;
97     std::shared_ptr<DInputSinkTransCallback> callback_;
98 
99     using SinkTransportFunc = void (DistributedInputSinkTransport::*)(int32_t sessionId,
100         const nlohmann::json &recMsg);
101     std::map<int32_t, SinkTransportFunc> memberFuncMap_;
102 };
103 } // namespace DistributedInput
104 } // namespace DistributedHardware
105 } // namespace OHOS
106 
107 #endif // DISTRIBUTED_INPUT_SINK_TRANSPORT_H
108