PX4 Firmware
PX4 Autopilot Software http://px4.io
ver.c
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * Copyright (c) 2014 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 ver.c
36 *
37 * Version command, unifies way of showing versions of HW, SW, Build, GCC
38 * In case you want to add new version just extend version_main function
39 *
40 * @author Vladimir Kulla <ufon@kullaonline.net>
41 */
42 
43 #include <px4_platform_common/px4_config.h>
44 #include <px4_platform_common/module.h>
45 #include <stdio.h>
46 #include <stdbool.h>
47 #include <string.h>
48 #include <version/version.h>
49 
50 /* string constants for version commands */
51 static const char sz_ver_hw_str[] = "hw";
52 static const char sz_ver_hwcmp_str[] = "hwcmp";
53 static const char sz_ver_hwtypecmp_str[] = "hwtypecmp";
54 static const char sz_ver_git_str[] = "git";
55 static const char sz_ver_bdate_str[] = "bdate";
56 static const char sz_ver_buri_str[] = "uri";
57 static const char sz_ver_gcc_str[] = "gcc";
58 static const char sz_ver_all_str[] = "all";
59 static const char mcu_ver_str[] = "mcu";
60 static const char px4_guid_str[] = "px4guid";
61 
62 static void usage(const char *reason)
63 {
64  if (reason != NULL) {
65  printf("%s\n\n", reason);
66  }
67 
68  PRINT_MODULE_DESCRIPTION("Tool to print various version information");
69 
70  PRINT_MODULE_USAGE_NAME("ver", "command");
71  PRINT_MODULE_USAGE_COMMAND_DESCR("hw", "Hardware architecture");
72  PRINT_MODULE_USAGE_COMMAND_DESCR("mcu", "MCU info");
73  PRINT_MODULE_USAGE_COMMAND_DESCR("git", "git version information");
74  PRINT_MODULE_USAGE_COMMAND_DESCR("bdate", "Build date and time");
75  PRINT_MODULE_USAGE_COMMAND_DESCR("gcc", "Compiler info");
76  PRINT_MODULE_USAGE_COMMAND_DESCR("bdate", "Build date and time");
77  PRINT_MODULE_USAGE_COMMAND_DESCR("px4guid", "PX4 GUID");
78  PRINT_MODULE_USAGE_COMMAND_DESCR("uri", "Build URI");
79 
80  PRINT_MODULE_USAGE_COMMAND_DESCR("all", "Print all versions");
81  PRINT_MODULE_USAGE_COMMAND_DESCR("hwcmp", "Compare hardware version (returns 0 on match)");
82  PRINT_MODULE_USAGE_ARG("<hw> [<hw2>]",
83  "Hardware to compare against (eg. PX4_FMU_V4). An OR comparison is used if multiple are specified", false);
84  PRINT_MODULE_USAGE_COMMAND_DESCR("hwtypecmp", "Compare hardware type (returns 0 on match)");
85  PRINT_MODULE_USAGE_ARG("<hwtype> [<hwtype2>]",
86  "Hardware type to compare against (eg. V2). An OR comparison is used if multiple are specified", false);
87 }
88 
89 __EXPORT int ver_main(int argc, char *argv[]);
90 
91 int ver_main(int argc, char *argv[])
92 {
93  /* defaults to an error */
94  int ret = 1;
95 
96  /* first check if there are at least 2 params */
97  if (argc >= 2) {
98  if (argv[1] != NULL) {
99 
100  if (!strncmp(argv[1], sz_ver_hwcmp_str, sizeof(sz_ver_hwcmp_str))) {
101  if (argc >= 3 && argv[2] != NULL) {
102  const char *board_name = px4_board_name();
103 
104  for (int i = 2; i < argc; ++i) {
105  if (strcmp(board_name, argv[i]) == 0) {
106  return 0; // if one of the arguments match, return success
107  }
108  }
109 
110  } else {
111  PX4_ERR("Not enough arguments, try 'ver hwcmp PX4_FMU_V2'");
112  }
113 
114  return 1;
115  }
116 
117  if (!strncmp(argv[1], sz_ver_hwtypecmp_str, sizeof(sz_ver_hwtypecmp_str))) {
118  if (argc >= 3 && argv[2] != NULL) {
119  const char *board_type = px4_board_sub_type();
120 
121  for (int i = 2; i < argc; ++i) {
122  if (strcmp(board_type, argv[i]) == 0) {
123  return 0; // if one of the arguments match, return success
124  }
125  }
126 
127  } else {
128  PX4_ERR("Not enough arguments, try 'ver hwtypecmp {V2|V2M|V30|V31}'");
129  }
130 
131  return 1;
132  }
133 
134  /* check if we want to show all */
135  bool show_all = !strncmp(argv[1], sz_ver_all_str, sizeof(sz_ver_all_str));
136 
137  if (show_all || !strncmp(argv[1], sz_ver_hw_str, sizeof(sz_ver_hw_str))) {
138  printf("HW arch: %s\n", px4_board_name());
139 #if defined(BOARD_HAS_VERSIONING)
140  char vb[14] = "NA";
141  char rb[14] = "NA";
142  int v = px4_board_hw_version();
143  int r = px4_board_hw_revision();
144 
145  if (v >= 0) {
146  snprintf(vb, sizeof(vb), "0x%08X", v);
147  }
148 
149  if (r >= 0) {
150  snprintf(rb, sizeof(rb), "0x%08X", r);
151  }
152 
153  printf("HW type: %s\n", strlen(px4_board_sub_type()) ? px4_board_sub_type() : "NA");
154  printf("HW version: %s\n", vb);
155  printf("HW revision: %s\n", rb);
156 #endif
157  ret = 0;
158 
159  }
160 
161  if (show_all || !strncmp(argv[1], sz_ver_git_str, sizeof(sz_ver_git_str))) {
162  printf("FW git-hash: %s\n", px4_firmware_version_string());
163  unsigned fwver = px4_firmware_version();
164  unsigned major = (fwver >> (8 * 3)) & 0xFF;
165  unsigned minor = (fwver >> (8 * 2)) & 0xFF;
166  unsigned patch = (fwver >> (8 * 1)) & 0xFF;
167  unsigned type = (fwver >> (8 * 0)) & 0xFF;
168 
169  if (type == 255) {
170  printf("FW version: Release %u.%u.%u (%u)\n", major, minor, patch, fwver);
171 
172  } else {
173  printf("FW version: %u.%u.%u %x (%u)\n", major, minor, patch, type, fwver);
174  }
175 
176  if (show_all) {
177  const char *git_branch = px4_firmware_git_branch();
178 
179  if (git_branch && git_branch[0]) {
180  printf("FW git-branch: %s\n", git_branch);
181  }
182  }
183 
184  fwver = px4_os_version();
185  major = (fwver >> (8 * 3)) & 0xFF;
186  minor = (fwver >> (8 * 2)) & 0xFF;
187  patch = (fwver >> (8 * 1)) & 0xFF;
188  type = (fwver >> (8 * 0)) & 0xFF;
189  printf("OS: %s\n", px4_os_name());
190 
191  if (type == 255) {
192  printf("OS version: Release %u.%u.%u (%u)\n", major, minor, patch, fwver);
193 
194  } else {
195  printf("OS version: %u.%u.%u %u (%u)\n", major, minor, patch, type, fwver);
196  }
197 
198  const char *os_git_hash = px4_os_version_string();
199 
200  if (os_git_hash) {
201  printf("OS git-hash: %s\n", os_git_hash);
202  }
203 
204  ret = 0;
205 
206  }
207 
208  if (show_all || !strncmp(argv[1], sz_ver_bdate_str, sizeof(sz_ver_bdate_str))) {
209  printf("Build datetime: %s %s\n", __DATE__, __TIME__);
210  ret = 0;
211 
212  }
213 
214  if (show_all || !strncmp(argv[1], sz_ver_buri_str, sizeof(sz_ver_buri_str))) {
215  printf("Build uri: %s\n", px4_build_uri());
216  ret = 0;
217 
218  }
219 
220 
221  if (show_all || !strncmp(argv[1], sz_ver_gcc_str, sizeof(sz_ver_gcc_str))) {
222  printf("Toolchain: %s, %s\n", px4_toolchain_name(), px4_toolchain_version());
223  ret = 0;
224 
225  }
226 
227  if (show_all || !strncmp(argv[1], px4_guid_str, sizeof(px4_guid_str))) {
228  char px4guid_fmt_buffer[PX4_GUID_FORMAT_SIZE];
229 
230  board_get_px4_guid_formated(px4guid_fmt_buffer, sizeof(px4guid_fmt_buffer));
231  printf("PX4GUID: %s\n", px4guid_fmt_buffer);
232  ret = 0;
233  }
234 
235  if (show_all || !strncmp(argv[1], mcu_ver_str, sizeof(mcu_ver_str))) {
236 
237  char rev = ' ';
238  const char *revstr = NULL;
239  const char *errata = NULL;
240 
241  int chip_version = board_mcu_version(&rev, &revstr, &errata);
242 
243  if (chip_version < 0) {
244  printf("UNKNOWN MCU\n");
245 
246  } else {
247  printf("MCU: %s, rev. %c\n", revstr, rev);
248 
249  if (errata != NULL) {
250  printf("\nWARNING WARNING WARNING!\n"
251  "Revision %c has a silicon errata:\n"
252  "%s"
253  "\nhttps://docs.px4.io/en/flight_controller/silicon_errata.html\n\n", rev, errata);
254  }
255  }
256 
257  ret = 0;
258  }
259 
260  if (ret == 1) {
261  PX4_ERR("unknown command");
262  return 1;
263  }
264 
265  } else {
266  usage("Error, input parameter NULL.");
267  }
268 
269  } else {
270  usage("Error, not enough parameters.");
271  }
272 
273  return ret;
274 }
const char * px4_os_name(void)
name of the operating system
Definition: version.c:297
static const char * px4_board_sub_type(void)
get the board sub type
Definition: version.h:62
uint32_t px4_firmware_version(void)
get the PX4 Firmware version
Definition: version.c:149
const char * px4_toolchain_version(void)
Toolchain version used to compile PX4 (no particular format)
Definition: version.c:329
static const char mcu_ver_str[]
Definition: ver.c:59
static const char px4_guid_str[]
Definition: ver.c:60
Definition: I2C.hpp:51
static const char sz_ver_hwtypecmp_str[]
Definition: ver.c:53
const char * px4_firmware_git_branch(void)
get the git branch name (can be empty, for example if HEAD points to a tag)
Definition: version.c:245
const char * px4_toolchain_name(void)
Toolchain name used to compile PX4.
Definition: version.c:314
static const char sz_ver_hw_str[]
Definition: ver.c:51
uint32_t px4_os_version(void)
operating system version
Definition: version.c:259
__EXPORT int ver_main(int argc, char *argv[])
Definition: ver.c:91
static int px4_board_hw_revision(void)
get the board HW revision
Definition: version.h:78
static __BEGIN_DECLS const char * px4_board_name(void)
get the board name as string (including the version if there are multiple)
Definition: version.h:54
const char * px4_firmware_version_string(void)
Firmware version as human readable string (git tag)
Definition: version.c:338
const char * px4_build_uri(void)
get the build URI (used for crash logging)
Definition: version.c:61
Tools for system version detection.
static const char sz_ver_bdate_str[]
Definition: ver.c:55
static const char sz_ver_all_str[]
Definition: ver.c:58
static const char sz_ver_hwcmp_str[]
Definition: ver.c:52
static void usage(const char *reason)
Definition: ver.c:62
static const char sz_ver_buri_str[]
Definition: ver.c:56
static const char sz_ver_gcc_str[]
Definition: ver.c:57
static int px4_board_hw_version(void)
get the board HW version
Definition: version.h:70
const char * px4_os_version_string(void)
Operating system version as human readable string (git tag)
Definition: version.c:288
static const char sz_ver_git_str[]
Definition: ver.c:54