SDK 初始化

2024-10-08
暂无评分

概述

在使用 SDK 其它模块功能前,需要调用 SDK 初始化建立信任的通信链路,并初始化基础组件的运行环境。

正确使用初始化 SDK

需要正确配置初始化以下内容:

借用以下接口原型实现 SDK 初始化,初始化成功将返回 kOk,若初始化失败可以通过日志打印查看到错误信息。

ErrorCode  ESDKInit::Init(const Options& options)

注意:设备首次使用时,需要通过 DJI Pilot 进行绑定,否则该接口会一直阻塞,直到成功绑定。

/* @brief ESDKInit initialize SDK environment, preparing for SDK function
 * components, caller should ensure that it is initialized correctly, otherwise
 * the function may be abnormal
 */
class ESDKInit final {
   public:
    ~ESDKInit();

    ESDKInit(const ESDKInit&) = delete;
    ESDKInit& operator=(const ESDKInit&) = delete;

    /* @brief get the singleton of ESDKInit.
     * @return instance.
     */
    static ESDKInit* Instance();

    /* @brief Initialize SDK environment
     * @param options: options for initializing the environment
     * @return Execution result.
     */
	ErrorCode Init(const Options& options);

    /* @brief Returns the firmware version
     * @return Firmware version
     */
    FirmwareVersion GetFirmwareVersion() const;

    /* @brief Returns the product name
     * @return Product name
     */
    ProductName GetProductName() const;

    /* @brief Returns the vendor name
     * @return Vendor name
     */
    VendorName GetVendorName() const;

    /* @brief Returns the serial number
     * @return Serial number
     */
    SerialNumber GetSerialNumber() const;

    /* @brief Returns the App license info
     * @return App info
     */
    AppInfo GetAppInfo() const;

    /* @brief DeInitialize SDK environment
     * @return Execution result.
     */
    ErrorCode DeInit();

   private:
    ESDKInit();
    class Impl;

    std::unique_ptr<Impl> impl_;
};

设备信息初始化

设备信息初始化后,通过 Pilot 连接机场时,可以看到初始化的设备相关信息,包含产品名称、供应商名称、产品序列号、固件版本等。

/**
 * brief 版本号定义
 */
struct Version {
    uint8_t major_version;
    uint8_t minor_version;
    uint8_t modify_version;
    uint8_t debug_version;
};

/**
 * brief 设备信息定义
 */
using FirmwareVersion = Version;
using ProductName = std::string;
using VendorName = std::string;
using SerialNumber = std::string;

/**
 * brief 用于初始化 EdgeSDK 配置信息
 */
struct Options {
    /*! 设备产品名称 */
    ProductName product_name;
    /*! 设备供应商名称 */
    VendorName vendor_name;
    /*! 设备产品序列号,应该保持每个设备的唯一性 */
    SerialNumber serial_number;
    /*! 固件版本号 */
    FirmwareVersion firmware_version;
    ...
}

开发者认证信息初始化

开发者网站open in new window中通过点击 “应用 > 创建应用” 自行填写信息,用于注册 ESDK 应用程序。未注册将无法正确初始化,会影响功能使用。

/**
 * brief AppInfo
 * 是由 DJI 开发者网站开发者账户生成的 App 认证信息,只有正确填入注册获取的信息才能成功初始化 EdgeSDK
 */
struct AppInfo {
	/*! 开发者网站注册的 APP 名称 */
    std::string app_name;
   
   /*! 开发者网站注册生成的 APP ID */
    std::string app_id;
   
   /*! 开发者网站注册生成的 APP KEY */
    std::string app_key;

    /*! 开发者网站注册生成的 APP LICENSE */
    std::string app_license;

    /*! 开发者网站注册用户账号 */
    std::string developer_account;
};

struct Options {
    ...
    /*! 开发者网站注册生产的 APP 认证信息 */
    AppInfo app_info;
    ...
}

日志终端初始化

初始化日志终端,可以初始化多个终端和对应的终端打印级别,在 SDK 运行期间生成的日志信息,将通过初始化的日志终端进行打印。

/**
 * @brief 日志终端配置
 */
struct LoggerConsole {
    /*! 日志输出函数定义 */
    using OutputFunc = std::function<ErrorCode(const uint8_t *, uint32_t)>;

    /*! 过滤日志级别 , 小于等于该级别的日志才输出 */
    LogLevel level;

    /*! 输出日志处理函数 */
    OutputFunc output_func;

    /*! 终端是否支持颜色显示 */
    bool is_support_color;
};

using LoggerConsoleList = std::list<LoggerConsole>;

struct Options {
    ...
    /*! 打印日志的终端列表,支持添加多个终端 */
    LoggerConsoleList logger_console_lists;
    ...
}

实现秘钥获取接口

秘钥接口获取该设备独有 RSA2048 秘钥信息,开发者需要实现设备秘钥的安全生产和持久化存储。EdgeSDK 通过该接口获取秘钥信息实现安全功能。

注意:一旦秘钥丢失,需要使用 Pilot 连接机场进行重新绑定。

examples/init/key_store_default.h 实现了获取秘钥接口的示例,生成的秘钥保存在 /tmp/ 目录中,设备重启后秘钥会丢失。

  • 函数调用介绍:
    EdgeSDK 通过该接口获取 RSA2048  DER 格式私钥与公钥。
class KeyStore {
   public:
    KeyStore() = default;
    virtual ~KeyStore() {}

    KeyStore(const KeyStore&) = delete;
    KeyStore& operator=(const KeyStore&) = delete;

    /* @brief Returns RSA2048 DER private key.
     * @return Execution result.
     */
    virtual ErrorCode RSA2048_GetDERPrivateKey(
        std::string& private_key) const = 0;

    /* @brief Returns RSA2048 DER public key.
     * @return Execution result.
     */
    virtual ErrorCode RSA2048_GetDERPublicKey(
        std::string& public_key) const = 0;
};
若您对文档有意见或疑惑,点击可快速反馈,我们会与您联系。