summaryrefslogtreecommitdiff
path: root/nuttx/configs/us7032evb1/shterm/shterm.c
blob: 0a1e143b1db2ed4630f9398999c1616eea836a44 (plain) (blame)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/****************************************************************************
 * config/us7032evb1/shterm/shterm.c
 *
 *   Copyright(C) 2008-2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <termios.h>
#include <fcntl.h>
#include <stdarg.h>
#include <errno.h>

/****************************************************************************
 * Definitions
 ****************************************************************************/

/* Size of the circular buffer used for interrupt I/O */

#define MAX_FILEPATH 255

#define ENQ          5
#define ACK          6

#define DEFAULT_BAUD 9600

#define dbg(format, arg...)  if (debug > 0) printconsole(format, ##arg)
#define vdbg(format, arg...) if (debug > 1) printconsole(format, ##arg)

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

static void sendfile(int fdtarg, char *filename, int verify);
static void receivefile(int fdtarg, char *filename);
static void getfilename(int fd, char *name);
static int readbyte(int fd, char *ch);
static void writebyte(int fd, char byte);
static void close_tty(void);
static void interrupt(int signo);
static void show_usage(const char *progname, int exitcode);

/****************************************************************************
 * Private Data
 ****************************************************************************/

static int debug = 0;
static int g_fd = -1;
static int g_fdnb = -1;
static FILE *g_logstream = NULL;
static const char g_dfttydev[] = "/dev/ttyS0";
static const char *g_ttydev = g_dfttydev;
static const char *g_logfile = 0;
static int g_baud = DEFAULT_BAUD;
static struct termios g_termios;

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: putconsole
 ****************************************************************************/

static void putconsole(char ch)
{
  if (g_logstream)
    {
      (void)putc(ch, g_logstream);
    }
  (void)putchar(ch);
}

/****************************************************************************
 * Name: flushconsole
 ****************************************************************************/

static void flushconsole(void)
{
  if (g_logstream)
    {
      (void)fflush(g_logstream);
    }
  (void)fflush(stdout);
}

/****************************************************************************
 * Name: printconsole
 ****************************************************************************/

static void printconsole(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  if (g_logstream)
    {
      (void)vfprintf(g_logstream, fmt, ap);
    }
  (void)vprintf(fmt, ap);
  va_end(ap);
}

/****************************************************************************
 * Name: sendfile
 ****************************************************************************/

static void sendfile(int fdtarg, char *filename, int verify)
{
  char chin;
  char chout;
  int  fdin;
  int  nbytes;
  int  ndots;
  int  ret;

  /* Source the source file */

  fdin = open(filename, O_RDONLY);
  if (fdin < 0)
    {
      fprintf(stderr, "ERROR: Failed to open '%s' for reading\n", filename);
      (void)writebyte(fdin, '>');
      return;
    }

  if (verify)
    {
      printconsole("Verifying file '%s':\n", filename);
    }
  else
    {
      printconsole("Loading file '%s':\n", filename);
    }
  flushconsole();

  /* This loop processes each byte from the source file */

  nbytes = 0;
  ndots  = 0;
  while ((ret = readbyte(fdin, &chout)) == 1)
    {
      /* If verbose debug is OFF, then output dots at a low rate */

      if (debug < 2)
        {
          if (++nbytes > 64)
            {
              nbytes = 0;
              putconsole('.');
              if (++ndots > 72)
                {
                   putconsole('\n');
                   ndots = 0;
                }
              flushconsole();
            }
        }

      /* If verbose debug is ON, dump everything */

      else if (chout == 'S')
        {
          printconsole("\n[%c", chout);
        }
      else if (isprint(chout))
        {
          printconsole("[%c", chout);
        }
      else
        {
          printconsole("[.");
        }

      /* Send the byte to the target */

      writebyte(fdtarg, chout);

      /* Get the response from the target.  Loop until the target responds
       * by either echoing the byte sent or by sending '>'
       */

      do
        {
          ret = readbyte(fdtarg, &chin);

          /* If verbose debug is ON, echo the response from the target */

          if (ret == 1 && debug >= 2)
            {
              if (chin != chout)
                {
                  if (isprint(chin))
                    {
                      putconsole(chin);
                    }
                  else
                    {
                      putconsole('.');
                    }
                }
              else
                {
                  putconsole(']');
                }
            }

          /* Check if the target is asking to terminate the transfer */

          if (ret == 1 && chin == '>')
            {
              close(fdin);
              writebyte(fdtarg, ACK);
              return;
            }
        }
      while (ret == 1 && chin != chout);
    }

  writebyte(fdtarg, '>');
  do
   {
     ret = readbyte(fdtarg, &chin);
   }
  while (ret == 1 && chin != ENQ);
  close(fdin);
  writebyte(fdtarg, ACK);
}

