~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to src/support/rig_io.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ----------------------------------------------------------------------------
 
2
// Copyright (C) 2014
 
3
//              David Freese, W1HKJ
 
4
//
 
5
// This file is part of flrig.
 
6
//
 
7
// flrig is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 3 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// flrig 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.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
// ----------------------------------------------------------------------------
 
20
 
 
21
#include <math.h>
 
22
#include <string>
 
23
 
 
24
#include <FL/Fl.H>
 
25
 
 
26
#include "rig.h"
 
27
#include "support.h"
 
28
#include "util.h"
 
29
#include "debug.h"
 
30
#include "status.h"
 
31
#include "rigbase.h"
 
32
#include "rig_io.h"
 
33
 
 
34
#include "socket_io.h"
 
35
 
 
36
using namespace std;
 
37
 
 
38
extern bool test;
 
39
 
 
40
int rig_nbr = 0;
 
41
 
 
42
const char *nuline = "\n";
 
43
 
 
44
static int iBaudRates[] = { 300, 600, 1200, 2400, 4800, 9600,
 
45
        19200, 38400, 57600, 115200, 230400, 460800 };
 
46
const char *szBaudRates[] = { "300", "600", "1200", "2400", "4800", "9600",
 
47
        "19200", "38400", "57600", "115200", "230400", "460800", NULL };
 
48
 
 
49
int BaudRate(int n)
 
50
{
 
51
        if (n > (int)sizeof(iBaudRates)) return 1200;
 
52
        return (iBaudRates[n]);
 
53
}
 
54
 
 
55
 
 
56
bool startXcvrSerial()
 
57
{
 
58
        bypass_serial_thread_loop = true;
 
59
// setup commands for serial port
 
60
        if (progStatus.xcvr_serial_port == "NONE") {
 
61
                bypass_serial_thread_loop = false;
 
62
                return false;
 
63
        }
 
64
 
 
65
        RigSerial.Device(progStatus.xcvr_serial_port);
 
66
        RigSerial.Baud(BaudRate(progStatus.comm_baudrate));
 
67
        RigSerial.Stopbits(progStatus.stopbits);
 
68
        RigSerial.Retries(progStatus.comm_retries);
 
69
        RigSerial.Timeout(progStatus.comm_timeout);
 
70
        RigSerial.RTSptt(progStatus.comm_rtsptt);
 
71
        RigSerial.DTRptt(progStatus.comm_dtrptt);
 
72
        RigSerial.RTSCTS(progStatus.comm_rtscts);
 
73
        RigSerial.RTS(progStatus.comm_rtsplus);
 
74
        RigSerial.DTR(progStatus.comm_dtrplus);
 
75
 
 
76
        if (!RigSerial.OpenPort()) {
 
77
                LOG_ERROR("Cannot access %s", progStatus.xcvr_serial_port.c_str());
 
78
                return false;
 
79
        } else if (debug::level == debug::DEBUG_LEVEL) {
 
80
                LOG_DEBUG("\n\
 
81
Serial port:\n\
 
82
    Port     : %s\n\
 
83
    Baud     : %d\n\
 
84
        Stopbits : %d\n\
 
85
    Retries  : %d\n\
 
86
    Timeout  : %d\n\
 
87
    Loop     : %d\n\
 
88
    RTSCTS   : %d\n\
 
89
    CATptt   : %d\n\
 
90
    RTSptt   : %d\n\
 
91
    DTRptt   : %d\n\
 
92
    RTS+     : %d\n\
 
93
    DTR+     : %d\n",
 
94
                        progStatus.xcvr_serial_port.c_str(),
 
95
                        progStatus.comm_baudrate,
 
96
                        progStatus.stopbits,
 
97
                        progStatus.comm_retries,
 
98
                        progStatus.comm_timeout,
 
99
                        progStatus.serloop_timing,
 
100
                        progStatus.comm_rtscts,
 
101
                        progStatus.comm_catptt,
 
102
                        progStatus.comm_rtsptt,
 
103
                        progStatus.comm_dtrptt,
 
104
                        progStatus.comm_rtsplus,
 
105
                        progStatus.comm_dtrplus );
 
106
        }
 
107
 
 
108
        RigSerial.FlushBuffer();
 
109
//      bypass_serial_thread_loop = false;
 
110
        return true;
 
111
}
 
112
 
 
113
bool startAuxSerial()
 
