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

working on #223: Auto-detection of serial ports. Mac version ready even if a bit slow

This commit is contained in:
Federico Fissore
2013-01-28 18:21:41 +01:00
parent 776952762f
commit 0d47f22787
11 changed files with 365 additions and 23 deletions

View File

@ -134,18 +134,18 @@ public class Platform {
}
}
public String resolveDeviceAttachedTo(String device, Map<String, TargetPackage> packages) {
public String resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages) {
return null;
}
public String resolveDeviceByVendorIdProductId(Map<String, TargetPackage> packages, String vendorId, String productId) {
protected String resolveDeviceByVendorIdProductId(Map<String, TargetPackage> packages, String readVIDPID) {
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)) {
if (vidPid.toUpperCase().equals(readVIDPID)) {
return board.get("name");
}
}

View File

@ -22,12 +22,6 @@
package processing.app.linux;
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;
@ -36,6 +30,9 @@ import processing.app.Preferences;
import processing.app.debug.TargetPackage;
import processing.core.PConstants;
import java.io.*;
import java.util.Map;
/**
* Used by Base for platform-specific tweaking, for instance finding the
@ -171,11 +168,15 @@ public class Platform extends processing.app.Platform {
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());
String vidPid = new UDevAdmParser().extractVIDAndPID(new String(baos.toByteArray()));
if (vidPid == null) {
return super.resolveDeviceAttachedTo(serial, packages);
}
return super.resolveDeviceByVendorIdProductId(packages, vidPid);
} catch (IOException e) {
return null;
return super.resolveDeviceAttachedTo(serial, packages);
}
}
}

View File

@ -0,0 +1,16 @@
package processing.app.linux;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
public class UDevAdmParser {
public String extractVIDAndPID(String output) throws IOException {
Properties properties = new Properties();
properties.load(new StringReader(output));
return properties.get("ID_VENDOR_ID").toString().toUpperCase() + "_" + properties.get("ID_MODEL_ID").toString().toUpperCase();
}
}

View File

@ -22,20 +22,23 @@
package processing.app.macosx;
import java.awt.Insets;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.net.URI;
import javax.swing.UIManager;
import com.apple.eio.FileManager;
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.Base;
import processing.app.debug.TargetPackage;
import processing.core.PApplet;
import processing.core.PConstants;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
/**
* Platform handler for Mac OS X.
@ -202,4 +205,52 @@ public class Platform extends processing.app.Platform {
return PConstants.platformNames[PConstants.MACOSX];
}
@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("/usr/sbin/system_profiler SPUSBDataType");
executor.execute(toDevicePath);
String output = new String(baos.toByteArray());
String vidPid = new SystemProfilerParser().extractVIDAndPID(output, serial);
if (vidPid == null) {
return super.resolveDeviceAttachedTo(serial, packages);
}
return super.resolveDeviceByVendorIdProductId(packages, vidPid);
} catch (IOException e) {
return super.resolveDeviceAttachedTo(serial, packages);
}
}
}

View File

@ -0,0 +1,62 @@
package processing.app.macosx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SystemProfilerParser {
private final Pattern vidRegex;
private final Pattern serialNumberRegex;
private final Pattern locationRegex;
private final Pattern pidRegex;
public SystemProfilerParser() {
serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
locationRegex = Pattern.compile("^Location ID: (.+)$");
pidRegex = Pattern.compile("^Product ID: (.+)$");
vidRegex = Pattern.compile("^Vendor ID: (.+)$");
}
public String extractVIDAndPID(String output, String serial) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(output));
String devicePrefix;
if (serial.startsWith("/dev/tty.")) {
devicePrefix = "/dev/tty.usbmodem";
} else {
devicePrefix = "/dev/cu.usbmodem";
}
Map<String, String> device = new HashMap<String, String>();
String line;
Matcher matcher;
while ((line = reader.readLine()) != null) {
line = line.trim();
line = line.replaceAll("\\s+", " ");
if ((matcher = serialNumberRegex.matcher(line)).matches()) {
device.put("serial_number", matcher.group(1));
} else if ((matcher = locationRegex.matcher(line)).matches()) {
device.put("device_path", devicePrefix + matcher.group(1).substring(2, 6) + "1");
} else if ((matcher = pidRegex.matcher(line)).matches()) {
device.put("pid", matcher.group(1));
} else if ((matcher = vidRegex.matcher(line)).matches()) {
device.put("vid", matcher.group(1));
} else if (line.equals("")) {
if (device.containsKey("serial_number") && device.get("device_path").equals(serial)) {
return device.get("vid").substring(2).toUpperCase() + "_" + device.get("pid").substring(2).toUpperCase();
}
device = new HashMap<String, String>();
}
}
return null;
}
}