1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-07 00:04:36 +03:00

working on #223: Auto-detection of serial ports. Linux version ready

This commit is contained in:
Federico Fissore
2013-01-28 16:33:18 +01:00
parent 5b8ac97a42
commit 776952762f
9 changed files with 107 additions and 15 deletions

View File

@@ -76,13 +76,13 @@
<os family="unix"/>
</condition>
<!--
<dirname property="blah" file="${java.home}" />
<echo message="here! ${java.home}/lib/tools.jar or there: ${blah}" />
<echo message="override ${env.JAVA_HOME}/lib/tools.jar" />
<fail />
-->
<javac source="1.5" target="1.5"
<!--
<dirname property="blah" file="${java.home}" />
<echo message="here! ${java.home}/lib/tools.jar or there: ${blah}" />
<echo message="override ${env.JAVA_HOME}/lib/tools.jar" />
<fail />
-->
<javac source="1.6" target="1.6"
srcdir="src"
destdir="bin"
encoding="UTF-8"
@@ -97,7 +97,7 @@
<target name="test" depends="compile" description="Runs the test">
<mkdir dir="test-bin"/>
<javac source="1.5" target="1.5"
<javac source="1.6" target="1.6"
srcdir="test"
destdir="test-bin"
encoding="UTF-8"

Binary file not shown.

View File

@@ -1002,7 +1002,13 @@ public class Editor extends JFrame implements RunnerListener {
{
//System.out.println("Adding port to serial port menu: " + commportidentifier);
String curr_port = commportidentifier.getName();
rbMenuItem = new JCheckBoxMenuItem(curr_port, curr_port.equals(Preferences.get("serial.port")));
String description = curr_port;
String additionalDescription = Base.getPlatform().resolveDeviceAttachedTo(curr_port, Base.packages);
if (additionalDescription != null) {
description += " (" + additionalDescription + ")";
}
rbMenuItem = new JCheckBoxMenuItem(description, curr_port.equals(Preferences.get("serial.port")));
rbMenuItem.addActionListener(serialMenuListener);
//serialGroup.add(rbMenuItem);
serialMenu.add(rbMenuItem);

View File

@@ -24,11 +24,15 @@ package processing.app;
import static processing.app.I18n._;
import java.io.File;
import java.util.Map;
import javax.swing.UIManager;
import com.sun.jna.Library;
import com.sun.jna.Native;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.PreferencesMap;
import processing.core.PConstants;
@@ -129,10 +133,31 @@ public class Platform {
showLauncherWarning();
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
public String resolveDeviceAttachedTo(String device, Map<String, TargetPackage> packages) {
return null;
}
public String resolveDeviceByVendorIdProductId(Map<String, TargetPackage> packages, String vendorId, String productId) {
for (TargetPackage targetPackage : packages.values()) {
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) {
for (PreferencesMap board : targetPlatform.getBoards().values()) {
if (board.containsKey("vid_pid")) {
String[] vidPids = board.get("vid_pid").split(",");
for (String vidPid : vidPids) {
if (vidPid.toUpperCase().equals(vendorId + "_" + productId)) {
return board.get("name");
}
}
}
}
}
}
return null;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);

View File

@@ -22,11 +22,18 @@
package processing.app.linux;
import java.io.File;
import java.io.*;
import java.util.Map;
import java.util.Properties;
import javax.swing.UIManager;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.Executor;
import processing.app.Preferences;
import processing.app.debug.TargetPackage;
import processing.core.PConstants;
@@ -124,4 +131,51 @@ public class Platform extends processing.app.Platform {
public String getName() {
return PConstants.platformNames[PConstants.LINUX];
}
@Override
public String resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages) {
Executor executor = new DefaultExecutor();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
executor.setStreamHandler(new ExecuteStreamHandler() {
@Override
public void setProcessInputStream(OutputStream outputStream) throws IOException {
}
@Override
public void setProcessErrorStream(InputStream inputStream) throws IOException {
}
@Override
public void setProcessOutputStream(InputStream inputStream) throws IOException {
byte[] buf = new byte[4096];
int bytes = -1;
while ((bytes = inputStream.read(buf)) != -1) {
baos.write(buf, 0, bytes);
}
}
@Override
public void start() throws IOException {
}
@Override
public void stop() {
}
});
try {
CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial);
executor.execute(toDevicePath);
String devicePath = new String(baos.toByteArray());
baos.reset();
CommandLine commandLine = CommandLine.parse("udevadm info --query=property -p " + devicePath);
executor.execute(commandLine);
Properties properties = new Properties();
properties.load(new ByteArrayInputStream(baos.toByteArray()));
return super.resolveDeviceByVendorIdProductId(packages, properties.get("ID_VENDOR_ID").toString().toUpperCase(), properties.get("ID_MODEL_ID").toString().toUpperCase());
} catch (IOException e) {
return null;
}
}
}