-
Notifications
You must be signed in to change notification settings - Fork 0
/
usart_termios_test.cpp
193 lines (143 loc) · 5.02 KB
/
usart_termios_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (C) 2018 Sergey Kapustin <kapucin@gmail.com>
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// SYSTEM INCLUDES
#include <gtest/gtest.h>
#include <thread>
#include <chrono>
// PROJECT INCLUDES
#include "devices/defines.hpp"
#include "devices/x86/usart_termios.hpp"
#include "devices/x86/pseudo_tty.hpp"
#include "utility/buff.hpp"
#include "utility/test_helpers.hpp"
using namespace std::chrono;
using namespace std::chrono_literals;
namespace btr
{
#define BAUD 115200
#define DATA_BITS 8
//------------------------------------------------------------------------------
class UsartTermiosTest : public testing::Test
{
public:
// LIFECYCLE
UsartTermiosTest()
:
tty_(),
reader_(),
sender_(),
wbuff_(),
rbuff_()
{
// On occasion, readWriteOK test would get bad file descriptor. One reason could be because
// of unfinished port set up in tty_ constructor. Add a bit of sleep for now.
std::this_thread::sleep_for(20ms);
reader_.configure(TTY_SIM_0, BAUD, DATA_BITS, ParityType::NONE, BTR_USART_IO_TIMEOUT_MS);
sender_.configure(TTY_SIM_1, BAUD, DATA_BITS, ParityType::NONE, BTR_USART_IO_TIMEOUT_MS);
reader_.open();
sender_.open();
resetBuffers();
}
void resetBuffers()
{
uint8_t h[] = { 'h','e','l','l','o' };
wbuff_.reset();
wbuff_.resize(sizeof(h));
wbuff_.write(h, sizeof(h)/sizeof(uint8_t));
// Don't expect to receive endline character(s)
rbuff_.reset();
rbuff_.resize(wbuff_.size());
}
protected:
// ATTRIBUTES
PseudoTTY tty_;
UsartTermios reader_;
UsartTermios sender_;
Buff wbuff_;
Buff rbuff_;
};
//------------------------------------------------------------------------------
// Tests {
TEST_F(UsartTermiosTest, readWriteOK)
{
ssize_t rc = sender_.send((char*)wbuff_.read_ptr(), wbuff_.available());
ASSERT_EQ(5, rc) << " Message: " << strerror(errno);
rc = reader_.recv((char*)rbuff_.write_ptr(), rbuff_.remaining());
ASSERT_EQ(5, rc) << " Message: " << strerror(errno);
ASSERT_EQ(0, memcmp(wbuff_.data(), rbuff_.data(), wbuff_.size()));
TEST_MSG << TestHelpers::toHex(rbuff_);
}
TEST_F(UsartTermiosTest, flush)
{
ssize_t rc = sender_.send((char*)wbuff_.read_ptr(), wbuff_.available(), true);
ASSERT_EQ(5, rc) << " Message: " << strerror(errno);
std::this_thread::sleep_for(20ms);
rc = reader_.available();
ASSERT_EQ(5, rc);
rc = reader_.flush(DirectionType::IN);
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
rc = reader_.available();
ASSERT_EQ(0, rc);
rc = reader_.recv((char*)rbuff_.write_ptr(), rbuff_.remaining());
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
resetBuffers();
rc = sender_.send((char*)wbuff_.read_ptr(), wbuff_.available());
ASSERT_EQ(5, rc) << " Message: " << strerror(errno);
rc = reader_.recv((char*)rbuff_.write_ptr(), rbuff_.remaining());
ASSERT_EQ(5, rc) << " Message: " << strerror(errno);
ASSERT_EQ(0, memcmp(wbuff_.data(), rbuff_.data(), wbuff_.size())) << TestHelpers::toHex(rbuff_);
}
TEST_F(UsartTermiosTest, readTimeout)
{
high_resolution_clock::time_point start = high_resolution_clock::now();
ssize_t rc = reader_.recv((char*)rbuff_.write_ptr(), rbuff_.remaining());
high_resolution_clock::time_point now = high_resolution_clock::now();
auto elapsed = duration_cast<milliseconds>(now - start).count();
ASSERT_LE(BTR_USART_IO_TIMEOUT_MS, elapsed);
ASSERT_GT(BTR_USART_IO_TIMEOUT_MS + 20, elapsed);
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
}
TEST_F(UsartTermiosTest, setTimeout)
{
uint32_t timeout = 200;
reader_.setTimeout(timeout);
high_resolution_clock::time_point start = high_resolution_clock::now();
ssize_t rc = reader_.recv((char*)rbuff_.write_ptr(), rbuff_.remaining());
high_resolution_clock::time_point now = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(now - start).count();
ASSERT_LE((timeout - 10), duration);
ASSERT_GT((timeout + 10), duration);
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
}
#if 0
TEST_F(UsartTermiosTest, DISABLED_sendBreak)
{
ssize_t rc = reader_.sendBreak(0);
std::this_thread::sleep_for(20ms);
rc = reader_.available();
ASSERT_EQ(1, rc);
uint32_t timeout = 200;
reader_.setTimeout(timeout);
high_resolution_clock::time_point start = high_resolution_clock::now();
rc = reader_.recv((char*)rbuff_.write_ptr(), 1);
high_resolution_clock::time_point now = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(now - start).count();
ASSERT_LE(duration, 10);
ASSERT_GT(10, duration);
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
}
#endif
TEST_F(UsartTermiosTest, DISABLED_WriteTimeout)
{
#if 0
// FIXME: Write time-out simulation doesn't work.
Buff large_buff;
large_buff.resize(65536);
int e = sender_.send(&large_buff);
ASSERT_EQ(0, rc) << " Message: " << strerror(errno);
#endif
}
// } Tests
} // namespace btr