Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tools/runonphone/symbianutils/symbiandevicemanager.cpp

    r769 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    4141
    4242#include "symbiandevicemanager.h"
     43#include "trkdevice.h"
    4344
    4445#include <QtCore/QSettings>
     
    4950#include <QtCore/QSharedData>
    5051#include <QtCore/QScopedPointer>
     52#include <QtCore/QSignalMapper>
    5153
    5254namespace SymbianUtils {
     
    6264class SymbianDeviceData : public QSharedData {
    6365public:
    64     SymbianDeviceData() : type(SerialPortCommunication) {}
     66    SymbianDeviceData();
     67    ~SymbianDeviceData();
     68
     69    inline bool isOpen() const { return !device.isNull() && device->isOpen(); }
     70    void forcedClose();
    6571
    6672    QString portName;
     
    6874    QString deviceDesc;
    6975    QString manufacturer;
     76    QString additionalInformation;
     77
    7078    DeviceCommunicationType type;
     79    QSharedPointer<trk::TrkDevice> device;
     80    bool deviceAcquired;
    7181};
     82
     83SymbianDeviceData::SymbianDeviceData() :
     84        type(SerialPortCommunication),
     85        deviceAcquired(false)
     86{
     87}
     88
     89SymbianDeviceData::~SymbianDeviceData()
     90{
     91    forcedClose();
     92}
     93
     94void 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}
    72107
    73108SymbianDevice::SymbianDevice(SymbianDeviceData *data) :
    74109    m_data(data)
    75110{
    76 
    77111}
    78112
     
    97131}
    98132
     133void SymbianDevice::forcedClose()
     134{
     135    m_data->forcedClose();
     136}
     137
    99138QString SymbianDevice::portName() const
    100139{
     
    107146}
    108147
     148QString SymbianDevice::additionalInformation() const
     149{
     150    return m_data->additionalInformation;
     151}
     152
     153void SymbianDevice::setAdditionalInformation(const QString &a)
     154{
     155    m_data->additionalInformation = a;
     156}
     157
     158SymbianDevice::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
     174void 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
    109193QString SymbianDevice::deviceDesc() const
    110194{
     
    124208bool SymbianDevice::isNull() const
    125209{
    126     return !m_data->portName.isEmpty();
     210    return m_data->portName.isEmpty();
     211}
     212
     213bool SymbianDevice::isOpen() const
     214{
     215    return m_data->isOpen();
    127216}
    128217
     
    159248}
    160249
    161 QDebug operator<<(QDebug d, const SymbianDevice &cd)
     250SYMBIANUTILS_EXPORT QDebug operator<<(QDebug d, const SymbianDevice &cd)
    162251{
    163252    d.nospace() << cd.toString();
     
    167256// ------------- SymbianDeviceManagerPrivate
    168257struct SymbianDeviceManagerPrivate {
    169     SymbianDeviceManagerPrivate() : m_initialized(false) {}
     258    SymbianDeviceManagerPrivate() : m_initialized(false), m_destroyReleaseMapper(0) {}
    170259
    171260    bool m_initialized;
    172261    SymbianDeviceManager::SymbianDeviceList m_devices;
     262    QSignalMapper *m_destroyReleaseMapper;
    173263};
    174264
     
    186276SymbianDeviceManager::SymbianDeviceList SymbianDeviceManager::devices() const
    187277{
    188     if (!d->m_initialized)
    189         const_cast<SymbianDeviceManager*>(this)->update(false);
     278    ensureInitialized();
    190279    return d->m_devices;
    191280}
     
    195284    QString rc;
    196285    QTextStream str(&rc);
     286    str << d->m_devices.size() << " devices:\n";
    197287    const int count = d->m_devices.size();
    198288    for (int i = 0; i < count; i++) {
     
    204294}
    205295
     296int 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
    206306QString SymbianDeviceManager::friendlyNameForPort(const QString &port) const
    207307{
    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
     312SymbianDeviceManager::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;
    213327}
    214328
     
    218332}
    219333
     334void 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
     346void 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
     353void SymbianDeviceManager::ensureInitialized() const
     354{
     355    if (!d->m_initialized) // Flag is set in update()
     356        const_cast<SymbianDeviceManager*>(this)->update(false);
     357}
     358
    220359void SymbianDeviceManager::update(bool emitSignals)
    221360{
     361    static int n = 0;
    222362    typedef SymbianDeviceList::iterator SymbianDeviceListIterator;
    223363
    224364    if (debug)
    225         qDebug(">SerialDeviceLister::update(%d)\n%s", int(emitSignals),
     365        qDebug(">SerialDeviceLister::update(#%d, signals=%d)\n%s", n++, int(emitSignals),
    226366               qPrintable(toString()));
    227367
     
    231371    if (newDevices.size() > 1)
    232372        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");
    234376        return;
     377    }
    235378    // Merge the lists and emit the respective added/removed signals, assuming
    236379    // no one can plug a different device on the same port at the speed of lightning
     
    241384                ++oldIt;
    242385            } else {
    243                 const SymbianDevice toBeDeleted = *oldIt;
     386                SymbianDevice toBeDeleted = *oldIt;
     387                toBeDeleted.forcedClose();
    244388                oldIt = d->m_devices.erase(oldIt);
    245389                if (emitSignals)
     
    302446    // or at least the first one.
    303447    const QString prefix = QLatin1String(linuxBlueToothDeviceRootC);
    304     const QString friendlyFormat = QLatin1String("Bluetooth device (%1)");
     448    const QString blueToothfriendlyFormat = QLatin1String("Bluetooth device (%1)");
    305449    for (int d = 0; d < 4; d++) {
    306450        QScopedPointer<SymbianDeviceData> device(new SymbianDeviceData);
     
    308452        device->portName = prefix + QString::number(d);
    309453        if (d == 0 || QFileInfo(device->portName).exists()) {
    310             device->friendlyName = friendlyFormat.arg(device->portName);
     454            device->friendlyName = blueToothfriendlyFormat.arg(device->portName);
    311455            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));
    312470        }
    313471    }
     
    323481}
    324482
    325 QDebug operator<<(QDebug d, const SymbianDeviceManager &sdm)
     483SYMBIANUTILS_EXPORT QDebug operator<<(QDebug d, const SymbianDeviceManager &sdm)
    326484{
    327485    d.nospace() << sdm.toString();
Note: See TracChangeset for help on using the changeset viewer.