| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
QTSampleCM.cpp |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
QT implementation of the Sample CM |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
cQTSampleCM |
| 10 |
QT implementation of the Sample CM |
| 11 |
|
| 12 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 13 |
|
| 14 |
Redistribution and use in source and binary forms, with or without |
| 15 |
modification, are permitted provided that the following conditions are met: |
| 16 |
* Redistributions of source code must retain the above copyright |
| 17 |
notice, this list of conditions and the following disclaimer. |
| 18 |
* Redistributions in binary form must reproduce the above copyright |
| 19 |
notice, this list of conditions and the following disclaimer in the |
| 20 |
documentation and/or other materials provided with the distribution. |
| 21 |
* Neither the name of Code Aurora Forum nor |
| 22 |
the names of its contributors may be used to endorse or promote |
| 23 |
products derived from this software without specific prior written |
| 24 |
permission. |
| 25 |
|
| 26 |
|
| 27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 31 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 32 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 33 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 34 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 35 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 36 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 37 |
POSSIBILITY OF SUCH DAMAGE. |
| 38 |
===========================================================================*/ |
| 39 |
|
| 40 |
//--------------------------------------------------------------------------- |
| 41 |
// Include Files |
| 42 |
//--------------------------------------------------------------------------- |
| 43 |
#include "QTSampleCM.h" |
| 44 |
|
| 45 |
/*=========================================================================*/ |
| 46 |
// Free Methods |
| 47 |
/*=========================================================================*/ |
| 48 |
|
| 49 |
/*=========================================================================== |
| 50 |
METHOD: |
| 51 |
AsyncConnectThread (Free Method) |
| 52 |
|
| 53 |
DESCRIPTION: |
| 54 |
Start a data session |
| 55 |
|
| 56 |
PARAMETERS: |
| 57 |
pData [ I ] - cQTSampleCM object |
| 58 |
|
| 59 |
RETURN VALUE: |
| 60 |
void * - always NULL |
| 61 |
===========================================================================*/ |
| 62 |
void * AsyncConnectThread( void * pData ) |
| 63 |
{ |
| 64 |
cQTSampleCM * pCM = (cQTSampleCM*)pData; |
| 65 |
if (pCM == NULL) |
| 66 |
{ |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
// Open the dialog window, disable the info and connection stats buttons |
| 71 |
pCM->mView.rootContext()->setContextProperty( "dialogText", |
| 72 |
"Connecting, please wait..." ); |
| 73 |
pCM->mView.rootContext()->setContextProperty( "windowState", |
| 74 |
"connectingDialog" ); |
| 75 |
|
| 76 |
ULONG failureCode = 0xFFFFFFFF; |
| 77 |
ULONG rc = pCM->OnStartDataSession( &failureCode ); |
| 78 |
if (rc != eGOBI_ERR_NONE) |
| 79 |
{ |
| 80 |
std::ostringstream error; |
| 81 |
error << "Failed to connect, error " << rc; |
| 82 |
|
| 83 |
TRACE( "rc %lu, failure code %lu", rc, failureCode ); |
| 84 |
|
| 85 |
// Show failure code, if present |
| 86 |
if (rc == 1014 && failureCode != 0xFFFFFFFF) |
| 87 |
{ |
| 88 |
error << "\nCall failure reason " << failureCode; |
| 89 |
} |
| 90 |
|
| 91 |
pCM->mView.rootContext()->setContextProperty( "dialogText", |
| 92 |
error.str().c_str() ); |
| 93 |
|
| 94 |
pCM->SetConnectButtonText( "Connect" ); |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
pCM->mView.rootContext()->setContextProperty( "dialogText", "Success!" ); |
| 99 |
|
| 100 |
// Connect button should be updated by state change indication |
| 101 |
} |
| 102 |
|
| 103 |
// Leave the dialog up for 2s |
| 104 |
sleep( 2 ); |
| 105 |
|
| 106 |
pCM->mView.rootContext()->setContextProperty( "windowState", "" ); |
| 107 |
|
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
/*=========================================================================== |
| 112 |
METHOD: |
| 113 |
OnInfosButton (Free Method) |
| 114 |
|
| 115 |
DESCRIPTION: |
| 116 |
Move to the info stats page |
| 117 |
|
| 118 |
PARAMETERS: |
| 119 |
pCM [ I ] - cQTSampleCM object |
| 120 |
|
| 121 |
RETURN VALUE: |
| 122 |
QVariant - always 0 |
| 123 |
===========================================================================*/ |
| 124 |
QVariant OnInfosButton( cQTSampleCM * pCM ) |
| 125 |
{ |
| 126 |
pCM->mView.rootContext()->setContextProperty( "windowState", "infos" ); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
/*=========================================================================== |
| 132 |
METHOD: |
| 133 |
OnConnectionsButton (Free Method) |
| 134 |
|
| 135 |
DESCRIPTION: |
| 136 |
Move to the connection stats page |
| 137 |
|
| 138 |
PARAMETERS: |
| 139 |
pCM [ I ] - cQTSampleCM object |
| 140 |
|
| 141 |
RETURN VALUE: |
| 142 |
QVariant - always 0 |
| 143 |
===========================================================================*/ |
| 144 |
QVariant OnConnectionsButton( cQTSampleCM * pCM ) |
| 145 |
{ |
| 146 |
// "" is the default state (connection stats page) |
| 147 |
pCM->mView.rootContext()->setContextProperty( "windowState", "" ); |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
/*=========================================================================== |
| 153 |
METHOD: |
| 154 |
OnConnectButton (Free Method) |
| 155 |
|
| 156 |
DESCRIPTION: |
| 157 |
Start, cancel, or disconnect from a data session |
| 158 |
|
| 159 |
NOTE: The UI is not updated until this function returns, so the connection |
| 160 |
will be established asynchronously |
| 161 |
|
| 162 |
PARAMETERS: |
| 163 |
pCM [ I ] - cQTSampleCM object |
| 164 |
|
| 165 |
RETURN VALUE: |
| 166 |
QVariant - always 0 |
| 167 |
===========================================================================*/ |
| 168 |
QVariant OnConnectButton( cQTSampleCM * pCM ) |
| 169 |
{ |
| 170 |
// Double check if there a device connected |
| 171 |
if (pCM->mDeviceID.size() == 0 |
| 172 |
|| pCM->mConnectButtonText.compare( "No Device" ) == 0) |
| 173 |
{ |
| 174 |
TRACE( "No Device" ); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
// Start a connection |
| 179 |
if (pCM->mConnectButtonText.compare( "Connect" ) == 0) |
| 180 |
{ |
| 181 |
pCM->SetConnectButtonText( "Cancel" ); |
| 182 |
|
| 183 |
// Create a detached thread to start the connection asynchronously |
| 184 |
pthread_attr_t attributes; |
| 185 |
pthread_attr_init( &attributes ); |
| 186 |
pthread_attr_setdetachstate( &attributes, PTHREAD_CREATE_DETACHED ); |
| 187 |
|
| 188 |
pthread_create( &pCM->mAsyncConnectThreadID, |
| 189 |
&attributes, |
| 190 |
AsyncConnectThread, |
| 191 |
pCM ); |
| 192 |
} |
| 193 |
else if (pCM->mConnectButtonText.compare( "Cancel" ) == 0) |
| 194 |
{ |
| 195 |
pCM->OnCancelDataSession(); |
| 196 |
|
| 197 |
pCM->SetConnectButtonText( "Connect" ); |
| 198 |
} |
| 199 |
else if (pCM->mConnectButtonText.compare( "Disconnect" ) == 0) |
| 200 |
{ |
| 201 |
pCM->OnStopDataSession(); |
| 202 |
|
| 203 |
pCM->SetConnectButtonText( "Connect" ); |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
// Externally connected, etc |
| 208 |
TRACE( "Unknown connect button state %s", |
| 209 |
pCM->mConnectButtonText.c_str() ); |
| 210 |
} |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
/*=========================================================================*/ |
| 216 |
// cQTSampleCM Methods |
| 217 |
/*=========================================================================*/ |
| 218 |
|
| 219 |
/*=========================================================================== |
| 220 |
METHOD: |
| 221 |
Init (Public Method) |
| 222 |
|
| 223 |
DESCRIPTION: |
| 224 |
Initialize GUI |
| 225 |
|
| 226 |
RETURN VALUE: |
| 227 |
bool |
| 228 |
===========================================================================*/ |
| 229 |
bool cQTSampleCM::Init() |
| 230 |
{ |
| 231 |
// Use the current screen orientation |
| 232 |
mView.setOrientation( QmlApplicationViewer::ScreenOrientationAuto ); |
| 233 |
|
| 234 |
// The buttons |
| 235 |
mView.rootContext()->setContextProperty( "connectButton", |
| 236 |
&mConnectButton ); |
| 237 |
mView.rootContext()->setContextProperty( "infosButton", |
| 238 |
&mInfosButton ); |
| 239 |
mView.rootContext()->setContextProperty( "connectionsButton", |
| 240 |
&mConnectionsButton ); |
| 241 |
|
| 242 |
// The input fields |
| 243 |
mView.rootContext()->setContextProperty( "apnNameText", &mAPNText ); |
| 244 |
mView.rootContext()->setContextProperty( "usernameText", &mUsernameText ); |
| 245 |
mView.rootContext()->setContextProperty( "passwordText", &mPasswordText ); |
| 246 |
|
| 247 |
// Default button value |
| 248 |
SetConnectButtonText( "No Device" ); |
| 249 |
|
| 250 |
// Default state |
| 251 |
mView.rootContext()->setContextProperty( "windowState", "" ); |
| 252 |
mView.rootContext()->setContextProperty( "dialogText", "" ); |
| 253 |
|
| 254 |
bool bRC = cSampleCM::Init(); |
| 255 |
|
| 256 |
mView.setMainQmlFile( "qml/GobiSampleCM/main.qml" ); |
| 257 |
mView.show(); |
| 258 |
|
| 259 |
return bRC; |
| 260 |
} |
| 261 |
|
| 262 |
/*=========================================================================== |
| 263 |
METHOD: |
| 264 |
Run (Public Method) |
| 265 |
|
| 266 |
DESCRIPTION: |
| 267 |
Run the GUI (blocks until exit) |
| 268 |
|
| 269 |
RETURN VALUE: |
| 270 |
bool |
| 271 |
===========================================================================*/ |
| 272 |
int cQTSampleCM::Run() |
| 273 |
{ |
| 274 |
return mApp.exec(); |
| 275 |
} |
| 276 |
|
| 277 |
/*=========================================================================== |
| 278 |
METHOD: |
| 279 |
Disconnect (Public Method) |
| 280 |
|
| 281 |
DESCRIPTION: |
| 282 |
Calls GobiDisconnect |
| 283 |
|
| 284 |
RETURN VALUE: |
| 285 |
ULONG |
| 286 |
===========================================================================*/ |
| 287 |
ULONG cQTSampleCM::Disconnect() |
| 288 |
{ |
| 289 |
SetConnectButtonText( "No device" ); |
| 290 |
|
| 291 |
return cSampleCM::Disconnect(); |
| 292 |
} |
| 293 |
|
| 294 |
/*=========================================================================== |
| 295 |
ETHOD: |
| 296 |
OnStartDataSession (Public Method) |
| 297 |
|
| 298 |
DESCRIPTION: |
| 299 |
Updates apn, username, and password input field values before starting |
| 300 |
a data session |
| 301 |
|
| 302 |
PARAMETERS: |
| 303 |
pFailureCode [ O ] - Call failure code, if provided |
| 304 |
|
| 305 |
RETURN VALUE: |
| 306 |
ULONG |
| 307 |
===========================================================================*/ |
| 308 |
ULONG cQTSampleCM::OnStartDataSession( ULONG * pFailureCode ) |
| 309 |
{ |
| 310 |
// Grab the APN, username, and password |
| 311 |
mAPN = mAPNText.getText().toUtf8().constData(); |
| 312 |
mUsername = mUsernameText.getText().toUtf8().constData(); |
| 313 |
mPassword = mPasswordText.getText().toUtf8().constData(); |
| 314 |
|
| 315 |
return cSampleCM::OnStartDataSession( pFailureCode ); |
| 316 |
} |