// GobiCarrierFWSelect.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "GobiCarrierFWSelect.h"
#include "Gobi.h"
#include <windows.h>
#include <commctrl.h>

#define MAX_LOADSTRING 100
#define	MAX_BUF_SIZE 255

// Global Variables:
HINSTANCE			g_hInst;			// current instance
#ifdef SHELL_AYGSHELL
HWND				g_hWndMenuBar;		// menu bar handle
#else // SHELL_AYGSHELL
HWND				g_hWndCommandBar;	// command bar handle
#endif // SHELL_AYGSHELL

// Forward declarations of functions included in this code module:
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	MainDlgProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

#define MAX_STRING_SIZE 0xFF
TCHAR g_szVoiceNum[MAX_STRING_SIZE];
DWORD g_dwCarrierIndex = NULL; 
TCHAR g_szTechType[MAX_STRING_SIZE]; 
TCHAR g_szNetStatus[MAX_STRING_SIZE];

int g_iNewCarrierIndex = 0;

CGobi theGobiObj;




int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
  MSG msg;

  // Perform application initialization:
  if (!InitInstance(hInstance, nCmdShow)) 
  {
    return FALSE;
  }

  HACCEL hAccelTable;
  hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GOBICARRIERFWSELECT));

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
  WNDCLASS wc;

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GOBICARRIERFWSELECT));
  wc.hCursor       = 0;
  wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName  = 0;
  wc.lpszClassName = szWindowClass;

  return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HINSTANCE, 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;
  TCHAR szTitle[MAX_LOADSTRING];		// title bar text
  TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name

  g_hInst = hInstance; // Store instance handle in our global variable

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
  // SHInitExtraControls should be called once during your application's initialization to initialize any
  // of the device specific controls such as CAPEDIT and SIPPREF.
  SHInitExtraControls();
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP

  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
  LoadString(hInstance, IDC_GOBICARRIERFWSELECT, szWindowClass, MAX_LOADSTRING);

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
  //If it is already running, then focus on the window, and exit
  hWnd = FindWindow(szWindowClass, szTitle);	
  if (hWnd) 
  {
    // set focus to foremost child window
    // The "| 0x00000001" is used to bring any owned windows to the foreground and
    // activate them.
    SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
    return 0;
  } 
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP

  if (!MyRegisterClass(hInstance, szWindowClass))
  {
    return FALSE;
  }

  hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
    return FALSE;
  }

#ifdef WIN32_PLATFORM_PSPC
  // 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
  if (g_hWndMenuBar)
  {
    RECT rc;
    RECT rcMenuBar;

    GetWindowRect(hWnd, &rc);
    GetWindowRect(g_hWndMenuBar, &rcMenuBar);
    rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

    MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
  }
#endif // WIN32_PLATFORM_PSPC

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  //initialize Gobi API here
  if(!theGobiObj.Initialize())
  {
    // their were problems
    MessageBox (NULL, _T("Failed to Init GobiAPI"), _T("Error"), MB_OK);
    return FALSE;
  }
  // create main dialog for Carrier Switching
  DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), hWnd, MainDlgProc);

#ifndef SHELL_AYGSHELL
  if (g_hWndCommandBar)
  {
    CommandBar_Show(g_hWndCommandBar, TRUE);
  }
#endif // !SHELL_AYGSHELL

  return TRUE;
}


