110 lines
3.0 KiB
C
110 lines
3.0 KiB
C
|
|
#ifndef AES_USB_COMM_H
|
||
|
|
#define AES_USB_COMM_H
|
||
|
|
|
||
|
|
#include <string>
|
||
|
|
#include "vector"
|
||
|
|
#include "stdint.h"
|
||
|
|
|
||
|
|
#ifdef __OHOS__
|
||
|
|
#include <usb/usb_ddk_api.h>
|
||
|
|
#include <usb/usb_ddk_types.h>
|
||
|
|
#include "hid/hid_ddk_api.h"
|
||
|
|
#include "hid/hid_ddk_types.h"
|
||
|
|
#else
|
||
|
|
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#define AES_BUFFER_MAX 60
|
||
|
|
|
||
|
|
typedef uint8_t AES_Buffer;
|
||
|
|
|
||
|
|
class AES_USB {
|
||
|
|
class HIDDevice;
|
||
|
|
HIDDevice* hidDevice;
|
||
|
|
typedef enum {
|
||
|
|
// 缓冲区过大或错误,缓冲区最大60字节
|
||
|
|
BUFFER_MAX_ERROR = -2,
|
||
|
|
// 设备不存在
|
||
|
|
DEVICE_NOT_EXIST = -1,
|
||
|
|
// UserPIN 错误
|
||
|
|
DEVICE_USER_PIN_ERROR = -3,
|
||
|
|
} HID_ERROR;
|
||
|
|
char devicePIN[21];
|
||
|
|
public:
|
||
|
|
typedef struct OH_USB_Driver{
|
||
|
|
int32_t vendor_id;
|
||
|
|
int32_t product_id;
|
||
|
|
int64_t deviceId;
|
||
|
|
}OH_USB_Driver;
|
||
|
|
/**
|
||
|
|
* @brief 初始化USB
|
||
|
|
* @return int 0成功 -1失败
|
||
|
|
*/
|
||
|
|
static int init();
|
||
|
|
/**
|
||
|
|
* @brief 释放USB
|
||
|
|
*/
|
||
|
|
static void release();
|
||
|
|
AES_USB();
|
||
|
|
~AES_USB();
|
||
|
|
/**
|
||
|
|
* @brief 打开设备
|
||
|
|
* @param vendor_id 设备VID
|
||
|
|
* @param product_id 设备PID
|
||
|
|
* @param drivers USB列表(只有鸿蒙需要)
|
||
|
|
* @return int 1成功 -1失败
|
||
|
|
*/
|
||
|
|
int open(uint16_t vendor_id, uint16_t product_id,const std::vector<OH_USB_Driver>& drivers = std::vector<OH_USB_Driver>()) const;
|
||
|
|
/**
|
||
|
|
* @brief 设置用户PIN
|
||
|
|
* @param pin 用户PIN
|
||
|
|
* @return 设置用户PIN结果 0: 失败 1: 成功 -1: PIN长度错误
|
||
|
|
*/
|
||
|
|
int setUserPIN(const std::string& pin);
|
||
|
|
/**
|
||
|
|
* @brief 写入指定的VID和PID
|
||
|
|
* @param vendor_id VID
|
||
|
|
* @param product_id PID
|
||
|
|
* @return int 0成功 -1失败
|
||
|
|
*/
|
||
|
|
int set_vid_pid(uint16_t vendor_id, uint16_t product_id) const;
|
||
|
|
/**
|
||
|
|
* @brief 读取VID和PID值
|
||
|
|
* @param vendor_id VID
|
||
|
|
* @param product_id PID
|
||
|
|
* @return int 1: 成功 -1: 失败
|
||
|
|
*/
|
||
|
|
int read_vid_pid(uint16_t* vendor_id, uint16_t* product_id) const;
|
||
|
|
/**
|
||
|
|
* @brief 写入数据
|
||
|
|
* @param data 数据
|
||
|
|
* @param length 数据长度
|
||
|
|
* @param timeout 超时时间
|
||
|
|
* @return int32_t 大于0成功 小于等于0失败 -> HID_ERROR
|
||
|
|
*/
|
||
|
|
int32_t write(uint8_t* data, int length, int timeout = 1000 * 1000) const;
|
||
|
|
/**
|
||
|
|
* @brief 读取数据
|
||
|
|
* @param data 数据
|
||
|
|
* @param length 数据长度
|
||
|
|
* @param timeout 超时
|
||
|
|
* @return int32_t 大于0成功 小于等于0失败 -> HID_ERROR
|
||
|
|
*/
|
||
|
|
int32_t read(uint8_t* data, int length, int timeout = 1000 * 1000) const;
|
||
|
|
/**
|
||
|
|
* @brief 关闭设备
|
||
|
|
*/
|
||
|
|
void close() const;
|
||
|
|
//---------------厂家函数------------------------------------------------------------------------------
|
||
|
|
static int resetAll();
|
||
|
|
typedef struct PID_Key {
|
||
|
|
uint16_t vendor_id;
|
||
|
|
uint16_t product_id;
|
||
|
|
uint8_t PIN[20];
|
||
|
|
} PID_Key;
|
||
|
|
static int readID_Pin(PID_Key* key);
|
||
|
|
static int writeID_Pin(const PID_Key* key);
|
||
|
|
//---------------厂家函数------------------------------------------------------------------------------
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif
|