public STM32BSPBuilder(BSPDirectories dirs, string cubeDir) : base(dirs) { STM32CubeDir = cubeDir; ShortName = "STM32"; SDKList = XmlTools.LoadObject <STM32SDKCollection>(Path.Combine(dirs.InputDir, SDKFetcher.SDKListFileName)); foreach (var sdk in SDKList.SDKs) { SystemVars[$"STM32:{sdk.Family.ToUpper()}_DIR"] = Directory.GetDirectories(Path.Combine(dirs.InputDir, sdk.FolderName), "STM32Cube_FW_*").First(); } }
public static void FetchLatestSDKs(string sdkRoot, string cubeRoot) { Directory.CreateDirectory(sdkRoot); var xml = new XmlDocument(); var catalogFile = cubeRoot + @"\db\plugins\updater\STMupdaters.xml"; var daysOld = (DateTime.Now - File.GetLastWriteTime(catalogFile)).TotalDays; if (daysOld > 40) { throw new Exception($"STM32CubeMX device list {daysOld:f0} days old. Please update STM32CubeMX."); } WebClient wc0 = new WebClient(); wc0.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"; wc0.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; wc0.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.5"; var onlineCatalog = wc0.DownloadData("https://www.st.com/resource/en/utility2/updaters.zip"); using (var archive = new ZipArchive(new MemoryStream(onlineCatalog))) { using (var stream = archive.GetEntry("STMupdaters.xml").Open()) { byte[] data = new byte[65536]; MemoryStream ms = new MemoryStream(); for (; ;) { int done = stream.Read(data, 0, data.Length); if (done == 0) { break; } ms.Write(data, 0, done); } string text = Encoding.UTF8.GetString(ms.ToArray()); xml.LoadXml(text); } } //xml.Load(catalogFile); var firmwaresNode = xml.DocumentElement.ChildNodes.OfType <XmlElement>().First(e => e.Name == "Firmwares"); List <ReleaseDefinition> releases = new List <ReleaseDefinition>(); foreach (var firmwareNode in firmwaresNode.OfType <XmlElement>().Where(e => e.Name == "Firmware")) { var packDescriptionNodes = firmwareNode.ChildNodes.OfType <XmlElement>().Where(e => e.Name == "PackDescription").ToArray(); if (packDescriptionNodes.Length == 0) { continue; } foreach (var node in packDescriptionNodes) { var r = new ReleaseDefinition(node); if (r.Release.IsValid) { releases.Add(r); } } } List <ReleaseDefinition> bestReleaseForEachFamily = new List <ReleaseDefinition>(); foreach (var g in releases.GroupBy(r => r.Family)) { var newReleases = g.Where(r => r.Status == "New").ToArray(); if (newReleases.Length > 1 && newReleases.Count(r => r.HasPatch) > 0) { newReleases = newReleases.Where(r => r.HasPatch).ToArray(); //Prefer patched releases to unpatched ones. } if (newReleases.Length == 0 && g.Count() == 1) //Experimental pre-release family { continue; } if (newReleases.Length == 1) { bestReleaseForEachFamily.Add(newReleases[0]); } else if (newReleases.Length > 1) { var newReleasesByMajor = newReleases.GroupBy(r => r.Release.Value).ToArray(); var bestReleaseGroup = newReleasesByMajor.OrderByDescending(r => r.Key, new SimpleVersionComparer()).First(); var bestRelease = bestReleaseGroup.OrderByDescending(r => r.Patch.Value).First(); bestReleaseForEachFamily.Add(bestRelease); } else { throw new Exception($"Don't know how to pick the best release for {g.Key}. Investigate and add missing logic here."); } } WebClient wc = new WebClient(); var now = DateTime.Now; STM32SDKCollection expectedSDKs = new STM32SDKCollection { SDKs = bestReleaseForEachFamily.Select(c => new STM32SDKCollection.SDK(c)).ToArray(), BSPVersion = $"{now.Year}.{now.Month:d2}", }; const string MarkerFileName = "BSPGeneratorSDK.xml"; int newSDKsFetched = 0; foreach (var sdk in expectedSDKs.SDKs) { string targetDir = Path.Combine(sdkRoot, sdk.FolderName); string markerFile = Path.Combine(targetDir, MarkerFileName); if (File.Exists(markerFile)) { var oldSDK = XmlTools.LoadObject <STM32SDKCollection.SDK>(markerFile); if (oldSDK.Equals(sdk)) { continue; } File.Delete(markerFile); } Directory.CreateDirectory(targetDir); DownloadAndUnpack(wc, sdk.URL, targetDir); if (sdk.PatchURL != null) { DownloadAndUnpack(wc, sdk.PatchURL, targetDir); } XmlTools.SaveObject(sdk, markerFile); newSDKsFetched++; } if (newSDKsFetched > 0) { XmlTools.SaveObject(expectedSDKs, Path.Combine(sdkRoot, SDKListFileName)); } }
public static void FetchLatestSDKs(string sdkRoot, string cubeRoot) { Directory.CreateDirectory(sdkRoot); var xml = new XmlDocument(); xml.Load(cubeRoot + @"\db\plugins\updater\STMupdaters.xml"); var databases = xml.DocumentElement.ChildNodes.OfType <XmlElement>().First(e => e.Name == "DataBases"); PackCollection[] packs = databases.ChildNodes.OfType <XmlElement>().Where(e => e.Name == "DataBase").Select(db => { var desc = db.ChildNodes.OfType <XmlElement>().First(e => e.Name == "PackDescription"); return(new PackCollection { Version = desc.GetAttribute("Release"), Status = desc.GetAttribute("Status"), FirmwareReleases = db.ChildNodes.OfType <XmlElement>().Where(e => e.Name == "FirmwareRelease").Select(e => e.GetAttribute("Release")).ToArray() }); }).ToArray(); var latestPack = packs.Where(p => p.Status == "New").ToArray(); if (latestPack.Length != 1) { throw new Exception("Unexpected count of 'New' packs in the STM32CubeMX database: " + latestPack.Length); } var now = DateTime.Now; STM32SDKCollection expectedSDKs = new STM32SDKCollection { SDKs = latestPack[0].FirmwareReleases.Select(s => s.Split('.')).Where(c => c[0] == "FW").Select(c => new STM32SDKCollection.SDK(c)).ToArray(), BSPVersion = $"{now.Year}.{now.Month:d2}", }; WebClient wc = new WebClient(); const string MarkerFileName = "BSPGeneratorSDK.xml"; int newSDKsFetched = 0; foreach (var sdk in expectedSDKs.SDKs) { string targetDir = Path.Combine(sdkRoot, sdk.FolderName); string markerFile = Path.Combine(targetDir, MarkerFileName); if (File.Exists(markerFile)) { var oldSDK = XmlTools.LoadObject <STM32SDKCollection.SDK>(markerFile); if (oldSDK.Equals(sdk)) { continue; } File.Delete(markerFile); } Directory.CreateDirectory(targetDir); Console.WriteLine($"Downloading {sdk.URL}..."); string archive = Path.Combine(targetDir, Path.GetFileName(sdk.URL)); wc.DownloadFile(sdk.URL, archive); Console.WriteLine($"Unpacking {Path.GetFileName(sdk.URL)}..."); using (var za = new ZipArchive(File.OpenRead(archive))) za.ExtractToDirectory(targetDir); XmlTools.SaveObject(sdk, markerFile); newSDKsFetched++; } if (newSDKsFetched > 0) { XmlTools.SaveObject(expectedSDKs, Path.Combine(sdkRoot, SDKListFileName)); } }