PX4 Firmware
PX4 Autopilot Software http://px4.io
reboot.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file reboot.c
36  * Tool similar to UNIX reboot command
37  *
38  * @author Lorenz Meier <lorenz@px4.io>
39  */
40 
41 #include <px4_platform_common/px4_config.h>
42 #include <px4_platform_common/getopt.h>
43 #include <px4_platform_common/log.h>
44 #include <px4_platform_common/module.h>
45 #include <px4_platform_common/shutdown.h>
46 #include <string.h>
47 
48 __EXPORT int reboot_main(int argc, char *argv[]);
49 
50 static void print_usage(void)
51 {
52  PRINT_MODULE_DESCRIPTION("Reboot the system");
53 
54  PRINT_MODULE_USAGE_NAME_SIMPLE("reboot", "command");
55  PRINT_MODULE_USAGE_PARAM_FLAG('b', "Reboot into bootloader", true);
56 
57  PRINT_MODULE_USAGE_ARG("lock|unlock", "Take/release the shutdown lock (for testing)", true);
58 }
59 
60 int reboot_main(int argc, char *argv[])
61 {
62  int ch;
63  bool to_bootloader = false;
64 
65  int myoptind = 1;
66  const char *myoptarg = NULL;
67 
68  while ((ch = px4_getopt(argc, argv, "b", &myoptind, &myoptarg)) != -1) {
69  switch (ch) {
70  case 'b':
71  to_bootloader = true;
72  break;
73 
74  default:
75  print_usage();
76  return 1;
77 
78  }
79  }
80 
81  if (myoptind >= 0 && myoptind < argc) {
82  int ret = -1;
83 
84  if (strcmp(argv[myoptind], "lock") == 0) {
85  ret = px4_shutdown_lock();
86 
87  if (ret != 0) {
88  PX4_ERR("lock failed (%i)", ret);
89  }
90  }
91 
92  if (strcmp(argv[myoptind], "unlock") == 0) {
93  ret = px4_shutdown_unlock();
94 
95  if (ret != 0) {
96  PX4_ERR("unlock failed (%i)", ret);
97  }
98  }
99 
100  return ret;
101  }
102 
103  int ret = px4_shutdown_request(true, to_bootloader);
104 
105  if (ret < 0) {
106  PX4_ERR("reboot failed (%i)", ret);
107  return -1;
108  }
109 
110  while (1) { px4_usleep(1); } // this command should not return on success
111 
112  return 0;
113 }
Definition: I2C.hpp:51
static void print_usage(void)
Definition: reboot.c:50
__EXPORT int reboot_main(int argc, char *argv[])
Definition: reboot.c:60