/*!
    \internal
*/
void QBluetoothHeadsetService::newConnection()
{
    qLog(Bluetooth) << "QBluetoothHeadsetService::New client has connected.";

    // New client has connected
    QBluetoothRfcommSocket *socket =
        static_cast<QBluetoothRfcommSocket *>(m_data->m_server->nextPendingConnection());

    qLog(Bluetooth) << "Socket is:" << socket->socketDescriptor();
    if (m_data->m_client &&
        (m_data->m_connectInProgress ||
        (m_data->m_client->state() == QBluetoothRfcommSocket::ConnectedState))) {

        qLog(Bluetooth) << "Already connected, closing client socket.";
        socket->close();
        delete socket;
        return;
    }

    hookupSocket(socket);
    m_data->m_interface->setValue("IsConnected", true);
    qLog(Bluetooth) << "Starting Bluetooth session for Headset";

    if (!m_data->m_session) {
        qLog(Bluetooth) << "Lazy initializing the QCommDeviceSession object";
        QBluetoothLocalDevice local;
        m_data->m_session = new QCommDeviceSession(local.deviceName().toLatin1());
        QObject::connect(m_data->m_session, SIGNAL(sessionOpen()), this, SLOT(sessionOpen()));
        QObject::connect(m_data->m_session, SIGNAL(sessionFailed()), this, SLOT(sessionFailed()));
    }

    m_data->m_session->startSession();

    qLog(Bluetooth) << "The socket remoteAddress is:" << socket->remoteAddress().toString();
    m_data->m_interface->setValue("RemotePeer",
                                  QVariant::fromValue(socket->remoteAddress()));

    emit newConnection(socket->remoteAddress());

    qLog(Bluetooth) << "The socket has bytesAvailable:" << socket->bytesAvailable();

    if (socket->bytesAvailable()) {
        readyRead();
    }
}
/*!
  \internal
*/
void QBluetoothSerialPortService::initiateModemEmulator()
{
    while ( !d->pendingConnections.isEmpty() ) {
        QBluetoothRfcommSerialPort* port = d->pendingConnections.takeFirst();
         // Send a message to the modem emulator to add the serial port.
        QtopiaServiceRequest req( "ModemEmulator", "addSerialPort(QString)" );
        req << port->device();
        req.send();

        d->runningConnections.append( port );

        if (d->runningConnections.size() == 1) {
            if (!d->m_session) {
                QBluetoothLocalDevice local;
                d->m_session = new QCommDeviceSession(local.deviceName().toLatin1(), this);
            }

            d->m_session->startSession();
        }
    }
}
/*!
    This method is the concrete implementation of the
    QBluetoothAudioGateway interface method of the same name.
    It is called from the QBluetoothHandsfreeAudioGatewayServer
    class, which acts as a forwarding agent.

    The address and channel to connect to are given by \a addr
    and \a rfcomm_channel respectively.

    \sa QBluetoothHeadsetAudioGatewayServer
*/
void QBluetoothHeadsetService::connect(const QBluetoothAddress &addr,
                                       int rfcomm_channel)
{
    // If the service is stop, deny connect requests
    if (!m_data->m_server->isListening()) {
        emit connectResult(false, tr("Service not available."));
        return;
    }

    // If we're still connecting or disconnecting, return
    if (m_data->m_connectInProgress || m_data->m_disconnectInProgress) {
        emit connectResult(false, tr("Connection in progress."));
        return;
    }

    // If we're connected, return, caller should call disconnect first
    if (m_data->m_client &&
        (m_data->m_client->state() == QBluetoothRfcommSocket::ConnectedState)) {
        emit connectResult(false, tr("Already connected."));
        return;
    }

    m_data->m_connectInProgress = true;
    m_data->m_numRings = 0;
    m_data->m_addr = addr;
    m_data->m_channel = rfcomm_channel;
    qLog(Bluetooth) << "Starting session for headset.";

    if (!m_data->m_session) {
        qLog(Bluetooth) << "Lazy initializing the QCommDeviceSession object";
        QBluetoothLocalDevice local;
        m_data->m_session = new QCommDeviceSession(local.deviceName().toLatin1());
        QObject::connect(m_data->m_session, SIGNAL(sessionOpen()), this, SLOT(sessionOpen()));
        QObject::connect(m_data->m_session, SIGNAL(sessionFailed()), this, SLOT(sessionFailed()));
    }

    m_data->m_session->startSession();
}