/****************************************************************************
 * Name: receivefile
 ****************************************************************************/

static void receivefile(int fdtarg, char *filename)
{
  char ch;
  int  fdout;
  int  nbytes;
  int  ndots;
  int  ret;

  fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fdout < 0)
    {
      fprintf(stderr, "ERROR: Failed to open '%s' for writing\n", filename);
      (void)writebyte(fdtarg, '>');
      return;
    }

  printconsole("Receiving file '%s':\n", filename);
  flushconsole();
  (void)writebyte(fdtarg, '+');

  /* Synchronize */

  do
    {
      ret = readbyte(fdtarg, &ch);
    }
  while (ret == 1 && ch != 'S' && ch != 'Q');

  nbytes = 0;
  ndots = 0;

  /* Receive the file */

  while (ret == 1)
    {
      /* Check for end-of-file */

      if (ch == '>')
        {
          close(fdout);
          return;
        }

      writebyte(fdout, ch);

      if (++nbytes > 256)
        {
          nbytes = 0;
          putconsole('.');
          if (++ndots > 72)
            {
               putconsole('\n');
               ndots = 0;
            }
            flushconsole();
        }

      ret = readbyte(fdtarg, &ch);
      if (ch == '\r')
        {
          writebyte(fdtarg, '+');
        }
    }

  close (fdout);
}

/****************************************************************************
 * Name: getfilename
 ****************************************************************************/

static void getfilename(int fd, char *name)
{
  char ch;
  int ret;

  /* Skip over spaces */

  do
    {
      ret = readbyte(fd, &ch);
    }
  while(ch == ' ' && ret == 1);

  /* Concatenate the filename */

  while(ret == 1 && ch > ' ')
    {
      *name++ = ch;
       ret = readbyte(fd, &ch);
    }
  *name++ = 0;
}

/****************************************************************************
 * Name: readbyte
 ****************************************************************************/

static int readbyte(int fd, char *ch)
{
  int ret;

  /* Read characters from the console, and echo them to the target tty */

  ret = read(fd, ch, 1);
  if(ret < 0)
    {
      if(errno != EAGAIN)
        {
          printconsole("ERROR: Failed to read from fd=%d: %s\n", fd, strerror(errno));
          close_tty();
          exit(12);
        }
      return -EAGAIN;
    }
  else if(ret > 1)
    {
      printconsole("ERROR: Unexpected number of bytes read(%d) from fd=%d\n", ret, fd);
      close_tty();
      exit(13);
    }
  return ret;
}

/****************************************************************************
 * Name: writebyte
 ****************************************************************************/

static void writebyte(int fd, char byte)
{
  int ret = write(fd, &byte, 1);
  if(ret < 0)
    {
      printconsole("ERROR: Failed to write to fd=%d: %s\n", fd, strerror(errno));
      close_tty();
      exit(14);
    }
}

/****************************************************************************
 * Name: close_tty
 ****************************************************************************/

static void close_tty(void)
{
  int ret;

  if (g_fdnb >= 0)
    {
      (void)close(g_fdnb);
    }

  if (g_fd >= 0)
    {
      ret = tcsetattr(g_fd, TCSANOW, &g_termios);
      if (ret < 0)
        {
          printconsole("ERROR: Failed to restore termios for %s: %s\n", g_ttydev, strerror(errno));
        }
      (void)close(g_fd);
    }

  if (g_logstream >= 0)
    {
      (void)fclose(g_logstream);
    }
}

