test_comms.cpp
Go to the documentation of this file.
1 #include "um7/comms.h"
2 #include "um7/registers.h"
3 #include "serial/serial.h"
4 #include <gtest/gtest.h>
5 #include <fcntl.h>
6 
7 class FakeSerial : public ::testing::Test
8 {
9 protected:
16  virtual void SetUp()
17  {
18  ASSERT_NE(-1, master_fd = posix_openpt( O_RDWR | O_NOCTTY | O_NDELAY ));
19  ASSERT_NE(-1, grantpt(master_fd));
20  ASSERT_NE(-1, unlockpt(master_fd));
21  ASSERT_TRUE((ser_name = ptsname(master_fd)) != NULL);
23  ser.open();
24  ASSERT_TRUE(ser.isOpen()) << "Couldn't open Serial connection to pseudoterminal.";
25  }
26 
27  void write_serial(const std::string& msg)
28  {
29  write(master_fd, msg.c_str(), msg.length());
30  }
31 
32  virtual void TearDown()
33  {
34  ser.close();
35  close(master_fd);
36  }
37 
39 
40 private:
41  int master_fd;
42  char* ser_name;
43 };
44 
45 TEST_F(FakeSerial, basic_message_rx)
46 {
47  // Send message from device which should write four bytes to the raw magnetometer's first register.
48  std::string msg(um7::Comms::message(DREG_MAG_RAW_XY, std::string("\x1\x2\x3\x4")));
49  write_serial(msg);
50 
51  um7::Comms sensor(&ser);
52  um7::Registers registers;
53  ASSERT_EQ(DREG_MAG_RAW_XY, sensor.receive(&registers)) << "Didn't return ID of arriving message.";
54  EXPECT_EQ(0x0102, registers.mag_raw.get(0));
55 }
56 
57 TEST_F(FakeSerial, batch_message_rx)
58 {
59  // Send message from device which should write four bytes to the raw accelerometer's registers.
60  std::string msg(um7::Comms::message(DREG_ACCEL_RAW_XY, std::string("\x5\x6\x7\x8\x9\xa\0\0", 8)));
61  write_serial(msg);
62 
63  um7::Comms sensor(&ser);
64  um7::Registers registers;
65  ASSERT_EQ(DREG_ACCEL_RAW_XY, sensor.receive(&registers)) << "Didn't return ID of arriving message.";
66  EXPECT_EQ(0x0506, registers.accel_raw.get(0));
67  EXPECT_EQ(0x0708, registers.accel_raw.get(1));
68  EXPECT_EQ(0x090a, registers.accel_raw.get(2));
69 }
70 
71 TEST_F(FakeSerial, bad_checksum_message_rx)
72 {
73  // Generate message, then twiddle final byte.
74  std::string msg(um7::Comms::message(DREG_MAG_RAW_XY, std::string("\x1\x2\x3\x4")));
75  msg[msg.length() - 1]++;
76  write_serial(msg);
77 
78  um7::Comms sensor(&ser);
79  um7::Registers registers;
80  EXPECT_EQ(-1, sensor.receive(&registers)) << "Didn't properly ignore bad checksum message.";
81 }
82 
83 TEST_F(FakeSerial, garbage_bytes_preceeding_message_rx)
84 {
85  // Generate message, then prepend junk.
86  std::string msg(um7::Comms::message(CONFIG_REG_START_ADDRESS, std::string()));
87  msg = "ssssssnsnsns" + msg;
88  write_serial(msg);
89 
90  um7::Comms sensor(&ser);
91  EXPECT_EQ(CONFIG_REG_START_ADDRESS, sensor.receive(NULL)) << "Didn't handle garbage prepended to message.";
92 }
93 
94 TEST_F(FakeSerial, timeout_message_rx)
95 {
96  std::string msg("snp\x12\x45");
97  write_serial(msg);
98  um7::Comms sensor(&ser);
99  EXPECT_EQ(-1, sensor.receive(NULL)) << "Didn't properly time out in the face of a partial message.";
100 }
101 
102 int main(int argc, char **argv)
103 {
104 testing::InitGoogleTest(&argc, argv);
105 return RUN_ALL_TESTS();
106 }
void write_serial(const std::string &msg)
Definition: test_comms.cpp:27
serial::Serial ser
Definition: test_comms.cpp:38
#define DREG_ACCEL_RAW_XY
#define CONFIG_REG_START_ADDRESS
const Accessor< int16_t > accel_raw
Definition: registers.h:187
const Accessor< int16_t > mag_raw
Definition: registers.h:187
Provides the Registers class, which initializes with a suite of accessors suitable for reading and wr...
RegT get(uint8_t field) const
Definition: registers.h:130
TEST_F(FakeSerial, basic_message_rx)
Definition: test_comms.cpp:45
int16_t receive(Registers *r)
Definition: comms.cpp:54
#define DREG_MAG_RAW_XY
bool isOpen() const
void setPort(const std::string &port)
virtual void TearDown()
Definition: test_comms.cpp:32
virtual void SetUp()
Definition: test_comms.cpp:16
Comms class definition. Does not manage the serial connection itself, but takes care of reading and w...
int main(int argc, char **argv)
Definition: test_comms.cpp:102
char * ser_name
Definition: test_comms.cpp:42
static std::string message(uint8_t address, std::string data)
Definition: comms.cpp:168


um7
Author(s): Mike Purvis , Alex Brown
autogenerated on Tue Feb 11 2020 03:26:50