/**
  ******************************************************************************
  * @file    GSM.h
  * @brief   This file contains all the function prototypes for
  *          the GSM.c file
  ******************************************************************************
  *1. define SIM800_Global_Struct
  *2. start init functions
		SIM800_Var_Init(&SIM800_Struct);
		HTTP_Data_Init(&SIM800_Struct);
  *3. DPC_TO_Init();
  *4.
  *
  *
  *
  *
  *
  *
  *
  *
  *
  ******************************************************************************
  */





#ifndef __GSMMODULE_H
#define __GSMMODULE_H

#include "main.h"
//#include "string.h"
//#include "stdlib.h"


typedef struct
	{
		uint8_t SIM_RX_Flag;
		char SIM_answer[1024];
		char* SIM_command;
		uint8_t Size;
	}SIM_Answer_Struct;

typedef struct {
		uint8_t year;
		uint8_t month;
		uint8_t day;
		uint8_t hour;
		uint8_t minute;
		uint8_t second;
		uint8_t timezone;
	}Time_Struct;

typedef struct{
	char* Serial_Number;
	char* Battery_Condition_ID;
	char* Event_time;
	char* Started_At_Time;
	char* Ended_At_Time;
	char* Status_Indication;
	char* Energy_Sum;
	char* Battery_Level_At_Start;
	char* Battery_Level_At_End;
	char* Battery_Voltage_At_Start;
	char* Battery_Voltage_At_End;
	char* Battery_Temperature_At_End;
	char* Latitude;
	char* Longitude;
	char* Maximum_Load_Current;
	char* Maximum_Charge_Current;
	char* Internal_Resistance;
	char* Moderated;
	char* Current_time;

	char Serial_Number_var[10];
	char Battery_Condition_ID_var[10];
	char Event_time_var[30];
	char Started_At_Time_var[30];
	char Ended_At_Time_var[30];
	char Status_Indication_var[10];
	char Energy_Sum_var[10];
	char Battery_Level_At_Start_var[10];
	char Battery_Level_At_End_var[10];
	char Battery_Voltage_At_Start_var[10];
	char Battery_Voltage_At_End_var[10];
	char Battery_Temperature_At_End_var[10];
	char Latitude_var[10];
	char Longitude_var[10];
	char Maximum_Load_Current_var[10];
	char Maximum_Charge_Current_var[10];
	char Internal_Resistance_var[15];
	char Moderated_var[15];
	char Current_time_var[30];

}HTTP_DATA_Struct;

typedef enum{
	AT,
	ATE,
	ATV,
	ATW,
	ATCCLK,
	ATCMEE2,
	ATCLTS,
	ATCNMI,
	ATCREG,
	ATCCID,
	ATCSQ,
	ATCOPS,
	ATCOPS_Manual,
	ATHTTPSTATUS,

	ATCGATT_check,
	ATCGATT_disconnect,
	ATCGATT_connect,
	ATSAPBR0,
	ATSAPBR1,
	ATSAPBR2,
	ATSAPBR3_1_CONTYPE,
	ATSAPBR3_1_APN,
	ATSAPBR3_1_USER,
	ATSAPBR3_1_PWD,
	ATSAPBR4_1,


	ATHTTP,
	ATHTTPPARA_CID,
	ATHTTPPARA_URL,
	ATHTTPPARA_URL_EVENT,
	ATHTTPPARA_CONTENT,
	ATHTTPPARA_USERDATA,
	ATHTTPPARA_PARAM,
	ATHTTPDATA,
	ATHTTPP_ACTION_POST,
	ATHTTPP_ACTION_GET,
	DATA0,
	DATA1,
	DATA2,
	ATHTTPREAD,
	ATHTTPREAD_REQ,
	ATHTTPHEAD,
	ATHTTPTERM,
	ATHTTPSSL,
	ATHTTPSSLREQUEST,
	ATCGMR,
	ATCDNSCFG,
	ATCDNSCFG_REQ,
	ATCIFSR,
	ATCIPSTATUS,
	ATCSTT,
	ATCIICR,
	ATCIPSHUT,
	ATCIPMUX,
	ATCIPGSMLOC,

	REQUEST_START,
	REQUEST_FINISHED,
	READY_TO_OPERATE,

	GPS_Req,
	GET_ACTUAL_TIME
} Sim_Stages;

typedef struct
	{
		uint8_t Init_Complete;
		uint8_t GPRS_Established;
		uint8_t Permission_to_Send;
	}SIM800_States;

