APIs and Examples

Last Updated on : 2022-11-24 09:20:29download

This topic describes the password service APIs and provides examples of how to implement this service using the APIs.

Online password

API

typedef struct {
    UCHAR_T  allday;        /*0:The password is valid at the specified time 1:password is valid all day*/
    UCHAR_T  start_hour;
    UCHAR_T  start_min;
    UCHAR_T  end_hour;
    UCHAR_T  end_min;
    UCHAR_T  week;          /*bit0:Sunday bit1:Monday ... bit6:Saturday*/
}TY_PW_SCHEDULE_S;

typedef struct {
    UCHAR_T             cnt;
    TY_PW_SCHEDULE_S    info[TY_SVC_LOCK_ONLINE_PW_SCHEDULE_MAX];
}TY_PW_SCHEDULE_ARR_S;

typedef struct {
    INT_T   year; // start 1990
    INT_T   mon;  // 0-11
    INT_T   day;  // 1-31
    INT_T   hour; // 0-11
    INT_T   min;  // 0-59
    INT_T   sec;  // 0-59
}TY_ONLINE_PW_TIME_S;

typedef struct {
    UCHAR_T pw_len;         /*len of the password*/
    UINT_T num;            /*number of the password*/
    UCHAR_T one_off;        /*0:unlimited 1:use only once*/
    UCHAR_T state;          /*0:the password is valid 1:the password had deleted by app*/
    TY_ONLINE_PW_TIME_S start_time;     /* green time */
    TY_ONLINE_PW_TIME_S end_time;       /* green time */
    UCHAR_T password[TY_SVC_LOCK_ONLINE_PW_MAX_LEN];        /*the ascii code of the password*/
    TY_PW_SCHEDULE_ARR_S schedule;
}TY_PW_INFO_S;

/**
 * @brief  The callback function of get online password result
 * @param  ret: True get online password success, False get online passowrd fail
 * @param  pw_cnt: the number of online passowrd return from cloud
 * @param  pw_info: the struct polinter of online password information
 * @return void
 */
typedef VOID (*ONLINE_PW_RESULT_CB)(IN BOOL_T ret, IN UCHAR_T pw_cnt, IN TY_PW_INFO_S *pw_info);

/**
 * @brief  Online password function get interface
 * @param  None
 * @return Function operation result  OPRT_OK is ok other is fail
 */
OPERATE_RET tuya_svc_lock_online_pw_get_pw_list(IN ONLINE_PW_RESULT_CB callback);

Example

STATIC VOID tuya_lock_online_pw_callback(IN BOOL_T ret, IN UCHAR_T pw_cnt, IN TY_PW_INFO_S *pw_info)
{
    int i = 0,pw_idx = 0;
    TY_PW_INFO_S *p_info = NULL;
    // Asynchronous callback. The password is returned.
    PR_DEBUG("get pw ret = %d pw_cnt = %d ",ret, pw_cnt);
  
    return;
}


STATIC OPERATE_RET ty_uart_get_pass_handle(void)
{
    OPERATE_RET op_ret = OPRT_OK;
    op_ret = tuya_svc_lock_online_pw_get_pw_list(tuya_lock_online_pw_callback);
    return op_ret;
}

Offline password

API

typedef enum {
    RESULT_SUCC = 0,
    RESULT_FAILED,
}TY_OFFLINE_PW_RET_T;

typedef enum {
    RET_TYPE_TIME       = 0x00,     //reusable password
    RET_TYPE_ONCE       = 0x01,     //one-off password
    RET_TYPE_CLEAR_ALL  = 0x02,     //clear all password
    RET_TYPE_TIME_FIRST = 0x10,     //password first time to activate
    RET_TYPE_CLEAR_ONE  = 0x12,     //clear one password
    RET_TYPE_ERR        = 0xFF,     //check error
}TY_OFFLINE_PW_RET_TYPE_T;


typedef struct {
    INT_T   year; // start 1990
    INT_T   mon;  // 0-11
    INT_T   day;  // 1-31
    INT_T   hour; // 0-11
    INT_T   min;  // 0-59
    INT_T   sec;  // 0-59
}TY_OFFLINE_PW_TIME_S;

typedef struct {
    TY_OFFLINE_PW_TIME_S time;
    UCHAR_T     pw_len;
    UCHAR_T     *pw;
}TY_OFFLINE_PW_INFO_S;

typedef struct {
    TY_OFFLINE_PW_RET_T   ret;
    TY_OFFLINE_PW_RET_TYPE_T   type;
    UCHAR_T   decode_len;
    UCHAR_T   decode[16];
}TY_OFFLINE_PW_RESULT_S;

/**
* @brief Verify whether the password is valid
* @param[in] active_time  The device activation time
* @param[in] p_pw_info  The original password info struct pointer
* @param[inout] p_result  Pointer to the password verification structure result
* @return Function Operation Result  OPRT_OK is ok other is fail 
*/
OPERATE_RET tuya_svc_lock_offline_pw_check(IN UINT_T active_time, IN TY_OFFLINE_PW_INFO_S *p_pw_info,INOUT TY_OFFLINE_PW_RESULT_S *p_result);

/**
* @brief Initialize the offline password list and structure
* @param[in] max_num  Max number of offline passwords
* @return Function Operation Result  OPRT_OK is ok other is fail 
*/
OPERATE_RET tuya_svc_lock_offline_pw_init(IN UINT_T max_num);

/**
* @brief Clear all offline password records and information
* @return Function Operation Result  OPRT_OK is ok other is fail 
*/
OPERATE_RET tuya_svc_lock_offline_pw_deinit(VOID);

Example

// When the device is initialized, call the offline password initialization API.
OPERATE_RET device_init(VOID_T)
{
    OPERATE_RET op_ret = OPRT_OK;

    PR_DEBUG("--------------dynamic password test demo-----------------------");
    //tuya offline password init
    op_ret = tuya_svc_lock_offline_pw_init(400);
    if(OPRT_OK != op_ret){
        PR_ERR("tuya_svc_lock_offline_pw_init failed op_ret:%d", op_ret);
        return op_ret;
    }

    PR_NOTICE("dev_init sucess !!!");
    return OPRT_OK;
}

// When the device is reset, call the offline password clearing API.
VOID ty_iot_gw_reset_cb(GW_RESET_TYPE_E type)
{
    OPERATE_RET op_ret = OPRT_OK;
    tuya_svc_lock_offline_pw_deinit();();
}

// To verify offline passwords, pass in the device activation time, the struct pointer containing the verification information, and the struct pointer used to store the verification result.

STATIC BOOL_T process_rx_uart_data(UCHAR_T *pmsg, UINT_T len, UCHAR_T cmd)
{
    OPERATE_RET op_ret = OPRT_OK;
    PR_NOTICE("cmd:%02X", cmd);

    TY_OFFLINE_PW_INFO_S *p_pw_info = (TY_OFFLINE_PW_INFO_S *)pmsg;
    TY_OFFLINE_PW_RESULT_S result;

    memset(&result, 0, SIZEOF(TY_OFFLINE_PW_RESULT_S));

    switch(cmd){
        case CMD_CHECK_OFFLINE_PASSWORD:
            op_ret = tuya_svc_lock_offline_pw_check(get_active_time(), p_pw_info, &result);
            if(OPRT_OK == op_ret) {
                PR_NOTICE("----------------dynamic pw verify result is %d------------------",result.ret);
                PR_DEBUG("type = %d, decode len = %d",result.type, result.decode_len);
            }
        break;
        default:break;
    }

    return TRUE;
}