Initial commit

This commit is contained in:
2013-11-05 11:44:26 -04:30
commit 9635163950
36 changed files with 1445 additions and 0 deletions

View File

@@ -0,0 +1,191 @@
package ve.ucv.ciens.ccg.nxtcam.network;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
public class BluetoothManager{
private static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final String OUI_LEGO = "00:16:53";
private static final String TAG = "BTMNGR";
private boolean connected;
private BluetoothAdapter bt_adapter;
private BluetoothSocket bt_socket = null;
private OutputStream nxt_out_stream = null;
private InputStream nxt_in_stream = null;
private static class SingletonHolder{
public static final BluetoothManager INSTANCE = new BluetoothManager();
}
private BluetoothManager(){
connected = false;
bt_adapter = BluetoothAdapter.getDefaultAdapter();
bt_socket = null;
nxt_in_stream = null;
nxt_out_stream = null;
}
public static BluetoothManager getInstance(){
return SingletonHolder.INSTANCE;
}
public boolean isBTSupported(){
return bt_adapter != null;
}
public boolean isConnected(){
return connected;
}
public boolean isBTEnabled(){
return bt_adapter.isEnabled();
}
public void disableBT(){
bt_adapter.disable();
}
public Set<BluetoothDevice> getPairedDevices(){
return bt_adapter.getBondedDevices();
}
/**
* Sets up a connection with a NXT device.
*
* Verifies if the target device is a valid NXT robot by checking agains Lego's OUI.
* Also creates the socket and the streams associated with the connection
*
* @param mac_address The mac address of the target device.
* @return true if the connection was established succesfully, otherwise false.
* @throws IOException
*/
public boolean establishConnection(String mac_address) throws IOException{
if (!bt_adapter.isEnabled()){
return false;
}
if(connected){
return false;
}
if(bt_adapter.isEnabled()){
if(mac_address == "NONE"){
return false;
}else{
if(mac_address.substring(0, 8).compareTo(OUI_LEGO) != 0){
Log.d(TAG, "establishConnection() :: Not a Lego MAC. Prefix : " + mac_address.substring(0, 8) + " :: OUI : " + OUI_LEGO);
return false;
}else{
try{
Log.d(TAG, "establishConnection() :: Getting device with mac address: " + mac_address);
BluetoothDevice nxtDevice = null;
nxtDevice = bt_adapter.getRemoteDevice(mac_address);
if (nxtDevice == null) {
Log.e(TAG, "establishConnection() :: No device found.");
throw new IOException();
}
Log.d(TAG, "establishConnection() :: Opening socket.");
bt_socket = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);
Log.d(TAG, "establishConnection() :: Connecting.");
bt_socket.connect();
Log.d(TAG, "establishConnection() :: Opening IO streams.");
nxt_in_stream = bt_socket.getInputStream();
nxt_out_stream = bt_socket.getOutputStream();
Log.d(TAG, "establishConnection() :: Connection established.");
connected = true;
}catch(IOException e){
Log.e(TAG, "establishConnection() :: Connection failed.");
Log.e(TAG, Log.getStackTraceString(e));
connected = false;
throw e;
}
return connected;
}
}
}
return false;
}
/**
* Closes the active connection if any.
*
* Additionally clears the socket and the streams associated to said connection.
*
* @return true if the connection was succesfully closed; false if no connection exists.
* @throws IOException
*/
public boolean stopConnection() throws IOException{
try{
if(bt_socket != null){
Log.d(TAG, "stopConnection() :: Closing connection.");
bt_socket.close();
bt_socket = null;
nxt_in_stream = null;
nxt_out_stream = null;
connected = false;
Log.d(TAG, "stopConnection() :: Connection closed.");
return true;
}
}catch( IOException e){
Log.e(TAG, "stopConnection()");
Log.e(TAG, Log.getStackTraceString(e));
throw e;
}
return false;
}
/**
* Sends a message to the NXT robot.
*
* @param message The data to be sent.
* @throws IOException
*/
public synchronized void writeMessage(byte[] message) throws IOException{
if(connected){
try{
nxt_out_stream.write(message);
nxt_out_stream.flush();
}catch(IOException e){
Log.e(TAG, "writeMessage()");
Log.e(TAG, Log.getStackTraceString(e));
throw e;
}
}
}
/**
* Reads a message sent by the NXT robot.
*
* @return The data received as a byte[] if a valid connection exists, otherwise null.
* @throws IOException
*/
public synchronized byte[] readMessage(int bytes) throws IOException{
if(connected){
try{
byte[] message = new byte[bytes];
for(int i = 0; i < message.length; ++i){
message[i] = 0x00;
}
nxt_in_stream.read(message, 0, bytes);
return message;
}catch(IOException e){
Log.e(TAG, "readMessage()");
Log.e(TAG, Log.getStackTraceString(e));
throw e;
}
}else{
return null;
}
}
}