//
//   FUNCTION: GetCurrentCarrier(TCHAR* szCarrierName)
//
//   PURPOSE: Query the current carrier
//
void GetCurrentCarrier(TCHAR* szCarrierName)
{
	BYTE pImageList[MAX_BUF_SIZE];
	ULONG imgListSize = MAX_BUF_SIZE;
	memset(pImageList, NULL, MAX_BUF_SIZE);
		
	if(! theGobiObj.m_pFnGetImagesPreference(&imgListSize, pImageList))
	{
		CHAR *pch;
		DWORD dwCarrierIndex;
		pch = strstr((char *)pImageList+2, "UQSU"); //pointer + 2 to skip the NULL char.
		if(pch)
		{
			dwCarrierIndex = *(pch+4);
			switch(dwCarrierIndex)
			{
			case 'V':	
			{
				_tcscpy(szCarrierName, _T("Verizon"));
				break;
			}
	
			case 'A':	
			{
				_tcscpy(szCarrierName, _T("AT&T"));
				break;
			}
			case 'S':	
			{
				_tcscpy(szCarrierName, _T("Sprint"));
				break;
			}
			case 'G':	
			{
				_tcscpy(szCarrierName, _T("Roger"));
				break;
			}
			case 'C':	
			{
				_tcscpy(szCarrierName, _T("China Telecom"));
				break;
			} 
			} // end switch
		}
		else
			_tcscpy(szCarrierName, _T("Unknown"));
	}
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  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)
{
  int wmId, wmEvent;
  PAINTSTRUCT ps;
  HDC hdc;

#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
  static SHACTIVATEINFO s_sai;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP

  switch (message) 
  {
  case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_HELP_ABOUT:
      DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
      break;
#ifndef SHELL_AYGSHELL
      //                case IDM_FILE_EXIT:
      //                    DestroyWindow(hWnd);
      //                    break;
#endif // !SHELL_AYGSHELL
#ifdef WIN32_PLATFORM_PSPC
    case IDM_OK:
      SendMessage (hWnd, WM_CLOSE, 0, 0);				
      break;
#endif // WIN32_PLATFORM_PSPC
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
  case WM_CREATE:
#ifndef SHELL_AYGSHELL
    g_hWndCommandBar = CommandBar_Create(g_hInst, hWnd, 1);
    CommandBar_InsertMenubar(g_hWndCommandBar, g_hInst, IDR_MENU, 0);
    CommandBar_AddAdornments(g_hWndCommandBar, 0, 0);
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
    SHMENUBARINFO mbi;

    memset(&mbi, 0, sizeof(SHMENUBARINFO));
    mbi.cbSize     = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hWnd;
    mbi.nToolBarId = IDR_MENU;
    mbi.hInstRes   = g_hInst;

    if (!SHCreateMenuBar(&mbi)) 
    {
      g_hWndMenuBar = NULL;
    }
    else
    {
      g_hWndMenuBar = mbi.hwndMB;
    }

    // Initialize the shell activate info structure
    memset(&s_sai, 0, sizeof (s_sai));
    s_sai.cbSize = sizeof (s_sai);
#endif // SHELL_AYGSHELL
    break;
  case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    // TODO: Add any drawing code here...

    EndPaint(hWnd, &ps);
    break;
  case WM_DESTROY:
#ifndef SHELL_AYGSHELL
    CommandBar_Destroy(g_hWndCommandBar);
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
    CommandBar_Destroy(g_hWndMenuBar);
#endif // SHELL_AYGSHELL
    PostQuitMessage(0);
    break;

#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
  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;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP


  default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

// Message handler for the main dialog box
INT_PTR CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  HWND hComboBox;
  HWND hPathEdit;
  TCHAR szCarrierName[81];
  TCHAR szFW_Path[MAX_PATH];
  HKEY hKey;
  LONG lResult;
  DWORD dwType;
  DWORD dwForceFWDownload=0;

  HANDLE hFind = INVALID_HANDLE_VALUE;
  WIN32_FIND_DATA ffd;
  DWORD dwLen = MAX_STRING_SIZE - 1;
  WCHAR szDir[MAX_STRING_SIZE];
  WCHAR szTemp[MAX_STRING_SIZE];
  HANDLE hFile;
  DWORD dwBytesRead;
  CHAR mapInfo[MAX_STRING_SIZE];
  CHAR folderName[10];
  DWORD dwCnt =0;


  memset(szCarrierName, 0, sizeof(szCarrierName));
  switch (message)
  {
  case WM_INITDIALOG:
	{
#ifndef SHELL_AYGSHELL
		RECT rectChild, rectParent;
		int DlgWidth, DlgHeight;	// dialog width and height in pixel units
		int NewPosX, NewPosY;

		// trying to center the About dialog
		if (GetWindowRect(hDlg, &rectChild)) 
		{
		  GetClientRect(GetParent(hDlg), &rectParent);
		  DlgWidth	= rectChild.right - rectChild.left;
		  DlgHeight	= rectChild.bottom - rectChild.top ;
		  NewPosX		= (rectParent.right - rectParent.left - DlgWidth) / 2;
		  NewPosY		= (rectParent.bottom - rectParent.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);
		}
#else    
      // Create a Done button and size it.
      SHINITDLGINFO shidi;
      shidi.dwMask = SHIDIM_FLAGS;
      shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
      shidi.hDlg = hDlg;
      SHInitDialog(&shidi);
#endif // SHELL_AYGSHELL
      hComboBox = GetDlgItem (hDlg, IDC_COMBO_SELECT_CARRIER);
     // TCHAR szCarrierName[MAX_STRING_SIZE]; 
  
	  memset(szDir, NULL, sizeof(szDir));
	  memset(szTemp, NULL, sizeof(szTemp));
	  lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
	  if(lResult == ERROR_SUCCESS)
	  {
		lResult = RegQueryValueEx(hKey,_T("FwRootLocation"),0, &dwType,(LPBYTE)&szDir, &dwLen);
		if ((lResult == ERROR_FILE_NOT_FOUND) || (szDir[0]==0))
		{
			wsprintf(szDir, L"\\IPSM\\Images\\Sierra");
			lResult = RegSetValueEx(hKey,_T("FwRootLocation"),0, REG_SZ,(LPBYTE)&szDir, (wcslen(szDir)+1)*2);
		}		

		lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
		if(lResult == ERROR_SUCCESS)
		{
		  dwLen=sizeof(dwForceFWDownload);
		  lResult = RegQueryValueEx(hKey,_T("ForceFWDownload"),0, &dwType,(LPBYTE)&dwForceFWDownload, &dwLen);
		  if (ERROR_FILE_NOT_FOUND==lResult)
		  {
			  dwLen=sizeof(dwForceFWDownload);
			  lResult = RegSetValueEx(hKey,_T("ForceFWDownload"),0, REG_DWORD,(LPBYTE)&dwForceFWDownload, sizeof(dwForceFWDownload));
		  }
		  CloseHandle(hKey);
		}
		if (dwForceFWDownload) 
			SendMessage(GetDlgItem (hDlg, IDC_FORCEDOWNLOAD), BM_SETCHECK, BST_CHECKED, 0);
		else
			SendMessage(GetDlgItem (hDlg, IDC_FORCEDOWNLOAD), BM_SETCHECK, BST_UNCHECKED, 0);


		if(lResult == ERROR_SUCCESS)
		{
			SetWindowText(GetDlgItem(hDlg, IDC_EDIT_PATH), szDir);

			// scan the firmware directory
			swprintf(szTemp, L"%s\\*", szDir);
			hFind = FindFirstFile(szTemp, &ffd);
			
			if (INVALID_HANDLE_VALUE == hFind) 
		    {
			  MessageBox (NULL, _T("Failed open firmware location"), _T("Error"), MB_OK);
			  return (INT_PTR)FALSE;
		    } 
			
			// read mapping file carrier.txt
			memset(szTemp, NULL, sizeof(szTemp));
			swprintf(szTemp, L"%s\\carrier.txt", szDir);
			hFile = CreateFile (szTemp, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);                  

			if (hFile == INVALID_HANDLE_VALUE)
			{
				MessageBox (NULL, _T("Could not open carrier.txt"), _T("Error"), MB_OK);
			    return (INT_PTR)FALSE;
			}

			ReadFile (hFile, mapInfo, MAX_STRING_SIZE, &dwBytesRead, NULL);
			CloseHandle (hFile);
			
			// map the folder to CARRIERINFO
		   do
		   {
			
			  if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			  {
				  CHAR *pch, *pch2;
			      pch = mapInfo;
				  WideCharToMultiByte(CP_ACP, 0, ffd.cFileName, -1, folderName, 10, NULL, NULL);
		  
				  pch = strstr(pch, folderName);
				  if(pch)
				  {
					  
					  wcscpy(theGobiObj.FwAvalible[dwCnt].FolderName, ffd.cFileName);
					  pch = strstr(pch, ",");
					  pch++;
					  theGobiObj.FwAvalible[dwCnt].FwSlot = atoi(pch);
					  pch = strstr(pch, ",");
					  pch++;
					  pch2 = strchr(pch, ';');
					  MultiByteToWideChar(CP_ACP, 0, pch, pch2-pch, theGobiObj.FwAvalible[dwCnt].CarrierName, MAX_STRING_SIZE);
					  
					  SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM)theGobiObj.FwAvalible[dwCnt].CarrierName);
					  dwCnt++;
				  }
			  }
		   }
		   while (FindNextFile(hFind, &ffd) != 0);
			
		}
	  }	

		hPathEdit= GetDlgItem(hDlg, IDC_EDIT_PATH);
		memset(szFW_Path, 0, sizeof(szFW_Path));
		lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
		if(lResult == ERROR_SUCCESS)
		{
		  dwLen=sizeof(szFW_Path);
		  lResult = RegQueryValueEx(hKey,_T("FwRootLocation"),0, &dwType,(LPBYTE)&szFW_Path, &dwLen);
		  CloseHandle(hKey);
		}
		SetWindowText(hPathEdit, szFW_Path);

      // connect to Gobi device
      if (theGobiObj.m_pFnConnect() == 0)
      {
        if (! theGobiObj.GetVoiceNumberInfo(g_szVoiceNum))
        {
        // their were problems 
        //MessageBox (NULL, _T("Failed to retrieve Phn No"), _T("Error"), MB_OK);
		  SetDlgItemText(hDlg, IDC_PHONE_NUMBER, (LPCTSTR) L"Phone no. unknown");
        }
        else
        {
          SetDlgItemText(hDlg, IDC_PHONE_NUMBER, (LPCTSTR) g_szVoiceNum);
        }

		
	    GetCurrentCarrier(szCarrierName); // GetCurrentCarrier haven't tested in Gobi3K
 		SetDlgItemText(hDlg, IDC_CURRENT_CARRIER, szCarrierName);
		
		theGobiObj.GetNetworkInfo(g_szTechType, g_szNetStatus);
        SetDlgItemText(hDlg, IDC_TECHNOLOGY, g_szTechType);
        SetDlgItemText(hDlg, IDC_NETWORK_STATUS, g_szNetStatus);
      }
      else
      {
#ifndef SHELL_AYGSHELL	  
	    MessageBox (NULL, _T("Failed to Connect to Gobi"), _T("Error"), MB_OK);
		EnableWindow( GetDlgItem( hDlg, IDSWITCH ), FALSE );
		return (INT_PTR)FALSE;
#else // !SHELL_AYGSHELL
		MessageBox (NULL, _T("Failed to Connect to Gobi"), _T("Error"), MB_OK);
        return (INT_PTR)FALSE;
#endif // SHELL_AYGSHELL		
      }	
    }


    return (INT_PTR)TRUE;

  case WM_COMMAND:
#ifndef SHELL_AYGSHELL
    if ((LOWORD(wParam) == IDCANCEL))
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
    if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
#endif
    {
        EndDialog(hDlg, LOWORD(wParam));
        PostQuitMessage (0);  // this will terminate the app as a whole!
        return (INT_PTR)TRUE;
    }
    else if ((LOWORD(wParam) == IDSWITCH))  // IDOK handles the 'Switch' button in dlg
    {
		{
			lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
			if(lResult == ERROR_SUCCESS)
			{
				memset(szDir, 0, sizeof(szDir));
				dwLen=sizeof(szDir);
				lResult = RegQueryValueEx(hKey,_T("FwRootLocation"),0, &dwType,(LPBYTE)&szDir, &dwLen);
				if(lResult == ERROR_SUCCESS)
				{ 
					WCHAR finalPath[MAX_STRING_SIZE];
					memset(finalPath, 0, sizeof(finalPath));
					wsprintf(finalPath, _T("%s\\%s"),szDir, theGobiObj.FwAvalible[g_iNewCarrierIndex].FolderName);
					lResult = RegSetValueEx(hKey,_T("FirmwareLocation"),0, REG_SZ,(LPBYTE)&finalPath, (wcslen(finalPath)+1)*2);
				}	
				CloseHandle(hKey);
			}
		}

        DWORD ret=theGobiObj.DoFirmwareUpgrade(g_iNewCarrierIndex);
		if (ret!=0)
        {
          // their were problems
			switch (ret)
			{ 
			 	case 1:
					MessageBox (NULL, _T("Filename/Path Not Found"), _T("Error"), MB_OK);
					break;
				case 2:
					MessageBox (NULL, _T("Unknown Carrier"), _T("Error"), MB_OK);
					break;
				case 3:
					MessageBox (NULL, _T("Cannot perform a carrier switch. Previous carrier ModemImageID not found"), _T("Error"), MB_OK);
					break;
				case 4:
					MessageBox (NULL, _T("Cannot perform a carrier switch. Previous carrier ModemBuildID not found"), _T("Error"), MB_OK);
					break;
				case 5:
					MessageBox (NULL, _T("Cannot perform a carrier switch. Previous carrier PRIImageID not found"), _T("Error"), MB_OK);
					break;
				case 6:
					MessageBox (NULL, _T("Cannot perform a carrier switch. Previous carrier PRIBuildID not found"), _T("Error"), MB_OK);
					break;
				case 7:
					MessageBox (NULL, _T("Failed to put the modem to offline mode"), _T("Error"), MB_OK);
					break;
				case 8:
					MessageBox (NULL, _T("Failed to reset the modem"), _T("Error"), MB_OK);
					break;
				case 9:
					MessageBox (NULL, _T("Failed to open the HKLM\\Software\\Sierra Wireless Inc\\QDL registry key"), _T("Error"), MB_OK);
					break;
				default:
					WCHAR erroStr[MAX_STRING_SIZE];
					wsprintf(erroStr, _T("Unknown Error %d"), ret);
					MessageBox (NULL, erroStr, _T("Error"), MB_OK);
					break;
			}
          return (INT_PTR)FALSE;
        }

        MessageBox (NULL, _T("Firmware will update, Application will exit"), _T("Note"), MB_OK);
        //The reset is taken care by the UpgradeFirmware2K API.
        //theGobiObj.m_pFnSetPower(3); 
        //theGobiObj.m_pFnSetPower(4); // reset the module

        EndDialog(hDlg, LOWORD(wParam));
        PostQuitMessage (0);  // this will terminate the app as a whole!
    }
	else if (LOWORD(wParam) == IDC_FORCEDOWNLOAD)
	{

		if (SendMessage(GetDlgItem (hDlg, IDC_FORCEDOWNLOAD),BM_GETCHECK,0,0) ==BST_CHECKED)
			dwForceFWDownload=1;
		else
			dwForceFWDownload=0;

		lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
		if(lResult == ERROR_SUCCESS)
		{
		  dwLen=sizeof(dwForceFWDownload);
		  lResult = RegSetValueEx(hKey,_T("ForceFWDownload"),0, REG_DWORD,(LPBYTE)&dwForceFWDownload, sizeof(dwForceFWDownload));
		  CloseHandle(hKey);
		}
	}
    else if (LOWORD(wParam) == IDC_COMBO_SELECT_CARRIER)
    {
        HWND hComboBox = GetDlgItem (hDlg, IDC_COMBO_SELECT_CARRIER);
        switch(HIWORD(wParam)) // Find out what message it was
        {
        case CBN_DROPDOWN: // This means that the list is about to display

          break;
        case CBN_CLOSEUP:
          {
            DWORD index = CB_ERR;
            index = SendMessage (hComboBox, CB_GETCURSEL, 0, 0);
			
			g_iNewCarrierIndex = index;
          }
          break;
        }
    }
	else if ((HIWORD(wParam)==EN_CHANGE)&& (LOWORD(wParam)==IDC_EDIT_PATH))
	{

		GetWindowText(GetDlgItem(hDlg, IDC_EDIT_PATH), szFW_Path, MAX_PATH);

		lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Sierra Wireless Inc\\QDL"), 0, 0 , &hKey );
		if(lResult == ERROR_SUCCESS)
		{
		  lResult = RegSetValueEx(hKey,_T("FwRootLocation"),0, REG_SZ,(LPBYTE)&szFW_Path, (wcslen(szFW_Path)+1)*2);
		  CloseHandle(hKey);
		}
	}
    break;

  case WM_DESTROY:

    if (theGobiObj.m_pFnDisconnect) 
    {
        theGobiObj.m_pFnDisconnect();
        theGobiObj.Deinit(); 
        FreeLibrary(theGobiObj.m_hGobiApi);
    }
    return (INT_PTR)TRUE;
  }
  return (INT_PTR)FALSE;
}



// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
  case WM_INITDIALOG:
#ifndef SHELL_AYGSHELL
    RECT rectChild, rectParent;
    int DlgWidth, DlgHeight;	// dialog width and height in pixel units
    int NewPosX, NewPosY;

    // trying to center the About dialog
    if (GetWindowRect(hDlg, &rectChild)) 
    {
      GetClientRect(GetParent(hDlg), &rectParent);
      DlgWidth	= rectChild.right - rectChild.left;
      DlgHeight	= rectChild.bottom - rectChild.top ;
      NewPosX		= (rectParent.right - rectParent.left - DlgWidth) / 2;
      NewPosY		= (rectParent.bottom - rectParent.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);
    }
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
    {
      // Create a Done button and size it.  
      SHINITDLGINFO shidi;
      shidi.dwMask = SHIDIM_FLAGS;
      shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
      shidi.hDlg = hDlg;
      SHInitDialog(&shidi);
    }
#endif // SHELL_AYGSHELL

    return (INT_PTR)TRUE;

  case WM_COMMAND:
#ifndef SHELL_AYGSHELL
    if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
      if (LOWORD(wParam) == IDOK)
#endif
      {
        EndDialog(hDlg, LOWORD(wParam));
        return (INT_PTR)TRUE;
      }
      break;

  case WM_CLOSE:
    EndDialog(hDlg, message);
    return (INT_PTR)TRUE;

  }
  return (INT_PTR)FALSE;
}