//PUT DATA STRUCT
typedef struct{
	uint64_t Serial;
	uint8_t Battery_cond_ID;
	Time_Struct Event_time;
	Time_Struct Started_At_Time;
	Time_Struct Ended_At_Time;
	uint8_t Status_Indication;
	uint16_t Energy_Sum;
	uint16_t Battery_Level_At_Start;
	uint16_t Battery_Level_At_End;
	float Battery_Voltage_At_Start;
	float Battery_Voltage_At_End;
	uint16_t Battery_Temperature_At_End;
	uint16_t Maximum_Load_Current;
	uint16_t Maximum_Charge_Current;
	float Internal_Resistance;
	uint8_t Moderated;
}Battery_Data_Struct;

typedef enum{
	INITIALISATION,
	READY,
	BUSY,
	SIM_ERROR,
	COMMAND_SET,
	COMMAND_NOT_SET
}SIM800_Op_States;

typedef struct
{
	char* AT ; // "AT\r\n";
	char* ATV ;// "ATV1\r\n"; //echo turn off
	char* ATE ;// "ATE0\r\n"; //answer without echo
	char* ATW ;// "AT&W\r\n"; //save presets to mem
	char* ATCFUN0;//"AT+CFUN;//0\r\n"; //reboot
	char* ATCFUN1;//"AT+CFUN;//1\r\n"; //reboot
	char* ATCMEE2;//"AT+CMEE;//2\r\n"; //return error code
	char* ATCGREG;//"AT+CGREG?\r\n"; //gprs availability
	char* ATCOPS;//"AT+COPS?\r\n";
	char* ATCPAS;//"AT+CPAS\r\n";
	char* ATCSQ;//"AT+CSQ\r\n";

	//sms
	char* ATCMGF1;//"AT+CMGF;//1\r\n";
	char* ATCSMP;//"AT+CSMP;//17,167,0,0\r\n";
	char* ATCMGFnum;//"AT+CMGS;//\"+79253801217\"\r\n";
	char* ATCMGFtext;//"Hello\32\r\n";
	char* ATD;//"ATD+79253801217\r\n";
	char* ATCSCSUCS2;//"AT+CSCS;//\"UCS2\"\r\n";

	//TIME
	char* ATCLTS ;// "AT+CLTS;//1\r\n";
	char* ATCCLK ;// "AT+CCLK?\r\n";
	//GPS
	char* ATCLBS;

	//GPRS
	char* ATCGATT_check;//"AT+CGATT?\r\n";
	char* ATCGATT_disconnect;
	char* ATCGATT_connect;//"AT+CGATT;//1\r\n";
	char* ATSAPBR1;//"AT+SAPBR;//1,1\r\n"; //Set connection
	char* ATSAPBR2;//"AT+SAPBR;//2,1\r\n"; //get info and IP
	char* ATSAPBR3_1_CONTYPE;//"AT+SAPBR;//3,1,\"CONTYPE\",\"GPRS\"\r\n"; //CONTYPE gprs connection types
	char* ATSAPBR3_1_APN;//"AT+SAPBR;//3,1,\"APN\",\"internet.beeline.ru\"\r\n"; //APN connection¤
	char* ATSAPBR3_1_USER;//"AT+SAPBR;//3,1,\"USER\",\"Beeline\"\r\n"; //
	char* ATSAPBR3_1_PWD;//"AT+SAPBR;//3,1,\"PWD\",\"Beeline\"\r\n"; //
	char* ATSAPBR4_1;//"AT+SAPBR;//4,1\r\n"; //current settings
	char* ATCSTT;
	char* ATCIICR;
	char* ATCIPSHUT;
	char* ATCIPGSMLOC;


	//HTTP
	char* ATHTTP;//"AT+HTTPINIT\r\n"; //init http
	char* ATHTTPPARA_CID;
	char* ATHTTPSTATUS;
	char* ATHTTPPARA_PARAM;
	char* ATHTTPPARA_URL;
	char* ATHTTPP_ACTION_POST;
	char* ATHTTPP_ACTION_GET;

	char* ATHTTPPARA_USERDATA;

	char* ATHTTPPARA_CONTENT; //set the Content-Type field in HTTP header
	char* ATHTTPPARA_KEY;
	//after this should return DOWNLOAD - we ready to transmit

	char* DATA2;

	char* ATHTTPACTION; //0-GET 1-POST 2-HEAD
	char* ATHTTPREAD; //should return 200
	char* ATHTTPTERM; //close
	char* ATSAPBR0; //close
	char* AT_CSCS_GSM;
	char* ATCUSD102;
	char* ATCNMI;
	char* ATCMGL;
	char* ATCCID;
	char* ATCREG;
	char* ATHTTPSSLREQUEST;
	char* ATHTTPSSL;
	char* ATCGMR;
	char* ATCOPS_Manual;
	char* ATCDNSCFG_REQ;
	char* ATCDNSCFG;
	char* ATCIFSR;
	char* ATCIPSTATUS;
	char* ATCIPMUX;
	char* ATHTTPREAD_REQ;
	char* ATHTTPHEAD;
	char* ATHTTPPARA_URL_EVENT;

	char* SE_HTTPDATA;

	char* SE_ENDOFSTRING;
	char* SE_DATATO;


	char* DATA0;
	char* DATA1; //data to transmit
	char* ATHTTPDATA0;
	char* ATHTTPDATA1;
	char* SE_Length1;
	char* SE_Length2;

	char DATA0_ch[500];
	char DATA1_ch[300];
	char ATHTTPDATA0_ch[100];
	char ATHTTPDATA1_ch[100];
	char SE_Length1_ch[100];
	char SE_Length2_ch[100];

} AT_Commands_Struct;

