#include "tool.h"

uint8_t efuse_mac[6];
int time_out(uint32_t* time_start, uint32_t timeout_ms)
{
    uint32_t time_new = os_get_sys_time();
    if(time_new - *time_start > timeout_ms)
    {
        *time_start = time_new;
        return 1;
    }
    return 0;
}

void printf_chill_time(uint8_t chill_time, uint16_t type)
{
    static size_t last_time[24] = {0};
    static uint16_t cnt[24] = {0};
    static uint32_t type_cnt_time[24] = {0};
    size_t now_time = os_get_sys_time();
    cnt[chill_time]++;
    type_cnt_time[chill_time] += now_time - last_time[chill_time];
    if(cnt[chill_time] % type == 0 && type_cnt_time[chill_time] / type >= now_time - last_time[chill_time] - 1)
    {
        SYS_LOG_INF("TIME_CHILL%d : %d : %d\n",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
        cnt[chill_time] = 0;
        type_cnt_time[chill_time] = 0;
    }
    else if(cnt[chill_time] % type == 0)
    {
        SYS_LOG_WRN("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
        cnt[chill_time] = 0;
        type_cnt_time[chill_time] = 0;
    }
    last_time[chill_time] = now_time;
}

uint32_t parse_hex_or_dec(const char *s) {
    if (!s) return 0;
    if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) return (uint32_t)strtoul(s+2, NULL, 16);
    return (uint32_t)strtoul(s, NULL, 0);
}

void aes_test(void)
{
    // if(!esp_efuse_mac_get_default_id(efuse_mac))
    //     printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac[0], efuse_mac[1], efuse_mac[2], efuse_mac[3], efuse_mac[4], efuse_mac[5]);

    // const unsigned char key[16] = "1234567890abcdef"; // 128-bit key
    // unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
    // unsigned char stream_block[16] = {0};  // 内部缓冲
    // size_t nc_off = 0;

    // const unsigned char input[] = "Hello AES-CTR on ESP32!";
    // unsigned char output[64] = {0};

    // mbedtls_aes_context aes;
    // mbedtls_aes_init(&aes);
    // mbedtls_aes_setkey_enc(&aes, key, 128);

    // // 加密
    // mbedtls_aes_crypt_ctr(&aes, sizeof(efuse_mac), &nc_off,
    //                       nonce_counter, stream_block, efuse_mac, output);

    // printf("Ciphertext (hex): ");
    // for (int i = 0; i < sizeof(efuse_mac); i++)
    //     printf("%02X", output[i]);
    // printf("\n");

    // // 解密（同一函数）
    // unsigned char decrypted[64] = {0};
    // nc_off = 0;
    // memset(nonce_counter, 0, 16);
    // memset(stream_block, 0, 16);

    // mbedtls_aes_crypt_ctr(&aes, sizeof(efuse_mac), &nc_off,
    //                       nonce_counter, stream_block, output, decrypted);

    // printf("Decrypted: %s\n", decrypted);

    // mbedtls_aes_free(&aes);
    

    if(!esp_efuse_mac_get_default_id(efuse_mac))
        printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac[0], efuse_mac[1], efuse_mac[2], efuse_mac[3], efuse_mac[4], efuse_mac[5]);
    
    uint8_t efuse_mac_encrypt[6] = {0};
    sertrf_aes_ctr_encrypt(efuse_mac, 6, efuse_mac_encrypt);
    printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac_encrypt[0], efuse_mac_encrypt[1], efuse_mac_encrypt[2], efuse_mac_encrypt[3], efuse_mac_encrypt[4], efuse_mac_encrypt[5]);

    uint8_t efuse_mac_decrypt[6] = {0};
    sertrf_aes_ctr_decrypt(efuse_mac_encrypt, 6, efuse_mac_decrypt);
    printf("mac: %02X:%02X:%02X:%02X:%02X:%02X\n", efuse_mac_decrypt[0], efuse_mac_decrypt[1], efuse_mac_decrypt[2], efuse_mac_decrypt[3], efuse_mac_decrypt[4], efuse_mac_decrypt[5]);
}

void sertrf_aes_ctr_encrypt(uint8_t *data, uint32_t len,uint8_t* output)
{
    size_t nc_off = 0;
    unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
    unsigned char stream_block[16] = {0};  // 内部缓冲

    const unsigned char key[16] = PRIVATE_KEY; // 128-bit key

    mbedtls_aes_context aes;
    mbedtls_aes_init(&aes);
    mbedtls_aes_setkey_enc(&aes, key, 128);

    // 加密
    mbedtls_aes_crypt_ctr(&aes, len, &nc_off,
                          nonce_counter, stream_block, data, output);
    mbedtls_aes_free(&aes);
}
void sertrf_aes_ctr_decrypt(uint8_t *data, uint32_t len,uint8_t* output)
{
    size_t nc_off = 0;
    unsigned char nonce_counter[16] = {0}; // 初始计数器块 (可用随机数 + 计数)
    unsigned char stream_block[16] = {0};  // 内部缓冲

    const unsigned char key[16] = PRIVATE_KEY; // 128-bit key

    mbedtls_aes_context aes;
    mbedtls_aes_init(&aes);
    mbedtls_aes_setkey_enc(&aes, key, 128);

    mbedtls_aes_crypt_ctr(&aes, len, &nc_off,
                          nonce_counter, stream_block, data, output);
    mbedtls_aes_free(&aes);
}
esp_err_t esp_efuse_mac_get_default_id(uint8_t *mac)
{
    esp_err_t err = esp_efuse_mac_get_default(mac);
    if (err != ESP_OK) {
        return err;
    }
#if CONFIG_SOC_IEEE802154_SUPPORTED
    return insert_mac_ext_into_mac(mac);
#else
    return ESP_OK;
#endif
}