PX4 Firmware
PX4 Autopilot Software http://px4.io
version.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 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 #include "version.h"
35 
36 #include "build_git_version.h"
37 
38 #include <string.h>
39 
40 #if !defined(CONFIG_CDCACM_PRODUCTID)
41 # define CONFIG_CDCACM_PRODUCTID 0
42 #endif
43 
44 #if defined(__PX4_LINUX)
45 #include <sys/utsname.h>
46 #endif
47 
48 // dev >= 0
49 // alpha >= 64
50 // beta >= 128
51 // release candidate >= 192
52 // release == 255
59 };
60 
61 const char *px4_build_uri(void)
62 {
63  return STRINGIFY(BUILD_URI);
64 }
65 
66 uint32_t version_tag_to_number(const char *tag)
67 {
68  uint32_t version_number = 0;
69 
70  int16_t buffer = -1;
71  size_t buffer_counter = 0;
72  size_t dash_count = 0;
73  size_t point_count = 0;
74  char version[3] = {0, 0, 0};
75  int firmware_type = FIRMWARE_TYPE_RELEASE;
76 
77  for (size_t i = 0; i < strlen(tag); i++) {
78  if (tag[i] == '-') {
79  dash_count++;
80 
81  } else if (tag[i] == '.') {
82  point_count++;
83  }
84 
85  if (tag[i] == 'r' && i < strlen(tag) - 1 && tag[i + 1] == 'c') {
86  firmware_type = FIRMWARE_TYPE_RC;
87 
88  } else if (tag[i] == 'p') {
89  firmware_type = FIRMWARE_TYPE_ALPHA;
90 
91  } else if (tag[i] == 't' && i < strlen(tag) - 1 && tag[i + 1] == 'y') {
92  firmware_type = FIRMWARE_TYPE_DEV;
93 
94  } else if (tag[i] == 't') {
95  firmware_type = FIRMWARE_TYPE_BETA;
96 
97  } else if (tag[i] == 'v' && i > 0) {
98  firmware_type = FIRMWARE_TYPE_DEV;
99  }
100  }
101 
102  if ((dash_count == 1 && point_count == 2 && firmware_type == FIRMWARE_TYPE_RELEASE) ||
103  (dash_count == 2 && point_count == 2) ||
104  (dash_count == 3 && point_count == 4) ||
105  (dash_count == 4 && point_count == 4)) {
106  firmware_type = FIRMWARE_TYPE_DEV;
107  }
108 
109  for (size_t i = 0; i < strlen(tag); i++) {
110  if (buffer_counter > 2) {
111  continue;
112  }
113 
114  if (tag[i] >= '0' && tag[i] <= '9') {
115  buffer = (buffer == -1) ? 0 : buffer;
116  buffer = buffer * 10 + (tag[i] - '0');
117 
118  } else {
119  if (buffer >= 0) {
120  version[buffer_counter] = buffer;
121  buffer_counter++;
122  }
123 
124  buffer = -1;
125  }
126  }
127 
128  if (buffer >= 0) {
129  version[buffer_counter] = buffer;
130  buffer_counter++;
131  }
132 
133  if (buffer_counter <= 0) {
134  firmware_type = 0x00;
135  }
136 
137  if (buffer_counter == 3 || buffer_counter == 6) {
138  version_number = ((uint8_t)version[0] << 8 * 3) |
139  ((uint8_t)version[1] << 8 * 2) |
140  ((uint8_t)version[2] << 8 * 1) | firmware_type;
141 
142  } else {
143  version_number = 0;
144  }
145 
146  return version_number;
147 }
148 
149 uint32_t px4_firmware_version(void)
150 {
151  return version_tag_to_number(PX4_GIT_TAG_STR);
152 }
153 
154 uint32_t version_tag_to_vendor_version_number(const char *tag)
155 {
156  uint32_t version_number = 0;
157 
158  int16_t buffer = -1;
159  size_t buffer_counter = 0;
160  char version[6] = {0, 0, 0, 0, 0, 0};
161  size_t dash_count = 0;
162  size_t point_count = 0;
163  int firmware_type = FIRMWARE_TYPE_RELEASE;
164 
165  for (size_t i = 0; i < strlen(tag); i++) {
166  if (tag[i] == '-') {
167  dash_count++;
168 
169  } else if (tag[i] == '.') {
170  point_count++;
171  }
172 
173  if (tag[i] == 'r' && i < strlen(tag) - 1 && tag[i + 1] == 'c') {
174  firmware_type = FIRMWARE_TYPE_RC;
175 
176  } else if (tag[i] == 'p') {
177  firmware_type = FIRMWARE_TYPE_ALPHA;
178 
179  } else if (tag[i] == 't' && i < strlen(tag) - 1 && tag[i + 1] == 'y') {
180  firmware_type = FIRMWARE_TYPE_DEV;
181 
182  } else if (tag[i] == 't') {
183  firmware_type = FIRMWARE_TYPE_BETA;
184 
185  } else if (tag[i] == 'v' && i > 0) {
186  firmware_type = FIRMWARE_TYPE_DEV;
187  }
188  }
189 
190  if ((dash_count == 1 && point_count == 2 && firmware_type == FIRMWARE_TYPE_RELEASE) ||
191  (dash_count == 2 && point_count == 2) ||
192  (dash_count == 3 && point_count == 4) ||
193  (dash_count == 4 && point_count == 4)) {
194  firmware_type = FIRMWARE_TYPE_DEV;
195  }
196 
197  for (size_t i = 0; i < strlen(tag); i++) {
198  if (buffer_counter > 5) {
199  continue;
200  }
201 
202  if (tag[i] >= '0' && tag[i] <= '9') {
203  buffer = (buffer == -1) ? 0 : buffer;
204  buffer = buffer * 10 + (tag[i] - '0');
205 
206  } else {
207  if (buffer >= 0) {
208  if (buffer_counter + 1 == 4 && tag[i] == '-') {
209  break;
210  }
211 
212  version[buffer_counter] = buffer;
213  buffer_counter++;
214  }
215 
216  buffer = -1;
217  }
218  }
219 
220  if (buffer >= 0 && (buffer_counter + 1 == 3 || buffer_counter + 1 == 6)) {
221  version[buffer_counter] = buffer;
222  buffer_counter++;
223  }
224 
225  if (buffer_counter == 6) {
226  version_number = ((uint8_t)version[3] << 8 * 3) |
227  ((uint8_t)version[4] << 8 * 2) |
228  ((uint8_t)version[5] << 8 * 1) | firmware_type;
229 
230  } else if (buffer_counter == 3) {
231  version_number = firmware_type;
232 
233  } else {
234  version_number = 0;
235  }
236 
237  return version_number;
238 }
239 
241 {
242  return version_tag_to_vendor_version_number(PX4_GIT_TAG_STR);
243 }
244 
245 const char *px4_firmware_git_branch(void)
246 {
247  return PX4_GIT_BRANCH_NAME;
248 }
249 
250 uint32_t px4_board_version(void)
251 {
252 #if defined(__PX4_NUTTX)
254 #else
255  return 1;
256 #endif
257 }
258 
259 uint32_t px4_os_version(void)
260 {
261 #if defined(__PX4_DARWIN) || defined(__PX4_CYGWIN) || defined(__PX4_QURT)
262  return 0; //TODO: implement version for Darwin, Cygwin, QuRT
263 #elif defined(__PX4_LINUX)
264  struct utsname name;
265 
266  if (uname(&name) == 0) {
267  char *c = name.release;
268 
269  // cut the part after the first '-'
270  while (*c && *c != '-') {
271  ++c;
272  }
273 
274  *c = 0;
275  return version_tag_to_number(name.release);
276 
277  } else {
278  return 0;
279  }
280 
281 #elif defined(__PX4_NUTTX)
282  return version_tag_to_number(NUTTX_GIT_TAG_STR);
283 #else
284 # error "px4_os_version not implemented for current OS"
285 #endif
286 }
287 
288 const char *px4_os_version_string(void)
289 {
290 #if defined(__PX4_NUTTX)
291  return NUTTX_GIT_VERSION_STR;
292 #else
293  return NULL;
294 #endif
295 }
296 
297 const char *px4_os_name(void)
298 {
299 #if defined(__PX4_DARWIN)
300  return "Darwin";
301 #elif defined(__PX4_LINUX)
302  return "Linux";
303 #elif defined(__PX4_QURT)
304  return "QuRT";
305 #elif defined(__PX4_NUTTX)
306  return "NuttX";
307 #elif defined(__PX4_CYGWIN)
308  return "Cygwin";
309 #else
310 # error "px4_os_name not implemented for current OS"
311 #endif
312 }
313 
314 const char *px4_toolchain_name(void)
315 {
316 #if defined(__clang__)
317  return "Clang/LLVM";
318 #elif defined(__ICC) || defined(__INTEL_COMPILER)
319  return "Intel ICC";
320 #elif defined(__GNUC__) || defined(__GNUG__)
321  return "GNU GCC";
322 #elif defined(_MSC_VER)
323  return "MS Visual Studio";
324 #else
325  return "Unknown";
326 #endif
327 }
328 
329 const char *px4_toolchain_version(void)
330 {
331 #ifdef __VERSION__
332  return __VERSION__;
333 #else
334  return "";
335 #endif
336 }
337 
339 {
340  return PX4_GIT_VERSION_STR;
341 }
342 
344 {
345  return PX4_GIT_VERSION_BINARY;
346 }
347 
348 const char *px4_ecl_lib_version_string(void)
349 {
350 #ifdef ECL_LIB_GIT_VERSION_STRING
351  return ECL_LIB_GIT_VERSION_STRING;
352 #else
353  return NULL;
354 #endif
355 }
356 
357 #ifdef MAVLINK_LIB_GIT_VERSION_BINARY
358 uint64_t px4_mavlink_lib_version_binary(void)
359 {
360  return MAVLINK_LIB_GIT_VERSION_BINARY;
361 }
362 #endif /* MAVLINK_LIB_GIT_VERSION_BINARY */
363 
364 uint64_t px4_os_version_binary(void)
365 {
366 #ifdef NUTTX_GIT_VERSION_BINARY
367  return NUTTX_GIT_VERSION_BINARY;
368 #else
369  return 0;
370 #endif
371 }
372 
__EXPORT uint64_t px4_mavlink_lib_version_binary(void)
MAVLink lib version in binary form (first part of the git tag)
const char * px4_os_name(void)
name of the operating system
Definition: version.c:297
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
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
uint64_t px4_os_version_binary(void)
Operating system version in binary form (first part of the git tag)
Definition: version.c:364
const char * px4_toolchain_name(void)
Toolchain name used to compile PX4.
Definition: version.c:314
uint32_t version_tag_to_number(const char *tag)
Convert a version tag string to a number.
Definition: version.c:66
uint32_t px4_os_version(void)
operating system version
Definition: version.c:259
uint64_t px4_firmware_version_binary(void)
Firmware version in binary form (first part of the git tag)
Definition: version.c:343
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.
uint32_t px4_firmware_vendor_version(void)
get the PX4 Firmware vendor version
Definition: version.c:240
uint32_t version_tag_to_vendor_version_number(const char *tag)
Convert a version tag string to a vendor version number.
Definition: version.c:154
#define STRINGIFY(s)
Definition: px4_macros.h:94
#define CONFIG_CDCACM_PRODUCTID
Definition: version.c:41
const char * px4_os_version_string(void)
Operating system version as human readable string (git tag)
Definition: version.c:288
uint32_t px4_board_version(void)
get the board version (last 8 bytes should be silicon ID, if any)
Definition: version.c:250
FIRMWARE_TYPE
Definition: version.c:53
const char * px4_ecl_lib_version_string(void)
ECL lib version as human readable string (git tag)
Definition: version.c:348