source: trunk/tools/qtestlib/wince/remotelib/commands.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41#include "commands.h"
42#include <Pm.h>
43#include <Pmpolicy.h>
44
45
46#define CLEAN_FAIL(a) {delete appName; \
47 delete arguments; \
48 return (a); }
49
50int qRemoteLaunch(DWORD, BYTE*, DWORD*, BYTE**, IRAPIStream* stream)
51{
52 if (!stream)
53 return -1;
54
55 DWORD bytesRead;
56 int appLength;
57 wchar_t* appName = 0;
58 int argumentsLength;
59 wchar_t* arguments = 0;
60 int timeout = -1;
61 int returnValue = -2;
62 DWORD error = 0;
63
64 if (S_OK != stream->Read(&appLength, sizeof(appLength), &bytesRead))
65 CLEAN_FAIL(-2);
66 appName = (wchar_t*) malloc(sizeof(wchar_t)*(appLength + 1));
67 if (S_OK != stream->Read(appName, sizeof(wchar_t)*appLength, &bytesRead))
68 CLEAN_FAIL(-2);
69 appName[appLength] = '\0';
70
71 if (S_OK != stream->Read(&argumentsLength, sizeof(argumentsLength), &bytesRead))
72 CLEAN_FAIL(-2);
73 arguments = (wchar_t*) malloc(sizeof(wchar_t)*(argumentsLength + 1));
74 if (S_OK != stream->Read(arguments, sizeof(wchar_t)*argumentsLength, &bytesRead))
75 CLEAN_FAIL(-2);
76 arguments[argumentsLength] = '\0';
77
78 if (S_OK != stream->Read(&timeout, sizeof(timeout), &bytesRead))
79 CLEAN_FAIL(-2);
80
81 bool result = qRemoteExecute(appName, arguments, &returnValue, &error, timeout);
82
83 if (timeout != 0) {
84 if (S_OK != stream->Write(&returnValue, sizeof(returnValue), &bytesRead))
85 CLEAN_FAIL(-4);
86 if (S_OK != stream->Write(&error, sizeof(error), &bytesRead))
87 CLEAN_FAIL(-5);
88 }
89 delete appName;
90 delete arguments;
91 // We need to fail here for the execute, otherwise the calling application will wait
92 // forever for the returnValue.
93 if (!result)
94 return -3;
95 return S_OK;
96}
97
98
99bool qRemoteExecute(const wchar_t* program, const wchar_t* arguments, int *returnValue, DWORD* error, int timeout)
100{
101 *error = 0;
102
103 if (!program)
104 return false;
105
106 PROCESS_INFORMATION pid;
107 if (!CreateProcess(program, arguments, NULL, NULL, false, 0, NULL, NULL, NULL, &pid)) {
108 *error = GetLastError();
109 wprintf(L"Could not launch: %s\n", program);
110 return false;
111 }
112
113 // Timeout is in seconds
114 DWORD waitingTime = (timeout == -1) ? INFINITE : timeout * 1000;
115
116 if (waitingTime != 0) {
117 DWORD waitStatus = WaitForSingleObject(pid.hProcess, waitingTime);
118 if (waitStatus == WAIT_TIMEOUT) {
119 TerminateProcess(pid.hProcess, 2);
120 return false;
121 } else if (waitStatus == WAIT_OBJECT_0 && returnValue) {
122 *returnValue = 0;
123 DWORD exitCode;
124 if (GetExitCodeProcess(pid.hProcess, &exitCode))
125 *returnValue = exitCode;
126 }
127 }
128 return true;
129}
130/**
131\brief Reset the device.
132*/
133int qRemoteSoftReset(DWORD, BYTE*, DWORD*, BYTE**, IRAPIStream* stream)
134{
135 //POWER_STATE_ON On state
136 //POWER_STATE_OFF Off state
137 //POWER_STATE_CRITICAL Critical state
138 //POWER_STATE_BOOT Boot state
139 //POWER_STATE_IDLE Idle state
140 //POWER_STATE_SUSPEND Suspend state
141 //POWER_STATE_RESET Reset state
142
143 DWORD returnValue = SetSystemPowerState(0, POWER_STATE_RESET, POWER_FORCE);
144 return returnValue;
145}
146
147/**
148\brief Toggle the unattended powermode of the device
149*/
150int qRemoteToggleUnattendedPowerMode(DWORD, BYTE*, DWORD*, BYTE**, IRAPIStream* stream)
151{
152 if (!stream)
153 return -1;
154
155 DWORD bytesRead;
156 int toggleVal = 0;
157 int returnValue = S_OK;
158
159 if (S_OK != stream->Read(&toggleVal, sizeof(toggleVal), &bytesRead))
160 return -2;
161
162 //PPN_REEVALUATESTATE 0x0001 Reserved. Set dwData to zero (0).
163 //PPN_POWERCHANGE 0x0002 Reserved. Set dwData to zero (0).
164 //PPN_UNATTENDEDMODE 0x0003 Set dwData to TRUE or FALSE.
165 //PPN_SUSPENDKEYPRESSED or
166 //PPN_POWERBUTTONPRESSED 0x0004 Reserved. Set dwData to zero (0).
167 //PPN_SUSPENDKEYRELEASED 0x0005 Reserved. Set dwData to zero (0).
168 //PPN_APPBUTTONPRESSED 0x0006 Reserved. Set dwData to zero (0).
169 //PPN_OEMBASE Greater than or equal to 0x10000
170 //You can define higher values, such as 0x10001, 0x10002, and so on.
171 // Reserved. Set dwData to zero (0).
172 returnValue = PowerPolicyNotify(PPN_UNATTENDEDMODE, toggleVal);
173
174 if (S_OK != stream->Write(&returnValue, sizeof(returnValue), &bytesRead))
175 return -3;
176 else
177 return S_OK;
178}
179
180/**
181\brief Virtually press the power button of the device
182*/
183int qRemotePowerButton(DWORD, BYTE*, DWORD*, BYTE**, IRAPIStream* stream)
184{
185 if (!stream)
186 return -1;
187
188 DWORD bytesRead;
189 int toggleVal = 0;
190 int returnValue = S_OK;
191
192 if (S_OK != stream->Read(&toggleVal, sizeof(toggleVal), &bytesRead))
193 return -2;
194
195 //PPN_REEVALUATESTATE 0x0001 Reserved. Set dwData to zero (0).
196 //PPN_POWERCHANGE 0x0002 Reserved. Set dwData to zero (0).
197 //PPN_UNATTENDEDMODE 0x0003 Set dwData to TRUE or FALSE.
198 //PPN_SUSPENDKEYPRESSED or
199 //PPN_POWERBUTTONPRESSED 0x0004 Reserved. Set dwData to zero (0).
200 //PPN_SUSPENDKEYRELEASED 0x0005 Reserved. Set dwData to zero (0).
201 //PPN_APPBUTTONPRESSED 0x0006 Reserved. Set dwData to zero (0).
202 //PPN_OEMBASE Greater than or equal to 0x10000
203 //You can define higher values, such as 0x10001, 0x10002, and so on.
204 // Reserved. Set dwData to zero (0).
205 returnValue = PowerPolicyNotify(PPN_POWERBUTTONPRESSED, 0);
206
207 if (S_OK != stream->Write(&returnValue, sizeof(returnValue), &bytesRead))
208 return -3;
209 else
210 return S_OK;
211}
212
Note: See TracBrowser for help on using the repository browser.