typedef enum{
	DONOTHING,
	HTTP_EVENT_POST,
	HTTP_PARAMETERS_POST,
	GET_GPS_COORDINATES,
	GET_TIME
}SIM800_Op_Commands;

typedef struct{
	Battery_Data_Struct BATTERY_DATA;
	SIM_Answer_Struct Sim_Answer;
	Time_Struct Sim_Time;
	HTTP_DATA_Struct Sim_HTTP_Data;
	Sim_Stages Sim_Stages_List;
	SIM800_Op_States Sim_States_to_return;
	SIM800_States Sim_Main_States;
	AT_Commands_Struct Sim_At_Commands;
	uint8_t SIM_RESET_Switch;
	uint8_t SIM_RESET_Set;
	uint32_t REQUEST_Timeout;
	uint8_t REQUEST_Timeout_Active;
	uint32_t INIT_Timeout;
	uint8_t INIT_Timeout_Active;
	uint32_t NORMAL_Timeout;
	uint8_t NORMAL_Timeout_Active;
	SIM800_Op_Commands Sim_Active_Command;
	char* Default_Longitude;
	char* Default_Latitude;
	char Raw_var[100];
}SIM800_Global_Struct;

void SIM800_Var_Init(SIM800_Global_Struct* Sim_Struct);
void SIM800_Send_Command(char* command);
void SIM800_Send_Command_Long(char* command);
void SIM800_RESET(SIM800_Global_Struct* Sim_Struct);
uint8_t SIM800_Received_LOOKFOR(SIM_Answer_Struct* answ, char* LOOKFOR );
void SIM800_get_time(SIM_Answer_Struct* answ, Time_Struct* time);
void SIM800_Time_To_String(Time_Struct* Time, char* DST);
void SIM800_import_time_to_struct(SIM800_Global_Struct* Sim_Struct, Time_Struct* DST);
void Battery_Data_Event_Processing(SIM800_Global_Struct* Sim_Struct);
void Battery_Data_Parameters_Processing(SIM800_Global_Struct* Sim_Struct);
void Battery_Data_Length_Calculating(SIM800_Global_Struct* Sim_Struct);
void Battery_Data_Time_Processing(SIM800_Global_Struct* Sim_Struct/*HTTP_DATA_Struct* HTTP_DATA,Time_Struct* time*/);
void HTTP_Data_Init(SIM800_Global_Struct* Sim_Struct);
void SIM800_Get_Coordinates(SIM800_Global_Struct* Sim_Struct);
void SIM800_Interrupt_Handler(SIM800_Global_Struct* Sim_Struct, uint8_t* UART_Buffer, uint16_t Size);
void SIM800_Processing(SIM800_Global_Struct* Sim_Struct, uint8_t* UART_Buf);
SIM800_Op_States SIM800_Init(SIM800_Global_Struct* Sim_Struct/*SIM800_States* State, Sim_Stages* CMD_Type, AT_Commands_Struct* AT_CMD ,SIM_Answer_Struct* Answer_Struct, Time_Struct* Time*/);
SIM800_Op_States SIM800_Active_Mode(SIM800_Global_Struct* Sim_Struct);
SIM800_Op_States SIM800_Set_Command(SIM800_Global_Struct* Sim_Struct, SIM800_Op_Commands CMD, uint32_t Timeout);
void Battery_Data_to_String_Converter(SIM800_Global_Struct* Sim_Struct);


#endif
