1 /* 2 * Copyright (c) 2022 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 33 #include "dinput_transbase_source_callback.h" 34 #include "dinput_transbase_sink_callback.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 namespace DistributedInput { 39 class DistributedInputTransportBase { 40 public: 41 static DistributedInputTransportBase &GetInstance(); 42 ~DistributedInputTransportBase(); 43 44 int32_t Init(); 45 46 int32_t StartSession(const std::string &remoteDevId); 47 void StopSession(const std::string &remoteDevId); 48 49 void RegisterSrcHandleSessionCallback(std::shared_ptr<DInputTransbaseSourceCallback> callback); 50 void RegisterSinkHandleSessionCallback(std::shared_ptr<DInputTransbaseSinkCallback> callback); 51 52 int32_t OnSessionOpened(int32_t sessionId, int32_t result); 53 void OnSessionClosed(int32_t sessionId); 54 void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); 55 56 int32_t GetCurrentSessionId(); 57 int32_t CountSession(const std::string &remoteDevId); 58 void EraseSessionId(const std::string &remoteDevId); 59 int32_t GetSessionIdByDevId(const std::string &srcId); 60 std::string GetDevIdBySessionId(int32_t sessionId); 61 int32_t SendMsg(int32_t sessionId, std::string &message); 62 63 private: 64 int32_t CheckDeviceSessionState(const std::string &remoteDevId); 65 bool CheckRecivedData(const std::string &message); 66 void HandleSession(int32_t sessionId, const std::string &message); 67 void Release(); 68 69 private: 70 std::atomic<bool> isSessSerCreateFlag_ = false; 71 std::mutex sessSerOperMutex_; 72 std::mutex operationMutex_; 73 std::string remoteDeviceId_; 74 std::map<std::string, int32_t> remoteDevSessionMap_; 75 std::map<std::string, bool> channelStatusMap_; 76 std::condition_variable openSessionWaitCond_; 77 std::string localSessionName_ = ""; 78 int32_t sessionId_ = 0; 79 80 std::shared_ptr<DInputTransbaseSourceCallback> srcCallback_; 81 std::shared_ptr<DInputTransbaseSinkCallback> sinkCallback_; 82 }; 83 84 } // namespace DistributedInput 85 } // namespace DistributedHardware 86 } // namespace OHOS 87 88 #endif // DISTRIBUTED_INPUT_TRANSPORT_BASE_H