www.pudn.com > ril.zip > Ril.cpp


// Ril.cpp : Defines the entry point for the application. 
// 
 
 
#include "stdafx.h" 
#include "Ril.h" 
#include  
#include  
#include  
 
#include "rilif.h" 
 
#define MAX_LOADSTRING 100 
 
// Global Variables: 
HWND				g_hwndEdit; 
HRIL				g_hRil = 0; 
DWORD				g_dwParam = 0x55AA55AA; 
 
FILE *logHandle = NULL; 
 
static SHACTIVATEINFO s_sai; 
 
HWND CreateRpCommandBar(HWND hwnd); 
void addLBMess( TCHAR *f, ...); 
 
// Global Variables: 
HINSTANCE			hInst;			// The current instance 
HWND				hwndCB = NULL;	// The command bar handle 
 
// Forward declarations of functions included in this code module: 
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR); 
BOOL				InitInstance	(HINSTANCE, int); 
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM); 
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM); 
TCHAR *hexdump(unsigned char *data, int n) 
{ 
    static TCHAR buf[3000]; 
    TCHAR *p; 
    p=buf; 
    while (n--) 
    { 
        p += _sntprintf(p, 4, TEXT(" %02x"), *data++); 
    } 
    return buf; 
} 
 
TCHAR *chardump(unsigned char *data, int n) 
{ 
    static TCHAR buff[3000]; 
    TCHAR *p; 
    p=buff; 
    while (n--) 
    { 
		if( *data > 32 && *data < 127)  
			p += _sntprintf(p, 4, TEXT("  %c"), *data++); 
		else 
			p += _sntprintf(p, 4, TEXT(" %02x"), *data++); 
    } 
    return buff; 
} 
 
void DoError(LPTSTR where) 
{ 
    TCHAR *msgbuf; 
 
    int msglen= FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
            NULL, GetLastError(), 0, (LPTSTR) &msgbuf, 0, NULL); 
 
	MessageBox(NULL, msgbuf, where, MB_OK); 
    LocalFree(msgbuf); 
} 
 
BOOL GetDateAndTime(SYSTEMTIME *st, LPTSTR szDate, LPTSTR szTime, DWORD dwSize) 
{ 
 
	if (!GetDateFormat(LOCALE_NEUTRAL, DATE_SHORTDATE, st, NULL, szDate, dwSize)) { 
		DoError(L"GetDateFormat"); 
		return FALSE; 
	} 
 
	if (!GetTimeFormat(LOCALE_NEUTRAL, TIME_NOSECONDS, st, NULL, szTime, dwSize)) { 
		DoError(L"GetTimeFormat"); 
		return FALSE; 
	} 
 
 
	return TRUE; 
} 
 
void  
DisplayDCS(RILMSGDCS * prmDCS) 
{ 
	addLBMess(L"    DCS cbSize: 0x%0x, %d", prmDCS->cbSize, prmDCS->cbSize); 
	addLBMess(L"    DCS dwParams: 0x%0x, %d", prmDCS->dwParams, prmDCS->dwParams); 
	addLBMess(L"    DCS dwType: 0x%0x, %d", prmDCS->dwType, prmDCS->dwType); 
	addLBMess(L"    DCS dwFlags: 0x%0x, %d", prmDCS->dwFlags, prmDCS->dwFlags); 
	addLBMess(L"    DCS dwMsgClass: 0x%0x, %d", prmDCS->dwMsgClass, prmDCS->dwMsgClass); 
	addLBMess(L"    DCS dwAlphabet: 0x%0x, %d", prmDCS->dwAlphabet, prmDCS->dwAlphabet); 
	addLBMess(L"    DCS dwIndication: 0x%0x, %d", prmDCS->dwIndication, prmDCS->dwIndication); 
	addLBMess(L"    DCS dwLanguage: 0x%0x, %d", prmDCS->dwLanguage, prmDCS->dwLanguage); 
} 
 
