version.c
8.41 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/****************************************************************************
*
* Copyright (c) 2016 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "version.h"
#include "build_git_version.h"
#include <string.h>
#if !defined(CONFIG_CDCACM_PRODUCTID)
# define CONFIG_CDCACM_PRODUCTID 0
#endif
#if defined(__PX4_LINUX)
#include <sys/utsname.h>
#endif
// dev >= 0
// alpha >= 64
// beta >= 128
// release candidate >= 192
// release == 255
enum FIRMWARE_TYPE {
FIRMWARE_TYPE_DEV = 0,
FIRMWARE_TYPE_ALPHA = 64,
FIRMWARE_TYPE_BETA = 128,
FIRMWARE_TYPE_RC = 192,
FIRMWARE_TYPE_RELEASE = 255
};
const char *px4_build_uri(void)
{
return STRINGIFY(BUILD_URI);
}
uint32_t version_tag_to_number(const char *tag)
{
uint32_t version_number = 0;
int16_t buffer = -1;
size_t buffer_counter = 0;
size_t dash_count = 0;
size_t point_count = 0;
char version[3] = {0, 0, 0};
int firmware_type = FIRMWARE_TYPE_RELEASE;
for (size_t i = 0; i < strlen(tag); i++) {
if (tag[i] == '-') {
dash_count++;
} else if (tag[i] == '.') {
point_count++;
}
if (tag[i] == 'r' && i < strlen(tag) - 1 && tag[i + 1] == 'c') {
firmware_type = FIRMWARE_TYPE_RC;
} else if (tag[i] == 'p') {
firmware_type = FIRMWARE_TYPE_ALPHA;
} else if (tag[i] == 't' && i < strlen(tag) - 1 && tag[i + 1] == 'y') {
firmware_type = FIRMWARE_TYPE_DEV;
} else if (tag[i] == 't') {
firmware_type = FIRMWARE_TYPE_BETA;
} else if (tag[i] == 'v' && i > 0) {
firmware_type = FIRMWARE_TYPE_DEV;
}
}
if ((dash_count == 1 && point_count == 2 && firmware_type == FIRMWARE_TYPE_RELEASE) ||
(dash_count == 2 && point_count == 2) ||
(dash_count == 3 && point_count == 4) ||
(dash_count == 4 && point_count == 4)) {
firmware_type = FIRMWARE_TYPE_DEV;
}
for (size_t i = 0; i < strlen(tag); i++) {
if (buffer_counter > 2) {
continue;
}
if (tag[i] >= '0' && tag[i] <= '9') {
buffer = (buffer == -1) ? 0 : buffer;
buffer = buffer * 10 + (tag[i] - '0');
} else {
if (buffer >= 0) {
version[buffer_counter] = buffer;
buffer_counter++;
}
buffer = -1;
}
}
if (buffer >= 0) {
version[buffer_counter] = buffer;
buffer_counter++;
}
if (buffer_counter <= 0) {
firmware_type = 0x00;
}
if (buffer_counter == 3 || buffer_counter == 6) {
version_number = ((uint8_t)version[0] << 8 * 3) |
((uint8_t)version[1] << 8 * 2) |
((uint8_t)version[2] << 8 * 1) | firmware_type;
} else {
version_number = 0;
}
return version_number;
}
uint32_t px4_firmware_version(void)
{
return version_tag_to_number(PX4_GIT_TAG_STR);
}
uint32_t version_tag_to_vendor_version_number(const char *tag)
{
uint32_t version_number = 0;
int16_t buffer = -1;
size_t buffer_counter = 0;
char version[6] = {0, 0, 0, 0, 0, 0};
size_t dash_count = 0;
size_t point_count = 0;
int firmware_type = FIRMWARE_TYPE_RELEASE;
for (size_t i = 0; i < strlen(tag); i++) {
if (tag[i] == '-') {
dash_count++;
} else if (tag[i] == '.') {
point_count++;
}
if (tag[i] == 'r' && i < strlen(tag) - 1 && tag[i + 1] == 'c') {
firmware_type = FIRMWARE_TYPE_RC;
} else if (tag[i] == 'p') {
firmware_type = FIRMWARE_TYPE_ALPHA;
} else if (tag[i] == 't' && i < strlen(tag) - 1 && tag[i + 1] == 'y') {
firmware_type = FIRMWARE_TYPE_DEV;
} else if (tag[i] == 't') {
firmware_type = FIRMWARE_TYPE_BETA;
} else if (tag[i] == 'v' && i > 0) {
firmware_type = FIRMWARE_TYPE_DEV;
}
}
if ((dash_count == 1 && point_count == 2 && firmware_type == FIRMWARE_TYPE_RELEASE) ||
(dash_count == 2 && point_count == 2) ||
(dash_count == 3 && point_count == 4) ||
(dash_count == 4 && point_count == 4)) {
firmware_type = FIRMWARE_TYPE_DEV;
}
for (size_t i = 0; i < strlen(tag); i++) {
if (buffer_counter > 5) {
continue;
}
if (tag[i] >= '0' && tag[i] <= '9') {
buffer = (buffer == -1) ? 0 : buffer;
buffer = buffer * 10 + (tag[i] - '0');
} else {
if (buffer >= 0) {
if (buffer_counter + 1 == 4 && tag[i] == '-') {
break;
}
version[buffer_counter] = buffer;
buffer_counter++;
}
buffer = -1;
}
}
if (buffer >= 0 && (buffer_counter + 1 == 3 || buffer_counter + 1 == 6)) {
version[buffer_counter] = buffer;
buffer_counter++;
}
if (buffer_counter == 6) {
version_number = ((uint8_t)version[3] << 8 * 3) |
((uint8_t)version[4] << 8 * 2) |
((uint8_t)version[5] << 8 * 1) | firmware_type;
} else if (buffer_counter == 3) {
version_number = firmware_type;
} else {
version_number = 0;
}
return version_number;
}
uint32_t px4_firmware_vendor_version(void)
{
return version_tag_to_vendor_version_number(PX4_GIT_TAG_STR);
}
const char *px4_firmware_git_branch(void)
{
return PX4_GIT_BRANCH_NAME;
}
uint32_t px4_board_version(void)
{
#if defined(__PX4_NUTTX)
return CONFIG_CDCACM_PRODUCTID;
#else
return 1;
#endif
}
uint32_t px4_os_version(void)
{
#if defined(__PX4_DARWIN) || defined(__PX4_CYGWIN) || defined(__PX4_QURT)
return 0; //TODO: implement version for Darwin, Cygwin, QuRT
#elif defined(__PX4_LINUX)
struct utsname name;
if (uname(&name) == 0) {
char *c = name.release;
// cut the part after the first '-'
while (*c && *c != '-') {
++c;
}
*c = 0;
return version_tag_to_number(name.release);
} else {
return 0;
}
#elif defined(__PX4_NUTTX)
return version_tag_to_number(NUTTX_GIT_TAG_STR);
#else
# error "px4_os_version not implemented for current OS"
#endif
}
const char *px4_os_version_string(void)
{
#if defined(__PX4_NUTTX)
return NUTTX_GIT_VERSION_STR;
#else
return NULL;
#endif
}
const char *px4_os_name(void)
{
#if defined(__PX4_DARWIN)
return "Darwin";
#elif defined(__PX4_LINUX)
return "Linux";
#elif defined(__PX4_QURT)
return "QuRT";
#elif defined(__PX4_NUTTX)
return "NuttX";
#elif defined(__PX4_CYGWIN)
return "Cygwin";
#else
# error "px4_os_name not implemented for current OS"
#endif
}
const char *px4_toolchain_name(void)
{
#if defined(__clang__)
return "Clang/LLVM";
#elif defined(__ICC) || defined(__INTEL_COMPILER)
return "Intel ICC";
#elif defined(__GNUC__) || defined(__GNUG__)
return "GNU GCC";
#elif defined(_MSC_VER)
return "MS Visual Studio";
#else
return "Unknown";
#endif
}
const char *px4_toolchain_version(void)
{
#ifdef __VERSION__
return __VERSION__;
#else
return "";
#endif
}
const char *px4_firmware_version_string(void)
{
return PX4_GIT_VERSION_STR;
}
uint64_t px4_firmware_version_binary(void)
{
return PX4_GIT_VERSION_BINARY;
}
const char *px4_ecl_lib_version_string(void)
{
#ifdef ECL_LIB_GIT_VERSION_STRING
return ECL_LIB_GIT_VERSION_STRING;
#else
return NULL;
#endif
}
#ifdef MAVLINK_LIB_GIT_VERSION_BINARY
uint64_t px4_mavlink_lib_version_binary(void)
{
return MAVLINK_LIB_GIT_VERSION_BINARY;
}
#endif /* MAVLINK_LIB_GIT_VERSION_BINARY */
uint64_t px4_os_version_binary(void)
{
#ifdef NUTTX_GIT_VERSION_BINARY
return NUTTX_GIT_VERSION_BINARY;
#else
return 0;
#endif
}
const char *px4_firmware_oem_version_string(void)
{
return PX4_GIT_OEM_VERSION_STR;
}