1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_DAEMON_UART_H 16 #define HDC_DAEMON_UART_H 17 #include <pthread.h> 18 #include "daemon_common.h" 19 20 namespace Hdc { 21 class HdcDaemon; 22 23 class HdcDaemonUART : public HdcUARTBase { 24 public: 25 explicit HdcDaemonUART(HdcDaemon &, ExternInterface & = HdcUARTBase::defaultInterface); 26 int Initial(const std::string &devPathIn = UART_HDC_NODE); 27 ~HdcDaemonUART(); 28 29 void OnNewHandshakeOK(const uint32_t sessionId); 30 void Stop(); 31 protected: 32 virtual HSession GetSession(const uint32_t sessionId, bool create) override; 33 virtual void OnTransferError(const HSession session) override; 34 35 private: UvWatchTimer(uv_timer_t * handle)36 static inline void UvWatchTimer(uv_timer_t *handle) 37 { 38 if (handle != nullptr) { 39 HdcDaemonUART *thisClass = static_cast<HdcDaemonUART *>(handle->data); 40 if (thisClass != nullptr) { 41 thisClass->WatcherTimerCallBack(); 42 return; 43 } 44 } 45 WRITE_LOG(LOG_FATAL, "UvWatchTimer have not got correct class parameter"); 46 }; 47 virtual void WatcherTimerCallBack(); // ut will overider this 48 virtual int CloseUartDevice(); 49 virtual int OpenUartDevice(); 50 virtual int LoopUARTRead(); 51 virtual int LoopUARTWrite(); 52 virtual bool IsSendReady(HSession hSession); 53 virtual int PrepareBufForRead(); 54 virtual HSession PrepareNewSession(uint32_t sessionId); 55 virtual void DeamonReadThread(); 56 virtual void DeamonWriteThread(); 57 std::vector<uint8_t> dataReadBuf; // from uart dev 58 virtual void ResetOldSession(uint32_t sessionId) override; 59 60 uv_timer_t checkSerialPort; // server-use 61 uint32_t currentSessionId = 0; 62 bool isAlive = false; 63 std::string devPath; 64 65 HdcDaemon &daemon; 66 }; 67 } // namespace Hdc 68 #endif 69