//=================================================================
//	smsDemo.cpp
//	----
//	SMS Demo for HSDPA.LIB Version 1.04
//=================================================================

#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");
}

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;
	int index=0;
	char sendNumber[20];
	char ip[16];		//my ip
	GPRSData gprsData;
	char serverIP[16];
	int serverPort;
	int serverUDPPort;
	int send_n;
	NetProfile netProfile;
	int sendFlag = 0;
	int sendStatus = 0;
	int tmpFlag = 0;
	int tmpStatus = 0;
	long ledTimer = 0;
	int tmp;

	//-- 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) Send ASCII messages\r\n");
        Puts("2) Send unicode messages\r\n");
		Puts("3) delete All messages\r\n");
		Puts("4) read a message\r\n");
		Puts("5) check signal quality\r\n");
		Puts("6) check registered?\r\n");
		Puts("10)close modem and turn it off\r\n");
		Puts("11)init. modem and turn if on\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");
        
        switch(iAction)
        {
		case 1://Send ASCII messages
			Print("Send ASCII messages\r\n");
			Print("Please Input Phone Number =");
			Scanf("%s", sendNumber);
			Print("How many message do you want to send?\r\n");
			Scanf("%d", &send_n);
			i = 0;
			sendFlag = -1;	//mean: could send
			ledTimer = GetTimeTicks();	//flash red LCE --> program live..
			while(1)
			{
				// press "ESC" to exit
				if(Kbhit())
				{
					tmp = Getch();
					if( tmp == 27 )
						break;
				}

				//--(1)flash red Led light per 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) send messages, when the modem can service
				if(i<send_n)
				{
					if(sendFlag == -1)
					{
						Print("sending message(%d)....\r\n", i);
						strcpy(SendMsg.phoneNumber, sendNumber);
						SendMsg.mode = GSM_7BIT;
						sprintf(SendMsg.msg, "GSM_Test(%2d), it's 7 Bit mode.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.", i);
						SendMsg.dataLen = strlen(SendMsg.msg);
						sendFlag = GM_SMS_SendMsg(SendMsg);
					}else
					{
						sendStatus = GM_SYS_CheckCmdStatus();
						switch(sendStatus)
						{
						case GM_NOERROR:	//success, and send next one
							Print("send success!!\r\n");
							sendFlag = -1;
							i++;
							break;
						case GM_BUSY:	//sending, and waiting replay
							break;
						case GM_TIMEOUT:
							Print("time out, and skip this one\r\n");
							sendFlag = -1;
							i++;		//remove this line, will re-send message
							break;
						default:
							Print("send error, and skip this one, error code=%d\r\n", sendStatus);
							sendFlag = -1;
							i++;
							break;
						}
					}
				}
				if(i==send_n)
				{
					Print("====================================================\r\n");
					Print("== waiting new sms.... please press <ESC> to exit ==\r\n");
					Print("====================================================\r\n");
					i++;
				}

				//--(3) if any sms message come in, print it
				if( GM_SMS_GetNewMsg(&RecMsg) != NULL)
					printMsg(index, RecMsg);
			}

			LedOn();	//program finish, turn on LED
			break;

		case 2://send unicode message
			Print("Send unicode messages\r\n");
			Print("Please Input Phone Number =");
			Scanf("%s", sendNumber);
			Print("How many message do you want to send?\r\n");
			Scanf("%d", &send_n);
			i = 0;
			sendFlag = -1;	//mean: could send
			while(1)
			{
				// press "ESC" to exit
				if(Kbhit())
				{
					tmp = Getch();
					if( tmp == 27 )
						break;
				}
				
				//--(1)flash red Led light per 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) send messages, the modem can service
				if(i<send_n && GM_SYS_CheckModemStatus()==GM_NOERROR)
				{
					if(sendFlag == -1)
					{
						Print("sending message(%d)....\r\n", i);
						strcpy(SendMsg.phoneNumber, sendNumber);
						SendMsg.mode = GSM_UCS2;
						memcpy(SendMsg.msg, "\x00\x55\x00\x43\x00\x53\x00\x32\x4E\x2D\x65\x87", 12);	//UCS2¤¤¤å
						SendMsg.dataLen = 12;
						sendFlag = GM_SMS_SendMsg(SendMsg);
					}else
					{
						sendStatus = GM_SYS_CheckCmdStatus();
						switch(sendStatus)
						{
						case GM_NOERROR:	//success, and send next one
							Print("send success!!\r\n");
							sendFlag = -1;
							i++;
							break;
						case GM_BUSY:	//sending, and waiting replay
							break;
						case GM_TIMEOUT:
							Print("time out, and skip this one\r\n");
							sendFlag = -1;
							i++;		//remove this line, will re-send message
							break;
						default:
							Print("send error, and skip this one, error code=%d\r\n", sendStatus);
							sendFlag = -1;
							i++;
							break;
						}
					}
				}
				if(i==send_n)
				{
					Print("====================================================\r\n");
					Print("== waiting new sms.... please press <ESC> to exit ==\r\n");
					Print("====================================================\r\n");
					i++;
				}

				//--(3) if any sms message come in, print it
				if( GM_SMS_GetNewMsg(&RecMsg) != NULL)
					printMsg(index, RecMsg);
			}

			LedOn();	//program finish, turn on LED
			break;

		case 3://delete all messages
			Print("delete all messages...\r\n");
			
			// delete sms message, and waiting respond from modem
			GM_SMS_DeleteAll();
			while( (Result=GM_SYS_CheckCmdStatus()) == GM_BUSY){}
			switch(Result)
			{
			case GM_NOERROR:	//success, and send next one
				Print("delete success!!\r\n");
				break;
			case GM_TIMEOUT:
				Print("time out, and give up to delete\r\n");
				break;
			default:
				Print("send error, and give up to delete, error code=%d\r\n", Result);
				break;
			}

			break;
		
		case 4://read message
			Print("----read message....\r\n");
			Print("please input the index you want read:\r\nFrom:");
			Scanf("%d", &i);
			Print("to:");
			Scanf("%d", &j);
			Print("read messages from %d to %d\r\n", i, j);

			tmpFlag = -1;	//mean: could send
			while(i<=j)
			{
				if(tmpFlag == -1)
				{
					//before reading, clear RecMsg first.... be safe program
					memset(RecMsg.phoneNumber, 0, sizeof(RecMsg.phoneNumber));
					memset(RecMsg.time, 0, sizeof(RecMsg.time));
					memset(RecMsg.msg, 0, sizeof(RecMsg.msg));
					RecMsg.mode = 0;
					RecMsg.dataLen=0;
					tmpFlag = GM_SMS_ReadMsg(i, &RecMsg);
					if(tmpFlag != GM_NOERROR)	//if can't read, try again
						tmpFlag = -1;
				}else
				{
					tmpStatus = GM_SYS_CheckCmdStatus();
					switch(tmpStatus)
					{
					case GM_NOERROR:	//success, and read next one
						Print("read success!!\r\n");
						printMsg(i, RecMsg);
						tmpFlag = -1;
						i++;
						break;
					case GM_NOSMS:
						Print("no sms message in index(%d)\r\n", i);
						tmpFlag = -1;
						i++;
						break;
					case GM_BUSY:	//reading, and waiting replay
						break;
					case GM_TIMEOUT:
						Print("time out in index(%d)\r\n", i);
						tmpFlag = -1;
						i++;		//remove this line, will re-read message
						break;
					default:
						Print("error in index(%d), error code=%d\r\n", i, tmpStatus);
						tmpFlag = -1;
						i++;
						break;
					}
				}
			}

			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;

		case 10:
			Print("close modem and turn it off\r\n");
			GM_SYS_CloseModem(1);
			DelayMs(100);
			break;
			
		case 11:
			Print("init modem and turn it on\r\n");
			GM_SYS_InitModem("0000");
			
			//--check Could the modem service?
			while(GM_SYS_CheckModemStatus() != GM_NOERROR)
			{
				Print("wait modem register...\n");
				DelayMs(1000);
			}
			Print("modem registered!!\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;
}
