Blame view

RIOT/boards/msba2-common/tools/src/pseudoterm.c 4.85 KB
fb11e647   vrobic   reseau statique a...
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
  /*
  
      Author: Kaspar Schleiser
  
  This is free and unencumbered software released into the public domain.
  
  Anyone is free to copy, modify, publish, use, compile, sell, or
  distribute this software, either in source code form or as a compiled
  binary, for any purpose, commercial or non-commercial, and by any
  means.
  
  In jurisdictions that recognize copyright laws, the author or authors
  of this software dedicate any and all copyright interest in the
  software to the public domain. We make this dedication for the benefit
  of the public at large and to the detriment of our heirs and
  successors. We intend this dedication to be an overt act of
  relinquishment in perpetuity of all present and future rights to this
  software under copyright law.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
  
  For more information, please refer to <http://unlicense.org/>
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <string.h>
  #include <ctype.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <termios.h>
  #include <sys/ioctl.h>
  #include <signal.h>
  
  #include "serial.h"
  #include "pthread.h"
  #include "download.h"
  
  int tty_fd;
  int stopped = 0;
  char *port_name = "/dev/ttyUSB1";
  pthread_t serial_reader;
  
  void *serial_reader_func(void *arg)
  {
      unsigned char buf[255];
  
      while (1) {
          int n = read_serial_port(buf, sizeof(buf));
  
          if (n > 0) {
              write(tty_fd, buf, n);
          }
      }
  }
  
  int init()
  {
      int result = open_serial_port(port_name);
      pthread_create(&serial_reader, NULL, serial_reader_func, NULL);
      hard_reset_to_user_code();
      return result;
  }
  
  struct termios old_term_setting;
  
  void close_tty()
  {
      tcsetattr(tty_fd, TCSANOW, &old_term_setting);
  }
  
  void sig_handler(int signal)
  {
      if (signal == SIGUSR1) {
          if (stopped) {
              stopped = 0;
              printf("\nSignal received, opening port.\r\n");
  
              if (init() < 0) {
                  printf("Cannot open port.\r\n");
                  close_tty();
                  exit(1);
              }
          }
      }
      else if (signal == SIGUSR2) {
          if (!stopped) {
              stopped = 1;
              printf("\nSignal received, closing port. \r\n");
              pthread_cancel(serial_reader);
              close_serial_port();
          }
      }
      else if (signal == SIGINT) {
          printf("SIGINT received, exiting...\n");
          pthread_cancel(serial_reader);
          close_serial_port();
          close_tty();
          exit(0);
      }
  }
  
  int open_tty(void)
  {
      int r, fd;
      struct termios term_setting;
  
      fd = open("/dev/tty", O_RDWR);
  
      if (fd < 0) {
          return -1;
      }
  
      r = tcgetattr(fd, &term_setting);
  
      if (r != 0) {
          return -2;
      }
  
      old_term_setting = term_setting;
  
      term_setting.c_oflag |= (ONLRET);
      term_setting.c_iflag |= (/*IGNBRK |*/ BRKINT | IGNPAR);
      term_setting.c_iflag &= ~(ISTRIP);
      term_setting.c_lflag &= ~(ICANON |/* ISIG |*/ ECHO);
      term_setting.c_lflag |= (ISIG);
      term_setting.c_cflag |= CREAD;
      term_setting.c_cc[VMIN] = 1;
      term_setting.c_cc[VTIME] = 1;
      r = tcsetattr(fd, TCSANOW, &term_setting);
  
      if (r != 0) {
          return -3;
      }
  
      return fd;
  }
  
  void install_sighandler()
  {
      struct sigaction action;
      sigemptyset(&action.sa_mask);
      sigaddset(&action.sa_mask, SIGINT);
      sigaddset(&action.sa_mask, SIGUSR1);
      sigaddset(&action.sa_mask, SIGUSR2);
      action.sa_flags = 0;
      action.sa_handler = sig_handler;
      sigaction(SIGINT, &action, NULL);
      sigaction(SIGUSR1, &action, NULL);
      sigaction(SIGUSR2, &action, NULL);
  }
  
  int main(int argc, char **argv)
  {
      if (argc == 2) {
          port_name = argv[1];
      }
  
      printf("Using %s as serial device.\n", port_name);
  
      char ttybuf[255];
      tty_fd = open_tty();
  
      if (tty_fd < 0) {
          printf("Error opening terminal.\n");
          return (1);
      }
  
      install_sighandler();
  
      if (init() < 0) {
          printf("Cannot open port.\r\n");
          exit(1);
      }
  
      while (1) {
          int n = read(tty_fd, ttybuf, sizeof(ttybuf));
          int i;
  
          /* check for 0x3 (ctrl-c), clean exit */
          for (i = 0; i < n; i++) {
              if (ttybuf[i] == 0x3) {
                  if (i > 0) {
                      write_serial_port(ttybuf, i);
                  }
  
                  close_serial_port();
                  close_tty();
                  system("tset -c");
                  return 0;
              }
  
          }
  
          write_serial_port(ttybuf, n);
      }
  
      close_tty();
      close_serial_port();
      return 0;
  }