固件升级教程
固件升级教程
1. 概述
MSDK固件升级功能支持检查已连接的可升级设备模块(遥控器设备,飞机设备),在获取到可升级的信息后,开发者可以下载对应的升级固件进行升级,同时可以通过监听获取升级过程中的信息。
2. 升级检查
2.1 检查飞机以及遥控器是否有新版本
示例代码如下:
OTAUpgradeManger.getInstance().detectDeviceUpdateInfo()
2.2 检查版本信息回调监听
/**
* 升级版本信息上报
*/
interface UpgradeVersionListener {
/**
* APP升级通知
*/
fun onAppUpgrade(manual:Boolean, needUpgrade: Boolean, bean: CheckResponseBean?){}
/**
* 设备升级通知
*/
fun onDeviceUpgrade(beanMap: HashMap<String, CheckResponseBean.Data>){}
/**
* 进入升级模式是否成功
*/
fun onEnterUpgradeMode(success: Boolean){}
/**
* 退出升级模式是否成功
*/
fun onExitUpgradeMode(success: Boolean){}
}
2.3 下载升级包
示例代码如下:
OTAUpgradeManger.getInstance()
.downloadFile(remoterBean.pkg_url, 0, object : FileTransmitListener<File> {
override fun onSuccess(result: File?) {
// download succcess
}
override fun onProgress(sendLength: Long, totalLength: Long, speed: Long) {
// progress
}
override fun onFailed(code: IAutelCode?, msg: String?) {
// download failed
}
})
3. 升级流程
3.1 进入升级模式
示例代码:
针对飞机升级需要切换升级模式
进入升级模式
OTAUpgradeManger.getInstance().switchUpgradeMode(true)
退出升级模式
OTAUpgradeManger.getInstance().switchUpgradeMode(false)
3.2 升级设备
3.2.1 初始化升级管理器
首先升级飞机然后升级遥控器,示例代码如下:
升级遥控器:
val deviceId = remoteDevice.getDeviceNumber()
UpgradeManager(deviceId).init(UpgradeClientTypeEnum.CLIENT_TYPE_GND)
升级飞机:
val deviceId = droneDevice.getDeviceNumber()
UpgradeManager(deviceId).init(UpgradeClientTypeEnum.CLIENT_TYPE_SKY)
3.2.2 开始升级流程
准备好升级包之后,就可以开始升级设备了。
示例代码如下:
UpgradeManager.startUpgradeFlow(file: File, md5: String?)
(加密包无需md5,目前也不对MD5进行校验)
3.2.3 升级过程状态监听
注册升级过程监听,示例代码如下:
UpgradeManager.registerUpgradeListener(listener:UpgradeListener)
4.Sample获取
参考Sample中OTAFirmwareUpgradeFragment
5.接口说明
5.1 升级过程监听
interface UpgradeListener {
/**
* 升级流程上报
*/
fun onUpgradeFlowChange(deviceId: Int, flowState: UpgradeFlowEnum){}
/**
* 升级流程中状态上报
*/
fun onUpgradeStateChange(deviceId: Int, state: UpgradeErrorStateEnum)
/**
* 上传 OTA 包进度上报
*/
fun onUploadPackageProgress(deviceId: Int, totalLength:Long, sendLength:Long, progress: Int, speed: Long )
/**
* 执行升级后,升级进度上报
*/
fun onUpgradeProgress(deviceId: Int, progress: Int)
/**
* 最终升级结果
*/
fun onUpgradeResult(deviceId: Int, resultBean: UpgradeResultBean)
}
5.2 升级流程
/**
* 升级流程状态
*/
enum class UpgradeFlowEnum {
GENERATE_FILE_MD5, //生成 OTA 文件 MD5
CHECK_UPGRADE_CONDITION, //检查升级条件
CHECK_UPGRADE_STATE, //检查当前升级状态
UPGRADE_ENTER, //进入升级状态
UPGRADE_HIGH_SPEED, //切换高速模式
NOTIFY_SINGLE_UPGRADE, //组网通知单个设备进入升级状态
UPGRADE_NORMAL_SPEED, //切换正常速度模式
UPGRADE_UPLOAD, //上传文件
UPGRADE_MD5_VERIFY, //上传完成,通知升级服务校验文件完整性
UPGRADE_EXECUTING, //升级执行中
UNKNOWN; // 未知状态
}
5.3 升级流程异常
/**
* 升级流程状态异常上报
*/
enum class UpgradeErrorStateEnum {
UPGRADE_UNKNOWN_DEVICE_CLIENT, //未知的升级设备类型,初始化时传递类型有误
UPGRADE_DEVICE_DISCONNECT, //升级设备断开连接
UPGRADE_DEVICE_NOT_EXIST, //升级设备不存在,根据Id未能找到设备
OTA_FILE_ERROR, //OTA 文件检查失败:不存在、长度异常、无法读取
UPGRADE_CONDITION_ERROR, //检查升级条件失败
UPGRADE_MODE_ENTER_ERROR, //进入升级模式失败
UPGRADE_HIGH_SPEED_ERROR, //切换高速模式失败
UPGRADE_NORMAL_SPEED_ERROR, //切换正常速度模式失败
ENTER_SINGLE_UPGRADE_ERROR, //组网 进入单机升级模式失败
CHECK_SINGLE_UPGRADE_STATUS_ERROR, //组网 检查单机升级模式失败
UPGRADE_UPLOAD_ERROR, //上传文件失败
UPGRADE_TRANSFER_PROGRESS_ERROR, //通知升级服务进度失败
UPGRADE_MD5_ERROR, //升级文件MD5校验失败
UPGRADE_EXECUTE_ERROR, //升级执行失败
UPGRADE_STATE_ERROR; //达到最大时间后,升级状态不正确
UPGRADE_STATE_ERROR_FOR_IDLE; //最终状态查询后为IDLE,认为失败;(一般认为升级过程中,升级服务异常重启,无法确认最终有无成功)
}
5.4 升级最终结果
/**
* 升级结果消息
* Copyright: Autel Robotics
*/
data class UpgradeResultBean(
/**
*升级错误, 参见 ERROR_TYPE_E 枚举
*/
var errCode: UpgradeErrorTypeEnum = UpgradeErrorTypeEnum.UPGRADE_ERR_NONE,
/**
*错误描述
*/
var errDesc: String = "",
/**
*应答结果 1-success 2-error
*/
var result: ResponseResultEnum = ResponseResultEnum.UNKNOWN,
)
5.4.1 ErrorCode枚举类
/**
* :响应结果枚举
* Copyright: Autel Robotics
*/
public enum class UpgradeErrorTypeEnum(var value: Int) {
/**
* 未知错误
*/
CODE_UNKNOWN(-100),
/**
* 升级超时
*/
UPGRADE_TIME_OUT (-101),
/**
* 升级上传文件失败
*/
UPGRADE_UPLOAD_ERROR (-102),
/**
* 升级下载文件失败
*/
UPGRADE_DOWNLAOD_ERROR (-103),
/**
* 重试次数达到
*/
UPGRADE_REPEAT_ERROR (-104),
/**
* "解析失败"
*/
PROTO_RESPONSE_PARSE_ERROR(-1),
/**
* "返回数据为空"
*/
PROTO_RESPONSE_EMPTY_DATA(-2),
/**
* "响应超时"
*/
PROTO_RESPONSE_TIMEOUT(-3),
/**
* "响应码错误"
*/
RESPONSE_CODE_ERROR(-4),
/**
* "设备未连接"
*/
DEVICE_DISCONNECTED(-5),
/**
* "重复请求"
*/
REPEAT_REQUEST(-6),
/**
* 成功
*/
UPGRADE_ERR_NONE(0),
/**
* in flight
*/
UPGRADE_ERR_FLY (1),
/**
* in upgrading
*/
UPGRADE_ERR_BUSY (2),
/**
* MD5 error
*/
UPGRADE_ERR_MD5 (3),
/**
* file not exist
*/
UPGRADE_ERR_EXIST (4),
/**
* low battery
*/
UPGRADE_ERR_LOBAT (5),
/**
* model not match
*/
UPGRADE_ERR_MODEL (6),
/**
* no enough space to upgrade
*/
UPGRADE_ERR_SPACE (7),
/**
* error header of pkg, like crc error
*/
UPGRADE_ERR_HEAD (8),
/**
* error signature, untrusted firmware
*/
UPGRADE_ERR_SIGN (9),
/**
* error firmware size, not match header
*/
UPGRADE_ERR_SIZE (10),
/**
* package error
*/
UPGRADE_ERR_PACKAGE (11),
/**
* error major version, not match firmware
*/
UPGRADE_ERR_VERSION (12),
/**
* lower than the current version, need force flag
*/
UPGRADE_ERR_LVERSION (13),
/**
* refuse upgrade request, errDesc for more details
*/
UPGRADE_ERR_REFUSE (14),
/**
* special error of every pkg, use errDesc for details
*/
UPGRADE_ERR_SPECIAL (15);
}
5.4.2 应答结果
/**
* :响应结果枚举
* Copyright: Autel Robotics
*/
enum class ResponseResultEnum(var value: Int) {
/**
* result response of upgrade finish
*/
FINISH(0),
/**
* result response of upgrade success
*/
SUCCESS(1),
/**
* result response of upgrade failure
*/
ERROR(2),
UNKNOWN(3);
}