#include "globalDef.h"

CurTick g_curTick; // 时间信息
RunData g_runData; //运行数据
FirmwareMsg g_firmwareMsg; // 网关 采集器固件信息存储
ReadCollcmd g_readCmd83; // 上电第一次读取0x83指令时的状态
ReadCollcmd g_readCmd58; // 读取58指令
ReadCollcmd g_readCmd56;  // 读取56指令
dacommQueue g_commQueue;
FirmwareExpl g_firmwareExpl; //固件信息版本格式
Da4G g_data4G; // 4G模块连接情况
LedStatus g_ledStatus; // led灯的情况
char g_upLinkTopic[64] = {0};
char g_logupLinkTopic[64] = {0};
GunStatus g_checkGun[16];
LoraCollMsg g_loraCollMsg;
key_func_t key_info;
timeout_t g_taxSendTime;
timeout_t g_taxDataTime; // 报税器读取57指令时，86 89 中间要有的延时
cmdInfo83 g_cmdInfo83;
updateProg g_updateProg;
EncodeOptDa g_encoderDa;
MqttRunDa g_mqttRunDa;
uint8_t g_bPrintfDebug = 0; // 0：不打印即不执行data_dump函数，1：执行
LogCfigDa g_logCfigDa;
uint8_t g_curSendCmd = 0;
Sm4Key g_sm4Key; // 密钥
/*时间信息初始化*/

/*系统信息*/

/*运行数据*/
#define QUEUE_MAX 16

void init_queue(cmdInfo83 *da)
{
    da->front = 0;
    da->rear = 0;
    da->bread83 = 0;

}
uint8_t en_queue_cmd83(cmdInfo83 *da, uint32_t sn, uint8_t para,uint8_t taxNo, uint8_t flag)
{
    if((da->rear+1)&QUEUE_MAX == da->front){
        printf("queue have no size\r\n");
        return 0;
    }
    da->da[da->rear%QUEUE_MAX].sn = sn;
    da->da[da->rear%QUEUE_MAX].para = para;
    da->da[da->rear&QUEUE_MAX].taxNo = taxNo;
    da->rear++;
    da->bread83 = 1;
    printf("queue size , = %d\r\n",da->rear);
    return 1;
}
uint8_t de_queue_cmd83(cmdInfo83 *inda,da83 *outda)
{

    if(inda->front == (inda->rear%QUEUE_MAX)) return 0; // 队列为空


    outda->sn = inda->da[inda->front].sn;
    outda->para = inda->da[inda->front].para;
    outda->taxNo = inda->da[inda->front].taxNo;
    inda->front = (inda->front+1)%QUEUE_MAX;

    return 1;
}

/*-------------------------------------------------------------
 *  日志上报info 初始化
 * ------------------------------------------------------------*/
void init_log_info(void)
{
    g_logCfigDa.bEnable = 0;
    g_logCfigDa.settimer = 0;
    timeout_setValue(&g_logCfigDa.time,0);
    timeout_stop(&g_logCfigDa.time);
    timeout_setValue(&g_logCfigDa.time2,0);
    timeout_stop(&g_logCfigDa.time2);
}
/*-------------------------------------------------------------
 *  sm4密钥
 * ------------------------------------------------------------*/
void get_sm4_key(void)
{
    g_sm4Key.keyda.flag[0] = 'W';
    g_sm4Key.keyda.flag[1] = 'B';
    g_sm4Key.keyda.flag[2] = 'J';
    g_sm4Key.keyda.flag[3] = 'W';
    g_sm4Key.keyda.flag[4] = 'Z';
    g_sm4Key.keyda.flag[5] = 'Z';
    g_sm4Key.keyda.flag[6] = '7';
    g_sm4Key.keyda.flag[7] = '6';
    g_sm4Key.keyda.flag[8] = '2';
    g_sm4Key.keyda.flag[9] = '9';
    g_sm4Key.keyda.flag[10] = '3';
    g_sm4Key.keyda.flag[11] = '7';

    g_sm4Key.keyda.sn = downlink_config.gateway_id;
}

void init_comm_queue(void)
{
    uint8_t i = 0;

    g_commQueue.front = 0;
    g_commQueue.rear = 0;
    for(i = 0; i<COMM_QUEUE_SZIE;i++){
        memset(g_commQueue.data[i].da,0,64);
        g_commQueue.data[i].size = 0;
    }

}
uint8_t save_comme_queue(uint8_t type1, uint16_t type2,uint8_t *data,uint8_t size)
{
    volatile uint8_t index = 0;

    if((g_commQueue.rear+1)&COMM_QUEUE_SZIE == g_commQueue.front){
        printf("queue have no size\r\n");
        return 0;
    }
    index = g_commQueue.rear%COMM_QUEUE_SZIE;
    memcpy((char*)g_commQueue.data[index].da,(char*)data,size);
    g_commQueue.data[index].type01 = type1;
    g_commQueue.data[index].type02 = type2;
    g_commQueue.data[index].size = size;
    g_commQueue.rear++;
    //data_dump("send111 0x01-0x1052:",data,size);
    printf("queue size = %d, save index = %d\r\n",g_commQueue.rear,index);
    return 1;
}
uint16_t read_comm_queue(uint8_t *type1, uint16_t *type2,uint8_t *outda)
{
    volatile uint8_t index = 0;

    if(g_commQueue.front == (g_commQueue.rear%COMM_QUEUE_SZIE)) {
        g_commQueue.front = 0;
        g_commQueue.rear = 0;
        return 0; // 队列为空

    }
    index = g_commQueue.front;
    *type1 = g_commQueue.data[index].type01;
    *type2 = g_commQueue.data[index].type02;
    //printf("aaaaaaaaa data size = %d\r\n",g_commQueue.data[index].size);
    memcpy(outda,g_commQueue.data[index].da,g_commQueue.data[index].size);
    g_commQueue.front = (g_commQueue.front+1)%COMM_QUEUE_SZIE;

    return g_commQueue.data[index].size;
}

