-
Notifications
You must be signed in to change notification settings - Fork 1
/
newtmnp.c
440 lines (396 loc) · 8.22 KB
/
newtmnp.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <stdio.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "newtmnp.h"
/* Constants definition */
uchar FrameStart[] = "\x16\x10\x02";
uchar FrameEnd[] = "\x10\x03";
uchar LRFrame[] = {
'\x17', /* Length of header */
'\x01', /* Type indication LR frame */
'\x02', /* Constant parameter 1 */
'\x01', '\x06', '\x01', '\x00', '\x00', '\x00', '\x00', '\xff',
/* Constant parameter 2 */
'\x02', '\x01', '\x02', /* Octet-oriented framing mode */
'\x03', '\x01', '\x01', /* k = 1 */
'\x04', '\x02', '\x40', '\x00', /* N401 = 64 */
'\x08', '\x01', '\x03' /* N401 = 256 & fixed LT, LA frames */
};
uchar LDFrame[] = {
'\x04', /* Length of header */
'\x02', /* Type indication LD frame */
'\x01', '\x01', '\xff'
};
int intNewtFd;
int InitNewtDev(NewtDevInfo *newtDevInfo)
{
int newtFd;
struct termios newtTty;
/* Install Ctrl-C function */
intNewtFd = -1;
if(signal(SIGINT, SigInt) == SIG_ERR)
ErrHandler("Error in setting up interrupt function!!");
/* Open the Newton device */
if((newtFd = open(newtDevInfo->devName, O_RDWR)) == -1)
return -1;
/* Get the current device settings */
tcgetattr(newtFd, &newtTty);
/* Change the device settings */
newtTty.c_iflag = IGNBRK | INPCK;
newtTty.c_oflag = 0;
newtTty.c_cflag = CREAD | CLOCAL | CS8 & ~PARENB & ~PARODD & ~CSTOPB;
newtTty.c_lflag = 0;
newtTty.c_cc[VMIN] = 1;
newtTty.c_cc[VTIME] = 0;
/* Select the communication speed */
switch(newtDevInfo->speed) {
#ifdef B2400
case 2400:
cfsetospeed(&newtTty, B2400);
cfsetispeed(&newtTty, B2400);
break;
#endif
#ifdef B4800
case 4800:
cfsetospeed(&newtTty, B4800);
cfsetispeed(&newtTty, B4800);
break;
#endif
#ifdef B9600
case 9600:
cfsetospeed(&newtTty, B9600);
cfsetispeed(&newtTty, B9600);
break;
#endif
#ifdef B19200
case 19200:
cfsetospeed(&newtTty, B19200);
cfsetispeed(&newtTty, B19200);
break;
#endif
#ifdef B38400
case 38400:
cfsetospeed(&newtTty, B38400);
cfsetispeed(&newtTty, B38400);
break;
#endif
#ifdef B57600
case 57600:
cfsetospeed(&newtTty, B57600);
cfsetispeed(&newtTty, B57600);
break;
#endif
#ifdef B115200
case 115200:
cfsetospeed(&newtTty, B115200);
cfsetispeed(&newtTty, B115200);
break;
#endif
#ifdef B230400
case 230400:
cfsetospeed(&newtTty, B230400);
cfsetispeed(&newtTty, B230400);
break;
#endif
default:
close(newtFd);
return -1;
}
/* Flush the device and restarts input and output */
tcflush(newtFd, TCIOFLUSH);
tcflow(newtFd, TCOON);
/* Update the new device settings */
tcsetattr(newtFd, TCSANOW, &newtTty);
/* Return with file descriptor */
intNewtFd = newtFd;
return newtFd;
}
void FCSCalc(ushort *fcsWord, unsigned char octet)
{
int i;
uchar pow;
pow = 1;
for(i = 0; i < 8; i++) {
if((((*fcsWord % 256) & 0x01) == 0x01) ^ ((octet & pow) == pow))
*fcsWord = (*fcsWord / 2) ^ 0xa001;
else
*fcsWord /= 2;
pow *= 2;
}
}
void SendFrame(int newtFd, uchar *head, uchar *info, int infoLen)
{
char errMesg[] = "Error in writing to Newton device, connection stopped!!";
int i;
ushort fcsWord;
uchar buf;
/* Initialize */
fcsWord = 0;
/* Send frame start */
if(write(newtFd, FrameStart, 3) < 0)
ErrHandler(errMesg);
/* Send frame head */
for(i = 0; i <= head[0]; i++) {
FCSCalc(&fcsWord, head[i]);
if(write(newtFd, &head[i], 1) < 0)
ErrHandler(errMesg);
if(head[i] == FrameEnd[0]) {
if(write(newtFd, &head[i], 1) < 0)
ErrHandler(errMesg);
}
}
/* Send frame information */
if(info != NULL) {
for(i = 0; i < infoLen; i++) {
FCSCalc(&fcsWord, info[i]);
if(write(newtFd, &info[i], 1) < 0)
ErrHandler(errMesg);
if(info[i] == FrameEnd[0]) {
if(write(newtFd, &info[i], 1) < 0)
ErrHandler(errMesg);
}
}
}
/* Send frame end */
if(write(newtFd, FrameEnd, 2) < 0)
ErrHandler(errMesg);
FCSCalc(&fcsWord, FrameEnd[1]);
/* Send FCS */
buf = fcsWord % 256;
if(write(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
buf = fcsWord / 256;
if(write(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
return;
}
void SendLTFrame(int newtFd, uchar seqNo, uchar *info, int infoLen)
{
uchar ltFrameHead[3] = {
'\x02', /* Length of header */
'\x04', /* Type indication LT frame */
};
ltFrameHead[2] = seqNo;
SendFrame(newtFd, ltFrameHead, info, infoLen);
return;
}
void SendLAFrame(int newtFd, uchar seqNo)
{
uchar laFrameHead[4] = {
'\x03', /* Length of header */
'\x05', /* Type indication LA frame */
'\x00', /* Sequence number */
'\x01' /* N(k) = 1 */
};
laFrameHead[2] = seqNo;
SendFrame(newtFd, laFrameHead, NULL, 0);
return;
}
int RecvFrame(int newtFd, unsigned char *frame)
{
char errMesg[] = "Error in reading from Newton device, connection stopped!!";
int state;
unsigned char buf;
unsigned short fcsWord;
int i;
/* Initialize */
fcsWord = 0;
i = 0;
/* Wait for head */
state = 0;
while(state < 3) {
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
switch(state) {
case 0:
if(buf == FrameStart[0])
state++;
break;
case 1:
if(buf == FrameStart[1])
state++;
else
state = 0;
break;
case 2:
if(buf == FrameStart[2])
state++;
else
state = 0;
break;
}
}
/* Wait for tail */
state = 0;
while(state < 2) {
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
switch(state) {
case 0:
if(buf == '\x10')
state++;
else {
FCSCalc(&fcsWord, buf);
if(i < MaxHeadLen + MaxInfoLen) {
frame[i] = buf;
i++;
}
else
return -1;
}
break;
case 1:
if(buf == '\x10') {
FCSCalc(&fcsWord, buf);
if(i < MaxHeadLen + MaxInfoLen) {
frame[i] = buf;
i++;
}
else
return -1;
state = 0;
}
else {
if(buf == '\x03') {
FCSCalc(&fcsWord, buf);
state++;
}
else
return -1;
}
break;
}
}
/* Check FCS */
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
if(fcsWord % 256 != buf)
return -1;
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
if(fcsWord / 256 != buf)
return -1;
if(frame[1] == '\x02')
ErrHandler("Newton device disconnected, connection stopped!!");
return 0;
}
int WaitLAFrame(int newtFd, uchar seqNo)
{
uchar frame[MaxHeadLen + MaxInfoLen];
do {
while(RecvFrame(newtFd, frame) < 0);
if(frame[1] == '\x04')
SendLAFrame(newtFd, frame[2]);
} while(frame[1] != '\x05');
if(frame[2] == seqNo)
return 0;
else
return -1;
}
int WaitLDFrame(int newtFd)
{
char errMesg[] = "Error in reading from Newton device, connection stopped!!";
int state;
unsigned char buf;
unsigned short fcsWord;
int i;
/* Initialize */
fcsWord = 0;
i = 0;
/* Wait for head */
state = 0;
while(state < 5) {
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
switch(state) {
case 0:
if(buf == FrameStart[0])
state++;
break;
case 1:
if(buf == FrameStart[1])
state++;
else
state = 0;
break;
case 2:
if(buf == FrameStart[2])
state++;
else
state = 0;
break;
case 3:
FCSCalc(&fcsWord, buf);
state++;
break;
case 4:
if(buf == '\x02') {
FCSCalc(&fcsWord, buf);
state++;
}
else {
state = 0;
fcsWord = 0;
}
break;
}
}
/* Wait for tail */
state = 0;
while(state < 2) {
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
switch(state) {
case 0:
if(buf == '\x10')
state++;
else
FCSCalc(&fcsWord, buf);
break;
case 1:
if(buf == '\x10') {
FCSCalc(&fcsWord, buf);
state = 0;
}
else {
if(buf == '\x03') {
FCSCalc(&fcsWord, buf);
state++;
}
else
return -1;
}
break;
}
}
/* Check FCS */
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
if(fcsWord % 256 != buf)
return -1;
if(read(newtFd, &buf, 1) < 0)
ErrHandler(errMesg);
if(fcsWord / 256 != buf)
return -1;
return 0;
}
void ErrHandler(char *errMesg)
{
fprintf(stderr, "\n");
fprintf(stderr, errMesg);
fprintf(stderr, "\n\n");
_exit(0);
}
void SigInt(int sigNo)
{
if(intNewtFd >= 0) {
/* Wait for all buffer sent */
tcdrain(intNewtFd);
SendFrame(intNewtFd, LDFrame, NULL, 0);
}
ErrHandler("User interrupted, connection stopped!!");
}