#include "NetworkInterface.h"
#include "ProtocolDefs.h"
#include "NetworkProtocol.h"
#include "InternalUart.h"
#include "define.h"
#include "BoardCfg.h"
#include "Uart.h"
#include "SPI_Flash.h"
#include "FlashMapping.h"
#include "versionbuild.h"
#include "WiFiCtrl.h"
#include "LoraInterface.h"
//

//enum eWiFiState
//{
//    WIFI_MODULE_OFF_STATE = 0,
//    WIFI_CONNECTED_STATE,
//    WIFI_DISCONNECTED_STATE,
//    WIFI_INIT_ERROR_STATE,
//    WIFI_UNKNOWN_STATE
//};
unsigned char mLoraPreamble[3]={0x00,LORA_MASTER_ADDRESS,LORA_CHANNEL};

static const char mFirmwareVersion[15] = VERSIONNUMBER;
unsigned int mTotalMasterNbRequests = 0;

void ExecuteMasterCommand(int Command, unsigned char *Data, int DataSize)
{
    if(LoraInterfaceIsBusy() == 1)
        return;

	mTotalMasterNbRequests++;
    switch(Command)
    {
		case LORA_IF_GET_STATUS_REQUEST:
		{
            char Payload[20];
            
            char temp;
            
			Payload[0] = LORA_MODULE_MODEL;
			temp = (char)(mModuleInternalAddress & 0xFF);
            Payload[2] = temp;
            temp = (char)((mModuleInternalAddress >> 8) & 0xFF);
            Payload[1] = temp;
            Payload[3] = mModuleUARTParity;
            Payload[4] = mModuleUARTRate;
            Payload[5] = mModuleAirRate;
            Payload[6] = mModuleSubPacket;
            Payload[7] = mModuleRSSIEnabled;
            Payload[8] = mModuleTxPower;
            Payload[9] = mModuleInternalChannel;
            Payload[10] = mModuleRSSIByteEnabled;
            Payload[11] = mModuleTxMethod;
            Payload[12] = mModuleLBTEnabled;
            Payload[13] = mModuleWORCycle;
            Payload[14] = mModuleAmbientRSSI;
            Payload[15] = mModuleLastRxRSSI;

			SendNetworkCommand(LORA_IF_GET_STATUS_RESPONSE,Payload,16);
			break;
		}
		case LORA_IF_SEND_FRAME_REQUEST:
		{
//            //First 4 bytes of data are the payload size.
//            int DataSize = 0;
//            DataSize = (unsigned int)Data[0];
//			DataSize <<= 8;
//            DataSize |= (unsigned int)Data[1];
//			DataSize <<= 8;
//            DataSize |= (unsigned int)Data[2];
//			DataSize <<= 8;
//            DataSize |= (unsigned int)Data[3];
            
            LoraInterfaceTransmitLoRaData(Data,DataSize);
            
			break;
		}
		case LORA_IF_GET_MODULE_CONFIG_REQUEST:
		{
			break;
		}	
		case LORA_IF_GET_RSSI_REQUEST:
		{
			break;
		}
		case LORA_IF_SET_MODULE_CONFIG_REQUEST:
		{
			break;
		}
		case LORA_IF_SET_MODULE_CONFIG_RESPONSE:
		case LORA_IF_GET_RSSI_RESPONSE:
		case LORA_IF_GET_MODULE_CONFIG_RESPONSE:
		case LORA_IF_NEW_FRAME_RESPONSE:
		case LORA_IF_SEND_FRAME_RESPONSE:
		case LORA_IF_GET_STATUS_RESPONSE:
		{
			break;
		}
	}
			
}

void SendNetworkCommand(int Command, unsigned char *Data, unsigned int DataSize)
{
    unsigned char *Payload;
    unsigned int PayloadSize;
    Payload = ProtocolGetFrame(ID_MASTER,MASTER_ADDRESS,ID_LORA_INTERFACE_DEVICE,Command,Data,DataSize,0,&PayloadSize);
    
	UartTransmitData(NETWORK_UART_PORT,Payload,PayloadSize);
    
}

void SendNetworkCommandBlocking(int Command, unsigned char *Data, unsigned int DataSize)
{
    unsigned char *Payload;
    unsigned int PayloadSize;
    //unsigned char DestDevice,unsigned char DestAddress, unsigned char SenderDevice, unsigned char Cmd, unsigned char *Data,unsigned int Size,unsigned char Flags, int *FrameSize)
    Payload = ProtocolGetFrame(ID_MASTER,MASTER_ADDRESS,ID_LORA_INTERFACE_DEVICE,Command,Data,DataSize,0,&PayloadSize);
    
    SendInternalUartDataBlocking(Payload,PayloadSize,NETWORK_UART_PORT);
}

