test_comms.cpp
Go to the documentation of this file.
00001 #include "um7/comms.h"
00002 #include "um7/registers.h"
00003 #include "serial/serial.h"
00004 #include <gtest/gtest.h>
00005 #include <fcntl.h>
00006 
00007 class FakeSerial : public ::testing::Test
00008 {
00009 protected:
00016   virtual void SetUp()
00017   {
00018     ASSERT_NE(-1, master_fd = posix_openpt( O_RDWR | O_NOCTTY | O_NDELAY ));
00019     ASSERT_NE(-1, grantpt(master_fd));
00020     ASSERT_NE(-1, unlockpt(master_fd));
00021     ASSERT_TRUE((ser_name = ptsname(master_fd)) != NULL);
00022     ser.setPort(ser_name);
00023     ser.open();
00024     ASSERT_TRUE(ser.isOpen()) << "Couldn't open Serial connection to pseudoterminal.";
00025   }
00026 
00027   void write_serial(const std::string& msg)
00028   {
00029     write(master_fd, msg.c_str(), msg.length());
00030   }
00031 
00032   virtual void TearDown()
00033   {
00034     ser.close();
00035     close(master_fd);
00036   }
00037 
00038   serial::Serial ser;
00039 
00040 private:
00041   int master_fd;
00042   char* ser_name;
00043 };
00044 
00045 TEST_F(FakeSerial, basic_message_rx)
00046 {
00047   // Send message from device which should write four bytes to the raw magnetometer's first register.
00048   std::string msg(um7::Comms::message(DREG_MAG_RAW_XY, std::string("\x1\x2\x3\x4")));
00049   write_serial(msg);
00050 
00051   um7::Comms sensor(&ser);
00052   um7::Registers registers;
00053   ASSERT_EQ(DREG_MAG_RAW_XY, sensor.receive(&registers)) << "Didn't return ID of arriving message.";
00054   EXPECT_EQ(0x0102, registers.mag_raw.get(0));
00055 }
00056 
00057 TEST_F(FakeSerial, batch_message_rx)
00058 {
00059   // Send message from device which should write four bytes to the raw accelerometer's registers.
00060   std::string msg(um7::Comms::message(DREG_ACCEL_RAW_XY, std::string("\x5\x6\x7\x8\x9\xa\0\0", 8)));
00061   write_serial(msg);
00062 
00063   um7::Comms sensor(&ser);
00064   um7::Registers registers;
00065   ASSERT_EQ(DREG_ACCEL_RAW_XY, sensor.receive(&registers)) << "Didn't return ID of arriving message.";
00066   EXPECT_EQ(0x0506, registers.accel_raw.get(0));
00067   EXPECT_EQ(0x0708, registers.accel_raw.get(1));
00068   EXPECT_EQ(0x090a, registers.accel_raw.get(2));
00069 }
00070 
00071 TEST_F(FakeSerial, bad_checksum_message_rx)
00072 {
00073   // Generate message, then twiddle final byte.
00074   std::string msg(um7::Comms::message(DREG_MAG_RAW_XY, std::string("\x1\x2\x3\x4")));
00075   msg[msg.length() - 1]++;
00076   write_serial(msg);
00077 
00078   um7::Comms sensor(&ser);
00079   um7::Registers registers;
00080   EXPECT_EQ(-1, sensor.receive(&registers)) << "Didn't properly ignore bad checksum message.";
00081 }
00082 
00083 TEST_F(FakeSerial, garbage_bytes_preceeding_message_rx)
00084 {
00085   // Generate message, then prepend junk.
00086   std::string msg(um7::Comms::message(CONFIG_REG_START_ADDRESS, std::string()));
00087   msg = "ssssssnsnsns" + msg;
00088   write_serial(msg);
00089 
00090   um7::Comms sensor(&ser);
00091   EXPECT_EQ(CONFIG_REG_START_ADDRESS, sensor.receive(NULL)) << "Didn't handle garbage prepended to message.";
00092 }
00093 
00094 TEST_F(FakeSerial, timeout_message_rx)
00095 {
00096   std::string msg("snp\x12\x45");
00097   write_serial(msg);
00098   um7::Comms sensor(&ser);
00099   EXPECT_EQ(-1, sensor.receive(NULL)) << "Didn't properly time out in the face of a partial message.";
00100 }
00101 
00102 int main(int argc, char **argv)
00103 {
00104 testing::InitGoogleTest(&argc, argv);
00105 return RUN_ALL_TESTS();
00106 }


um7
Author(s): Mike Purvis , Alex Brown
autogenerated on Thu Jun 6 2019 19:34:53