//=================================================================
// HSDPA.h	version 1.04
//=================================================================

#ifndef HSDPA_H_
#define HSDPA_H_

#ifdef __cplusplus
extern "C" {
#endif

//--Error codes
#define GM_NOERROR		0	//NO ERROR, success
#define GM_COMERROR		-1	//COM PORT ERROR
#define GM_TIMEOUT		-2	//time out
#define GM_ERROR		-3	//command fail error
#define GM_NOSMS		-4	//no sms
#define GM_NOREG		-5	//no register, or modem can't service
#define GM_INITERROR	-6	//init. Error
#define GM_BUSY			-7	//modem is busy

#define GM_LINKERROR	-31	//Link to Server ERROR
#define GM_NETERROR		-32	//Network not opened ERROR

// encode style for SMS
#define GSM_7BIT		0
#define GSM_UCS2		8

//-- structure for sending/reading SMS
typedef struct STRENCODE_MSG{
	char phoneNumber[30];	//phone number
	char time[20];			//sms_time_stamp
	char msg[161];			//message's content
	unsigned char dataLen;	//Message's length, Max length: 7-bit=160, UCS2=140(70 word)
	char mode;				//encode style: 0=GSM_7BIT, 8=GSM_UCS2(uni-code)
} strEncode_Msg;

//-- structure for reading/sending GPRS sockets
typedef struct GPRSDATA{
	char data[1024];		//data
	int dataLen;			//data length
	char IP[16];			//IP of the serverˇA000.000.000.000 and '0x00', total 16 byte
	unsigned int port;		//TCP/UDP port of the server
	char link;				//data of link[n]
} GPRSData;

//-- structure for setting network
typedef struct NET_PROFILE
{
	char APN[60];	//APN for network provided by your cellular provider
	char user[32];	//username for network provided by your cellular provider
	char pw[32];	//password for network provided by your cellular provider
} NetProfile;

//==== SMS API for user ====

//== Get lib. version
//return:
//	version format = A.BC
int GM_SYS_GetLibVersion(void);

//== return lib. date
//parameter:
//	libDate:	a string of lib. date, format="Jul 21 2010"
void GM_SYS_GetLibDate(char* libDate);

//== initialize Modem, and turn it on
//		must use GM_SYS_CheckModemStatus() to check modem status later
//parameter:
//	PinNum:		PIN code, format="xxxx", NULL mean don't send PIN
//return:
//		GM_NOERROR:		success
//		GM_COMERROR:	comport error
//		GM_INITERROR:	init fail error
int GM_SYS_InitModem(const char* PinNum);

//== Close the modem
//		(1) use it before you want to finish the program
//		(2) or your case must save the power of the battery during device working
//		after GM_SYS_CloseModem(), you must use GM_SYS_InitModem() to wake up modem
//parameter:
//	mode:	1:	close modem and set it power off (only for G-4500)
//			0:	close modem, still power on
//return:
//		GM_NOERROR		no error
//		GM_ERROR:		Error
int GM_SYS_CloseModem(int mode);

//== check modem status, suggest you check it in your loop every time
//return:
//		GM_NOERROR:		modem register success, can service
//		GM_NOREG:		modem not registered, can't service
int GM_SYS_CheckModemStatus(void);

//== get the status of the command you sent
//return:
//	GM_BUSY:	 	modem busy, you can't send other command
//	GM_NOERROR:		success
//	GM_TIMEOUT:		time out
//	GM_ERROR:		command fail error
//	other:			please refer to error codes of HSDPA.h
int GM_SYS_CheckCmdStatus(void);

//== check signal quality
//return:
//	signal:		signal quality
//					0		-113 dBm or less
//					1		-111 dBm
//					2...30	-109... -53 dBm
//					31		-51 dBm or greater
//					99		not known or not detectable 
int GM_SYS_CheckSignal(void);

//== check register
//return:
//	register flag
//			0:	not registered
//			1:	registered, home network
//			2:	not registered, and searching...
//			3:	registration denied
//			4:	unknown
//			5:	registered, roaming
int GM_SYS_CheckReg(void);


//======================
//==== SMS function ====
//======================

//== Read message(index)
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//parameter:
//	index:	index of sms
//	strMsg:	sms will be put here
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_SMS_ReadMsg(int index, strEncode_Msg* strMsg);

//== Send a message
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//parameter:
//	strMsg: the message
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_SMS_SendMsg(strEncode_Msg strMsg);

//== Get new sms message
//	this command will read the new message, and delete it
//parameter:
//	msg:	new sms message
//return:
//		NULL:	no new message
//		msg:	new message
strEncode_Msg* GM_SMS_GetNewMsg(strEncode_Msg* msg);

//== delete sms message(index)
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//parameter:
//	index:	index of the message you want to delete
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_SMS_DeleteMsg(int index);

//== delete all sms message in SIM card
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_SMS_DeleteAll(void);


//==============================================
//==== 3G / GPRS data Transmission Function ====
//==============================================

//== Set Net profile data
//parameter:
//	netProfile:	include APN(Access Point Name), password and username
//return:
//		GM_NOERROR	no error
//		GM_ERROR	error
int GM_NET_SetNet(NetProfile netProfile);

//== Start TCP/UDP Client NetWork
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_NET_OpenNet(void);

//== Close NetWork
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_NET_CloseNet(void);

//== Auto-Relink to server
//parameter:
//	autoRelink:		0=not relink, 1=auto-relink
void GM_NET_AutoRelink(int autoRelink);

//== get IP
//parameter:
//	ipaddr:		IP string, format: char ipaddr[16];
//return:
//		ipaddr		success
//		NULL		error, network may be broken
char* GM_NET_GetIP(char* ipaddr);

//== Link to the server as TCP or UDP client
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//	* if you link to the same IP and Port, will couldn't recognize new packet from which link
//parameter:
//	n:		link number (0~9), generally, you can build 10 link for client
//	tcp:	client type, tcp=1 for TCP client ; tcp=0 for UDP client
//	ip:		IP that you want to link, ex: "61.111.222.333"
//	port:	TCP/UDP Port, ex: 1234
//return:
//		GM_NOERROR	no error
//		GM_ERROR	parameter error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_NET_LinkTo(int n, int tcp, char* ip, unsigned int port);

//== Close link[n] in client mode
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//parameter:
//	n:		client link[n]
//return:
//		GM_NOERROR	no error
//		GM_ERROR	parameter error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_NET_CloseLink(int n);

//== get status of Link[n]
//parameter:
//	n:	link[n]
//return:
//		status:		0=not link, 1=linked
//		GM_ERROR:	index error
int GM_NET_GetLinkStatus(int n);

//== Send a packet
//	must use "GM_SYS_CheckCmdStatus()" to check status later
//parameter:
//	gprsData:	the data you want to send, the Max. data length = 1024
//return:
//		GM_NOERROR	no error
//		GM_NOREG:	not registered, or can't service
//		GM_BUSY:	modem busy
int GM_NET_Send(GPRSData gprsData);

//== Get the new packet
//parameter:
//	gprsData:	data pointer
//return:
//		NULL:		no new packet
//		gprsData:	new packet ,and return data pointer that you give
GPRSData* GM_NET_GetNewPacket(GPRSData* gprsData);

//== Get the new packet
//parameter:
//	gprsData:	data pointer
//	link:		get new data from link[n]
//				suggest you using "GM_NET_GetNewPacket", to be sure get all data in buffer....
//				avoid lose some link, let buffer full!!!!
//return:
//		NULL:		no new packet
//		gprsData:	new packet ,and return data pointer that you give
GPRSData* GM_NET_GetNewPacketFrom(GPRSData* gprsData, int link);


//== Built TCP/UDP link in auto relink mode
//parameter:
//	netProfile	include APN(Access Point Name), password and username
//	n			link number (0~9), generally, you can build 10 link for client
//	tcp			client type, tcp=1 for TCP client ; tcp=0 for UDP client
//	serverIP	IP of the server, ex: "61.111.222.333"
//	serverPort	TCP/UDP Port of the server, ex: 1234
//return:
//		GM_NOERROR		success to built link[n]
int GM_NET_InstallLink(NetProfile netProfile, int n, int tcp, char* serverIP, unsigned int serverPort);


#ifdef __cplusplus
}
#endif


#endif //HSDPA_H_