package cn.com.magnity.magnitycx.sdk; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbEndpoint; import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbManager; import android.os.Handler; import android.os.Message; import android.support.v4.media.TransportMediator; import android.support.v7.widget.helper.ItemTouchHelper; import cn.com.magnity.magnitycx.ImageViewer; import cn.com.magnity.magnitycx.MagApplication; import cn.com.magnity.magnitycx.R; import cn.com.magnity.magnitycx.log.Logging; import cn.com.magnity.magnitycx.sdk.DeviceController; import com.baidu.mobstat.Config; import com.baidu.mobstat.autotrace.Common; import java.io.File; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.apache.log4j.Priority; /* loaded from: classes.dex */ public class UsbCommunication implements DeviceController.ShutterCallBack { private static final int DOWNLOAD_CALI_TIMEOUT = 5000; private static final int HEADER_SIZE = 28; private static final int IMG_END_CODE = 464630044; private static final int IMG_START_CODE = 464630043; private static final int MAX_CALI_FILE_LEN = 104857600; private static final int MAX_RECEIVED_LEN = 16384; private static final int MIN_CALI_FILE_LEN = 65536; public static final String MSG_SET_BASEPARAMETER = "set_baseparameter"; public static final String MSG_TRIGGER_FFC = "trigger_ffc"; public static final int PROGRESS_CALI_ID = 0; private static final int PROGRESS_CANCEL = 3; private static final int PROGRESS_CHANGED = 2; private static final int PROGRESS_FINISHED = 1; private static final int PROGRESS_START = 0; public static final int PROGRESS_WAIT_FOR_CORRECT_STREAM_ID = 1; private static final int QUERY_DEVICE_LIFETIME = 4; private static final int TAIL_SIZE = 28; private static final int TIMEOUT = 800; private static final int TIME_FOR_DEVICE_INIT = 15000; private static volatile Lock lock_ = new ReentrantLock(); private static volatile long[] ticks_ = new long[20]; private volatile int camTemp_; private UsbConnection conn_; private volatile int fpaTemp_; private byte[] imgExchangeBuf1_; private ImageViewer imgViewer_; private volatile boolean isExitThreadImgProcess_; private IProgressCallback progressCallback_; private ThreadCaliRecv threadCaliRecv_; private ThreadImgProcess threadImgProcess_; private ThreadImgRecv threadImgRecv_; private UsbDeviceConnection usbConn_; private UsbEndpoint usbEpCaliIn_; private UsbEndpoint usbEpCaliOut_; private UsbEndpoint usbEpCmdIn_; private UsbEndpoint usbEpCmdOut_; private UsbEndpoint usbEpImgIn_; private UsbInterface usbInterface_; private volatile boolean connected_ = false; private volatile boolean isExitCaliRecvThread = false; private WaitCondition condImgProcess_ = new WaitCondition(); private volatile boolean isToRecvFirstCaliPacket_ = false; private String caliPathName_ = ""; private Handler handler_ = new Handler() { // from class: cn.com.magnity.magnitycx.sdk.UsbCommunication.1 @Override // android.os.Handler public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: if (UsbCommunication.this.progressCallback_ != null) { UsbCommunication.this.progressCallback_.progressStart(((Integer) msg.obj).intValue()); break; } break; case 1: if (UsbCommunication.this.progressCallback_ != null) { UsbCommunication.this.progressCallback_.progressFinished(((Integer) msg.obj).intValue()); break; } break; case 2: if (UsbCommunication.this.progressCallback_ != null) { UsbCommunication.this.progressCallback_.progressChanged(msg.arg1, msg.arg2); break; } break; case 3: if (UsbCommunication.this.progressCallback_ != null) { UsbCommunication.this.progressCallback_.progressCancel(((Integer) msg.obj).intValue()); break; } break; case 4: int t = UsbCommunication.this.getDevLifeTime(); if (t != -1) { if (MagApplication.magParameter.basePara1.devType == 0 || MagApplication.magParameter.basePara1.devType == 5 || MagApplication.magParameter.basePara1.devType == 6 || MagApplication.magParameter.basePara1.devType == 3) { if (t > 5000) { if (UsbCommunication.this.imgViewer_.getImageStableCounter() == 4) { } UsbCommunication.this.imgViewer_.setImageStableCounter(0); } else { UsbCommunication.this.imgViewer_.setImageStableCounter(4); } } else if (MagApplication.magParameter.basePara1.devType == 1 || MagApplication.magParameter.basePara1.devType == 2) { if (t > 26000) { if (UsbCommunication.this.imgViewer_.getImageStableCounter() == 1) { DeviceController.Lock(); DeviceController.triggerFFC(); DeviceController.Unlock(); } UsbCommunication.this.imgViewer_.setImageStableCounter(0); } else if (t > 19500) { if (UsbCommunication.this.imgViewer_.getImageStableCounter() == 2) { DeviceController.Lock(); DeviceController.triggerFFC(); DeviceController.Unlock(); } UsbCommunication.this.imgViewer_.setImageStableCounter(1); } else if (t > 13000) { if (UsbCommunication.this.imgViewer_.getImageStableCounter() == 3) { DeviceController.Lock(); DeviceController.triggerFFC(); DeviceController.Unlock(); } UsbCommunication.this.imgViewer_.setImageStableCounter(2); } else if (t > 6500) { if (UsbCommunication.this.imgViewer_.getImageStableCounter() == 4) { DeviceController.Lock(); DeviceController.triggerFFC(); DeviceController.Unlock(); } UsbCommunication.this.imgViewer_.setImageStableCounter(3); } else { UsbCommunication.this.imgViewer_.setImageStableCounter(4); } } if (t > 40000) { UsbCommunication.this.imgViewer_.setTempStableFlag(true); break; } else { UsbCommunication.this.imgViewer_.setTempStableFlag(false); break; } } break; } } }; private BroadcastReceiver mReceiver = new BroadcastReceiver() { // from class: cn.com.magnity.magnitycx.sdk.UsbCommunication.2 @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case "set_baseparameter": UsbCommunication.this.setFrameRate(); DeviceController.triggerFFC(); break; case "trigger_ffc": DeviceController.triggerFFC(); break; } } }; public interface DelegateNewFrame { void onNewFrameReceived(int i, int i2); } public interface IProgressCallback { void progressCancel(int i); void progressChanged(int i, int i2); void progressFinished(int i); void progressStart(int i); } /* JADX INFO: Access modifiers changed from: private */ public int getDevLifeTime() { int i = -1; if (sendEmptyCmd(P2DCmd.P2D_GetLifeTime, TIMEOUT) < 0) { Logging.error("Fail to get device lifetime"); } byte[] data = new byte[64]; int len = recvCmd(data, 0, data.length, TIMEOUT); if (len < 0) { Logging.error("Fail to get lifetime ack"); } else if (len < 4) { Logging.error("Get wrong ack size(" + len + ")"); } else if (len < 8) { Logging.error("Get wrong ack size(" + len + ", cmd: " + GlobalFunc.byteArrayToInt(data) + ")"); } else { ByteBuffer bb = ByteBuffer.wrap(data, 0, len); bb.order(ByteOrder.LITTLE_ENDIAN); try { if (bb.getInt() == 1538635105) { i = bb.getInt(); } else { Logging.error("Get wrong cmd(expected is lifetime)"); } } catch (IndexOutOfBoundsException e) { Logging.error("Exception of get liftime"); } } return i; } public void setCallbackReceiver(IProgressCallback cb) { this.progressCallback_ = cb; } public UsbCommunication(UsbConnection conn, UsbManager usbMgr, UsbDevice device, IProgressCallback cb) throws Exception { this.conn_ = conn; this.progressCallback_ = cb; this.usbConn_ = usbMgr.openDevice(device); if (this.usbConn_ == null) { throw new Exception("Unable to open USB device"); } if (device.getInterfaceCount() < 1) { throw new Exception("Unable to get USB interface"); } this.usbInterface_ = device.getInterface(0); if (!this.usbConn_.claimInterface(this.usbInterface_, true)) { throw new Exception("Unable to claim USB interface"); } if (this.usbInterface_.getEndpointCount() < 4) { throw new Exception("Unable to get USB endpoint (" + this.usbInterface_.getEndpointCount() + ")"); } for (int i = this.usbInterface_.getEndpointCount() - 1; i != -1; i--) { UsbEndpoint ep = this.usbInterface_.getEndpoint(i); switch (ep.getAddress()) { case 3: this.usbEpCmdOut_ = ep; break; case 5: this.usbEpCaliOut_ = ep; break; case 129: this.usbEpImgIn_ = ep; break; case TransportMediator.KEYCODE_MEDIA_RECORD /* 130 */: this.usbEpCmdIn_ = ep; break; case 132: this.usbEpCaliIn_ = ep; break; default: Logging.warn("Unknown endpoint found."); break; } } if (this.usbEpCmdIn_ == null || this.usbEpCmdOut_ == null || this.usbEpCaliIn_ == null || this.usbEpCaliOut_ == null || this.usbEpImgIn_ == null) { Logging.warn("Some endpoint lost."); } } public void setImageViewer(ImageViewer imageViewer) { this.imgViewer_ = imageViewer; } public int connect() { if (this.connected_) { return 1; } MsgBus.getInstance().register(this.mReceiver, new String[]{MSG_SET_BASEPARAMETER, MSG_TRIGGER_FFC}); byte[] data = new byte[64]; if (sendEmptyCmd(P2DCmd.P2D_GetParameter1, TIMEOUT) < 0) { Logging.error("Fail to get parameter1"); return -1; } int len = recvCmd(data, 0, data.length, TIMEOUT); if (len > 0) { if (parseCmd(data, len) != 1) { return -1; } if (MagApplication.magParameter.basePara1.devType != 0 && MagApplication.magParameter.basePara1.devType != 5 && MagApplication.magParameter.basePara1.devType != 6 && MagApplication.magParameter.basePara1.devType != 1 && MagApplication.magParameter.basePara1.devType != 2 && MagApplication.magParameter.basePara1.devType != 3) { Logging.error("unknown device type " + MagApplication.magParameter.basePara1.devType); return -1; } if (sendEmptyCmd(P2DCmd.P2D_GetParameter2, TIMEOUT) < 0) { Logging.error("Fail to get parameter2"); return -1; } int len2 = recvCmd(data, 0, data.length, TIMEOUT); if (len2 > 0) { if (parseCmd(data, len2) != 1) { return -1; } if (sendEmptyCmd(P2DCmd.P2D_GetCaliInfo, TIMEOUT) < 0) { Logging.error("Fail to get remote file info"); return -1; } int len3 = recvCmd(data, 0, data.length, TIMEOUT); if (len3 > 0) { int ret = parseCmd(data, len3); if (ret == 1) { return !startTransfer(this.caliPathName_) ? -1 : 1; } return ret; } Logging.error("Fail to get remote file info"); return -1; } Logging.error("Fail to get parameter2 ack"); return -1; } Logging.error("Fail to get parameter1 ack"); return -1; } /* JADX INFO: Access modifiers changed from: private */ public boolean startTransfer(String caliPathName) { BasePara1 para1 = MagApplication.magParameter.basePara1; BasePara2 para2 = MagApplication.magParameter.basePara2; if (!DeviceController.startProcess(this, para1.fpaWidth, para1.fpaHeight, para1.serialNumber, para1.devType, para2.shutterCloseSpeed, para2.shutterOpenSpeed, para2.ffcTriggerFrame, para2.ffcTriggerTemperature, SharedPreferencesManager.getInt("paletteIndex", 2), para2.baseLineAcc, caliPathName)) { Logging.error("SDK start fail"); try { File f = new File(caliPathName); f.delete(); return false; } catch (Exception e) { return false; } } DeviceController.setEX(MagApplication.magParameter.imageEX, MagApplication.magParameter.basePara1.fpaWidth / 2, MagApplication.magParameter.basePara1.fpaHeight / 2); ExtPara param = new ExtPara(); DeviceController.Lock(); DeviceController.getExtParameter(param); param.dwFlip = MagApplication.magParameter.imageFlip; DeviceController.setExtParameter(param); DeviceController.setAutoEnlarge(para2.enlargeRange); DeviceController.Unlock(); Logging.info("****Start****"); this.connected_ = true; this.threadImgRecv_ = new ThreadImgRecv(); this.threadImgRecv_.start(); this.threadImgProcess_ = new ThreadImgProcess(); this.isExitThreadImgProcess_ = false; this.condImgProcess_.notified = false; this.threadImgProcess_.start(); try { Thread.sleep(5L); } catch (InterruptedException e2) { } if (sendEmptyCmd(P2DCmd.P2D_StartTransferImg, Common.EDIT_SNAPSHOT_INTERVAL) < 0) { Logging.error("Fail to start remote transferring"); DeviceController.stopProcess(); return false; } if (para1.devType == 1) { MagApplication.magParameter.isTraceRoi = false; MagApplication.magParameter.isShowCross = false; MagApplication.magParameter.isTraceMaxMin = true; MagApplication.magParameter.maxMinTrace = -1; MagApplication.magParameter.emissivity = 100; DeviceController.Lock(); DeviceController.setEmissivity(100, Priority.INFO_INT); DeviceController.Unlock(); } else if (para1.devType == 0 || para1.devType == 5 || para1.devType == 6 || para1.devType == 3) { MagApplication.magParameter.isTraceRoi = false; MagApplication.magParameter.isShowMovingCross = false; MagApplication.magParameter.emissivity = 100; DeviceController.Lock(); DeviceController.setEmissivity(100, Priority.INFO_INT); DeviceController.Unlock(); } else if (para1.devType == 2) { MagApplication.magParameter.isShowMovingCross = false; MagApplication.magParameter.isTraceMaxMin = true; } if (para1.devType == 1 || para1.devType == 2) { if (MagApplication.magParameter.fpsMode == 0) { MagApplication.magParameter.basePara1.interFrame = 9; MagApplication.magParameter.basePara1.interLine = 12; } else { MagApplication.magParameter.basePara1.interFrame = 105; MagApplication.magParameter.basePara1.interLine = 105; } setFrameRate(); } return true; } private void stopTransfer() { if (sendEmptyCmd(P2DCmd.P2D_StopTransferImg, TIMEOUT) < 0) { Logging.error("Fail to stop remote transferring"); } this.connected_ = false; try { if (this.threadImgRecv_ != null) { this.threadImgRecv_.join(); this.threadImgRecv_ = null; } } catch (InterruptedException e) { this.threadImgRecv_ = null; Logging.trace("Thread join interrupted1"); } this.isExitThreadImgProcess_ = true; synchronized (this.condImgProcess_.cond) { this.condImgProcess_.notified = true; this.condImgProcess_.cond.notify(); } try { if (this.threadImgProcess_ != null) { this.threadImgProcess_.join(); this.threadImgProcess_ = null; } } catch (InterruptedException e2) { this.condImgProcess_ = null; Logging.trace("Thread join interrupted2"); } DeviceController.stopProcess(); Logging.trace("****Stop****"); } public void disconnect() { MsgBus.getInstance().unregister(this.mReceiver); setLaserState(0); MagApplication.magParameter.isLaserOn = false; stopTransfer(); this.conn_ = null; if (this.usbConn_ != null) { if (this.usbInterface_ != null) { this.usbConn_.releaseInterface(this.usbInterface_); } this.usbConn_.close(); this.usbConn_ = null; } this.usbEpImgIn_ = null; this.usbEpCaliOut_ = null; this.usbEpCaliIn_ = null; this.usbEpCmdOut_ = null; this.usbEpCmdIn_ = null; stopCaliRecvThread(); } @Override // cn.com.magnity.magnitycx.sdk.DeviceController.ShutterCallBack public void setShutterState(int on) { setShutterState_(on); } private void startCaliRecvThread() { if (this.threadCaliRecv_ == null) { this.threadCaliRecv_ = new ThreadCaliRecv(); this.isExitCaliRecvThread = false; this.threadCaliRecv_.start(); } } private void stopCaliRecvThread() { try { if (this.threadCaliRecv_ != null) { this.isExitCaliRecvThread = true; this.threadCaliRecv_.join(); this.threadCaliRecv_ = null; } } catch (InterruptedException e) { this.threadCaliRecv_ = null; Logging.trace("Thread join interrupted."); } } private void setShutterState_(int on) { byte[] buf = {(byte) P2DCmd.P2D_SetShutterState, (byte) 7059126, (byte) 27574, (byte) 107, (byte) on, (byte) (on >> 8), (byte) (on >> 16), (byte) (on >> 24)}; sendCmd(buf, 0, buf.length, TIMEOUT); } public boolean setLaserState(int on) { byte[] buf = {(byte) P2DCmd.P2D_SetLaserState, (byte) 7059126, (byte) 27574, (byte) 107, (byte) on, (byte) (on >> 8), (byte) (on >> 16), (byte) (on >> 24)}; return sendCmd(buf, 0, buf.length, TIMEOUT) >= 0; } /* JADX INFO: Access modifiers changed from: private */ public boolean setFrameRate() { int interFrame; int interLine; byte[] buf = new byte[12]; if (MagApplication.magParameter.fpsMode == 0) { interFrame = 9; interLine = 12; } else { interFrame = 105; interLine = 105; } buf[0] = (byte) P2DCmd.P2D_SetFrameRate; buf[1] = (byte) 7059126; buf[2] = (byte) 27574; buf[3] = (byte) 107; buf[4] = (byte) interFrame; buf[5] = (byte) (interFrame >> 8); buf[6] = (byte) (interFrame >> 16); buf[7] = (byte) (interFrame >> 24); buf[8] = (byte) interLine; buf[9] = (byte) (interLine >> 8); buf[10] = (byte) (interLine >> 16); buf[11] = (byte) (interLine >> 24); return sendCmd(buf, 0, buf.length, TIMEOUT) >= 0; } private boolean waitForCmdAck(int cmd, int timeout) { switch (cmd) { case P2DCmd.P2D_SetParameter1 /* 1807136365 */: case P2DCmd.P2D_SetParameter2 /* 1807136366 */: case P2DCmd.P2D_GetCaliFile /* 1807136368 */: case P2DCmd.P2D_SendCaliFile /* 1807136369 */: case P2DCmd.P2D_SetShutterState /* 1807136370 */: case P2DCmd.P2D_StartTransferImg /* 1807136371 */: case P2DCmd.P2D_StopTransferImg /* 1807136372 */: case P2DCmd.P2D_SetLaserState /* 1807136374 */: case P2DCmd.P2D_SetFrameRate /* 1807136377 */: byte[] buf = new byte[64]; int ret = this.usbConn_.bulkTransfer(this.usbEpCmdIn_, buf, 0, buf.length, timeout); if (ret < 0) { Logging.trace("Fail to get command ack - " + Integer.toHexString(cmd)); return false; } case P2DCmd.P2D_GetCaliInfo /* 1807136367 */: case P2DCmd.P2D_GetLifeTime /* 1807136373 */: case P2DCmd.P2D_PowerSave /* 1807136375 */: case P2DCmd.P2D_GetFPAState /* 1807136376 */: default: return true; } } public synchronized int sendCmd(byte[] data, int offset, int len, int timeout) { int ret; ret = this.usbConn_.bulkTransfer(this.usbEpCmdOut_, data, offset, len, timeout); if (ret < 0) { ret = -1; } else if (!waitForCmdAck(GlobalFunc.byteArrayToInt(data), timeout)) { ret = -1; } return ret; } public synchronized int sendEmptyCmd(int data, int timeout) { int ret; ret = this.usbConn_.bulkTransfer(this.usbEpCmdOut_, GlobalFunc.intToByteArray(data), 0, 4, timeout); if (ret < 0) { ret = -1; } else if (!waitForCmdAck(data, timeout)) { ret = -1; } return ret; } public synchronized int recvCmd(byte[] buffer, int offset, int len, int timeout) { return this.usbConn_.bulkTransfer(this.usbEpCmdIn_, buffer, offset, len, timeout); } public int sendCali(byte[] data, int offset, int len, int timeout) { int ret = this.usbConn_.bulkTransfer(this.usbEpCaliOut_, data, offset, len, timeout); if (ret >= 0 && waitForCmdAck(GlobalFunc.byteArrayToInt(data), timeout)) { return ret; } return -1; } public synchronized int sendEmptyCali(int data, int timeout) { int ret; ret = this.usbConn_.bulkTransfer(this.usbEpCaliOut_, GlobalFunc.intToByteArray(data), 0, 4, timeout); if (ret < 0) { ret = -1; } else if (!waitForCmdAck(data, timeout)) { ret = -1; } return ret; } public int recvCali(byte[] buffer, int offset, int len, int timeout) { return this.usbConn_.bulkTransfer(this.usbEpCaliIn_, buffer, offset, len, timeout); } public int recvImg(byte[] buffer, int offset, int len, int timeout) { return this.usbConn_.bulkTransfer(this.usbEpImgIn_, buffer, offset, len, timeout); } public int getCmdInAddress() { return this.usbEpCmdIn_.getAddress(); } public int getCaliInAddress() { return this.usbEpCaliIn_.getAddress(); } public int getImgInAddress() { return this.usbEpImgIn_.getAddress(); } private int parseCmd(byte[] data, int len) { String productType; if (len < 4) { return -1; } ByteBuffer bb = ByteBuffer.wrap(data, 0, len); bb.order(ByteOrder.LITTLE_ENDIAN); int cmd = bb.getInt(); switch (cmd) { case D2PCmd.D2P_SendParameter1 /* 1538635099 */: Logging.trace("D2P_SendParameter1"); BasePara1 para1 = MagApplication.magParameter.basePara1; try { para1.serialNumber = bb.getInt(); int val = bb.getInt(); para1.hwVersion = 16777215 & val; para1.devType = (val >> 24) & 255; para1.swVersion = bb.getInt(); para1.reserved1 = bb.getInt(); para1.fpaWidth = bb.getInt(); para1.fpaHeight = bb.getInt(); para1.fps = bb.getInt(); para1.reserved2 = bb.getInt(); para1.fpaGain = bb.getInt(); para1.fpaFlip = bb.getInt(); para1.interFrame = bb.getInt(); para1.interLine = bb.getInt(); para1.gfid = bb.getInt(); para1.gsk = bb.getInt(); if (bb.hasRemaining()) { Logging.warn("D2PCmd.D2P_SendParameter1 cmd size is too long"); return -1; } Logging.trace("Serial number: " + para1.serialNumber); return 1; } catch (IndexOutOfBoundsException e) { para1.reset(); Logging.error("D2PCmd.D2P_SendParameter1 cmd size is not enough"); return -1; } case D2PCmd.D2P_SendParameter2 /* 1538635100 */: Logging.trace("D2P_SendParameter2"); BasePara2 para2 = MagApplication.magParameter.basePara2; try { para2.baseLineAcc = bb.getInt(); para2.denoiseLevel = bb.getInt(); para2.reserved1 = bb.getInt(); para2.reserved2 = bb.getShort(); para2.fpaTempFix = bb.getShort(); para2.shutterCloseSpeed = bb.getInt(); para2.shutterOpenSpeed = bb.getInt(); para2.ffcTriggerFrame = bb.getInt(); para2.ffcTriggerTemperature = bb.getInt(); para2.enlargeRange = bb.getInt(); para2.laserPos = bb.getInt(); para2.ATZeroErrorPoint = bb.getInt(); para2.ATErrorSlope = bb.getFloat(); para2.reserved5 = bb.getInt(); para2.reserved6 = bb.getInt(); if (bb.hasRemaining()) { Logging.warn("D2PCmd.D2P_SendParameter2 cmd size is too long"); return -1; } return 1; } catch (IndexOutOfBoundsException e2) { para2.reset(); Logging.error("D2PCmd.D2P_SendParameter2 cmd size is not enough"); return -1; } case D2PCmd.D2P_SendCaliFile /* 1538635101 */: Logging.trace("D2P_SendRemoteFile"); this.isToRecvFirstCaliPacket_ = true; return 1; case D2PCmd.D2P_SendCaliInfo /* 1538635102 */: Logging.trace("D2P_SendRemoteFileInfo"); CaliInfo caliInfo = MagApplication.magParameter.caliInfo; try { caliInfo.size = bb.getInt(); caliInfo.reserved = bb.getInt(); caliInfo.date = bb.getLong(); if (bb.hasRemaining()) { Logging.warn("D2PCmd.D2P_SendRemoteFileInfo cmd size is too long"); return -1; } Logging.trace("Remote file info: " + caliInfo.size + ", " + caliInfo.date); if (MagApplication.magParameter.caliInfo.size < 65536 || MagApplication.magParameter.caliInfo.size > MAX_CALI_FILE_LEN) { this.caliPathName_ = ""; Logging.error("remoteFileInfo error(invalid size)"); return -1; } if (MagApplication.magParameter.caliDir != null) { switch (MagApplication.magParameter.basePara1.devType) { case 0: productType = MagApplication.getInstance().getResources().getString(R.string.c1); break; case 1: productType = MagApplication.getInstance().getResources().getString(R.string.c3); break; case 2: productType = MagApplication.getInstance().getResources().getString(R.string.c3p); break; case 3: productType = MagApplication.getInstance().getResources().getString(R.string.core160); break; case 4: default: productType = MagApplication.getInstance().getResources().getString(R.string.unknownProductType); break; case 5: productType = MagApplication.getInstance().getResources().getString(R.string.c1pro); break; case 6: productType = MagApplication.getInstance().getResources().getString(R.string.c1prolite); break; } this.caliPathName_ = MagApplication.magParameter.caliDir + File.separator + productType + "." + MagApplication.magParameter.basePara1.serialNumber + "." + MagApplication.magParameter.caliInfo.date; File dir = new File(MagApplication.magParameter.caliDir); File file = new File(this.caliPathName_); if (!file.exists() || file.length() != MagApplication.magParameter.caliInfo.size) { File[] files = dir.listFiles(); for (File f : files) { if (f.getName().contains(MagApplication.magParameter.basePara1.serialNumber + ".")) { f.delete(); } } stopCaliRecvThread(); startCaliRecvThread(); try { Thread.sleep(50L); } catch (InterruptedException e3) { } sendEmptyCmd(P2DCmd.P2D_GetCaliFile, TIMEOUT); return 0; } } return 1; } catch (IndexOutOfBoundsException e4) { caliInfo.reset(); Logging.error("D2PCmd.D2P_SendRemoteFileInfo cmd size is not enough"); return -1; } default: Logging.warn("Unknown command(" + cmd + ")"); return -1; } } private class ThreadImgRecv extends Thread { private ThreadImgRecv() { } @Override // java.lang.Thread, java.lang.Runnable public void run() { int drop; int frameBytes = ((MagApplication.magParameter.basePara1.fpaWidth * MagApplication.magParameter.basePara1.fpaHeight) * 16) / 8; int imgRecvBufLength = frameBytes + 1024; if (UsbCommunication.this.imgExchangeBuf1_ == null || UsbCommunication.this.imgExchangeBuf1_.length < imgRecvBufLength) { UsbCommunication.this.imgExchangeBuf1_ = new byte[imgRecvBufLength]; } Logging.trace("Enter ThreadImgReceiver"); byte[] recvbuf = new byte[imgRecvBufLength]; boolean headerFound = false; int total = 0; int index = 0; long t1 = 0; while (UsbCommunication.this.connected_) { int len = UsbCommunication.this.recvImg(recvbuf, total, recvbuf.length - total, ItemTouchHelper.Callback.DEFAULT_DRAG_ANIMATION_DURATION); if (len > 0) { if (!headerFound) { if (len >= 28) { ByteBuffer bb = ByteBuffer.wrap(recvbuf, 0, 28); bb.order(ByteOrder.LITTLE_ENDIAN); if (bb.getInt() == UsbCommunication.IMG_START_CODE) { headerFound = true; } } total = 0; } else if (len >= 28) { ByteBuffer bb2 = ByteBuffer.wrap(recvbuf, (total + len) - 28, 28); bb2.order(ByteOrder.LITTLE_ENDIAN); if (bb2.getInt() == UsbCommunication.IMG_END_CODE) { if ((total + len) - 28 == frameBytes) { int frameIndex = bb2.getInt(); long t = System.currentTimeMillis(); synchronized (UsbCommunication.this.condImgProcess_.cond) { System.arraycopy(recvbuf, 0, UsbCommunication.this.imgExchangeBuf1_, 0, recvbuf.length); if (MagApplication.magParameter.basePara1.devType == 0 || MagApplication.magParameter.basePara1.devType == 5 || MagApplication.magParameter.basePara1.devType == 6 || MagApplication.magParameter.basePara1.devType == 3) { UsbCommunication.this.fpaTemp_ = bb2.getInt(); UsbCommunication.this.camTemp_ = UsbCommunication.this.fpaTemp_ - 500; drop = bb2.getInt(); } else { bb2.getInt(); drop = bb2.getInt(); UsbCommunication.this.camTemp_ = bb2.getInt(); UsbCommunication.this.fpaTemp_ = UsbCommunication.this.camTemp_ + 500; } if (drop == 0 || drop == 1) { UsbCommunication.this.condImgProcess_.notified = true; UsbCommunication.this.condImgProcess_.cond.notify(); } } if (UsbCommunication.this.imgViewer_ != null && (index == 0 || !UsbCommunication.this.imgViewer_.getTempStableFlag() || UsbCommunication.this.imgViewer_.getImageStableCounter() != 0)) { int index2 = index + 1; if ((index & 15) == 0) { UsbCommunication.this.handler_.sendEmptyMessage(4); } index = index2; } if (MagApplication.isShowFps) { if (t1 != 0) { UsbCommunication.lock_.lock(); UsbCommunication.ticks_[frameIndex % UsbCommunication.ticks_.length] = t - t1; UsbCommunication.lock_.unlock(); } t1 = t; } } headerFound = false; total = 0; } else { total += len; } } else { total += len; } if (total > frameBytes) { headerFound = false; total = 0; } } } Logging.trace("Leave ThreadImgReceiver"); } } public static int getReceivedFps() { long[] ticks = new long[ticks_.length]; lock_.lock(); System.arraycopy(ticks_, 0, ticks, 0, ticks_.length); lock_.unlock(); int sum = 0; for (int i = ticks.length - 1; i != -1; i--) { sum = (int) (sum + ticks[i]); } return (ticks_.length * 1000) / sum; } private class ThreadImgProcess extends Thread { private ThreadImgProcess() { } @Override // java.lang.Thread, java.lang.Runnable public void run() { int fpaTemp; int camTemp; Logging.trace("Enter ThreadImgProcess"); int frameBytes = ((MagApplication.magParameter.basePara1.fpaWidth * MagApplication.magParameter.basePara1.fpaHeight) * 16) / 8; byte[] imgExchangeBuf2 = new byte[frameBytes + 1024]; while (!UsbCommunication.this.isExitThreadImgProcess_) { synchronized (UsbCommunication.this.condImgProcess_.cond) { while (!UsbCommunication.this.condImgProcess_.notified) { try { UsbCommunication.this.condImgProcess_.cond.wait(); } catch (InterruptedException e) { } } UsbCommunication.this.condImgProcess_.notified = false; if (imgExchangeBuf2 == null) { imgExchangeBuf2 = new byte[UsbCommunication.this.imgExchangeBuf1_.length]; } System.arraycopy(UsbCommunication.this.imgExchangeBuf1_, 0, imgExchangeBuf2, 0, UsbCommunication.this.imgExchangeBuf1_.length); fpaTemp = UsbCommunication.this.fpaTemp_ + MagApplication.magParameter.basePara2.fpaTempFix; camTemp = UsbCommunication.this.camTemp_ + MagApplication.magParameter.basePara2.fpaTempFix; } DeviceController.Lock(); boolean ret = DeviceController.pushFrame(imgExchangeBuf2, fpaTemp, camTemp); DeviceController.Unlock(); if (UsbCommunication.this.isExitThreadImgProcess_) { break; } else if (ret && UsbCommunication.this.imgViewer_ != null) { UsbCommunication.this.imgViewer_.onNewFrameReceived(fpaTemp, camTemp); } } Logging.trace("Leave ThreadImgProcess"); } } private class ThreadCaliRecv extends Thread { private ThreadCaliRecv() { } @Override // java.lang.Thread, java.lang.Runnable public void run() { Logging.trace("Enter ThreadRemoteFileReceiver"); byte[] recvbuf = new byte[16384]; int totalSize = 0; FileOutputStream out = null; UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(0, 0)); long t1 = System.currentTimeMillis(); while (true) { if (UsbCommunication.this.isExitCaliRecvThread) { break; } int remain = MagApplication.magParameter.caliInfo.size - totalSize; UsbCommunication usbCommunication = UsbCommunication.this; if (remain > 16384) { remain = recvbuf.length; } int len = usbCommunication.recvCali(recvbuf, 0, remain, UsbCommunication.TIMEOUT); if (len >= 0 && !UsbCommunication.this.caliPathName_.isEmpty()) { if (UsbCommunication.this.isToRecvFirstCaliPacket_) { totalSize = 0; UsbCommunication.this.isToRecvFirstCaliPacket_ = false; } if (totalSize == 0) { try { FileOutputStream out2 = new FileOutputStream(UsbCommunication.this.caliPathName_); out = out2; } catch (Exception e) { Logging.error("Receive remote file error 1."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); } } totalSize += len; if (totalSize > MagApplication.magParameter.caliInfo.size) { try { out.close(); Logging.error("Receive remote file error 3."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); break; } catch (Exception e2) { Logging.error("Receive remote file error 2."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); } } else { try { out.write(recvbuf, 0, len); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(2, 0, (totalSize * 100) / MagApplication.magParameter.caliInfo.size)); if (totalSize == MagApplication.magParameter.caliInfo.size) { try { out.close(); Logging.info("Receive remote file succ."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(1, 0)); if (!UsbCommunication.this.startTransfer(UsbCommunication.this.caliPathName_)) { UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); Logging.error("SDK start fail."); } } catch (Exception e3) { Logging.error("Receive remote file error 5."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); } } } catch (Exception e4) { Logging.error("Receive remote file error 4."); UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); } } } else if (System.currentTimeMillis() - t1 > Config.BPLUS_DELAY_TIME) { UsbCommunication.this.handler_.sendMessage(UsbCommunication.this.handler_.obtainMessage(3, 0)); break; } } Logging.trace("Leave ThreadRemoteFileReceiver"); } } }