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"
29 static unsigned inject_port_fault
;
31 class FaultInjectionPort
: public Port
{
34 DEFAULT_BAUD_RATE
= 1234,
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
50 virtual size_t Write(const void *data
, size_t length
) override
{
54 virtual bool Drain() override
{
58 virtual void Flush() override
{}
60 virtual unsigned GetBaudrate() const override
{
64 virtual bool SetBaudrate(unsigned _baud_rate
) override
{
65 baud_rate
= _baud_rate
;
69 virtual bool StopRxThread() override
{
74 virtual bool StartRxThread() override
{
79 virtual int Read(void *Buffer
, size_t Size
) override
{
80 if (inject_port_fault
== 0)
84 char *p
= (char *)Buffer
;
85 std::fill(p
, p
+ Size
, ' ');
89 virtual WaitResult
WaitRead(unsigned timeout_ms
) override
{
90 return inject_port_fault
> 0
96 Port::Port(DataHandler
&_handler
)
102 Port::WaitConnected(OperationEnvironment
&env
)
104 return GetState() == PortState::READY
;
108 Port::Write(const char *s
)
110 return Write(s
, strlen(s
));
114 Port::FullWrite(const void *buffer
, size_t length
,
115 OperationEnvironment
&env
, unsigned timeout_ms
)
117 return Write(buffer
, length
) == length
;
121 Port::FullWriteString(const char *s
,
122 OperationEnvironment
&env
, unsigned timeout_ms
)
124 return FullWrite(s
, strlen(s
), env
, timeout_ms
);
131 return Read(&ch
, sizeof(ch
)) == sizeof(ch
)
137 Port::FullFlush(OperationEnvironment
&env
, unsigned timeout_ms
,
138 unsigned total_timeout_ms
)
145 Port::FullRead(void *buffer
, size_t length
, OperationEnvironment
&env
,
148 return Read(buffer
, length
) == (int)length
;
152 Port::WaitRead(OperationEnvironment
&env
, unsigned timeout_ms
)
154 return WaitRead(timeout_ms
);
158 Port::ExpectString(const char *token
,
159 OperationEnvironment
&env
, unsigned timeout_ms
)
161 if (inject_port_fault
== 0)
169 Port::WaitForChar(const char token
, OperationEnvironment
&env
,
172 return inject_port_fault
> 0 ? WaitResult::READY
: WaitResult::FAILED
;