Source ruffy@eb659b3aa47a1ed699789df7e50e0d80c4640cc3, with pumpdata hackery.

This commit is contained in:
Johannes Mockenhaupt 2017-10-18 21:37:11 +02:00
parent dca219d900
commit 52d3803275
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
39 changed files with 8459 additions and 96 deletions

View file

@ -105,6 +105,12 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<service
android:name="org.monkey.d.ruffy.ruffy.driver.Ruffy"
android:enabled="true"
android:exported="false">
</service>
<!-- Service processing incomming data --> <!-- Service processing incomming data -->
<service <service
android:name=".Services.DataService" android:name=".Services.DataService"

View file

@ -13,6 +13,7 @@ import com.google.common.base.Joiner;
import org.monkey.d.ruffy.ruffy.driver.IRTHandler; import org.monkey.d.ruffy.ruffy.driver.IRTHandler;
import org.monkey.d.ruffy.ruffy.driver.IRuffyService; import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
import org.monkey.d.ruffy.ruffy.driver.Ruffy;
import org.monkey.d.ruffy.ruffy.driver.display.Menu; import org.monkey.d.ruffy.ruffy.driver.display.Menu;
import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute; import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute;
import org.monkey.d.ruffy.ruffy.driver.display.MenuType; import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
@ -136,18 +137,7 @@ public class RuffyScripter implements RuffyCommands {
boolean boundSucceeded = false; boolean boundSucceeded = false;
try { try {
Intent intent = new Intent() Intent intent = new Intent(context, Ruffy.class);
.setComponent(new ComponentName(
// this must be the base package of the app (check package attribute in
// manifest element in the manifest file of the providing app)
"org.monkey.d.ruffy.ruffy",
// full path to the driver;
// in the logs this service is mentioned as (note the slash)
// "org.monkey.d.ruffy.ruffy/.driver.Ruffy";
// org.monkey.d.ruffy.ruffy is the base package identifier
// and /.driver.Ruffy the service within the package
"org.monkey.d.ruffy.ruffy.driver.Ruffy"
));
context.startService(intent); context.startService(intent);
ServiceConnection mRuffyServiceConnection = new ServiceConnection() { ServiceConnection mRuffyServiceConnection = new ServiceConnection() {

View file

@ -0,0 +1,24 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.nio.ByteBuffer;
/**
* Callbacks for the Application-Layer
*/
public interface AppHandler {
void log(String s);
void connected();
void rtModeActivated();
void modeDeactivated();
void addDisplayFrame(ByteBuffer b);
void modeError();
void sequenceError();
void error(short error, String desc);
}

View file

@ -0,0 +1,369 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
/**
* Some statics to do some things on application level
*/
public class Application {
/**
* how often we accept an mode error before accepting we are in the wrong mode
*/
public static int MODE_ERROR_TRESHHOLD = 3;
public static enum Command
{
COMMANDS_SERVICES_VERSION,
REMOTE_TERMINAL_VERSION,
BINDING,
COMMAND_MODE,
COMMAND_DEACTIVATE,
RT_MODE,
RT_DEACTIVATE,
DEACTIVATE_ALL,
APP_DISCONNECT,
CMD_PING,
}
public static void sendAppConnect(BTConnection btConn) {
ByteBuffer payload = null;
byte[] connect_app_layer = {16, 0, 85, -112};
payload = ByteBuffer.allocate(8); //4 bytes for application header, 4 for payload
payload.put(connect_app_layer); //Add prefix array
payload.order(ByteOrder.LITTLE_ENDIAN);
payload.putInt(Integer.parseInt("12345"));
Application.sendData(payload,true,btConn);
}
private static void sendData(ByteBuffer payload, boolean reliable, BTConnection btConn) {
btConn.getPumpData().incrementNonceTx();
byte[] sendR = {16,3,0,0,0};
List<Byte> packet = Packet.buildPacket(sendR, payload, true,btConn); //Add the payload, set the address if valid
if(reliable) {
int seq = btConn.seqNo;
packet.set(1, setSeqRel(packet.get(1), true,btConn)); //Set the sequence and reliable bits
}
Packet.adjustLength(packet, payload.capacity()); //Set the payload length
packet = Utils.ccmAuthenticate(packet, btConn.getPumpData().getToPumpKey(), btConn.getPumpData().getNonceTx()); //Authenticate packet
List<Byte> temp = Frame.frameEscape(packet);
byte[] ro = new byte[temp.size()];
int i = 0;
for(byte b : temp)
ro[i++]=b;
btConn.write(ro);
}
private static Byte setSeqRel(Byte b, boolean rel, BTConnection btConn)
{
b = (byte) (b | btConn.seqNo); //Set the sequence bit
if(rel)
b = (byte) (b |0x20); //Set the reliable bit
btConn.seqNo ^= 0x80;
return b;
}
private static byte[] service_activate = {16, 0, 0x66, (byte)0x90};
private static byte[] service_deactivate = {16, 0, 0x69, (byte)0x90};
public static void sendAppCommand(Command command, BTConnection btConn){
ByteBuffer payload = null;
String s = "";
boolean reliable = true;
switch(command)
{
case COMMAND_MODE:
s = "COMMAND_ACTIVATE";
payload = ByteBuffer.allocate(7);
payload.put(service_activate);
payload.put((byte)0xB7);
payload.put((byte)0x01);
payload.put((byte)0x00);
reliable = true;
break;
case COMMAND_DEACTIVATE:
s = "COMMAND DEACTIVATE";
payload = ByteBuffer.allocate(5);
payload.put(service_deactivate);
payload.put((byte)0xB7);
reliable = true;
break;
case RT_MODE:
s = "RT_ACTIVATE";
payload = ByteBuffer.allocate(7);
payload.put(service_activate);
payload.put((byte)0x48);
payload.put((byte)0x01);
payload.put((byte)0x00);
reliable = true;
break;
case RT_DEACTIVATE:
s = "RT DEACTIVATE";
payload = ByteBuffer.allocate(5);
payload.put(service_deactivate);
payload.put((byte)0x48);
reliable = true;
break;
case COMMANDS_SERVICES_VERSION:
s = "COMMAND_SERVICES_VERSION";
payload = ByteBuffer.allocate(5);
payload.put((byte)16);
payload.put((byte)0);
payload.put((byte) (((short)0x9065) & 0xFF));
payload.put((byte) ((((short)0x9065)>>8) & 0xFF));
payload.put((byte)0xb7);
reliable = true;
break;
case REMOTE_TERMINAL_VERSION:
s = "REMOTE_TERMINAL_VERSION";
payload = ByteBuffer.allocate(5);
payload.put((byte)16);
payload.put((byte)0);
payload.put((byte) (((short)0x9065) & 0xFF));
payload.put((byte) ((((short)0x9065)>>8) & 0xFF));
payload.put((byte)0x48);
reliable = true;
break;
case BINDING:
s = "BINDING";
payload = ByteBuffer.allocate(5);
payload.put((byte)16);
payload.put((byte)0);
payload.put((byte) (((short)0x9095) & 0xFF));
payload.put((byte) ((((short)0x9095)>>8) & 0xFF));
payload.put((byte) 0x48); //Binding OK
reliable = true;
break;
case APP_DISCONNECT:
byte[] connect_app_layer = {16 , 0, 0x5a, 0x00};
payload = ByteBuffer.allocate(6); //4 bytes for application header, 4 for payload
payload.put(connect_app_layer); //Add prefix array
payload.order(ByteOrder.LITTLE_ENDIAN);
payload.putShort((byte)0x6003);
reliable = true;
break;
case CMD_PING:
payload.put((byte)16);
payload.put((byte)0xB7);
payload.put((byte) (0x9AAA & 0xFF));
payload.put((byte) ((0x9AAA>>8) & 0xFF));
reliable = false;
break;
default:
s = "uknown subcommand: "+command;
break;
}
if(payload != null)
{
sendData(payload,reliable,btConn);
}
}
public static void sendAppDisconnect(BTConnection btConn) {
sendAppCommand(Command.APP_DISCONNECT,btConn);
}
public static void cmdPing(BTConnection btConn)
{
sendAppCommand(Command.CMD_PING,btConn);
}
public static short sendRTKeepAlive(short rtSeq, BTConnection btConn) {
ByteBuffer payload = ByteBuffer.allocate(6);
payload.put((byte)16);
payload.put((byte)0x48);
payload.put((byte) (0x0566 & 0xFF));
payload.put((byte) ((0x0566>>8) & 0xFF));
payload.put((byte) (rtSeq & 0xFF));
payload.put((byte) ((rtSeq>>8) & 0xFF));
sendData(payload,false,btConn);
btConn.log("/////////////////////////////////////////////////////////////////////");
btConn.log("send alive with seq: "+rtSeq);
btConn.log("/////////////////////////////////////////////////////////////////////");
rtSeq++;
return rtSeq;
}
public static void cmdErrStatus(BTConnection btConn)
{
ByteBuffer payload = ByteBuffer.allocate(4);
payload.put((byte)16);
payload.put((byte)0x48);
payload.put((byte) (0x9AA5 & 0xFF));
payload.put((byte) ((0x9AA5>>8) & 0xFF));
sendData(payload, true, btConn);
}
public static short rtSendKey(byte key, boolean changed,short rtSeq, BTConnection btConn)
{
ByteBuffer payload = ByteBuffer.allocate(8);
payload.put((byte)16);
payload.put((byte)0x48);
payload.put((byte) 0x65);
payload.put((byte) 0x05);
payload.put((byte) (rtSeq & 0xFF));
payload.put((byte) ((rtSeq>>8) & 0xFF));
payload.put(key);
if(changed)
payload.put((byte) 0xB7);
else
payload.put((byte) 0x48);
btConn.log("/////////////////////////////////////////////////////////////////////");
String k = "";
switch (key)
{
case 0x00:
k="NOKEY";
break;
case 0x03:
k="MENU";
break;
case 0x0C:
k="CHECK";
break;
case 0x30:
k="UP";
break;
case (byte)0xC0:
k="DOWN";
break;
}
btConn.log("send key "+k+" with seq: "+rtSeq);
btConn.log("/////////////////////////////////////////////////////////////////////");
sendData(payload, false, btConn);
rtSeq++;
return rtSeq;
}
public static void processAppResponse(byte[] payload, boolean reliable, AppHandler handler) {
handler.log("processing app response");
ByteBuffer b = ByteBuffer.wrap(payload);
b.order(ByteOrder.LITTLE_ENDIAN);
b.get();//ignore
byte servId = b.get();
short commId = b.getShort();
handler.log("Service ID: " + String.format("%X", servId) + " Comm ID: " + String.format("%X", commId) + " reliable: " + reliable);
String descrip = null;
if(reliable)
{
short error = b.getShort();
if (!cmdProcessError(error,handler)) {
return;
}
switch (commId) {
case (short) 0xA055://connect answer:
handler.connected();
break;
case (short) 0xA065://something
case (short) 0xA095://bind
handler.log("not should happen here!");
break;
case (short) 0xA066://activate rt:
handler.rtModeActivated();
break;
case (short) 0x005A://AL_DISCONNECT_RES:
descrip = "AL_DISCONNECT_RES";
break;
case (short) 0xA069://service deactivated
descrip = "AL_DEACTIVATE_RES";
handler.modeDeactivated();
break;
case (short) 0xA06A://service all deactivate
descrip = "AL_DEACTIVATE_ALL_RES";
handler.modeDeactivated();
break;
default:
descrip = "UNKNOWN";
break;
}
} else {
switch (commId) {
case (short) 0x0555://Display frame
handler.addDisplayFrame(b);
break;
case (short) 0x0556://key answer
break;
case (short) 0x0566://alive answer, often missed
break;
default:
descrip = "UNKNOWN";
break;
}
}
handler.log("appProcess: "+descrip);
}
private static boolean cmdProcessError(short error, AppHandler handler) {
String desc = "Error > " + String.format("%X", error) + " ";
if (error == 0x0000) {
desc = "No error found!";
return true;
} else {
switch (error) {
//Application Layer **********************************************//
case (short) 0xF003:
desc = "Unknown Service ID, AL, RT, or CMD";
break;
case (short) 0xF006:
desc = "Invalid payload length";
break;
case (short) 0xF05F:
desc = "wrong mode";
handler.modeError();
break;
case (short) 0xF50C:
desc = "wrong sequence";
handler.sequenceError();
break;
case (short) 0xF533:
desc = "died - no alive";
break;
case (short) 0xF056:
desc = "not complete connected";
break;
}
handler.error(error,desc);
return false;
}
}
}

View file

@ -0,0 +1,298 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.IntentFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Created by SandraK82 on 15.05.17.
*/
public class BTConnection {
private final BTHandler handler;
private BluetoothAdapter bluetoothAdapter;
private ListenThread listen;
private BluetoothSocket currentConnection;
public int seqNo;
private InputStream currentInput;
private OutputStream currentOutput;
private PairingRequest pairingReciever;
private ConnectReceiver connectReceiver;
private PumpData pumpData;
public BTConnection(final BTHandler handler)
{
this.handler = handler;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
handler.requestBlueTooth();
}
}
public void makeDiscoverable(Activity activity) {
this.pumpData = new PumpData(activity);
IntentFilter filter = new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST");
pairingReciever = new PairingRequest(activity, handler);
activity.registerReceiver(pairingReciever, filter);
Intent discoverableIntent = new Intent("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE");
discoverableIntent.putExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 60);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
activity.startActivity(discoverableIntent);
BluetoothServerSocket srvSock = null;
try {
srvSock = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("SerialLink", UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
} catch (IOException e) {
handler.fail("socket listen() failed");
return;
}
final BluetoothServerSocket lSock = srvSock;
listen = new ListenThread(srvSock);
filter = new IntentFilter("android.bluetooth.device.action.ACL_CONNECTED");
connectReceiver = new ConnectReceiver(handler);
activity.registerReceiver(connectReceiver, filter);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 1 );
scheduler.execute(listen);
}
public void stopDiscoverable() {
if(listen!=null)
{
listen.halt();
}
if(bluetoothAdapter.isDiscovering())
{
bluetoothAdapter.cancelDiscovery();
}
}
public void connect(BluetoothDevice device) {
connect(device.getAddress(), 4);
}
public void connect(PumpData pumpData, int retries)
{
this.pumpData = pumpData;
connect(pumpData.getPumpMac(),retries);
}
private int state = 0;
private void connect(String deviceAddress, int retry) {
if(state!=0)
{
handler.log("in connect!");
return;
}
state=1;
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
BluetoothSocket tmp = null;
try {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
tmp = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
} catch (IOException e) {
handler.log("socket create() failed: "+e.getMessage());
}
if(tmp != null) {
stopDiscoverable();
activateConnection(tmp);
}
else
{
handler.log("failed the pump connection( retries left: "+retry+")");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(retry>0)
{
connect(deviceAddress,retry-1);
}
else
{
handler.fail("Failed to connect");
}
}
}
private void startReadThread() {
new Thread() {
@Override
public void run() {
try {
currentConnection.connect();//This method will block until a connection is made or the connection fails. If this method returns without an exception then this socket is now connected.
currentInput = currentConnection.getInputStream();
currentOutput = currentConnection.getOutputStream();
} catch (IOException e) {
//e.printStackTrace();
handler.fail("no connection possible");
//??????????
//state=1;
//return;
}
try {
pumpData.getActivity().unregisterReceiver(connectReceiver);
}catch(Exception e){/*ignore*/}
try {
pumpData.getActivity().unregisterReceiver(pairingReciever);
}catch(Exception e){/*ignore*/}
state=0;
//here check if really connected!
//this will start thread to write
handler.deviceConnected();//in ruffy.java
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
byte[] buffer = new byte[512];
while (true) {
try {
int bytes = currentInput.read(buffer);
handler.log("read "+bytes+": "+ Utils.byteArrayToHexString(buffer,bytes));
handler.handleRawData(buffer,bytes);
} catch (Exception e) {
//e.printStackTrace();
//do not fail here as we maybe just closed the socket..
handler.log("got error in read");
return;
}
}
}
}.start();
}
public void writeCommand(byte[] key) {
List<Byte> out = new LinkedList<Byte>();
for(Byte b : key)
out.add(b);
for (Byte n : pumpData.getNonceTx())
out.add(n);
Utils.addCRC(out);
List<Byte> temp = Frame.frameEscape(out);
byte[] ro = new byte[temp.size()];
int i = 0;
for(byte b : temp)
ro[i++]=b;
StringBuilder sb = new StringBuilder();
for (i = 0; i < key.length; i++) {
sb.append(String.format("%02X ", key[i]));
}
handler.log("writing command: "+sb.toString());
write(ro);
}
private void activateConnection(BluetoothSocket newConnection){
if(this.currentConnection!=null)
{
try {
this.currentOutput.close();
} catch (Exception e) {/*ignore*/}
try {
this.currentInput.close();
} catch (Exception e) {/*ignore*/}
try {
this.currentConnection.close();
} catch (Exception e) {/*ignore*/}
this.currentInput=null;
this.currentOutput=null;
this.currentConnection=null;
handler.log("closed current Connection");
}
handler.log("got new Connection: "+newConnection);
this.currentConnection = newConnection;
if(newConnection!=null)
{
startReadThread();
}
}
public void write(byte[] ro){
handler.log("!!!write!!!");
if(this.currentConnection==null)
{
handler.fail("unable to write: no socket");
return;
}
try {
currentOutput.write(ro);
handler.log("wrote "+ro.length+" bytes: "+ Utils.byteArrayToHexString(ro,ro.length));
}catch(Exception e)
{
//e.printStackTrace();
handler.fail("failed write of "+ro.length+" bytes!");
}
}
public void log(String s) {
if(handler!=null)
handler.log(s);
}
public void disconnect() {
try {
this.currentOutput.close();
} catch (Exception e) {/*ignore*/}
try {
this.currentInput.close();
} catch (Exception e) {/*ignore*/}
try {
this.currentConnection.close();
} catch (Exception e) {/*ignore*/}
this.currentInput=null;
this.currentOutput=null;
this.currentConnection=null;
this.pumpData = null;
handler.log("disconnect() closed current Connection");
}
public PumpData getPumpData() {
return pumpData;
}
public boolean isConnected() {
return this.currentConnection != null && this.currentConnection.isConnected();
}
}

View file

@ -0,0 +1,21 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.bluetooth.BluetoothDevice;
/**
* Created by fishermen21 on 15.05.17.
*/
public interface BTHandler {
void deviceConnected();
void log(String s);
void fail(String s);
void deviceFound(BluetoothDevice bd);
void handleRawData(byte[] buffer, int bytes);
void requestBlueTooth();
}

View file

@ -0,0 +1,9 @@
package org.monkey.d.ruffy.ruffy.driver;
/**
* Created by fishermen21 on 20.05.17.
*/
public interface CompleteDisplayHandler {
void handleCompleteFrame(byte[][] pixels);
}

View file

@ -0,0 +1,44 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by fishermen21 on 15.05.17.
*/
public class ConnectReceiver extends BroadcastReceiver {
private final BTHandler handler;
public ConnectReceiver(BTHandler handler)
{
this.handler = handler;
}
public void onReceive(Context context, Intent intent) {
for(String k: intent.getExtras().keySet())
{
if(k.equals(BluetoothDevice.EXTRA_DEVICE))
{
if(intent.getStringExtra("address")== null)
{
BluetoothDevice bd = ((BluetoothDevice)intent.getExtras().get(k));
String address = bd.getAddress();
intent.getExtras().putString("address",address);
if (address.substring(0, 8).equals("00:0E:2F")) {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
handler.log("Pump found: "+address);
handler.deviceFound(bd);
}
}
}
}
}
}

View file

@ -0,0 +1,87 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.nio.ByteBuffer;
public class Display {
private final DisplayUpdater updater;
private int index = -1;
private CompleteDisplayHandler completeHandler;
private boolean[] complete = {false,false,false,false};
private byte[][] displayBytes = new byte[4][];
public Display(DisplayUpdater updater)
{
this.updater = updater;
}
public void setCompletDisplayHandler(CompleteDisplayHandler completeHandler) {this.completeHandler = completeHandler;}
private void update(byte[] rowBytes, boolean quarter[][], int which, int index)
{
updater.update(rowBytes,which);
if(this.index==index)
{
complete[which]=true;
this.displayBytes[which] = rowBytes;
if(this.completeHandler != null && complete[0] && complete[1] && complete[2] && complete[3])
completeHandler.handleCompleteFrame(this.displayBytes);
}
else
{
this.index = index;
complete = new boolean[]{false,false,false,false};
complete[which] = true;
this.displayBytes= new byte[4][];
this.displayBytes[which] = rowBytes;
}
}
public void addDisplayFrame(ByteBuffer b)
{
//discard first 3
b.getShort();
b.get();
int index = (int)(b.get() & 0xFF);
byte row = b.get();
byte[] displayBytes = new byte[96]; //New array
b.get(displayBytes); //Read in array from packet
boolean[][] quarter = new boolean[8][96];
int column = 96;
for(byte d:displayBytes)
{
column--;
quarter[0][column] = ((d & 0x01)!=0);
quarter[1][column] = ((d & 0x02)!=0);
quarter[2][column] = ((d & 0x04)!=0);
quarter[3][column] = ((d & 0x08)!=0);
quarter[4][column] = ((d & 0x10)!=0);
quarter[5][column] = ((d & 0x20)!=0);
quarter[6][column] = ((d & 0x40)!=0);
quarter[7][column] = ((d & 0x80)!=0);
}
switch(row)
{
case 0x47:
update(displayBytes,quarter,0,index);
break;
case 0x48:
update(displayBytes,quarter,1,index);
break;
case (byte)0xB7:
update(displayBytes,quarter,2,index);
break;
case (byte)0xB8:
update(displayBytes,quarter,3,index);
break;
}
}
}

View file

@ -0,0 +1,11 @@
package org.monkey.d.ruffy.ruffy.driver;
/**
* Created by fishermen21 on 19.05.17.
*/
public interface DisplayUpdater {
void clear();
void update(byte[] quarter, int which);
}

View file

@ -0,0 +1,124 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.util.ArrayList;
import java.util.List;
/**
* Created by fishermen21 on 16.05.17.
*/
public class Frame {
public static List<Byte> frameEscape(List<Byte> out)
{
List<Byte> temp = new ArrayList();
temp.add((byte)-52);
for (int i = 0; i < out.size(); i++) {
if (((Byte) out.get(i)).byteValue() == -52) {
temp.add((byte)119);
temp.add((byte)-35);
} else if (((Byte) out.get(i)).byteValue() == (byte) 119) {
temp.add((byte) 119);
temp.add((byte) -18);
} else {
temp.add((Byte) out.get(i));
}
}
temp.add((byte)-52);
return temp;
}
private static List<Byte> packet = new ArrayList<Byte>();
private static boolean start = false, stop = false, escaped = false;
public static List<List<Byte>> frameDeEscaping(List<Byte> buffer)
{
List<List<Byte>> complete = new ArrayList<List<Byte>>();
if(start)
{
}
else
{
start = stop = escaped = false;
packet.clear();
}
for(int i=0;i<buffer.size();i++)
{
if(escaped == true)
{
escaped = false;
if(buffer.get(i) == -35)
{
packet.add((byte)-52);
}
else if(buffer.get(i) == -18)
{
packet.add((byte)119);
}
}
else if(buffer.get(i) == 119)
{
if(i+1 >= buffer.size())
{
escaped = true; //If we are at the end of the buffer and find an escape character
}
else
{
Byte next = buffer.get(i+1);
if(next == -35)
{
packet.add((byte)-52);
i++; //Skip the next byte
}
else if(next == -18)
{
packet.add((byte)119); //Skip the next byte
i++;
}
}
}
else if(buffer.get(i) == -52) //We need to cover the chance that there are multiple packets in the buffer
{
if(!start)
{
start = true;
}
else
{
stop = true;
}
if(start && stop)
{
start = false;
stop = false;
if(packet.size() == 0)
{
start = true;
stop = false;
}
else if(i == 0)
{
start = true;
stop = false;
}
else
{
complete.add(packet);
packet = new ArrayList<Byte>();
}
}
}
else
{
if(start)
packet.add(buffer.get(i));
}
}
return complete;
}
}

View file

@ -0,0 +1,43 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
/**
* Created by fishermen21 on 15.05.17.
*/
class ListenThread implements Runnable {
private final BluetoothServerSocket srvSock;
public ListenThread(BluetoothServerSocket srvSock) {
this.srvSock = srvSock;
}
public void run() {
BluetoothSocket socket = null;
try {
if (socket != null) {
socket = srvSock.accept();
}
if (socket != null) {
socket.close();
socket=null;
}
}
catch(Exception e)
{
}
}
public void halt()
{
try {
srvSock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,11 @@
package org.monkey.d.ruffy.ruffy.driver;
/**
* Created by fishermen21 on 20.05.17.
*/
public interface LogHandler {
void log(String s);
void fail(String s);
}

View file

@ -0,0 +1,203 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by fishermen21 on 16.05.17.
*/
public class Packet {
public static byte[] padPacket(byte[] packet)
{
byte pad;
byte[] output;
pad = (byte) (16 - (packet.length % 16)); //(ENCRYPT_BLOCKSIZE - (packet.length % ENCRYPT_BLOCKSIZE));
if(pad > 0)
{
output = new byte[pad+packet.length];
for(int n=0;n<packet.length;n++)
output[n] = packet[n];
for(int i=0;i < pad;i++)
{
output[packet.length+i] = pad;
}
}
else
output = packet;
return output;
}
public static List<Byte> buildPacket(byte[] command, ByteBuffer payload, boolean address, BTConnection btConn)
{
List<Byte> output = new ArrayList<Byte>();
for(int i=0; i < command.length; i++)
output.add(command[i]);
if(address) //Replace the default address with the real one
{
output.remove(command.length-1); //Remove the last byte (address)
output.add(btConn.getPumpData().getAddress()); //Add the real address byte
}
Packet.addNonce(output, btConn.getPumpData().getNonceTx());
if(payload!=null)
{
payload.rewind();
for(int i=0;i<payload.capacity();i++)
output.add(payload.get());
}
return output;
}
public static void adjustLength(List<Byte> packet, int length)
{
packet.set(2, (byte) (length & 0xFF));
packet.set(3, (byte) (length >> 8));
}
public static void addNonce(List<Byte> packet, byte[] nonce)
{
for(int i=0;i<nonce.length;i++)
packet.add(nonce[i]);
}
public static void handleRawData(byte buffer[], int bytes, PacketHandler handler) {
List<Byte> t = new ArrayList<>();
for (int i = 0; i < bytes; i++)
t.add(buffer[i]);
for (List<Byte> x : Frame.frameDeEscaping(t)) {
byte[] xx = new byte[x.size()];
for (int i = 0; i < x.size(); i++)
xx[i] = x.get(i);
boolean rel = false;
if (x.size()>1 && (x.get(1) & 0x20) == 0x20) {
rel = true;
byte seq = 0x00;
if ((x.get(1) & 0x80) == 0x80)
seq = (byte) 0x80;
handler.sendImidiateAcknowledge(seq);
} else {
rel = false;
}
handleRX(xx, x.size(), rel,handler);
}
}
public static enum Response{
ID,
SYNC,
RELIABLE_DATA,
UNRELIABLE_DATA
}
private static void handleRX(byte[] inBuf, int length, boolean reliableFlagged, PacketHandler handler) {
ByteBuffer buffer = ByteBuffer.wrap(inBuf, 0, length);
buffer.order(ByteOrder.LITTLE_ENDIAN);
byte[] nonce, payload, umac, packetNoUmac;
Byte command;
buffer.get(); //ignore
command = buffer.get();
short payloadlength = buffer.getShort();
buffer.get(); //ignore
nonce = new byte[13];
buffer.get(nonce, 0, nonce.length);
payload = new byte[payloadlength];
buffer.get(payload, 0, payload.length);
umac = new byte[8];
buffer.get(umac, 0, umac.length);
packetNoUmac = new byte[buffer.capacity() - umac.length];
buffer.rewind();
for (int i = 0; i < packetNoUmac.length; i++)
packetNoUmac[i] = buffer.get();
buffer.rewind();
byte c = (byte)(command & 0x1F);
switch (c) {
case 20:
handler.log("got an id response");
if (Utils.ccmVerify(packetNoUmac, handler.getToDeviceKey(), umac, nonce)) {
handler.handleResponse(Response.ID,reliableFlagged,payload);
}
break;
case 24:
handler.log("got a sync response ");
if (Utils.ccmVerify(packetNoUmac, handler.getToDeviceKey(), umac, nonce)) {
handler.handleResponse(Response.SYNC,reliableFlagged,payload);
}
break;
case 0x23:
if (Utils.ccmVerify(packetNoUmac, handler.getToDeviceKey(), umac, nonce)) {
handler.handleResponse(Response.RELIABLE_DATA,reliableFlagged,payload);
}
break;
case 0x03:
if (Utils.ccmVerify(packetNoUmac, handler.getToDeviceKey(), umac, nonce)) {
handler.handleResponse(Response.UNRELIABLE_DATA,reliableFlagged,payload);
}
break;
case 0x06:
if(Utils.ccmVerify(packetNoUmac, handler.getToDeviceKey(), umac, nonce))
{
byte error = 0;
String err = "";
if(payload.length > 0)
error = payload[0];
switch(error)
{
case 0x00:
err = "Undefined";
break;
case 0x0F:
err = "Wrong state";
break;
case 0x33:
err = "Invalid service primitive";
break;
case 0x3C:
err = "Invalid payload length";
break;
case 0x55:
err = "Invalid source address";
break;
case 0x66:
err = "Invalid destination address";
break;
}
handler.log( "Error in Transport Layer! ("+err+")");
handler.handleErrorResponse(error,err,reliableFlagged,payload);
}
break;
default:
handler.log("not yet implemented rx command: " + command + " ( " + String.format("%02X", command));
}
}
}

View file

@ -0,0 +1,17 @@
package org.monkey.d.ruffy.ruffy.driver;
/**
* Created by fishermen21 on 19.05.17.
*/
public interface PacketHandler {
void sendImidiateAcknowledge(byte sequenceNUmber);
void log(String s);
void handleResponse(Packet.Response response, boolean reliableFlagged, byte[] payload);
void handleErrorResponse(byte errorCode, String errDecoded, boolean reliableFlagged, byte[] payload);
Object getToDeviceKey();
}

View file

@ -0,0 +1,66 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.lang.reflect.Method;
class PairingRequest extends BroadcastReceiver {
private final Activity activity;
private final BTHandler handler;
public PairingRequest(final Activity activity, final BTHandler handler)
{
super();
this.activity = activity;
this.handler = handler;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
try {
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try
{
byte[] pinBytes;
pinBytes = ("}gZ='GD?gj2r|B}>").getBytes("UTF-8");
handler.log( "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
handler.log("Success to add the PIN.");
try {
m = device.getClass().getMethod("createBond");
m.invoke(device);
handler.log("Success to start bond.");
} catch (Exception e) {
e.printStackTrace();
}
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
handler.log( "Success to setPairingConfirmation.");
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,91 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.bluetooth.BluetoothAdapter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
/**
* Created by fishermen21 on 16.05.17.
*/
public class Protocol {
public static void sendSyn(BTConnection btConn) {
Utils.incrementArray(btConn.getPumpData().getNonceTx());
byte[] p_s = {16,23,0,0,0};
List<Byte> packet = Packet.buildPacket(p_s, null, true, btConn); //Use real address (gathered in Key Response)
packet = Utils.ccmAuthenticate(packet, btConn.getPumpData().getToPumpKey(), btConn.getPumpData().getNonceTx()); //Add U-MAC (Use D->P key)
List<Byte> temp = Frame.frameEscape(packet);
byte[] ro = new byte[temp.size()];
int i = 0;
for(byte b : temp)
ro[i++]=b;
btConn.write(ro);
}
public static void sendIDReq(BTConnection btConn) {
btConn.getPumpData().resetTxNonce(); //Reset TX Nonce (previous to this the nonce is not used and is zero)
Utils.incrementArray(btConn.getPumpData().getNonceTx()); //Increment it to 1
ByteBuffer ids = ByteBuffer.allocate(17); //Allocate payload
String btName = BluetoothAdapter.getDefaultAdapter().getName(); //Get the Device ID
byte[] deviceId = new byte[13];
for(int i=0;i<deviceId.length;i++)
{
if(i < btName.length())
deviceId[i] = (byte)btName.charAt(i);
else
deviceId[i] = (byte)0x00;
}
String swver = "5.04"; //Get the SW Version
int clientId = 0;
clientId += (((byte)swver.charAt(3)) - 0x30);
clientId += (((byte)swver.charAt(2)) - 0x30)*10;
clientId += (((byte)swver.charAt(0)) - 0x30)*100;
clientId += (10000);
ids.order(ByteOrder.LITTLE_ENDIAN);
ids.putInt(clientId);
ids.put(deviceId);
byte[] p_r = {16,0x12,17,0,0};
List<Byte> packet = Packet.buildPacket(p_r, ids, true,btConn); //Use real address (gathered in Key Response)
packet = Utils.ccmAuthenticate(packet, btConn.getPumpData().getToPumpKey(), btConn.getPumpData().getNonceTx()); //Add U-MAC (Use D->P key)
List<Byte> temp = Frame.frameEscape(packet);
byte[] ro = new byte[temp.size()];
int i = 0;
for(byte b : temp)
ro[i++]=b;
btConn.write(ro);
}
public static void sendAck(byte sequenceNUmber,BTConnection btConn) {
btConn.getPumpData().incrementNonceTx();
List<Byte> packet = Packet.buildPacket(new byte[]{16, 5, 0, 0, 0}, null, true,btConn);
packet.set(1, (byte) (packet.get(1) | sequenceNUmber));
packet = Utils.ccmAuthenticate(packet, btConn.getPumpData().getToPumpKey(), btConn.getPumpData().getNonceTx());
List<Byte> temp = Frame.frameEscape(packet);
byte[] ro = new byte[temp.size()];
int i = 0;
for (byte b : temp)
ro[i++] = b;
btConn.write(ro);
}
}

View file

@ -0,0 +1,139 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;
import java.security.InvalidKeyException;
/**
* Created by fishermen21 on 20.05.17.
*/
public class PumpData {
private String pumpMac;
private Object pump_tf;
private Object driver_tf;
private byte address;
private byte[] nonceTx;
private Context activity;
public PumpData(Context activity) {
this.activity = activity;
this.nonceTx = new byte[13];
}
public static PumpData loadPump(Context activity, IRTHandler handler) {
PumpData data = new PumpData(activity);
SharedPreferences prefs = activity.getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
String dp = prefs.getString("dp","E9 24 39 46 84 8A B6 B7 B0 7E 90 C3 2E 2E C2 40 ");
String pd = prefs.getString("pd","29 E2 BD 11 DE 42 49 24 40 30 70 61 DD 4A DF DD ");
data.pumpMac = prefs.getString("device", "00:0E:2F:E8:A5:89");
try {
handler.log("Loading data of Pump "+data.pumpMac);
} catch (RemoteException e) {
e.printStackTrace();
}
if(data.pumpMac != null)
{
try {
data.pump_tf = Twofish_Algorithm.makeKey(Utils.hexStringToByteArray(pd));
data.driver_tf = Twofish_Algorithm.makeKey(Utils.hexStringToByteArray(dp));
data.address = (byte)prefs.getInt("address",16); //0);
data.nonceTx = Utils.hexStringToByteArray(prefs.getString("nonceTx", "F5 01 00 00 00 00 00 00 00 00 00 00 00 "));//"00 00 00 00 00 00 00 00 00 00 00 00 00"));
} catch(Exception e)
{
e.printStackTrace();
try {
handler.fail("unable to load keys!");
} catch (RemoteException e1) {
e1.printStackTrace();
}
return null;
}
}
return data;
}
public byte[] getNonceTx() {
return nonceTx;
}
public void setAndSaveAddress(byte address) {
this.address = address;
SharedPreferences prefs = activity.getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
prefs.edit().putInt("address",address).commit();
}
public byte getAddress() {
return address;
}
public String getPumpMac() {
return pumpMac;
}
public void resetTxNonce() {
for (int i = 0; i < nonceTx.length; i++)
nonceTx[i] = 0;
SharedPreferences prefs = activity.getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
prefs.edit().putString("nonceTx", Utils.byteArrayToHexString(nonceTx,nonceTx.length)).apply();
}
public void incrementNonceTx() {
Utils.incrementArray(nonceTx);
SharedPreferences prefs = activity.getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
prefs.edit().putString("nonceTx", Utils.byteArrayToHexString(nonceTx,nonceTx.length)).apply();
}
public Context getActivity() {
return activity;
}
public Object getToPumpKey() {
return driver_tf;
}
public Object getToDeviceKey() {
return pump_tf;
}
public void setAndSaveToDeviceKey(byte[] key_pd, Object tf) throws InvalidKeyException {
byte[] key_pd_de = Twofish_Algorithm.blockDecrypt(key_pd, 0, tf);
this.pump_tf = Twofish_Algorithm.makeKey(key_pd_de);
SharedPreferences prefs = getActivity().getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("pd", Utils.byteArrayToHexString(key_pd_de,key_pd_de.length));
editor.apply();
}
public void setAndSaveToPumpKey(byte[] key_dp, Object tf) throws InvalidKeyException {
byte[] key_dp_de = Twofish_Algorithm.blockDecrypt(key_dp, 0, tf);
this.driver_tf = Twofish_Algorithm.makeKey(key_dp_de);
SharedPreferences prefs = getActivity().getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("dp", Utils.byteArrayToHexString(key_dp_de,key_dp_de.length));
editor.apply();
}
public void setAndSavePumpMac(String pumpMac) {
this.pumpMac = pumpMac;
SharedPreferences prefs = getActivity().getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("device",pumpMac);
editor.putBoolean("paired",true);
editor.apply();
}
}

View file

@ -0,0 +1,412 @@
package org.monkey.d.ruffy.ruffy.driver;
import android.app.Activity;
import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.RemoteException;
import org.monkey.d.ruffy.ruffy.driver.display.DisplayParser;
import org.monkey.d.ruffy.ruffy.driver.display.DisplayParserHandler;
import org.monkey.d.ruffy.ruffy.driver.display.Menu;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Created by fishermen21 on 25.05.17.
*/
public class Ruffy extends Service {
public static class Key {
public static byte NO_KEY =(byte)0x00;
public static byte MENU =(byte)0x03;
public static byte CHECK =(byte)0x0C;
public static byte UP =(byte)0x30;
public static byte DOWN =(byte)0xC0;
}
private IRTHandler rtHandler = null;
private BTConnection btConn;
private PumpData pumpData;
private boolean rtModeRunning = false;
private long lastRtMessageSent = 0;
private final Object rtSequenceSemaphore = new Object();
private short rtSequence = 0;
private int modeErrorCount = 0;
private int step = 0;
private boolean synRun=false;//With set to false, write process is started at first time
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 3 );
private Display display;
private final IRuffyService.Stub serviceBinder = new IRuffyService.Stub(){
@Override
public void setHandler(IRTHandler handler) throws RemoteException {
Ruffy.this.rtHandler = handler;
}
@Override
public int doRTConnect() throws RemoteException {
step= 0;
if(Ruffy.this.rtHandler==null)
{
return -2;//FIXME make errors
}
if(pumpData==null)
{
pumpData = PumpData.loadPump(Ruffy.this,rtHandler);
}
if(pumpData != null) {
btConn = new BTConnection(rtBTHandler);
btConn.connect(pumpData, 10);
return 0;
}
return -1;
}
public void doRTDisconnect()
{
step = 200;
stopRT();
btConn.disconnect();
}
public void rtSendKey(byte keyCode, boolean changed)
{
//FIXME
lastRtMessageSent = System.currentTimeMillis();
synchronized (rtSequenceSemaphore) {
rtSequence = Application.rtSendKey(keyCode, changed, rtSequence, btConn);
}
}
public void resetPairing()
{
SharedPreferences prefs = Ruffy.this.getSharedPreferences("pumpdata", Activity.MODE_PRIVATE);
prefs.edit().putBoolean("paired",false).apply();
synRun=false;
rtModeRunning =false;
}
public boolean isConnected()
{
if (btConn!=null) {
return btConn.isConnected();
} else {
return false;
}
}
};
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
}
private BTHandler rtBTHandler = new BTHandler() {
@Override
public void deviceConnected() {
log("connected to pump");
if(synRun==false) {
synRun = true;
log("start synThread");
scheduler.execute(synThread);
}
}
@Override
public void log(String s) {
Ruffy.this.log(s);
if(s.equals("got error in read") && step < 200)
{
synRun=false;
btConn.connect(pumpData,4);
}
}
@Override
public void fail(String s) {
log("failed: "+s);
synRun=false;
if(step < 200)
btConn.connect(pumpData,4);
else
Ruffy.this.fail(s);
}
@Override
public void deviceFound(BluetoothDevice bd) {
log("not be here!?!");
}
@Override
public void handleRawData(byte[] buffer, int bytes) {
log("got data from pump");
synRun=false;
step=0;
Packet.handleRawData(buffer,bytes, rtPacketHandler);
}
@Override
public void requestBlueTooth() {
try {
rtHandler.requestBluetooth();
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
private void stopRT()
{
rtModeRunning = false;
rtSequence =0;
}
private void startRT() {
log("starting RT keepAlive");
new Thread(){
@Override
public void run() {
rtModeRunning = true;
rtSequence = 0;
lastRtMessageSent = System.currentTimeMillis();
rtModeRunning = true;
try {
display = new Display(new DisplayUpdater() {
@Override
public void clear() {
try {
rtHandler.rtClearDisplay();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void update(byte[] quarter, int which) {
try {
rtHandler.rtUpdateDisplay(quarter,which);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
display.setCompletDisplayHandler(new CompleteDisplayHandler() {
@Override
public void handleCompleteFrame(byte[][] pixels) {
DisplayParser.findMenu(pixels, new DisplayParserHandler() {
@Override
public void menuFound(Menu menu) {
try {
rtHandler.rtDisplayHandleMenu(menu);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void noMenuFound() {
try {
rtHandler.rtDisplayHandleNoMenu();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
});
rtHandler.rtStarted();
} catch (RemoteException e) {
e.printStackTrace();
}
while(rtModeRunning)
{
if(System.currentTimeMillis() > lastRtMessageSent +1000L) {
log("sending keep alive");
synchronized (rtSequenceSemaphore) {
rtSequence = Application.sendRTKeepAlive(rtSequence, btConn);
lastRtMessageSent = System.currentTimeMillis();
}
}
try{
Thread.sleep(500);}catch(Exception e){/*ignore*/}
}
try {
rtHandler.rtStopped();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}.start();
}
private Runnable synThread = new Runnable(){
@Override
public void run() {
while(synRun)
{
Protocol.sendSyn(btConn);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private AppHandler rtAppHandler = new AppHandler() {
@Override
public void log(String s) {
Ruffy.this.log(s);
}
@Override
public void connected() {
Application.sendAppCommand(Application.Command.RT_MODE, btConn);
}
@Override
public void rtModeActivated() {
startRT();
}
@Override
public void modeDeactivated() {
rtModeRunning = false;
Application.sendAppCommand(Application.Command.RT_MODE, btConn);
}
@Override
public void addDisplayFrame(ByteBuffer b) {
display.addDisplayFrame(b);
}
@Override
public void modeError() {
modeErrorCount++;
if (modeErrorCount > Application.MODE_ERROR_TRESHHOLD) {
stopRT();
log("wrong mode, deactivate");
modeErrorCount = 0;
Application.sendAppCommand(Application.Command.DEACTIVATE_ALL, btConn);
}
}
@Override
public void sequenceError() {
Application.sendAppCommand(Application.Command.RT_DEACTIVATE, btConn);
}
@Override
public void error(short error, String desc) {
switch (error)
{
case (short) 0xF056:
PumpData d = btConn.getPumpData();
btConn.disconnect();
btConn.connect(d,4);
break;
default:
log(desc);
}
}
};
public void log(String s) {
try{
rtHandler.log(s);
}catch(Exception e){e.printStackTrace();}
}
public void fail(String s) {
try{
rtHandler.fail(s);
}catch(Exception e){e.printStackTrace();}
}
private PacketHandler rtPacketHandler = new PacketHandler(){
@Override
public void sendImidiateAcknowledge(byte sequenceNumber) {
Protocol.sendAck(sequenceNumber,btConn);
}
@Override
public void log(String s) {
Ruffy.this.log(s);
}
@Override
public void handleResponse(Packet.Response response, boolean reliableFlagged, byte[] payload) {
switch (response)
{
case ID:
Protocol.sendSyn(btConn);
break;
case SYNC:
btConn.seqNo = 0x00;
if(step<100)
Application.sendAppConnect(btConn);
else
{
Application.sendAppDisconnect(btConn);
step = 200;
}
break;
case UNRELIABLE_DATA:
case RELIABLE_DATA:
Application.processAppResponse(payload, reliableFlagged, rtAppHandler);
break;
}
}
@Override
public void handleErrorResponse(byte errorCode, String errDecoded, boolean reliableFlagged, byte[] payload) {
log(errDecoded);
}
@Override
public Object getToDeviceKey() {
return pumpData.getToDeviceKey();
}
};
}

View file

@ -0,0 +1,836 @@
// $Id: $
//
// $Log: $
// Revision 1.0 1998/03/24 raif
// + start of history.
//
// $Endlog$
/*
* Copyright (c) 1997, 1998 Systemics Ltd on behalf of
* the Cryptix Development Team. All rights reserved.
*/
package org.monkey.d.ruffy.ruffy.driver;
import java.io.PrintWriter;
import java.security.InvalidKeyException;
//...........................................................................
/**
* Twofish is an AES candidate algorithm. It is a balanced 128-bit Feistel
* cipher, consisting of 16 rounds. In each round, a 64-bit S-box value is
* computed from 64 bits of the block, and this value is xored into the other
* half of the block. The two half-blocks are then exchanged, and the next
* round begins. Before the first round, all input bits are xored with key-
* dependent "whitening" subkeys, and after the final round the output bits
* are xored with other key-dependent whitening subkeys; these subkeys are
* not used anywhere else in the algorithm.<p>
*
* Twofish was submitted by Bruce Schneier, Doug Whiting, John Kelsey, Chris
* Hall and David Wagner.<p>
*
* Reference:<ol>
* <li>TWOFISH2.C -- Optimized C API calls for TWOFISH AES submission,
* Version 1.00, April 1998, by Doug Whiting.</ol><p>
*
* <b>Copyright</b> &copy; 1998
* <a href="http://www.systemics.com/">Systemics Ltd</a> on behalf of the
* <a href="http://www.systemics.com/docs/cryptix/">Cryptix Development Team</a>.
* <br>All rights reserved.<p>
*
* <b>$Revision: $</b>
* @author Raif S. Naffah
*/
public final class Twofish_Algorithm // implicit no-argument constructor
{
// Debugging methods and variables
//...........................................................................
static final String NAME = "Twofish_Algorithm";
static final boolean IN = true, OUT = false;
static final boolean DEBUG = Twofish_Properties.GLOBAL_DEBUG;
static final int debuglevel = DEBUG ? Twofish_Properties.getLevel(NAME) : 0;
static final PrintWriter err = DEBUG ? Twofish_Properties.getOutput() : null;
static final boolean TRACE = Twofish_Properties.isTraceable(NAME);
static void debug (String s) { err.println(">>> "+NAME+": "+s); }
static void trace (boolean in, String s) {
if (TRACE) err.println((in?"==> ":"<== ")+NAME+"."+s);
}
static void trace (String s) { if (TRACE) err.println("<=> "+NAME+"."+s); }
// Constants and variables
//...........................................................................
static final int BLOCK_SIZE = 16; // bytes in a data-block
private static final int ROUNDS = 16;
private static final int MAX_ROUNDS = 16; // max # rounds (for allocating subkeys)
/* Subkey array indices */
private static final int INPUT_WHITEN = 0;
private static final int OUTPUT_WHITEN = INPUT_WHITEN + BLOCK_SIZE/4;
private static final int ROUND_SUBKEYS = OUTPUT_WHITEN + BLOCK_SIZE/4; // 2*(# rounds)
private static final int TOTAL_SUBKEYS = ROUND_SUBKEYS + 2*MAX_ROUNDS;
private static final int SK_STEP = 0x02020202;
private static final int SK_BUMP = 0x01010101;
private static final int SK_ROTL = 9;
/** Fixed 8x8 permutation S-boxes */
private static final byte[][] P = new byte[][] {
{ // p0
(byte) 0xA9, (byte) 0x67, (byte) 0xB3, (byte) 0xE8,
(byte) 0x04, (byte) 0xFD, (byte) 0xA3, (byte) 0x76,
(byte) 0x9A, (byte) 0x92, (byte) 0x80, (byte) 0x78,
(byte) 0xE4, (byte) 0xDD, (byte) 0xD1, (byte) 0x38,
(byte) 0x0D, (byte) 0xC6, (byte) 0x35, (byte) 0x98,
(byte) 0x18, (byte) 0xF7, (byte) 0xEC, (byte) 0x6C,
(byte) 0x43, (byte) 0x75, (byte) 0x37, (byte) 0x26,
(byte) 0xFA, (byte) 0x13, (byte) 0x94, (byte) 0x48,
(byte) 0xF2, (byte) 0xD0, (byte) 0x8B, (byte) 0x30,
(byte) 0x84, (byte) 0x54, (byte) 0xDF, (byte) 0x23,
(byte) 0x19, (byte) 0x5B, (byte) 0x3D, (byte) 0x59,
(byte) 0xF3, (byte) 0xAE, (byte) 0xA2, (byte) 0x82,
(byte) 0x63, (byte) 0x01, (byte) 0x83, (byte) 0x2E,
(byte) 0xD9, (byte) 0x51, (byte) 0x9B, (byte) 0x7C,
(byte) 0xA6, (byte) 0xEB, (byte) 0xA5, (byte) 0xBE,
(byte) 0x16, (byte) 0x0C, (byte) 0xE3, (byte) 0x61,
(byte) 0xC0, (byte) 0x8C, (byte) 0x3A, (byte) 0xF5,
(byte) 0x73, (byte) 0x2C, (byte) 0x25, (byte) 0x0B,
(byte) 0xBB, (byte) 0x4E, (byte) 0x89, (byte) 0x6B,
(byte) 0x53, (byte) 0x6A, (byte) 0xB4, (byte) 0xF1,
(byte) 0xE1, (byte) 0xE6, (byte) 0xBD, (byte) 0x45,
(byte) 0xE2, (byte) 0xF4, (byte) 0xB6, (byte) 0x66,
(byte) 0xCC, (byte) 0x95, (byte) 0x03, (byte) 0x56,
(byte) 0xD4, (byte) 0x1C, (byte) 0x1E, (byte) 0xD7,
(byte) 0xFB, (byte) 0xC3, (byte) 0x8E, (byte) 0xB5,
(byte) 0xE9, (byte) 0xCF, (byte) 0xBF, (byte) 0xBA,
(byte) 0xEA, (byte) 0x77, (byte) 0x39, (byte) 0xAF,
(byte) 0x33, (byte) 0xC9, (byte) 0x62, (byte) 0x71,
(byte) 0x81, (byte) 0x79, (byte) 0x09, (byte) 0xAD,
(byte) 0x24, (byte) 0xCD, (byte) 0xF9, (byte) 0xD8,
(byte) 0xE5, (byte) 0xC5, (byte) 0xB9, (byte) 0x4D,
(byte) 0x44, (byte) 0x08, (byte) 0x86, (byte) 0xE7,
(byte) 0xA1, (byte) 0x1D, (byte) 0xAA, (byte) 0xED,
(byte) 0x06, (byte) 0x70, (byte) 0xB2, (byte) 0xD2,
(byte) 0x41, (byte) 0x7B, (byte) 0xA0, (byte) 0x11,
(byte) 0x31, (byte) 0xC2, (byte) 0x27, (byte) 0x90,
(byte) 0x20, (byte) 0xF6, (byte) 0x60, (byte) 0xFF,
(byte) 0x96, (byte) 0x5C, (byte) 0xB1, (byte) 0xAB,
(byte) 0x9E, (byte) 0x9C, (byte) 0x52, (byte) 0x1B,
(byte) 0x5F, (byte) 0x93, (byte) 0x0A, (byte) 0xEF,
(byte) 0x91, (byte) 0x85, (byte) 0x49, (byte) 0xEE,
(byte) 0x2D, (byte) 0x4F, (byte) 0x8F, (byte) 0x3B,
(byte) 0x47, (byte) 0x87, (byte) 0x6D, (byte) 0x46,
(byte) 0xD6, (byte) 0x3E, (byte) 0x69, (byte) 0x64,
(byte) 0x2A, (byte) 0xCE, (byte) 0xCB, (byte) 0x2F,
(byte) 0xFC, (byte) 0x97, (byte) 0x05, (byte) 0x7A,
(byte) 0xAC, (byte) 0x7F, (byte) 0xD5, (byte) 0x1A,
(byte) 0x4B, (byte) 0x0E, (byte) 0xA7, (byte) 0x5A,
(byte) 0x28, (byte) 0x14, (byte) 0x3F, (byte) 0x29,
(byte) 0x88, (byte) 0x3C, (byte) 0x4C, (byte) 0x02,
(byte) 0xB8, (byte) 0xDA, (byte) 0xB0, (byte) 0x17,
(byte) 0x55, (byte) 0x1F, (byte) 0x8A, (byte) 0x7D,
(byte) 0x57, (byte) 0xC7, (byte) 0x8D, (byte) 0x74,
(byte) 0xB7, (byte) 0xC4, (byte) 0x9F, (byte) 0x72,
(byte) 0x7E, (byte) 0x15, (byte) 0x22, (byte) 0x12,
(byte) 0x58, (byte) 0x07, (byte) 0x99, (byte) 0x34,
(byte) 0x6E, (byte) 0x50, (byte) 0xDE, (byte) 0x68,
(byte) 0x65, (byte) 0xBC, (byte) 0xDB, (byte) 0xF8,
(byte) 0xC8, (byte) 0xA8, (byte) 0x2B, (byte) 0x40,
(byte) 0xDC, (byte) 0xFE, (byte) 0x32, (byte) 0xA4,
(byte) 0xCA, (byte) 0x10, (byte) 0x21, (byte) 0xF0,
(byte) 0xD3, (byte) 0x5D, (byte) 0x0F, (byte) 0x00,
(byte) 0x6F, (byte) 0x9D, (byte) 0x36, (byte) 0x42,
(byte) 0x4A, (byte) 0x5E, (byte) 0xC1, (byte) 0xE0
},
{ // p1
(byte) 0x75, (byte) 0xF3, (byte) 0xC6, (byte) 0xF4,
(byte) 0xDB, (byte) 0x7B, (byte) 0xFB, (byte) 0xC8,
(byte) 0x4A, (byte) 0xD3, (byte) 0xE6, (byte) 0x6B,
(byte) 0x45, (byte) 0x7D, (byte) 0xE8, (byte) 0x4B,
(byte) 0xD6, (byte) 0x32, (byte) 0xD8, (byte) 0xFD,
(byte) 0x37, (byte) 0x71, (byte) 0xF1, (byte) 0xE1,
(byte) 0x30, (byte) 0x0F, (byte) 0xF8, (byte) 0x1B,
(byte) 0x87, (byte) 0xFA, (byte) 0x06, (byte) 0x3F,
(byte) 0x5E, (byte) 0xBA, (byte) 0xAE, (byte) 0x5B,
(byte) 0x8A, (byte) 0x00, (byte) 0xBC, (byte) 0x9D,
(byte) 0x6D, (byte) 0xC1, (byte) 0xB1, (byte) 0x0E,
(byte) 0x80, (byte) 0x5D, (byte) 0xD2, (byte) 0xD5,
(byte) 0xA0, (byte) 0x84, (byte) 0x07, (byte) 0x14,
(byte) 0xB5, (byte) 0x90, (byte) 0x2C, (byte) 0xA3,
(byte) 0xB2, (byte) 0x73, (byte) 0x4C, (byte) 0x54,
(byte) 0x92, (byte) 0x74, (byte) 0x36, (byte) 0x51,
(byte) 0x38, (byte) 0xB0, (byte) 0xBD, (byte) 0x5A,
(byte) 0xFC, (byte) 0x60, (byte) 0x62, (byte) 0x96,
(byte) 0x6C, (byte) 0x42, (byte) 0xF7, (byte) 0x10,
(byte) 0x7C, (byte) 0x28, (byte) 0x27, (byte) 0x8C,
(byte) 0x13, (byte) 0x95, (byte) 0x9C, (byte) 0xC7,
(byte) 0x24, (byte) 0x46, (byte) 0x3B, (byte) 0x70,
(byte) 0xCA, (byte) 0xE3, (byte) 0x85, (byte) 0xCB,
(byte) 0x11, (byte) 0xD0, (byte) 0x93, (byte) 0xB8,
(byte) 0xA6, (byte) 0x83, (byte) 0x20, (byte) 0xFF,
(byte) 0x9F, (byte) 0x77, (byte) 0xC3, (byte) 0xCC,
(byte) 0x03, (byte) 0x6F, (byte) 0x08, (byte) 0xBF,
(byte) 0x40, (byte) 0xE7, (byte) 0x2B, (byte) 0xE2,
(byte) 0x79, (byte) 0x0C, (byte) 0xAA, (byte) 0x82,
(byte) 0x41, (byte) 0x3A, (byte) 0xEA, (byte) 0xB9,
(byte) 0xE4, (byte) 0x9A, (byte) 0xA4, (byte) 0x97,
(byte) 0x7E, (byte) 0xDA, (byte) 0x7A, (byte) 0x17,
(byte) 0x66, (byte) 0x94, (byte) 0xA1, (byte) 0x1D,
(byte) 0x3D, (byte) 0xF0, (byte) 0xDE, (byte) 0xB3,
(byte) 0x0B, (byte) 0x72, (byte) 0xA7, (byte) 0x1C,
(byte) 0xEF, (byte) 0xD1, (byte) 0x53, (byte) 0x3E,
(byte) 0x8F, (byte) 0x33, (byte) 0x26, (byte) 0x5F,
(byte) 0xEC, (byte) 0x76, (byte) 0x2A, (byte) 0x49,
(byte) 0x81, (byte) 0x88, (byte) 0xEE, (byte) 0x21,
(byte) 0xC4, (byte) 0x1A, (byte) 0xEB, (byte) 0xD9,
(byte) 0xC5, (byte) 0x39, (byte) 0x99, (byte) 0xCD,
(byte) 0xAD, (byte) 0x31, (byte) 0x8B, (byte) 0x01,
(byte) 0x18, (byte) 0x23, (byte) 0xDD, (byte) 0x1F,
(byte) 0x4E, (byte) 0x2D, (byte) 0xF9, (byte) 0x48,
(byte) 0x4F, (byte) 0xF2, (byte) 0x65, (byte) 0x8E,
(byte) 0x78, (byte) 0x5C, (byte) 0x58, (byte) 0x19,
(byte) 0x8D, (byte) 0xE5, (byte) 0x98, (byte) 0x57,
(byte) 0x67, (byte) 0x7F, (byte) 0x05, (byte) 0x64,
(byte) 0xAF, (byte) 0x63, (byte) 0xB6, (byte) 0xFE,
(byte) 0xF5, (byte) 0xB7, (byte) 0x3C, (byte) 0xA5,
(byte) 0xCE, (byte) 0xE9, (byte) 0x68, (byte) 0x44,
(byte) 0xE0, (byte) 0x4D, (byte) 0x43, (byte) 0x69,
(byte) 0x29, (byte) 0x2E, (byte) 0xAC, (byte) 0x15,
(byte) 0x59, (byte) 0xA8, (byte) 0x0A, (byte) 0x9E,
(byte) 0x6E, (byte) 0x47, (byte) 0xDF, (byte) 0x34,
(byte) 0x35, (byte) 0x6A, (byte) 0xCF, (byte) 0xDC,
(byte) 0x22, (byte) 0xC9, (byte) 0xC0, (byte) 0x9B,
(byte) 0x89, (byte) 0xD4, (byte) 0xED, (byte) 0xAB,
(byte) 0x12, (byte) 0xA2, (byte) 0x0D, (byte) 0x52,
(byte) 0xBB, (byte) 0x02, (byte) 0x2F, (byte) 0xA9,
(byte) 0xD7, (byte) 0x61, (byte) 0x1E, (byte) 0xB4,
(byte) 0x50, (byte) 0x04, (byte) 0xF6, (byte) 0xC2,
(byte) 0x16, (byte) 0x25, (byte) 0x86, (byte) 0x56,
(byte) 0x55, (byte) 0x09, (byte) 0xBE, (byte) 0x91
}
};
/**
* Define the fixed p0/p1 permutations used in keyed S-box lookup.
* By changing the following constant definitions, the S-boxes will
* automatically get changed in the Twofish engine.
*/
private static final int P_00 = 1;
private static final int P_01 = 0;
private static final int P_02 = 0;
private static final int P_03 = P_01 ^ 1;
private static final int P_04 = 1;
private static final int P_10 = 0;
private static final int P_11 = 0;
private static final int P_12 = 1;
private static final int P_13 = P_11 ^ 1;
private static final int P_14 = 0;
private static final int P_20 = 1;
private static final int P_21 = 1;
private static final int P_22 = 0;
private static final int P_23 = P_21 ^ 1;
private static final int P_24 = 0;
private static final int P_30 = 0;
private static final int P_31 = 1;
private static final int P_32 = 1;
private static final int P_33 = P_31 ^ 1;
private static final int P_34 = 1;
/** Primitive polynomial for GF(256) */
private static final int GF256_FDBK = 0x169;
private static final int GF256_FDBK_2 = 0x169 / 2;
private static final int GF256_FDBK_4 = 0x169 / 4;
/** MDS matrix */
private static final int[][] MDS = new int[4][256]; // blank final
private static final int RS_GF_FDBK = 0x14D; // field generator
/** data for hexadecimal visualisation. */
private static final char[] HEX_DIGITS = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
// Static code - to intialise the MDS matrix
//...........................................................................
static {
long time = System.currentTimeMillis();
if (DEBUG && debuglevel > 6) {
System.out.println("Algorithm Name: "+ Twofish_Properties.FULL_NAME);
System.out.println("Electronic Codebook (ECB) Mode");
System.out.println();
}
//
// precompute the MDS matrix
//
int[] m1 = new int[2];
int[] mX = new int[2];
int[] mY = new int[2];
int i, j;
for (i = 0; i < 256; i++) {
j = P[0][i] & 0xFF; // compute all the matrix elements
m1[0] = j;
mX[0] = Mx_X( j ) & 0xFF;
mY[0] = Mx_Y( j ) & 0xFF;
j = P[1][i] & 0xFF;
m1[1] = j;
mX[1] = Mx_X( j ) & 0xFF;
mY[1] = Mx_Y( j ) & 0xFF;
MDS[0][i] = m1[P_00] << 0 | // fill matrix w/ above elements
mX[P_00] << 8 |
mY[P_00] << 16 |
mY[P_00] << 24;
MDS[1][i] = mY[P_10] << 0 |
mY[P_10] << 8 |
mX[P_10] << 16 |
m1[P_10] << 24;
MDS[2][i] = mX[P_20] << 0 |
mY[P_20] << 8 |
m1[P_20] << 16 |
mY[P_20] << 24;
MDS[3][i] = mX[P_30] << 0 |
m1[P_30] << 8 |
mY[P_30] << 16 |
mX[P_30] << 24;
}
time = System.currentTimeMillis() - time;
if (DEBUG && debuglevel > 8) {
System.out.println("==========");
System.out.println();
System.out.println("Static Data");
System.out.println();
System.out.println("MDS[0][]:"); for(i=0; i<64; i++) { for(j=0; j<4; j++) System.out.print("0x"+intToString(MDS[0][i*4+j])+", "); System.out.println();}
System.out.println();
System.out.println("MDS[1][]:"); for(i=0; i<64; i++) { for(j=0; j<4; j++) System.out.print("0x"+intToString(MDS[1][i*4+j])+", "); System.out.println();}
System.out.println();
System.out.println("MDS[2][]:"); for(i=0; i<64; i++) { for(j=0; j<4; j++) System.out.print("0x"+intToString(MDS[2][i*4+j])+", "); System.out.println();}
System.out.println();
System.out.println("MDS[3][]:"); for(i=0; i<64; i++) { for(j=0; j<4; j++) System.out.print("0x"+intToString(MDS[3][i*4+j])+", "); System.out.println();}
System.out.println();
System.out.println("Total initialization time: "+time+" ms.");
System.out.println();
}
}
private static final int LFSR1( int x ) {
return (x >> 1) ^
((x & 0x01) != 0 ? GF256_FDBK_2 : 0);
}
private static final int LFSR2( int x ) {
return (x >> 2) ^
((x & 0x02) != 0 ? GF256_FDBK_2 : 0) ^
((x & 0x01) != 0 ? GF256_FDBK_4 : 0);
}
private static final int Mx_1( int x ) { return x; }
private static final int Mx_X( int x ) { return x ^ LFSR2(x); } // 5B
private static final int Mx_Y( int x ) { return x ^ LFSR1(x) ^ LFSR2(x); } // EF
// Basic API methods
//...........................................................................
/**
* Expand a user-supplied key material into a session key.
*
* @param key The 64/128/192/256-bit user-key to use.
* @return This cipher's round keys.
* @exception InvalidKeyException If the key is invalid.
*/
public static synchronized Object makeKey (byte[] k)
throws InvalidKeyException {
if (DEBUG) trace(IN, "makeKey("+k+")");
if (k == null)
throw new InvalidKeyException("Empty key");
int length = k.length;
if (!(length == 8 || length == 16 || length == 24 || length == 32))
throw new InvalidKeyException("Incorrect key length");
if (DEBUG && debuglevel > 7) {
System.out.println("Intermediate Session Key Values");
System.out.println();
System.out.println("Raw="+toString(k));
System.out.println();
}
int k64Cnt = length / 8;
int subkeyCnt = ROUND_SUBKEYS + 2*ROUNDS;
int[] k32e = new int[4]; // even 32-bit entities
int[] k32o = new int[4]; // odd 32-bit entities
int[] sBoxKey = new int[4];
//
// split user key material into even and odd 32-bit entities and
// compute S-box keys using (12, 8) Reed-Solomon code over GF(256)
//
int i, j, offset = 0;
for (i = 0, j = k64Cnt-1; i < 4 && offset < length; i++, j--) {
k32e[i] = (k[offset++] & 0xFF) |
(k[offset++] & 0xFF) << 8 |
(k[offset++] & 0xFF) << 16 |
(k[offset++] & 0xFF) << 24;
k32o[i] = (k[offset++] & 0xFF) |
(k[offset++] & 0xFF) << 8 |
(k[offset++] & 0xFF) << 16 |
(k[offset++] & 0xFF) << 24;
sBoxKey[j] = RS_MDS_Encode( k32e[i], k32o[i] ); // reverse order
}
// compute the round decryption subkeys for PHT. these same subkeys
// will be used in encryption but will be applied in reverse order.
int q, A, B;
int[] subKeys = new int[subkeyCnt];
for (i = q = 0; i < subkeyCnt/2; i++, q += SK_STEP) {
A = F32( k64Cnt, q , k32e ); // A uses even key entities
B = F32( k64Cnt, q+SK_BUMP, k32o ); // B uses odd key entities
B = B << 8 | B >>> 24;
A += B;
subKeys[2*i ] = A; // combine with a PHT
A += B;
subKeys[2*i + 1] = A << SK_ROTL | A >>> (32-SK_ROTL);
}
//
// fully expand the table for speed
//
int k0 = sBoxKey[0];
int k1 = sBoxKey[1];
int k2 = sBoxKey[2];
int k3 = sBoxKey[3];
int b0, b1, b2, b3;
int[] sBox = new int[4 * 256];
for (i = 0; i < 256; i++) {
b0 = b1 = b2 = b3 = i;
switch (k64Cnt & 3) {
case 1:
sBox[ 2*i ] = MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)];
sBox[ 2*i+1] = MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)];
sBox[0x200+2*i ] = MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)];
sBox[0x200+2*i+1] = MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)];
break;
case 0: // same as 4
b0 = (P[P_04][b0] & 0xFF) ^ b0(k3);
b1 = (P[P_14][b1] & 0xFF) ^ b1(k3);
b2 = (P[P_24][b2] & 0xFF) ^ b2(k3);
b3 = (P[P_34][b3] & 0xFF) ^ b3(k3);
case 3:
b0 = (P[P_03][b0] & 0xFF) ^ b0(k2);
b1 = (P[P_13][b1] & 0xFF) ^ b1(k2);
b2 = (P[P_23][b2] & 0xFF) ^ b2(k2);
b3 = (P[P_33][b3] & 0xFF) ^ b3(k2);
case 2: // 128-bit keys
sBox[ 2*i ] = MDS[0][(P[P_01][(P[P_02][b0] & 0xFF) ^ b0(k1)] & 0xFF) ^ b0(k0)];
sBox[ 2*i+1] = MDS[1][(P[P_11][(P[P_12][b1] & 0xFF) ^ b1(k1)] & 0xFF) ^ b1(k0)];
sBox[0x200+2*i ] = MDS[2][(P[P_21][(P[P_22][b2] & 0xFF) ^ b2(k1)] & 0xFF) ^ b2(k0)];
sBox[0x200+2*i+1] = MDS[3][(P[P_31][(P[P_32][b3] & 0xFF) ^ b3(k1)] & 0xFF) ^ b3(k0)];
}
}
Object sessionKey = new Object[] { sBox, subKeys };
if (DEBUG && debuglevel > 7) {
System.out.println("S-box[]:");
for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[i*4+j])+", "); System.out.println();}
System.out.println();
for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[256+i*4+j])+", "); System.out.println();}
System.out.println();
for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[512+i*4+j])+", "); System.out.println();}
System.out.println();
for(i=0;i<64;i++) { for(j=0;j<4;j++) System.out.print("0x"+intToString(sBox[768+i*4+j])+", "); System.out.println();}
System.out.println();
System.out.println("User (odd, even) keys --> S-Box keys:");
for(i=0;i<k64Cnt;i++) { System.out.println("0x"+intToString(k32o[i])+" 0x"+intToString(k32e[i])+" --> 0x"+intToString(sBoxKey[k64Cnt-1-i])); }
System.out.println();
System.out.println("Round keys:");
for(i=0;i<ROUND_SUBKEYS + 2*ROUNDS;i+=2) { System.out.println("0x"+intToString(subKeys[i])+" 0x"+intToString(subKeys[i+1])); }
System.out.println();
}
if (DEBUG) trace(OUT, "makeKey()");
return sessionKey;
}
/**
* Encrypt exactly one block of plaintext.
*
* @param in The plaintext.
* @param inOffset Index of in from which to start considering data.
* @param sessionKey The session key to use for encryption.
* @return The ciphertext generated from a plaintext using the session key.
*/
public static byte[]
blockEncrypt (byte[] in, int inOffset, Object sessionKey) {
if (DEBUG) trace(IN, "blockEncrypt("+in+", "+inOffset+", "+sessionKey+")");
Object[] sk = (Object[]) sessionKey; // extract S-box and session key
int[] sBox = (int[]) sk[0];
int[] sKey = (int[]) sk[1];
if (DEBUG && debuglevel > 6) System.out.println("PT="+toString(in, inOffset, BLOCK_SIZE));
int x0 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x1 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x2 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x3 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
x0 ^= sKey[INPUT_WHITEN ];
x1 ^= sKey[INPUT_WHITEN + 1];
x2 ^= sKey[INPUT_WHITEN + 2];
x3 ^= sKey[INPUT_WHITEN + 3];
if (DEBUG && debuglevel > 6) System.out.println("PTw="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3));
int t0, t1;
int k = ROUND_SUBKEYS;
for (int R = 0; R < ROUNDS; R += 2) {
t0 = Fe32( sBox, x0, 0 );
t1 = Fe32( sBox, x1, 3 );
x2 ^= t0 + t1 + sKey[k++];
x2 = x2 >>> 1 | x2 << 31;
x3 = x3 << 1 | x3 >>> 31;
x3 ^= t0 + 2*t1 + sKey[k++];
if (DEBUG && debuglevel > 6) System.out.println("CT"+(R)+"="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3));
t0 = Fe32( sBox, x2, 0 );
t1 = Fe32( sBox, x3, 3 );
x0 ^= t0 + t1 + sKey[k++];
x0 = x0 >>> 1 | x0 << 31;
x1 = x1 << 1 | x1 >>> 31;
x1 ^= t0 + 2*t1 + sKey[k++];
if (DEBUG && debuglevel > 6) System.out.println("CT"+(R+1)+"="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3));
}
x2 ^= sKey[OUTPUT_WHITEN ];
x3 ^= sKey[OUTPUT_WHITEN + 1];
x0 ^= sKey[OUTPUT_WHITEN + 2];
x1 ^= sKey[OUTPUT_WHITEN + 3];
if (DEBUG && debuglevel > 6) System.out.println("CTw="+intToString(x0)+intToString(x1)+intToString(x2)+intToString(x3));
byte[] result = new byte[] {
(byte) x2, (byte)(x2 >>> 8), (byte)(x2 >>> 16), (byte)(x2 >>> 24),
(byte) x3, (byte)(x3 >>> 8), (byte)(x3 >>> 16), (byte)(x3 >>> 24),
(byte) x0, (byte)(x0 >>> 8), (byte)(x0 >>> 16), (byte)(x0 >>> 24),
(byte) x1, (byte)(x1 >>> 8), (byte)(x1 >>> 16), (byte)(x1 >>> 24),
};
if (DEBUG && debuglevel > 6) {
System.out.println("CT="+toString(result));
System.out.println();
}
if (DEBUG) trace(OUT, "blockEncrypt()");
return result;
}
/**
* Decrypt exactly one block of ciphertext.
*
* @param in The ciphertext.
* @param inOffset Index of in from which to start considering data.
* @param sessionKey The session key to use for decryption.
* @return The plaintext generated from a ciphertext using the session key.
*/
public static byte[] blockDecrypt (byte[] in, int inOffset, Object sessionKey)
{
if (DEBUG) trace(IN, "blockDecrypt("+in+", "+inOffset+", "+sessionKey+")");
Object[] sk = (Object[]) sessionKey; // extract S-box and session key
int[] sBox = (int[]) sk[0];
int[] sKey = (int[]) sk[1];
if (DEBUG && debuglevel > 6) System.out.println("CT="+toString(in, inOffset, BLOCK_SIZE));
int x2 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x3 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x0 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
int x1 = (in[inOffset++] & 0xFF) |
(in[inOffset++] & 0xFF) << 8 |
(in[inOffset++] & 0xFF) << 16 |
(in[inOffset++] & 0xFF) << 24;
x2 ^= sKey[OUTPUT_WHITEN ];
x3 ^= sKey[OUTPUT_WHITEN + 1];
x0 ^= sKey[OUTPUT_WHITEN + 2];
x1 ^= sKey[OUTPUT_WHITEN + 3];
if (DEBUG && debuglevel > 6) System.out.println("CTw="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1));
int k = ROUND_SUBKEYS + 2*ROUNDS - 1;
int t0, t1;
for (int R = 0; R < ROUNDS; R += 2) {
t0 = Fe32( sBox, x2, 0 );
t1 = Fe32( sBox, x3, 3 );
x1 ^= t0 + 2*t1 + sKey[k--];
x1 = x1 >>> 1 | x1 << 31;
x0 = x0 << 1 | x0 >>> 31;
x0 ^= t0 + t1 + sKey[k--];
if (DEBUG && debuglevel > 6) System.out.println("PT"+(ROUNDS-R)+"="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1));
t0 = Fe32( sBox, x0, 0 );
t1 = Fe32( sBox, x1, 3 );
x3 ^= t0 + 2*t1 + sKey[k--];
x3 = x3 >>> 1 | x3 << 31;
x2 = x2 << 1 | x2 >>> 31;
x2 ^= t0 + t1 + sKey[k--];
if (DEBUG && debuglevel > 6) System.out.println("PT"+(ROUNDS-R-1)+"="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1));
}
x0 ^= sKey[INPUT_WHITEN ];
x1 ^= sKey[INPUT_WHITEN + 1];
x2 ^= sKey[INPUT_WHITEN + 2];
x3 ^= sKey[INPUT_WHITEN + 3];
if (DEBUG && debuglevel > 6) System.out.println("PTw="+intToString(x2)+intToString(x3)+intToString(x0)+intToString(x1));
byte[] result = new byte[] {
(byte) x0, (byte)(x0 >>> 8), (byte)(x0 >>> 16), (byte)(x0 >>> 24),
(byte) x1, (byte)(x1 >>> 8), (byte)(x1 >>> 16), (byte)(x1 >>> 24),
(byte) x2, (byte)(x2 >>> 8), (byte)(x2 >>> 16), (byte)(x2 >>> 24),
(byte) x3, (byte)(x3 >>> 8), (byte)(x3 >>> 16), (byte)(x3 >>> 24),
};
if (DEBUG && debuglevel > 6) {
System.out.println("PT="+toString(result));
System.out.println();
}
if (DEBUG) trace(OUT, "blockDecrypt()");
return result;
}
/** A basic symmetric encryption/decryption test. */
public static boolean self_test() { return self_test(BLOCK_SIZE); }
// own methods
//...........................................................................
private static final int b0( int x ) { return x & 0xFF; }
private static final int b1( int x ) { return (x >>> 8) & 0xFF; }
private static final int b2( int x ) { return (x >>> 16) & 0xFF; }
private static final int b3( int x ) { return (x >>> 24) & 0xFF; }
/**
* Use (12, 8) Reed-Solomon code over GF(256) to produce a key S-box
* 32-bit entity from two key material 32-bit entities.
*
* @param k0 1st 32-bit entity.
* @param k1 2nd 32-bit entity.
* @return Remainder polynomial generated using RS code
*/
private static final int RS_MDS_Encode( int k0, int k1) {
int r = k1;
for (int i = 0; i < 4; i++) // shift 1 byte at a time
r = RS_rem( r );
r ^= k0;
for (int i = 0; i < 4; i++)
r = RS_rem( r );
return r;
}
/*
* Reed-Solomon code parameters: (12, 8) reversible code:<p>
* <pre>
* g(x) = x**4 + (a + 1/a) x**3 + a x**2 + (a + 1/a) x + 1
* </pre>
* where a = primitive root of field generator 0x14D
*/
private static final int RS_rem( int x ) {
int b = (x >>> 24) & 0xFF;
int g2 = ((b << 1) ^ ( (b & 0x80) != 0 ? RS_GF_FDBK : 0 )) & 0xFF;
int g3 = (b >>> 1) ^ ( (b & 0x01) != 0 ? (RS_GF_FDBK >>> 1) : 0 ) ^ g2 ;
int result = (x << 8) ^ (g3 << 24) ^ (g2 << 16) ^ (g3 << 8) ^ b;
return result;
}
private static final int F32( int k64Cnt, int x, int[] k32 ) {
int b0 = b0(x);
int b1 = b1(x);
int b2 = b2(x);
int b3 = b3(x);
int k0 = k32[0];
int k1 = k32[1];
int k2 = k32[2];
int k3 = k32[3];
int result = 0;
switch (k64Cnt & 3) {
case 1:
result =
MDS[0][(P[P_01][b0] & 0xFF) ^ b0(k0)] ^
MDS[1][(P[P_11][b1] & 0xFF) ^ b1(k0)] ^
MDS[2][(P[P_21][b2] & 0xFF) ^ b2(k0)] ^
MDS[3][(P[P_31][b3] & 0xFF) ^ b3(k0)];
break;
case 0: // same as 4
b0 = (P[P_04][b0] & 0xFF) ^ b0(k3);
b1 = (P[P_14][b1] & 0xFF) ^ b1(k3);
b2 = (P[P_24][b2] & 0xFF) ^ b2(k3);
b3 = (P[P_34][b3] & 0xFF) ^ b3(k3);
case 3:
b0 = (P[P_03][b0] & 0xFF) ^ b0(k2);
b1 = (P[P_13][b1] & 0xFF) ^ b1(k2);
b2 = (P[P_23][b2] & 0xFF) ^ b2(k2);
b3 = (P[P_33][b3] & 0xFF) ^ b3(k2);
case 2: // 128-bit keys (optimize for this case)
result =
MDS[0][(P[P_01][(P[P_02][b0] & 0xFF) ^ b0(k1)] & 0xFF) ^ b0(k0)] ^
MDS[1][(P[P_11][(P[P_12][b1] & 0xFF) ^ b1(k1)] & 0xFF) ^ b1(k0)] ^
MDS[2][(P[P_21][(P[P_22][b2] & 0xFF) ^ b2(k1)] & 0xFF) ^ b2(k0)] ^
MDS[3][(P[P_31][(P[P_32][b3] & 0xFF) ^ b3(k1)] & 0xFF) ^ b3(k0)];
break;
}
return result;
}
private static final int Fe32( int[] sBox, int x, int R ) {
return sBox[ 2*_b(x, R ) ] ^
sBox[ 2*_b(x, R+1) + 1] ^
sBox[0x200 + 2*_b(x, R+2) ] ^
sBox[0x200 + 2*_b(x, R+3) + 1];
}
private static final int _b( int x, int N) {
int result = 0;
switch (N%4) {
case 0: result = b0(x); break;
case 1: result = b1(x); break;
case 2: result = b2(x); break;
case 3: result = b3(x); break;
}
return result;
}
/** @return The length in bytes of the Algorithm input block. */
public static int blockSize() { return BLOCK_SIZE; }
/** A basic symmetric encryption/decryption test for a given key size. */
private static boolean self_test (int keysize) {
if (DEBUG) trace(IN, "self_test("+keysize+")");
boolean ok = false;
try {
byte[] kb = new byte[keysize];
byte[] pt = new byte[BLOCK_SIZE];
int i;
for (i = 0; i < keysize; i++)
kb[i] = (byte) i;
for (i = 0; i < BLOCK_SIZE; i++)
pt[i] = (byte) i;
if (DEBUG && debuglevel > 6) {
System.out.println("==========");
System.out.println();
System.out.println("KEYSIZE="+(8*keysize));
System.out.println("KEY="+toString(kb));
System.out.println();
}
Object key = makeKey(kb);
if (DEBUG && debuglevel > 6) {
System.out.println("Intermediate Ciphertext Values (Encryption)");
System.out.println();
}
byte[] ct = blockEncrypt(pt, 0, key);
if (DEBUG && debuglevel > 6) {
System.out.println("Intermediate Plaintext Values (Decryption)");
System.out.println();
}
byte[] cpt = blockDecrypt(ct, 0, key);
ok = areEqual(pt, cpt);
if (!ok)
throw new RuntimeException("Symmetric operation failed");
} catch (Exception x) {
if (DEBUG && debuglevel > 0) {
debug("Exception encountered during self-test: " + x.getMessage());
x.printStackTrace();
}
}
if (DEBUG && debuglevel > 0) debug("Self-test OK? " + ok);
if (DEBUG) trace(OUT, "self_test()");
return ok;
}
// utility static methods (from cryptix.util.core ArrayUtil and Hex classes)
//...........................................................................
/** @return True iff the arrays have identical contents. */
private static boolean areEqual (byte[] a, byte[] b) {
int aLength = a.length;
if (aLength != b.length)
return false;
for (int i = 0; i < aLength; i++)
if (a[i] != b[i])
return false;
return true;
}
/**
* Returns a string of 8 hexadecimal digits (most significant
* digit first) corresponding to the integer <i>n</i>, which is
* treated as unsigned.
*/
private static String intToString (int n) {
char[] buf = new char[8];
for (int i = 7; i >= 0; i--) {
buf[i] = HEX_DIGITS[n & 0x0F];
n >>>= 4;
}
return new String(buf);
}
/**
* Returns a string of hexadecimal digits from a byte array. Each
* byte is converted to 2 hex symbols.
*/
private static String toString (byte[] ba) {
return toString(ba, 0, ba.length);
}
private static String toString (byte[] ba, int offset, int length) {
char[] buf = new char[length * 2];
for (int i = offset, j = 0, k; i < offset+length; ) {
k = ba[i++];
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
buf[j++] = HEX_DIGITS[ k & 0x0F];
}
return new String(buf);
}
// main(): use to generate the Intermediate Values KAT
//...........................................................................
public static void main (String[] args) {
self_test(16);
self_test(24);
self_test(32);
}
}

View file

@ -0,0 +1,199 @@
// $Id: $
//
// $Log: $
// Revision 1.0 1998/03/24 raif
// + start of history.
//
// $Endlog$
/*
* Copyright (c) 1997, 1998 Systemics Ltd on behalf of
* the Cryptix Development Team. All rights reserved.
*/
package org.monkey.d.ruffy.ruffy.driver;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;
/**
* This class acts as a central repository for an algorithm specific
* properties. It reads an (algorithm).properties file containing algorithm-
* specific properties. When using the AES-Kit, this (algorithm).properties
* file is located in the (algorithm).jar file produced by the "jarit" batch/
* script command.<p>
*
* <b>Copyright</b> &copy; 1997, 1998
* <a href="http://www.systemics.com/">Systemics Ltd</a> on behalf of the
* <a href="http://www.systemics.com/docs/cryptix/">Cryptix Development Team</a>.
* <br>All rights reserved.<p>
*
* <b>$Revision: $</b>
* @author David Hopwood
* @author Jill Baker
* @author Raif S. Naffah
*/
public class Twofish_Properties // implicit no-argument constructor
{
// Constants and variables with relevant static code
//...........................................................................
static final boolean GLOBAL_DEBUG = false;
static final String ALGORITHM = "Twofish";
static final double VERSION = 0.2;
static final String FULL_NAME = ALGORITHM + " ver. " + VERSION;
static final String NAME = "Twofish_Properties";
static final Properties properties = new Properties();
/** Default properties in case .properties file was not found. */
private static final String[][] DEFAULT_PROPERTIES = {
{"Trace.Twofish_Algorithm", "true"},
{"Debug.Level.*", "1"},
{"Debug.Level.Twofish_Algorithm", "9"},
};
static {
if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Looking for " + ALGORITHM + " properties");
String it = ALGORITHM + ".properties";
InputStream is = Twofish_Properties.class.getResourceAsStream(it);
boolean ok = is != null;
if (ok)
try {
properties.load(is);
is.close();
if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Properties file loaded OK...");
} catch (Exception x) {
ok = false;
}
if (!ok) {
if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": WARNING: Unable to load \"" + it + "\" from CLASSPATH.");
if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Will use default values instead...");
int n = DEFAULT_PROPERTIES.length;
for (int i = 0; i < n; i++)
properties.put(
DEFAULT_PROPERTIES[i][0], DEFAULT_PROPERTIES[i][1]);
if (GLOBAL_DEBUG) System.err.println(">>> " + NAME + ": Default properties now set...");
}
}
// Properties methods (excluding load and save, which are deliberately not
// supported).
//...........................................................................
/** Get the value of a property for this algorithm. */
public static String getProperty (String key) {
return properties.getProperty(key);
}
/**
* Get the value of a property for this algorithm, or return
* <i>value</i> if the property was not set.
*/
public static String getProperty (String key, String value) {
return properties.getProperty(key, value);
}
/** List algorithm properties to the PrintStream <i>out</i>. */
public static void list (PrintStream out) {
list(new PrintWriter(out, true));
}
/** List algorithm properties to the PrintWriter <i>out</i>. */
public static void list (PrintWriter out) {
out.println("#");
out.println("# ----- Begin "+ALGORITHM+" properties -----");
out.println("#");
String key, value;
Enumeration enumer = properties.propertyNames();
while (enumer.hasMoreElements()) {
key = (String) enumer.nextElement();
value = getProperty(key);
out.println(key + " = " + value);
}
out.println("#");
out.println("# ----- End "+ALGORITHM+" properties -----");
}
// public synchronized void load(InputStream in) throws IOException {}
public static Enumeration propertyNames() {
return properties.propertyNames();
}
// public void save (OutputStream os, String comment) {}
// Developer support: Tracing and debugging enquiry methods (package-private)
//...........................................................................
/**
* Return true if tracing is requested for a given class.<p>
*
* User indicates this by setting the tracing <code>boolean</code>
* property for <i>label</i> in the <code>(algorithm).properties</code>
* file. The property's key is "<code>Trace.<i>label</i></code>".<p>
*
* @param label The name of a class.
* @return True iff a boolean true value is set for a property with
* the key <code>Trace.<i>label</i></code>.
*/
static boolean isTraceable (String label) {
String s = getProperty("Trace." + label);
if (s == null)
return false;
return new Boolean(s).booleanValue();
}
/**
* Return the debug level for a given class.<p>
*
* User indicates this by setting the numeric property with key
* "<code>Debug.Level.<i>label</i></code>".<p>
*
* If this property is not set, "<code>Debug.Level.*</code>" is looked up
* next. If neither property is set, or if the first property found is
* not a valid decimal integer, then this method returns 0.
*
* @param label The name of a class.
* @return The required debugging level for the designated class.
*/
static int getLevel (String label) {
String s = getProperty("Debug.Level." + label);
if (s == null) {
s = getProperty("Debug.Level.*");
if (s == null)
return 0;
}
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return 0;
}
}
/**
* Return the PrintWriter to which tracing and debugging output is to
* be sent.<p>
*
* User indicates this by setting the property with key <code>Output</code>
* to the literal <code>out</code> or <code>err</code>.<p>
*
* By default or if the set value is not allowed, <code>System.err</code>
* will be used.
*/
static PrintWriter getOutput() {
PrintWriter pw;
String name = getProperty("Output");
if (name != null && name.equals("out"))
pw = new PrintWriter(System.out, true);
else
pw = new PrintWriter(System.err, true);
return pw;
}
}

View file

@ -0,0 +1,251 @@
package org.monkey.d.ruffy.ruffy.driver;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by fishermen21 on 16.05.17.
*/
public class Utils {
public static byte[] generateKey(String strKey)
{
byte[] pin = new byte[10];
for(int i=0;i<strKey.length();i++)
pin[i] = ((byte)(strKey.charAt(i))); //Don't convert to decimal here
byte[] key = new byte[16];
for(int i = 0; i<16; i++)
{
if(i < 10)
{
key[i] = pin[i];
}
else
{
key[i] = (byte) (0xFF ^ pin[i - 10]);
}
}
return key;
}
public static byte[] ccmEncrypt(byte[] padded, Object key, byte[] nonce)
{
byte[] xi = new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //Initialization vector
byte[] u = new byte[8]; //Output array U-MAC
xi[0] = 0x79; //Set flags for IV
for(int i=0;i<nonce.length;i++) //Copy nonce
xi[i+1] = nonce[i];
xi[14] = 0; //Length is zero
xi[15] = 0;
xi = Twofish_Algorithm.blockEncrypt(xi, 0, key); //Encrypt to generate XI from IV
for(int i=0;i<(padded.length / 16);i++)
{
for(int n=0;n<16;n++)
{
xi[n] ^= padded[(i * 16)+n];
}
xi = Twofish_Algorithm.blockEncrypt(xi, 0, key);
}
//should not happen
if ((padded.length % 16) != 0)
{
for (int i=0; i < 16; i++)
{
if ( i < (padded.length % 16) )
{
xi[i] ^= padded[((padded.length / 16) * 16) + i];
}
else
{
xi[i] ^= 16 - (padded.length % 16);
}
}
xi = Twofish_Algorithm.blockEncrypt(xi, 0, key);
}
for(int i=0;i<u.length;i++) //Copy XI to U
u[i] = xi[i];
xi[0] = 65; //Set flags
for(int n=0;n<nonce.length;n++)
xi[n+1] = nonce[n];
xi[14] = 0;
xi[15] = 0;
xi = Twofish_Algorithm.blockEncrypt(xi, 0, key); //Encrypt XI
for(int i=0;i<u.length;i++) //XOR to create U-MAC
u[i] ^= xi[i];
return u;
}
public static boolean ccmVerify(byte[] paddedPacket, Object key, byte[] u, byte[] nonce)
{
paddedPacket = Packet.padPacket(paddedPacket);
boolean verified = true;
byte[] u_prime = Utils.ccmEncrypt(paddedPacket, key, nonce); //Run encryption and check if U-MAC matches
for(int i=0;i<u_prime.length;i++)
{
if(u_prime[i] != u[i]) //Compare U-MAC values
{
verified = false;
break;
}
}
return verified;
}
public static List<Byte> ccmAuthenticate(List<Byte> buffer, Object key, byte[] nonceIn)
{
List<Byte> output = new ArrayList<Byte>();
int origLength = buffer.size(); //Hang on to the original length
byte[] packet = new byte[buffer.size()]; //Create primitive array
for(int i=0;i<packet.length;i++) //Copy to byte array
packet[i] = buffer.get(i);
byte[] paddedPacket = Packet.padPacket(packet);
byte[] nonce = nonceIn;
byte[] umac = Utils.ccmEncrypt(paddedPacket,key,nonce); //Generate U-MAC value
ByteBuffer packetBuffer = ByteBuffer.allocate(origLength + umac.length);
packetBuffer.order(ByteOrder.LITTLE_ENDIAN);
packetBuffer.put(paddedPacket, 0, origLength);
packetBuffer.put(umac);
for(int i=0;i<packetBuffer.array().length;i++) //Convert to List<Byte>
output.add(packetBuffer.array()[i]);
return output;
}
public static void addCRC(List<Byte> out)
{
short crc = -1;
Object[] objArr = new Object[1];
objArr[0] = Short.valueOf((short)-1);
for (int i = 0; i < out.size(); i += 1) {
crc = Utils.updateCrc(crc, ((Byte) out.get(i)).byteValue());
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
}
out.add(Byte.valueOf((byte) (crc & 255)));
out.add(Byte.valueOf((byte) (crc >> 8)));
for (int i = 0; i < 8; i += 1) {
out.add(Byte.valueOf((byte) 0));
}
}
private static short updateCrc(short crc, byte input) {
Object[] objArr = new Object[1];
objArr[0] = Byte.valueOf(input);
short crcTemp = (short) (((short) input) ^ crc);
crc = (short) (((short) (crc >> 8)) & 255);
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
objArr = new Object[1];
objArr[0] = Short.valueOf(crcTemp);
if ((crcTemp & 128) > 0) {
crc = (short) (crc ^ -31736);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 64) > 0) {
crc = (short) (crc ^ 16900);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 32) > 0) {
crc = (short) (crc ^ 8450);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 16) > 0) {
crc = (short) (crc ^ 4225);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 8) > 0) {
crc = (short) (crc ^ -29624);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 4) > 0) {
crc = (short) (crc ^ 17956);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 2) > 0) {
crc = (short) (crc ^ 8978);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
if ((crcTemp & 1) > 0) {
crc = (short) (crc ^ 4489);
}
objArr = new Object[1];
objArr[0] = Short.valueOf(crc);
return crc;
}
public static int incrementArray(byte[] array) {
int i = 0, carry = 0;
array[i]++;
if (array[i] == 0)
carry = 1;
for (i = 1; i < array.length; i++) {
if (carry == 1) {
array[i] += carry;
if (array[i] > 0) {
carry = 0;
return carry;
} else
carry = 1;
}
}
return carry;
}
public static String byteArrayToHexString(byte[] buffer, int bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes; i++) {
sb.append(String.format("%02X ", buffer[i]));
}
return sb.toString();
}
public static byte[] hexStringToByteArray(String s) {
s = s.replaceAll(" ","");
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

View file

@ -0,0 +1,129 @@
package org.monkey.d.ruffy.ruffy.driver.display;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuFactory;
import org.monkey.d.ruffy.ruffy.driver.display.parser.LargeTextParser;
import org.monkey.d.ruffy.ruffy.driver.display.parser.SmallTextParser;
import java.util.LinkedList;
/**
* Created by fishermen21 on 20.05.17.
*/
public class DisplayParser {
private static boolean busy = false;
public static void findMenu(final byte[][] pixels, final DisplayParserHandler handler) {
if(busy)
{
// Log.v("Tokens","skipping frame, busy…");
return;
}
busy = true;
long t1 = System.currentTimeMillis();
try {
byte[][] display = new byte[4][96];
for(int i = 0; i < 4;i++)
for(int c = 0;c < 96;c++)
display[i][c]=pixels[i][95-c];
LinkedList<Token>[] tokens = new LinkedList[]{
new LinkedList<Token>(),
new LinkedList<Token>(),
new LinkedList<Token>(),
new LinkedList<Token>()};
int toks = 0;
for(int i = 0; i< 4;i++)
{
for(int x = 0; x < 92;)//no token is supposed to be smaller then 5 columns
{
Token t = null;
t = LargeTextParser.findToken(display,i,x);
if(t==null)
{
t = SmallTextParser.findToken(display, i, x);
}
if (t != null) {
tokens[i].add(t);
toks++;
x += t.getWidth()-1;
} else {
x++;
}
}
}
long s = 0;
for(int i = 0; i< 4;i++) {
for (int x = 0; x < 96; x++) {
s+=display[i][x];
}
}
if(s!=0)
{
print(display,"not empty");
}
Menu menu = MenuFactory.get(tokens);
if(menu != null) {
// Log.v("tokens", " needed " + ((((double) (System.currentTimeMillis() - t1)) / 1000d)) + " for parsing " + (menu != null ? menu.getType() : "no menu"));
menu.setAttribute(MenuAttribute.DEBUG_TIMING, (((double) (System.currentTimeMillis() - t1)) / 1000d));
handler.menuFound(menu);
}
else
handler.noMenuFound();
int nct = 0;
for(int i=0;i<4;i++)nct+=tokens[i].size();
// if(nct>0 && menu!= null)
// Log.v("tokens",nct+" toks not consumed in "+menu.getType());
}catch(Throwable e){e.printStackTrace();
// Log.e("Tokens","error...",e);
}
finally {
busy=false;
}
}
private static String[] makeStrings(boolean[][][] pixels) {
String[] display = new String[32];
for (int w = 0; w < 4; w++) {
for (int r = 0; r < 8; r++) {
String line = "";
for (int c = 0; c < 96; c++) {
line += pixels[w][r][c] ? "" : " ";
}
display[(w*8)+r]=line;
}
}
return display;
}
public static void print(byte[][] display, String text) {
// Log.d("DisplayParser","////////////////////////////////////////////////////////////////////////////////////////////////");
// Log.d("DisplayParser",text);
for (int i = 0; i < 4; i++) {
String[] lines = new String[]{"","","","","","","",""};
for(int c = 0;c < 96;c++)
{
for(int r = 0; r < 8; r++) {
lines[r] += (display[i][c] & ((1 << r) & 0xFF)) != 0 ? "" : " ";
}
}
// for(int r = 0; r < 8; r++) {
// Log.d("DisplayParser", lines[r]);
// }
}
// Log.d("DisplayParser","////////////////////////////////////////////////////////////////////////////////////////////////");
}
}

View file

@ -0,0 +1,11 @@
package org.monkey.d.ruffy.ruffy.driver.display;
/**
* Created by fishermen21 on 22.05.17.
*/
public interface DisplayParserHandler {
void menuFound(Menu menu);
void noMenuFound();
}

View file

@ -2,7 +2,6 @@ package org.monkey.d.ruffy.ruffy.driver.display;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.util.Log;
import org.monkey.d.ruffy.ruffy.driver.display.menu.BolusType; import org.monkey.d.ruffy.ruffy.driver.display.menu.BolusType;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuBlink; import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuBlink;
@ -18,7 +17,7 @@ import java.util.Map;
* Created by fishermen21 on 20.05.17. * Created by fishermen21 on 20.05.17.
*/ */
public class Menu implements Parcelable{ public class Menu implements Parcelable {
private MenuType type; private MenuType type;
private Map<MenuAttribute,Object> attributes = new HashMap<>(); private Map<MenuAttribute,Object> attributes = new HashMap<>();
@ -35,36 +34,34 @@ public class Menu implements Parcelable{
String clas = in.readString(); String clas = in.readString();
String value = in.readString(); String value = in.readString();
if(attr!=null && clas!=null && value!=null) { MenuAttribute a = MenuAttribute.valueOf(attr);
MenuAttribute a = MenuAttribute.valueOf(attr); Object o = null;
Object o = null; if (Integer.class.toString().equals(clas)) {
if (Integer.class.toString().equals(clas)) { o = new Integer(value);
o = new Integer(value); } else if (Double.class.toString().equals(clas)) {
} else if (Double.class.toString().equals(clas)) { o = new Double(value);
o = new Double(value); } else if (Boolean.class.toString().equals(clas)) {
} else if (Boolean.class.toString().equals(clas)) { o = new Boolean(value);
o = new Boolean(value); } else if (MenuDate.class.toString().equals(clas)) {
} else if (MenuDate.class.toString().equals(clas)) { o = new MenuDate(value);
o = new MenuDate(value); } else if (MenuTime.class.toString().equals(clas)) {
} else if (MenuTime.class.toString().equals(clas)) { o = new MenuTime(value);
o = new MenuTime(value); } else if (MenuBlink.class.toString().equals(clas)) {
} else if (MenuBlink.class.toString().equals(clas)) { o = new MenuBlink();
o = new MenuBlink(); } else if (BolusType.class.toString().equals(clas)) {
} else if (BolusType.class.toString().equals(clas)) { o = BolusType.valueOf(value);
o = BolusType.valueOf(value); } else if (String.class.toString().equals(clas)) {
} else if (String.class.toString().equals(clas)) { o = new String(value);
o = new String(value); }
}
if (o != null) { if (o != null) {
attributes.put(a, o); attributes.put(a, o);
} else { } else {
Log.e("MenuIn", "failed to parse: " + attr + " / " + clas + " / " + value); // Log.e("MenuIn", "failed to parse: " + attr + " / " + clas + " / " + value);
}
} }
}catch(Exception e) }catch(Exception e)
{ {
Log.e("MenuIn","Exception in read",e); // Log.e("MenuIn","Exception in read",e);
} }
} }
@ -108,12 +105,12 @@ public class Menu implements Parcelable{
dest.writeString(o.toString()); dest.writeString(o.toString());
}catch(Exception e) }catch(Exception e)
{ {
Log.v("MenuOut","error in write",e); // Log.v("MenuOut","error in write",e);
} }
} }
} }
public static final Creator<Menu> CREATOR = new public static final Parcelable.Creator<Menu> CREATOR = new
Creator<Menu>() { Parcelable.Creator<Menu>() {
public Menu createFromParcel(Parcel in) { public Menu createFromParcel(Parcel in) {
return new Menu(in); return new Menu(in);
} }
@ -122,12 +119,4 @@ public class Menu implements Parcelable{
return new Menu[size]; return new Menu[size];
} }
}; };
@Override
public String toString() {
String to = "Menu: "+getType()+" atr:";
for(MenuAttribute atr : attributes())
to+=atr.toString()+"="+getAttribute(atr)+";";
return to;
}
} }

