/*
* MISC_FUNC.c
*
* Created: 13/5/2015
*
* Author: Umar Naeem
*/
#include "MISC_FUNC.h"
const char dearSt[] = "\r\nVehicle # ";
const char infSt[] = "\r\nwww.teresol.org";
const char region[] = " region.";
static unsigned char GPRS_FLAG = FALSE;
/********************************************************************************************************
GSM_StateMachine
This function handles GSM Connection with network and with GPRS and FTP servers.
*********************************************************************************************************/
void GSM_StateMachine(void)
{
GSM_PWR_ON_OFF(ON); // If GSM is Off, Turn on the GSM Module
if(GSM.FTPPUTConnectFlag==FALSE)
{
if(states.GSMFirstInit == FALSE && IAP.Data_Start_Flag == 0)
{
states.GSMFirstInit = GSM_INIT();
GET_IMEI();
// USART_SendData_s( DEBUG_COM,(unsigned char*)"GSMFirstInit\r\n");
}
IWDG_ReloadCounter(); // Reload IWDG counter
states.NetworkInit = GET_GSM_STATUS(); // Check for GSM network Strength and RSSI
if(states.NetworkInit == FALSE || states.GPRSStateOk == FALSE) // Check if GSM network Strength is not
{ // enough or GPRS is not connected
GSM.FTPPUTConnectFlag=FALSE;
states.NoNetworkCounter++;
if(states.NoNetworkCounter >= config.GSMNoNetworkTry)
{
states.GPRSStateOk = FALSE;
states.NoNetworkCounter = 0;
if(config.simShiftEn == TRUE)
{
USART_SendData_s( DEBUG_COM,(unsigned char*)"SIMShift++\r\n");
config.simNumber++;
states.SIMChangeEvent = TRUE;
if(config.simNumber == 3)
{
config.simNumber = 1;
}
INIT_TIMERS(Timer4,DISABLE);//To resolve break issue due to magnetometer()
READ_SETTINGS_FROM_EEPROM(FALSE,config.simNumber);
INIT_TIMERS(Timer4,ENABLE);
GSM_SELECT_SIM(config.simNumber);
}
USART_SendData_s( DEBUG_COM,(unsigned char*)"Network ERROR: Reinitialize GSM...\r\n");
states.GSMFirstInit = FALSE;
}
STATE_MACHINE_LED_TOGGLE();
Delay_ms(config.GSMTryDelay*1000); //Server side next try delay in sec
}
else
{
STATE_MACHINE_LED_TOGGLE();
states.NoNetworkCounter = 0;
}
states.GPRSStateOk = GET_GPRS_STATUS(); // check GPRS Current State
if(config.FTPSetting==TRUE)
{
states.FTPStateOk = GET_FTP_STATUS(); // check FTP Current State
}
if(states.NetworkInit == TRUE && states.GPRSStateOk == FALSE) // If Network Strenth is OK and GPRS is not connected
{
if(Ack.LoginAckRetransmit!=2 && states.GPRSInit==FALSE)
{
Ack.LoginAckRetransmit=TRUE;
}
states.GPRSInit = GPRS_INIT(config.ServerIP1, config.ServerPort1, 1); // Connect GPRS Connection with server
if(states.GPRSInit == FALSE)
{
states.SeverShiftCounter++;
if(states.SeverShiftCounter >=config.GSMIPTry)
{
USART_SendData_s( DEBUG_COM,(unsigned char*)"Change Server IP////////////\r\n");
states.SeverShiftCounter = 0;
if(GSM.IpSelection == 0)
{
GSM.IpSelection = 1;
}
else
{
GSM.IpSelection = 0;
}
if(config.GSMTryRebootEnDs==TRUE && states.CallOngoingFlag==FALSE) // Turn OFF the GSM Module
{
USART_SendData_s( DEBUG_COM,(unsigned char*)"GPRS/IP ERROR:Calling gsmPowerOff()////////\r\n");
GSM_PWR_ON_OFF(OFF);
}
}
}
else
{
states.SeverShiftCounter = 0;
}
Delay_ms(config.GSMTryDelay*1000); //Server side next try delay in sec
}
if(Ack.LoginAckRetransmit==TRUE && Ack.LoginPackFlag==FALSE)
{
Ack.LoginAckRetransmit=FALSE;
Ack.LoginPackFlag=TRUE;
}
}
else
{
USART_SendData_s( DEBUG_COM,(unsigned char*)"TIM4 DS\r\n");
INIT_TIMERS(Timer4,DISABLE);
IAP.FTPConnectCounter=0;
while(GSM.FTPPUTConnectFlag==TRUE && IAP.FTPConnectCounter<30)// wait 10 mint for IAP download
{
Delay_ms(1000);
}
GSM.FTPPUTConnectFlag=FALSE;
IAP.FTPConnectCounter=0;
INIT_TIMERS(Timer4,ENABLE);
USART_SendData_s( DEBUG_COM,(unsigned char*)"TIM4 EN\r\n");
}
if(states.NetworkInit == TRUE && states.GPRSStateOk == TRUE && states.FTPStateOk == FALSE && config.FTPSetting==TRUE)// If initialization and GSM network OK
{
GSM.FTPPUTConnectFlag=FALSE;
FTP_INIT();
IAP.RetryCount=0;
}
if( GSM.FTPPUTConnectFlag==FALSE && config.IAPSetting==TRUE && states.GPRSStateOk == TRUE && states.FTPStateOk == TRUE && config.FTPSetting==TRUE && GPIO_ReadInputDataBit( CAR_IGNITION_ON_PORT,CAR_IGNITION_ON_PIN))
{
PACKET_HANDLER(); //Service any pending requests
Delay_ms(1000);
USART_SendData_s( DEBUG_COM,(unsigned char*)"Getting IAP File from FTP Server////////////////\r\n");
IAP.RetryCount++;
config.OTAReadyFlag = FALSE;
if(IAP.RetryCount>config.IAPTry)
{
states.FirmwareErrorFlag= TRUE;
USART_SendData_s( DEBUG_COM,(unsigned char*)"IAP Error++\r\n");
config.FTPSetting = FALSE;
config.IAPSetting = FALSE;
EEPROM_WRITE_BYTE(IAP_SETTING,config.IAPSetting);
IAP.RetryCount=0;
}
else
{
#ifdef EEPROM_ENABLED
EEPROM_DMA_CONFIGURATION(IAP.USART_ISR_buffer1,EEPROM_PAGE_SIZE);
IAP.Data_Start_Flag=FALSE;
IAP.File_download_status=FALSE;
IAP.Write_addess=0;
FTP_GET_FILE(config.OTAFileName);
#endif
}
}
if(IAP.File_download_status==TRUE )
{
IAP.FileCheckError=OTA_CONVERT_FILE();
if(IAP.FileCheckError==TRUE)
{
config.IAPSetting=TRUE;
config.FTPSetting=TRUE;
}
IAP.File_download_status=FALSE;
}
if(config.FTPSetting == FALSE && states.FTPStateOk==TRUE )
{
// check FTP Current State
// states.FTPStateOk = Command_Send_SATE_FTP_Get();
USART_SendData_s( DEBUG_COM,(unsigned char*)"AT+QFTPCLOSE\r\n");
USART_SendData_s( GSM_COM,(unsigned char*)FTP_CLOSE);
Delay_ms(1000);
}
if(states.CallOngoingFlag==TRUE)
{
CHECK_CALL_STATUS();
}
}
/*********************************************************************************************************
SMS_GENERATOR
This module generates the sms based on the parameters passed to it
*********************************************************************************************************/
void SMS_GENERATOR(unsigned char *a_SMSContent,unsigned char *a_SMSNumber,unsigned char msgType)
{
USART_SendData_s( GSM_COM,(unsigned char*)SMS_SEND);
Delay(3*0x8FFF7);
//Delay_ms(300);
if(strstr((const char*)GSM.ModuleCommandsBuffer,(const char*)ERROR))// if sms reqeust to GSM has error
{
USART_SendData_s(DEBUG_COM,(unsigned char*)"P_K\r\n");
}
else// process ahead and send sms message
{
USART_SendData_s( GSM_COM,(unsigned char*)a_SMSNumber);
USART_SendData_s( GSM_COM,(unsigned char*)"\"\r\n");
Delay(10*0x8FFF7);
//Delay_ms(1000);
if(strstr((const char*)GSM.ModuleCommandsBuffer,(const char*)ERROR))// if GPRS is connected already
{
USART_SendData_s( DEBUG_COM,(unsigned char*)"O_K\r\n");
}
else
{
if(msgType == 0)
{
USART_SendData_s( GSM_COM,(unsigned char*)a_SMSContent);
}
else if(msgType == 1)// all io sms
{
USART_SendData_s( GSM_COM,(unsigned char*)dearSt);//const char string1[] PROGMEM = "Dear Customer, Your vehicle #"
USART_SendData_s( GSM_COM,(unsigned char*)config.carRegNo);//IDN-55112
USART_SendData_s( GSM_COM,(unsigned char*)a_SMSContent);// Door Open
USART_SendData_s( GSM_COM,(unsigned char*)infSt);//for more information please visit www.teresol.org
}
else if (msgType == 2)// geofence sms
{
USART_SendData_s( GSM_COM,(unsigned char*)dearSt);//const char string1[] PROGMEM = "Dear Customer, Your vehicle #"
USART_SendData_s( GSM_COM,(unsigned char*)config.carRegNo);//IDN-55112
USART_SendData_s( G