/*
 * Copyright (C) 2017-2019 Alibaba Group Holding Limited
 */

#include "itls/config.h"
#include "itls/net.h"
#include "itls/debug.h"
#include "itls/ssl.h"
#include "itls/hal_itls.h"

#include "ali_crypto.h"

#if defined(ON_DAILY)
#define SERVER_PORT     1883
#define SERVER_NAME     "iot-test-daily.iot-as-mqtt.unify.aliyuncs.com"
#else
#define SERVER_PORT     1883
#define SERVER_NAME     "itls.cn-shanghai.aliyuncs.com"
#endif

unsigned char mqtt_req_data[] = {
    0x10, 0xa7, 0x01, 0x00, 0x04, 0x4d, 0x51, 0x54,
    0x54, 0x04, 0xc2, 0x00, 0x3c, 0x00, 0x59, 0x42,
    0x66, 0x4b, 0x78, 0x42, 0x44, 0x53, 0x6a, 0x57,
    0x43, 0x48, 0x2e, 0x61, 0x6f, 0x73, 0x5f, 0x6d,
    0x71, 0x74, 0x74, 0x5f, 0x74, 0x65, 0x73, 0x74,
    0x7c, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x6d,
    0x6f, 0x64, 0x65, 0x3d, 0x2d, 0x31, 0x2c, 0x74,
    0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70,
    0x3d, 0x32, 0x35, 0x32, 0x34, 0x36, 0x30, 0x38,
    0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x73,
    0x69, 0x67, 0x6e, 0x6d, 0x65, 0x74, 0x68, 0x6f,
    0x64, 0x3d, 0x68, 0x6d, 0x61, 0x63, 0x73, 0x68,
    0x61, 0x31, 0x2c, 0x67, 0x77, 0x3d, 0x30, 0x7c,
    0x00, 0x1e, 0x4c, 0x76, 0x55, 0x4d, 0x52, 0x49,
    0x33, 0x57, 0x39, 0x48, 0x31, 0x50, 0x6d, 0x69,
    0x64, 0x74, 0x4d, 0x39, 0x67, 0x42, 0x30, 0x30,
    0x31, 0x30, 0x61, 0x34, 0x66, 0x62, 0x30, 0x31,
    0x00, 0x20, 0x30, 0x34, 0x30, 0x66, 0x36, 0x32,
    0x36, 0x63, 0x34, 0x39, 0x33, 0x38, 0x34, 0x65,
    0x33, 0x30, 0x61, 0x62, 0x35, 0x63, 0x31, 0x66,
    0x61, 0x64, 0x31, 0x32, 0x63, 0x39, 0x61, 0x39,
    0x66, 0x63
};

static int itls_hal_sample(const char *host, int port,
         const char *product_key, const char *product_secret, int debug_level)
{
    int ret = 0, len;
    char version[16] = {0};
    unsigned char buf[1024];
    uintptr_t handle;

    hal_itls_get_version(version);
    SSL_DBG_LOG("itls version: %s\n", version);

    hal_itls_set_debug_level(debug_level);

    handle = hal_itls_establish(host,
                 port, product_key, product_secret);
    if (handle == (uintptr_t)NULL) {
        SSL_DBG_LOG("itls establish fail\n");
        ret = -1;
        goto _out;
    }

    len = sizeof(mqtt_req_data);
    memcpy(buf, mqtt_req_data, len);
    ret = hal_itls_write(handle, (char *)buf, len, 2000);
    if (ret < 0) {
        SSL_DBG_LOG("itls write fail, %d\n", ret);
        goto _out;
    }

    SSL_DBG_LOG("%d bytes written\n", ret);

    ret = hal_itls_read(handle, (char *)buf, len, 2000);
    if (ret < 0) {
        SSL_DBG_LOG("itls read fail\n");
        goto _out;
    }

    SSL_DBG_LOG("%d bytes read\n", ret);

    ret = 0;

_out:
    SSL_DBG_LOG("hal itls alert type: %d\n", hal_itls_get_alert_type());

    hal_itls_destroy(handle);

    return ret;
}

int itls_client_sample(
         char *product_key, char *product_secret, int debug_level)
{
    int ret;
    int port = SERVER_PORT;
    char host[128] = {0};

#if defined(ON_DAILY)
    ls_osa_snprintf(host, 128, "%s",SERVER_NAME);
#else
    ls_osa_snprintf(host, 128, "%s.%s", product_key, SERVER_NAME);
#endif

    ret = itls_hal_sample(host, port,
               product_key, product_secret, debug_level);
    if (ret < 0) {
        return -1;
    }

    return 0;
}