void 
DisplayMessage(RILMESSAGE *prmMsg) 
{ 
	TCHAR szDate[256], szTime[256]; 
 
	addLBMess(L"  cbSize: 0x%0x, %d", prmMsg->cbSize, prmMsg->cbSize); 
	addLBMess(L"  dwParams: 0x%0x, %d", prmMsg->dwParams, prmMsg->dwParams); 
	addLBMess(L"  Service Centre: %s", prmMsg->raSvcCtrAddress.wszAddress); 
	addLBMess(L"  dwType: 0x%0x, %d", prmMsg->dwType, prmMsg->dwType); 
	addLBMess(L"  dwFlags: 0x%0x, %d", prmMsg->dwFlags, prmMsg->dwFlags); 
 
	switch(prmMsg->dwType) { 
		case RIL_MSGTYPE_IN_DELIVER: 
			{ 
				addLBMess(L"  From: %s", prmMsg->msgInDeliver.raOrigAddress.wszAddress); 
				addLBMess(L"  dwProtocolID: 0x%0x, %d", prmMsg->msgInDeliver.dwProtocolID, prmMsg->msgInDeliver.dwProtocolID); 
				DisplayDCS(&prmMsg->msgInDeliver.rmdDataCoding); 
 
				if (!GetDateAndTime(&prmMsg->msgInDeliver.stSCReceiveTime, szDate, szTime, 256)) 
				{ 
					return; 
				} 
				addLBMess(L"  Received: %s on %s", szTime, szDate); 
				addLBMess(L"  Header Length: %d", prmMsg->msgInDeliver.cbHdrLength); 
				addLBMess(L"  Body Length: %d", prmMsg->msgInDeliver.cchMsgLength); 
				if (prmMsg->msgInDeliver.cchMsgLength) { 
					addLBMess(L"  Message: %s", hexdump(prmMsg->msgInDeliver.rgbMsg, prmMsg->msgInDeliver.cchMsgLength)); 
				} 
				break; 
			} 
		case RIL_MSGTYPE_IN_STATUS: 
			{ 
				addLBMess(L"  Target address: %s", prmMsg->msgInStatus.raTgtRecipAddress.wszAddress); 
 
				if (!GetDateAndTime(&prmMsg->msgInStatus.stTgtSCReceiveTime, szDate, szTime, 256)) 
				{ 
					return; 
				} 
 
				addLBMess(L"  Receive Time: %s on %s", szTime, szDate); 
 
				if (!GetDateAndTime(&prmMsg->msgInStatus.stTgtDischargeTime, szDate, szTime, 256)) 
				{ 
					return; 
				} 
 
				addLBMess(L"  Discharge Time: %s on %s", szTime, szDate); 
				addLBMess(L"  dwTgtDlvStatus: 0x%0x, %d", prmMsg->msgInStatus.dwTgtDlvStatus, prmMsg->msgInStatus.dwTgtDlvStatus); 
				addLBMess(L"  dwProtocolID: 0x%0x, %d", prmMsg->msgInStatus.dwProtocolID, prmMsg->msgInStatus.dwProtocolID); 
 
				DisplayDCS(&prmMsg->msgInStatus.rmdDataCoding); 
				 
				addLBMess(L"  Header Length: %d", prmMsg->msgInStatus.cbHdrLength); 
				addLBMess(L"  Body Length: %d", prmMsg->msgInStatus.cchMsgLength); 
				if (prmMsg->msgInStatus.cchMsgLength) { 
					addLBMess(L"  Message: %s", hexdump(prmMsg->msgInStatus.rgbMsg, prmMsg->msgInStatus.cchMsgLength)); 
				} 
				break; 
			} 
		default: 
				addLBMess(L"Message type not covered yet!"); 
				break; 
	} 
 
} 
 