View file

@ -5,47 +5,38 @@ package org.monkey.d.ruffy.ruffy.driver.display;
*/ */
public enum MenuType { public enum MenuType {
MAIN_MENU(true), MAIN_MENU,
STOP_MENU(true), STOP_MENU,
BOLUS_MENU(true), BOLUS_MENU,
BOLUS_ENTER(false), BOLUS_ENTER,
EXTENDED_BOLUS_MENU(true), EXTENDED_BOLUS_MENU,
BOLUS_DURATION(false), BOLUS_DURATION,
MULTIWAVE_BOLUS_MENU(true), MULTIWAVE_BOLUS_MENU,
IMMEDIATE_BOLUS(false), IMMEDIATE_BOLUS,
TBR_MENU(true), TBR_MENU,
MY_DATA_MENU(true), MY_DATA_MENU,
BASAL_MENU(true), BASAL_MENU,
BASAL_1_MENU(true), BASAL_1_MENU,
BASAL_2_MENU(true), BASAL_2_MENU,
BASAL_3_MENU(true), BASAL_3_MENU,
BASAL_4_MENU(true), BASAL_4_MENU,
BASAL_5_MENU(true), BASAL_5_MENU,
DATE_AND_TIME_MENU(true), DATE_AND_TIME_MENU,
ALARM_MENU(true), ALARM_MENU,
MENU_SETTINGS_MENU(true), MENU_SETTINGS_MENU,
BLUETOOTH_MENU(true), BLUETOOTH_MENU,
THERAPY_MENU(true), THERAPY_MENU,
PUMP_MENU(true), PUMP_MENU,
QUICK_INFO(false), QUICK_INFO,
BOLUS_DATA(false), BOLUS_DATA,
DAILY_DATA(false), DAILY_DATA,
TBR_DATA(false), TBR_DATA,
ERROR_DATA(false), ERROR_DATA,
TBR_SET(false), TBR_SET,
TBR_DURATION(false), TBR_DURATION,
STOP(false), STOP,
START_MENU(false), START_MENU,
BASAL_TOTAL(false), BASAL_TOTAL,
BASAL_SET(false), BASAL_SET,
WARNING_OR_ERROR(false),; WARNING_OR_ERROR,
private boolean maintype = false;
MenuType(boolean b) {
maintype=b;
}
public boolean isMaintype() {
return maintype;
}
} }

