testconns/statdesktop/desktop/source/common/transport/src/cstatmessagecom.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "CSTATMessageCOM.h"
       
    22 #include <initguid.h>
       
    23 #include "ECGUID.H"
       
    24 #include <statcommon.h>
       
    25 
       
    26 //-------------------------------------------------------------------------------
       
    27 //MESSAGE MAP
       
    28 
       
    29 BEGIN_MESSAGE_MAP(CSTATMessageCOM, CCmdTarget)
       
    30 	//{{AFX_MSG_MAP(CSTATMessageCOM)
       
    31 	//}}AFX_MSG_MAP
       
    32 END_MESSAGE_MAP()
       
    33 
       
    34 //-------------------------------------------------------------------------------
       
    35 IMPLEMENT_DYNCREATE(CSTATMessageCOM, CCmdTarget)
       
    36 
       
    37 
       
    38 //constructor
       
    39 CSTATMessageCOM::CSTATMessageCOM()
       
    40 : 	m_pISendReq(NULL), m_pICommonSink(NULL), m_pResponseData(NULL), m_pIECEngineS(NULL),
       
    41 	m_pIECMachine(NULL), m_pIECDrive(NULL), Connected(false), bExternalInterface(false)
       
    42 {}
       
    43 
       
    44 //----------------------------------------------------------------------------
       
    45 //destructor
       
    46 CSTATMessageCOM::~CSTATMessageCOM()
       
    47 {
       
    48 	Disconnect();
       
    49 }
       
    50 
       
    51 //-------------------------------------------------------------------------------
       
    52 
       
    53 BEGIN_INTERFACE_MAP(CSTATMessageCOM, CCmdTarget)
       
    54 	INTERFACE_PART(CSTATMessageCOM, IID_ICommonSink, CommonSink)
       
    55 END_INTERFACE_MAP()
       
    56 
       
    57 //-------------------------------------------------------------------------------
       
    58 // IUnknown Interface 
       
    59 
       
    60 //memory management functions
       
    61 STDMETHODIMP_(ULONG) CSTATMessageCOM::XCommonSink::AddRef()
       
    62 {
       
    63 	METHOD_PROLOGUE(CSTATMessageCOM, CommonSink)
       
    64 	ASSERT(pThis);
       
    65  
       
    66 	TRACE(_T("Info::AddRef %d\n") ,pThis -> m_dwRef);
       
    67 	return pThis -> ExternalAddRef(); 
       
    68 }
       
    69 
       
    70 STDMETHODIMP_(ULONG) CSTATMessageCOM::XCommonSink::Release()
       
    71 {
       
    72 	METHOD_PROLOGUE(CSTATMessageCOM, CommonSink)
       
    73 	ASSERT(pThis);
       
    74 
       
    75 	TRACE(_T("Info::Release %d\n"),pThis -> m_dwRef);
       
    76 	return pThis -> ExternalRelease();
       
    77 }
       
    78 
       
    79 STDMETHODIMP CSTATMessageCOM::XCommonSink::QueryInterface(REFIID riid, void** ppv)
       
    80 {
       
    81 	METHOD_PROLOGUE(CSTATMessageCOM, CommonSink);
       
    82 	TRACE(_T("CSTATMessageCOM::XPWBKInfoQueryInterface\n"));
       
    83 
       
    84 	return pThis -> ExternalQueryInterface(&riid, ppv);
       
    85 }
       
    86 //-------------------------------------------------------------------------------
       
    87 // ICommonSink Interface
       
    88 STDMETHODIMP CSTATMessageCOM::XCommonSink::ProcessResponse(BYTE* Response, DWORD bufsize)
       
    89 {
       
    90 	METHOD_PROLOGUE(CSTATMessageCOM, CommonSink);
       
    91 	ASSERT(pThis);
       
    92 
       
    93 	pThis -> ProcessResponse(Response, bufsize);
       
    94 
       
    95 	return S_OK;
       
    96 }
       
    97 //----------------------------------------------------------------------------
       
    98 //Connection
       
    99 int CSTATMessageCOM::Connect(CString& platformtype, CSTATLogFile *theLog)
       
   100 {
       
   101 	pLog = theLog;
       
   102 
       
   103 	// convert to uppercase
       
   104 	platformtype.MakeUpper();
       
   105 
       
   106 	//select DLL to be initialised
       
   107 	CString STATServer;
       
   108 	if(platformtype == _T("ARM") || platformtype == _T("ARM4"))
       
   109 		STATServer = _T("STATApiARM4.DLL");
       
   110 	else if(platformtype == _T("WINS"))
       
   111 		STATServer = _T("STATApiWINS.DLL");
       
   112 	else if(platformtype == _T("THUMB"))
       
   113 		STATServer = _T("STATApiTHUMB.DLL");
       
   114 	else if(platformtype == _T("WINSCW"))
       
   115 		STATServer = _T("STATApiWINSCW.DLL");
       
   116 	else
       
   117 		return E_BADPLATFORM;
       
   118 
       
   119 	// disconnect if already connected
       
   120 	Disconnect();
       
   121 
       
   122 	// create an instance of the EPOC Connect Engine
       
   123 	if (FAILED(CoCreateInstance(CLSID_ECEngineS, NULL, CLSCTX_INPROC_SERVER, IID_IECEngineS, (void **)&m_pIECEngineS))) 
       
   124 	{
       
   125 		Disconnect();
       
   126 		return E_ENGINEOBJECTFAILURE;
       
   127 	}
       
   128 		
       
   129 	// get ISendReq interface
       
   130 	if (FAILED(ExternalQueryInterface(&IID_ICommonSink, (void**)&m_pICommonSink)))
       
   131 	{
       
   132 		Disconnect();
       
   133 		return E_BADREQUESTINTERFACE;
       
   134 	}
       
   135 	
       
   136 	IFileSystemInfoS *pFSInfo;
       
   137 	LPVOID *ppFSInfo = (LPVOID *)&pFSInfo;
       
   138 
       
   139 	// get an interface pointer to the FileSystemInfo struct
       
   140 	if (!bExternalInterface)
       
   141 	{
       
   142 		ExternalQueryInterface(&IID_IFileSystemInfoS, ppFSInfo);
       
   143 		bExternalInterface = true;
       
   144 	}
       
   145 
       
   146 	// initialise the EPOC Connect engine
       
   147 	m_pIECEngineS -> Init(pFSInfo);
       
   148 	if (m_pIECEngineS->Connect(15000) != S_OK) // connect, giving 15 seconds as maximum wait
       
   149 	{
       
   150 		Disconnect();
       
   151 		return E_ENGINECONNECTFAILURE;
       
   152 	}
       
   153 
       
   154 	// create an instance of the EPOC send request
       
   155 	if (FAILED(CoCreateInstance(CLSID_SendRequest, NULL, CLSCTX_INPROC_SERVER, IID_ISendRequest, (void**) &m_pISendReq)))
       
   156 	{
       
   157 		Disconnect();
       
   158 		return E_REQUESTOBJECTFAILURE;
       
   159 	}
       
   160 
       
   161 	BSTR bSTATstrServer = STATServer.AllocSysString();	//converts from CString to BSTR
       
   162 
       
   163 	//The 'Initialise' function starts up the generic custom server, copying it to the remote 
       
   164 	//EPOC machine if it is not there. It then checks that the application-specific server DLL 
       
   165 	//is loaded and up to date and, if not, copies it over to C:/system/libs. Finally, it 
       
   166 	//checks that the generic custom server found the interface.
       
   167 	if (FAILED(m_pISendReq -> Initialise(bSTATstrServer)))
       
   168 	{
       
   169 		Disconnect();
       
   170 		SysFreeString(bSTATstrServer);
       
   171 		return E_REQUESTINITFAILURE;
       
   172 	}
       
   173 
       
   174 	SysFreeString(bSTATstrServer);
       
   175 	Connected = true;	//flagged
       
   176 	return ITS_OK;
       
   177 }
       
   178 
       
   179 //-------------------------------------------------------------------------------
       
   180 //Disconnection
       
   181 bool CSTATMessageCOM::Disconnect()
       
   182 {
       
   183 	Connected = false;	//flagged as disconnected
       
   184 
       
   185 	if (m_pIECEngineS)
       
   186 	{
       
   187 		m_pIECEngineS->Release();
       
   188 		m_pIECEngineS = NULL;
       
   189 	}
       
   190 
       
   191 	// make sure tell EPOC side to close
       
   192 	if (m_pISendReq)
       
   193 	{
       
   194 		m_pISendReq->Release();
       
   195 		m_pISendReq = NULL;
       
   196 	}
       
   197 
       
   198 	if (bExternalInterface)
       
   199 	{
       
   200 		ExternalRelease();
       
   201 		bExternalInterface = false;
       
   202 	}
       
   203 
       
   204 	if(m_pResponseData)
       
   205 	{
       
   206 		delete[] m_pResponseData;
       
   207 		m_pResponseData = NULL;
       
   208 	}
       
   209 	
       
   210 	return true;
       
   211 }
       
   212 
       
   213 //-------------------------------------------------------------------------------
       
   214 //Send Message to device - no data
       
   215 HRESULT CSTATMessageCOM::SendMsg(BYTE id)
       
   216 {
       
   217 	HRESULT hr = S_OK;
       
   218 
       
   219 	for (int i=0;i<STAT_MAXRETRIES;i++)
       
   220 	{
       
   221 		if (SUCCEEDED(hr = m_pISendReq -> BuildNewRequest(1, id)))	//BuildNewRequest needed for creating header of message
       
   222 			if (SUCCEEDED(hr = m_pISendReq -> SendRequest(m_pICommonSink)))
       
   223 				return hr;
       
   224 
       
   225 		pLog->Set(E_COMMANDRETRY);
       
   226 		Sleep(STAT_RETRYDELAY);
       
   227 	}
       
   228 
       
   229 	pLog->Set(E_COMMANDFAILED);
       
   230 	return hr;
       
   231 }
       
   232 
       
   233 //-------------------------------------------------------------------------------
       
   234 //send message with data
       
   235 HRESULT CSTATMessageCOM::SendMsg(BYTE id, VARIANT data)
       
   236 {
       
   237 	HRESULT hr = S_OK;
       
   238 	
       
   239 	for (int i=0;i<STAT_MAXRETRIES;i++)
       
   240 	{
       
   241 		//will send data that has already been created in calling function
       
   242 		if (SUCCEEDED(hr = m_pISendReq -> BuildNewRequest(1, id)))
       
   243 			if (SUCCEEDED(hr = m_pISendReq -> AddBody(data)))		//adds body to the header (newly converted data)
       
   244 		{
       
   245 			if (SUCCEEDED(hr = m_pISendReq -> SendRequest(m_pICommonSink)))
       
   246 				return hr;
       
   247 		}
       
   248 
       
   249 		pLog->Set(E_COMMANDRETRY);
       
   250 		Sleep(STAT_RETRYDELAY);
       
   251 	}
       
   252 
       
   253 	pLog->Set(E_COMMANDFAILED);
       
   254 	return hr;
       
   255 }
       
   256 
       
   257 //-------------------------------------------------------------------------------
       
   258 // Retrieves a response from EPOC device
       
   259 STDMETHODIMP CSTATMessageCOM::ProcessResponse(BYTE* pResponse, DWORD bufsize)
       
   260 {
       
   261 	CopyResponseData(pResponse + 11, bufsize - 11);	//function called, removing the first 11 bytes (header info)
       
   262 	return S_OK;
       
   263 }
       
   264 
       
   265 //-------------------------------------------------------------------------------
       
   266 //function used to store newly formatted data into new variable
       
   267 void CSTATMessageCOM::CopyResponseData(BYTE* pResponse, DWORD bufsize)
       
   268 {
       
   269 	if(m_pResponseData)
       
   270 	{
       
   271 		delete[] m_pResponseData;
       
   272 		m_pResponseData = NULL;
       
   273 	}
       
   274 	
       
   275 	if(bufsize)
       
   276 	{
       
   277 		m_pResponseData = new BYTE[bufsize];
       
   278 		memcpy(m_pResponseData, pResponse, bufsize);
       
   279 	}
       
   280 }
       
   281 
       
   282 //-----------------------------------------------------------------------------