| | |
| | | |
| | | import android.widget.Toast; |
| | | |
| | | import com.fwupgrade.saymanss.deviceplug.CameraControlTransfer; |
| | | import com.fwupgrade.saymanss.deviceplug.IUsbDevicePlugDelegate; |
| | | import com.fwupgrade.saymanss.deviceplug.UstorageDeviceInstance; |
| | | import com.fwupgrade.saymanss.utils.AppPathInfo; |
| | | import com.fwupgrade.saymanss.utils.UtilTools; |
| | | import com.fwupgrade.saymanss.view.GeneralDialog; |
| | | import com.jni.NativeMethods; |
| | | import com.jni.UStorageTestModule; |
| | | import com.spca.usb.CUSBListener; |
| | | |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | Log.i(TAG,"onCreate"); |
| | | initTarget(); |
| | | |
| | | initData(); |
| | | acceptFwList(); |
| | | initDeviceSDK(); |
| | | UStorageTestModule.getInstance().initUStorageDeviceModuleWithVirtualDisk(AppPathInfo.getLogPath()); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | private File[] mFileList; |
| | | private ArrayList<CameraControlTransfer> upgradeList = new ArrayList<>(); |
| | | private volatile boolean upgrading = false; |
| | | |
| | | private HashMap<String, Integer> tty = new HashMap<>(); |
| | | |
| | | private int mUpgradeSuccessNum = 0; |
| | | private int mUpgradeFailNum = 0; |
| | | private int mDevicesNum = 0; |
| | | |
| | | private final Handler mHander = new Handler(Looper.getMainLooper()) { |
| | | @Override |
| | |
| | | |
| | | case USB_DEVICE_DETACHED: |
| | | |
| | | int count = (int) msg.obj; |
| | | Log.i(TAG,"count="+count); |
| | | if (count<2) { |
| | | tty.clear(); |
| | | initTarget(); |
| | | int ret = UstorageDeviceInstance.getInstance().tryAgain(FwUpgradeService.this); |
| | | if (ret==1){ |
| | | Log.i(TAG,"发送失败结果广播给驾培"); |
| | | sendToDriveTrainByBroadCast(0,1); |
| | | sendToNewDriveTrainByBroadCast(0,1); |
| | | } |
| | | } |
| | | // int count = (int) msg.obj; |
| | | // Log.i(TAG,"count="+count); |
| | | // if (count<2) { |
| | | // int ret = UstorageDeviceInstance.getInstance().tryAgain(FwUpgradeService.this); |
| | | // if (ret==1){ |
| | | // Log.i(TAG,"发送失败结果广播给驾培"); |
| | | // sendToDriveTrainByBroadCast(0,1); |
| | | // sendToNewDriveTrainByBroadCast(0,1); |
| | | // } |
| | | // } |
| | | |
| | | break; |
| | | |
| | |
| | | |
| | | case USB_DEVICE_UPGRADE: |
| | | |
| | | String devName = (String) msg.obj; |
| | | int errCode = msg.arg1; |
| | | int succ_num = 0, fail_num = 0; |
| | | Log.d(TAG, "devicesNum:" + mDevicesNum + " mUpgradeSuccessNum:" + mUpgradeSuccessNum + " mUpgradeFailNum:" + mUpgradeFailNum); |
| | | |
| | | Log.d("fwup","Upgrade result:" + devName + ", errCode: " + errCode+".ttySize="+tty.size()); |
| | | |
| | | if (tty.containsKey(devName)) { |
| | | tty.put(devName, errCode == 0? UPGRADE_SUCCESS:UPGRADE_FAIL); |
| | | } |
| | | |
| | | for (Map.Entry<String, Integer> entry : tty.entrySet()) { |
| | | if (entry.getValue() == UPGRADE_SUCCESS) { |
| | | succ_num++; |
| | | } else if (entry.getValue() == UPGRADE_FAIL) { |
| | | fail_num++; |
| | | } |
| | | } |
| | | |
| | | if (succ_num + fail_num == tty.size()) { |
| | | if (mUpgradeFailNum + mUpgradeSuccessNum == mDevicesNum) { |
| | | Log.d(TAG, "Upgrade Finish!"); |
| | | |
| | | |
| | | sendToNewDriveTrainByBroadCast(succ_num,tty.size()); |
| | | sendToDriveTrainByBroadCast(succ_num,tty.size()); |
| | | |
| | | |
| | | |
| | | sendToNewDriveTrainByBroadCast(mUpgradeSuccessNum,mDevicesNum); |
| | | sendToDriveTrainByBroadCast(mUpgradeSuccessNum,mDevicesNum); |
| | | // System.exit(0); |
| | | } |
| | | break; |
| | |
| | | } |
| | | |
| | | // 每次升级路径值最小的 |
| | | private void initTarget() { |
| | | ArrayList<UsbDevice> targets = UstorageDeviceInstance.getInstance().acceptOTGDeviceInfo((UsbManager) getSystemService(USB_SERVICE)); |
| | | String min = "Z"; |
| | | |
| | | for (UsbDevice usbDevice: targets) { |
| | | if (usbDevice.getDeviceName().compareTo(min) < 0) { |
| | | min = usbDevice.getDeviceName(); |
| | | |
| | | } |
| | | } |
| | | Log.i(TAG,"min="+min); |
| | | tty.put(min, UPGRADE_PRESET); |
| | | } |
| | | |
| | | /** |
| | | * 获取fw列表 |
| | |
| | | * UstorageDeviceSDK初始化. |
| | | */ |
| | | private void initDeviceSDK() { |
| | | int count = 0 ; |
| | | int ret = UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(FwUpgradeService.this, FwUpgradeService.this); |
| | | Log.i(TAG,"initDeviceSDk,ret="+ret); |
| | | if (ret==1){ |
| | | |
| | | ++count; |
| | | Log.i(TAG,"再次的初始化"); |
| | | Message message = Message.obtain(); |
| | | message.obj = count; |
| | | message.what = USB_DEVICE_DETACHED; |
| | | mHander.sendMessageDelayed(message,1000); |
| | | } |
| | | |
| | | Log.i(TAG,"initDeviceSDk"); |
| | | UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(FwUpgradeService.this, FwUpgradeService.this); |
| | | } |
| | | |
| | | @Override |
| | |
| | | Log.d(TAG,"usbDeviceInsert"); |
| | | } |
| | | |
| | | @Override |
| | | public void permissionFinish(boolean isSuccessful, CUSBListener.UsbControlBlock ctrlBlock) { |
| | | synchronized (this) { |
| | | Message msg = Message.obtain(); |
| | | msg.obj = isSuccessful; |
| | | msg.what = USB_DEVICE_PERRMISSION; |
| | | mHander.sendMessage(msg); |
| | | Log.d(TAG,"permissionFinish:" + isSuccessful); |
| | | if (isSuccessful && ctrlBlock != null) { |
| | | initFwUpgrade(ctrlBlock); |
| | | }else{ |
| | | sendToDriveTrainByBroadCast(0,1); |
| | | sendToNewDriveTrainByBroadCast(0,1); |
| | | } |
| | | public void onProgress(float progress) { |
| | | |
| | | } |
| | | |
| | | private void upgradeNext() { |
| | | if (upgradeList.isEmpty()) { |
| | | Log.d(TAG, "upgrade complete!"); |
| | | mHander.sendEmptyMessage(USB_DEVICE_UPGRADE); |
| | | } |
| | | |
| | | if (!upgrading && upgradeList.size() > 0) { |
| | | upgrading = true; |
| | | CameraControlTransfer cct = upgradeList.get(0); |
| | | |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (!cct.bPermission) { |
| | | return; |
| | | } |
| | | Log.d(TAG,"UpdateCameraInfo->dev" + cct.dev.getDeviceName()); |
| | | boolean bClaim = cct.connection.claimInterface(cct.dev.getInterface(0), true); |
| | | //devInfo.connection.releaseInterface(devInfo.dev.getInterface(0)); |
| | | Log.d(TAG,"UpdateCameraInfo->bClaim" + String.valueOf(bClaim)); |
| | | |
| | | Log.d(TAG, "Start init"); |
| | | boolean r = NativeMethods.getInstance().Initialize(cct); |
| | | if (r) { |
| | | Log.d(TAG, "Initialise success!"); |
| | | byte data[] = new byte[100]; |
| | | java.util.Arrays.fill(data, (byte) 0); |
| | | if (NativeMethods.getInstance().GetProduct(data, 100)) { |
| | | if (data[0] != 0xFF) { |
| | | String devName = new String(data).trim(); |
| | | } |
| | | } |
| | | |
| | | java.util.Arrays.fill(data, (byte) 0); |
| | | |
| | | if (NativeMethods.getInstance().GetFwVersion(data, 100, true)) { |
| | | if (data[0] != 0xFF) { |
| | | String fwVersion = new String(data).trim(); |
| | | } |
| | | } |
| | | |
| | | final NativeMethods.SERIAL_FLASH_TYPE sft = NativeMethods.SERIAL_FLASH_TYPE.SFT_UNKNOW; |
| | | if(!NativeMethods.getInstance().GetSerialFlashType(sft, true)){ |
| | | Log.i("test", "GetSerialFlashType failed"); |
| | | } |
| | | sft.setValue(NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.ordinal()); |
| | | Log.i("test", "sft= " + sft.getValue()); |
| | | |
| | | if(sft.getValue() == 0){ |
| | | //return; |
| | | Log.i("test", "set sft= " + NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.toString()); |
| | | sft.setValue(NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.ordinal()); |
| | | } |
| | | |
| | | if (mFileList != null && mFileList.length > 0) { |
| | | try { |
| | | String path = mFileList[0].getAbsolutePath(); |
| | | |
| | | NativeMethods.ASIC_ROM_TYPE romType = NativeMethods.ASIC_ROM_TYPE.ART_Unknow; |
| | | int chipID[] = new int[1]; |
| | | if (!NativeMethods.getInstance().GetAsicRomType(romType, chipID)) { |
| | | throw new Exception("get chip rom type failed!"); |
| | | } |
| | | Log.d("RomType", String.valueOf(chipID[0])); |
| | | FileInputStream fis = new FileInputStream(path); |
| | | fis.available(); |
| | | byte[] buffer = new byte[7]; |
| | | |
| | | if (fis.read(buffer, 0, 7) != -1) { |
| | | if (buffer[5] < 48 || buffer[5] >= 65 || buffer[6] < 48 || buffer[6] >= 65) { |
| | | throw new Exception("The selected firmware and device do not match!"); |
| | | } |
| | | int id = (buffer[5] - 48) * 16 + (buffer[6] - 48); |
| | | if (id != chipID[0]) { |
| | | throw new Exception("The selected firmware and device do not match!"); |
| | | } |
| | | } |
| | | |
| | | Log.d(TAG, "BurnFw start..."); |
| | | if (!NativeMethods.getInstance().BurnerFW(FwUpgradeService.this, path, sft, true)) { |
| | | mUpgradeFailNum++; |
| | | Log.d(TAG, "BurnFw fail!!!"); |
| | | } else { |
| | | mUpgradeSuccessNum++; |
| | | Log.d(TAG, "BurnFw success!!!"); |
| | | } |
| | | |
| | | fis.close(); |
| | | SystemClock.sleep(500); |
| | | NativeMethods.getInstance().RestartDevice(); |
| | | } catch (Exception ex) { |
| | | Log.e(TAG, "upgrade fail: " + ex.getMessage()); |
| | | } |
| | | } |
| | | NativeMethods.getInstance().UnInitialize(); |
| | | } |
| | | upgradeList.remove(cct); |
| | | upgrading = false; |
| | | |
| | | SystemClock.sleep(1000); |
| | | |
| | | upgradeNext(); |
| | | } |
| | | }).start(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void permissionFinish(boolean isSuccessful, com.fwupgrade.saymanss.deviceplug.CUSBListener.UsbControlBlock ctrlBlock, CameraControlTransfer cct) { |
| | | synchronized (this) { |
| | | mDevicesNum++; |
| | | upgradeList.add(cct); |
| | | upgradeNext(); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void usbDeviceDetached() { |
| | | Log.d(TAG,"usbDeviceDetached"); |
| | | } |
| | | |
| | | /** |
| | | * 初始化升级环境 |
| | | */ |
| | | private void initFwUpgrade(CUSBListener.UsbControlBlock ctrlBlock) { |
| | | final int vendorId = ctrlBlock.getVenderId(); |
| | | final int productId = ctrlBlock.getProductId(); |
| | | int fileDescriptor = ctrlBlock.getFileDescriptor(); |
| | | int busNum = ctrlBlock.getBusNum(); |
| | | int devNum = ctrlBlock.getDevNum(); |
| | | boolean isTarget = false; |
| | | String usbfsName = getUSBFSName(ctrlBlock); |
| | | String devName = ctrlBlock.getDeviceName(); |
| | | |
| | | String serial = ctrlBlock.getSerial(); |
| | | |
| | | Log.d(TAG,"sonixCamInit " + String.format("vendorId:%d productId:%d fileDescriptor:%d busNum:%d devNum:%d usbfsName:%s devName:%s serial:%s,tty.size=%d", vendorId, productId, fileDescriptor, busNum, devNum, usbfsName, devName, serial,tty.size())); |
| | | |
| | | |
| | | if (tty.containsKey(devName) && tty.get(devName) == 0) { |
| | | isTarget = true; |
| | | tty.put(devName, 1); |
| | | } |
| | | |
| | | |
| | | |
| | | if (!isTarget) { |
| | | |
| | | Log.d(TAG,"Not sonixCam, close"); |
| | | ctrlBlock.close(); |
| | | |
| | | return; |
| | | } |
| | | |
| | | Log.d(TAG,"Is sonixCam, upgrade!"); |
| | | Message msg = Message.obtain(); |
| | | //msg.obj = errCode == 0; |
| | | msg.obj = new CameraInfo(vendorId, productId, fileDescriptor, busNum, devNum, usbfsName, devName); |
| | | msg.what = USB_DEVICE_OPEN; |
| | | mHander.sendMessage(msg); |
| | | } |
| | | |
| | | |
| | | private final String getUSBFSName(final CUSBListener.UsbControlBlock ctrlBlock) { |
| | | String result = null; |
| | | final String name = ctrlBlock.getDeviceName(); |
| | | final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null; |
| | | if ((v != null) && (v.length > 2)) { |
| | | final StringBuilder sb = new StringBuilder(v[0]); |
| | | for (int i = 1; i < v.length - 2; i++) |
| | | sb.append("/").append(v[i]); |
| | | result = sb.toString(); |
| | | } |
| | | if (TextUtils.isEmpty(result)) { |
| | | // Log.w(TAG, "failed to get USBFS path, try to use default path:" + name); |
| | | result = "/dev/bus/usb"; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | class Update implements Runnable { |
| | | CameraInfo cam; |