View file

@ -0,0 +1,62 @@
package org.monkey.d.ruffy.ruffy.driver.display;
/**
* Created by fishermen21 on 20.05.17.
*/
public enum Symbol {
CLOCK,
LOCK_CLOSED,
LOCK_OPENED,
CHECK,
LOW_BAT,
WARNING,
DIVIDE,
LOW_INSULIN,
NO_INSULIN,
CALENDAR,
SEPERATOR,
ARROW,
UNITS_PER_HOUR,
BOLUS,
MULTIWAVE,
SPEAKER,
ERROR,
DOT,
UP,
DOWN,
SUM,
BRACKET_RIGHT,
BRACKET_LEFT,
EXTENDED_BOLUS,
PERCENT,
BASAL,
MINUS,
WARANTY,
LARGE_DOT,
LARGE_SEPERATOR,
LARGE_WARNING,
LARGE_PERCENT,
LARGE_UNITS_PER_HOUR,
LARGE_BASAL_SET,
LARGE_AMPULE_FULL,
LARGE_ARROW,
LARGE_STOP,
LARGE_CALENDAR,
LARGE_TBR,
LARGE_BOLUS,
LARGE_MULTIWAVE,
LARGE_MULTIWAVE_BOLUS,
LARGE_EXTENDED_BOLUS,
LARGE_BLUETOOTH_SETTINGS,
LARGE_THERAPIE_SETTINGS,
LARGE_PUMP_SETTINGS,
LARGE_MENU_SETTINGS,
LARGE_BASAL,
LARGE_MY_DATA,
LARGE_ALARM_SETTINGS,
LARGE_CHECK,
LARGE_ERROR,
}