114
{
 
115
        if (progStatus.aux_serial_port == "NONE") return false;
 
116
 
 
117
        AuxSerial.Device(progStatus.aux_serial_port);
 
118
        AuxSerial.Baud(1200);
 
119
        AuxSerial.RTS(progStatus.aux_rts);
 
120
        AuxSerial.DTR(progStatus.aux_dtr);
 
121
 
 
122
        if (!AuxSerial.OpenPort()) {
 
123
                LOG_ERROR("Cannot access %s", progStatus.aux_serial_port.c_str());
 
124
                return false;
 
125
        }
 
126
        return true;
 
127
}
 
128
 
 
129
bool startSepSerial()
 
130
{
 
131
        if (progStatus.sep_serial_port == "NONE") return false;
 
132
 
 
133
        SepSerial.Device(progStatus.sep_serial_port);
 
134
        SepSerial.Baud(1200);
 
135
 
 
136
        SepSerial.RTSCTS(false);
 
137
        SepSerial.RTS(progStatus.sep_rtsplus);
 
138
        SepSerial.RTSptt(progStatus.sep_rtsptt);
 
139
 
 
140
        SepSerial.DTR(progStatus.sep_dtrplus);
 
141
        SepSerial.DTRptt(progStatus.sep_dtrptt);
 
142
 
 
143
        if (!SepSerial.OpenPort()) {
 
144
                LOG_ERROR("Cannot access %s", progStatus.sep_serial_port.c_str());
 
145
                return false;
 
146
        }
 
147
        return true;
 
148
}
 
149
 
 
150
char replybuff[RXBUFFSIZE+1];
 
151
string replystr;
 
152
 
 
153
int readResponse()
 
154
{
 
155
        int numread = 0;
 
156
        replystr.clear();
 
157
        memset(replybuff, 0, RXBUFFSIZE + 1);
 
158
        if (progStatus.use_tcpip)
 
159
                numread = read_from_remote(replystr);
 
160
        else {
 
161
                numread = RigSerial.ReadBuffer(replybuff, RXBUFFSIZE);
 
162
                for (int i = 0; i < numread; replystr += replybuff[i++]);
 
163
        }
 
164
        if (numread)
 
165
                LOG_DEBUG("rsp:%3d, %s", numread, str2hex(replystr.c_str(), replystr.length()));
 
166
        return numread;
 
167
}
 
168
 
 
169
int sendCommand (string s, int nread)
 
170
{
 
171
        int numwrite = (int)s.size();
 
172
 
 
173
        if (progStatus.use_tcpip) {
 
174
                readResponse();
 
175
                send_to_remote(s, progStatus.byte_interval);
 
176
                int timeout = 
 
177
                        progStatus.comm_wait + progStatus.tcpip_ping_delay +
 
178
                        (int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud() );
 
179
                while (timeout > 0) {
 
180
                        if (timeout > 10) MilliSleep(10);
 
181
                        else MilliSleep(timeout);
 
182
                        timeout -= 10;
 
183
                        Fl::awake();
 
184
                }
 
185
                if (nread == 0) return 0;
 
186
                return readResponse();
 
187
        }
 
188
 
 
189
        if (RigSerial.IsOpen() == false)
 
190
                return 0;
 
191
 
 
192
        LOG_DEBUG("cmd:%3d, %s", (int)s.length(), str2hex(s.data(), s.length()));
 
193
 
 
194
        clearSerialPort();
 
195
        RigSerial.WriteBuffer(s.c_str(), numwrite);
 
196
 
 
197
        if (nread == 0) return 0;
 
198
        int timeout = progStatus.comm_wait + 
 
199
                (int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud()
 
200
                + progStatus.use_tcpip ? progStatus.tcpip_ping_delay : 0);
 
201
        while (timeout > 0) {
 
202
                if (timeout > 10) MilliSleep(10);
 
203
                else MilliSleep(timeout);
 
204
                timeout -= 10;
 
205
                Fl::awake();
 
206
        }
 
207
        return readResponse();
 
208
}
 
209
 
 
210
bool waitCommand(
 
211
                                string command,
 
212
                                int nread,
 
213
                                string info,
 
214
                                int msec,
 
215
                                char term,
 
216
                                int how,
 
217
                                int level )
 
