- Timestamp:
- May 5, 2011, 5:36:53 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.7.2 (added) merged: 845 /branches/vendor/nokia/qt/current merged: 844 /branches/vendor/nokia/qt/4.6.3 removed
- Property svn:mergeinfo changed
-
trunk/tools/runonphone/symbianutils/symbiandevicemanager.cpp
r769 r846 1 1 /**************************************************************************** 2 2 ** 3 ** Copyright (C) 201 0Nokia Corporation and/or its subsidiary(-ies).3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** All rights reserved. 5 5 ** Contact: Nokia Corporation (qt-info@nokia.com) … … 41 41 42 42 #include "symbiandevicemanager.h" 43 #include "trkdevice.h" 43 44 44 45 #include <QtCore/QSettings> … … 49 50 #include <QtCore/QSharedData> 50 51 #include <QtCore/QScopedPointer> 52 #include <QtCore/QSignalMapper> 51 53 52 54 namespace SymbianUtils { … … 62 64 class SymbianDeviceData : public QSharedData { 63 65 public: 64 SymbianDeviceData() : type(SerialPortCommunication) {} 66 SymbianDeviceData(); 67 ~SymbianDeviceData(); 68 69 inline bool isOpen() const { return !device.isNull() && device->isOpen(); } 70 void forcedClose(); 65 71 66 72 QString portName; … … 68 74 QString deviceDesc; 69 75 QString manufacturer; 76 QString additionalInformation; 77 70 78 DeviceCommunicationType type; 79 QSharedPointer<trk::TrkDevice> device; 80 bool deviceAcquired; 71 81 }; 82 83 SymbianDeviceData::SymbianDeviceData() : 84 type(SerialPortCommunication), 85 deviceAcquired(false) 86 { 87 } 88 89 SymbianDeviceData::~SymbianDeviceData() 90 { 91 forcedClose(); 92 } 93 94 void SymbianDeviceData::forcedClose() 95 { 96 // Close the device when unplugging. Should devices be in 'acquired' state, 97 // their owners should hit on write failures. 98 // Apart from the <shared item> destructor, also called by the devicemanager 99 // to ensure it also happens if other shared instances are still around. 100 if (isOpen()) { 101 if (deviceAcquired) 102 qWarning("Device on '%s' unplugged while an operation is in progress.", 103 qPrintable(portName)); 104 device->close(); 105 } 106 } 72 107 73 108 SymbianDevice::SymbianDevice(SymbianDeviceData *data) : 74 109 m_data(data) 75 110 { 76 77 111 } 78 112 … … 97 131 } 98 132 133 void SymbianDevice::forcedClose() 134 { 135 m_data->forcedClose(); 136 } 137 99 138 QString SymbianDevice::portName() const 100 139 { … … 107 146 } 108 147 148 QString SymbianDevice::additionalInformation() const 149 { 150 return m_data->additionalInformation; 151 } 152 153 void SymbianDevice::setAdditionalInformation(const QString &a) 154 { 155 m_data->additionalInformation = a; 156 } 157 158 SymbianDevice::TrkDevicePtr SymbianDevice::acquireDevice() 159 { 160 if (debug) 161 qDebug() << "SymbianDevice::acquireDevice" << m_data->portName 162 << "acquired: " << m_data->deviceAcquired << " open: " << isOpen(); 163 if (isNull() || m_data->deviceAcquired) 164 return TrkDevicePtr(); 165 if (m_data->device.isNull()) { 166 m_data->device = TrkDevicePtr(new trk::TrkDevice); 167 m_data->device->setPort(m_data->portName); 168 m_data->device->setSerialFrame(m_data->type == SerialPortCommunication); 169 } 170 m_data->deviceAcquired = true; 171 return m_data->device; 172 } 173 174 void SymbianDevice::releaseDevice(TrkDevicePtr *ptr /* = 0 */) 175 { 176 if (debug) 177 qDebug() << "SymbianDevice::releaseDevice" << m_data->portName 178 << " open: " << isOpen(); 179 if (m_data->deviceAcquired) { 180 if (m_data->device->isOpen()) 181 m_data->device->clearWriteQueue(); 182 // Release if a valid pointer was passed in. 183 if (ptr && !ptr->isNull()) { 184 ptr->data()->disconnect(); 185 *ptr = TrkDevicePtr(); 186 } 187 m_data->deviceAcquired = false; 188 } else { 189 qWarning("Internal error: Attempt to release device that is not acquired."); 190 } 191 } 192 109 193 QString SymbianDevice::deviceDesc() const 110 194 { … … 124 208 bool SymbianDevice::isNull() const 125 209 { 126 return !m_data->portName.isEmpty(); 210 return m_data->portName.isEmpty(); 211 } 212 213 bool SymbianDevice::isOpen() const 214 { 215 return m_data->isOpen(); 127 216 } 128 217 … … 159 248 } 160 249 161 QDebug operator<<(QDebug d, const SymbianDevice &cd)250 SYMBIANUTILS_EXPORT QDebug operator<<(QDebug d, const SymbianDevice &cd) 162 251 { 163 252 d.nospace() << cd.toString(); … … 167 256 // ------------- SymbianDeviceManagerPrivate 168 257 struct SymbianDeviceManagerPrivate { 169 SymbianDeviceManagerPrivate() : m_initialized(false) {}258 SymbianDeviceManagerPrivate() : m_initialized(false), m_destroyReleaseMapper(0) {} 170 259 171 260 bool m_initialized; 172 261 SymbianDeviceManager::SymbianDeviceList m_devices; 262 QSignalMapper *m_destroyReleaseMapper; 173 263 }; 174 264 … … 186 276 SymbianDeviceManager::SymbianDeviceList SymbianDeviceManager::devices() const 187 277 { 188 if (!d->m_initialized) 189 const_cast<SymbianDeviceManager*>(this)->update(false); 278 ensureInitialized(); 190 279 return d->m_devices; 191 280 } … … 195 284 QString rc; 196 285 QTextStream str(&rc); 286 str << d->m_devices.size() << " devices:\n"; 197 287 const int count = d->m_devices.size(); 198 288 for (int i = 0; i < count; i++) { … … 204 294 } 205 295 296 int SymbianDeviceManager::findByPortName(const QString &p) const 297 { 298 ensureInitialized(); 299 const int count = d->m_devices.size(); 300 for (int i = 0; i < count; i++) 301 if (d->m_devices.at(i).portName() == p) 302 return i; 303 return -1; 304 } 305 206 306 QString SymbianDeviceManager::friendlyNameForPort(const QString &port) const 207 307 { 208 foreach (const SymbianDevice &device, d->m_devices) { 209 if (device.portName() == port) 210 return device.friendlyName(); 211 } 212 return QString(); 308 const int idx = findByPortName(port); 309 return idx == -1 ? QString() : d->m_devices.at(idx).friendlyName(); 310 } 311 312 SymbianDeviceManager::TrkDevicePtr 313 SymbianDeviceManager::acquireDevice(const QString &port) 314 { 315 ensureInitialized(); 316 const int idx = findByPortName(port); 317 if (idx == -1) { 318 qWarning("Attempt to acquire device '%s' that does not exist.", qPrintable(port)); 319 if (debug) 320 qDebug() << *this; 321 return TrkDevicePtr(); 322 } 323 const TrkDevicePtr rc = d->m_devices[idx].acquireDevice(); 324 if (debug) 325 qDebug() << "SymbianDeviceManager::acquireDevice" << port << " returns " << !rc.isNull(); 326 return rc; 213 327 } 214 328 … … 218 332 } 219 333 334 void SymbianDeviceManager::releaseDevice(const QString &port) 335 { 336 const int idx = findByPortName(port); 337 if (debug) 338 qDebug() << "SymbianDeviceManager::releaseDevice" << port << idx << sender(); 339 if (idx != -1) { 340 d->m_devices[idx].releaseDevice(); 341 } else { 342 qWarning("Attempt to release non-existing device %s.", qPrintable(port)); 343 } 344 } 345 346 void SymbianDeviceManager::setAdditionalInformation(const QString &port, const QString &ai) 347 { 348 const int idx = findByPortName(port); 349 if (idx != -1) 350 d->m_devices[idx].setAdditionalInformation(ai); 351 } 352 353 void SymbianDeviceManager::ensureInitialized() const 354 { 355 if (!d->m_initialized) // Flag is set in update() 356 const_cast<SymbianDeviceManager*>(this)->update(false); 357 } 358 220 359 void SymbianDeviceManager::update(bool emitSignals) 221 360 { 361 static int n = 0; 222 362 typedef SymbianDeviceList::iterator SymbianDeviceListIterator; 223 363 224 364 if (debug) 225 qDebug(">SerialDeviceLister::update( %d)\n%s", int(emitSignals),365 qDebug(">SerialDeviceLister::update(#%d, signals=%d)\n%s", n++, int(emitSignals), 226 366 qPrintable(toString())); 227 367 … … 231 371 if (newDevices.size() > 1) 232 372 qStableSort(newDevices.begin(), newDevices.end()); 233 if (d->m_devices == newDevices) // Happy, nothing changed. 373 if (d->m_devices == newDevices) { // Happy, nothing changed. 374 if (debug) 375 qDebug("<SerialDeviceLister::update: unchanged\n"); 234 376 return; 377 } 235 378 // Merge the lists and emit the respective added/removed signals, assuming 236 379 // no one can plug a different device on the same port at the speed of lightning … … 241 384 ++oldIt; 242 385 } else { 243 const SymbianDevice toBeDeleted = *oldIt; 386 SymbianDevice toBeDeleted = *oldIt; 387 toBeDeleted.forcedClose(); 244 388 oldIt = d->m_devices.erase(oldIt); 245 389 if (emitSignals) … … 302 446 // or at least the first one. 303 447 const QString prefix = QLatin1String(linuxBlueToothDeviceRootC); 304 const QString friendlyFormat = QLatin1String("Bluetooth device (%1)");448 const QString blueToothfriendlyFormat = QLatin1String("Bluetooth device (%1)"); 305 449 for (int d = 0; d < 4; d++) { 306 450 QScopedPointer<SymbianDeviceData> device(new SymbianDeviceData); … … 308 452 device->portName = prefix + QString::number(d); 309 453 if (d == 0 || QFileInfo(device->portName).exists()) { 310 device->friendlyName = friendlyFormat.arg(device->portName);454 device->friendlyName = blueToothfriendlyFormat.arg(device->portName); 311 455 rc.push_back(SymbianDevice(device.take())); 456 } 457 } 458 // New kernel versions support /dev/ttyUSB0, /dev/ttyUSB1. Trk responds 459 // on the latter (usually), try first. 460 static const char *usbTtyDevices[] = { "/dev/ttyUSB1", "/dev/ttyUSB0" }; 461 const int usbTtyCount = sizeof(usbTtyDevices)/sizeof(const char *); 462 for (int d = 0; d < usbTtyCount; d++) { 463 const QString ttyUSBDevice = QLatin1String(usbTtyDevices[d]); 464 if (QFileInfo(ttyUSBDevice).exists()) { 465 SymbianDeviceData *device = new SymbianDeviceData; 466 device->type = SerialPortCommunication; 467 device->portName = ttyUSBDevice; 468 device->friendlyName = QString::fromLatin1("USB/Serial device (%1)").arg(device->portName); 469 rc.push_back(SymbianDevice(device)); 312 470 } 313 471 } … … 323 481 } 324 482 325 QDebug operator<<(QDebug d, const SymbianDeviceManager &sdm)483 SYMBIANUTILS_EXPORT QDebug operator<<(QDebug d, const SymbianDeviceManager &sdm) 326 484 { 327 485 d.nospace() << sdm.toString();
Note:
See TracChangeset
for help on using the changeset viewer.