/* =========================================================================

DESCRIPTION
   Netease && Xunfei speech processing main program
   Cae voice process

Copyright (c) 2017 by Netease, Co,LTD.  All Rights Reserved.
============================================================================ */

/* =========================================================================

                             REVISION

when                       who why
--------                    ---------
-------------------------------------------
2017/06/28          wangzijiao                                  Created.
============================================================================ */

/* ------------------------------------------------------------------------
** Includes
** ------------------------------------------------------------------------ */
#include "base.h"
#include "error.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uvdbus/libuv_dbus.h>

/* ------------------------------------------------------------------------
** Macros
** ------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------
** Defines
** ------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------
** Types
** ------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------
** Global Variable Definitions
** ------------------------------------------------------------------------ */
static pthread_mutex_t lock;
static char *g_pChipId = "";
static char g_uuid[LEN_UUID] = {0};
static char g_yx_token[LEN_TOKEN] = {0};
static char g_songInfo[LEN_SONGINFO] = {0};
static char g_binduser[LEN_UUID] = {0};
static VadStatus vad_status = 0;
static int voiceAng = 0;

static char voice_wakeupword[20];
static int voice_major;
static int voice_status;
static double voice_confidence;
static VoiceSessionStatus vss = VoiceSession_Init;
static WorkMode wm = WorkMode_R311_PV1;

/* ------------------------------------------------------------------------
** Function Definitions
** ------------------------------------------------------------------------ */
void setWorkMode(WorkMode mode) {
    wm = mode;
}

WorkMode getWorkMode() {
    return wm;
}
void setVss(VoiceSessionStatus st) { vss = st; }
VoiceSessionStatus getVss() { return vss; }

void setVoiceWakeupword(char *word) {
    strncpy(voice_wakeupword, word, sizeof(voice_wakeupword));
}
char *getVoiceWakeupword() { return voice_wakeupword; }

void setVoiceMajor(int major) { voice_major = major; }
int getVoiceMajor() { return voice_major; }

void setVoiceStatus(int status) { voice_status = status; }
int getVoiceStatus() { return voice_status; }

void setVoiceConfidence(double voiceConfidence) {
    voice_confidence = voiceConfidence;
}
double getVoiceConfidence() { return voice_confidence; }

void setAng(int ang) { voiceAng = ang; }

int getAng() { return voiceAng; }

int getVadStatus() { return vad_status; }
void setVadStatus(VadStatus status) { vad_status = status; }

void BaseInit() {
    memset(g_uuid, 0, sizeof(g_uuid));
    memset(g_songInfo, 0, sizeof(g_songInfo));
    g_pChipId = GetCpuSerial();
    pthread_mutex_init(&lock, NULL);
}
void BaseLock() { pthread_mutex_lock(&lock); }
void BaseUnlock() { pthread_mutex_unlock(&lock); }

void printHex(char *buf, size_t len) {
    int i = 0;
    n_toast("--------------------------------------------\n");
    for (; i < len; i++) {
        n_toast("%02x ", buf[i]);
        if ((i + 1) % 16 == 0) {
            n_toast("\n");
        }
    }
    n_toast("\n--------------------------------------------\n");
}

static char *_strncpy(char *dest, const char *src, size_t n) {
    size_t i;
    for (i = 0; i < n && src[i] != '\0'; i++) {
        dest[i] = src[i];
        // n_toast("%d, %x -> %x\n", i, src[i], dest[i]);
    }
    for (; i < n; i++) {
        // n_toast("%d set 0\n", i);
        dest[i] = '\0';
    }

    return dest;
}

static void baseCopy(char *src, size_t srcLen, char *dest, size_t destCap) {
    // size_t cpLen = srcLen < destCap ? srcLen : destCap;

    if (srcLen > destCap || srcLen == 0) {
        n_error("copy error:beyond the max cap, %d > %d\n", srcLen, destCap);
        n_debug("Copy src:%s\n", src);
        return;
    }
    // n_toast("src: %s\n", src);
    // printHex(src, srcLen + 1);
    _strncpy(dest, src, destCap);
    // n_toast("dest: %s\n", dest);
    // printHex(dest, destCap);
    // n_debug("Copy dest:%s\n", dest);
}

void setUuid(char *uuid, size_t len) {
    baseCopy(uuid, len, g_uuid, sizeof(g_uuid));
}
char *getUuid() { return (char *)g_uuid; }

char *getChipId() { return g_pChipId; }

void setYxToken(char *token, size_t len) {
    baseCopy(token, len, g_yx_token, sizeof(g_uuid));
}
char *getYxToken() { return (char *)g_yx_token; }

void setSongInfo(char *songInfo, size_t len) {
    baseCopy(songInfo, len, g_songInfo, sizeof(g_songInfo));
}

char *getSongInfo() { return (char *)g_songInfo; }

void setBindUser(char *user, size_t len) {
    baseCopy(user, len, g_binduser, sizeof(g_binduser));
}

char *getBindUser() { return (char *)g_binduser; }

void resetAdc() {
    n_debug("Begin to reset adc!\n");
    char * cmd = "1";
    int fd = open("/sys/netease/cpld_control/cpld_init", O_RDWR);
    write(fd, cmd, strlen(cmd));
    close(fd);
}