#include "ATCmdInterpreter.h"
#include "SIM7080GInterface.h"
#include "define.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "SIM7080GInterface.h"

bool mCAStateReceived;
char mMasterData[100];
int mMasterDataSize;
char mInputString[100];
bool mIsDataPending;

void InitATCmdInterpreter()
{
    mCAStateReceived = false;
    mIsDataPending = false;
}

int AnalyzeNewATString(char* Str, int StrLen, int CurCmd)
{
    memset(mInputString,0,100);
    memcpy(mInputString,Str,StrLen);
    switch(CurCmd)
    {
        case LTE_MODULE_CONNECT_APN_CMD:        //AT+CNACT=0,1
        {
            if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_DISCONNECT_APN_CMD:
        {
            if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_CHECK_APN_CONNECTION_CMD:
        {
            if(strncmp("+CNACT:",mInputString,strlen("+CNACT:")) == 0) //+CNACT: 0,1,"10.177.232.192","2605:8D80:5C0:3EED:D90E:5B72:BC1B:281"
            {
                if(mInputString[8] == '0')    //Only consider port 0
                {
                    if(mInputString[10] == '1' || mInputString[10] == '2')
                    {
                        LTEModuleAPNConnectionStatus(LTE_MODULE_APN_CONNECTED);
                    }
                    else
                    {
                        LTEModuleAPNConnectionStatus(LTE_MODULE_APN_DISCONNECTED);
                    }
                }
            }
            else if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_CONNECT_TO_MASTER_CMD:
        {
            if(strncmp("+CAOPEN:",mInputString,strlen("+CAOPEN:")) == 0)
            {
                int result = atoi(&mInputString[11]);
                if(StrLen >= 12 && mInputString[11] == '0')    //The connection on port 0 is established
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_CONNECTED);
                }
                else if(result == 1)    //socket error, probably caused by APN not connected..
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_SOCKET_ERROR);
                }
                else
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_DISCONNECTED);
                }
            }
            else if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_DISCONNECT_FROM_MASTER_CMD:
        {
            if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_CHECK_MASTER_CONNECTION_CMD:
        {
            if(strncmp("+CASTATE:",mInputString,strlen("+CASTATE:")) == 0)  //+CASTATE: 0,0
            {
                mCAStateReceived = true;
                if(mInputString[12] == '1')    //The connection is established
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_CONNECTED);
                }
                else
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_DISCONNECTED);
                }
            }
            else if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                if(mCAStateReceived == false)
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_DISCONNECTED);
                }
                mCAStateReceived = false;
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                mCAStateReceived = false;
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_RX_DATA_CMD:
        {
            if(strncmp("+CARECV:",mInputString,strlen("+CARECV:")) == 0)  //+CARECV: 4,test
            {
                //         printf("ATCmdInterpreter : Received +CARECV, storing data in buffer, waiting for OK\n\r");
                memset(mMasterData,0,100);
                mMasterDataSize = 0;
                char *token;
                //  sscanf("%d,%s",&mInputString[9],&DataSize,Data);
                token = strtok(&mInputString[9],",");
                if(token != NULL)
                {
                    mMasterDataSize = atoi(token);
                    if(mMasterDataSize < 100)
                    {
                        token = strtok(NULL, ",");
                        if(token != NULL)
                        {
                            memcpy(mMasterData,token,mMasterDataSize);
                        }
                    }
                }
                
            }
            if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                //                printf("ATCmdInterpreter : Received OK, calling LTEModuleDataReceived\n\r");
                LTEModuleDataReceived(mMasterData,mMasterDataSize);
                //                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
            }
            break;
        }
        case LTE_MODULE_TX_DATA_CMD:
        {
            if(strncmp(mInputString,"OK",2) == 0)    //The command was executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_OK);
                if(mIsDataPending == true)
                {
                    mIsDataPending = false;
                    LTEModuleNewDataReady();
                }
            }
            else if(strncmp(mInputString,"ERROR",5) == 0)    //The command was not executed
            {
                LTECmdResponseReceived(CurCmd,LTE_MODULE_RESULT_ERROR);
                if(mIsDataPending == true)
                {
                    mIsDataPending = false;
                    LTEModuleNewDataReady();
                }
            }
            if(strncmp("+CADATAIND:",mInputString,strlen("+CADATAIND:")) == 0)
            {
                //                We sometimes receive data while sending... 
                mIsDataPending = true;
            }
            break;
        }
        
        
        case LTE_MODULE_NO_CMD:  //+CADATAIND:
        default:
        {
            if(strncmp("+CADATAIND:",mInputString,strlen("+CADATAIND:")) == 0)
            {
                //                printf("ATCmdInterpreter : Received +CADATAIND, calling NewDataReady\n\r");
                LTEModuleNewDataReady();
            }
            if(strncmp("+APP PDP:",mInputString,strlen("+APP PDP:")) == 0)
            {
                // LTEModuleNewDataReady();
            }
            if(strncmp("+CASTATE:",mInputString,strlen("+CASTATE:")) == 0)  //+CASTATE: 0,0
            {
                if(mInputString[12] == '1')    //The connection is established
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_CONNECTED);
                }
                else
                {
                    LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_DISCONNECTED);
                }
            }
            if(strncmp("+CAURC:",mInputString,strlen("+CAURC:")) == 0)
            {
                //                printf("ATCmdInterpreter : Received +CADATAIND, calling NewDataReady\n\r");
                //LTEModuleReset();
                LTEModuleMasterConnectionStatus(LTE_MODULE_MASTER_DISCONNECTED);
            }
            break;  
        }
        
    }
}