Permalink
Browse files

nasty serial hack

  • Loading branch information...
chrisdew
chrisdew committed Feb 1, 2011
1 parent 55e75f9 commit 9b0a66d9d21ad17e3d1984156c0b0e467e233d64
@@ -0,0 +1,139 @@
+package com.dummy;
+
+import gnu.io.CommPort;
+import gnu.io.RXTXPort;
+import gnu.io.SerialPortEvent;
+
+import com.dummy.DummySerialPortEvent;
+import gnu.io.SerialPortEventListener;
+import gnu.io.UnsupportedCommOperationException;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.TooManyListenersException;
+
+public class DummyRXTXPort {
+
+ //private RXTXPort serialPort;
+ @SuppressWarnings("restriction")
+ private List<DummySerialPortEventListener> listeners = new LinkedList<DummySerialPortEventListener>();
+ private InputStream is;
+ public boolean polling;
+ String name;
+
+ public DummyRXTXPort(String name) {
+ this.name = name;
+ }
+
+ public void setSerialPortParams(int baudRate, int databits8, int stopbits1,
+ int parityNone) throws UnsupportedCommOperationException {
+ //serialPort.setSerialPortParams(baudRate, databits8, stopbits1, parityNone);
+ }
+
+ public void setEndOfInputChar(byte b) throws UnsupportedCommOperationException {
+ //serialPort.setEndOfInputChar(b);
+ }
+
+ public void setFlowControlMode(int flowcontrolNone) {
+ //serialPort.setFlowControlMode(flowcontrolNone);
+ }
+
+ public InputStream getInputStream() {
+ //return serialPort.getInputStream();
+ try {
+ is = new FileInputStream(name);
+ InputPoller poller = new InputPoller(is);
+ poller.start();
+ return is;
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public OutputStream getOutputStream() {
+ //return serialPort.getOutputStream();
+ try {
+ return new FileOutputStream(name);
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public void enableReceiveTimeout(int i) {
+ //serialPort.enableReceiveTimeout(i);
+ }
+
+ public void setOutputBufferSize(int i) {
+ //serialPort.setOutputBufferSize(i);
+ }
+
+ public void setInputBufferSize(int i) {
+ //serialPort.setInputBufferSize(i);
+ }
+
+ public void notifyOnDataAvailable(boolean b) {
+ //serialPort.notifyOnDataAvailable(b);
+ }
+
+ public void addEventListener(DummySerialPortEventListener eventListener) throws TooManyListenersException {
+ //serialPort.addEventListener(eventListener);
+ listeners.add(eventListener);
+ }
+
+ public void removeEventListener() {
+ listeners.clear();
+ }
+
+ public void close() {
+ polling = false; // no need for sync block?
+ //serialPort.close();
+ }
+
+ private class InputPoller extends Thread {
+
+ private InputStream is;
+
+ public InputPoller(InputStream is) {
+ this.is = is;
+ polling = true;
+ }
+
+ @SuppressWarnings("restriction")
+ public void run() {
+ while (polling) { // no need for sync block?
+ //System.out.println("polling");
+ try {
+ int available = is.available();
+ if (available > 0) {
+ //System.out.println("" + available + " bytes available");
+ for (DummySerialPortEventListener listener: listeners) {
+ DummySerialPortEvent event = new DummySerialPortEvent(null, SerialPortEvent.DATA_AVAILABLE, false, false);
+ listener.serialEvent(event);
+ }
+ }
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ return;
+ }
+ try {
+ sleep(100);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ }
+}
+
@@ -0,0 +1,18 @@
+package com.dummy;
+
+import gnu.io.SerialPort;
+
+public class DummySerialPortEvent {
+
+ private int eventType;
+
+ public DummySerialPortEvent(SerialPort arg0, int arg1, boolean arg2,
+ boolean arg3) {
+ eventType = arg1;
+ }
+
+ public int getEventType() {
+ return eventType;
+ }
+
+}
@@ -0,0 +1,7 @@
+package com.dummy;
+
+import com.dummy.DummySerialPortEvent;
+
+public interface DummySerialPortEventListener {
+ public void serialEvent(final DummySerialPortEvent spe);
+}
@@ -29,6 +29,13 @@
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.RXTXPort;
+//import gnu.io.RXTXPort;
+import com.dummy.DummyInputStream;
+import com.dummy.DummyOutputStream;
+import com.dummy.DummyRXTXPort;
+import com.dummy.DummySerialPortEvent;
+import com.dummy.DummySerialPortEventListener;
+
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
@@ -79,7 +86,7 @@
*/
public static final int MODE_DATA = 2;
private static final Logger LOGGER = Utility.getLogger();
- private final SerialPortEventListener eventListener = new ATSerialPortEventListener();
+ private final DummySerialPortEventListener eventListener = new ATSerialPortEventListener();
private final ArrayList<ConnectionModeListener> connModeListners = new ArrayList<ConnectionModeListener>(10);
private final ArrayList<DataEventListener> dataEventListners = new ArrayList<DataEventListener>(10);
private final ArrayList<ATEventListener> atEventListners = new ArrayList<ATEventListener>(10);
@@ -92,7 +99,7 @@
private CommPortIdentifier commPortIdentifier;
private InputStream is;
private OutputStream os;
- private RXTXPort serialPort;
+ private DummyRXTXPort serialPort;
private OBEXDevice device;
private int errors = 0;
@@ -328,8 +335,9 @@ public void removeATEventListener(final ATEventListener listener) {
*/
private synchronized void open() throws IOException, UnsupportedCommOperationException, PortInUseException {
LOGGER.log(Level.FINEST, "Configuring serial port");
- CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000);
- serialPort = (RXTXPort) commPort;
+ //CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000);
+ //serialPort = (RXTXPort) commPort;
+ serialPort = new DummyRXTXPort(commPortIdentifier.getName());
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setEndOfInputChar((byte) 10);
switch (flowControl) {
@@ -584,13 +592,13 @@ public void setDevice(final OBEXDevice device) {
* Private class to hold the SerialPortEventListener#serialEvent(gnu.io.SerialPortEvent) out visibility of public.
* @see SerialPortEventListener#serialEvent(gnu.io.SerialPortEvent)
*/
- private final class ATSerialPortEventListener implements SerialPortEventListener {
+ private final class ATSerialPortEventListener implements DummySerialPortEventListener {
/**
* Method used to recieve SerialPortEvents.
* @param spe
*/
- public void serialEvent(final SerialPortEvent spe) {
+ public void serialEvent(final DummySerialPortEvent spe) {
synchronized (holder) {
switch (spe.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
@@ -19,6 +19,7 @@
*/
package com.lhf.obexftplib.io;
+import com.dummy.DummyOutputStream;
import com.lhf.obexftplib.event.DataEventListener;
import com.lhf.obexftplib.event.ConnectionModeListener;
import com.lhf.obexftplib.etc.*;

0 comments on commit 9b0a66d

Please sign in to comment.