Communication rebuild(worked).

This commit is contained in:
dxl
2020-04-15 19:02:35 +08:00
parent ce5bb4e536
commit bb8b60fdcc
11 changed files with 121 additions and 69 deletions
@@ -12,6 +12,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView;
import cn.dxl.com.LocalComBridgeAdapter;
import cn.dxl.common.util.DisplayUtil;
import cn.dxl.common.util.FragmentUtil;
import cn.dxl.common.util.LogUtils;
import cn.rrg.rdv.R;
import cn.rrg.rdv.fragment.base.AppMainDevicesFragment;
import cn.rrg.rdv.fragment.tools.MainSettingsFragment;
@@ -94,6 +95,7 @@ public class AppMain extends BaseActivity {
@Override
protected void onDestroy() {
super.onDestroy();
LocalComBridgeAdapter.getInstance().stop();
LocalComBridgeAdapter.getInstance().stopServer();
LogUtils.d("AppMain结束!");
}
}
@@ -46,7 +46,7 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
LocalComBridgeAdapter.getInstance()
.setInputStream(control.getInput())
.setOutputStream(control.getOutput())
.start();
.startServer();
initViews();
initActions();
@@ -144,7 +144,7 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
control.unregister();
closeClient();
LocalComBridgeAdapter.getInstance()
.stop();
.stopClient();
}
@Override
@@ -28,10 +28,16 @@ public abstract class Proxmark3ConsoleActivity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//重定向!
IORedirector.setStdEO(Paths.PM3_FORWARD_O, IORedirector.STD_OUT);
IORedirector.setStdEO(Paths.PM3_FORWARD_E, IORedirector.STD_ERR);
IORedirector.setStdIN(Paths.COMMON_FORWARD_I);
new Thread(new Runnable() {
@Override
public void run() {
//重定向!
IORedirector.setStdEO(Paths.PM3_FORWARD_O, IORedirector.STD_OUT);
IORedirector.setStdEO(Paths.PM3_FORWARD_E, IORedirector.STD_ERR);
IORedirector.setStdIN(Paths.COMMON_FORWARD_I);
}
}).start();
//父类先初初始化!
super.onCreate(savedInstanceState);
@@ -63,8 +63,13 @@ public class Proxmark3Rdv4RRGRedTeamConsoleActivity
initViews();
initActions();
// 更改工作目录!
IORedirector.chdir(Paths.PM3_DIRECTORY);
new Thread(new Runnable() {
@Override
public void run() {
// 更改工作目录!
IORedirector.chdir(Paths.PM3_DIRECTORY);
}
}).start();
}
private void initViews() {
@@ -17,24 +17,27 @@ public abstract class DeviceChecker implements Serializable, Closeable {
this.communication = communication;
}
private void initCom() {
// Auto init communication to mapper.
LocalComBridgeAdapter.getInstance()
.setInputStream(communication.getInput())
.setOutputStream(communication.getOutput())
.start();
}
/**
* Test a device is can work!
*
* @return if can work return true, if no, return false
*/
public boolean check() throws IOException {
initCom();
return false;
public final boolean check() throws IOException {
// Auto init communication to mapper.
LocalComBridgeAdapter.getInstance()
.setInputStream(communication.getInput())
.setOutputStream(communication.getOutput())
.startServer();
if (!checkDevice()) {
// if check failed, we must to close device!
LocalComBridgeAdapter.getInstance().stopClient();
return false;
}
return true;
}
protected abstract boolean checkDevice() throws IOException;
/**
* Close the device(Should no close communication. only close device!)
*/
@@ -25,9 +25,9 @@ public final class LocalComBridgeAdapter implements Serializable {
* */
// The namespace of the LocalServerSocket
public static final String NAMESPACE = "CN.DXL.ComBridgeAdapter.2020_0413";
public static final String NAMESPACE = "LocalComBridgeAdapter";
// The tag of the log.
public static final String LOG_TAG = "ComBridgeAdapter";
public static final String LOG_TAG = "LocalComBridgeAdapter";
// 本地套接字服务!
private LocalServerSocket serverSocket;
// 单例!
@@ -38,8 +38,12 @@ public final class LocalComBridgeAdapter implements Serializable {
private OutputStream mOutputStream;
// 是否已经连接!
private volatile boolean isHasClient = false;
// 是否关闭监听!
private volatile boolean listenAccept = false;
// 暂停转发!
private boolean pause = false;
// 连接到转发服务的客户端!
private LocalSocket socket;
private LocalComBridgeAdapter() {
// No instantiation is required
@@ -52,20 +56,30 @@ public final class LocalComBridgeAdapter implements Serializable {
private class WorkThread extends Thread {
@Override
public void run() {
while (true) {
while (listenAccept) {
try {
LocalSocket socket = serverSocket.accept();
if (isHasClient) {
Log.e(LOG_TAG, "The server only once online supported.");
Log.e(LOG_TAG, "please disconnect your previous con.");
continue;
if (serverSocket != null) {
socket = serverSocket.accept();
if (isHasClient) {
isHasClient = false;
Log.e(LOG_TAG, "The server only once online supported.");
Log.e(LOG_TAG, "please disconnect your previous con.");
continue;
}
isHasClient = true;
Thread socket2Device = new DataThread(socket, mOutputStream, socket.getInputStream());
Thread device2Socket = new DataThread(socket, socket.getOutputStream(), mInputStream);
// start task!
device2Socket.start();
socket2Device.start();
} else {
return;
}
isHasClient = true;
new DataThread(socket, mOutputStream, socket.getInputStream()).start();
new DataThread(socket, socket.getOutputStream(), mInputStream).start();
} catch (IOException e) {
e.printStackTrace();
isHasClient = false;
listenAccept = false;
Log.d(LOG_TAG, "链接线程终止!");
break;
}
}
@@ -73,10 +87,10 @@ public final class LocalComBridgeAdapter implements Serializable {
}
class DataThread extends Thread {
private LocalSocket socket;
private OutputStream os;
private InputStream is;
private byte[] buffer = new byte[1024];
private byte[] buffer = new byte[1024 * 500];
private LocalSocket socket;
DataThread(LocalSocket socket, OutputStream os, InputStream is) {
this.os = os;
@@ -88,15 +102,17 @@ public final class LocalComBridgeAdapter implements Serializable {
public void run() {
while (isHasClient) {
try {
if (socket.getFileDescriptor() == null) {
throw new IOException("Socket disconnected!");
}
if (pause) {
Log.e(LOG_TAG, "pausing");
continue;
}
if (os != null && is != null) {
int len = is.read(buffer);
if (len != -1) {
if (len > 0) {
os.write(Arrays.copyOf(buffer, len));
os.flush();
}
} else {
throw new IOException("IO closed.");
@@ -104,12 +120,6 @@ public final class LocalComBridgeAdapter implements Serializable {
} catch (IOException e) {
e.printStackTrace();
isHasClient = false;
try {
socket.shutdownInput();
socket.shutdownOutput();
} catch (IOException ex) {
ex.printStackTrace();
}
break;
}
}
@@ -118,8 +128,9 @@ public final class LocalComBridgeAdapter implements Serializable {
}
@Override
protected void finalize() throws Throwable {
stop();
protected void finalize() {
stopClient();
stopServer();
}
public static LocalComBridgeAdapter getInstance() {
@@ -149,27 +160,48 @@ public final class LocalComBridgeAdapter implements Serializable {
Log.d(LOG_TAG, "ComBridgeAdapter pause!");
}
public LocalComBridgeAdapter start() {
public LocalComBridgeAdapter startServer() {
synchronized (LocalComBridgeAdapter.class) {
try {
if (serverSocket != null) stop();
serverSocket = new LocalServerSocket(NAMESPACE);
new WorkThread().start();
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, "If you see an error message like \"Address already in use\", check that you call the stop function");
Log.e(LOG_TAG, "如果你看到了类似Address already in use的错误消息,请检查你是否调用停止函数");
if (!listenAccept) {
listenAccept = true;
try {
if (serverSocket == null) {
serverSocket = new LocalServerSocket(NAMESPACE);
}
new WorkThread().start();
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, "If you see an error message like \"Address already in use\", check that you call the stopServer function");
Log.e(LOG_TAG, "如果你看到了类似Address already in use的错误消息,请检查你是否调用停止函数");
}
Log.d(LOG_TAG, "ComBridgeAdapter start!");
}
}
Log.d(LOG_TAG, "ComBridgeAdapter start!");
return this;
}
public void stop() {
public void stopServer() {
listenAccept = false;
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
serverSocket = null;
}
}
public void stopClient() {
isHasClient = false;
try {
if (serverSocket != null)
serverSocket.close();
if (socket != null) {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
socket = null;
}
} catch (IOException e) {
e.printStackTrace();
}
@@ -11,6 +11,8 @@ import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.util.Log;
import com.felhr.usbserial.SerialInputStream;
import com.felhr.usbserial.SerialOutputStream;
import com.felhr.usbserial.UsbSerialDevice;
import java.io.IOException;
@@ -163,15 +165,21 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
@Override
public OutputStream getOutput() {
if (mPort != null)
return mPort.getOutputStream();
if (mPort != null) {
SerialOutputStream os = mPort.getOutputStream();
os.setTimeout(0);
return os;
}
return null;
}
@Override
public InputStream getInput() {
if (mPort != null)
return mPort.getInputStream();
if (mPort != null) {
SerialInputStream is = mPort.getInputStream();
is.setTimeout(0);
return is;
}
return null;
}
@@ -12,10 +12,11 @@ public class EmptyDeivce extends DeviceChecker {
}
@Override
public boolean check() throws IOException {
protected boolean checkDevice() throws IOException {
return true;
}
@Override
public void close() throws IOException {
}
@@ -32,8 +32,7 @@ public class PN53X extends DeviceChecker {
}
@Override
public boolean check() throws IOException {
super.check();
public boolean checkDevice() throws IOException {
return testPN53x(name);
}
@@ -6,7 +6,7 @@ import cn.dxl.com.Communication;
import cn.dxl.com.DeviceChecker;
public class Proxmark3RRGRdv4 extends DeviceChecker {
static {
System.loadLibrary("pm3rrg_rdv4");
}
@@ -16,8 +16,7 @@ public class Proxmark3RRGRdv4 extends DeviceChecker {
}
@Override
public boolean check() throws IOException {
super.check();
public boolean checkDevice() throws IOException {
return testPm3();
}
@@ -37,18 +37,15 @@ public class LanguageUtil {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
switch (language) {
case "zh":
Log.d("setAppLanguage()", "配置为中文!");
configuration.setLocale(Locale.SIMPLIFIED_CHINESE);
break;
case "en":
default:
Log.d("setAppLanguage()", "配置为英语或跟随系统!");
configuration.setLocale(Locale.ENGLISH);
break;
}
return AppUtil.getInstance().getApp().createConfigurationContext(configuration);
} else {
Log.d("updateConfiguration()", "配置为: " + language);
context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
return context;
}