/****************************************************************************
 * Name: interrupt
 ****************************************************************************/

static void interrupt(int signo)
{
  printconsole("Exit-ing...\n");
  close_tty();
  exit(0);
}

/****************************************************************************
 * Name: interrupt
 ****************************************************************************/

static void show_usage(const char *progname, int exitcode)
{
  fprintf(stderr, "\nUSAGE: %s [-h] [-d] [-t <ttyname>] [-b <baud>] [-l <log-file>]\n", progname);
  fprintf(stderr, "\nWhere:\n");
  fprintf(stderr, "\t-h: Prints this message then exit.\n");
  fprintf(stderr, "\t-d: Enable debug output (twice for verbose output).\n");
  fprintf(stderr, "\t-t <ttyname>:  Use <ttyname> device instead of %s.\n", g_dfttydev);
  fprintf(stderr, "\t-b <baud>: Use <baud> instead of %d.\n", DEFAULT_BAUD);
  fprintf(stderr, "\t-l <log-file>: Echo console output in <log-file>.\n");
  exit(exitcode);
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

int main(int argc, char **argv, char **envp)
{
  struct termios tty;
  char filename[MAX_FILEPATH];
  char ch;
  int  speed;
  int  opt;
  int  oflags;
  int  ret;

  while((opt = getopt(argc, argv, ":dt:b:hl:")) != -1)
    {
      switch(opt)
        {
        case 'd':
          debug++;
          break;

        case 't':
          g_ttydev = optarg;
          break;

        case 'b':
          g_baud = atoi(optarg);
          break;

        case 'h':
          show_usage(argv[0], 0);
          break;

        case 'l':
          g_logfile = optarg;
          break;

        case ':':
          fprintf(stderr, "ERROR: Missing argument to option '%c'\n", optopt);
          show_usage(argv[0], 1);
          break;

        case '?':
          fprintf(stderr, "ERROR: Unrecognized option '%c'\n", optopt);
          show_usage(argv[0], 2);
          break;
        }
    }

  if(optind < argc)
    {
      fprintf(stderr, "ERROR: Unexpected arguments at end of line\n");
      show_usage(argv[0], 3);
    }

  switch(g_baud)
    {
    case 0:      speed = B0;      break;
    case 50:     speed = B50;     break;
    case 75:     speed = B75;     break;
    case 110:    speed = B110;    break;
    case 134:    speed = B134;    break;
    case 150:    speed = B150;    break;
    case 200:    speed = B200;    break;
    case 300:    speed = B300;    break;
    case 600:    speed = B600;    break;
    case 1200:   speed = B1200;   break;
    case 1800:   speed = B1800;   break;
    case 2400:   speed = B2400;   break;
    case 4800:   speed = B4800;   break;
    case 9600:   speed = B9600;   break;
    case 19200:  speed = B19200;  break;
    case 38400:  speed = B38400;  break;
    case 57600:  speed = B57600;  break;
    case 115200: speed = B115200; break;
    case 230400: speed = B230400; break;

    default:
      fprintf(stderr, "ERROR: Unsupported BAUD=%d\n", g_baud);
      show_usage(argv[0], 4);
    }

  /* Was a log file specified? */

  if (g_logfile)
    {
      g_logstream = fopen(g_logfile, "w");
      if (!g_logstream)
        {
          fprintf(stderr, "ERROR: Failed to open '%s' for writing\n", g_logfile);
          return 5;
        }
    }

  /* Set the host stdin to O_NONBLOCK */

  oflags = fcntl(0, F_GETFL, 0);
  if(oflags == -1)
    {
      fprintf(stderr, "ERROR: fnctl(F_GETFL) failed: %s\n", strerror(errno));
      return 6;
    }

  ret = fcntl(0, F_SETFL, oflags | O_NONBLOCK);
  if(ret < 0)
    {
      fprintf(stderr, "ERROR: fnctl(F_SETFL) failed: %s\n", strerror(errno));
      return 7;
    }

  /* Open the selected serial port (blocking)*/

  g_fd = open(g_ttydev, O_RDWR);
  if(g_fd < 0)
    {
      printconsole("ERROR: Failed to open %s: %s\n", g_ttydev, strerror(errno));
      return 8;
    }

  /* Configure the serial port in at the selected baud in 8-bit, no-parity, raw mode
   * and turn off echo, etc.
   */

  ret = tcgetattr(g_fd, &g_termios);
  if(ret < 0)
    {
      printconsole("ERROR: Failed to get termios for %s: %s\n", g_ttydev, strerror(errno));
      close(g_fd);
      return 9;
    }

  memcpy(&tty, &g_termios, sizeof(struct termios));
  tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  tty.c_oflag &= ~OPOST;
  tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  tty.c_cflag &= ~(CSIZE|PARENB);
  tty.c_cflag |= CS8;

 (void)cfsetispeed(&tty, speed);
 (void)cfsetospeed(&tty, speed);

  ret = tcsetattr(g_fd, TCSANOW, &tty);
  if(ret < 0)
    {
      printconsole("ERROR: Failed to set termios for %s: %s\n", g_ttydev, strerror(errno));
      close(g_fd);
      return 10;
    }

#if 1
  /* Open the selected serial port (non-blocking)*/

  g_fdnb = open(g_ttydev, O_RDONLY | O_NONBLOCK);
  if(g_fdnb < 0)
    {
      printconsole("ERROR: Failed to open %s: %s\n", g_ttydev, strerror(errno));
      return 11;
    }
#else
  /* Create a non-blocking copy of the configure tty descriptor */

  g_fdnb = dup(g_fd);
  if (g_fdnb < 0)
    {
      printconsole("ERROR: Failed to dup %s fd=%d: %s\n", g_ttydev, g_fd, strerror(errno));
      close_tty();
      return 12;
    }

  oflags = fcntl(g_fdnb, F_GETFL, 0);
  if(oflags == -1)
    {
      fprintf(stderr, "ERROR: fnctl(F_GETFL) failed: %s\n", strerror(errno));
      close_tty();
      return 13;
    }

  ret = fcntl(g_fdnb, F_SETFL, oflags | O_NONBLOCK);
  if(ret < 0)
    {
      fprintf(stderr, "ERROR: fnctl(F_SETFL) failed: %s\n", strerror(errno));
      close_tty();
      return 14;
    }
#endif

  /* Catch attempts to control-C out of the program so that we can restore
   * the TTY settings.
   */

  signal(SIGINT, interrupt);

  /* Loopo until control-C */

  for(;;)
    {
      /* Read characters from the console, and echo them to the target tty */

      ret = readbyte(0, &ch);
      if (ret == 0)
        {
          printconsole("End-of-file: exitting\n");
          close_tty();
          return 0;
        }
      else if (ret == 1)
        {
          writebyte(g_fd, ch);
        }

      /* Read characters from target TTY and echo them on the console */

      ret = readbyte(g_fdnb, &ch);
      if (ret == 0)
        {
          printconsole("ERROR: Unexpected number of bytes read(%d) from %s\n", ret, g_ttydev);
          close_tty();
          return 15;
        }
      else if (ret == 1)
        {
          if (ch == ENQ)
            {
              char ch1;
              char ch2;

              writebyte(g_fd, '*');
              ret = readbyte(g_fd, &ch1);
              if (ret != 1)
                {
                  printconsole("ERROR: Unexpected number of bytes read(%d) from %s\n", ret, g_ttydev);
                  close_tty();
                  return 15;
                }
              ret = readbyte(g_fd, &ch2);
              if (ret != 1)
                {
                  printconsole("ERROR: Unexpected number of bytes read(%d) from %s\n", ret, g_ttydev);
                  close_tty();
                  return 16;
                }

              getfilename(g_fd, filename);

              if (ch1 == 'l' || ch1 == 'L')
                {
                  sendfile(g_fd, filename, 0);
                }
              else if (ch1 == 'v' || ch1 == 'v')
                {
                  sendfile(g_fd, filename, 1);
                }
              else if (ch1 == 's' || ch1 == 'S')
                {
                  receivefile(g_fd, filename);
                }
            }
          else
            {
              putconsole(ch);
              flushconsole();
            }
        }
    }
  return 0;
}