void CALLBACK NotifyCallback(DWORD dwCode, const void *lpData,  
										   DWORD cbData, DWORD dwParam) 
{ 
	addLBMess(L"notify: dwCode=0x%0X lpData=%p, cbData=%d",dwCode, lpData, cbData); 
	switch (dwCode & RIL_NCLASS_ALL) { 
	case RIL_NCLASS_MESSAGE: 
		{ 
			addLBMess(L"SMS Message Notification:"); 
 
			switch (dwCode) { 
			case RIL_NOTIFY_MESSAGE: 
				{ 
					RILMESSAGE *prm = (RILMESSAGE *)lpData; 
 
					addLBMess(L"  Incoming message"); 
					DisplayMessage(prm); 
					break; 
				} 
			case RIL_NOTIFY_STATUSMESSAGE: 
				{ 
					RILMESSAGE *prm = (RILMESSAGE *)lpData; 
 
					addLBMess(L"  Incoming status message"); 
					DisplayMessage(prm); 
					break; 
				} 
			} 
		} 
		break; 
	default : 
		addLBMess(L"  Message not implem. Dumping %d chars.: ", cbData); 
		addLBMess(L"%s", hexdump( ( unsigned char *) lpData, cbData)); 
		// addLBMess(L"%s", chardump( ( unsigned char *) lpData, cbData)); 
		 
		break; 
	}						 
 
} 
 
void CALLBACK ResultCallback(DWORD dwCode, HRESULT hrCmdID,		const void *lpData,  
										   DWORD cbData, DWORD dwParam) 
{ 
	addLBMess(L"result: dwCode=%d, hrCmdID=%d lpData=%p, cbData=%d",dwCode, hrCmdID, lpData, cbData); 
} 
 
 
DWORD 
DoRIL(LPVOID lpvoid)  
{	 
	HRESULT result; 
	DWORD dwNotificationClasses = 0xffff0000; 
	LRESULT lresult; 
	int cnt = 50; 
//	BYTE req[4]= {24, 0, 0, 0}; 
 
	SendMessage(g_hwndEdit, LB_RESETCONTENT, 0, 0); 
	lresult = SendMessage(g_hwndEdit,	LB_GETHORIZONTALEXTENT, 0, 0); 
	SendMessage(g_hwndEdit,	LB_SETHORIZONTALEXTENT, 1000, 0); 
 
	result = RIL_Initialize(1, ResultCallback, NotifyCallback, 
					   dwNotificationClasses, g_dwParam, &g_hRil); 
 
	addLBMess(L"RIL Handle: %08X, result %08X", g_hRil, result); 
 
	result = RIL_GetCellTowerInfo( g_hRil); 
	addLBMess(L"RIL_GetCellTowerInfo=%x",result); 
 
	result = RIL_GetCellBroadcastMsgConfig( g_hRil); 
	addLBMess(L"RIL_GetCellBroadcastMsgConfig=%x",result); 
	 
	while(1) { 
		Sleep(100); 
//		wsprintf(szString, L"%s",L"..."); 
//		SendMessage(g_hwndEdit, LB_ADDSTRING, 0, (LPARAM) szString); 
//		if( --cnt < 0) { 
//			lresult = RIL_GetCellTowerInfo( g_hRil); 
//			cnt = 50; 
//			wsprintf(szString, L"RIL_GetCellTowerInfo=%x",lresult); 
//			SendMessage(g_hwndEdit, LB_ADDSTRING, 0, (LPARAM) szString); 
// 
//		} 
 
	} 
 
 
	return 0; 
} 
 
 
int WINAPI WinMain(	HINSTANCE hInstance, 
					HINSTANCE hPrevInstance, 
					LPTSTR    lpCmdLine, 
					int       nCmdShow) 
{ 
	MSG msg; 
	HACCEL hAccelTable; 
 
	// Perform application initialization: 
	if (!InitInstance (hInstance, nCmdShow))  
	{ 
		return FALSE; 
	} 
 
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_RIL); 
 
	// Main message loop: 
	while (GetMessage(&msg, NULL, 0, 0))  
	{ 
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))  
		{ 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		} 
	} 
 
	return msg.wParam; 
} 
 
