#include    "goahead.h"
#include    "libipmi_IPM.h"
#include    "com_IPMIDefs.h"
#include    "ResultUtils.h"
#include    "cJSON.h"
#include    "libipmi_storlead_OEM.h"
#include    "libsensor.h"
#include    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

static uint8_t progress = 0;
/* 进入固件更新模式
* 杀死所有其它进程
*/
void prepareDevice(Webs *wp)
{
    printf("Update Firmware: Prepare Device...\n");
    //TODO: kill all other process
    progress = 0;
    sleep(1);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    //websWriteHeader(wp, "","prepareDevice");
    websWriteEndHeaders(wp);
    //websWrite(wp, "%s", pStr);
    //websFlush(wp, 0);
    websDone(wp);
    printf("Update Firmware: Prepare Device Ok.\n");
}

/* 接收上传的固件
*/
void uploadFirmware(Webs *wp)
{
    WebsKey         *s;
    WebsUpload      *up;
    char            *upfile;
   
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteHeader(wp, "Content-Type", "text/plain");
    websWriteEndHeaders(wp);
    if (scaselessmatch(wp->method, "POST")) {
        for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) {
            up = s->content.value.symbol;
            websWrite(wp, "FILE: %s\r\n", s->name.value.string);
            websWrite(wp, "FILENAME=%s\r\n", up->filename);
            websWrite(wp, "CLIENT=%s\r\n", up->clientFilename);
            websWrite(wp, "TYPE=%s\r\n", up->contentType);
            websWrite(wp, "SIZE=%d\r\n", up->size);
            upfile = sfmt("%s/tmp/%s", websGetDocuments(), "tmp.uImage"/*up->clientFilename*/);
            printf("upfile: %s\n", upfile);
            if (rename(up->filename, upfile) < 0) {
                error("Cannot rename uploaded file: %s to %s, errno %d", up->filename, upfile, errno);
            }
            wfree(upfile);
        }
        websWrite(wp, "\r\nVARS:\r\n");
        for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) {
            websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string);
        }
    }
    websDone(wp);
    printf("Update Firmware: Upload Firmware oK.\n");
}

/*
    开始执行固件更新
    擦除Flash，写Flash
*/
void updateFlash(Webs *wp)
{
    printf("Update Firmware: Update Flash...\n");
    //TODO: 
    

    sleep(1);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    //websWriteHeader(wp, "updateFlash");
    websWriteEndHeaders(wp);
    //websWrite(wp, "%s", pStr);
    //websFlush(wp, 0);
    websDone(wp);
}


/*
    获取Flash更新的进度
    返回 Flash更新的百分比。
*/
void getUpdateProgress(Webs *wp)
{
    uint8_t progressStr[5] = {0};
    //TODO: 
    if(progress < 100)
        progress++;
    sprintf(progressStr, "%d%%", progress);
    printf("Update Firmware: %s\n", progressStr);

    char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON_AddStringToObject(root, "msg", progressStr);
    cJSON_AddNumberToObject(root, "code", 200);
    

    pStr = cJSON_PrintUnformatted(root);

    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    //websWriteHeader(wp, "getUpdateProgress");
    websWriteEndHeaders(wp);
    websWrite(wp, "%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}

void resetBmc(Webs *wp)
{
    printf("Update Firmware: Reset BMC...\n");
    //TODO: 
    if(progress < 100)
        progress++;

    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    //websWriteHeader(wp, "resetBmc");
    websWriteEndHeaders(wp);
    //websWrite(wp, "%s", pStr);
    //websFlush(wp, 0);
    websDone(wp);
}




// void getDeviceInfo(Webs *wp){
   
//     char *pStr;
//     cJSON * root =  cJSON_CreateObject();
//     cJSON * data =  cJSON_CreateObject();
//     cJSON_AddItemToObject(root, "data", data);//根节点下添加
//     cJSON_AddStringToObject(root, "msg", "");
//     cJSON_AddNumberToObject(root, "code", 200);
//     cJSON_AddStringToObject(data, "buildTime", BuildTime);
//     cJSON_AddStringToObject(data, "fwVersion", FwVersion);
//     cJSON_AddStringToObject(data, "macAddr", MacAddr);
//     cJSON_AddStringToObject(data, "ipAddr", IpAddr);
//     cJSON_AddStringToObject(data, "netMode", "static");

//     pStr = cJSON_PrintUnformatted(root);

//     printf("---> cJSON Str:\n%s\n", pStr);
//     websSetStatus(wp, 200);
//     websWriteHeaders(wp, -1, 0);
//     websWriteEndHeaders(wp);
//     websWrite(wp, "%s", pStr);
//     websFlush(wp, 0);
//     websDone(wp);

//     if(pStr)
//         wfree(pStr);
//     if(root)
//         cJSON_Delete(root);

// }
