/**
 * @file tkl_system.c
 * @brief this file was auto-generated by tuyaos v&v tools, developer can add implements between BEGIN and END
 *
 * @warning: changes between user 'BEGIN' and 'END' will be keeped when run tuyaos v&v tools
 *           changes in other place will be overwrited and lost
 *
 * @copyright Copyright 2020-2021 Tuya Inc. All Rights Reserved.
 *
 */

// --- BEGIN: user defines and implements ---
#include "tkl_system.h"
#include "tuya_error_code.h"
#include <sys/time.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
// --- END: user defines and implements ---

/**
 * @brief system reset
 *
 * @param none
 *
 * @return none
 */
void tkl_system_reset(void)
{
    // --- BEGIN: user implements ---
    return;
    // --- END: user implements ---
}

/**
 * @brief Get system tick count
 *
 * @param none
 *
 * @return system tick count
 */
SYS_TICK_T tkl_system_get_tick_count(void)
{
    // --- BEGIN: user implements ---
    struct timespec time1 = {0, 0};
    clock_gettime(CLOCK_MONOTONIC, &time1);
    return 1000 * ((uint32_t)time1.tv_sec) + ((uint32_t)time1.tv_nsec) / 1000000;
    // --- END: user implements ---
}

/**
 * @brief Get system millisecond
 *
 * @param none
 *
 * @return system millisecond
 */
SYS_TIME_T tkl_system_get_millisecond(void)
{
    // --- BEGIN: user implements ---
    struct timespec time1 = {0, 0};
    clock_gettime(CLOCK_MONOTONIC, &time1);
    return 1000 * ((uint32_t)time1.tv_sec) + ((uint32_t)time1.tv_nsec) / 1000000;
    // --- END: user implements ---
}

/**
 * @brief Get system random data
 *
 * @param[in] range: random from 0  to range
 *
 * @return random value
 */
int tkl_system_get_random(uint32_t range)
{
    // --- BEGIN: user implements ---
    return rand() % range;
    // --- END: user implements ---
}

/**
 * @brief Get system reset reason
 *
 * @param[in] describe: point to reset reason describe
 *
 * @return reset reason
 */
TUYA_RESET_REASON_E tkl_system_get_reset_reason(char **describe)
{
    // --- BEGIN: user implements ---
    // if (describe)
    //     *describe = "unknown reset reason";

    return TUYA_RESET_REASON_UNKNOWN;
    // --- END: user implements ---
}

/**
 * @brief  system sleep
 *
 * @param[in] describe: num ms
 *
 * @return none
 */
void tkl_system_sleep(uint32_t num_ms)
{
    // --- BEGIN: user implements ---
    TIME_S sTmpTime;
    TIME_MS msTmpTime;

    sTmpTime = num_ms / 1000;
    msTmpTime = num_ms % 1000;

    if (sTmpTime)
        sleep(sTmpTime);

    if (msTmpTime)
        usleep(msTmpTime * 1000);

    return;
    // --- END: user implements ---
}

/**
 * @brief system delay
 *
 * @param[in] msTime: time in MS
 *
 * @note This API is used for system sleep.
 *
 * @return void
 */
void tkl_system_delay(uint32_t num_ms)
{
    // --- BEGIN: user implements ---
    tkl_system_sleep(num_ms);
    return;
    // --- END: user implements ---
}

/**
 * @brief get system cpu info
 *
 * @param[in] cpu_ary: info of cpus
 * @param[in] cpu_cnt: num of cpu
 * @note This API is used for system cpu info get.
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */

OPERATE_RET tkl_system_get_cpu_info(TUYA_CPU_INFO_T **cpu_ary, int *cpu_cnt)
{
    // --- BEGIN: user implements ---
    // TUYA_CPU_INFO_T *cpus = tkl_system_malloc(sizeof(TUYA_CPU_INFO_T));
    // if(cpus) {
    //     cpus->use_ratio = 50;
    //     *cpu_cnt = 1;
    //     *cpu_ary = cpus;
    // } else {
    //     *cpu_cnt = 0;
    // }
    return OPRT_OK;
    // --- END: user implements ---
}