/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef UARTDE_VICEIMPL_H #define UARTDE_VICEIMPL_H #include "StatusCode.h" #include "UartDevice.h" #include #include #define UART_DEVICE_INIT_NAME "UART" typedef struct uart_device_header { const char *mCheckName; } UartDeviceHeader; class UartDeviceImpl { public: UartDeviceImpl(const UartInfo &uatrInfo); virtual ~UartDeviceImpl() = default; const StatusCode UartOpen(void); const ssize_t UartSend(const void *buff, const size_t &buffLength); const ssize_t UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs = 0, const unsigned int delayReadMs = 0); void UartTcflush(void); private: const StatusCode SetConfig(void); private: const UartInfo mUatrInfo; int mFd; }; typedef struct uart_device UartDevice; typedef struct uart_device { const StatusCode (*mOpen)(UartDevice *); const ssize_t (*mSend)(UartDevice *, const void *, const size_t); const ssize_t (*mRecv)(UartDevice *, void *, const size_t, const unsigned int); void (*mTcflush)(UartDevice *); void (*mFree)(void *); } UartDevice; // TODO: There may be a CPU byte alignment bug. typedef struct uart_device_impl UartDeviceImpl_S; typedef struct uart_device_impl { UartDeviceHeader mHeader; UartDevice mBase; std::shared_ptr mUartImpl; } UartDeviceImpl_S; UartDevice *NewUartDeviceImpl(const UartInfo &uartInfo); static inline UartDeviceImpl_S *UartDeviceImplConvert(UartDevice *object) { return ((UartDeviceImpl_S *)(((char *)object) - sizeof(UartDeviceHeader))); } const char *GetUartDeviceModuleName(void); #endif