/*
smsDemo.c: send and receive sms message demo
Compiler:
  VC6

Hardware: GTM-201, GTM-201-3GWA serial

notice:

[Apr 12, 2011] Create by Malo
*/

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "GSM.h"

//== Unicode format to MBCS (default formate of VC6's string)
//parameter:
//	pSrc:		Unicode string, and MBCS string will be put here later
//	nSrcLength:	pSrc's length (byte)
//return:
//	Length of MBCS string (unit:byte)
int UCS2toMBCS(unsigned char* pSrc, int nSrcLength)
{
	int nDstLength;		// UNICODE length
	WCHAR wchar[128];	// UNICODE buffer
	char* pDst = (char*)pSrc;
	
	// change high/low byte, and fit into unicode string
	for(int i=0; i<nSrcLength/2; i++)
	{
		wchar[i] = *pSrc++ << 8;	// high byte
		wchar[i] |= *pSrc++;		// low byte
	}
	
	// UNICODE --> MBCS
	nDstLength = WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/2, pDst, 160, NULL, NULL);
	
	// end char
	pDst[nDstLength] = 0x00;
	
	return nDstLength;
}

//== MBCS format to Unicode format
//parameter:
//	pSrc:		MBCS string, and Unicode string will be put here later
//	nSrcLength:	pSrc's length
//return:
//	Length of Unicode string (unit:byte)
int MBCStoUCS2(char* pSrc, int nSrcLength)
{
	int nDstLength;		// UNICODE length
	WCHAR wchar[128];	// UNICODE buffer
	unsigned char* pDst = (unsigned char*)pSrc;
	
	// MBCS --> UNICODE
	nDstLength = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLength, wchar, 128);
	
	// change high/low byte, and fit into result string
	for(int i=0; i<nDstLength; i++)
	{
		*pDst++ = wchar[i] >> 8;		// high byte
		*pDst++ = wchar[i] & 0xff;		// low byte
	}
	
	return nDstLength * 2;
}

void printfMsg(strEncode_Msg RecMsg)
{
	printf("\n---- new SMS message come in ----\n");
	printf("phone number = %s\n", RecMsg.phoneNumber);
	printf("receive time = %s\n", RecMsg.time);
	printf("mode = %d\n", RecMsg.mode);
	printf("message length = %d\n", RecMsg.dataLen);
	printf("message = %s\n", RecMsg.msg);
	printf("\n---------------------------------\n");
}

int main(void)
{
	int iAction=1, quit=1;
	int i;
	int tmp;
	int libVer;
	char libDate[20];
	int Result=0;
	strEncode_Msg RecMsg, SendMsg;
	char sendNumber[20];
	int send_n;
	int sendStatus = 0;
	SYSProfile sysProfile;

	//-- print HSDPA lib. version
	GM_SYS_GetLibDate(libDate);
	libVer = GM_SYS_GetLibVersion();
	printf("GSM LIB vesion %X.%02X, Date:%s\n", libVer>>8, libVer&0xff, libDate);

	//-- init modem
	strcpy(sysProfile.PINCode, "0000");	//The pin code	of SIM card, ex: "0000"
	sysProfile.modemPort = 2;
	sysProfile.hardware = 0;	//0: GTM-201
	if( (Result = GM_SYS_InitModem(sysProfile)) == GM_NOERROR)
		printf("init_modem success!!\n");
	else{
		printf("init_modem fail!! return value is %d\n", Result);
		return 1;
	}

	//--check Could the modem service?
	while(GM_SYS_CheckModemStatus() != GM_NOERROR)
	{
		printf("wait modem register...\n");
		Sleep(1000);
	}
	printf("modem registered!!\n");

	while(iAction!=0)
	{
		iAction=0;
		quit=0;
		
        printf("\n");
		printf("1) Send ASCII messages\n");
		printf("2) check signal quality\n");
		printf("3) check registered?\n");
        printf("\n");
        printf("0) Quits demo program\n\n");
        printf("Choose an option and press [Enter]: ");
        scanf("%d", &iAction);
        printf("\n");

		switch(iAction)
		{
		case 1://Send ASCII messages
			printf("Send ASCII messages\n");
			printf("Please Input Phone Number =");
			scanf("%s", sendNumber);
			printf("How many message do you want to send?\n");
			scanf("%d", &send_n);
			i = 0;
			printf("== start to send sms, please press <ESC> to exit ==\n");
			while(1)
			{
				// press "ESC" to exit
				if(kbhit())
				{
					tmp = getch();
					if( tmp == 27 || tmp == 'q')
						break;
				}
				
				//--(1) 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)
				{
					switch((sendStatus=GM_SYS_CheckCmdStatus()))
					{
					case GM_READY:
						printf("sending message(%d)....\n", i);

						strcpy(SendMsg.phoneNumber, sendNumber);

						SendMsg.mode = GSM_7BIT;
						sprintf(SendMsg.msg, "GSM_Test(%2d)", i);
						SendMsg.dataLen = strlen(SendMsg.msg);
						//-- if you want to send uni-code string, sample codes as below
						//SendMsg.mode = GSM_UCS2;
						//sprintf(SendMsg.msg, "UCS2¤¤¤å(%2d)", i);
						//SendMsg.dataLen = MBCStoUCS2(SendMsg.msg, strlen(SendMsg.msg));

						GM_SMS_SendMsg(SendMsg);
						break;
					case GM_NOERROR:
						printf("send success!!\n");
						i++;
						break;
					case GM_BUSY:	//sending, and waiting reply
						break;
					default:
						printf("send error, and skip this one, error code=%d\n", sendStatus);
						i++;
						break;
					}
				}
				
				//--(3) if any sms message come in, print it
				if( GM_SMS_GetNewMsg(&RecMsg) != 0)
				{
					if(RecMsg.mode == GSM_UCS2)
						RecMsg.dataLen = UCS2toMBCS((unsigned char*)RecMsg.msg, RecMsg.dataLen);

					printfMsg(RecMsg);
				}
			}
			
			break;

		case 2://check signal quality
			Result = GM_SYS_CheckSignal();
			printf("-----------------------------------\n");
			printf("signal value = %d\n", Result);
			printf("signal quality:\n");
			printf("0		-113 dBm or less\n");
			printf("1		-111 dBm\n");
			printf("2~30		-109... -53 dBm\n");
			printf("31		-51 dBm or greater\n");
			printf("99		not known or not detectable\n");
			printf("-----------------------------------\n");
			break;

		case 3://check register value
			Result = GM_SYS_CheckReg();
			printf("-----------------------------------\n");
			printf("register value = %d  (0:no register, 1:registered, 2:registering)\n", Result);
			printf("register flag:\n");
			printf("	0:	not registered\n");
			printf("	1:	registered, home network\n");
			printf("	2:	not registered, and searching...\n");
			printf("	3:	registration denied\n");
			printf("	4:	unknown\n");
			printf("	5:	registered, roaming\n");
			printf("-----------------------------------\n");
			break;

		case 0:
		default:
			quit=1;
			break;

		}//end switch()

		if(!quit)
		{
			printf("Press any key to continue...\n\r");
			getch();
		}
	}//end while(1)

	//must close before program ending to release you resource
	GM_SYS_CloseModem(0);	//== Close the modem, 0:not turn off modem, 1:turn off modem

	printf("Please press ENTER to exit...\n");

	getch();
	return 0;
}