forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPFirmware.cs
242 lines (178 loc) · 8.86 KB
/
APFirmware.cs
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using log4net;
using MissionPlanner.Utilities;
namespace MissionPlanner.ArduPilot
{
public class APFirmware
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public class FirmwareInfo
{
[JsonProperty("board_id", NullValueHandling = NullValueHandling.Ignore)]
public long BoardId { get; set; }
[JsonProperty("mav-type")] public string MavType { get; set; }
[JsonProperty("mav-firmware-version-minor", NullValueHandling = NullValueHandling.Ignore)]
public long MavFirmwareVersionMinor { get; set; }
[JsonProperty("format")] public string Format { get; set; }
[JsonProperty("url")] public Uri Url { get; set; }
[JsonProperty("mav-firmware-version-type")]
public string MavFirmwareVersionType { get; set; }
[JsonProperty("mav-firmware-version-patch", NullValueHandling = NullValueHandling.Ignore)]
public long MavFirmwareVersionPatch { get; set; }
[JsonProperty("mav-autopilot")] public string MavAutopilot { get; set; }
[JsonProperty("vehicletype")] public string VehicleType { get; set; }
[JsonProperty("USBID", NullValueHandling = NullValueHandling.Ignore)]
public string[] Usbid { get; set; } = new string[0];
[JsonProperty("platform")] public string Platform { get; set; }
[JsonProperty("mav-firmware-version", NullValueHandling = NullValueHandling.Ignore)]
public Version MavFirmwareVersion { get; set; }
[JsonProperty("bootloader_str", NullValueHandling = NullValueHandling.Ignore)]
public string[] BootloaderStr { get; set; } = new string[0];
[JsonProperty("git-sha")] public string GitSha { get; set; }
[JsonProperty("mav-firmware-version-major", NullValueHandling = NullValueHandling.Ignore)]
public long MavFirmwareVersionMajor { get; set; }
[JsonProperty("latest")] public long Latest { get; set; }
}
public class ManifestRoot
{
[JsonProperty("firmware")] public FirmwareInfo[] Firmware { get; set; }
[JsonProperty("format-version")] public Version FormatVersion { get; set; }
}
// from generate_manifest.py - with map
public enum RELEASE_TYPES
{
BETA, // beta
DEV, // latest
OFFICIAL // stable
}
public enum MAV_TYPE
{
ANTENNA_TRACKER,
Copter,
HELICOPTER,
FIXED_WING,
GROUND_ROVER,
SUBMARINE
}
public static void GetList(string url = "https://firmware.ardupilot.org/manifest.json.gz", bool force = false)
{
if (force == false && Manifest != null)
return;
log.Info(url);
var client = new HttpClient();
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
var manifestgz = client.GetByteArrayAsync(url).GetAwaiter().GetResult();
var mssrc = new MemoryStream(manifestgz);
var msdest = new MemoryStream();
GZipStream gz = new GZipStream(mssrc, CompressionMode.Decompress);
gz.CopyTo(msdest);
msdest.Position = 0;
var manifest = new StreamReader(msdest).ReadToEnd();
Manifest = JsonConvert.DeserializeObject<ManifestRoot>(manifest);
log.Info(Manifest.Firmware?.Length);
}
public static ManifestRoot Manifest { get; set; }
public static long? GetBoardID(DeviceInfo device)
{
GetList();
// match the board description
var ans = Manifest.Firmware.Where(a => (
a.Platform == device.board ||
a.BootloaderStr.Any(b => b == device.board)) && a.BoardId != 0);
if (ans.Any())
{
return ans.First().BoardId;
}
if (device.hardwareid == null)
return null;
// match the vid/pid
Regex vidpid = new Regex("VID_([0-9a-f]+)&PID_([0-9a-f]+)&", RegexOptions.IgnoreCase);
if (vidpid.IsMatch(device.hardwareid))
{
var match = vidpid.Match(device.hardwareid);
var lookfor = String.Format("0x{0}/0x{1}", match.Groups[1].Value, match.Groups[2].Value);
var vidandusbdesc = Manifest.Firmware.Where(a => a.Usbid.Any(b => b.ToLower().Contains(lookfor.ToLower())) && a.BoardId != 0);
if (vidandusbdesc.Any())
{
return vidandusbdesc.First().BoardId;
}
}
return null;
}
public static List<FirmwareInfo> GetOptions(DeviceInfo device, RELEASE_TYPES? reltype = null, MAV_TYPE? mav_type = null)
{
GetList();
// match the board description
var ans = Manifest.Firmware.Where(a =>
a.Platform == device.board || a.BootloaderStr.Any(b => b == device.board));
// ignore platform
ans = Manifest.Firmware;
if (reltype.HasValue)
ans = ans.Where(a => a.MavFirmwareVersionType == reltype.Value.ToString());
if (mav_type.HasValue)
ans = ans.Where(a => a.MavType == mav_type.Value.ToString());
// "0x26AC/0x0011"
//USB\VID_2DAE&PID_1011&REV_0200
// match the vid/pid
Regex vidpid = new Regex("VID_([0-9a-f]+)&PID_([0-9a-f]+)&", RegexOptions.IgnoreCase);
if (vidpid.IsMatch(device.hardwareid))
{
var match = vidpid.Match(device.hardwareid);
var lookfor = String.Format("0x{0}/0x{1}", match.Groups[1].Value, match.Groups[2].Value);
var vidandusbdesc = ans.Where(a => a.Usbid.Any(b => b.ToLower().Contains(lookfor.ToLower())));
if (vidandusbdesc.Count() == 0)
return ans.ToList();
return vidandusbdesc.ToList();
}
return ans.ToList();
}
public static List<FirmwareInfo> GetRelease(RELEASE_TYPES reltype)
{
GetList();
var ans = Manifest.Firmware.Where(a => a.MavFirmwareVersionType == reltype.ToString());
ans = ans.GroupBy(b => b.MavType)
.SelectMany(a =>
a.Where(b => a.Key == b.MavType && b.MavFirmwareVersion == a.Max(c => c.MavFirmwareVersion)).OrderBy(b=>b.Format));
/*
ans = ans.GroupBy(b => b.MavType).Select(a =>
a.Where(b => a.Key == b.MavType && b.MavFirmwareVersion == a.Max(c => c.MavFirmwareVersion))
.FirstOrDefault());
*/
return ans.ToList();
}
public static void test()
{
GetList();
var cb = Manifest.Firmware.Where(a => a.Platform == "CubeBlack")
.Where(a => a.MavFirmwareVersionType == "OFFICIAL");
var vid = Manifest.Firmware.Where(a => a.Usbid.Any(b => b.ToLower().Contains("ac")))
.Where(a => a.MavFirmwareVersionType == "OFFICIAL");
var bl = Manifest.Firmware.Where(a => a.BootloaderStr.Any(b => b.ToLower().Contains("cubeblack")))
.Where(a => a.MavFirmwareVersionType == "OFFICIAL");
var bid = Manifest.Firmware.Where(a => a.BoardId == 9)
.Where(a => a.MavFirmwareVersionType == "OFFICIAL");
var vidandusbdesc = Manifest.Firmware
.Where(a => a.Usbid.Any(b => b.ToLower().Contains("26ac")))
.Where(a => a.BootloaderStr.Any(c => c.ToLower().Contains("fmuv3") || c.ToLower().Contains("fmuv3-bl")))
.Where(a => a.MavFirmwareVersionType == "OFFICIAL");
var vidandusbdesc1 = Manifest.Firmware
.Where(a => a.Usbid.Any(b => b.ToLower().Contains("26ac")))
.Where(a => a.BootloaderStr.Any(c => c.ToLower().Contains("fmuv3") || c.ToLower().Contains("fmuv3-bl")))
.Where(a => a.MavFirmwareVersionType == "DEV");
var vidandusbdesc2 = Manifest.Firmware
.Where(a => a.Usbid.Any(b => b.ToLower().Contains("26ac")))
.Where(a => a.BootloaderStr.Any(c => c.ToLower().Contains("fmuv3") || c.ToLower().Contains("fmuv3-bl")))
.Where(a => a.MavFirmwareVersionType == "BETA");
}
}
}