1 /*
2  *  serialport_loopback.h
3  *  PHD Guiding
4  *
5  *  Created by Bret McKee
6  *  Copyright (c) 2013 Bret McKee
7  *  All rights reserved.
8  *
9  *  This source code is distributed under the following "BSD" license
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions are met:
12  *    Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *    Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *    Neither the name of Bret McKee, Dad Dog Development, nor the names of its
18  *     Craig Stark, Stark Labs nor the names of its
19  *     contributors may be used to endorse or promote products derived from
20  *     this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include "phd.h"
37 
38 #ifdef USE_LOOPBACK_SERIAL
39 
GetSerialPortList(void)40 wxArrayString SerialPortLoopback::GetSerialPortList(void)
41 {
42     wxArrayString ret;
43 
44     try
45     {
46         DWORD res = 0;
47 
48         ret.Add("Loopback 1");
49     }
50     catch (const wxString& Msg)
51     {
52         POSSIBLY_UNUSED(Msg);
53     }
54 
55     return ret;
56 }
57 
SerialPortLoopback(void)58 SerialPortLoopback::SerialPortLoopback(void)
59 {
60     m_data = 0;
61 }
62 
~SerialPortLoopback(void)63 SerialPortLoopback::~SerialPortLoopback(void)
64 {
65 }
66 
Connect(const wxString & portName,int baud,int dataBits,int stopBits,PARITY Parity,bool useRTS,bool useDTR)67 bool SerialPortLoopback::Connect(const wxString& portName, int baud, int dataBits, int stopBits, PARITY Parity, bool useRTS, bool useDTR)
68 {
69     bool bError = false;
70 
71     try
72     {
73     }
74     catch (const wxString& Msg)
75     {
76         POSSIBLY_UNUSED(Msg);
77         bError = true;
78     }
79 
80     return bError;
81 }
82 
Disconnect(void)83 bool SerialPortLoopback::Disconnect(void)
84 {
85     bool bError = false;
86 
87     try
88     {
89     }
90     catch (const wxString& Msg)
91     {
92         POSSIBLY_UNUSED(Msg);
93         bError = true;
94     }
95 
96     return bError;
97 }
98 
SetReceiveTimeout(int timeoutMs)99 bool SerialPortLoopback::SetReceiveTimeout(int timeoutMs)
100 {
101     bool bError = false;
102 
103     try
104     {
105     }
106     catch (const wxString& Msg)
107     {
108         POSSIBLY_UNUSED(Msg);
109         bError = true;
110     }
111 
112     return bError;
113 }
114 
Send(const unsigned char * pData,unsigned count)115 bool SerialPortLoopback::Send(const unsigned char *pData, unsigned count)
116 {
117     bool bError = false;
118 
119     try
120     {
121         m_data = *pData;
122     }
123     catch (const wxString& Msg)
124     {
125         POSSIBLY_UNUSED(Msg);
126         bError = true;
127     }
128 
129     return bError;
130 }
131 
Receive(unsigned char * pData,unsigned count)132 bool SerialPortLoopback::Receive(unsigned char *pData, unsigned count)
133 {
134     bool bError = false;
135 
136     try
137     {
138         if (count == 3)
139         {
140             char *pVersion = "999";
141 
142             memcpy(pData, pVersion, strlen(pVersion));
143         }
144         else
145         {
146             if (m_data == 0)
147             {
148                 throw ERROR_INFO("not enough characters");
149             }
150 
151             if (m_data == 'R')
152             {
153                 m_data = 'K';
154             }
155 
156             *pData = m_data;
157         }
158     }
159     catch (const wxString& Msg)
160     {
161         POSSIBLY_UNUSED(Msg);
162         bError = true;
163     }
164 
165     return bError;
166 }
167 
168 #endif // USE_LOOPBACK_SERIAL
169