// 
//  FUNCTION: MyRegisterClass() 
// 
//  PURPOSE: Registers the window class. 
// 
//  COMMENTS: 
// 
//    It is important to call this function so that the application  
//    will get 'well formed' small icons associated with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass) 
{ 
	WNDCLASS	wc; 
 
    wc.style			= CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc		= (WNDPROC) WndProc; 
    wc.cbClsExtra		= 0; 
    wc.cbWndExtra		= 0; 
    wc.hInstance		= hInstance; 
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RIL)); 
    wc.hCursor			= 0; 
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH); 
    wc.lpszMenuName		= 0; 
    wc.lpszClassName	= szWindowClass; 
 
	return RegisterClass(&wc); 
} 
 
// 
//  FUNCTION: InitInstance(HANDLE, int) 
// 
//  PURPOSE: Saves instance handle and creates main window 
// 
//  COMMENTS: 
// 
//    In this function, we save the instance handle in a global variable and 
//    create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
	HWND	hWnd = NULL; 
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text 
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name 
 
	hInst = hInstance;		// Store instance handle in our global variable 
	// Initialize global strings 
	LoadString(hInstance, IDC_RIL, szWindowClass, MAX_LOADSTRING); 
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
 
	//If it is already running, then focus on the window 
	hWnd = FindWindow(szWindowClass, szTitle);	 
	if (hWnd)  
	{ 
		// set focus to foremost child window 
		// The "| 0x01" is used to bring any owned windows to the foreground and 
		// activate them. 
		SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001)); 
		return 0; 
	}  
 
	MyRegisterClass(hInstance, szWindowClass); 
	 
	RECT	rect; 
	GetClientRect(hWnd, &rect); 
	 
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, 
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 
	if (!hWnd) 
	{	 
		return FALSE; 
	} 
	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one 
	// is created is not taken into account). So we resize the window after creating it 
	// if a menubar is present 
	{ 
		RECT rc; 
		GetWindowRect(hWnd, &rc); 
		rc.bottom -= 20; 
		if (hwndCB) 
			MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE); 
	} 
 
 
	ShowWindow(hWnd, nCmdShow); 
	UpdateWindow(hWnd); 
 
	return TRUE; 
} 
 
