/*
 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws.
 * The information contained herein is confidential and proprietary to
 * MediaTek Inc. and/or its licensors.
 * Except as otherwise provided in the applicable licensing terms with
 * MediaTek Inc. and/or its licensors, any reproduction, modification, use or
 * disclosure of MediaTek Software, and information contained herein, in whole
 * or in part, shall be strictly prohibited.
*/

#include <command.h>
#include <common.h>
#include <stdio.h>
#include <debug_impl.h>
#include <utility.h>
#include <dm.h>
#include <led.h>
#include <dm/uclass-internal.h>
#include <iniparser.h>
#include <malloc.h>
#include <sony_customer.h>

#define CMD_BUF 128

#define LED_INI_PATH        "/config/led.ini"
#define INI_INFO_SIZE        5

/*****
[led]
ui_respond          = 1
ui_voice            = 1
tally_status        = 0
ui_respond_default  = 1
ui_voice_default    = 1
tally_default       = 0
*****/

//static int ui_respond    = 0;
//static int ui_voice      = 0;
//static int tally_status  = 0;

char ui_respond[INI_INFO_SIZE]="1";
char ui_voice[INI_INFO_SIZE]="1";
char tally_status[INI_INFO_SIZE]="0";

int get_key_response(void)
{
	if (strcmp(ui_respond, "1") == 0)
	{
		return 1;
	}else{
		return 0;
	}
}

///INI file 
int get_led_info(void)
{
    loff_t size;
    int ret;
    unsigned char *ini_file_buf;
    iniparser_handle_t ini_handle = NULL;
    struct section *section_entry;
//"/mnt/vendor/persist/config/led.ini"
    ini_file_buf = read_storage_file_to_memory("persist", LED_INI_PATH, &size);
    if(ini_file_buf == NULL)
    {
        free(ini_file_buf);
        UBOOT_ERROR("Error: Read led ini file to DRAM failure\n");
        return -1;
    }

    ret = iniparser_create(ini_file_buf, size, &ini_handle);
    free(ini_file_buf);
    if (ret < 0) {
        UBOOT_ERROR("Error: parse ini  failure!\n");
        return -1;
    }

    ret = iniparser_get_section(ini_handle, "led", &section_entry);
    if (ret < 0) {
        iniparser_destroy(ini_handle);
        UBOOT_ERROR("Error: get ini section[%s] failure\n", "led");
        return -1;
    }

    //iniparser_getint(section_entry, "ui_respond", 0, &ui_respond);
    //iniparser_getint(section_entry, "ui_voice", 0,   &ui_voice);
    //iniparser_getint(section_entry, "tally_status", 0, &tally_status);

    iniparser_getstring(section_entry, "ui_respond", "", ui_respond, INI_INFO_SIZE);
    iniparser_getstring(section_entry, "ui_voice", "", ui_voice, INI_INFO_SIZE);
    iniparser_getstring(section_entry, "tally_status", "", tally_status, INI_INFO_SIZE);
    
    iniparser_destroy(ini_handle);
    return 0;
}

/***********
///if is json file
static void get_token_value(jsmntok_t tok, const char * jstr, char * buf) {
    strncpy(buf, jstr + tok.start, tok.end - tok.start);
    buf[tok.end - tok.start] = '\0';
}

int get_led_info(void)
{

    int r;
    char *file_buf = NULL;
    char *my_buf = NULL;
    loff_t size;
    jsmn_parser p;
    jsmntok_t t[512];
    char tokbuf_key[64] = "\0";
    char tokbuf_value[64] = "\0";
    jsmn_init(&p);

    printf("led Json IN\n");
    my_buf = (char *) read_storage_file_to_memory("persist", "/led.json", &size);

    if(my_buf == NULL){
        printf("Error to get led.json in persist\n");
    } else {
        file_buf = (char *)malloc(size + 1);
        memcpy(file_buf, my_buf, size);
        file_buf[size] = '\0';
        free(my_buf);
        printf("Success to get led.json parse in /persist \n");
        r = jsmn_parse(&p, file_buf, strlen(file_buf), t, sizeof(t) / sizeof(t[0]));
        if (r < 0) {
            return CMD_RET_FAILURE;
        }

        // Assume the top-level element is an object 
        if (r < 1 || t[0].type != JSMN_OBJECT) {
            return CMD_RET_FAILURE;
        }
        // get KEY: ui_respond 
        get_token_value(t[1], file_buf, tokbuf_key);
        if (strcmp(tokbuf_key, "ui_respond") == 0) {
            get_token_value(t[2], file_buf, tokbuf_value);
            ui_respond = simple_strtoul(tokbuf_value, NULL, 10);
        }

        get_token_value(t[4], file_buf, tokbuf_key);
        if (strcmp(tokbuf_key, "tally_status") == 0) {
              get_token_value(t[5], file_buf, tokbuf_value);
              tally_status = simple_strtoul(tokbuf_value, NULL, 10);
        }
        printf("ui_respond =%d, tally_status=%d\n", ui_respond, tally_status);
        free(file_buf);

    }

    return CMD_RET_SUCCESS;
}
********/

