PX4 Firmware
PX4 Autopilot Software http://px4.io
nshterm.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012, 2013 PX4 Development Team. All rights reserved.
4  * Author: Andrew Tridgell
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 /**
36  * @file nshterm.c
37  * start a nsh terminal on a given port. This can be useful for error
38  * handling in startup scripts to start a nsh shell on /dev/ttyACM0
39  * for diagnostics
40  */
41 
42 #include <px4_platform_common/px4_config.h>
43 #include <termios.h>
44 #include <stdbool.h>
45 #include <stdio.h>
46 #include <stdarg.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <errno.h>
50 #include <nshlib/nshlib.h>
51 #include <fcntl.h>
52 #include <systemlib/err.h>
53 #include <drivers/drv_hrt.h>
54 
55 __EXPORT int nshterm_main(int argc, char *argv[]);
56 
57 int
58 nshterm_main(int argc, char *argv[])
59 {
60  if (argc < 2) {
61  printf("Usage: nshterm <device>\n");
62  exit(1);
63  }
64 
65  unsigned retries = 0;
66  int fd = -1;
67 
68  /* back off 800 ms to avoid running into the USB setup timing */
69  while (hrt_absolute_time() < 800U * 1000U) {
70  usleep(50000);
71  }
72 
73  /* try to bring up the console - stop doing so if the system gets armed */
74  while (true) {
75 
76  /* the retries are to cope with the behaviour of /dev/ttyACM0 */
77  /* which may not be ready immediately. */
78  fd = open(argv[1], O_RDWR);
79 
80  if (fd != -1) {
81  break;
82  }
83 
84  usleep(100000);
85  retries++;
86  }
87 
88  if (fd == -1) {
89  perror(argv[1]);
90  exit(1);
91  }
92 
93  /* set up the serial port with output processing */
94 
95  /* Try to set baud rate */
96  struct termios uart_config;
97  int termios_state;
98 
99  /* Back up the original uart configuration to restore it after exit */
100  if ((termios_state = tcgetattr(fd, &uart_config)) < 0) {
101  warnx("ERR get config %s: %d\n", argv[1], termios_state);
102  close(fd);
103  return -1;
104  }
105 
106  /* Set ONLCR flag (which appends a CR for every LF) */
107  uart_config.c_oflag |= (ONLCR | OPOST);
108 
109  if ((termios_state = tcsetattr(fd, TCSANOW, &uart_config)) < 0) {
110  warnx("ERR set config %s\n", argv[1]);
111  close(fd);
112  return -1;
113  }
114 
115  /* setup standard file descriptors */
116  close(0);
117  close(1);
118  close(2);
119  dup2(fd, 0);
120  dup2(fd, 1);
121  dup2(fd, 2);
122 
123  nsh_consolemain(0, NULL);
124 
125  close(fd);
126 
127  return OK;
128 }
Definition: I2C.hpp:51
High-resolution timer with callouts and timekeeping.
#define warnx(...)
Definition: err.h:95
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
__EXPORT int nshterm_main(int argc, char *argv[])
Definition: nshterm.c:58
int fd
Definition: dataman.cpp:146
#define OK
Definition: uavcan_main.cpp:71
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).