View File

@@ -0,0 +1,95 @@
package ve.ucv.ciens.ccg.nxtcam.network;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
import android.util.Log;
public class ImageTransferThread extends Thread{
private final String TAG = "IM_THREAD";
private final String CLASS_NAME = ImageTransferThread.class.getSimpleName();
private boolean pause, done, connected;
private Object threadPauseMonitor;
private CameraImageMonitor camMonitor;
private Socket socket;
private BufferedWriter writer;
private BufferedReader reader;
private byte[] image;
public ImageTransferThread(){
pause = false;
done = false;
connected = false;
threadPauseMonitor = new Object();
socket = null;
writer = null;
reader = null;
camMonitor = CameraImageMonitor.getInstance();
}
public void run(){
if(!connected){
Log.e(TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
}else{
while(!done){
checkPause();
image = camMonitor.getImageData();
// TODO: implement image transfer protocol.
}
}
}
public void connectToServer(String serverIp){
try{
if(ProjectConstants.DEBUG) Log.i(TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
socket = new Socket(InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
connected = true;
if(ProjectConstants.DEBUG) Log.i(TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
}catch(IOException io){
Log.e(TAG, CLASS_NAME + ".connectToServer() :: Connection failed with message: " + io.getMessage());
connected = false;
}
}
public synchronized void finish(){
done = true;
if(ProjectConstants.DEBUG) Log.i(TAG, CLASS_NAME + ".finish() :: Finishing thread.");
}
private void checkPause(){
synchronized (threadPauseMonitor){
while(pause){
if(ProjectConstants.DEBUG) Log.d(TAG, CLASS_NAME + ".checkPause() :: Pause requested.");
try{ threadPauseMonitor.wait(); }catch(InterruptedException ie){}
}
}
}
public synchronized void pauseThread(){
pause = true;
if(ProjectConstants.DEBUG) Log.d(TAG, CLASS_NAME + ".pauseThread() :: Pausing thread.");
}
public synchronized void resumeThread(){
if(ProjectConstants.DEBUG) Log.d(TAG, CLASS_NAME + ".resumeThread() :: Resuming thread.");
synchronized (threadPauseMonitor) {
pause = false;
threadPauseMonitor.notifyAll();
}
}
public boolean isConnected(){
return connected;
}
}

View File

@@ -0,0 +1,12 @@
package ve.ucv.ciens.ccg.nxtcam.network;
public class LCPThread extends Thread{
public LCPThread(){
}
public void run(){
}
}

View File

@@ -0,0 +1,13 @@
package ve.ucv.ciens.ccg.nxtcam.network;
public class ServiceDiscoveryThread extends Thread {
public ServiceDiscoveryThread(){
}
@Override
public void run(){
}
}

View File

@@ -0,0 +1,17 @@
package ve.ucv.ciens.ccg.nxtcam.network.protocols;
public abstract class ImageTransferProtocol{
public static enum ProtocolState{
SALUTE, IMG_FOLLOWS, SEND_DATA, PAUSED, WAITING, GOODBYE
}
public static final byte MSG_HELLO = (byte)0x89;
public static final byte MSG_GOODBYE = (byte)0x90;
public static final byte MSG_IMG_DATA = (byte)0x42;
public static final byte CMD_IMG_FOLLOWS = (byte)0x10;
public static final byte CMD_PAUSE = (byte)0x15;
public static final byte CMD_IMG_WAIT = (byte)0x20;
public static final byte ACK_SEND_IMG = (byte)0x40;
public static final byte ACK_IMG_RCVD = (byte)0x50;
}

View File

@@ -0,0 +1,248 @@
package ve.ucv.ciens.ccg.nxtcam.network.protocols;
import java.security.InvalidParameterException;
public abstract class LegoCommunicationProtocol{
/**
* Command types. Byte 0;
*/
private static final byte DIRECT_COMMAND_REPLY = 0x00;
private static final byte SYSTEM_COMMAND_REPLY = 0x01;
private static final byte DIRECT_COMMAND_NO_REPLY = (byte)0x80;
/**
* Comand bytes. Byte 1;
*/
private static final byte GET_FIRMWARE_VERSION = (byte)0x88;
private static final byte GET_DEVICE_INFO = (byte)0x9B;
private static final byte SET_OUTPUT_STATE = 0x04;
private static final byte SET_INPUT_MODE = 0x05;
private static final byte GET_OUTPUT_STATE = 0x06;
private static final byte GET_INPUT_VALUES = 0x07;
/**
* Ports for get/setOutputState() and get/setInputMode().
*/
public static final byte PORT_0 = 0x00;
public static final byte PORT_1 = 0x01;
public static final byte PORT_2 = 0x02;
public static final byte PORT_3 = 0x03;
/**
* Mode bytes for setOutputState().
*/
public static final byte MOTORON = 0x01;
public static final byte BRAKE = 0x02;
public static final byte REGULATED = 0x04;
/**
* Regulation modes for setOutputState().
*/
public static final byte REGULATION_MODE_IDLE = 0x00;
public static final byte REGULATION_MODE_MOTOR_SPEED = 0x01;
public static final byte REGULATION_MODE_MOTOR_SYNC = 0x02;
/**
* Run states for setOutputState().
*/
public static final byte MOTOR_RUN_STATE_IDLE = 0x00;
public static final byte MOTOR_RUN_STATE_RAMPUP = 0x10;
public static final byte MOTOR_RUN_STATE_RUNNING = 0x20;
public static final byte MOTOR_RUN_STATE_RAMPDOWN = 0x40;
/**
* Sensor types for setInputMode().
*/
public static final byte NO_SENSOR = 0x00;
public static final byte SWITCH = 0x01;
public static final byte TEMPERATURE = 0x02;
public static final byte REFLECTION = 0x03;
public static final byte ANGLE = 0x04;
public static final byte LIGHT_ACTIVE = 0x05;
public static final byte LIGHT_INACTIVE = 0x06;
public static final byte SOUND_DB = 0x07;
public static final byte SOUND_DBA = 0x08;
public static final byte CUSTOM = 0x09;
public static final byte LOWSPEED = 0x0A;
public static final byte LOWSPEED_9V = 0x0B;
public static final byte NO_OF_SENSOR_TYPES = 0x0C;
/**
* Sensor modes for setInputMode().
*/
public static final byte RAWMODE = 0x00;
public static final byte BOOLEANMODE = 0x20;
public static final byte TRANSITIONCNTMODE = 0x40;
public static final byte PERIODCOUNTERMODE = 0x60;
public static final byte PCTFULLSCALEMODE = (byte)0x80;
public static final byte CELSIUSMODE = (byte)0xA0;
public static final byte FARENHEITMODE = (byte)0xC0;
public static final byte ANGLESTEPMODE = (byte)0xE0;
public static final byte SLOPEMASK = (byte)0x1F;
public static final byte MODEMASK = (byte)0xE0;
/**
* Firmware and protocol version request pdu. Page 11 of appendix 1.
*
* @return byte[4], the pdu.
*/
public static byte[] getFirmwareVersion(){
byte[] message = new byte[4];
message[0] = 0x02;
message[1] = 0x00;
message[2] = SYSTEM_COMMAND_REPLY;
message[3] = GET_FIRMWARE_VERSION;
return message;
}
/**
* Device info request pdu. Page 14 of appendix 1.
*
* @return byte[4], the pdu.
*/
public static byte[] getDeviceInfo(){
byte[] message = new byte[4];
message[0] = 0x02;
message[1] = 0x00;
message[2] = SYSTEM_COMMAND_REPLY;
message[3] = GET_DEVICE_INFO;
return message;
}
/**
* Set motor configuration pdu. Page 6 of appendix 2.
*
* @param output_port The port in the brick the motor is connected to.
* @param power_set_point
* @param mode_byte
* @param regulation_mode
* @param turn_ratio
* @param run_state
* @return byte[15], the pdu.
* @throws InvalidParameterException When any parameter is out of range or is an invalid enum. Ranges defined in appendix 2.
*/
public static byte[] setOutputState(byte output_port, byte power_set_point, byte mode_byte, byte regulation_mode, byte turn_ratio, byte run_state) throws InvalidParameterException{
byte[] message = new byte[15];
if(output_port < PORT_0 || output_port > PORT_2){
throw new InvalidParameterException("Output port out of range.");
}
if(power_set_point < -100 || power_set_point > 100){
throw new InvalidParameterException("Power set point out of range.");
}
if(turn_ratio < -100 || turn_ratio > 100){
throw new InvalidParameterException("Turn ratio out of range.");
}
if(mode_byte != MOTORON && mode_byte != BRAKE && mode_byte != REGULATED){
throw new InvalidParameterException("Invalid mode byte.");
}
if(regulation_mode != REGULATION_MODE_IDLE && regulation_mode != REGULATION_MODE_MOTOR_SPEED && regulation_mode != REGULATION_MODE_MOTOR_SYNC){
throw new InvalidParameterException("Invalid regulation mode.");
}
if(run_state != MOTOR_RUN_STATE_IDLE && run_state != MOTOR_RUN_STATE_RAMPUP && run_state != MOTOR_RUN_STATE_RUNNING && run_state != MOTOR_RUN_STATE_RAMPDOWN){
throw new InvalidParameterException("Invalid run state.");
}
message[0] = 0x0C;
message[1] = 0x00;
message[2] = DIRECT_COMMAND_NO_REPLY;
message[3] = SET_OUTPUT_STATE;
message[4] = output_port;
message[5] = power_set_point;
message[6] = mode_byte;
message[7] = regulation_mode;
message[8] = turn_ratio;
message[9] = run_state;
message[10] = message[11] = message[12] = message[13] = message[14] = 0x00;
return message;
}
/**
* Motor configuration request pdu. Page 8 of appendix 2.
*
* @param output_port The port in the brick the motor is connected to.
* @return byte[5], the pdu.
* @throws InvalidParameterException When any parameter is out of range or is an invalid enum. Ranges defined in appendix 2.
*/
public static byte[] getOutputState(byte output_port) throws InvalidParameterException{
byte[] message = new byte[5];
if(output_port < PORT_0 || output_port > PORT_2){
throw new InvalidParameterException("Output port out of range.");
}
message[0] = 0x03;
message[1] = 0x00;
message[2] = DIRECT_COMMAND_REPLY;
message[3] = GET_OUTPUT_STATE;
message[4] = output_port;
return message;
}
/**
* Sensor feed request pdu. Page 8 of appendix 2.
*
* @param input_port The port in the brick the sensor is connected to.
* @return byte[5], the pdu.
* @throws InvalidParameterException When any parameter is out of range or is an invalid enum. Ranges defined in appendix 2.
*/
public static byte[] getInputValues(byte input_port) throws InvalidParameterException{
byte[] message = new byte[5];
if(input_port < PORT_0 || input_port > PORT_3){
throw new InvalidParameterException("Input port is out of range.");
}
message[0] = 0x03;
message[1] = 0x00;
message[2] = DIRECT_COMMAND_REPLY;
message[3] = GET_INPUT_VALUES;
message[4] = input_port;
return message;
}
/**
* Sensor configuration pdu.
*
* @param input_port The port in the brick the sensor is connected to.
* @param sensor_type The sensor to be configured.
* @param sensor_mode The configuration to set.
* @return byte[7], the pdu.
* @throws InvalidParameterException When any parameter is out of range or is an invalid enum. Ranges defined in appendix 2.
*/
public static byte[] setInputMode(byte input_port, byte sensor_type, byte sensor_mode) throws InvalidParameterException{
byte[] message = new byte[7];
if(input_port < PORT_0 || input_port > PORT_3){
throw new InvalidParameterException("Input port is out of range.");
}
if(sensor_type < 0x00 || sensor_type > 0x0C){
throw new InvalidParameterException("Invalid sensor type.");
}
message[0] = 0x05;
message[1] = 0x00;
message[2] = DIRECT_COMMAND_NO_REPLY;
message[3] = SET_INPUT_MODE;
message[4] = input_port;
message[5] = sensor_type;
switch(sensor_mode){
case RAWMODE:
case BOOLEANMODE:
case TRANSITIONCNTMODE:
case PERIODCOUNTERMODE:
case PCTFULLSCALEMODE:
case CELSIUSMODE:
case FARENHEITMODE:
case ANGLESTEPMODE: // Same case as MODEMASK.
case SLOPEMASK:
message[6] = sensor_mode;
break;
}
return message;
}
}