本文介绍如何在第三方OS上通过ID²-KM方式完成设备端适配。

注意事项

如果您不需要适配OSA多线程管理接口或OSA网络操作接口,为防止编译过程中出现对应symbol找不到的问题,请在实现时直接return或者返回0。代码示例如下。
/* for void type, directly return */
void ls_osa_net_disconnect(int fd) {
    return;
}
/* for int type, return 0 */
int ls_osa_net_connect(const char *host, const char *port, int type) {
    return 0;
}

步骤一:适配OSA接口

  1. 适配OSA基础功能接口。API接口详情,请参照设备端SDK 适配手册
    void ls_osa_print(const char *fmt, …)
    int ls_osa_snprintf(char *str, size_t size, const char *fmt, ...)
    void *ls_osa_malloc(size_t size)
    void ls_osa_free(void *ptr)
    long long ls_osa_get_time_ms(void)         
  2. (可选)若使用多线程的场景,需要实现OSA多线程管理接口,否则直接返回成功。
    int ls_osa_mutex_create(void **mutex)
    void ls_osa_mutex_destroy(void *mutex)
    int ls_osa_mutex_lock(void *mutex)
    int ls_osa_mutex_unlock(void *mutex)        
  3. (可选)若使用安全SDK的安全连接服务iTLS,需要实现OSA网络操作接口,否则直接返回成功。
    int ls_osa_net_connect(const char *host, const char *port, int type)
    void ls_osa_net_disconnect(int fd)
    int ls_osa_net_send(int fd, unsigned char *buf, size_t len, int *ret_orig)
    int ls_osa_net_recv(int fd, unsigned char *buf, size_t len, int timeout, int *ret_orig)       
  4. 适配Soft-KM HAL接口。

    Soft-KM HAL接口详细信息,请参考设备端SDK 适配手册

    int ls_hal_get_dev_id(uint8_t *dev_id, uint32_t *id_len)
    int ls_hal_open_rsvd_part(int flag)
    int ls_hal_write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
    int ls_hal_read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
    int ls_hal_close_rsvd_part(int fd)        

步骤二:适配验证OSA接口

编译并运行hal_test,显示结果如下时验证通过。

hal_test 打印结果
<LS_LOG> PL ERR ls_hal_get_dev_id 167: short buffer id len is 1
<LS_LOG> PL ERR ls_hal_get_dev_id 167: short buffer id len is 1
<LS_LOG> PL ERR ls_hal_write_rsvd_part 245: bad param
<LS_LOG> PL ERR ls_hal_read_rsvd_part 283: read failed real read len is -1
<LS_LOG> INF hal_km_rsvd_part_stress_test 357: write 1K data total time: 1.000000ms, av_time: 0.001000ms
<LS_LOG> INF hal_km_rsvd_part_stress_test 372: read 1K data total time: 1.000000ms, av_time: 0.001000ms
<LS_LOG> INF hal_km_test 434: ============================> HAL KM Test Pass.

<LS_LOG> INF hal_crypto_test 43: HAL Hash Test:
<LS_LOG> INF hal_hash_test 140: SHA1 test success!
<LS_LOG> INF hal_hash_test 190: SHA256 test success!
<LS_LOG> INF hal_hash_test 239: md5 test success!
<LS_LOG> INF hal_hash_test 249: =========================> HAL Hash Test Pass.

<LS_LOG> INF hal_crypto_test 49: HAL Rand Test:
<LS_LOG> INF hal_rand_test 22: ==========================> HAL Rand Test Pass.

<LS_LOG> INF hal_crypto_test 55: HAL AES Test:
<LS_LOG> INF hal_aes_test 682: HAL AES ECB Test:
<LS_LOG> INF _aes_ecb_test 445: AES_ECB_128 test success!
<LS_LOG> INF _aes_ecb_test 445: AES_ECB_192 test success!
<LS_LOG> INF _aes_ecb_test 445: AES_ECB_256 test success!
<LS_LOG> INF hal_aes_test 691: HAL AES CBC Test:
<LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_128 test success!
<LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_192 test success!
<LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_256 test success!
<LS_LOG> INF hal_aes_test 700: HAL AES CTR Test:
<LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_128 test success!
<LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_192 test success!
<LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_256 test success!
<LS_LOG> INF hal_aes_test 709: HAL AES CFB Test:
<LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_128 test success!
<LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_192 test success!
<LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_256 test success!
<LS_LOG> INF hal_aes_test 720: =============================> HAL AES Test Pass.

<LS_LOG> INF hal_crypto_test 61: HAL RSA Test:
<LS_LOG> INF _hal_encrypt_decrypt 150: RSA encrypt/decrypt test success
<LS_LOG> INF hal_rsa_test 192: =============================> HAL RSA Test Pass.