Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / FaultInjectionPort.hpp
blobd2ace4059eac3443628fd5b4e08cc257bc36a889
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Device/Port/Port.hpp"
25 #include <algorithm>
26 #include <assert.h>
27 #include <stdio.h>
29 static unsigned inject_port_fault;
31 class FaultInjectionPort : public Port {
32 public:
33 enum {
34 DEFAULT_BAUD_RATE = 1234,
37 bool running;
38 unsigned baud_rate;
40 FaultInjectionPort(DataHandler &_handler)
41 :Port(_handler), running(true), baud_rate(DEFAULT_BAUD_RATE) {}
43 /* virtual methods from class Port */
44 virtual PortState GetState() const override {
45 return inject_port_fault > 0
46 ? PortState::READY
47 : PortState::FAILED;
50 virtual size_t Write(const void *data, size_t length) override {
51 return length;
54 virtual bool Drain() override {
55 return true;
58 virtual void Flush() override {}
60 virtual unsigned GetBaudrate() const override {
61 return baud_rate;
64 virtual bool SetBaudrate(unsigned _baud_rate) override {
65 baud_rate = _baud_rate;
66 return true;
69 virtual bool StopRxThread() override {
70 running = false;
71 return true;
74 virtual bool StartRxThread() override {
75 running = true;
76 return true;
79 virtual int Read(void *Buffer, size_t Size) override {
80 if (inject_port_fault == 0)
81 return -1;
83 --inject_port_fault;
84 char *p = (char *)Buffer;
85 std::fill(p, p + Size, ' ');
86 return Size;
89 virtual WaitResult WaitRead(unsigned timeout_ms) override {
90 return inject_port_fault > 0
91 ? WaitResult::READY
92 : WaitResult::FAILED;
96 Port::Port(DataHandler &_handler)
97 :handler(_handler) {}
99 Port::~Port() {}
101 bool
102 Port::WaitConnected(OperationEnvironment &env)
104 return GetState() == PortState::READY;
107 size_t
108 Port::Write(const char *s)
110 return Write(s, strlen(s));
113 bool
114 Port::FullWrite(const void *buffer, size_t length,
115 OperationEnvironment &env, unsigned timeout_ms)
117 return Write(buffer, length) == length;
120 bool
121 Port::FullWriteString(const char *s,
122 OperationEnvironment &env, unsigned timeout_ms)
124 return FullWrite(s, strlen(s), env, timeout_ms);
128 Port::GetChar()
130 unsigned char ch;
131 return Read(&ch, sizeof(ch)) == sizeof(ch)
132 ? ch
133 : EOF;
136 bool
137 Port::FullFlush(OperationEnvironment &env, unsigned timeout_ms,
138 unsigned total_timeout_ms)
140 Flush();
141 return true;
144 bool
145 Port::FullRead(void *buffer, size_t length, OperationEnvironment &env,
146 unsigned timeout_ms)
148 return Read(buffer, length) == (int)length;
151 Port::WaitResult
152 Port::WaitRead(OperationEnvironment &env, unsigned timeout_ms)
154 return WaitRead(timeout_ms);
157 bool
158 Port::ExpectString(const char *token,
159 OperationEnvironment &env, unsigned timeout_ms)
161 if (inject_port_fault == 0)
162 return false;
164 --inject_port_fault;
165 return true;
168 Port::WaitResult
169 Port::WaitForChar(const char token, OperationEnvironment &env,
170 unsigned timeout_ms)
172 return inject_port_fault > 0 ? WaitResult::READY : WaitResult::FAILED;