diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 72163610..4223a6b1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,13 +15,13 @@ if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=gethostbyname" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=connect" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=socket" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=select" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_select" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_open" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_read" CACHE STRING INTERNAL FORCE) set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_write" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_close" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fclose" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fread" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_read" CACHE STRING INTERNAL FORCE) # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fcntl" CACHE STRING INTERNAL FORCE) endif() # Mock Linux api. diff --git a/test/utils/LinuxApiMock/include/LinuxApiMock.h b/test/utils/LinuxApiMock/include/LinuxApiMock.h index 66e1fd6d..d8efdc93 100644 --- a/test/utils/LinuxApiMock/include/LinuxApiMock.h +++ b/test/utils/LinuxApiMock/include/LinuxApiMock.h @@ -45,6 +45,7 @@ using ::testing::Unused; using ::testing::WithArgs; using ::testing::internal::BuiltInDefaultValue; constexpr int INVALID_HANDLE = -1; +constexpr int MOCK_SELECT_TIME_OUT = 0; class LinuxApiMock { public: @@ -65,6 +66,8 @@ public: virtual int fx_tcgetattr(int fd, struct termios *termios_p); virtual int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p); virtual ssize_t fx_write(int fd, const void *buf, size_t count); + virtual ssize_t fx_read(int fd, void *buf, size_t count); + virtual int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); public: /** @@ -91,6 +94,8 @@ public: MOCK_METHOD2(fx_tcgetattr, int(int, struct termios *)); MOCK_METHOD3(fx_tcsetattr, int(int, int, const struct termios *)); MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t)); + MOCK_METHOD3(fx_read, ssize_t(int, void *, size_t)); + MOCK_METHOD5(fx_select, int(int, fd_set *, fd_set *, fd_set *, struct timeval *)); public: /** diff --git a/test/utils/LinuxApiMock/src/LinuxApiMock.cpp b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp index 3b1fc710..5d1a812d 100644 --- a/test/utils/LinuxApiMock/src/LinuxApiMock.cpp +++ b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp @@ -15,13 +15,36 @@ #include "LinuxApiMock.h" #include "ILog.h" #include "LinuxTestImpl.h" -int LinuxApiMock::fx_open(const char *pathname, int flags) { return __real_fx_open(pathname, flags); } -int LinuxApiMock::fx_tcgetattr(int fd, struct termios *termios_p) { return __real_fx_tcgetattr(fd, termios_p); } +#include "WrapApi.h" +int LinuxApiMock::fx_open(const char *pathname, int flags) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_open(pathname, flags); +} +int LinuxApiMock::fx_tcgetattr(int fd, struct termios *termios_p) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_tcgetattr(fd, termios_p); +} int LinuxApiMock::fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p) { return __real_fx_tcsetattr(fd, optional_actions, termios_p); } -ssize_t LinuxApiMock::fx_write(int fd, const void *buf, size_t count) { return __real_fx_write(fd, buf, count); } +ssize_t LinuxApiMock::fx_write(int fd, const void *buf, size_t count) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_write(fd, buf, count); +} +ssize_t LinuxApiMock::fx_read(int fd, void *buf, size_t count) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_read(fd, buf, count); +} +int LinuxApiMock::fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_select(nfds, readfds, writefds, exceptfds, timeout); +} std::shared_ptr LinuxTest::CreateLinuxTest(void) { std::shared_ptr test = std::make_shared(); diff --git a/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp index 6f5f6889..1de75c28 100644 --- a/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp +++ b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp @@ -30,45 +30,67 @@ void LinuxTestImpl::ApiInit(std::shared_ptr &mock) openFd = __real_fx_open(pathname, flags); LogInfo("openFd = %d\n", openFd); }; - static int resultTcgetattr = -1; - auto api_tcgetattr = [=](int fd, struct termios *termios_p) { - resultTcgetattr = __real_fx_tcgetattr(fd, termios_p); - LogInfo("resultTcgetattr = %d\n", resultTcgetattr); - }; - static int resultTcsetattr = -1; - auto api_tcsetattr = [=](int fd, int optional_actions, const struct termios *termios_p) { - resultTcsetattr = __real_fx_tcsetattr(fd, optional_actions, termios_p); - LogInfo("resultTcsetattr = %d\n", resultTcsetattr); - }; - static int writeLength = -1; - auto api_write = [=](int fd, const void *buf, size_t count) { - writeLength = __real_fx_write(fd, buf, count); - LogInfo("writeLength = %d\n", writeLength); - }; EXPECT_CALL(*mock.get(), fx_open(_, _)) .WillRepeatedly( DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), WithArgs<0, 1>(Invoke(api_open)), Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), ReturnPointee(&openFd))); + static int resultTcgetattr = -1; + auto api_tcgetattr = [=](int fd, struct termios *termios_p) { + resultTcgetattr = __real_fx_tcgetattr(fd, termios_p); + LogInfo("resultTcgetattr = %d\n", resultTcgetattr); + }; EXPECT_CALL(*mock.get(), fx_tcgetattr(_, _)) .WillRepeatedly( DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), WithArgs<0, 1>(Invoke(api_tcgetattr)), Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), ReturnPointee(&resultTcgetattr))); + static int resultTcsetattr = -1; + auto api_tcsetattr = [=](int fd, int optional_actions, const struct termios *termios_p) { + resultTcsetattr = __real_fx_tcsetattr(fd, optional_actions, termios_p); + LogInfo("resultTcsetattr = %d\n", resultTcsetattr); + }; EXPECT_CALL(*mock.get(), fx_tcsetattr(_, _, _)) .WillRepeatedly( DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), WithArgs<0, 1, 2>(Invoke(api_tcsetattr)), Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), ReturnPointee(&resultTcsetattr))); + static int writeLength = -1; + auto api_write = [=](int fd, const void *buf, size_t count) { + writeLength = __real_fx_write(fd, buf, count); + LogInfo("writeLength = %d\n", writeLength); + }; EXPECT_CALL(*mock.get(), fx_write(_, _, _)) .WillRepeatedly( DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), WithArgs<0, 1, 2>(Invoke(api_write)), Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), ReturnPointee(&writeLength))); + static int readLength = -1; + auto api_read = [=](int fd, void *buf, size_t count) { + readLength = __real_fx_read(fd, buf, count); + LogInfo("readLength = %d\n", readLength); + }; + EXPECT_CALL(*mock.get(), fx_read(_, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2>(Invoke(api_read)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&readLength))); + static int selectResult = -1; + auto api_select = [=](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + selectResult = __real_fx_select(nfds, readfds, writefds, exceptfds, timeout); + LogInfo("selectResult = %d\n", selectResult); + }; + EXPECT_CALL(*mock.get(), fx_select(_, _, _, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2, 3, 4>(Invoke(api_select)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&selectResult))); } void LinuxTestImpl::Init() {} void LinuxTestImpl::UnInit() diff --git a/test/utils/LinuxApiMock/src/WrapApi.cpp b/test/utils/LinuxApiMock/src/WrapApi.cpp index ed59201a..546ee1df 100644 --- a/test/utils/LinuxApiMock/src/WrapApi.cpp +++ b/test/utils/LinuxApiMock/src/WrapApi.cpp @@ -30,6 +30,11 @@ ssize_t __wrap_fx_write(int fd, const void *buf, size_t count) { return LinuxApiMock::GetInstance()->fx_write(fd, buf, count); } +ssize_t __wrap_fx_read(int fd, void *buf, size_t count) { return LinuxApiMock::GetInstance()->fx_read(fd, buf, count); } +int __wrap_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + return LinuxApiMock::GetInstance()->fx_select(nfds, readfds, writefds, exceptfds, timeout); +} #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/WrapApi.h b/test/utils/LinuxApiMock/src/WrapApi.h index 260fe40c..ced75c54 100644 --- a/test/utils/LinuxApiMock/src/WrapApi.h +++ b/test/utils/LinuxApiMock/src/WrapApi.h @@ -14,6 +14,9 @@ */ #ifndef WRAP_API_H #define WRAP_API_H +#include +#include +#include #include #ifdef __cplusplus extern "C" { @@ -23,6 +26,8 @@ int __real_fx_open(const char *pathname, int flags); int __real_fx_tcgetattr(int fd, struct termios *termios_p); int __real_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p); ssize_t __real_fx_write(int fd, const void *buf, size_t count); +ssize_t __real_fx_read(int fd, void *buf, size_t count); +int __real_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); #ifdef __cplusplus } #endif diff --git a/test/utils/UartDevice/src/UartDevice_Test.cpp b/test/utils/UartDevice/src/UartDevice_Test.cpp index 68b5ef28..e51da3bc 100644 --- a/test/utils/UartDevice/src/UartDevice_Test.cpp +++ b/test/utils/UartDevice/src/UartDevice_Test.cpp @@ -18,11 +18,11 @@ #include namespace UartDeviceTest { -// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.IllegalObject +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_AUTO_IllegalObject /** * UartDevice module api will not crash when object is illegal. */ -TEST(UartDeviceTest, IllegalObject) +TEST(UartDeviceTest, UNIT_UartDevice_AUTO_IllegalObject) { CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); @@ -31,8 +31,8 @@ TEST(UartDeviceTest, IllegalObject) IUartRecv(nullptr, nullptr, 0, 0); IUartTcflush(nullptr); IUartDeviceFree(nullptr); - int illegalObject = 0; - void *object = &illegalObject; + char illegalObject[100] = {0}; + void *object = &illegalObject[10]; IUartOpen(object); IUartSend(object, nullptr, 0); IUartRecv(object, nullptr, 0, 0); @@ -40,8 +40,8 @@ TEST(UartDeviceTest, IllegalObject) IUartDeviceFree(object); ILogUnInit(); } -// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.Demo -TEST(UartDeviceTest, Demo) +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_EXAMPLE_Demo +TEST(UartDeviceTest, UNIT_UartDevice_EXAMPLE_Demo) { CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); diff --git a/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp b/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp index 5509dd4b..3c72f22b 100644 --- a/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp +++ b/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp @@ -55,21 +55,26 @@ public: mLinuxTest = std::make_shared(); std::shared_ptr test = std::make_shared(); LinuxApiMock::GetInstance(&test); + UnregisterUartDevice(gUartDevice); } public: std::shared_ptr mLinuxTest; }; -// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_Demo -TEST_F(UartDeviceMockTest, UNIT_UartDevice_EXAMPLE_Demo) +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_AUTO_Demo +TEST_F(UartDeviceMockTest, UNIT_UartDevice_EXAMPLE_AUTO_Demo) { void *object = CreateUartDevice(gUartDevice); IUartOpen(object); const char *SEND_BUF = "TEST"; ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); - // IUartRecv(object, nullptr, 0, 0); - // IUartTcflush(object); + constexpr int RECV_TIME_OUT_MS = 1000; + constexpr int RECV_BUF_LENGTH = 256; + char recvBuf[RECV_BUF_LENGTH] = {0}; + ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS); + LogInfo("recvResult = %d\n", recvResult); + IUartTcflush(object); IUartDeviceFree(object); } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror @@ -93,6 +98,7 @@ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror) IUartOpen(object); IUartDeviceFree(object); EXPECT_EQ(object, nullptr); + UnregisterUartDevice(uartDevice); } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror2 /** @@ -137,6 +143,21 @@ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_WriteAllData) EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); IUartDeviceFree(object); } +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_RecvData +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_RecvData) +{ + const char *RECV_BUFF = "TEST"; + SetRecvApiOnce(mLinuxTest, gUartDevice, (void *)RECV_BUFF, strlen(RECV_BUFF)); + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + constexpr int RECV_TIME_OUT_MS = 1000; + constexpr int RECV_BUF_LENGTH = 256; + char recvBuf[RECV_BUF_LENGTH] = {0}; + ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS); + EXPECT_EQ(recvResult, static_cast(strlen(RECV_BUFF))); + EXPECT_EQ(0, memcmp(recvBuf, RECV_BUFF, strlen(RECV_BUFF))); + IUartDeviceFree(object); +} // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_MultiThreadTest /** * @brief Construct a new test f object diff --git a/test/utils/UartDevice/tool/include/UartDeviceTestTool.h b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h index daaa88a2..be73c227 100644 --- a/test/utils/UartDevice/tool/include/UartDeviceTestTool.h +++ b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h @@ -29,6 +29,12 @@ public: * @param uart */ void RegisterUartDevice(std::shared_ptr &mock, const uart_info &uart); + /** + * @brief + * + * @param uart + */ + void UnregisterUartDevice(const uart_info &uart); /** * @brief Set the Send Api object * @@ -37,6 +43,15 @@ public: * @param length */ void SetSendApiOnce(std::shared_ptr &mock, const uart_info &uart, const ssize_t &length); + /** + * @brief Set the Recv Api Once object + * + * @param mock + * @param uart + * @param recvBuff + * @param length + */ + void SetRecvApiOnce(std::shared_ptr &mock, const uart_info &uart, void *recvBuff, const ssize_t &length); private: std::map mDeviceMap; diff --git a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp index 2fa0df1d..144851e4 100644 --- a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp +++ b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp @@ -14,6 +14,7 @@ */ #include "UartDeviceTestTool.h" #include "ILog.h" +#include static size_t WRITE_COUNT = -1; void UartDeviceTestTool::RegisterUartDevice(std::shared_ptr &mock, const uart_info &uart) { @@ -30,8 +31,27 @@ void UartDeviceTestTool::RegisterUartDevice(std::shared_ptr &mock, co EXPECT_CALL(*mock.get(), fx_tcsetattr(uartFd, _, _)).WillRepeatedly(DoAll(Return(TCSETATTR_SUCCEED))); EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) .WillRepeatedly(DoAll(SaveArg<2>(&WRITE_COUNT), ReturnPointee(&WRITE_COUNT))); + auto selectTimeOut = + [=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + std::this_thread::sleep_for(std::chrono::milliseconds(timeMs)); + }; + EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _)) + .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); + constexpr int READ_NOTHING = 0; + EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)).WillRepeatedly(DoAll(Return(READ_NOTHING))); mDeviceMap[uart.mDevice] = uartFd; } +void UartDeviceTestTool::UnregisterUartDevice(const uart_info &uart) +{ + std::map::iterator iter; + iter = mDeviceMap.find(uart.mDevice); + if (iter != mDeviceMap.end()) { + mDeviceMap.erase(iter); + return; + } + LogError("Can't found the uart device[%s].\n", uart.mDevice); +} void UartDeviceTestTool::SetSendApiOnce(std::shared_ptr &mock, const uart_info &uart, const ssize_t &length) { std::map::iterator iter; @@ -43,4 +63,34 @@ void UartDeviceTestTool::SetSendApiOnce(std::shared_ptr &mock, const EXPECT_CALL(*mock.get(), fx_write(mDeviceMap[uart.mDevice], _, _)) .WillOnce(DoAll(Return(length))) .WillRepeatedly(DoAll(SaveArg<2>(&WRITE_COUNT), ReturnPointee(&WRITE_COUNT))); +} +void UartDeviceTestTool::SetRecvApiOnce(std::shared_ptr &mock, const uart_info &uart, void *recvBuff, + const ssize_t &length) +{ + std::map::iterator iter; + iter = mDeviceMap.find(uart.mDevice); + if (iter == mDeviceMap.end()) { + LogError("Can't found the uart device[%s].\n", uart.mDevice); + return; + } + int uartFd = mDeviceMap[uart.mDevice]; + auto selectSucceed = + [=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + FD_ZERO(readfds); + FD_SET(uartFd, readfds); + }; + auto selectTimeOut = + [=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + std::this_thread::sleep_for(std::chrono::milliseconds(timeMs)); + }; + constexpr int SELECT_READABLE = 1; + EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _)) + .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectSucceed)), Return(SELECT_READABLE))) + .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); + auto readBuf = [=, &mock](int fd, void *buf, size_t count) { memcpy(buf, recvBuff, length); }; + constexpr int READ_NOTHING = 0; + EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)) + .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(readBuf)), Return(length))) + .WillRepeatedly(DoAll(Return(READ_NOTHING))); } \ No newline at end of file diff --git a/utils/LinuxApi/include/LinuxApi.h b/utils/LinuxApi/include/LinuxApi.h index c5b5d3c3..b0168e8a 100644 --- a/utils/LinuxApi/include/LinuxApi.h +++ b/utils/LinuxApi/include/LinuxApi.h @@ -15,6 +15,9 @@ #ifndef LINUX_API_H #define LINUX_API_H #include +#include +#include +#include #include #include #ifdef __cplusplus @@ -25,6 +28,8 @@ int fx_open(const char *pathname, int flags); int fx_tcgetattr(int fd, struct termios *termios_p); int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p); ssize_t fx_write(int fd, const void *buf, size_t count); +ssize_t fx_read(int fd, void *buf, size_t count); +int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); #ifdef __cplusplus } #endif diff --git a/utils/LinuxApi/src/LinuxApi.c b/utils/LinuxApi/src/LinuxApi.c index f014cc8a..d49a9087 100644 --- a/utils/LinuxApi/src/LinuxApi.c +++ b/utils/LinuxApi/src/LinuxApi.c @@ -29,4 +29,9 @@ int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p) { return tcsetattr(fd, optional_actions, termios_p); } -ssize_t fx_write(int fd, const void *buf, size_t count) { return write(fd, buf, count); } \ No newline at end of file +ssize_t fx_write(int fd, const void *buf, size_t count) { return write(fd, buf, count); } +ssize_t fx_read(int fd, void *buf, size_t count) { return read(fd, buf, count); } +int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + return select(nfds, readfds, writefds, exceptfds, timeout); +} \ No newline at end of file diff --git a/utils/UartDevice/src/UartDeviceImpl.cpp b/utils/UartDevice/src/UartDeviceImpl.cpp index bf142b8d..6bac8877 100644 --- a/utils/UartDevice/src/UartDeviceImpl.cpp +++ b/utils/UartDevice/src/UartDeviceImpl.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include constexpr int INVALID_FD = -1; static const char *UART_DEVICE_NAME = "uart_device"; @@ -61,14 +62,20 @@ const size_t UartDeviceImpl::UartSend(const char *buff, const size_t &buffLength return writeTotal; } const size_t UartDeviceImpl::UartRecv(char *buff, const size_t &buffLength, const unsigned int &timeOutMs, - const unsigned int delayRead) + const unsigned int delayReadMs) { constexpr int RECV_FAILED = -1; - int len, fs_sel; + int readLength = 0; + int fs_sel = -1; fd_set fs_read; struct timeval time; + if (nullptr == buff) { + LogError("data buff is nullptr.\n"); + return RECV_FAILED; + } if (mFd <= 0) { + LogError("open uart failed.\n"); return RECV_FAILED; } @@ -76,11 +83,11 @@ const size_t UartDeviceImpl::UartRecv(char *buff, const size_t &buffLength, cons FD_SET(mFd, &fs_read); time.tv_sec = timeOutMs / 1000; time.tv_usec = (timeOutMs % 1000) * 1000; - fs_sel = select(mFd + 1, &fs_read, NULL, NULL, &time); + fs_sel = fx_select(mFd + 1, &fs_read, NULL, NULL, &time); if (fs_sel) { - usleep(1000 * delayRead); - len = read(mFd, buff, buffLength); - return len; + std::this_thread::sleep_for(std::chrono::milliseconds(delayReadMs)); + readLength = fx_read(mFd, buff, buffLength); + return readLength; } return RECV_FAILED; } diff --git a/utils/UartDevice/src/UartDeviceImpl.h b/utils/UartDevice/src/UartDeviceImpl.h index b6f4bae2..8218d9fe 100644 --- a/utils/UartDevice/src/UartDeviceImpl.h +++ b/utils/UartDevice/src/UartDeviceImpl.h @@ -31,7 +31,7 @@ public: const StatusCode UartOpen(void); const size_t UartSend(const char *buff, const size_t &buffLength); const size_t UartRecv(char *buff, const size_t &buffLength, const unsigned int &timeOutMs = 0, - const unsigned int delayRead = 0); + const unsigned int delayReadMs = 0); void UartTcflush(void); private: