package com.fwupgrade.saymanss;
|
|
import android.app.Service;
|
import android.content.Intent;
|
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbManager;
|
import android.os.Handler;
|
import android.os.IBinder;
|
import android.os.Looper;
|
import android.os.Message;
|
import android.os.RemoteException;
|
import android.os.SystemClock;
|
import android.support.annotation.Nullable;
|
import android.text.TextUtils;
|
import android.util.Log;
|
|
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 java.io.File;
|
import java.io.FileInputStream;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @ProjectName: FwUpgrade
|
* @Package: com.fwupgrade.saymanss
|
* @ClassName: FwUpgradeService
|
* @Description: java类作用描述
|
* @Author: zhanwei.li
|
* @CreateDate: 2024/2/19 10:02
|
* @UpdateUser: 更新者
|
* @UpdateDate: 2024/2/19 10:02
|
* @UpdateRemark: 更新说明
|
* @Version: 1.0
|
*/
|
public class FwUpgradeService extends Service implements IUsbDevicePlugDelegate {
|
|
/** usb插入. */
|
private static final int USB_DEVICE_INSERT = 0;
|
/** usb权限. */
|
private static final int USB_DEVICE_PERRMISSION = 1;
|
/** usb拔出. */
|
private static final int USB_DEVICE_DETACHED = 2;
|
/** usb打开. */
|
private static final int USB_DEVICE_OPEN = 3;
|
/** 升级结束. */
|
private static final int USB_DEVICE_UPGRADE = 4;
|
/** 显示 */
|
private static final int USB_DEVICE_MESSAGE = 5;
|
|
private static final int UPGRADE_PRESET = 0;
|
|
private static final int UPGRADE_FAIL = 3;
|
private static final int UPGRADE_SUCCESS = 4;
|
|
|
private CameraInfo mCamInfo = null;
|
private static final String TAG = FwUpgradeService.class.getCanonicalName();
|
private IFwAidlInterface.Stub mBinder = new IFwAidlInterface.Stub() {
|
@Override
|
public void upgradeFw(String path, String device) throws RemoteException {
|
Log.i(TAG,"客户端调用更新固件方法,device="+device+"_path="+path);
|
|
}
|
};
|
|
|
@Nullable
|
@Override
|
public IBinder onBind(Intent intent) {
|
return mBinder;
|
}
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
Log.i(TAG,"onCreate");
|
|
initData();
|
acceptFwList();
|
initDeviceSDK();
|
|
}
|
|
|
private File[] mFileList;
|
private ArrayList<CameraControlTransfer> upgradeList = new ArrayList<>();
|
private volatile boolean upgrading = false;
|
|
private int mUpgradeSuccessNum = 0;
|
private int mUpgradeFailNum = 0;
|
private int mDevicesNum = 0;
|
|
private final Handler mHander = new Handler(Looper.getMainLooper()) {
|
@Override
|
public void dispatchMessage(Message msg) {
|
switch (msg.what) {
|
|
case USB_DEVICE_INSERT:
|
Toast.makeText(FwUpgradeService.this, "USB设备已插入", Toast.LENGTH_SHORT).show();
|
break;
|
|
case USB_DEVICE_PERRMISSION:
|
boolean isSuccessful = (boolean) msg.obj;
|
|
if (isSuccessful) {
|
Toast.makeText(FwUpgradeService.this, "USB设备权限申请成功", Toast.LENGTH_SHORT).show();
|
} else {
|
Toast.makeText(FwUpgradeService.this, "USB设备权限申请失败", Toast.LENGTH_SHORT).show();
|
}
|
break;
|
|
case USB_DEVICE_DETACHED:
|
|
// 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_OPEN:
|
Log.d(TAG, "USB_DEVICE_OPEN");
|
CameraInfo cameraInfo = (CameraInfo) msg.obj;
|
|
Toast.makeText(FwUpgradeService.this, "USB设备初始化成功,可以准备升级", Toast.LENGTH_SHORT).show();
|
acceptFwList();
|
mCamInfo = cameraInfo;
|
|
executeUpgrade(mCamInfo);
|
|
break;
|
|
case GeneralDialog.BUTTON_CLICK_OK:
|
|
break;
|
|
case USB_DEVICE_UPGRADE:
|
|
Log.d(TAG, "devicesNum:" + mDevicesNum + " mUpgradeSuccessNum:" + mUpgradeSuccessNum + " mUpgradeFailNum:" + mUpgradeFailNum);
|
|
if (mUpgradeFailNum + mUpgradeSuccessNum == mDevicesNum) {
|
Log.d(TAG, "Upgrade Finish!");
|
|
sendToNewDriveTrainByBroadCast(mUpgradeSuccessNum,mDevicesNum);
|
sendToDriveTrainByBroadCast(mUpgradeSuccessNum,mDevicesNum);
|
// System.exit(0);
|
}
|
break;
|
case USB_DEVICE_MESSAGE:
|
String text = (String) msg.obj;
|
Toast.makeText(FwUpgradeService.this, text, Toast.LENGTH_SHORT).show();
|
break;
|
}
|
}
|
};
|
|
private void sendToNewDriveTrainByBroadCast(int succ_num,int ttySize) {
|
Intent intent = new Intent();
|
intent.putExtra("result", succ_num == ttySize? 0 : 1); //0-成功
|
intent.setAction("com.fwupgrade.saymanss.UPGRADE_COMPLETE");
|
intent.setPackage("safeluck.drive.training");
|
sendBroadcast(intent);
|
}
|
private void sendToDriveTrainByBroadCast(int succ_num,int ttySize) {
|
Intent intent = new Intent();
|
intent.putExtra("result", succ_num == ttySize? 0 : 1);
|
intent.setAction("com.fwupgrade.saymanss.UPGRADE_COMPLETE");
|
intent.setPackage("demo1.tech.anyun.com.myapplication");
|
sendBroadcast(intent);
|
}
|
|
// 每次升级路径值最小的
|
|
|
/**
|
* 获取fw列表
|
*/
|
private void acceptFwList() {
|
Log.d(TAG,"acceptFwList: ");
|
File file = new File(AppPathInfo.getFwSavePath());
|
File[] files = file.listFiles();
|
if (files != null && files.length > 0) {
|
mFileList = files;
|
} else {
|
}
|
}
|
private void executeUpgrade(CameraInfo cameraInfo) {
|
if (mFileList != null && mFileList.length > 0) {
|
Log.i(TAG,"executeUpgrade");
|
new Thread(new Update(cameraInfo, mFileList[0])).start();
|
}
|
}
|
|
|
/**
|
* 初始化数据
|
*/
|
private void initData() {
|
UtilTools.createFolderInSdcard(AppPathInfo.getFwSavePath());
|
|
}
|
|
/**
|
* UstorageDeviceSDK初始化.
|
*/
|
private void initDeviceSDK() {
|
Log.i(TAG,"initDeviceSDk");
|
UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(FwUpgradeService.this, FwUpgradeService.this);
|
}
|
|
@Override
|
public void usbDeviceInsert() {
|
Log.d(TAG,"usbDeviceInsert");
|
}
|
|
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");
|
}
|
|
|
class Update implements Runnable {
|
CameraInfo cam;
|
File file;
|
public Update(CameraInfo cam, File file) {
|
this.cam = cam;
|
this.file = file;
|
}
|
@Override
|
public void run() {
|
int errCode = fwUpdate(cam, file);
|
|
Message msg = Message.obtain();
|
msg.obj = cam.devName;
|
msg.arg1 = errCode;
|
msg.what = USB_DEVICE_UPGRADE;
|
mHander.sendMessage(msg);
|
}
|
}
|
|
int fwUpdate(final CameraInfo cam, final File file) {
|
int errCode = -1;
|
int retry = 0;
|
|
synchronized (this) {
|
Log.d(TAG, String.format("Upgrade %s...", cam.devName));
|
|
Message msg = Message.obtain();
|
msg.obj = String.format("Upgrade %s...", cam.devName);
|
msg.what = USB_DEVICE_MESSAGE;
|
mHander.sendMessage(msg);
|
|
while (retry++ <= 3) {
|
errCode = UStorageTestModule.getInstance().sonixCamInit(cam.vendorId, cam.productId, cam.fileDescriptor, cam.busNum, cam.devNum, cam.usbfsName);
|
Log.d(TAG, "Initialize result:" + errCode);
|
if (errCode != 0) {
|
return errCode;
|
}
|
errCode = UStorageTestModule.getInstance().updateFW(file.getPath());
|
Log.d(TAG, "Upgrade result:" + errCode);
|
UStorageTestModule.getInstance().sonixCamUnInit();
|
if (errCode == 0) {
|
return 0;
|
}
|
SystemClock.sleep(1000);
|
}
|
}
|
return errCode;
|
}
|
|
static class CameraInfo {
|
int vendorId;
|
int productId;
|
int fileDescriptor;
|
int busNum;
|
int devNum;
|
String usbfsName;
|
String devName;
|
public CameraInfo(int vendorId, int productId, int fileDescriptor, int busNum, int devNum, String usbfsName, String devName) {
|
this.vendorId = vendorId;
|
this.productId = productId;
|
this.fileDescriptor = fileDescriptor;
|
this.busNum = busNum;
|
this.devNum = devNum;
|
this.usbfsName = usbfsName;
|
this.devName = devName;
|
}
|
}
|
}
|