From 11db302b3e2231aa8dad1d4215d2bf1f9e33eb45 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 22 Apr 2013 11:53:49 +0200 Subject: [PATCH] made parser aware of that now vid & pid have 0x --- .../processing/app/linux/UDevAdmParser.java | 2 +- .../app/macosx/SystemProfilerParser.java | 34 ++++++++++++------- .../app/windows/ListComPortsParser.java | 2 +- .../app/AbstractWithPreferencesTest.java | 2 +- .../app/debug/UploaderFactoryTest.java | 19 +++++++---- .../app/linux/UDevAdmParserTest.java | 2 +- .../app/macosx/SystemProfilerParserTest.java | 11 +++--- .../app/windows/ListComPortsParserTest.java | 6 ++-- 8 files changed, 44 insertions(+), 34 deletions(-) diff --git a/app/src/processing/app/linux/UDevAdmParser.java b/app/src/processing/app/linux/UDevAdmParser.java index 0b7470fd8..147c13bff 100644 --- a/app/src/processing/app/linux/UDevAdmParser.java +++ b/app/src/processing/app/linux/UDevAdmParser.java @@ -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(); } } diff --git a/app/src/processing/app/macosx/SystemProfilerParser.java b/app/src/processing/app/macosx/SystemProfilerParser.java index da82805b9..69482d317 100644 --- a/app/src/processing/app/macosx/SystemProfilerParser.java +++ b/app/src/processing/app/macosx/SystemProfilerParser.java @@ -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 device = new HashMap(); @@ -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(); } diff --git a/app/src/processing/app/windows/ListComPortsParser.java b/app/src/processing/app/windows/ListComPortsParser.java index 14a679956..ed0a19e76 100644 --- a/app/src/processing/app/windows/ListComPortsParser.java +++ b/app/src/processing/app/windows/ListComPortsParser.java @@ -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(); } } } diff --git a/app/test/processing/app/AbstractWithPreferencesTest.java b/app/test/processing/app/AbstractWithPreferencesTest.java index 008df0b9f..dd2946eeb 100644 --- a/app/test/processing/app/AbstractWithPreferencesTest.java +++ b/app/test/processing/app/AbstractWithPreferencesTest.java @@ -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); } diff --git a/app/test/processing/app/debug/UploaderFactoryTest.java b/app/test/processing/app/debug/UploaderFactoryTest.java index b5445d287..162b5cb0d 100644 --- a/app/test/processing/app/debug/UploaderFactoryTest.java +++ b/app/test/processing/app/debug/UploaderFactoryTest.java @@ -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 prefs = new HashMap(); - 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 prefs = new HashMap(); - 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 prefs = new HashMap(); - 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); diff --git a/app/test/processing/app/linux/UDevAdmParserTest.java b/app/test/processing/app/linux/UDevAdmParserTest.java index 574a01314..efcc70bfb 100644 --- a/app/test/processing/app/linux/UDevAdmParserTest.java +++ b/app/test/processing/app/linux/UDevAdmParserTest.java @@ -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)); } } diff --git a/app/test/processing/app/macosx/SystemProfilerParserTest.java b/app/test/processing/app/macosx/SystemProfilerParserTest.java index d683fdeab..ce756d855 100644 --- a/app/test/processing/app/macosx/SystemProfilerParserTest.java +++ b/app/test/processing/app/macosx/SystemProfilerParserTest.java @@ -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")); } } diff --git a/app/test/processing/app/windows/ListComPortsParserTest.java b/app/test/processing/app/windows/ListComPortsParserTest.java index 89539e205..dd6d9755a 100644 --- a/app/test/processing/app/windows/ListComPortsParserTest.java +++ b/app/test/processing/app/windows/ListComPortsParserTest.java @@ -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")); }