2016-10-23 15:34:57 +02:00
|
|
|
package de.mas.wupclient.client;
|
2016-10-22 18:52:14 +02:00
|
|
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.util.Arrays;
|
2016-10-23 15:34:57 +02:00
|
|
|
|
2016-10-23 16:28:05 +02:00
|
|
|
import de.mas.wupclient.client.utils.Logger;
|
2016-10-23 15:34:57 +02:00
|
|
|
import de.mas.wupclient.client.utils.Result;
|
2016-10-22 18:52:14 +02:00
|
|
|
|
|
|
|
public class WUPClient {
|
|
|
|
private String IP;
|
|
|
|
private int fsaHandle = -1;
|
|
|
|
private String cwd = "";
|
|
|
|
private Socket socket;
|
|
|
|
|
|
|
|
public WUPClient(String ip){
|
|
|
|
setIP(ip);
|
|
|
|
createSocket(ip);
|
|
|
|
setFsaHandle(-1);
|
|
|
|
setCwd("/vol/storage_mlc01");
|
|
|
|
}
|
|
|
|
|
2016-10-23 15:34:57 +02:00
|
|
|
public Result<byte[]> send(int command, byte[] data) throws IOException{
|
2016-10-22 18:52:14 +02:00
|
|
|
DataOutputStream outToServer = new DataOutputStream(getSocket().getOutputStream());
|
|
|
|
ByteBuffer destByteBuffer = ByteBuffer.allocate(data.length + 4);
|
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
destByteBuffer.putInt(command);
|
|
|
|
destByteBuffer.put(data);
|
|
|
|
try {
|
2016-10-23 16:28:05 +02:00
|
|
|
outToServer.write(destByteBuffer.array());
|
2016-10-22 18:52:14 +02:00
|
|
|
} catch (IOException e) {
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("send failed");
|
2016-10-22 18:52:14 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
destByteBuffer.clear();
|
|
|
|
destByteBuffer = ByteBuffer.allocate(0x600);
|
|
|
|
byte[] result = new byte[0x0600];
|
|
|
|
int size = getSocket().getInputStream().read(result);
|
|
|
|
destByteBuffer.put(result, 0, size);
|
|
|
|
int returnValue = destByteBuffer.getInt();
|
2016-10-23 15:34:57 +02:00
|
|
|
return new Result<byte[]>(returnValue,Arrays.copyOfRange(result, 4,result.length));
|
2016-10-22 18:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] read(int addr, int len) throws IOException{
|
|
|
|
ByteBuffer destByteBuffer = ByteBuffer.allocate(8);
|
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
destByteBuffer.putInt(addr);
|
|
|
|
destByteBuffer.putInt(len);
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> result = send(1,destByteBuffer.array());
|
2016-10-22 18:52:14 +02:00
|
|
|
if(result.getResultValue() == 0){
|
|
|
|
return result.getData();
|
|
|
|
}else{
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("Read error: " + result.getResultValue());
|
2016-10-22 18:52:14 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int write(int addr, byte[] data ) throws IOException{
|
|
|
|
ByteBuffer destByteBuffer = ByteBuffer.allocate(4 + data.length);
|
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
destByteBuffer.putInt(addr);
|
|
|
|
destByteBuffer.put(data);
|
|
|
|
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> result = send(0,destByteBuffer.array());
|
2016-10-22 18:52:14 +02:00
|
|
|
if(result.getResultValue() == 0){
|
|
|
|
return result.getResultValue();
|
|
|
|
}else{
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("write error: " + result.getResultValue());
|
2016-10-22 18:52:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int svc(int svc_id, int[] arguments) throws IOException{
|
|
|
|
ByteBuffer destByteBuffer = ByteBuffer.allocate(4 + (arguments.length * 0x04));
|
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
destByteBuffer.putInt(svc_id);
|
|
|
|
for(int i = 0;i<arguments.length;i++){
|
|
|
|
destByteBuffer.putInt(arguments[i]);
|
|
|
|
}
|
|
|
|
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> result = send(2,destByteBuffer.array());
|
2016-10-22 18:52:14 +02:00
|
|
|
if(result.getResultValue() == 0){
|
2016-10-23 16:28:05 +02:00
|
|
|
destByteBuffer = ByteBuffer.allocate(0x04);
|
2016-10-22 18:52:14 +02:00
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
2016-10-23 16:28:05 +02:00
|
|
|
destByteBuffer.put(Arrays.copyOfRange(result.getData(), 0, 0x04));
|
2016-10-22 18:52:14 +02:00
|
|
|
return destByteBuffer.getInt(0);
|
|
|
|
}else{
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("svc error: " + result.getResultValue());
|
2016-10-22 18:52:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int kill() throws IOException{
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> result = send(3,new byte[0]);
|
2016-10-22 18:52:14 +02:00
|
|
|
return result.getResultValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int memcpy(int dest, int source, int len) throws IOException{
|
|
|
|
ByteBuffer destByteBuffer = ByteBuffer.allocate(12);
|
|
|
|
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
destByteBuffer.putInt(dest);
|
|
|
|
destByteBuffer.putInt(source);
|
|
|
|
destByteBuffer.putInt(len);
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> result = send(4,destByteBuffer.array());
|
2016-10-22 18:52:14 +02:00
|
|
|
if(result.getResultValue() == 0){
|
|
|
|
return result.getResultValue();
|
|
|
|
}else{
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("memcpy error: " + result.getResultValue());
|
2016-10-22 18:52:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public int alloc(int size) throws IOException{
|
|
|
|
return alloc(size,0x00);
|
|
|
|
}
|
|
|
|
public int alloc(int size, int align) throws IOException{
|
|
|
|
if(size == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-23 16:28:05 +02:00
|
|
|
if(align == 0){
|
|
|
|
int result = svc(0x27, new int[] {0xCAFF,size});
|
2016-10-22 18:52:14 +02:00
|
|
|
return result;
|
|
|
|
}else{
|
2016-10-23 16:28:05 +02:00
|
|
|
int result = svc(0x28, new int[] {0xCAFF,size,align});
|
2016-10-22 18:52:14 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public int free(int address) throws IOException{
|
|
|
|
if(address == 0){
|
|
|
|
return 0;
|
2016-10-23 16:28:05 +02:00
|
|
|
}
|
|
|
|
return svc(0x29, new int[] {0xCAFF,address});
|
2016-10-22 18:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int load_buffer(byte[] data) throws IOException{
|
|
|
|
return load_buffer(data,0x00);
|
|
|
|
}
|
|
|
|
public int load_buffer(byte[] data,int align) throws IOException{
|
|
|
|
if(data.length == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int address = alloc(data.length, align);
|
|
|
|
write(address, data);
|
|
|
|
return address;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public int load_string(String b) throws IOException{
|
|
|
|
return load_string(b,0x00);
|
|
|
|
}
|
|
|
|
public int load_string(String b,int align) throws IOException{
|
|
|
|
byte[] buffer = b.getBytes();
|
|
|
|
byte[] newBuffer = new byte[buffer.length+1];
|
|
|
|
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
|
|
|
|
newBuffer[buffer.length] = 0;
|
|
|
|
|
|
|
|
int result = load_buffer(newBuffer, align);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int open(String device, int mode) throws IOException{
|
2016-10-23 16:28:05 +02:00
|
|
|
int address = load_string(device);
|
|
|
|
int handle = svc(0x33, new int[] {address,mode});
|
2016-10-22 18:52:14 +02:00
|
|
|
free(address);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
public int close(int handle) throws IOException{
|
2016-10-23 16:28:05 +02:00
|
|
|
return svc(0x34,new int[]{handle});
|
2016-10-22 18:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int get_fsa_handle() throws IOException{
|
|
|
|
if(getFsaHandle()== -1){
|
|
|
|
setFsaHandle(open("/dev/fsa", 0));
|
|
|
|
}
|
|
|
|
return getFsaHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int FSA_Close(int handle) throws IOException{
|
2016-10-23 16:28:05 +02:00
|
|
|
int result = svc(0x34, new int[]{handle});
|
2016-10-22 18:52:14 +02:00
|
|
|
if(result == 0){
|
|
|
|
setFsaHandle(-1);
|
2016-10-23 16:28:05 +02:00
|
|
|
}else{
|
|
|
|
Logger.logErr("FSA_Close: failed");
|
2016-10-22 18:52:14 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-10-23 15:34:57 +02:00
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
private Socket createSocket(String ip) {
|
|
|
|
Socket clientSocket = null;
|
|
|
|
try {
|
|
|
|
clientSocket = new Socket(ip, 1337);
|
|
|
|
} catch (UnknownHostException e) {
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("Unkown Host");
|
2016-10-22 18:52:14 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.logErr("IO Error Host");
|
2016-10-22 18:52:14 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
setSocket(clientSocket);
|
2016-10-23 16:28:05 +02:00
|
|
|
Logger.log("Connected");
|
2016-10-22 18:52:14 +02:00
|
|
|
return clientSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void closeSocket(){
|
|
|
|
Socket socket = getSocket();
|
|
|
|
if(socket!= null){
|
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public String getIP() {
|
|
|
|
return IP;
|
|
|
|
}
|
|
|
|
public void setIP(String iP) {
|
|
|
|
IP = iP;
|
|
|
|
}
|
|
|
|
private int getFsaHandle() {
|
|
|
|
return fsaHandle;
|
|
|
|
}
|
2016-10-23 15:34:57 +02:00
|
|
|
public void setFsaHandle(int fsaHandle) {
|
2016-10-22 18:52:14 +02:00
|
|
|
this.fsaHandle = fsaHandle;
|
|
|
|
}
|
2016-10-23 15:34:57 +02:00
|
|
|
public String getCwd() {
|
2016-10-22 18:52:14 +02:00
|
|
|
return cwd;
|
|
|
|
}
|
|
|
|
private void setCwd(String cwd) {
|
|
|
|
this.cwd = cwd;
|
|
|
|
}
|
|
|
|
public Socket getSocket() {
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
public void setSocket(Socket socket) {
|
|
|
|
this.socket = socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|