View file

@ -0,0 +1,40 @@
package org.monkey.d.ruffy.ruffy.driver.display;
import org.monkey.d.ruffy.ruffy.driver.display.parser.Pattern;
/**
* Created by fishermen21 on 21.05.17.
*/
public class Token {
private final Pattern pattern;
private final int block;
private final int column;
public Token(Pattern pattern, int block, int x) {
this.pattern = pattern;
this.block = block;
this.column = x;
}
public int getWidth() {
return pattern.getWidth();
}
@Override
public String toString() {
return pattern+" at ["+block+" / "+column+"]";
}
public Pattern getPattern() {
return pattern;
}
public int getBlock() {
return block;
}
public int getColumn() {
return column;
}
}

View file

@ -22,6 +22,6 @@ public class MenuDate {
@Override @Override
public String toString() { public String toString() {
return day+"."+String.format("%02d",month)+"."; return day+"."+ String.format("%02d",month)+".";
} }
} }

View file

@ -31,6 +31,6 @@ public class MenuTime {
@Override @Override
public String toString() { public String toString() {
return hour+":"+String.format("%02d",minute); return hour+":"+ String.format("%02d",minute);
} }
} }

View file

@ -0,0 +1,18 @@
package org.monkey.d.ruffy.ruffy.driver.display.menu;
/**
* Created by fishermen21 on 22.05.17.
*/
enum Title {
BOLUS_AMOUNT,
IMMEDIATE_BOLUS,
BOLUS_DURATION,
QUICK_INFO,
BOLUS_DATA,
ERROR_DATA,
DAILY_TOTALS,
TBR_DATA,
TBR_SET,
TBR_DURATION,
}

