made parser aware of that now vid & pid have 0x

This commit is contained in:
Federico Fissore 2013-04-22 11:53:49 +02:00
parent 8ba3533a7a
commit 11db302b3e
8 changed files with 44 additions and 34 deletions

View File

@ -10,7 +10,7 @@ public class UDevAdmParser {
Properties properties = new Properties();
properties.load(new StringReader(output));
return properties.get("ID_VENDOR_ID").toString().toUpperCase() + "_" + properties.get("ID_MODEL_ID").toString().toUpperCase();
return ("0x" + properties.get("ID_VENDOR_ID").toString() + "_0x" + properties.get("ID_MODEL_ID").toString()).toUpperCase();
}
}

View File

@ -10,26 +10,34 @@ import java.util.regex.Pattern;
public class SystemProfilerParser {
private static final String DEVICE_PATH = "device_path";
private static final String VID = "vid";
private static final String PID = "pid";
private static final String SERIAL_NUMBER = "serial_number";
private static final String DEV_TTY = "/dev/tty.";
private static final String DEV_TTY_USBMODEM = "/dev/tty.usbmodem";
private static final String DEV_CU_USBMODEM = "/dev/cu.usbmodem";
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: (.+)$");
this.serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
this.locationRegex = Pattern.compile("^Location ID: (.+)$");
this.pidRegex = Pattern.compile("^Product ID: (.+)$");
this.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";
if (serial.startsWith(DEV_TTY)) {
devicePrefix = DEV_TTY_USBMODEM;
} else {
devicePrefix = "/dev/cu.usbmodem";
devicePrefix = DEV_CU_USBMODEM;
}
Map<String, String> device = new HashMap<String, String>();
@ -41,16 +49,16 @@ public class SystemProfilerParser {
line = line.replaceAll("\\s+", " ");
if ((matcher = serialNumberRegex.matcher(line)).matches()) {
device.put("serial_number", matcher.group(1));
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");
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));
device.put(PID, matcher.group(1));
} else if ((matcher = vidRegex.matcher(line)).matches()) {
device.put("vid", matcher.group(1));
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();
if (device.containsKey(DEVICE_PATH) && device.get(DEVICE_PATH).equals(serial)) {
return (device.get(VID) + "_" + device.get(PID)).toUpperCase();
}
device = new HashMap<String, String>();
}

View File

@ -29,7 +29,7 @@ public class ListComPortsParser {
Matcher vidMatcher = vidRegExp.matcher(line);
Matcher pidMatcher = pidRegExp.matcher(line);
if (vidMatcher.find() && pidMatcher.find()) {
return vidMatcher.group(1).toUpperCase() + "_" + pidMatcher.group(1).toUpperCase();
return ("0x" + vidMatcher.group(1) + "_0x" + pidMatcher.group(1)).toUpperCase();
}
}
}

View File

@ -5,7 +5,7 @@ import org.junit.BeforeClass;
public abstract class AbstractWithPreferencesTest {
@BeforeClass
public static void setUp() throws Exception {
public static void init() throws Exception {
Base.initPlatform();
Preferences.init(null);
}

View File

@ -1,9 +1,11 @@
package processing.app.debug;
import org.junit.Before;
import org.junit.Test;
import processing.app.AbstractWithPreferencesTest;
import processing.app.helpers.PreferencesMap;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@ -11,11 +13,16 @@ import static org.junit.Assert.assertTrue;
public class UploaderFactoryTest extends AbstractWithPreferencesTest {
private TargetPackage targetPackage;
@Before
public void setUp() throws Exception {
targetPackage = new TargetPackage("arduino", new File(".", "hardware/arduino/"));
}
@Test
public void shouldCreateAnInstanceOfHttpUploader() throws Exception {
Map<String, String> prefs = new HashMap<String, String>();
prefs.put("upload.via_http", "true");
TargetBoard board = new TargetBoard("dummy", new PreferencesMap(prefs), null);
TargetBoard board = targetPackage.getPlatforms().get("avr").getBoards().get("dogstick");
Uploader uploader = new UploaderFactory().newUploader(board, "192.168.0.1 (mydogstick)");
assertTrue(uploader instanceof HttpUploader);
@ -23,8 +30,7 @@ public class UploaderFactoryTest extends AbstractWithPreferencesTest {
@Test
public void shouldCreateAnInstanceOfBasicUploaderWhenHTTPIsUnsupported() throws Exception {
Map<String, String> prefs = new HashMap<String, String>();
TargetBoard board = new TargetBoard("dummy", new PreferencesMap(prefs), null);
TargetBoard board = targetPackage.getPlatforms().get("avr").getBoards().get("uno");
Uploader uploader = new UploaderFactory().newUploader(board, "192.168.0.1 (mydogstick)");
assertTrue(uploader instanceof BasicUploader);
@ -32,8 +38,7 @@ public class UploaderFactoryTest extends AbstractWithPreferencesTest {
@Test
public void shouldCreateAnInstanceOfBasicUploaderWhenPortIsSerial() throws Exception {
Map<String, String> prefs = new HashMap<String, String>();
TargetBoard board = new TargetBoard("dummy", new PreferencesMap(prefs), null);
TargetBoard board = targetPackage.getPlatforms().get("avr").getBoards().get("uno");
Uploader uploader = new UploaderFactory().newUploader(board, "/dev/ttyACM0 (Arduino Leonardo)");
assertTrue(uploader instanceof BasicUploader);

View File

@ -11,6 +11,6 @@ public class UDevAdmParserTest {
public void shouldCorrectlyParse() throws Exception {
String output = TestHelper.inputStreamToString(UDevAdmParserTest.class.getResourceAsStream("udev_output.txt"));
assertEquals("2341_0036", new UDevAdmParser().extractVIDAndPID(output));
assertEquals("0X2341_0X0036", new UDevAdmParser().extractVIDAndPID(output));
}
}

View File

@ -11,13 +11,12 @@ public class SystemProfilerParserTest {
public void shouldCorrectlyParse() throws Exception {
String output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output.txt"));
assertEquals("2341_0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
assertEquals("2341_0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
assertEquals("0X2341_0X0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
assertEquals("0X2341_0X0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output2.txt"));
assertEquals("2341_8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
assertEquals("2341_8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output2.txt"));
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
}
}

View File

@ -10,10 +10,8 @@ public class ListComPortsParserTest {
public void shouldFindVIDPID() throws Exception {
String listComPortsOutput = "COM26 - FTDI - FTDIBUS\\VID_0403+PID_6001+A6004CCFA\\0000\nCOM24 - PJRC.COM, LLC. - USB\\VID_16C0&PID_0483\\12345";
ListComPortsParser parser = new ListComPortsParser();
assertEquals("0403_6001", parser.extractVIDAndPID(listComPortsOutput, "COM26"));
assertEquals("16C0_0483", parser.extractVIDAndPID(listComPortsOutput, "COM24"));
assertEquals("0X0403_0X6001", new ListComPortsParser().extractVIDAndPID(listComPortsOutput, "COM26"));
assertEquals("0X16C0_0X0483", new ListComPortsParser().extractVIDAndPID(listComPortsOutput, "COM24"));
}