218
{
 
219
        int numwrite = (int)command.length();
 
220
        if (nread == 0)
 
221
                LOG_DEBUG("cmd:%3d, %s", numwrite, how == ASC ? command.c_str() : str2hex(command.data(), numwrite));
 
222
 
 
223
        if (progStatus.use_tcpip) {
 
224
                send_to_remote(command, progStatus.byte_interval);
 
225
                if (nread == 0) return 0;
 
226
        } else {
 
227
                if (RigSerial.IsOpen() == false) {
 
228
                        LOG_DEBUG("cmd: %s", how == ASC ? command.c_str() : str2hex(command.data(), command.length()));
 
229
                        return 0;
 
230
                }
 
231
                replystr.clear();
 
232
                clearSerialPort();
 
233
                RigSerial.WriteBuffer(command.c_str(), numwrite);
 
234
                if (nread == 0) return 0;
 
235
        }
 
236
 
 
237
// minimimum time to wait for a response
 
238
        int timeout = (int)((nread + progStatus.comm_echo ? numwrite : 0)*11000.0/RigSerial.Baud()
 
239
                + progStatus.use_tcpip ? progStatus.tcpip_ping_delay : 0);
 
240
        while (timeout > 0) {
 
241
                if (timeout > 10) MilliSleep(10);
 
242
                else MilliSleep(timeout);
 
243
                timeout -= 10;
 
244
                Fl::awake();
 
245
        }
 
246
// additional wait for xcvr processing
 
247
        string returned = "";
 
248
        static char sztemp[100];
 
249
        int waited = 0;
 
250
        while (waited < msec) {
 
251
                if (readResponse())
 
252
                        returned.append(replystr);
 
253
                if (    ((int)returned.length() >= nread) || 
 
254
                                (returned.find(term) != string::npos) ) {
 
255
                        replystr = returned;
 
256
                        snprintf(sztemp, sizeof(sztemp), "%s rcvd in %d msec", info.c_str(), waited + timeout);
 
257
                        showresp(level, how, sztemp, command, returned);
 
258
                        return true;
 
259
                }
 
260
                waited += 10;
 
261
                MilliSleep(10);
 
262
                Fl::awake();
 
263
        }
 
264
        replystr = returned;
 
265
        snprintf(sztemp, sizeof(sztemp), "%s TIMED OUT in %d ms",  info.c_str(), waited + timeout);
 
266
        showresp(ERR, how, sztemp, command, returned);
 
267
        return false;
 
268
}
 
269
 
 
270
int waitResponse(int timeout)
 
271
{
 
272
        int n = 0;
 
273
        if (!progStatus.use_tcpip && RigSerial.IsOpen() == false)
 
274
                return 0;
 
275
 
 
276
        MilliSleep(10);
 
277
        if (!(n = readResponse()))
 
278
                while (timeout > 0) {
 
279
                        if (timeout > 10) MilliSleep(10);
 
280
                        else MilliSleep(timeout);
 
281
                        timeout -= 10;
 
282
                        Fl::awake();
 
283
                }
 
284
        return n;
 
285
}
 
286
 
 
287
void clearSerialPort()
 
288
{
 
289
        if (RigSerial.IsOpen() == false) return;
 
290
        RigSerial.FlushBuffer();
 
291
}
 
292
 
 
293
void showresp(int level, int how, string s, string tx, string rx) 
 
294
{
 
295
        time_t now;
 
296
        time(&now);
 
297
        struct tm *local = localtime(&now);
 
298
        char sztm[20];
 
299
        strftime(sztm, sizeof(sztm), "%H:%M:%S", local);
 
300
 
 
301
        string s1 = how == HEX ? str2hex(tx.c_str(), tx.length()) : tx;
 
302
        string s2 = how == HEX ? str2hex(rx.c_str(), rx.length()) : rx;
 
303
        if (how == ASC) {
 
304
                size_t p;
 
305
                while((p = s1.find('\r')) != string::npos)
 
306
                        s1.replace(p, 1, "<cr>");
 
307
                while((p = s1.find('\n')) != string::npos)
 
308
                        s1.replace(p, 1, "<lf>");
 
309
                while((p = s2.find('\r')) != string::npos)
 
310
                        s2.replace(p, 1, "<cr>");
 
311
                while((p = s2.find('\n')) != string::npos)
 
312
                        s2.replace(p, 1, "<lf>");
 
313
        }
 
314
 
 
315
        switch (level) {
 
316
        case QUIET:
 
317
                SLOG_QUIET("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
 
318
                break;
 
319
        case ERR:
 
320
                SLOG_ERROR("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
 
321
                break;
 
322
        case WARN:
 
323
                SLOG_WARN("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
 
324
                break;
 
325
        case INFO:
 
326
                SLOG_INFO("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
 
327
                break;
 
328
        default:
 
329
                SLOG_DEBUG("%s: %10s\ncmd %s\nans %s", sztm, s.c_str(), s1.c_str(), s2.c_str());
 
330
        }
 
331
}