1 /* 2 * Copyright (c) 2025 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 OHOS_MECH_SEND_MANAGER_H 17 #define OHOS_MECH_SEND_MANAGER_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <set> 22 #include <shared_mutex> 23 #include <string> 24 #include <sys/prctl.h> 25 #include <thread> 26 27 #include "ble_send_manager.h" 28 #include "event_handler.h" 29 #include "mc_subscription_center.h" 30 31 namespace OHOS { 32 namespace MechBodyController { 33 class TransportSendAdapter : public std::enable_shared_from_this<TransportSendAdapter> { 34 public: 35 explicit TransportSendAdapter(); 36 virtual ~TransportSendAdapter(); 37 int32_t SendCommand(const std::shared_ptr<CommandBase> &cmd, int32_t delayMs = 0); 38 int32_t RegisterBluetoothListener(); 39 int32_t UnRegisterBluetoothListener(); 40 int32_t OnReceive(bool isAck, uint16_t seqNo, std::shared_ptr<MechDataBuffer> dataBuffer); 41 42 private: 43 int32_t PushResponseTask(const std::shared_ptr<CommandBase> &cmd, uint16_t seqNo); 44 int32_t ExeResponseTask(uint16_t seqNo, const std::shared_ptr<MechDataBuffer>& mechDataBuffer); 45 int32_t ExeRespTimeoutTask(uint16_t seqNo); 46 int32_t RemoveRespTimeoutTask(uint16_t seqNo); 47 void StartSendEvent(); 48 void StartRecvEvent(); 49 uint16_t CreateResponseSeqNo(); 50 const std::shared_ptr<CommandBase> GetCmdBySeqNo(uint16_t seqNo); 51 52 private: 53 std::mutex sendEventMutex_; 54 std::thread sendEventThread_; 55 std::condition_variable sendEventCon_; 56 std::shared_ptr<OHOS::AppExecFwk::EventHandler> sendEventHandler_; 57 58 std::mutex recvEventMutex_; 59 std::thread recvEventThread_; 60 std::condition_variable recvEventCon_; 61 std::shared_ptr<OHOS::AppExecFwk::EventHandler> recvEventHandler_; 62 std::shared_mutex responseMutex_; 63 std::map<uint16_t, std::shared_ptr<CommandBase>> pendingRequests_; 64 uint16_t lastSeqNo_ = 0; 65 std::shared_ptr<BleReceviceListener> receviceListener_; 66 }; 67 68 class BleReceviceListenerImpl : public BleReceviceListener { 69 public: 70 virtual ~BleReceviceListenerImpl() = default; 71 BleReceviceListenerImpl(std::shared_ptr<TransportSendAdapter> sendAdapter); 72 int32_t OnReceive(const uint8_t *data, uint32_t dataLen) override; 73 74 private: 75 std::shared_ptr<TransportSendAdapter> sendAdapter_; 76 }; 77 } // namespace MechBodyController 78 } // namespace OHOS 79 #endif // OHOS_MECH_SEND_MANAGER_H 80