View file

@ -0,0 +1,459 @@
package org.monkey.d.ruffy.ruffy.driver.display.menu;
/**
* Created by fishermen21 on 22.05.17.
*/
class TitleResolver {
public static Title resolve(String title) {
/**english titles**/
if(title.equalsIgnoreCase("bolus amount"))
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("immediate bolus"))
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolus duration"))
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info"))
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolus data"))
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("error data"))
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("daily totals"))
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tbr data"))
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tbr percentage"))
return Title.TBR_SET;
if(title.equalsIgnoreCase("tbr duration"))
return Title.TBR_DURATION;
/**german titles**/
if(title.equalsIgnoreCase("bolus-menge"))
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("sofortige abgabe"))
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("abgabedauer"))
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info"))
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusinformation"))
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("fehlermeldungen"))
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("tagesgesamtmenge"))
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tbr-information"))
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tbr wert"))
return Title.TBR_SET;
if(title.equalsIgnoreCase("tbr dauer"))
return Title.TBR_DURATION;
/**French titles**/
if(title.equalsIgnoreCase("quantité bolus"))
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("quanti. immédiate"))
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("durée du bolus"))
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info"))
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolus"))
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("erreurs"))
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("quantités journ."))
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("dbt"))
return Title.TBR_DATA;
if(title.equalsIgnoreCase("valeur du dbt"))
return Title.TBR_SET;
if(title.equalsIgnoreCase("durée du dbt"))
return Title.TBR_DURATION;
/**spanish titles**/
if(title.equalsIgnoreCase("cantidad de bolo"))
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("bolo inmediato"))
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("duración de bolo"))
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info"))
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("datos de bolo"))
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("datos de error"))
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("totales diarios"))
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("datos de dbt"))
return Title.TBR_DATA;
if(title.equalsIgnoreCase("porcentaje dbt"))
return Title.TBR_SET;
if(title.equalsIgnoreCase("duración de dbt"))
return Title.TBR_DURATION;
/**italian titles**/
if(title.equalsIgnoreCase("quantita bolo")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("bolo immediato")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("tempo erogazione")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("memoria boli")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("memoria allarmi")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("totali giornata")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("memoria pbt")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("percentuale pbt")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("durata pbt")) //TBR 2
return Title.TBR_DURATION;
/**dutch titles**/
if(title.equalsIgnoreCase("bolushoeveelheid")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("directe bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolusduur")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusgegevens")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("foutengegevens")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("dagtotalen")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tbd-gegevens")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tbd-percentage")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("tbd-duur")) //TBR 2
return Title.TBR_DURATION;
/**norwegian titles**/
if(title.equalsIgnoreCase("bolusmengde")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("umiddelbar bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolusvarighet")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusdata")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("feildata")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("døgnmengde")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("mbd-data")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("mbd-prosent")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("mbd-varighet")) //TBR 2
return Title.TBR_DURATION;
/**polish titles**/
if(title.equalsIgnoreCase("wielkość bolusa")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("bolus natychm.")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("cz. trw. bolusa")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("dane bolusa")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("dane błędu")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("dzien. d. całk.")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("dane tdp")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("procent tdp")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("czas trwania tdp")) //TBR 2
return Title.TBR_DURATION;
/**cz titles**/
if(title.equalsIgnoreCase("množství bolusu")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("okamžitý bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("trvání bolusu")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("údaje bolusů")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("údaje chyb")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("celk. den. dávky")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("údaje dbd")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("procento dbd")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("trvání dbd")) //TBR 2
return Title.TBR_DURATION;
/**finnish titles**/
if(title.equalsIgnoreCase("boluksen määrä")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("nopea bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("boluksen kesto")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolustiedot")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("hälytystiedot")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("päiv. kok.annos")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tba - tiedot")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tba - prosentti")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("tba - kesto")) //TBR 2
return Title.TBR_DURATION;
/**turkish titles**/
if(title.equalsIgnoreCase("bolus mİktari")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("hemen bolus uygl")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolus süresİ")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolus verİlerİ")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("hata verİlerİ")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("günlük toplam")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("gbh verİlerİ")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("gbh yüzdesİ")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("gbh süresİ")) //TBR 2
return Title.TBR_DURATION;
/**romanian titles**/
if(title.equalsIgnoreCase("cantitate bolus")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("bolus imediat")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("durată bolus")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("date bolus")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("date eroare")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("totaluri zilnice")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("date rbt")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("procent rbt")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("durata rbt")) //TBR 2
return Title.TBR_DURATION;
/**swedish titles**/
if(title.equalsIgnoreCase("bolusmängd")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("direkt bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolusduration")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusdata")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("feldata")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("dygnshistorik")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tbd data")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tbd procent")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("tbd duration")) //TBR 2
return Title.TBR_DURATION;
/**danish titles**/
if(title.equalsIgnoreCase("bolusmængde")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("umiddelbar bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bolusvarighed")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusdata")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("fejldata")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("daglig total")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("mbr-data")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("mbr-procent")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("mbr-varighed")) //TBR 2
return Title.TBR_DURATION;
/**hungarian titles**/
if(title.equalsIgnoreCase("bólusmennyiség")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("azonnali bólus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("bólus időtartam")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bólusadatok")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("hibaadatok")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("napi teljes")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("tbr-adatok")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("tbr százalék")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("tbr időtartam")) //TBR 2
return Title.TBR_DURATION;
/**slovak titles**/
if(title.equalsIgnoreCase("množstvo bolusu")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("okamžitý bolus")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("trvanie bolusu")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("bolusové dáta")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("dáta o chybách")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("súčty dňa")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("dbd dáta")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("percento dbd")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("trvanie dbd")) //TBR 2
return Title.TBR_DURATION;
/**portugues titles**/
if(title.equalsIgnoreCase("volume do bolus")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("bolus imediato")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("duraÇão do bolus")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("dados de bolus")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("dados de erros")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("totais diários")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("dados dbt")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("dbt percentagem")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("dbt duraÇão")) //TBR 2
return Title.TBR_DURATION;
/**russian titles**/
if(title.equalsIgnoreCase("OбъEм бOлюCа")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("пPямOй бOлюC")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("пPOдOлж. бOлюCа")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("даHHыE O бOлюCE")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("даHHыE Oб O иб.")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("CуTOчHыE дOзы")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("даHHыE O BбC")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("пPOцEHT BбC")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("пPOдOлжиT. BбC")) //TBR 2
return Title.TBR_DURATION;
/**croatian titles**/
if(title.equalsIgnoreCase("KOLIčINA BOLUSA")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("TRENUTNI BOLUS")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("TRAJANJE BOLUSA")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("PODACI O BOLUSU")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("PODACI O GREšK.")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("UKUPNE DNEV.DOZE")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("PODACI O PBD-u")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("POSTOTAK PBD-a")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("TRAJANJE PBD-a")) //TBR 2
return Title.TBR_DURATION;
/**greek titles**/
if(title.equalsIgnoreCase("пOΣOTHTа ΔOΣHΣ")) //multiwave 1
return Title.BOLUS_AMOUNT;
if(title.equalsIgnoreCase("амEΣH ΔOΣH")) //multiwave 2
return Title.IMMEDIATE_BOLUS;
if(title.equalsIgnoreCase("ΔIаPKEIа ΔOΣHΣ")) //multiwave 3
return Title.BOLUS_DURATION;
if(title.equalsIgnoreCase("quick info")) //check1
return Title.QUICK_INFO;
if(title.equalsIgnoreCase("ΔEΔOмENа ΔOΣEΩN")) //check2, mydata 1
return Title.BOLUS_DATA;
if(title.equalsIgnoreCase("ΔEΔOм. ΣΦаΛмаTΩN")) //mydata 2
return Title.ERROR_DATA;
if(title.equalsIgnoreCase("HмEPHΣIO ΣυNOΛO")) //mydata 3
return Title.DAILY_TOTALS;
if(title.equalsIgnoreCase("ΔEΔOмENа п.B.P.")) //mydata 4
return Title.TBR_DATA;
if(title.equalsIgnoreCase("пOΣOΣTO п.B.P.")) //TBR 1
return Title.TBR_SET;
if(title.equalsIgnoreCase("ΔIаPKEIа п.B.P.")) //TBR 2
return Title.TBR_DURATION;
//FIXME add Translations
return null;
}
}

