/* 
 * File:   SIM7080GInterface.h
 * Author: JF
 *
 * Created on August 13, 2024, 1:43 PM
 */

#ifndef SIM7080GINTERFACE_H
#define	SIM7080GINTERFACE_H

#include "define.h"

#define LTE_MODULE_POWERUP_TIMEOUT 2000
#define LTE_MODULE_WAIT_INIT_TIMEOUT 5000
#define LTE_MODULE_CONNECTION_CHECK_TIMEOUT 5000
#define LTE_MODULE_MAX_CMD_LENGTH 128
#define LTE_MODULE_MAX_MASTER_CONNECT_RETRY 5
#define LTE_MODULE_MAX_COMM_TIMEOUT 10000

enum eLTEModuleCommands
{
    LTE_MODULE_NO_CMD = 0,
    LTE_MODULE_CONNECT_APN_CMD,
    LTE_MODULE_DISCONNECT_APN_CMD,
    LTE_MODULE_CONNECT_TO_MASTER_CMD,
    LTE_MODULE_DISCONNECT_FROM_MASTER_CMD,
    LTE_MODULE_RX_DATA_CMD,
    LTE_MODULE_TX_DATA_CMD,
    LTE_MODULE_CHECK_APN_CONNECTION_CMD,
    LTE_MODULE_CHECK_MASTER_CONNECTION_CMD,
    
    LTE_MODULE_MAX_CMD
};

enum eLTEModuleCmdResult
{
    LTE_MODULE_RESULT_OK,
    LTE_MODULE_RESULT_ERROR
};

enum eLTEModuleAPNConnectionStatus
{
    LTE_MODULE_APN_DISCONNECTED,
    LTE_MODULE_APN_CONNECTED
};

enum eLTEModuleMasterConnectionStatus
{
    LTE_MODULE_MASTER_DISCONNECTED,
    LTE_MODULE_MASTER_CONNECTED,
    LTE_MODULE_MASTER_SOCKET_ERROR
};


enum eLTEModuleStates
{
    LTE_MODULE_SM_INIT_STATE,
    LTE_MODULE_SM_RESET_STATE,
    LTE_MODULE_SM_POWERUP_STATE,
    LTE_MODULE_SM_WAIT_RDY_STATE,
    LTE_MODULE_SM_SETECHOOFF_STATE,
    LTE_MODULE_SM_SETAPN_STATE,
    LTE_MODULE_SM_DELAY_STATE,
    LTE_MODULE_SM_RUN_STATE,
    LTE_MODULE_SM_PASSTHROUGH_STATE,
    
    LTE_MODULE_SM_MAX_STATE
            
};

enum eLTEModuleEvents
{
    LTE_MODULE_SM_TICK_EVENT,
    LTE_MODULE_RX_DATA_EVENT,
    LTE_MODULE_RX_RDY_EVENT,
    LTE_MODULE_ENTER_PASSTHROUGH_EVENT,
    LTE_MODULE_EXIT_PASSTHROUGH_EVENT,
    
    LTE_MODULE_MAX_EVENT
    
};

enum eLTEModuleOperationStates
{
    LTE_MODULE_OPERATION_INIT_STATE,
    LTE_MODULE_OPERATION_WAIT_CONNECT_APN_STATE,
    LTE_MODULE_OPERATION_CHECK_APN_STATE,
    LTE_MODULE_OPERATION_WAIT_CONNECT_SERVER_STATE,
    LTE_MODULE_OPERATION_WAIT_DISCONNECT_SERVER_STATE,
    LTE_MODULE_OPERATION_STANDBY_STATE,
    LTE_MODULE_OPERATION_WAIT_FOR_DATA_STATE,
    LTE_MODULE_OPERATION_SENDING_DATA_STATE,
    
    LTE_MODULE_OPERATION_MAX_STATE
};

enum eLTEModuleOperationSubStates
{
    LTE_MODULE_OPERATION_NOMINAL_SUBSTATE,
    LTE_MODULE_OPERATION_RECONNECT_MASTER_SUBSTATE,
    LTE_MODULE_OPERATION_CHECK_APN_SUBSTATE,
    LTE_MODULE_OPERATION_RECONNECT_APN_SUBSTATE,
    
    LTE_MODULE_OPERATION_MAX_SUBSTATE
};

enum eLTEModuleOperationSMEvents
{
    LTE_MODULE_OP_TICK_EVENT,
    LTE_MODULE_OP_APN_CONNECTED_EVENT,
    LTE_MODULE_OP_APN_DISCONNECTED_EVENT,
    LTE_MODULE_OP_APN_CONN_FAILED_EVENT,
    LTE_MODULE_OP_APN_CONN_OK_EVENT,
    LTE_MODULE_OP_SERVER_CONNECTED_EVENT,
    LTE_MODULE_OP_SERVER_DISCONNECTED_EVENT,
    LTE_MODULE_OP_SERVER_CONN_FAILED_EVENT,
    LTE_MODULE_OP_CMD_RESP_RECEIVED_EVENT,
    LTE_MODULE_OP_MASTER_DATA_READY_EVENT,
    LTE_MODULE_OP_MASTER_DATA_RECEIVED_EVENT,
    LTE_MODULE_OP_MASTER_SEND_DATA_REQUEST_EVENT,
    LTE_MODULE_OP_MASTER_DATA_SENT_EVENT,
    
    LTE_MODULE_OP_MAX_EVENT
};



void InitLTEModule(bool FunctionnalReset);
void LTEModuleSM(int SMEvent);
void TickLTEModule();
void LTEModuleOperationSM(int OperationEvent);
void LTEModuleNewData(unsigned char Data);
void LTEEnterSerialPassthrough();
void LTEExitSerialPassthrough();
void LTEFwdPassthroughData(char Data);
int LTEEnableSerialDebug(bool EnModule, bool EnPic);
int LTESendCommandToModule(int Command);
void SendDataToLteModule(char *Data, int Size);
int LTEModuleReset();
int LTESendDataToMaster(char *Buffer, int Size);
int LTESendMasterBufferToModule();

void LTECmdResponseReceived(int Cmd, int Result);
void LTEModuleAPNConnectionStatus(int Status);
void LTEModuleMasterConnectionStatus(int Status);
void LTEModuleNewDataReady();
void LTEModuleDataReceived(char *Data, int Size);
void LTEModuleMasterCtrlCommStatus(bool Offline);

bool LTEModuleIsMasterConnected();

#endif	/* SIM7080GINTERFACE_H */

