// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAINFRM_H__1DDDF3B9_ED10_47BF_8EF9_3AD8B814C217__INCLUDED_)
#define AFX_MAINFRM_H__1DDDF3B9_ED10_47BF_8EF9_3AD8B814C217__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>
#include "BMAGlobal.h"

#include "IBMAFramework.h"

#include "SettingsSheet.h"

#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include "BootModeitf.h"
#include "BootModeguid.h"

#include "CoolToolBar.h"
#include "FlashInfo.h"
#include "./phasecheck/PhaseCheckBuild.h"

#ifdef _DOWNLOAD_FOR_PRODUCTION
#include "BarcodeDlg.h"
#define WM_REQUIRE_SN			(WM_USER + 0x1213)
#endif

#define PRODUCTION_INFO_SIZE    (MAX_PRODUCTIONINFO_SIZE)         //8K
#define X_SN_LEN                (MAX_SN_LEN)


#if defined(_DOWNLOAD_FOR_PRODUCTION) || defined(_SPUPGRADE)
struct PORT_DATA
{
    DWORD  dwPort;
    LPBYTE lpPhaseCheck;
	HANDLE hSNEvent;
};
#endif

#define SAFE_DELETE(p)  \
do \
{\
    if (p != NULL) \
       delete p; \
} while(0) \

#define SAFE_DELETE_ARRAY(p)  \
do \
{\
    if (p != NULL) {\
       delete []p; \
	   p=NULL;\
	}\
} while(0) \

#define  MAX_RECEIVE_SIZE   0x0800

#define WM_STOP_ONE_PORT		(WM_USER + 0x1214)

struct _BMOBJ
{
	_BMOBJ()
	{
		memset(this,0, sizeof(_BMOBJ));
	}

    DWORD dwCookie;
	DWORD dwIsUart;
	DWORD dwBufSize;
	BYTE* pBuf;
	char  szSN[X_SN_LEN+1];
	TCHAR szErrorMsg[_MAX_PATH*2];
};

typedef struct _EXT_IMG_INFO
{
	_EXT_IMG_INFO()
	{
		memset(this,0, sizeof(_EXT_IMG_INFO));
	}
	DWORD dwSize;
	BYTE *pBuf;
	TCHAR szFilePath[MAX_PATH];
}EXT_IMG_INFO,*EXT_IMG_INFO_PTR;

typedef CMap<DWORD,DWORD,EXT_IMG_INFO_PTR,EXT_IMG_INFO_PTR> MAP_EXTIMG;


struct BMFileInfo_TAG;

class CMainFrame : public CFrameWnd
{
	
protected: // create from serialization only
	CMainFrame();
	DECLARE_DYNCREATE(CMainFrame)

// Attributes
protected:
    BOOL m_bStarted;


   

    BYTE m_lpReceiveBuffer[MAX_RECEIVE_SIZE];
    DWORD m_dwReceiveSize;
	long  m_lStartNVRef;

    int       m_nFileCount;
    CStringArray m_arrFile;

	IBMAFramework *m_pBMAF; 
	
	CString m_strPacketPath;

	CMap<DWORD, DWORD, _BMOBJ*, _BMOBJ* > m_mapBMObj;

public:
	CStringArray m_aPacReleaseDir;
	CString m_strSpecConfig;
	LPBYTE m_pNVFileBuf;
	DWORD  m_dwNvFileSize;
	CFlashInfo m_FlashInfo;

	CSettingsSheet m_sheetSettings;
	BOOL m_bPacketOpen;
// Operations
public:
    HRESULT InitModule( DWORD dwOprCookie );
	BOOL GetIniFilePath(LPTSTR pName);
	CString GetSpecConfigFile();
    void SetPrdVersion(LPCTSTR lpszSpdVer);	

#if defined(_DOWNLOAD_FOR_PRODUCTION) || defined(_SPUPGRADE)
	PORT_DATA * GetPortDataByPort(int nPort);
#endif

	static DWORD WINAPI GetThreadFunc(LPVOID lpParam);
	DWORD ClosePortFunc(DWORD dwPort);
	BOOL StartOnePortWork(DWORD dwPort, BOOL bUart);

protected:
    //@ Liu Kai 2004-3-25 CR8123
    BOOL LoadSettings();
    void SaveSettings();

    
    static DWORD WINAPI GetRcvDataThread( LPVOID lpParam );
    DWORD  RcvDataThreadProc( DWORD pChannel );
    HRESULT ProcessData( DWORD pChannel , LPBYTE lpData, int iDataLen );
    
    

    
	BOOL StopOnePortWork(DWORD dwPort,BOOL bRemoved = FALSE);

