|
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 // myclientDlg.cpp : implementation file |
|
22 // |
|
23 |
|
24 #include "stdafx.h" |
|
25 #include "myclient.h" |
|
26 #include "myclientDlg.h" |
|
27 #include <statsocket.h> |
|
28 |
|
29 #ifdef _DEBUG |
|
30 #define new DEBUG_NEW |
|
31 #undef THIS_FILE |
|
32 static char THIS_FILE[] = __FILE__; |
|
33 #endif |
|
34 |
|
35 ///////////////////////////////////////////////////////////////////////////// |
|
36 // CAboutDlg dialog used for App About |
|
37 |
|
38 class CAboutDlg : public CDialog |
|
39 { |
|
40 public: |
|
41 CAboutDlg(); |
|
42 |
|
43 // Dialog Data |
|
44 //{{AFX_DATA(CAboutDlg) |
|
45 enum { IDD = IDD_ABOUTBOX }; |
|
46 //}}AFX_DATA |
|
47 |
|
48 // ClassWizard generated virtual function overrides |
|
49 //{{AFX_VIRTUAL(CAboutDlg) |
|
50 protected: |
|
51 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support |
|
52 //}}AFX_VIRTUAL |
|
53 |
|
54 // Implementation |
|
55 protected: |
|
56 //{{AFX_MSG(CAboutDlg) |
|
57 //}}AFX_MSG |
|
58 DECLARE_MESSAGE_MAP() |
|
59 }; |
|
60 |
|
61 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) |
|
62 { |
|
63 //{{AFX_DATA_INIT(CAboutDlg) |
|
64 //}}AFX_DATA_INIT |
|
65 } |
|
66 |
|
67 void CAboutDlg::DoDataExchange(CDataExchange* pDX) |
|
68 { |
|
69 CDialog::DoDataExchange(pDX); |
|
70 //{{AFX_DATA_MAP(CAboutDlg) |
|
71 //}}AFX_DATA_MAP |
|
72 } |
|
73 |
|
74 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) |
|
75 //{{AFX_MSG_MAP(CAboutDlg) |
|
76 // No message handlers |
|
77 //}}AFX_MSG_MAP |
|
78 END_MESSAGE_MAP() |
|
79 |
|
80 ///////////////////////////////////////////////////////////////////////////// |
|
81 // CMyclientDlg dialog |
|
82 |
|
83 CMyclientDlg::CMyclientDlg(CWnd* pParent /*=NULL*/) |
|
84 : CDialog(CMyclientDlg::IDD, pParent) |
|
85 { |
|
86 //{{AFX_DATA_INIT(CMyclientDlg) |
|
87 // NOTE: the ClassWizard will add member initialization here |
|
88 //}}AFX_DATA_INIT |
|
89 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); |
|
90 } |
|
91 |
|
92 void CMyclientDlg::DoDataExchange(CDataExchange* pDX) |
|
93 { |
|
94 CDialog::DoDataExchange(pDX); |
|
95 //{{AFX_DATA_MAP(CMyclientDlg) |
|
96 DDX_Control(pDX, IDC_INFO, m_Info); |
|
97 DDX_Control(pDX, IDC_RECEIVE, m_Receive); |
|
98 DDX_Control(pDX, IDC_CHKLISTEN, m_ChkListen); |
|
99 DDX_Control(pDX, IDC_PORT, m_Port); |
|
100 DDX_Control(pDX, IDC_MACHINE, m_Machine); |
|
101 DDX_Control(pDX, IDC_CONNECT, m_Connect); |
|
102 DDX_Control(pDX, IDC_SEND, m_Send); |
|
103 DDX_Control(pDX, IDC_SENDMSG, m_SendMsg); |
|
104 DDX_Control(pDX, IDC_RECVLIST, m_RecvList); |
|
105 //}}AFX_DATA_MAP |
|
106 } |
|
107 |
|
108 BEGIN_MESSAGE_MAP(CMyclientDlg, CDialog) |
|
109 //{{AFX_MSG_MAP(CMyclientDlg) |
|
110 ON_WM_SYSCOMMAND() |
|
111 ON_WM_PAINT() |
|
112 ON_WM_QUERYDRAGICON() |
|
113 ON_BN_CLICKED(IDC_SEND, OnSend) |
|
114 ON_BN_CLICKED(IDC_CONNECT, OnConnect) |
|
115 ON_BN_CLICKED(IDC_RECEIVE, OnReceive) |
|
116 ON_BN_CLICKED(IDC_CHKLISTEN, OnChklisten) |
|
117 ON_BN_CLICKED(IDC_INFO, OnInfo) |
|
118 //}}AFX_MSG_MAP |
|
119 END_MESSAGE_MAP() |
|
120 |
|
121 ///////////////////////////////////////////////////////////////////////////// |
|
122 // CMyclientDlg message handlers |
|
123 |
|
124 BOOL CMyclientDlg::OnInitDialog() |
|
125 { |
|
126 CDialog::OnInitDialog(); |
|
127 |
|
128 // IDM_ABOUTBOX must be in the system command range. |
|
129 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); |
|
130 ASSERT(IDM_ABOUTBOX < 0xF000); |
|
131 |
|
132 CMenu* pSysMenu = GetSystemMenu(FALSE); |
|
133 if (pSysMenu != NULL) |
|
134 { |
|
135 CString strAboutMenu; |
|
136 strAboutMenu.LoadString(IDS_ABOUTBOX); |
|
137 if (!strAboutMenu.IsEmpty()) |
|
138 { |
|
139 pSysMenu->AppendMenu(MF_SEPARATOR); |
|
140 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); |
|
141 } |
|
142 } |
|
143 |
|
144 SetIcon(m_hIcon, TRUE); // Set big icon |
|
145 SetIcon(m_hIcon, FALSE); // Set small icon |
|
146 |
|
147 hThreadHandle = 0; |
|
148 pSocket = NULL; |
|
149 |
|
150 m_Machine.SetWindowText("LON-PHILH03"); |
|
151 m_Port.SetWindowText("3001"); |
|
152 m_SendMsg.SetWindowText("<B><S><E>"); |
|
153 |
|
154 Prepare(); |
|
155 ResetDeviceInfo(); |
|
156 |
|
157 return TRUE; // return TRUE unless you set the focus to a control |
|
158 } |
|
159 |
|
160 void CMyclientDlg::OnSysCommand(UINT nID, LPARAM lParam) |
|
161 { |
|
162 if ((nID & 0xFFF0) == IDM_ABOUTBOX) |
|
163 { |
|
164 CAboutDlg dlgAbout; |
|
165 dlgAbout.DoModal(); |
|
166 } |
|
167 else |
|
168 { |
|
169 CDialog::OnSysCommand(nID, lParam); |
|
170 } |
|
171 } |
|
172 |
|
173 // If you add a minimize button to your dialog, you will need the code below |
|
174 // to draw the icon. For MFC applications using the document/view model, |
|
175 // this is automatically done for you by the framework. |
|
176 |
|
177 void CMyclientDlg::OnPaint() |
|
178 { |
|
179 if (IsIconic()) |
|
180 { |
|
181 CPaintDC dc(this); // device context for painting |
|
182 |
|
183 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); |
|
184 |
|
185 // Center icon in client rectangle |
|
186 int cxIcon = GetSystemMetrics(SM_CXICON); |
|
187 int cyIcon = GetSystemMetrics(SM_CYICON); |
|
188 CRect rect; |
|
189 GetClientRect(&rect); |
|
190 int x = (rect.Width() - cxIcon + 1) / 2; |
|
191 int y = (rect.Height() - cyIcon + 1) / 2; |
|
192 |
|
193 // Draw the icon |
|
194 dc.DrawIcon(x, y, m_hIcon); |
|
195 } |
|
196 else |
|
197 { |
|
198 CDialog::OnPaint(); |
|
199 } |
|
200 } |
|
201 |
|
202 HCURSOR CMyclientDlg::OnQueryDragIcon() |
|
203 { |
|
204 return (HCURSOR) m_hIcon; |
|
205 } |
|
206 |
|
207 void CMyclientDlg::OnSend() |
|
208 { |
|
209 if (pSocket) |
|
210 { |
|
211 char szData[256]; |
|
212 m_SendMsg.GetWindowText(szData, 256); |
|
213 if (*szData) |
|
214 { |
|
215 CWaitCursor oWait; |
|
216 |
|
217 if (pSocket->Send(SymbianScript, szData, strlen(szData)) != ITS_OK) |
|
218 { |
|
219 AddString("Could not send. Try to re-connect."); |
|
220 Prepare(); |
|
221 } |
|
222 } |
|
223 } |
|
224 else |
|
225 AddString("Bad socket"); |
|
226 } |
|
227 |
|
228 void CMyclientDlg::OnReceive() |
|
229 { |
|
230 if (pSocket) |
|
231 { |
|
232 CWaitCursor oWait; |
|
233 |
|
234 char ID = 0; |
|
235 unsigned long Length = 0; |
|
236 char *pData = NULL; |
|
237 |
|
238 if (pSocket->Receive(&ID, &pData, &Length) != ITS_OK) |
|
239 { |
|
240 AddString("Could not receive. Try to re-connect."); |
|
241 Prepare(); |
|
242 } |
|
243 else |
|
244 { |
|
245 char szBuffer[120] = {0}; |
|
246 |
|
247 switch(ID) |
|
248 { |
|
249 case SymbianScript: |
|
250 if ((Length < 80) && pData) |
|
251 sprintf(szBuffer, "%ld [%s]", Length, pData); |
|
252 else |
|
253 { |
|
254 sprintf(szBuffer, "Received %ld bytes of data", Length); |
|
255 OpenDataInNotepad(pData, Length); |
|
256 } |
|
257 break; |
|
258 case SymbianError: |
|
259 if ((Length < 100) && pData) |
|
260 sprintf(szBuffer, "ERROR: %ld [%s]", Length, pData); |
|
261 else |
|
262 { |
|
263 sprintf(szBuffer, "ERROR: Received %ld bytes of data", Length); |
|
264 OpenDataInNotepad(pData, Length); |
|
265 } |
|
266 break; |
|
267 case SymbianDeviceInfo: |
|
268 StoreDeviceInformation(pData, Length); |
|
269 DisplayDeviceInformation(pData); |
|
270 break; |
|
271 }; |
|
272 |
|
273 AddString(szBuffer); |
|
274 } |
|
275 |
|
276 if (pData) |
|
277 { |
|
278 delete [] pData; |
|
279 pData = NULL; |
|
280 } |
|
281 } |
|
282 else |
|
283 AddString("Bad socket"); |
|
284 } |
|
285 |
|
286 void CMyclientDlg::OnConnect() |
|
287 { |
|
288 bool IsConnected = false; |
|
289 |
|
290 pSocket = new CSTATSocket; |
|
291 if (pSocket) |
|
292 { |
|
293 CWaitCursor oWait; |
|
294 |
|
295 if (pSocket->Initialise() != ITS_OK) |
|
296 return; |
|
297 |
|
298 char szMachine[256] = {0}; |
|
299 if (!m_ChkListen.GetCheck()) |
|
300 { |
|
301 // get machine name and port number |
|
302 m_Machine.GetWindowText(szMachine, 247); |
|
303 strcat(szMachine, ":"); |
|
304 m_Port.GetWindowText(szMachine + strlen(szMachine), 8); |
|
305 } |
|
306 else |
|
307 { |
|
308 m_Port.GetWindowText(szMachine, 8); |
|
309 } |
|
310 |
|
311 if (pSocket->Connect(szMachine) == ITS_OK) |
|
312 { |
|
313 AddString("Connected."); |
|
314 m_SendMsg.EnableWindow(TRUE); |
|
315 m_Send.EnableWindow(TRUE); |
|
316 m_Receive.EnableWindow(TRUE); |
|
317 m_Connect.EnableWindow(FALSE); |
|
318 m_ChkListen.EnableWindow(FALSE); |
|
319 m_Machine.EnableWindow(FALSE); |
|
320 m_Port.EnableWindow(FALSE); |
|
321 m_Info.EnableWindow(TRUE); |
|
322 } |
|
323 else |
|
324 AddString("Could not connect."); |
|
325 } |
|
326 else |
|
327 AddString("Memory error."); |
|
328 } |
|
329 |
|
330 |
|
331 void CMyclientDlg::OnChklisten() |
|
332 { |
|
333 if (m_ChkListen.GetCheck()) |
|
334 { |
|
335 m_Machine.EnableWindow(FALSE); |
|
336 } |
|
337 else |
|
338 { |
|
339 m_Machine.EnableWindow(TRUE); |
|
340 } |
|
341 } |
|
342 |
|
343 void CMyclientDlg::OnOK() |
|
344 { |
|
345 Prepare(); |
|
346 |
|
347 // delete once we've finished with it |
|
348 DeleteFile(STAT_TEMP_FILE); |
|
349 |
|
350 CDialog::OnOK(); |
|
351 } |
|
352 |
|
353 |
|
354 void CMyclientDlg::OnInfo() |
|
355 { |
|
356 if (pSocket) |
|
357 { |
|
358 if (pSocket->Send(SymbianDeviceInfo) != ITS_OK) |
|
359 { |
|
360 AddString("Could not send. Try to re-connect."); |
|
361 Prepare(); |
|
362 } |
|
363 else |
|
364 { |
|
365 OnReceive(); |
|
366 } |
|
367 } |
|
368 } |
|
369 |
|
370 |
|
371 /////////////////////////////////////////////////////////////////////////////////// |
|
372 // PRIVATE FUNCTIONS |
|
373 /////////////////////////////////////////////////////////////////////////////////// |
|
374 |
|
375 /////////////////////////////////////////////////////////////////////////////////// |
|
376 // Add a string to the list box |
|
377 /////////////////////////////////////////////////////////////////////////////////// |
|
378 void CMyclientDlg::AddString(char *szText) |
|
379 { |
|
380 m_RecvList.AddString(szText); |
|
381 int nCount = m_RecvList.GetCount(); |
|
382 if (nCount > 0) |
|
383 m_RecvList.SetTopIndex(nCount - 1); |
|
384 UpdateWindow(); |
|
385 } |
|
386 |
|
387 |
|
388 ////////////////////////////////////////////////////////////////////////////////////// |
|
389 // Open a text file in Notepad |
|
390 void CMyclientDlg::OpenDataInNotepad(char *pContents, unsigned long ulLength) |
|
391 { |
|
392 // save data |
|
393 int ret = E_BADFILENAME; |
|
394 |
|
395 CFile script_file; |
|
396 if (script_file.Open(STAT_TEMP_FILE, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite)) |
|
397 { |
|
398 try |
|
399 { |
|
400 // might be a zero-length file |
|
401 if (pContents && ulLength) |
|
402 script_file.Write(pContents, ulLength); |
|
403 |
|
404 ret = ITS_OK; |
|
405 } |
|
406 catch(CFileException *e) |
|
407 { |
|
408 e->Delete(); |
|
409 ret = GENERAL_FAILURE; |
|
410 } |
|
411 |
|
412 script_file.Flush(); |
|
413 script_file.Close(); |
|
414 } |
|
415 |
|
416 |
|
417 if (ret == ITS_OK) |
|
418 { |
|
419 // open file |
|
420 TCHAR szCurrentDir[MAX_PATH + 1]; |
|
421 if (GetWindowsDirectory(szCurrentDir, sizeof(szCurrentDir))) |
|
422 { |
|
423 CString cBuf; |
|
424 cBuf = _T("\""); |
|
425 cBuf += szCurrentDir; |
|
426 cBuf += _T("\\Notepad.exe\" "); |
|
427 cBuf += STAT_TEMP_FILE; |
|
428 |
|
429 if (!CallProcess(NULL, cBuf.GetBuffer(0), NULL)) |
|
430 MessageBox(_T("Could not invoke Notepad.exe to view script file"), NULL, MB_OK); |
|
431 } |
|
432 else |
|
433 MessageBox(_T("Could not locate Windows directory"), NULL, MB_OK); |
|
434 } |
|
435 } |
|
436 |
|
437 |
|
438 ////////////////////////////////////////////////////////////////////////////////////// |
|
439 // Invoke a process |
|
440 bool CMyclientDlg::CallProcess(LPCTSTR szApplication, LPTSTR szCommandLine, LPCTSTR szDirectory) |
|
441 { |
|
442 bool valid = false; |
|
443 STARTUPINFO startInfo = {0}; |
|
444 startInfo.cb = sizeof(STARTUPINFO); |
|
445 |
|
446 PROCESS_INFORMATION procInfo = {0}; |
|
447 |
|
448 // event attributes for the child process |
|
449 SECURITY_ATTRIBUTES eventAttr; |
|
450 eventAttr.nLength = sizeof(eventAttr); |
|
451 eventAttr.lpSecurityDescriptor = NULL; |
|
452 eventAttr.bInheritHandle = TRUE; |
|
453 |
|
454 if (CreateProcess(szApplication, szCommandLine, NULL, NULL, FALSE, NULL, NULL, |
|
455 szDirectory, &startInfo, &procInfo)) |
|
456 { |
|
457 valid = true; |
|
458 CloseHandle(procInfo.hThread); |
|
459 CloseHandle(procInfo.hProcess); |
|
460 } |
|
461 |
|
462 return valid; |
|
463 } |
|
464 |
|
465 |
|
466 ////////////////////////////////////////////////////////////////////////////////////// |
|
467 // Reset dialog buttons |
|
468 void CMyclientDlg::Prepare() |
|
469 { |
|
470 m_Info.EnableWindow(FALSE); |
|
471 m_SendMsg.EnableWindow(FALSE); |
|
472 m_Send.EnableWindow(FALSE); |
|
473 m_Receive.EnableWindow(FALSE); |
|
474 m_Connect.EnableWindow(TRUE); |
|
475 m_ChkListen.EnableWindow(TRUE); |
|
476 |
|
477 if (pSocket) |
|
478 { |
|
479 pSocket->Disconnect(); |
|
480 pSocket->Release(); |
|
481 delete pSocket; |
|
482 pSocket = NULL; |
|
483 } |
|
484 } |
|
485 |
|
486 |
|
487 //---------------------------------------------------------------------------- |
|
488 // Break up newline delimited list of device info and store |
|
489 void CMyclientDlg::StoreDeviceInformation(char *info, unsigned long length) |
|
490 { |
|
491 ResetDeviceInfo(); |
|
492 if (length && info) |
|
493 { |
|
494 char szInfo[2048] = {0}; |
|
495 char *pEnd = szInfo; |
|
496 strncpy(szInfo, info, length); |
|
497 |
|
498 char *p[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
|
499 int i = 0; |
|
500 |
|
501 // skip over leading CR/LF's |
|
502 while ((*pEnd) && (*pEnd) == '\r' || (*pEnd) == '\n') |
|
503 { |
|
504 (*pEnd) = (char)0; |
|
505 pEnd++; |
|
506 } |
|
507 |
|
508 // split |
|
509 while(*pEnd) |
|
510 { |
|
511 p[i] = pEnd; |
|
512 |
|
513 // step over setting |
|
514 while ((*pEnd) && (*pEnd) != '\r' && (*pEnd) != '\n') |
|
515 pEnd++; |
|
516 |
|
517 // null terminate |
|
518 while ((*pEnd) && (*pEnd) == '\r' || (*pEnd) == '\n') |
|
519 { |
|
520 (*pEnd) = (char)0; |
|
521 pEnd++; |
|
522 } |
|
523 |
|
524 i++; |
|
525 if (i > 18) |
|
526 break; |
|
527 } |
|
528 *pEnd = (char)0; |
|
529 |
|
530 // assign |
|
531 strcpy(szMachine, p[0]); |
|
532 strcpy(szCPU, p[1]); |
|
533 strcpy(szInterface, p[2]); |
|
534 strcpy(szFamily, p[3]); |
|
535 strcpy(szManufacturer, p[4]); |
|
536 lModel = atol(p[5]); |
|
537 lFamilyRevision = atol(p[6]); |
|
538 lHardwareRevision = atol(p[7]); |
|
539 lSoftwareRevision = atol(p[8]); |
|
540 lSoftwareBuild = atol(p[9]); |
|
541 lMemoryPageSize = atol(p[10]); |
|
542 lMemoryRAM = atol(p[11]); |
|
543 lMemoryRAMFree = atol(p[12]); |
|
544 lMemoryROM = atol(p[13]); |
|
545 iDelay = atoi(p[14]); |
|
546 iImageVerification = atoi(p[15]); |
|
547 if (iImageVerification) |
|
548 { |
|
549 strcpy(szRefDir, p[16]); |
|
550 iFudge = atoi(p[17]); |
|
551 } |
|
552 } |
|
553 } |
|
554 |
|
555 |
|
556 //---------------------------------------------------------------------------- |
|
557 // Clear any device info settings |
|
558 void CMyclientDlg::ResetDeviceInfo() |
|
559 { |
|
560 // device info |
|
561 memset(&szMachine, 0, sizeof(szMachine)); |
|
562 memset(&szCPU, 0, sizeof(szCPU)); |
|
563 memset(&szInterface, 0, sizeof(szInterface)); |
|
564 memset(&szFamily, 0, sizeof(szFamily)); |
|
565 memset(&szManufacturer, 0, sizeof(szManufacturer)); |
|
566 lModel = 0; |
|
567 lFamilyRevision = 0; |
|
568 lHardwareRevision = 0; |
|
569 lSoftwareRevision = 0; |
|
570 lSoftwareBuild = 0; |
|
571 lMemoryPageSize = 0; |
|
572 lMemoryRAM = 0; |
|
573 lMemoryRAMFree = 0; |
|
574 lMemoryROM = 0; |
|
575 iDelay = 0; |
|
576 iImageVerification = 0; |
|
577 memset(&szRefDir, 0, sizeof(szRefDir)); |
|
578 iFudge = 0; |
|
579 } |
|
580 |
|
581 |
|
582 //---------------------------------------------------------------------------- |
|
583 // Clear any device info settings |
|
584 void CMyclientDlg::DisplayDeviceInformation(char *data) |
|
585 { |
|
586 MessageBox(data, "Info", MB_OK); |
|
587 } |
|
588 |