1 /*
2  * The contents of this file are subject to the Mozilla Public License
3  * Version 1.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9  * License for the specific language governing rights and limitations
10  * under the License.
11  *
12  * The Initial Developer of this code is David Baum.
13  * Portions created by David Baum are Copyright (C) 1998 David Baum.
14  * All Rights Reserved.
15  *
16  * Portions created by John Hansen are Copyright (C) 2005 John Hansen.
17  * All Rights Reserved.
18  *
19  */
20 
21 #ifndef __RCX_PipeTransport_h
22 #define __RCX_PipeTransport_h
23 
24 #ifndef __RCX_Transport_h
25 #include "RCX_Transport.h"
26 #endif
27 
28 #ifndef __RCX_Pipe_h
29 #include "RCX_Pipe.h"
30 #endif
31 
32 
33 class RCX_PipeTransport : public RCX_Transport
34 {
35 public:
36 	enum
37 	{
38 		kMinTimeout = 50,
39 		kMaxTimeout = 500
40 	};
41 
42 
43 			RCX_PipeTransport(RCX_Pipe *pipe);
44 	virtual		~RCX_PipeTransport();
45 
46 
47 	virtual RCX_Result 	Open(RCX_TargetType target, const char *deviceName, ULong options);
48 	virtual void		Close();
49 
50 	virtual RCX_Result	Send(const UByte *txData, int txLength, UByte *rxData, int rxExpected, int rxMax, bool retry, int timeout);
51 
52 	virtual bool		FastModeSupported() const;
FastModeOddParity()53 	virtual bool		FastModeOddParity() const { return fPipe->GetCapabilities() & RCX_Pipe::kFastOddParityFlag; }
54 	virtual void		SetFastMode(bool fast);
GetFastMode()55         virtual bool            GetFastMode() const { return fFastMode; }
GetComplementData()56         virtual bool            GetComplementData() const { return fComplementData; }
57 
58 	// the Receive() interface is still experiemental
59 	RCX_Result      Receive(UByte *data, int maxLength, bool echo);
60 
61 private:
62         int             ExpectedReceiveLen(const int rxExpected);
63 	void		BuildTxData(const UByte *data, int length, bool duplicateReduction);
64 	void		SendFromTxBuffer(int delay);
65 	RCX_Result	ReceiveReply(int rxExpected, int timeout, int &replyOffset);
66 	//int		ValidateRxData();  // obsolete
67 	int		FindReply(const int rxExpected, int &offset);
68 	void		CopyReply(UByte *dst, int offset, int length);
69 	void		AdjustTimeout(RCX_Result result, int attempt);
70 
71 	void		ProcessRxByte(UByte b);
72 	int		VerifyReply(const int rxExpected, const UByte *data, int length, UByte cmd);
73 
74 	RCX_Pipe*	fPipe;
75 	UByte*	        fTxData;
76 	int		fTxLength;
77 	UByte	        fTxLastCommand;
78 
79 	UByte*	        fRxData;
80 	int		fRxLength;
81 
82 	// these fields are used by the Receive() state machine
83 	int		fRxExpectedLength;
84 	int		fRxState;
85 
86 	// parameters that define packet formatting
87 	const UByte*	fSync;	// sync pattern for packets (depends on target and usb mode)
88 	bool		fComplementData;
89 
90 	bool		fVerbose;
91 	bool		fSynced;
92 	RCX_TargetType	fTarget;
93 	int		fRxTimeout;
94 	bool		fDynamicTimeout;
95 
96 	// use for fast download
97 	bool		fFastMode;
98 };
99 
100 
101 #endif
102