//=================================================================
//	TCPdemo.cpp
//	----
//	TCP/UDP client demo for HSDPA lib. Version 1.02
//=================================================================

#include <conio.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dos.h>
#include "HSDPA.h"
#include "G4500.h"


void printMsg(int index, strEncode_Msg RecMsg)
{
	int i=0;
	Print("----index = %d----\r\n", index);
	Print("phone number = %s\r\n", RecMsg.phoneNumber);
	Print("receive time = %s\r\n", RecMsg.time);
	Print("mode = %d\r\n", (int)RecMsg.mode);
	Print("message length = %d\r\n", RecMsg.dataLen);
	if((int)RecMsg.mode == 8)
	{
		Print("message = ");
		for(i=0; i<RecMsg.dataLen; i++)
			Putch((int)RecMsg.msg[i]);
		Print("\r\n");
	}else{
		Print("message = %s\r\n", RecMsg.msg);
	}
	Print("------------------\r\n");
}

void printPacket(GPRSData gprsData)
{
	int i;
	static int no = 0;
	no++;
	Print("----new GPRS Data (%d)----\r\n", no);
	Print("IP:Port = %s:%d, from link = %d\r\n", gprsData.IP, gprsData.port, gprsData.link);
	Print("Data length = %d\r\n", gprsData.dataLen);
	Print("Data = \r\n");
	for(i=0; i<gprsData.dataLen; i++)
		Putch(gprsData.data[i]);
	Print("\r\n------------------\r\n");
}

