Skip to content

Latest commit

 

History

History
executable file
·
101 lines (82 loc) · 7.18 KB

固件升级.md

File metadata and controls

executable file
·
101 lines (82 loc) · 7.18 KB

OTA设备固件升级

固件升级简介

设备固件升级又称 OTA,是物联网通信服务的重要组成部分。当物联设备有新功能或者需要修复漏洞时,设备可以通过 OTA 服务快速进行固件升级。请参考官网文档 控制台使用手册 固件升级

体验固件升级需要在控制台中添加新的固件,请参考官网文档 开发者手册 设备固件升级

运行示例程序体验检查固件更新功能

请先按照 基于TCP的MQTT设备接入 的步骤,按密钥或证书方式配置 OTASampleTest.java 中以下信息,将设备 连接MQTT 进行认证接入。

{
  private static String mProductID = "";
  private static String mDevName = "";
  private static String mDevPSK  = ""; //若使用证书验证,设为null
  private static String mCertFilePath = "";           // 填写 resources 文件夹下设备证书文件名称
  private static String mPrivKeyFilePath = "";        // 填写 resources 文件夹下设备私钥文件名称
  private static String mDevCert = "";           // 填写 设备证书文件内容
  private static String mDevPriv = "";           // 填写 设备私钥文件内容
}

运行 OTASampleTest.java 的main函数,设备上线后调用checkFirmware(),将上报固件版本号检查是否有固件更新。示例代码如下:

private static void checkFirmware() {
    try {
        Thread.sleep(2000);
        String workDir = System.getProperty("user.dir") + "/hub/hub-device-java/src/test/resources/";
        mqttconnection.initOTA(workDir, new TXOTACallBack() {
        	@Override
            public void onReportFirmwareVersion(int resultCode, String version, String resultMsg) {
                LOG.error("onReportFirmwareVersion:" + resultCode + ", version:" + version + ", resultMsg:" + resultMsg);
            }
            
            @Override
            public boolean onLastestFirmwareReady(String url, String md5, String version) {
                LOG.error("MQTTSample onLastestFirmwareReady");
                return false;
            }
            
            @Override
            public void onDownloadProgress(int percent, String version) {
                LOG.error("onDownloadProgress:" + percent);
            }
            
            @Override
            public void onDownloadCompleted(String outputFile, String version) {
                LOG.error("onDownloadCompleted:" + outputFile + ", version:" + version);
            
                mqttconnection.reportOTAState(TXOTAConstansts.ReportState.DONE, 0, "OK", version);
            }
            
            @Override
            public void onDownloadFailure(int errCode, String version) {
                LOG.error("onDownloadFailure:" + errCode);
            
                mqttconnection.reportOTAState(TXOTAConstansts.ReportState.FAIL, errCode, "FAIL", version);
            }
        });
        mqttconnection.reportCurrentFirmwareVersion("0.0.1");
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

以下是设备成功订阅 OTA 升级的 Topic 主题 和 上报当前版本号 的logcat日志,示例中的版本号version传入的值为0.0.1。

17/03/2021 10:12:08,546 [main] INFO  TXMqttConnection subscribe 749  - Starting subscribe topic: $ota/update/AP9ZLEVFKT/gateway2
17/03/2021 10:12:08,556 [MQTT Call: AP9ZLEVFKTgateway2] DEBUG OTASampleTest onSubscribeCompleted 405  - onSubscribeCompleted, status[OK], topics[[$ota/update/AP9ZLEVFKT/gateway2]], userContext[], errMsg[subscribe success]
onSubscribeCompleted status OK
17/03/2021 10:12:08,549 [main] INFO  TXMqttConnection publish 567  - Starting publish topic: $ota/report/AP9ZLEVFKT/gateway2 Message: {"report":{"version":"0.0.1"},"type":"report_version"}
17/03/2021 10:12:08,564 [MQTT Call: AP9ZLEVFKTgateway2] DEBUG OTASampleTest onPublishCompleted 393  - onPublishCompleted, status[OK], topics[[$ota/report/AP9ZLEVFKT/gateway2]],  userContext[], errMsg[publish success]
17/03/2021 10:12:08,578 [MQTT Call: AP9ZLEVFKTgateway2] INFO  TXMqttConnection messageArrived 1129  - Received topic: $ota/update/AP9ZLEVFKT/gateway2, id: 0, message: {"result_code":0,"result_msg":"success","type":"report_version_rsp","version":"0.0.1"}

当在控制台中触发固件升级操作后,设备端会通过订阅的 OTA 升级的 Topic $ota/update/${productID}/${deviceName} 收到固件升级的消息

17/03/2021 10:14:05,260 [MQTT Call: AP9ZLEVFKTgateway2] INFO  TXMqttConnection messageArrived 1129  - Received topic: $ota/update/AP9ZLEVFKT/gateway2, id: 0, message: {"file_size":234775,"md5sum":"f2f1b3317d4f1ef7f512bfae5050563b","type":"update_firmware","url":"https://ota-1255858890.cos.ap-guangzhou.myqcloud.com/100012619289_AP9ZLEVFKT_0.0.2?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdO8ldrUa0Uts4H5Gzx6FZ9nfedjpiCd7%26q-sign-time%3D1615947245%3B1616033645%26q-key-time%3D1615947245%3B1616033645%26q-header-list%3D%26q-url-param-list%3D%26q-signature%3D47e9850aaf8942711ea3cd9896af1694c1df4d79","version":"0.0.2"}
mStoragePath=/Users/sun/Documents/work_code_from_gerrit/iot-device-android/hub/hub-device-java/src/test/resources/
17/03/2021 10:14:05,262 [Thread-2] DEBUG TXOTAImpl run 513  - fileLength 0 bytes
17/03/2021 10:14:05,262 [Thread-2] DEBUG TXOTAImpl run 522  - connect: https://ota-1255858890.cos.ap-guangzhou.myqcloud.com/100012619289_AP9ZLEVFKT_0.0.2?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdO8ldrUa0Uts4H5Gzx6FZ9nfedjpiCd7%26q-sign-time%3D1615947245%3B1616033645%26q-key-time%3D1615947245%3B1616033645%26q-header-list%3D%26q-url-param-list%3D%26q-signature%3D47e9850aaf8942711ea3cd9896af1694c1df4d79
17/03/2021 10:14:08,532 [Thread-2] INFO  TXOTAImpl checkServerTrusted 446  - checkServerTrusted OK!!!
17/03/2021 10:14:08,668 [Thread-2] DEBUG TXOTAImpl run 532  - totalLength 234775 bytes
17/03/2021 10:14:08,669 [Thread-2] DEBUG TXOTAImpl run 555  - download 7789 bytes. percent:3
17/03/2021 10:14:08,705 [Thread-2] DEBUG TXOTAImpl run 555  - download 163301 bytes. percent:69
17/03/2021 10:14:08,713 [Thread-2] DEBUG TXOTAImpl run 555  - download 234775 bytes. percent:100
17/03/2021 10:14:08,717 [Thread-2] INFO  TXMqttConnection publish 567  - Starting publish topic: $ota/report/AP9ZLEVFKT/gateway2 Message: {"report":{"progress":{"result_msg":"OK","result_code":"0","state":"done"},"version":"0.0.2"},"type":"report_progress"}
17/03/2021 10:14:08,718 [MQTT Call: AP9ZLEVFKTgateway2] DEBUG OTASampleTest onPublishCompleted 393  - onPublishCompleted, status[OK], topics[[$ota/report/AP9ZLEVFKT/gateway2]],  userContext[], errMsg[publish success]

以上日志为 收到固件升级的消息 ,下载并显示下载进度,下载新版本固件成功后,上报最新的版本号,此时示例中上报的最新版本号version为0.0.2。