View file

@ -0,0 +1,23 @@
package org.monkey.d.ruffy.ruffy.driver.display.parser;
/**
* Created by fishermen21 on 21.05.17.
*/
public class CharacterPattern extends Pattern {
private final char character;
public CharacterPattern(char c, String[] patternString, int blocksize) {
super(patternString,blocksize);
this.character = c;
}
@Override
public String toString() {
return "Character("+character+")";
}
public char getCharacter() {
return character;
}
}

View file

@ -0,0 +1,683 @@
package org.monkey.d.ruffy.ruffy.driver.display.parser;
import org.monkey.d.ruffy.ruffy.driver.display.Symbol;
import org.monkey.d.ruffy.ruffy.driver.display.Token;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* Created by fishermen21 on 20.05.17.
*/
public class LargeTextParser {
private static String[][] numbers = {
{
" ████ ",
" ██ ██ ",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ██ ██ ",
" ████ "
},
{
" ██ ",
" ███ ",
" ████ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ "
},
{
" ████ ",
" ██ ██ ",
"██ ██",
"██ ██",
" ██",
" ██",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
"██ ",
"██ ",
"██ ",
"████████"
},
{
" █████ ",
"██ ██ ",
" ██",
" ██",
" ██",
" ██ ",
" ███ ",
" ██ ",
" ██",
" ██",
" ██",
" ██",
" ██",
"██ ██ ",
" █████ "
},
{
" ██ ",
" ███ ",
" ███ ",
" ████ ",
" █ ██ ",
" ██ ██ ",
" █ ██ ",
" ██ ██ ",
"██ ██ ",
"████████",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ "
},
{
"███████ ",
"██ ",
"██ ",
"██ ",
"██ ",
"██████ ",
" ██ ",
" ██",
" ██",
" ██",
" ██",
" ██",
" ██",
"██ ██ ",
" █████ "
},
{
" ███ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
"██ ",
"██████ ",
"███ ██ ",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ██ ██ ",
" ████ "
},
{
"████████",
" ██",
" ██",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ "
},
{
" ████ ",
" ██ ██ ",
"██ ██",
"██ ██",
"██ ██",
" ██ ██ ",
" ████ ",
" ██ ██ ",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ██ ██ ",
" ████ "
},
{
" ████ ",
" ██ ██ ",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ██ ███",
" ██████",
" ██",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ███ "
}
};
private static Map<Character, String[]> letters = new HashMap<Character, String[]>();
static {
letters.put('E', new String[]{
"████████",
"██ ",
"██ ",
"██ ",
"██ ",
"██ ",
"██ ",
"███████ ",
"██ ",
"██ ",
"██ ",
"██ ",
"██ ",
"██ ",
"████████"
});
letters.put('W', new String[]{
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ██ ██",
"██ ████ ██",
"██████████",
" ███ ███ ",
" █ █ "
});
letters.put('u', new String[]{
" ",
" ",
" ",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ████ "
});
}
private static Map<Symbol, String[]> symbols = new HashMap<Symbol, String[]>();
static {
symbols.put(Symbol.LARGE_DOT, new String[]{
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"███ ",
"███ ",
"███ ",
" "
});
symbols.put(Symbol.LARGE_SEPERATOR, new String[]{
" ",
" ",
" ",
"███ ",
"███ ",
"███ ",
" ",
" ",
"███ ",
"███ ",
"███ ",
" ",
" ",
});
symbols.put(Symbol.LARGE_WARNING, new String[]{
" ██ ",
" ████ ",
" █ █ ",
" ██ ██ ",
" █ █ ",
" ██ ██ ██ ",
" █ ██ █ ",
" ██ ██ ██ ",
" █ ██ █ ",
" ██ ██ ██ ",
" █ █ ",
" ██ ██ ██ ",
" █ █ ",
"████████████████",
" ███████████████"
});
symbols.put(Symbol.LARGE_PERCENT, new String[]{
" ██ ██",
"████ ██ ",
"████ ██ ",
" ██ ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ",
" ██ ██ ",
" ██ ████",
" ██ ████",
"██ ██ ",
});
symbols.put(Symbol.LARGE_UNITS_PER_HOUR, new String[]{
"██ ██ ██ ██ ",
"██ ██ ██ ██ ",
"██ ██ ██ ██ ",
"██ ██ ██ ██ ",
"██ ██ ██ █████ ",
"██ ██ ██ ███ ██",
"██ ██ ██ ██ ██",
"██ ██ ██ ██ ██",
"██ ██ ██ ██ ██",
"██ ██ ██ ██ ██",
"██ ██ ██ ██ ██",
" ████ ██ ██ ██"
});
symbols.put(Symbol.LARGE_BASAL_SET, new String[]{
" ███████ ",
" ███████ ",
" ██ █ ██ ",
" ███ ████████",
" ██ █ ███████",
"████████ █ █ █ ██",
"███████ █ █ █ ███",
"██ █ █ █ █ █ █ ██",
"███ █ █ █ █ █ ███",
"██ █ █ █ █ █ █ ██",
"███ █ █ █ █ █ ███",
"██ █ █ █ █ █ █ ██",
"███ █ █ █ █ █ ███",
"██ █ █ █ █ █ █ ██"
});
symbols.put(Symbol.LARGE_AMPULE_FULL, new String[]{
"████████████████████ ",
"████████████████████ ",
"████████████████████ ███",
"██████████████████████ █",
"██████████████████████ █",
"██████████████████████ █",
"██████████████████████ █",
"████████████████████ ███",
"████████████████████ ",
"████████████████████ "
});
symbols.put(Symbol.LARGE_ARROW, new String[]{
" ██ ",
" ███ ",
" ████ ",
" █████ ",
" ██████ ",
"███████████████ ",
"████████████████",
"████████████████",
"███████████████ ",
" ██████ ",
" █████ ",
" ████ ",
" ███ ",
" ██ "
});
symbols.put(Symbol.LARGE_EXTENDED_BOLUS, new String[]{
"█████████████ ",
"█████████████ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ █████",
"██ █████"
});
symbols.put(Symbol.LARGE_MULTIWAVE, new String[]{
"██████ ",
"██████ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ████████████",
"██ ████████████",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██"
});
symbols.put(Symbol.LARGE_BOLUS, new String[]{
" ██████ ",
" ██████ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ ",
"█████ ████████",
"█████ ████████"
});
symbols.put(Symbol.LARGE_MULTIWAVE_BOLUS, new String[]{
"██████ ",
"██████ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ",
"██ ██ ██ ██ ██",
"██ ██ ██ ██ ██",
"██ ",
"██ ██",
"██ ██",
"██ ",
"██ ██",
"██ ██"
});
symbols.put(Symbol.LARGE_STOP, new String[]{
" ████████ ",
" ██████████ ",
" ████████████ ",
" ██████████████ ",
"████████████████",
"█ █ █ █ ██",
"█ ███ ██ █ █ █ █",
"█ ██ ██ █ █ ██",
"██ ██ ██ █ █ ███",
"█ ██ ██ █ ███",
"████████████████",
" ██████████████ ",
" ████████████ ",
" ██████████ ",
" ████████ "
});
symbols.put(Symbol.LARGE_CALENDAR, new String[]{
" █████ ",
" █ █ ",
"██████ █ █ ",
"█ █ █ █ ",
"█████ █ █ ",
"█ █ █ ███ █ ",
"█████ ",
"█ █ █ ███████",
"██████ █ █",
"█ █ █ █ █ ██",
"█████████ █ █ █",
"█ █ █ █ █ ██ █ █",
"█████████ █ █ █",
" ████████ ███████"
});
symbols.put(Symbol.LARGE_TBR, new String[]{
" ███████ ██ ██",
" ███████ ████ ██ ",
" ██ ██ ████ ██ ",
" ██ ███████ ██ ██ ",
" ██ ███████ ██ ",
"███████ ██ ██ ██ ",
"███████ ██ ██ ██ ",
"██ ██ ██ ██ ██ ",
"██ ██ ██ ██ ██ ",
"██ ██ ██ ██ ██ ",
"██ ██ ██ ██ ██ ██ ",
"██ ██ ██ ██ ██ ████",
"██ ██ ██ ██ ██ ████",
"██ ██ ██ ██ ██ ██ "
});
symbols.put(Symbol.LARGE_BASAL, new String[]{
" ███████ ",
" ███████ ",
" ██ ██ ",
" ██ ███████",
" ██ ███████",
"███████ ██ ██",
"███████ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██",
"██ ██ ██ ██"
});
symbols.put(Symbol.LARGE_PUMP_SETTINGS, new String[]{
"███████████ ",
"███████████ ",
"████████████ ",
"██ ███ ",
"██ ████ ",
"█████████████ ",
"██ ██████ ",
"███████████████ ██",
"███████████████ █",
" ██",
" █ █ █",
" ██ █ █",
" █ █ █",
" ███████"
});
symbols.put(Symbol.LARGE_PUMP_SETTINGS, new String[]{
"███████████ ",
"███████████ ",
"████████████ ",
"██ ███ ",
"██ ████ ",
"█████████████ ",
"██ ██████ ",
"███████████████ ██",
"███████████████ █",
" ██",
" █ █ █",
" ██ █ █",
" █ █ █",
" ███████"
});
symbols.put(Symbol.LARGE_THERAPIE_SETTINGS, new String[]{
" ████ ",
" █ █ ",
" █ █ ",
"████ ████ ",
"█ █ ",
"█ █ ",
"████ ████ ",
" █ █ ",
" █ █ ███████",
" ████ █ █",
" █ ██",
" █ █ █",
" ██ █ █",
" █ █ █",
" ███████"
});
symbols.put(Symbol.LARGE_BLUETOOTH_SETTINGS, new String[]{
" ██████ ",
" ███ ████ ",
" ███ ███ ",
"████ █ ███ ",
"████ ██ ██ ",
"██ █ █ ███ ",
"███ ████ ",
"████ ██ ",
"███ █ ███████",
"██ █ █ █ █",
"████ ██ █ ██",
"████ █ █ █ █",
" ███ █ ██ █ █",
" ███ ██ █ █ █",
" █████ ███████"
});
symbols.put(Symbol.LARGE_MENU_SETTINGS, new String[]{
" █████████ ",
" █ █ ",
" █ █ ",
"█████████ █ ",
"█████████ █ ",
"█████████ █ ",
"█████████ █ ",
"█████ ",
"█████ ███████",
"█████ █ █",
"█████ █ ██",
"█████ █ █ █",
"█████ ██ █ █",
"█████ █ █ █",
" ███████"
});
symbols.put(Symbol.LARGE_MY_DATA, new String[]{
" ████ ",
" ██████ ",
" ████████ ",
" ██ ██ ",
"",
"███████ █ ",
"█ █ █ ",
"█ ███ █ ███ ",
"█ █ ",
"█ ███ █ ████ ",
"█ █ █████ ",
"█ █ ██████",
"███████ ██████"
});
symbols.put(Symbol.LARGE_ALARM_SETTINGS, new String[]{
"",
" █ █ ",
" ███ ",
" █ █ █ ",
" █ █ █ ",
" █ █ ██ ",
" █ █ █ ",
" █ █ █ ",
" █ █ ███████",
" █ █ █ █ █",
" █ █ █ ██",
"█████████ █ █ █",
" ███ ██ █ █",
" █ █ █ █",
" ███████"
});
symbols.put(Symbol.LARGE_CHECK, new String[]{
" ███",
" ███ ",
" ███ ",
" ███ ",
"███ ███ ",
" ███ ███ ",
" ███ ███ ",
" █████ ",
" ███ ",
""
});
symbols.put(Symbol.LARGE_ERROR, new String[]{
" █████ ",
" █████████ ",
" ███████████ ",
" ███ █████ ███ ",
" ██ ███ ██ ",
"████ █ ████",
"█████ █████",
"██████ ██████",
"█████ █████",
"████ █ ████",
" ██ ███ ██ ",
" ███ █████ ███ ",
" ███████████ ",
" █████████ ",
" █████ "
});
}
private static LinkedList<Pattern> pattern = new LinkedList<>();
static
{
for(Symbol s : Symbol.values())
{
if(symbols.containsKey(s)) {
String[] patternString = symbols.get(s);
Pattern p = new SymbolPattern(s, patternString, 16);
pattern.add(p);
}
}
for(int i = 0; i < 10;i++)
{
String[] patternString = numbers[i];
Pattern p = new NumberPattern(i,patternString,16);
pattern.add(p);
}
for(Character c : letters.keySet())
{
String[] patternString = letters.get(c);
Pattern p = new CharacterPattern(c,patternString,16);
pattern.add(p);
}
}
public static Token findToken(byte[][] display, int which, int x) {
for(Pattern p : pattern)
{
Token t = p.match(display, which, x);
if (t != null)
return t;
}
return null;
}
}

