0
|
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 |
// testcomDlg.cpp : implementation file
|
|
22 |
//
|
|
23 |
|
|
24 |
#include "stdafx.h"
|
|
25 |
#include "testcom.h"
|
|
26 |
#include "testcomDlg.h"
|
|
27 |
#include <statcommon.h>
|
|
28 |
#include <statcomms.h>
|
|
29 |
|
|
30 |
#ifdef _DEBUG
|
|
31 |
#define new DEBUG_NEW
|
|
32 |
#undef THIS_FILE
|
|
33 |
static char THIS_FILE[] = __FILE__;
|
|
34 |
#endif
|
|
35 |
|
|
36 |
DWORD WINAPI ThreadProcOut(LPVOID lpParameter);
|
|
37 |
void ProcessInput(STATComms *pComms, CTestcomDlg *pDlg);
|
|
38 |
|
|
39 |
bool bFinished = true;
|
|
40 |
bool bReset = true;
|
|
41 |
char szBitmapFile[] = "C:\\basicscreen.mbm";
|
|
42 |
char szReceiveFile[] = "C:\\basicapp.exe";
|
|
43 |
|
|
44 |
/////////////////////////////////////////////////////////////////////////////
|
|
45 |
// CTestcomDlg dialog
|
|
46 |
|
|
47 |
CTestcomDlg::CTestcomDlg(CWnd* pParent /*=NULL*/)
|
|
48 |
: CDialog(CTestcomDlg::IDD, pParent)
|
|
49 |
{
|
|
50 |
//{{AFX_DATA_INIT(CTestcomDlg)
|
|
51 |
// NOTE: the ClassWizard will add member initialization here
|
|
52 |
//}}AFX_DATA_INIT
|
|
53 |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
|
54 |
}
|
|
55 |
|
|
56 |
CTestcomDlg::~CTestcomDlg()
|
|
57 |
{
|
|
58 |
if (hThread2Out)
|
|
59 |
CloseHandle(hThread2Out);
|
|
60 |
}
|
|
61 |
|
|
62 |
void CTestcomDlg::DoDataExchange(CDataExchange* pDX)
|
|
63 |
{
|
|
64 |
CDialog::DoDataExchange(pDX);
|
|
65 |
//{{AFX_DATA_MAP(CTestcomDlg)
|
|
66 |
DDX_Control(pDX, IDC_PORT, m_Port);
|
|
67 |
DDX_Control(pDX, IDC_TRANSPORT, m_Transport);
|
|
68 |
DDX_Control(pDX, IDC_LIST1, m_List1);
|
|
69 |
//}}AFX_DATA_MAP
|
|
70 |
}
|
|
71 |
|
|
72 |
BEGIN_MESSAGE_MAP(CTestcomDlg, CDialog)
|
|
73 |
//{{AFX_MSG_MAP(CTestcomDlg)
|
|
74 |
ON_WM_PAINT()
|
|
75 |
ON_WM_QUERYDRAGICON()
|
|
76 |
ON_BN_CLICKED(IDC_TRANSFER, OnTransfer)
|
|
77 |
//}}AFX_MSG_MAP
|
|
78 |
END_MESSAGE_MAP()
|
|
79 |
|
|
80 |
/////////////////////////////////////////////////////////////////////////////
|
|
81 |
// CTestcomDlg message handlers
|
|
82 |
|
|
83 |
BOOL CTestcomDlg::OnInitDialog()
|
|
84 |
{
|
|
85 |
CDialog::OnInitDialog();
|
|
86 |
|
|
87 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
|
88 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
|
89 |
|
|
90 |
// defaults
|
|
91 |
m_Transport.SetCurSel(1);
|
|
92 |
m_Port.SetWindowText("3000");
|
|
93 |
m_Port.SetLimitText(25);
|
|
94 |
|
|
95 |
return TRUE; // return TRUE unless you set the focus to a control
|
|
96 |
}
|
|
97 |
|
|
98 |
// If you add a minimize button to your dialog, you will need the code below
|
|
99 |
// to draw the icon. For MFC applications using the document/view model,
|
|
100 |
// this is automatically done for you by the framework.
|
|
101 |
|
|
102 |
void CTestcomDlg::OnPaint()
|
|
103 |
{
|
|
104 |
if (IsIconic())
|
|
105 |
{
|
|
106 |
CPaintDC dc(this); // device context for painting
|
|
107 |
|
|
108 |
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
|
|
109 |
|
|
110 |
// Center icon in client rectangle
|
|
111 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
|
112 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
|
113 |
CRect rect;
|
|
114 |
GetClientRect(&rect);
|
|
115 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
|
116 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
|
117 |
|
|
118 |
// Draw the icon
|
|
119 |
dc.DrawIcon(x, y, m_hIcon);
|
|
120 |
}
|
|
121 |
else
|
|
122 |
{
|
|
123 |
CDialog::OnPaint();
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
HCURSOR CTestcomDlg::OnQueryDragIcon()
|
|
128 |
{
|
|
129 |
return (HCURSOR) m_hIcon;
|
|
130 |
}
|
|
131 |
|
|
132 |
|
|
133 |
///////////////////////////////////////////////////////////////////////////////////
|
|
134 |
// Start it all
|
|
135 |
///////////////////////////////////////////////////////////////////////////////////
|
|
136 |
void CTestcomDlg::OnTransfer()
|
|
137 |
{
|
|
138 |
if (bFinished)
|
|
139 |
{
|
|
140 |
bReset = true;
|
|
141 |
DWORD dwThreadID2Out;
|
|
142 |
if (!(hThread2Out = CreateThread( NULL, // security attributes
|
|
143 |
0, // stack size
|
|
144 |
ThreadProcOut, // proc to call
|
|
145 |
this, // proc parameter
|
|
146 |
0, // creation flags
|
|
147 |
&dwThreadID2Out))) // thread identifier
|
|
148 |
{
|
|
149 |
AfxMessageBox("Could not create listening thread");
|
|
150 |
}
|
|
151 |
}
|
|
152 |
else
|
|
153 |
{
|
|
154 |
bFinished = true; // signal thread to stop
|
|
155 |
AddString("Stopping...");
|
|
156 |
Sleep(250); // ...and wait for it
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
|
|
161 |
///////////////////////////////////////////////////////////////////////////////////
|
|
162 |
// Thread entry point
|
|
163 |
///////////////////////////////////////////////////////////////////////////////////
|
|
164 |
DWORD WINAPI ThreadProcOut(LPVOID lpParameter)
|
|
165 |
{
|
|
166 |
CTestcomDlg *pDlg = (CTestcomDlg *)lpParameter;
|
|
167 |
|
|
168 |
STATComms *pComms;
|
|
169 |
pComms = new STATComms();
|
|
170 |
if (pComms)
|
|
171 |
{
|
|
172 |
pComms->SetTransport((STATCONNECTTYPE)(pDlg->m_Transport.GetCurSel() + 1));
|
|
173 |
if (pComms->Initialise() == ITS_OK)
|
|
174 |
{
|
|
175 |
char szPort[25];
|
|
176 |
pDlg->m_Port.GetWindowText(szPort, 25);
|
|
177 |
|
|
178 |
while (bReset)
|
|
179 |
{
|
|
180 |
pDlg->AddString("Listening...");
|
|
181 |
if (pComms->Connect(szPort) == ITS_OK)
|
|
182 |
{
|
|
183 |
bFinished = false;
|
|
184 |
bReset = false;
|
|
185 |
pDlg->AddString("Connected.");
|
|
186 |
pDlg->AddString(szPort);
|
|
187 |
|
|
188 |
ProcessInput(pComms, pDlg);
|
|
189 |
|
|
190 |
pComms->Disconnect();
|
|
191 |
}
|
|
192 |
else
|
|
193 |
{
|
|
194 |
pDlg->AddString("Could not connect.");
|
|
195 |
pDlg->AddString(szPort);
|
|
196 |
}
|
|
197 |
|
|
198 |
pDlg->AddString("Stopped.");
|
|
199 |
}
|
|
200 |
|
|
201 |
pComms->Release();
|
|
202 |
}
|
|
203 |
else
|
|
204 |
{
|
|
205 |
pDlg->AddString("Could not initialise.");
|
|
206 |
}
|
|
207 |
}
|
|
208 |
else
|
|
209 |
pDlg->AddString("Out of memory.");
|
|
210 |
|
|
211 |
return 1;
|
|
212 |
}
|
|
213 |
|
|
214 |
|
|
215 |
///////////////////////////////////////////////////////////////////////////////////
|
|
216 |
// Listen on the port
|
|
217 |
///////////////////////////////////////////////////////////////////////////////////
|
|
218 |
void
|
|
219 |
ProcessInput(STATComms *pComms, CTestcomDlg *pDlg)
|
|
220 |
{
|
|
221 |
int ret = GENERAL_FAILURE;
|
|
222 |
char szBuf[512] = {0};
|
|
223 |
char cIdentifier;
|
|
224 |
char *pExtra = NULL;
|
|
225 |
unsigned long ulReadLength = 0;
|
|
226 |
|
|
227 |
while (!bFinished)
|
|
228 |
{
|
|
229 |
if ((ret = pComms->Receive(&cIdentifier, &pExtra, &ulReadLength)) == ITS_OK)
|
|
230 |
{
|
|
231 |
// display what we received
|
|
232 |
if (ulReadLength && ulReadLength < MAX_LOG_MSG_LEN)
|
|
233 |
{
|
|
234 |
char szContents[MAX_LOG_MSG_LEN + 1];
|
|
235 |
strncpy(szContents, pExtra, ulReadLength);
|
|
236 |
*(szContents + ulReadLength) = (char)0;
|
|
237 |
sprintf(szBuf, "%c [%s] (%ld)", cIdentifier, szContents, ulReadLength);
|
|
238 |
}
|
|
239 |
else
|
|
240 |
sprintf(szBuf, "%c (%ld)", cIdentifier, ulReadLength);
|
|
241 |
pDlg->AddString(szBuf);
|
|
242 |
|
|
243 |
// special behaviour
|
|
244 |
switch(cIdentifier)
|
|
245 |
{
|
|
246 |
case 'T':
|
|
247 |
{
|
|
248 |
pDlg->ReleaseData(&pExtra);
|
|
249 |
ulReadLength = 0;
|
|
250 |
break;
|
|
251 |
}
|
|
252 |
case 'S':
|
|
253 |
{
|
|
254 |
// read bitmap data
|
|
255 |
pDlg->ReleaseData(&pExtra);
|
|
256 |
ret = pDlg->ReadTransferFile(szBitmapFile, &pExtra, &ulReadLength);
|
|
257 |
if (ret != ITS_OK)
|
|
258 |
{
|
|
259 |
pDlg->AddString("Read of bitmap failed!!!");
|
|
260 |
pDlg->AddString(szBitmapFile);
|
|
261 |
}
|
|
262 |
break;
|
|
263 |
}
|
|
264 |
case 'R':
|
|
265 |
case 'X':
|
|
266 |
{
|
|
267 |
// read binary
|
|
268 |
pDlg->ReleaseData(&pExtra);
|
|
269 |
ret = pDlg->ReadTransferFile(szReceiveFile, &pExtra, &ulReadLength);
|
|
270 |
if (ret != ITS_OK)
|
|
271 |
{
|
|
272 |
pDlg->AddString("Read of application file failed!!!");
|
|
273 |
pDlg->AddString(szReceiveFile);
|
|
274 |
}
|
|
275 |
break;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
// now send data back as a response to show we got it ok
|
|
280 |
if (pComms->Send(cIdentifier, pExtra, ulReadLength) != ITS_OK)
|
|
281 |
pDlg->AddString("Send failed!!!");
|
|
282 |
|
|
283 |
// need to delete received data once finished with it
|
|
284 |
pDlg->ReleaseData(&pExtra);
|
|
285 |
}
|
|
286 |
else
|
|
287 |
{
|
|
288 |
// restart the connection
|
|
289 |
if (ret != NO_DATA_AT_PORT)
|
|
290 |
{
|
|
291 |
bReset = true;
|
|
292 |
break;
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
Sleep (250);
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
|
|
301 |
///////////////////////////////////////////////////////////////////////////////////
|
|
302 |
// Release memory resources
|
|
303 |
///////////////////////////////////////////////////////////////////////////////////
|
|
304 |
void
|
|
305 |
CTestcomDlg::ReleaseData(char **ppData)
|
|
306 |
{
|
|
307 |
if (*ppData)
|
|
308 |
{
|
|
309 |
delete [] (*ppData);
|
|
310 |
(*ppData) = NULL;
|
|
311 |
}
|
|
312 |
}
|
|
313 |
|
|
314 |
|
|
315 |
///////////////////////////////////////////////////////////////////////////////////
|
|
316 |
// Add a string to the list box
|
|
317 |
///////////////////////////////////////////////////////////////////////////////////
|
|
318 |
void
|
|
319 |
CTestcomDlg::AddString(char *szText)
|
|
320 |
{
|
|
321 |
m_List1.AddString(szText);
|
|
322 |
int nCount = m_List1.GetCount();
|
|
323 |
if (nCount > 0)
|
|
324 |
m_List1.SetTopIndex(nCount - 1);
|
|
325 |
UpdateWindow();
|
|
326 |
}
|
|
327 |
|
|
328 |
|
|
329 |
///////////////////////////////////////////////////////////////////////////////////
|
|
330 |
// Read in a file
|
|
331 |
///////////////////////////////////////////////////////////////////////////////////
|
|
332 |
int CTestcomDlg::ReadTransferFile(const char *pFile, char **ppContents, unsigned long *pLength)
|
|
333 |
{
|
|
334 |
int ret = E_BADFILENAME;
|
|
335 |
CString path = pFile;
|
|
336 |
CFile script_file;
|
|
337 |
if (script_file.Open(path, CFile::modeRead))
|
|
338 |
{
|
|
339 |
try
|
|
340 |
{
|
|
341 |
(*pLength) = script_file.GetLength();
|
|
342 |
(*ppContents) = new char [*pLength];
|
|
343 |
if (*ppContents)
|
|
344 |
{
|
|
345 |
script_file.Read(*ppContents, *pLength);
|
|
346 |
ret = ITS_OK;
|
|
347 |
}
|
|
348 |
else
|
|
349 |
ret = E_OUTOFMEM;
|
|
350 |
}
|
|
351 |
catch(CFileException *e)
|
|
352 |
{
|
|
353 |
e->Delete();
|
|
354 |
ret = GENERAL_FAILURE;
|
|
355 |
}
|
|
356 |
|
|
357 |
script_file.Abort();
|
|
358 |
}
|
|
359 |
|
|
360 |
return ret;
|
|
361 |
}
|