    void StartWork();
	
	DWORD BCDToWString(LPBYTE pBcd,DWORD dwSize,LPTSTR szStr,DWORD dwStrLen);

	void DoReport(DWORD dwOprCookie,BOOL bSuccess = TRUE);
	
	BOOL CheckDLFiles();
	BOOL InitReportInfo();
	BOOL InitNVBuffer();
	BOOL InitUDiskBufMap();	
	BOOL InitChipDspBufmap();

	void ClearExtImgMap(MAP_EXTIMG &map);
	
	void GetOprErrorCodeDescription(DWORD dwErrorCode,
									LPTSTR lpszErrorDescription, 
									int nSize );
        

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMainFrame)
	public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	protected:
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMainFrame();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
	CStatusBar		m_wndStatusBar;
	CCoolToolBar    m_wndToolBar;	

    void* m_pMasterImg;

	CRITICAL_SECTION m_csReportFile;
	FILE * m_pReportFile;

	DWORD  m_dwMaxNVLength;

#if defined(_DOWNLOAD_FOR_PRODUCTION) || defined(_SPUPGRADE)
	PORT_DATA* CreatePortData( DWORD nPort );
	CMap<DWORD,DWORD,PORT_DATA*,PORT_DATA*> m_mapPortData;
	CRITICAL_SECTION m_cs;
#endif

	MAP_EXTIMG m_mapUDiskIMg;
	MAP_EXTIMG m_mapChipDsp;

	CString    m_strOprErrorConfigFile;

// Generated message map functions
protected:
	BOOL m_bAutoStart;
	//{{AFX_MSG(CMainFrame)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnSettings();
	afx_msg void OnStart();
	afx_msg void OnStop();
	afx_msg void OnUpdateStart(CCmdUI* pCmdUI);
	afx_msg void OnUpdateStop(CCmdUI* pCmdUI);
	afx_msg void OnClose();
	afx_msg void OnUpdateSettings(CCmdUI* pCmdUI);
	afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);
	afx_msg void OnLoadPacket();
	afx_msg void OnUpdateLoadPacket(CCmdUI* pCmdUI);
	//}}AFX_MSG
	afx_msg void OnWarnMessageBox(WPARAM wParam,LPARAM lpParam);
	afx_msg void OnInitalPacket(WPARAM wParam,LPARAM lpParam);
	afx_msg void OnStopOnePort(WPARAM wParam,LPARAM lpParam);
    
	DECLARE_MESSAGE_MAP()

    BEGIN_INTERFACE_PART( BMOprObserver, IBMOprObserver )
    
	STDMETHOD(OnStart)( THIS_ DWORD dwOprCookie , 
						DWORD dwResult );       
    
	STDMETHOD(OnEnd)(  THIS_ DWORD dwOprCookie, 
		               DWORD dwResult ); 
        
	STDMETHOD(OnOperationStart)(  THIS_ DWORD dwOprCookie, 
		                            const BSTR cbstrFileID,
									const BSTR cbstrFileType,		
									const BSTR cbstrOperationType,
									DWORD pBMFileInterface );     
    
	STDMETHOD(OnOperationEnd)(  THIS_ DWORD dwOprCookie, 
								const BSTR cbstrFileID,
								const BSTR cbstrFileType, 
								const BSTR cbstrOperationType, 
								DWORD dwResult,
								DWORD pBMFileInterface );  
    
	STDMETHOD(OnFileOprStart)( THIS_ DWORD dwOprCookie,
								const BSTR cbstrFileID, 
								const BSTR cbstrFileType , 
								DWORD pBMFileInterface );
    
	STDMETHOD(OnFileOprEnd)(    THIS_ DWORD dwOprCookie, 
								const BSTR cbstrFileID,
								const BSTR cbstrFileType, 
								DWORD dwResult );
	
	STDMETHOD(OnFilePrepare)(THIS_ DWORD dwOprCookie,
							 const BSTR bstrProduct,
							 const BSTR bstrFileName,
							 DWORD lpFileInfo,
							 /*[out]*/ DWORD      pBMFileInfoArr,
							 /*[out]*/ LPDWORD    lpBMFileInfoCount,
							 /*[out]*/ LPDWORD    lpdwFlag);
        
    END_INTERFACE_PART( BMOprObserver )
        
    DECLARE_INTERFACE_MAP()
        
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINFRM_H__1DDDF3B9_ED10_47BF_8EF9_3AD8B814C217__INCLUDED_)
