Skip to content

Commit

Permalink
better identify, better estabilize, makefile fixes, sh fixes, standal…
Browse files Browse the repository at this point in the history
…one fixes
  • Loading branch information
Ricardo Guilherme Schmidt committed Jan 31, 2011
1 parent 881ae9a commit b176774
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 105 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.MF
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.lhf.jobexftp.StandAloneApp
Class-Path:
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -22,11 +22,11 @@ builddir:

$(BINARY): builddir
# packaging $(BINARY)
$(JAR) cfe $(BINARY) com.lhf.jobexftp.StandAloneApp -C builddir com
$(JAR) cf $(BINARY) -m MANIFEST.MF -C builddir com

install:
if ! test `whoami` = "root" ; then echo "you need root privileges" ; exit 0 ; fi
mkdir -p /usr/share/jobexftp/lib
if test -e $(BINARY) ; then mv $(BINARY) /usr/share/jobexftp/ ; rm /usr/bin/jobexftp ; echo "java -Djava.library.path=/usr/share/jobexftp/lib/ -cp /usr/share/jobexftp/RXTXcomm.jar -jar /usr/share/jobexftp/jobexftp.jar \$$*" > /usr/bin/jobexftp ; chmod +x /usr/bin/jobexftp ; fi
if test -e $(BINARY) ; then mv $(BINARY) /usr/share/jobexftp/ ; rm /usr/bin/jobexftp ; echo "java -Djava.library.path=/usr/share/jobexftp/lib/ -cp /usr/share/jobexftp/RXTXcomm.jar -jar /usr/share/jobexftp/jobexftp.jar \"\$@\"" > /usr/bin/jobexftp ; chmod +x /usr/bin/jobexftp ; fi
if test `uname -m` = "x86_64" ; then cp lib/x86_64/lib* /usr/share/jobexftp/lib/ ; cp lib/x86_64/RXTXcomm.jar /usr/share/jobexftp/ ; else cp lib/i386/lib* /usr/share/jobexftp/lib/ ; cp lib/i386/RXTXcomm.jar /usr/share/jobexftp/ ; fi
if test -d builddir ; then rm -rf builddir ; fi
Binary file modified bin/JObexFTP2.jar 100755 → 100644
Binary file not shown.
2 changes: 1 addition & 1 deletion bin/jobexftp
@@ -1,3 +1,3 @@
#!/bin/bash
currentdir=`dirname $(readlink -f $0)`
java -jar $currentdir/JObexFTP2.jar $*
java -jar $currentdir/JObexFTP2.jar "$@"
6 changes: 3 additions & 3 deletions bin/jobexftpfs.sh
Expand Up @@ -53,7 +53,7 @@ if [ -a $ttypath ]; then
echo "Killing all process using $ttypath :"
echo "`fuser -k $ttypath`"
stty -F $ttypath raw ispeed 115200 ospeed 115200 min 1 time 0 -cstopb -evenp crtscts
echo "Checking if tc65 is working :"
echo "Checking if tc65 is ready :"
device=`echo -e "AT+CGMM\r" > $ttypath && timeout 1 cat $ttypath | head -n 2 | tail -n 1 | head -c 4`

if [[ $device == "" ]]; then
Expand All @@ -71,11 +71,11 @@ if [ -a $ttypath ]; then
fi

if [[ $device != "TC65" ]]; then
echo " Failed to find a working device. "
echo " Failed to find a ready device. "
exit 0
fi
else
echo " Failed to find a working device. "
echo " Failed to find a device ready for communication. "
exit 0
fi

Expand Down
File renamed without changes.
37 changes: 12 additions & 25 deletions src/com/lhf/jobexftp/StandAloneApp.java
Expand Up @@ -27,16 +27,12 @@
import com.lhf.obexftplib.etc.Log;
import com.lhf.obexftplib.etc.Utility;
import com.lhf.obexftplib.fs.OBEXFile;
import com.lhf.obexftplib.fs.OBEXFolder;
import com.lhf.obexftplib.io.ATConnection;
import com.lhf.obexftplib.io.OBEXClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;

/**
Expand All @@ -45,8 +41,6 @@
*/
public class StandAloneApp {

private static final String VERSION_STRING = "1.2.0";

public static void main(String[] args) {
try {
if (args.length == 0) {
Expand All @@ -63,8 +57,12 @@ public static void main(String[] args) {
}
}
new StandAloneApp().exec(args);
} catch (gnu.io.NoSuchPortException e) {
Log.info("Fatal: Port is unavaliable.", e);
System.exit(1);
} catch (Exception e) {
Log.info("PANIC", e);
Log.info("Fatal: ", e);
System.exit(1);
}
}

Expand Down Expand Up @@ -189,22 +187,21 @@ public void exec(String[] args) throws Exception {
Log.info("Please specify the port.");
System.exit(0);
}

ATConnection device = new ATConnection(portname);
device.setBaudRate(baudrate);
device.setFlowControl(flowControl);
Log.info("connecting to serial " + portname + " " + baudrate);
try {
Log.info("connecting to serial " + portname + " " + baudrate);
device.setConnMode(ATConnection.MODE_DATA);
if (ui == null) {
ui = new InteractiveUserInterface();
Log.info("starting interactive mode, type 'exit' to exit, 'help' for help.");
}
run(device, ui);

} finally {
Log.info("disconnecting serial port");
device.setConnMode(ATConnection.MODE_DISCONNECTED);
} catch (Exception e) {
e.printStackTrace();
}
}

