forked from pretorean/DrvFR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.cpp
378 lines (351 loc) · 10.4 KB
/
conn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/***************************************************************************
conn.cpp - description
-------------------
begin : Sat Jan 12 2002
copyright : (C) 2002 by Igor V. Youdytsky
email : Pitcher@gw.tander.ru
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include "conn.h"
#include "drvfr.h"
#ifdef __APPLE__
#ifndef TEMP_FAILURE_RETRY
# define TEMP_FAILURE_RETRY(expression) \
(__extension__ \
({ long int __result; \
do __result = (long int) (expression); \
while (__result == -1L && errno == EINTR); \
__result; }))
#endif
#endif
/**********************************************************
* Local functions & procedures *
**********************************************************/
int sendNAK(void);
int sendACK(void);
int sendENQ(void);
int determsp(int);
int set_up_tty (int);
int input_timeout(int);
int composecomm(command*, int, unsigned char*);
void settimeout(int);
const char* devname(int number);
unsigned short int readbyte(int);
unsigned short int LRC(unsigned char*, int, int);
void PrintComm(command*);
/**********************************************************
* Local static variables *
**********************************************************/
static int devfile, fdflags, connected;
static fd_set set;
static fr_prop *prop;
static struct timeval timeout;
/**********************************************************
* Implementation of procedures *
**********************************************************/
#ifdef DEBUG
void PrintComm(command* cmd)
{
char *s;
char *t;
s = (char*)malloc(cmd->len*3*3);
t = (char*)malloc(5);
memset(s, 0, cmd->len*3*3);
for(int i = 0; i < cmd->len; i++)
{
sprintf(t, "%02X ",(int)cmd->buff[i]);
s = strcat(s,t);
}
perror(s);
free(s);
free(t);
};
#endif
//--------------------------------------------------------------------------------------
void settimeout(int msec)
{
if(msec <= 1000)
{
timeout.tv_sec = 0;
timeout.tv_usec = msec * 1000;
}
else
{
timeout.tv_sec = msec/1000;
timeout.tv_usec = (msec - timeout.tv_sec * 1000) * 1000;
};
}
//--------------------------------------------------------------------------------------
int opendev(fr_prop *f)
{
prop = f;
while ((devfile = open(devname(prop->ComPortNumber), O_NONBLOCK | O_RDWR, 0)) < 0)
if (errno != EINTR) return -1;
if ((fdflags = fcntl(devfile, F_GETFL)) == -1 || fcntl(devfile, F_SETFL, fdflags & ~O_NONBLOCK) < 0) return -1;
set_up_tty(devfile);
FD_ZERO (&set);
FD_SET (devfile, &set);
settimeout(prop->Timeout);
return 1;
}
//--------------------------------------------------------------------------------------
int set_up_tty (int tty_fd)
{
int speed;
struct termios tios;
if (tcgetattr(tty_fd, &tios) < 0) return -1;
tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
tios.c_cflag |= CS8 | CREAD | HUPCL;
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
tios.c_cc[VMIN] = 1;
tios.c_cc[VTIME] = 0;
tios.c_cflag &= ~CRTSCTS;
speed = LineSpeedVal[prop->BaudRate];
if (speed)
{
cfsetospeed (&tios, speed);
cfsetispeed (&tios, speed);
}
if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0) return -1;
return 1;
}
//--------------------------------------------------------------------------------------
int determsp(int spval)
{
for(int i=0; i<7; i++) if((unsigned)spval == LineSpeedVal[i]) return i;
return 0;
}
//--------------------------------------------------------------------------------------
int closedev(void)
{
return close(devfile);
}
//--------------------------------------------------------------------------------------
int input_timeout(int msec)
{
if(msec > 0 ) settimeout(msec);
connected = TEMP_FAILURE_RETRY (select (FD_SETSIZE, &set, NULL, NULL, &timeout));
return connected;
}
//--------------------------------------------------------------------------------------
unsigned short int LRC(unsigned char *str, int len, int offset = 0)
{
int i;
unsigned char *ptr;
unsigned char ch = 0;
ptr = str + offset;
for(i=0; i<len; i++)ch ^= ptr[i];
return ch;
}
//--------------------------------------------------------------------------------------
int sendNAK(void)
{
char buff[2];
buff[0] = NAK;
return write(devfile,buff,1);
}
//--------------------------------------------------------------------------------------
int sendACK(void)
{
char buff[2];
buff[0] = ACK;
return write(devfile,buff,1);
}
//--------------------------------------------------------------------------------------
int sendENQ(void)
{
char buff[2];
buff[0] = ENQ;
return write(devfile,buff,1);
}
//--------------------------------------------------------------------------------------
int composecomm(command *cmd, int comm, int pass, parameter *param)
{
int len;
len = commlen[comm];
if(len >= 5 && param->len > (len - 5)) param->len = len - 5;
cmd->buff[0] = STX;
cmd->buff[1] = len;
cmd->buff[2] = comm;
if(len >= 5)
{
memcpy(cmd->buff + 3, &pass, sizeof(int));
if(param->len > 0) memcpy(cmd->buff + 7, param->buff, param->len);
};
cmd->buff[len + 2] = LRC(cmd->buff, len + 1, 1);
cmd->len = len + 3;
return 1;
}
//--------------------------------------------------------------------------------------
unsigned short int readbyte(int msec = prop->Timeout)
{
unsigned char readbuff[2] = "";
if(input_timeout(msec) == 0) return 0;
if(read(devfile, readbuff, 1) > 0) return (unsigned int) readbuff[0];
else return 0;
}
//--------------------------------------------------------------------------------------
int readbytes(unsigned char *buff, int len)
{
int i;
for(i = 0; i < len; i++)
{
if(input_timeout(prop->Timeout) == 1)
if(read(devfile, buff+i, 1) == -1) return -1;
};
return len;
}
//--------------------------------------------------------------------------------------
int checkstate(void)
{
short int repl;
sendENQ();
repl = readbyte(1000);
if(connected == 0)return -1;
switch(repl)
{
case NAK:
return NAK;
case ACK:
return ACK;
default:
return -1;
};
};
//--------------------------------------------------------------------------------------
int sendcommand(int comm, int pass, parameter *param)
{
short int repl, tries;
command cmd;
composecomm(&cmd, comm, pass, param);
#ifdef DEBUG
PrintComm(&cmd);
#endif
answer a;
tries = 1;
bool flag = false;
while(tries < MAX_TRIES && !flag)
{
int state = checkstate();
switch(state)
{
case NAK:
flag = true;
break;
case ACK:
readanswer(&a);
//clearanswer();
flag = true;
break;
case -1:
tries++;
};
};
if (!flag)
return -1;
for(tries = 1; tries <= MAX_TRIES; tries++)
{
if(write(devfile,cmd.buff,cmd.len) != -1)
{
repl = readbyte(prop->Timeout * 100);
if(connected != 0)
{
if(repl == ACK) return 1;
};
};
};
return -1;
};
//--------------------------------------------------------------------------------------
int readanswer(answer *ans)
{
short int len, crc, tries;
for(tries = 0; tries < MAX_TRIES; tries++)
{
short int repl = readbyte(prop->Timeout * 100);
if(connected == 1)
{
if(repl == STX)
{
len = readbyte();
if(connected == 1)
{
if(readbytes(ans->buff, len) == len)
{
crc = readbyte();
if(connected != 0)
{
if(crc == (LRC(ans->buff, len, 0) ^ len))
{
sendACK();
ans->len = len;
return len;
}
else
sendNAK();
}
}
}
}
}
sendENQ();
repl = readbyte(prop->Timeout * 2);
if(connected != 1 || repl != ACK) tries++;
};
return -1;
}
//--------------------------------------------------------------------------------------
int clearanswer(void)
{
short int len;
unsigned char buf[0x100];
sendENQ();
len = readbytes(buf, 0x100);
if(connected == 1 && len > 0)
{
if(buf[0] != NAK)
{
sendACK();
return 1;
};
};
return 0;
}
//--------------------------------------------------------------------------------------
const char* devname(int number)
{
const char *devn[6] =
{
"/dev/null",
"/dev/ttyS0",
"/dev/ttyS1",
"/dev/ttyS2",
"/dev/ttyS3",
"/dev/ttyUSB0"
};
if(number > 0 && number < 6) return devn[number];
return devn[0];
}
//--------------------------------------------------------------------------------------