// 
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG) 
// 
//  PURPOSE:  Processes messages for the main window. 
// 
//  WM_COMMAND	- process the application menu 
//  WM_PAINT	- Paint the main window 
//  WM_DESTROY	- post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	HDC hdc; 
	int wmId, wmEvent; 
	PAINTSTRUCT ps; 
	DWORD dwError, dwThreadID; 
	HANDLE hReadThread = NULL; 
 
	switch (message)  
	{ 
		case WM_COMMAND: 
			wmId    = LOWORD(wParam);  
			wmEvent = HIWORD(wParam);  
			// Parse the menu selections: 
			switch (wmId) 
			{	 
				case IDM_TOOLS_EXIT: 
					return SendMessage (hWnd, WM_CLOSE, 0, 0); 
 
				case IDM_HELP_ABOUT: 
					DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); 
				    break; 
				case IDOK: 
					SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd); 
					SendMessage (hWnd, WM_CLOSE, 0, 0); 
					break; 
				default: 
				   return DefWindowProc(hWnd, message, wParam, lParam); 
			} 
			break; 
		case WM_CREATE: 
 
			hwndCB = CreateRpCommandBar(hWnd); 
 
            // Initialize the shell activate info structure 
            memset (&s_sai, 0, sizeof (s_sai)); 
            s_sai.cbSize = sizeof (s_sai); 
 
			g_hwndEdit = CreateWindow(L"listbox", L"", WS_VISIBLE|WS_CHILD|WS_VSCROLL|WS_HSCROLL, 
				0, 0, 400, 300, hWnd, 0, hInst, 0); 
 
			// Create a read thread for reading data from the communication port. 
			if (hReadThread = CreateThread (NULL, 0, DoRIL, 0, 0, &dwThreadID)) 
			{ 
				CloseHandle (hReadThread); 
			} 
			else 
			{ 
				// Could not create the read thread. 
				MessageBox (hWnd, TEXT("Unable to create the RIL thread"), 
							TEXT("Error"), MB_OK); 
				dwError = GetLastError (); 
				return FALSE; 
			} 
 
			break; 
	    case WM_SIZE: 
			MoveWindow(g_hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); 
			break; 
		case WM_PAINT: 
			RECT rt; 
			hdc = BeginPaint(hWnd, &ps); 
			GetClientRect(hWnd, &rt); 
			EndPaint(hWnd, &ps); 
			break;  
		case WM_DESTROY: 
			CommandBar_Destroy(hwndCB); 
			PostQuitMessage(0); 
			fclose( logHandle); 
			break; 
		case WM_ACTIVATE: 
            // Notify shell of our activate message 
			SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE); 
     		break; 
		case WM_SETTINGCHANGE: 
			SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai); 
     		break; 
		default: 
			return DefWindowProc(hWnd, message, wParam, lParam); 
   } 
   return 0; 
} 
 
 
HWND CreateRpCommandBar(HWND hwnd) 
{ 
	SHMENUBARINFO mbi; 
 
	memset(&mbi, 0, sizeof(SHMENUBARINFO)); 
	mbi.cbSize     = sizeof(SHMENUBARINFO); 
	mbi.hwndParent = hwnd; 
	mbi.nToolBarId = IDM_MENU; 
	mbi.hInstRes   = hInst; 
	mbi.dwFlags    = 0; 
	mbi.nBmpId     = 0; 
	mbi.cBmpImages = 0; 
 
	if (!SHCreateMenuBar(&mbi))  
		return NULL; 
 
	return mbi.hwndMB; 
} 
 
 
// Mesage handler for the About box. 
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	RECT rt, rt1; 
	int DlgWidth, DlgHeight;	// dialog width and height in pixel units 
	int NewPosX, NewPosY; 
 
	switch (message) 
	{ 
		case WM_INITDIALOG: 
			// trying to center the About dialog 
			if (GetWindowRect(hDlg, &rt1)) { 
				GetClientRect(GetParent(hDlg), &rt); 
				DlgWidth	= rt1.right - rt1.left; 
				DlgHeight	= rt1.bottom - rt1.top ; 
				NewPosX		= (rt.right - rt.left - DlgWidth)/2; 
				NewPosY		= (rt.bottom - rt.top - DlgHeight)/2; 
				 
				// if the About box is larger than the physical screen  
				if (NewPosX < 0) NewPosX = 0; 
				if (NewPosY < 0) NewPosY = 0; 
				SetWindowPos(hDlg, 0, NewPosX, NewPosY, 
					0, 0, SWP_NOZORDER | SWP_NOSIZE); 
			} 
			return TRUE; 
 
		case WM_COMMAND: 
			if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL)) 
			{ 
				EndDialog(hDlg, LOWORD(wParam)); 
				return TRUE; 
			} 
			break; 
	} 
    return FALSE; 
} 
 
 
void addLBMess( TCHAR *f, ...) 
{ 
	va_list args; 
	TCHAR szString[1024]; 
 
	if( logHandle == NULL) { 
		if ((logHandle = fopen("\\Storage\\Application Data\\Volatile\\ril.txt", "w")) == NULL) { 
			wsprintf(szString, L"Error in open of \\Storage\\Application Data\\Volatile\\ril.txt"); 
			SendMessage(g_hwndEdit, LB_ADDSTRING, 0, (LPARAM) szString); 
			 return; 
		} 
	} 
 
	va_start( args, f); 
 
	vswprintf(szString, f, args); 
	SendMessage(g_hwndEdit, LB_ADDSTRING, 0, (LPARAM) szString); 
	vfwprintf( logHandle, f, args); 
	fprintf( logHandle, "\n"); 
	fflush(logHandle); 
	va_end( args); 
 
}