1 /* 2 * Copyright (c) 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 DINPUT_ADAPTER_H 17 #define DINPUT_ADAPTER_H 18 19 #include "nocopyable.h" 20 #include "prepare_d_input_call_back_stub.h" 21 #include "register_session_state_callback_stub.h" 22 #include "start_stop_d_inputs_call_back_stub.h" 23 #include "unprepare_d_input_call_back_stub.h" 24 25 #include "i_context.h" 26 #include "i_dinput_adapter.h" 27 28 namespace OHOS { 29 namespace Msdp { 30 namespace DeviceStatus { 31 class DInputAdapter final : public IDInputAdapter, public std::enable_shared_from_this<DInputAdapter> { 32 public: 33 DInputAdapter(IContext *env); 34 ~DInputAdapter() = default; 35 DISALLOW_COPY_AND_MOVE(DInputAdapter); 36 37 bool IsNeedFilterOut(const std::string &networkId, BusinessEvent &&event) override; 38 39 int32_t StartRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 40 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback) override; 41 int32_t StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 42 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback) override; 43 44 int32_t StopRemoteInput(const std::string &originNetworkId, 45 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback) override; 46 47 int32_t PrepareRemoteInput(const std::string &remoteNetworkId, 48 const std::string &originNetworkId, DInputCallback callback) override; 49 int32_t UnPrepareRemoteInput(const std::string &remoteNetworkId, 50 const std::string &originNetworkId, DInputCallback callback) override; 51 52 int32_t PrepareRemoteInput(const std::string &networkId, DInputCallback callback) override; 53 int32_t UnPrepareRemoteInput(const std::string &networkId, DInputCallback callback) override; 54 int32_t RegisterSessionStateCb(std::function<void(uint32_t)> callback) override; 55 int32_t UnregisterSessionStateCb() override; 56 57 private: 58 enum class CallbackType { 59 StartDInputCallbackSink, 60 StopDInputCallbackSink, 61 StopDInputCallbackDHIds, 62 PrepareDInputCallback, 63 UnprepareDInputCallback, 64 PrepareStartDInputCallbackSink, 65 UnPrepareStopDInputCallbackSink 66 }; 67 68 struct TimerInfo { 69 int32_t times { 0 }; 70 int32_t timerId { 0 }; 71 }; 72 73 class StopDInputCallbackDHIds final : 74 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 75 public: 76 StopDInputCallbackDHIds(std::shared_ptr<DInputAdapter> dinput); 77 ~StopDInputCallbackDHIds() = default; 78 DISALLOW_COPY_AND_MOVE(StopDInputCallbackDHIds); 79 80 void OnResultDhids(const std::string &devId, const int32_t &status) override; 81 82 private: 83 std::weak_ptr<DInputAdapter> dinput_; 84 }; 85 86 class StartDInputCallbackSink final : 87 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 88 public: 89 StartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput); 90 ~StartDInputCallbackSink() = default; 91 DISALLOW_COPY_AND_MOVE(StartDInputCallbackSink); 92 93 void OnResultDhids(const std::string &devId, const int32_t &status) override; 94 95 private: 96 std::weak_ptr<DInputAdapter> dinput_; 97 }; 98 99 class StopDInputCallbackSink final : 100 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 101 public: 102 StopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput); 103 ~StopDInputCallbackSink() = default; 104 DISALLOW_COPY_AND_MOVE(StopDInputCallbackSink); 105 106 void OnResultDhids(const std::string &devId, const int32_t &status) override; 107 108 private: 109 std::weak_ptr<DInputAdapter> dinput_; 110 }; 111 112 class PrepareDInputCallback final : 113 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 114 public: 115 PrepareDInputCallback(std::shared_ptr<DInputAdapter> dinput); 116 ~PrepareDInputCallback() = default; 117 DISALLOW_COPY_AND_MOVE(PrepareDInputCallback); 118 119 void OnResult(const std::string &devId, const int32_t &status) override; 120 121 private: 122 std::weak_ptr<DInputAdapter> dinput_; 123 }; 124 125 class UnprepareDInputCallback final : 126 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 127 public: 128 UnprepareDInputCallback(std::shared_ptr<DInputAdapter> dinput); 129 ~UnprepareDInputCallback() = default; 130 DISALLOW_COPY_AND_MOVE(UnprepareDInputCallback); 131 132 void OnResult(const std::string &devId, const int32_t &status) override; 133 134 private: 135 std::weak_ptr<DInputAdapter> dinput_; 136 }; 137 138 class PrepareStartDInputCallbackSink final : 139 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 140 public: 141 PrepareStartDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput); 142 ~PrepareStartDInputCallbackSink() = default; 143 DISALLOW_COPY_AND_MOVE(PrepareStartDInputCallbackSink); 144 145 void OnResult(const std::string &devId, const int32_t &status) override; 146 147 private: 148 std::weak_ptr<DInputAdapter> dinput_; 149 }; 150 151 class UnPrepareStopDInputCallbackSink final : 152 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 153 public: 154 UnPrepareStopDInputCallbackSink(std::shared_ptr<DInputAdapter> dinput); 155 ~UnPrepareStopDInputCallbackSink() = default; 156 DISALLOW_COPY_AND_MOVE(UnPrepareStopDInputCallbackSink); 157 158 void OnResult(const std::string &devId, const int32_t &status) override; 159 160 private: 161 std::weak_ptr<DInputAdapter> dinput_; 162 }; 163 164 class SessionStateCallback final : 165 public DistributedHardware::DistributedInput::RegisterSessionStateCallbackStub { 166 public: 167 SessionStateCallback(std::function<void(uint32_t)> callback); 168 ~SessionStateCallback() = default; 169 DISALLOW_COPY_AND_MOVE(SessionStateCallback); 170 171 void OnResult(const std::string &devId, const uint32_t status) override; 172 173 private: 174 std::function<void(uint32_t)> callback_; 175 }; 176 177 void SaveCallback(CallbackType type, DInputCallback callback); 178 void AddTimer(const CallbackType &type); 179 void RemoveTimer(const CallbackType &type); 180 void ProcessDInputCallback(CallbackType type, int32_t status); 181 182 IContext *env_ { nullptr }; 183 std::map<CallbackType, TimerInfo> watchings_; 184 std::map<CallbackType, DInputCallback> callbacks_; 185 std::mutex adapterLock_; 186 }; 187 } // namespace DeviceStatus 188 } // namespace Msdp 189 } // namespace OHOS 190 #endif // DINPUT_ADAPTER_H 191