int do_led_info_check(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{

    int ret = 0 ;
    char buffer[CMD_BUF] = "\0";
    int snprintf_len;
        /* Validate arguments */
    if (argc < 2)
        return CMD_RET_USAGE;
    ret = get_led_info();
    if (ret< 0) {   
        printf("getStatus error !\n");
    }

    if (snprintf(buffer, CMD_BUF, "tally_status=%s", tally_status) < 0)
    {
         UBOOT_ERROR("cannot generate %s from %s\n",
                        "tally_status",tally_status);
        return CMD_RET_FAILURE;
    }
//    add_bootargs("tally_status", buffer, 0);

/*
//use leds-mtk-t6y.key_response for led driver get key_response
    if (snprintf(buffer, CMD_BUF, "key_response=%s", ui_respond) < 0)
    {
        UBOOT_ERROR("cannot generate %s from %s\n",
                        "key_response", ui_respond);
        return CMD_RET_FAILURE;
    }
    add_bootargs("key_response", buffer, 0);
*/

    if (snprintf(buffer, CMD_BUF, "leds-mtk-t6y.key_response=%s", ui_respond) < 0)
    {
        UBOOT_ERROR("cannot generate %s from %s\n",
                        "leds-mtk-t6y.key_response", ui_respond);
        return CMD_RET_FAILURE;
    }
    //add_bootargs("leds-mtk-t6y.key_response", buffer, 0);

    if(!strcmp(argv[1], "0")) {
        UBOOT_DEBUG("ui_respond = %s\n", ui_respond);
        UBOOT_DEBUG("ui_voice = %s\n", ui_voice);
        UBOOT_DEBUG("tally_status = %s\n",tally_status);
    }
    else if (!strcmp(argv[1], "1")) {
        if(!strcmp(ui_respond, "1")){
            printf("need set white power led on \n");
            memset(buffer, 0, CMD_BUF);
            snprintf_len = snprintf(buffer, CMD_BUF, "led pwm_led0 oneshot");
            if (snprintf_len >= CMD_BUF)
            {
                UBOOT_ERROR("The array size is too small(%d), snprintf fail '%s'\n", CMD_BUF, buffer);
                return -1;
            }
            UBOOT_DEBUG("cmd=%s\n", buffer);
            run_command(buffer, 0);
        }
    }
    else if (!strcmp(argv[1], "2")) {
        if(!strcmp(tally_status, "1")){
            printf("need set tally led on \n");
            memset(buffer, 0, CMD_BUF);
            snprintf_len = snprintf(buffer, CMD_BUF, "led pwm_led1 on");
            if (snprintf_len >= CMD_BUF)
            {
                UBOOT_ERROR("The array size is too small(%d), snprintf fail '%s'\n", CMD_BUF, buffer);
                return -1;
            }
            UBOOT_DEBUG("cmd=%s\n", buffer);
            run_command(buffer, 0);
        }
    }
    else {
         UBOOT_DEBUG("do_led_info_check Error input, try again\n");
    }

    return 0;
}

U_BOOT_CMD(
    led_info_check, 2, 1, do_led_info_check,
    "manage LEDs \n",
    "command: led_status_check\n"
    "led_info_check 0 \t   show the of led info\n"
    "led_info_check 1 \t do while led by the status\n"
    "led_info_check 2 \t do tally led by the status\n"
);