Expand Down Expand Up @@ -234,47 +231,39 @@ private void run(ATConnection device, UserInterface ui) throws IOException {
obexClient.connect();
}
if (tok[0].equals("cd")) {
// cd <deviceDirectory>
if (tok.length > 1) {
obexClient.changeDirectory(tok[1], false);
}
ui.setDir(obexClient.getCurrentFolder().getPath());
} else if (tok[0].equals("mkdir")) {
// mkdir <directory>
if (tok.length > 1) {
obexClient.changeDirectory(tok[1], true);
}
ui.setDir(obexClient.getCurrentFolder().getPath());
} else if (tok[0].equals("ls") || tok[0].equals("dir")) {
// ls
ui.println(obexClient.loadFolderListing().getListing());
} else if (tok[0].equals("rm") || tok[0].equals("del")) {
// rm <deviceFilename>
if (tok.length > 1) {
obexClient.removeObject(Utility.nameToBytes(tok[1]));
}
} else if (tok[0].equals("put")) {
// put <localFilePath> <deviceFilename>
if (tok.length > 2) {
OBEXFile fileHolder = loadLocalFile(tok[1]);
obexClient.removeObject(fileHolder);
obexClient.writeFile(fileHolder);
}
} else if (tok[0].equals("get")) {
// get <deviceFilename> <localFilePath>
if (tok.length > 2) {
OBEXFile fh = obexClient.readFile(tok[1]);
saveLocalFile(fh);
}
} else if (tok[0].equals("cat")) {
// cat <deviceFilename>
if (tok.length > 1) {
OBEXFile fh = obexClient.readFile(tok[1]);
String catString = new String(fh.getContents());
ui.println(catString);
}
} else if (tok[0].equals("erasedisk")) {
// erasedisk
if (tok.length > 1) {
if (tok[1].toLowerCase().startsWith("-y")) {
obexClient.eraseDisk();
Expand All @@ -285,9 +274,7 @@ private void run(ATConnection device, UserInterface ui) throws IOException {
obexClient.eraseDisk();
}
}

} else if (tok[0].equals("sleep")) {
// sleep <milliseconds>
if (tok.length > 1) {
long millis = Long.parseLong(tok[1]);
try {
Expand All @@ -300,11 +287,11 @@ private void run(ATConnection device, UserInterface ui) throws IOException {
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Log.info("disconnecting obex");
obexClient.disconnect();
}
Log.info("disconnecting obex");
obexClient.disconnect();

}

private void printHelp(UserInterface ui) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/lhf/jobexftp/ui/CommandLineUserInterface.java
Expand Up @@ -38,7 +38,7 @@ public String readCommand() {
if (commands.isEmpty()) {
return null;
}
return commands.removeFirst();
return commands.poll();
}

@Override
Expand Down
68 changes: 35 additions & 33 deletions src/com/lhf/obexftplib/etc/Log.java
Expand Up @@ -17,37 +17,39 @@

public abstract class Log {

public static int LOG_NONE = 0;
public static int LOG_INFO = 1;
public static int LOG_DEBUG = 2;

public static int logLevel = LOG_INFO;


public static void info(String msg) {
info(msg,null);
}

public static void info(String msg, Throwable t) {
if ( logLevel >= LOG_INFO ) {
System.out.println(msg);
if ( t != null ) {
t.printStackTrace();
}
}
}

public static void debug(Class<?> klass, String msg) {
debug(klass,msg,null);
}

public static void debug(Class<?> klass, String msg, Throwable t) {
if ( logLevel >= LOG_DEBUG ) {
System.out.println("["+klass.getName()+"]" +msg);
if ( t != null ) {
t.printStackTrace();
}
}
}

public static int LOG_NONE = 0;
public static int LOG_INFO = 1;
public static int LOG_DEBUG = 2;
public static int logLevel = LOG_INFO;

public static void info(String msg) {
info(msg, null);
}

public static void info(String msg, Throwable t) {
if (logLevel >= LOG_INFO) {
System.out.print(msg);
if (t != null) {
if (t.getMessage() != null) {
System.out.print(t.getMessage());
} else if (t.getLocalizedMessage() != null){
System.out.print(t.getLocalizedMessage());
}
}
}
System.out.println();
}

public static void debug(Class<?> klass, String msg) {
debug(klass, msg, null);
}

public static void debug(Class<?> klass, String msg, Throwable t) {
if (logLevel >= LOG_DEBUG) {
System.out.println("[" + klass.getName() + "]" + msg);
if (t != null) {
t.printStackTrace();
}
}
}
}
68 changes: 29 additions & 39 deletions src/com/lhf/obexftplib/io/ATConnection.java
Expand Up @@ -141,7 +141,7 @@ public synchronized void setConnMode(final int newConnMode) throws IOException {
throw new IOException("Operation not supported: " + ex.getMessage(), ex);
} catch (PortInUseException ex) {
terminate();
throw new IOException("The port " + commPortIdentifier.getName() + "is in use.", ex);
throw new IOException("The port " + commPortIdentifier.getName() + " is in use.", ex);
}
if (newConnMode == MODE_DATA) {
connMode = openDataMode() ? MODE_DATA : connMode;
Expand Down Expand Up @@ -188,49 +188,34 @@ public synchronized void setConnMode(final int newConnMode) throws IOException {
*/
public synchronized byte[] send(final byte[] b, final int timeout) throws IOException {
if (connMode != MODE_AT) {
LOGGER.log(Level.WARNING, "Trying to send in wrong mode. Mode is {0}", connMode);
LOGGER.log(Level.FINE, "Trying to send in wrong mode. Mode is {0}", connMode);
}
return sendPacket(b, timeout);
}

/**
* Method used to auto identify the device, if it is not yet identified.
* Method used to auto identify the device
*/
public void identifyDevice() throws IOException {
if (device == null && connMode == MODE_AT) {
String s = new String(send("AT+CGMM\r".getBytes(), 500));
if (s.indexOf("ERROR") > -1) {
LOGGER.log(Level.WARNING, "Warning: Device is in wrong mode.");
} else if (s.indexOf("TC65") > -1) {
LOGGER.log(Level.FINE, "Found TC65 device.");
String s = "";
for (int m = 5; m > 0; m--) {
s = new String(send("AT+CGMM\r".getBytes(), 500));
if (s.indexOf("TC65i") > -1) {
LOGGER.log(Level.FINE, "Found TC65i device.");
setDevice(OBEXDevice.TC65);
} else {
LOGGER.log(Level.WARNING, "Unknown device {0}, using default settings", s);
setDevice(OBEXDevice.DEFAULT);
}
}

}

/**
* Method used to auto identify the device, if it is not yet identified.
*/
@Deprecated
public boolean identifyDevice(byte[] b) throws IOException {
if (device == null && connMode == MODE_AT) {
String s = new String(b);
if (s.indexOf("ERROR") > -1) {
LOGGER.log(Level.WARNING, "Warning: Device is in wrong mode.");
return;
} else if (s.indexOf("TC65") > -1) {
LOGGER.log(Level.FINE, "Found TC65 device.");
setDevice(OBEXDevice.TC65);
return true;
} else {
LOGGER.log(Level.WARNING, "Unknown device" + s + ", using default settings");
setDevice(OBEXDevice.DEFAULT);
return;
} else if (s.indexOf("AT+CGMM") > -1) {
LOGGER.log(Level.WARNING, "Unexpected behavior, trying to fix.", s);
send(new byte[]{'A', 'T', 'E', '\r'}, 50);
}
}
return false;
if (device == null) {
throw new IOException("Device is in wrong mode or device not supported.");
}
}

public boolean isAnswering() {
Expand Down Expand Up @@ -376,15 +361,21 @@ protected void onOpen() throws IOException {
*/
public void estabilize() throws IOException {
LOGGER.log(Level.FINEST, "Estabilizating I/O");
// sendPacket(OBEXDevice.CMD_CHECK, 50);
if (send(OBEXDevice.CMD_CHECK, 50).length < 1) {
is.skip(is.available());
if (sendPacket(OBEXDevice.CMD_CHECK, 50).length < 1) {
closeDataMode();
}
send(OBEXDevice.CMD_CHECK, 50);
send(new byte[]{'A', 'T', 'E', '\r'}, 50);
send(OBEXDevice.CMD_CHECK, 50);
send(OBEXDevice.CMD_CHECK, 50);
send(OBEXDevice.CMD_CHECK, 50);
checkSend(new byte[]{'A', 'T', 'Z', '\r'}, 50);
checkSend(("at+ipr=" + baudRate + "\r").getBytes(), 100);
checkSend(new byte[]{'A', 'T', 'E', '\r'}, 50);
checkSend(new byte[]{'A', 'T', 'E', '\r'}, 50);
checkSend(OBEXDevice.CMD_CHECK, 50);
}

private void checkSend(byte[] b, int timeout) throws IOException {
if (new String(sendPacket(b, 50)).contains("ERROR")) {
throw new IOException("Device is in wrong mode or is not supported");
}
}

/**
Expand All @@ -407,7 +398,6 @@ private void close() throws IOException {
}

public void terminate() {
System.out.println("Terminating...");
if (serialPort != null) {
serialPort.removeEventListener();
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/lhf/obexftplib/io/OBEXClient.java
Expand Up @@ -37,7 +37,7 @@
*/
public class OBEXClient {

public final static String version = "2.1 beta";
public final static String version = "2.2 beta";
private final static int TIMEOUT = 30000;
private final static Logger logger = Utility.getLogger();
private final ObexEventListener eventListener = new ObexEventListener();
Expand Down

0 comments on commit b176774

Please sign in to comment.