View file

@ -0,0 +1,23 @@
package org.monkey.d.ruffy.ruffy.driver.display.parser;
/**
* Created by fishermen21 on 21.05.17.
*/
public class NumberPattern extends Pattern {
private final int number;
public NumberPattern(int number, String[] patternString, int blockSize) {
super(patternString,blockSize);
this.number = number;
}
@Override
public String toString() {
return "Numbervalue("+number+")";
}
public int getNumber() {
return number;
}
}

View file

@ -0,0 +1,79 @@
package org.monkey.d.ruffy.ruffy.driver.display.parser;
import org.monkey.d.ruffy.ruffy.driver.display.Token;
/**
* Created by fishermen21 on 21.05.17.
*/
public abstract class Pattern {
private final byte[][] pattern;
private final int shiftable;
public Pattern(String[] patternString, int blocksize) {
this.shiftable = blocksize - patternString.length;
this.pattern = new byte[blocksize/8][patternString[0].length()];
for(int row = 0; row < patternString.length;row+=8)
{
for(int c = 0; c < patternString[row].length();c++)
{
byte b = 0;
for(int br = 0; br < 8; br++)
{
if (row+br >= patternString.length)
break;
if (patternString[row+br].charAt(c) == '█')
b |= 1 << br;
}
pattern[row/8][c] = b;
}
}
}
public Token match(byte[][] display, int which, int x)
{
int s = 0;
while(s<=shiftable)
{
boolean run = true;
for(int r = 0;run && r < pattern.length;r++)
{
if(which+r>3){
run=false;
break;
}
for(int c = 0;run && c < pattern[0].length; c++)
{
byte compare = ((byte)(pattern[r][c]<<s));
if(r>0)
{
short cs = pattern[r][c];
cs = ((short)(cs << 8));
cs |= pattern[r-1][c]&0xFF;
cs = ((short)(cs << s));
compare = (byte) (cs >> 8);
}
if(x+c >= 96 || display[which+r][x+c] != compare)
run = false;
}
}
if(run)
{
for(int r = 0;run && r < pattern.length;r++) {
for (int c = 0; run && c < pattern[0].length; c++) {
display[which+r][x + c] = 0;
}
}
return new Token(this, which, x);//pattern found
}
s++;
}
return null;
}
public int getWidth() {
return pattern[0].length;
}
}

View file

@ -0,0 +1,26 @@
package org.monkey.d.ruffy.ruffy.driver.display.parser;
import org.monkey.d.ruffy.ruffy.driver.display.Symbol;
/**
* Created by fishermen21 on 21.05.17.
*/
public class SymbolPattern extends Pattern {
private final Symbol symbol;
public SymbolPattern(Symbol s, String[] patternString, int blocksize) {
super(patternString, blocksize);
this.symbol = s;
}
@Override
public String toString() {
return "Symbol("+symbol+")";
}
public Symbol getSymbol() {
return symbol;
}
}