-
Notifications
You must be signed in to change notification settings - Fork 133
IEP-1756 Deprecated! use 'adapter usb location <location>' command instead of OPENOCD_USB_ADAPTER_LOCATION variable #1450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/OpenOcdVersionManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2026 Espressif Systems (Shanghai) PTE LTD. All rights reserved. | ||
| * Use is subject to license terms. | ||
| *******************************************************************************/ | ||
| package com.espressif.idf.core.util; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.espressif.idf.core.logging.Logger; | ||
|
|
||
| public class OpenOcdVersionManager | ||
| { | ||
| private OpenOcdVersionManager() | ||
| { | ||
| /* This utility class should not be instantiated */ | ||
| } | ||
|
|
||
| private static final Map<String, OpenOcdVersion> versionCache = new ConcurrentHashMap<>(); | ||
|
|
||
| private static final Pattern VERSION_PATTERN = Pattern.compile( | ||
| "(?i)(?:Open On-Chip Debugger\\s+|v)(?<major>\\d+)\\.(?<minor>\\d+)(?:\\.(?<patch>\\d+))?(?:-[a-z0-9]+-(?<build>\\d+))?"); //$NON-NLS-1$ | ||
|
|
||
| public static class OpenOcdVersion | ||
| { | ||
| public final int major; | ||
| public final int minor; | ||
| public final int patch; | ||
| public final int buildDate; | ||
|
|
||
| public OpenOcdVersion(int major, int minor, int patch, int buildDate) | ||
| { | ||
| this.major = major; | ||
| this.minor = minor; | ||
| this.patch = patch; | ||
| this.buildDate = buildDate; | ||
| } | ||
|
|
||
| public boolean isAtLeast(int targetMajor, int targetMinor) | ||
| { | ||
| return isAtLeast(targetMajor, targetMinor, 0); | ||
| } | ||
|
|
||
| public boolean isAtLeast(int targetMajor, int targetMinor, int targetPatch) | ||
| { | ||
|
|
||
| if (this.major > targetMajor) | ||
| return true; | ||
| if (this.major == targetMajor && this.minor > targetMinor) | ||
| return true; | ||
| return (this.major == targetMajor && this.minor == targetMinor && this.patch >= targetPatch); | ||
| } | ||
|
|
||
| public boolean isBuildDateAtLeast(int targetMajor, int targetMinor, int targetBuildDate) | ||
| { | ||
| // If strictly newer major/minor, we assume the feature exists | ||
| if (this.major > targetMajor) | ||
| return true; | ||
| if (this.major == targetMajor && this.minor > targetMinor) | ||
| return true; | ||
|
|
||
| // If exactly the same major/minor, check the build date | ||
| if (this.major == targetMajor && this.minor == targetMinor) | ||
| { | ||
| return this.buildDate >= targetBuildDate; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return major + "." + minor + "." + patch + " (Build: " + buildDate + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | ||
| } | ||
| } | ||
|
|
||
| public static OpenOcdVersion getVersion(String executablePath) | ||
| { | ||
| if (executablePath == null || executablePath.isEmpty()) | ||
| { | ||
| return new OpenOcdVersion(0, 0, 0, 0); | ||
| } | ||
| return versionCache.computeIfAbsent(executablePath, OpenOcdVersionManager::fetchVersionFromProcess); | ||
| } | ||
|
|
||
| private static OpenOcdVersion fetchVersionFromProcess(String executablePath) | ||
| { | ||
| try | ||
| { | ||
| ProcessBuilder pb = new ProcessBuilder(executablePath, "--version"); //$NON-NLS-1$ | ||
| pb.redirectErrorStream(true); | ||
| Process process = pb.start(); | ||
|
|
||
| try | ||
| { | ||
| if (!process.waitFor(2, TimeUnit.SECONDS)) | ||
| { | ||
| return new OpenOcdVersion(0, 0, 0, 0); | ||
| } | ||
|
|
||
| try (BufferedReader reader = process.inputReader()) | ||
| { | ||
| String output = reader.lines().collect(Collectors.joining("\n")); //$NON-NLS-1$ | ||
| return parseVersionString(output); | ||
| } | ||
| } finally | ||
| { | ||
| if (process.isAlive()) | ||
| { | ||
| process.destroyForcibly(); | ||
| } | ||
| } | ||
| } | ||
| catch (IOException e) | ||
| { | ||
| Logger.log("Failed to execute or parse OpenOCD version fallback for path: " + executablePath, e); //$NON-NLS-1$ | ||
| } | ||
| catch (InterruptedException e) | ||
| { | ||
| Thread.currentThread().interrupt(); | ||
| Logger.log("Thread was interrupted while waiting for OpenOCD process to exit.", e); //$NON-NLS-1$ | ||
| } | ||
|
|
||
| return new OpenOcdVersion(0, 0, 0, 0); | ||
| } | ||
|
|
||
| public static OpenOcdVersion parseVersionString(String output) | ||
| { | ||
| if (output == null || output.trim().isEmpty()) | ||
| { | ||
| return new OpenOcdVersion(0, 0, 0, 0); | ||
| } | ||
|
|
||
| Matcher matcher = VERSION_PATTERN.matcher(output); | ||
| if (matcher.find()) | ||
| { | ||
| int major = Integer.parseInt(matcher.group("major")); //$NON-NLS-1$ | ||
| int minor = Integer.parseInt(matcher.group("minor")); //$NON-NLS-1$ | ||
| int patch = matcher.group("patch") != null ? Integer.parseInt(matcher.group("patch")) : 0; //$NON-NLS-1$ //$NON-NLS-2$ | ||
| int buildDate = matcher.group("build") != null ? Integer.parseInt(matcher.group("build")) : 0; //$NON-NLS-1$//$NON-NLS-2$ | ||
|
|
||
| return new OpenOcdVersion(major, minor, patch, buildDate); | ||
| } | ||
|
|
||
| return new OpenOcdVersion(0, 0, 0, 0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.