int main(void)
{
	int iAction=1, quit=1;
    int libVer;
	char libDate[20];
	int Result=0;
	strEncode_Msg RecMsg, SendMsg;
	int i, j;
	long socketn;
	char sendNumber[20];
	char ip[16];		//my ip
	GPRSData gprsData;
	char serverIP[16];
	int serverPort;
	int serverUDPPort;
	int send_n;
	NetProfile netProfile;
	long ledTimer = 0;
	int netSendFlag = 0;
	int netSendStatus = 0;
	int tmp;
	int link_n = 0;

	//-- init G-4500
	InitLib();
	
	//-- print G4500 lib. version
	GetLibDate(libDate);     
    libVer=GetLibVersion();    
	Print("G4500.Lib vesion %X.%02X, Date:%s\r\n", libVer>>8, libVer&0xff, libDate);
	
	//-- print HSDPA lib. version
	GM_SYS_GetLibDate(libDate);
	libVer = GM_SYS_GetLibVersion();
	Print("HSDPA LIB vesion %X.%02X, Date:%s\n", libVer>>8, libVer&0xff, libDate);
	
	//---- init modem
	if( (Result = GM_SYS_InitModem("0000")) == GM_NOERROR)
		Print("init_modem success!!\n");
	else{
		Print("init_modem fail!! return value is %d\n", Result);
		return 1;
	}
	
	//--check that the modem can service
	while(GM_SYS_CheckModemStatus() != GM_NOERROR)
	{
		Print("wait modem register...\n");
		DelayMs(1000);
	}
	Print("modem registered!!\n");
	
    while(iAction!=0)
    {
        iAction=0;
        quit=0;
        
        Puts("\r\n");
		Puts("1) TCP client demo\r\n");
		Puts("2) multi TCP/UDP client auto send, receive\r\n");
		Puts("5) check signal quality\r\n");
		Puts("6) check registered?\r\n");
        Puts("\r\n");
        Puts("0) Quits demo program\r\n\r\n");
        Puts("Choose an option and press [Enter]: ");
        Scanf("%d", &iAction);
        Puts("\r\n");
        Puts("\r\n");
        
        switch(iAction)
        {
		case 1:	//TCP client demo
			Print("TCP client demo start\r\n");

			// set Network profile
			strcpy(netProfile.APN, "INTERNET");
			strcpy(netProfile.pw, "");
			strcpy(netProfile.user, "");
			strcpy(serverIP, "74.125.227.19");
			serverPort = 80;

			socketn = 0;
			ledTimer = GetTimeTicks();
			netSendFlag = -2;		//-2:waiting InstallLink(), -1:can send, 0:check status
			GM_NET_AutoRelink(1);	//set as auto-relink mode
			while (1)
			{
				// press "ESC" to exit
				if(Kbhit())
				{
					tmp = Getch();
					if( tmp == 27 || tmp == 'q')
						break;
				}

				//--(1)flash red Led light every 1 sec.
				if(GetTimeTicks()-ledTimer > 1000)
				{
					ledTimer = GetTimeTicks();
					LedToggle();
				}

				//--check that could the modem service, if it can't, skip operating the modem below
				if(GM_SYS_CheckModemStatus() != GM_NOERROR)
					continue;

				//--(2) open network and link to server
				if(netSendFlag == -2)
				{
					if(GM_NET_InstallLink(netProfile, 1, 1, serverIP, serverPort) == GM_NOERROR)
						netSendFlag = -1;	//set as "can send"
				}
				
				//--(3) send the data to server, and when LinkStatus[1]=1
				if(netSendFlag==-1 && GM_NET_GetLinkStatus(1)==1)
				{
					Print("\r\nsend package!!\r\n");
					
					gprsData.link = 1;
					sprintf(gprsData.data, "-<%8ld>-TCP send test!!", socketn);
					gprsData.dataLen = strlen(gprsData.data);
					netSendFlag = GM_NET_Send(gprsData);
					switch(netSendFlag)
					{
					case GM_BUSY:
					case GM_NOREG:
						Print("can't send package[%8ld]....., error code=%d\r\n", socketn, netSendFlag);
						netSendFlag = -1;//fail, send again later
						break;
					case GM_NOERROR:
						netSendFlag = 0;//success check status later
						Print("sending package[%8ld]...\r\n", socketn);
						break;
					}
				}
				if(netSendFlag == 0)
				{
					netSendStatus = GM_SYS_CheckCmdStatus();
					switch(netSendStatus)
					{
					case GM_NOERROR:	//success, and send next one
						Print("send success!!\r\n");
						netSendFlag = -1;
						socketn++;
						break;
					case GM_BUSY:	//sending, and waiting reply
						break;
					default:
						Print("send error, and re-send again, error code=%d\r\n", netSendStatus);
						netSendFlag = -1;	//re-send again
						break;
					}
					
				}
				
				//--(4) if any new data packet come in, print it
				if(GM_NET_GetNewPacket(&gprsData) != NULL)
				{
					Print("\r\n== new data packet come in\r\n");
					printPacket(gprsData);
				}

			}
			GM_NET_CloseNet();
			DelayMs(1000);
			
			LedOn();	//program finish, turn on LED
			break;

		case 2:	//multi TCP/UDP client demo
			Print("multi TCP/UDP client demo start\r\n");

			// set Network profile
			strcpy(netProfile.APN, "INTERNET");
			strcpy(netProfile.pw, "");
			strcpy(netProfile.user, "");
			strcpy(serverIP, "74.125.227.19");
			serverPort = 80;
			
			socketn = 0;
			ledTimer = GetTimeTicks();
			netSendFlag = -2;		//-2:waiting InstallLink(), -1:can send, 0:check status
			GM_NET_AutoRelink(1);	//set as auto-relink mode
			link_n = 0;
			while (1)
			{
				// press "ESC" to exit
				if(Kbhit())
				{
					tmp = Getch();
					if( tmp == 27 || tmp == 'q')
						break;
				}
				
				//--(1)flash red Led light every 1 sec.
				if(GetTimeTicks()-ledTimer > 1000)
				{
					ledTimer = GetTimeTicks();
					LedToggle();
				}
				
				//--check that could the modem service, if it can't, skip operating the modem below
				if(GM_SYS_CheckModemStatus() != GM_NOERROR)
					continue;
				
				//--(2) open network and link to server
				if(netSendFlag == -2)
				{
					if(GM_NET_InstallLink(netProfile, link_n, 1, serverIP, serverPort) == GM_NOERROR)
					{
						Print("Install link[%d] success\r\n", link_n);
						link_n++;
						if(link_n==4)
						{
							netSendFlag = -1;
							link_n = 0;
						}
					}
				}
				
				//--(3) send the data to server, and when LinkStatus[1]=1
				if(netSendFlag==-1 && GM_NET_GetLinkStatus(1)==1)
				{
					Print("\r\nsend package!!\r\n");
					
					gprsData.link = link_n;
					sprintf(gprsData.data, "-<%8ld>-TCP send test!!", socketn);
					gprsData.dataLen = strlen(gprsData.data);
					netSendFlag = GM_NET_Send(gprsData);
					switch(netSendFlag)
					{
					case GM_BUSY:
					case GM_NOREG:
						Print("can't send package[%8ld]....., error code=%d\r\n", socketn, netSendFlag);
						netSendFlag = -1;//fail, send again later
						break;
					case GM_NOERROR:
						netSendFlag = 0;//success check status later
						Print("sending package[%8ld]...\r\n", socketn);
						break;
					}
				}
				if(netSendFlag == 0)
				{
					netSendStatus = GM_SYS_CheckCmdStatus();
					switch(netSendStatus)
					{
					case GM_NOERROR:	//success, and send next one
						Print("send success!!\r\n");
						netSendFlag = -1;
						socketn++;
						link_n = (link_n+1)%4;
						break;
					case GM_BUSY:	//sending, and waiting reply
						break;
					default:
						Print("send error, and re-send again, error code=%d\r\n", netSendStatus);
						netSendFlag = -1;	//re-send again
						break;
					}
					
				}
				
				//--(4) if any new data packet come in, print it
				if(GM_NET_GetNewPacket(&gprsData) != NULL)
				{
					Print("\r\n== new data packet come in\r\n");
					printPacket(gprsData);
				}
				
			}
			GM_NET_CloseNet();
			DelayMs(1000);
			
			LedOn();	//program finish, turn on LED

			break;

		case 5://check signal quality
			Result = GM_SYS_CheckSignal();
			Print("-----------------------------------\r\n");
			Print("signal value = %d\r\n", Result);
			Print("signal quality:\r\n");
			Print("0		-113 dBm or less\r\n");
			Print("1		-111 dBm\r\n");
			Print("2~30		-109... -53 dBm\r\n");
			Print("31		-51 dBm or greater\r\n");
			Print("99		not known or not detectable\r\n");
			Print("-----------------------------------\r\n");
			break;
			
		case 6://check registered?
			Result = GM_SYS_CheckReg();
			Print("-----------------------------------\r\n");
			Print("register value = %d  (0:no register, 1:registered, 2:registering)\r\n", Result);
			Print("register flag:\r\n");
			Print("	0:	not registered\r\n");
			Print("	1:	registered, home network\r\n");
			Print("	2:	not registered, and searching...\r\n");
			Print("	3:	registration denied\r\n");
			Print("	4:	unknown\r\n");
			Print("	5:	registered, roaming\r\n");
			Print("-----------------------------------\r\n");
			break;
			
		default:
			quit=1;
			break;
        }
        
        if(!quit)
        {
            Puts("Press any key to continue...\n\r");
            Getch();
        }
    }
			
	//must close before program ending to release you resource
	GM_SYS_CloseModem(0);

	return 0;
}
