upgrade.cxx 2.8 KB
#include <string>
#include <thread>
#include <atomic>
#include "uav_core.h"
#include "uav_logger.h"
#include "uav_upgrade.h"
#include "uav_platform.h"
#include "uav_sdk_app_info.h"

static std::string s_model("tracer");
static std::atomic_bool s_upgrade_flag(false);

static bool getModuleInfo(T_ModuleInfo &info)
{
    info.model = "tracer";
    info.app_version = "V1.0.0";
    info.boot_version = "V1.0.0";
    info.hard_version = "V1.0.0";
    LOG_INFO("getModuleInfo");
    return true;
}

static bool upgradeConditionCheck(void)
{
    LOG_INFO("upgradeConditionCheck");
    return true;
}

static int upgradeExcute(bool force, const std::string &model, const std::string &filename)
{
    s_upgrade_flag.store(true);
    LOG_INFO("force: {}, model: {}, filename: {}", force, model.c_str(), filename.c_str());
    return 10;
}

static bool upgradeQuery(T_UAVUpgradeStatus &status)
{
    status.state = 1;
    status.progress = 10;
    status.error_code = 0;
    status.module = "tracer";
    return true;
}

static T_UAVUpgradeHandler g_uavUpgradeHandler = {
    .getModuleInfo = getModuleInfo,
    .upgradeConditionCheck = upgradeConditionCheck,
    .upgradeExcute = upgradeExcute,
    .upgradeQuery = upgradeQuery,
};

static void upgrade_routine(void)
{
    T_UAVFileDesc desc;

    // 升级初始化
    UAV_Upgrade_Init(&g_uavUpgradeHandler);

    // wait register success
    if(false == wait_register_ready(100)){
        LOG_ERROR("waitHandShakeRegister failed");
        return;
    }

    // 等待无人机控制进入升级模式
    while(false == s_upgrade_flag.load())
        std::this_thread::sleep_for(std::chrono::seconds(1));
    s_upgrade_flag.store(false);

    // 升级固件下载
    if(true == UAV_Upgrade_Firmware_Download(s_model, desc))
    {
        int64_t read_len = 0;
        std::vector<uint8_t> data;
        int64_t file_size = desc.file_size;
        LOG_INFO("file name: {}, size: {}, package size: {}", desc.file_name, desc.file_size, desc.package_size);
        while(file_size > 0)
        {
            UAV_Upgrade_Firmware_Request(read_len, desc.package_size, 200, data);
            read_len += data.size();
            file_size -= data.size();
            LOG_INFO("download data size: {}", data.size());
        }
        UAV_Upgrade_Firmware_Verify(DIGEST_TYPE_CRC32, "12345678", 50);
        UAV_Upgrade_Firmware_Finish("seccuss", 50);
    }
}

int main(int argc, char *argv[])
{
    T_AUserInfo usrInfo;
    logger_init(std::string(argv[1]));
    if(false == uav_sdk_app_info_init(&usrInfo)) {
        LOG_ERROR("uav_sdk_app_info_init failed");
        return -1;
    }
    UAV_Core_Init(&usrInfo);
    UAV_Core_SetAlias("tracer");
    UAV_Uart_Init("/dev/pts/20",115200);

    std::thread upgrade(upgrade_routine);
    upgrade.detach();

    return UAV_Core_ApplicationStart();
}