-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
454 lines (401 loc) · 8.42 KB
/
main.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* msTERM
*
* Copyright (c) 2019 joshua stein <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mailstation.h"
#include "logo.h"
unsigned char last_key;
unsigned char esc;
unsigned char old_minutes;
unsigned char obuf_sent_pos;
unsigned char last_modem_msr;
#define MODEM_MSR_DCD (1 << 7)
int process_keyboard(void);
void process_input(unsigned char b);
void update_clock(void);
void update_f1(void);
void obuf_flush(void);
void wifi_hangup(void);
unsigned char source;
enum {
STATUSBAR_F1,
STATUSBAR_F2,
STATUSBAR_F3,
STATUSBAR_F4,
STATUSBAR_F5,
STATUSBAR_INIT,
};
void
obuf_queue(unsigned char *c)
{
unsigned char x;
for (x = 0; c[x] != '\0'; x++)
obuf[obuf_pos++] = c[x];
}
int
main(void)
{
unsigned char ms[10];
unsigned char shown_logo;
int b, j;
/* ignore first peekkey() if it returns power button */
last_key = KEY_POWER;
esc = 0;
putchar_sgr = 0;
in_csi = 0;
csibuflen = 0;
obuf_pos = 0;
obuf_sent_pos = 0;
debug0 = 0;
shown_logo = 0;
patch_isr();
settings_read();
source = setting_default_source;
if (source >= SOURCE_LAST)
source = SOURCE_WIFI;
clear_screen_bufs();
clear_screen();
update_statusbar(STATUSBAR_INIT, NULL);
begin:
if (source == SOURCE_WIFI)
/* call this early to sleep while we draw the logo */
wifi_init();
if (!shown_logo) {
/* - 1 to ignore final null byte */
for (b = 0; b < sizeof(logo) - 1; b++) {
if (b == 0 || logo[b - 1] == '\n') {
/* center without wasting space in logo[] */
for (j = 0; j < 14; j++)
putchar(' ');
}
putchar(logo[b]);
}
printf(" v%u\n\n", msTERM_version);
shown_logo = 1;
}
old_minutes = 0xff;
update_clock();
update_f1();
switch (source) {
case SOURCE_MODEM:
modem_init();
/* Restore factory configuration 0 */
obuf_queue("AT&F0");
/* Select modulation - V.34, automode, min_rate */
obuf_queue("+MS=11,1,300,");
/* max_rate */
itoa(setting_modem_speed, ms, 10);
obuf_queue(ms);
/* Turn speaker on */
obuf_queue("M1");
/* Set low speaker volume */
obuf_queue("L0");
/* Allow result codes to DTE */
obuf_queue("Q0");
/* Enable transparent XON/XOFF flow control */
obuf_queue("&K5\r");
break;
case SOURCE_WIFI:
obuf_queue("\rAT\r");
obuf_flush();
break;
}
obuf_flush();
for (;;) {
b = process_keyboard();
if (b == KEY_F4)
/* we changed sources */
goto begin;
switch (source) {
case SOURCE_MODEM:
if (modem_msr() != last_modem_msr) {
update_f1();
last_modem_msr = modem_curmsr;
}
if (modem_lsr() & (1 << 0)) {
process_input(modem_read());
continue;
}
break;
case SOURCE_LPT:
b = lptrecv();
if (b <= 0xff) {
process_input(b & 0xff);
continue;
}
break;
case SOURCE_WIFI:
b = wifi_read();
if (b != -1) {
process_input(b & 0xff);
continue;
}
break;
}
if (obuf_sent_pos != obuf_pos)
obuf_flush();
update_clock();
}
return 0;
}
void
update_statusbar(char which, char *status, ...)
{
va_list args;
char tstatus[64];
char *result = NULL;
unsigned char i, l;
if (which == STATUSBAR_INIT) {
for (i = 0; i < LCD_COLS; i++)
putchar_attr(LCD_ROWS - 1, i,
((i + 1) % 13 == 0 ? '|' : ' '), ATTR_REVERSE);
return;
}
va_start(args, status);
l = vsprintf(tstatus, status, args);
va_end(args);
if (l > sizeof(tstatus)) {
new_mail(1);
panic();
}
for (i = 0; i < l; i++) {
putchar_attr(LCD_ROWS - 1, (which * 13) + i, tstatus[i],
ATTR_REVERSE);
}
}
void
update_clock(void)
{
static const char modem_s[] = "Modem";
static const char wifi_s[] = "WiFi ";
if (rtcminutes == old_minutes)
return;
update_statusbar(STATUSBAR_F4, " %s ",
(source == SOURCE_MODEM ? modem_s: wifi_s));
update_statusbar(STATUSBAR_F5, " %02d:%02d ",
(rtc10hours * 10) + rtchours,
(rtc10minutes * 10) + rtcminutes);
old_minutes = rtcminutes;
}
void
update_f1(void)
{
unsigned char s = 0;
if (source == SOURCE_MODEM) {
if (modem_curmsr & MODEM_MSR_DCD)
update_statusbar(STATUSBAR_F1, " Hangup ");
else
update_statusbar(STATUSBAR_F1, " No Carrier");
} else if (source == SOURCE_WIFI) {
update_statusbar(STATUSBAR_F1, " Hangup ");
}
}
int
process_keyboard(void)
{
unsigned char b;
b = peekkey();
/* this breaks key-repeat, but it's needed to debounce */
if (b == 0)
last_key = 0;
else if (b == last_key)
b = 0;
else
last_key = b;
if (b == 0)
return 0;
switch (b) {
case KEY_POWER:
/* not sure why the delay is needed to actually power down */
clear_screen();
delay(200);
powerdown();
break;
case KEY_F1:
if (source == SOURCE_MODEM) {
if (modem_curmsr & MODEM_MSR_DCD)
modem_hangup();
} else if (source == SOURCE_WIFI) {
wifi_hangup();
}
break;
case KEY_F4:
if (source == SOURCE_MODEM) {
printf("\nHanging up modem...\n");
modem_hangup();
modem_powerdown();
printf("\nSwitching to WiFiStation...\n");
source = SOURCE_WIFI;
} else if (source == SOURCE_WIFI) {
printf("\nDisconnecting WiFiStation...\n");
wifi_hangup();
printf("\nSwitching to modem...\n");
source = SOURCE_MODEM;
}
setting_default_source = source;
settings_write();
break;
case KEY_MAIN_MENU:
/* send escape */
obuf[obuf_pos++] = ESC;
break;
case KEY_EMAIL:
reboot();
break;
case KEY_PAGE_UP:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = '5';
obuf[obuf_pos++] = '~';
break;
case KEY_PAGE_DOWN:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = '6';
obuf[obuf_pos++] = '~';
break;
case KEY_UP:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = 'A';
break;
case KEY_DOWN:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = 'B';
break;
case KEY_LEFT:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = 'D';
break;
case KEY_RIGHT:
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = 'C';
break;
case KEY_SIZE:
redraw_screen();
update_f1();
break;
default:
if (b >= META_KEY_BEGIN)
return 0;
if (b == '\n')
b = '\r';
obuf[obuf_pos++] = b;
}
return b;
}
void
process_input(unsigned char b)
{
if (in_csi) {
if (csibuflen >= sizeof(csibuf) - 1) {
/* going to overflow, dump */
parseCSI();
in_csi = 0;
esc = 0;
csibuf[0] = '\0';
csibuflen = 0;
}
if (b == ESC) {
/* esc, maybe new csi, dump previous */
parseCSI();
in_csi = 0;
esc = 1;
csibuf[0] = '\0';
csibuflen = 0;
} else {
csibuf[csibuflen] = b;
csibuflen++;
parseCSI();
}
return;
}
switch (b) {
case 7: /* visual bell, ping 'new mail' light */
new_mail(1);
delay(150);
new_mail(0);
break;
case 9: /* tab */
while ((cursorx + 1) % 8 != 0)
putchar(' ');
break;
case 26: /* ^Z end of ansi */
break;
case ESC: /* esc */
if (esc)
/* our previous esc is literal */
putchar(b);
esc = 1;
break;
case 91: /* [ */
if (esc) {
esc = 0;
in_csi = 1;
break;
}
/* fall through */
default:
putchar(b);
}
}
void
obuf_flush(void)
{
int b;
while (obuf_sent_pos != obuf_pos) {
switch (source) {
case SOURCE_MODEM:
if (modem_lsr() & (1 << 5))
/* Transmitter Holding Register Empty */
modem_write(obuf[obuf_sent_pos++]);
break;
case SOURCE_LPT:
lptsend(obuf[obuf_sent_pos++]);
break;
case SOURCE_ECHO:
putchar(obuf[obuf_sent_pos++]);
break;
case SOURCE_WIFI:
if (wifi_write(obuf[obuf_sent_pos]) == -1) {
if ((b = wifi_read()) != -1)
process_input(b & 0xff);
} else
obuf_sent_pos++;
break;
}
}
}
void
wifi_hangup(void)
{
obuf[obuf_pos++] = '+';
obuf[obuf_pos++] = '+';
obuf[obuf_pos++] = '+';
obuf_flush();
delay(800);
obuf[obuf_pos++] = 'A';
obuf[obuf_pos++] = 'T';
obuf[obuf_pos++] = 'H';
obuf[obuf_pos++] = '\r';
obuf_flush();
}