Home · All Namespaces · All Classes · Grouped Classes · Modules · Functions codeless banner

Tutorial: Making Phone Calls

This document provides some examples of how to make phone calls within Qt Extended from within a client application. It also contains a walk-through of the code showing how a voice call request is handled by the system.

Making a Voice Call

The simplest way to dial a voice call is as follows:

    QPhoneCallManager *mgr;

    void Caller::dial()
    {
        QPhoneCall call = mgr->create("Voice");

        QDialOptions dialOptions;
        dialOptions.setNumber(numberToDial->text());

        call.dial(dialOptions);

        call.connectStateChanged(this, SLOT(stateChanged(QPhoneCall)));
    }

The QPhoneCallManager class provides access to the global list of all calls (voice, data, fax, etc) that are currently active within the system, and allows new calls to be created.

The client application can connect to the state change signal to be notified about the call's progress. The following example shows a message whenever the call changes to the Connected state, or when it ends:

    void Caller::stateChanged(const QPhoneCall& call)
    {
        if (call.state() == QPhoneCall::Connected)
            status->setText("Call " + call.number() + " has connected");
        else if ( call.dropped() )
            status->setText("Call " + call.number() + " has ended");
    }

The QPhoneCall class is a reference-counted pointer to a QPhoneCallPrivate instance. The QPhoneCallPrivate instance will be deleted once the call ends and all outstanding QPhoneCall references to it are removed.

Making a Data Call

The procedure for making a data call is very similar to that for voice calls:

    void Caller::dialData()
    {
        QPhoneCall call = mgr->create("Data");

        QDialOptions dialOptions;
        dialOptions.setNumber(numberToDial->text());
        dialOptions.setSpeed(9600);
        dialOptions.setTransparentMode(QDialOptions::NonTransparent);

        call.dial(dialOptions);

        call.connectStateChanged(this, SLOT(dataStateChanged(QPhoneCall)));
    }

The primary difference is the use of Data for the call type instead of Voice. There are several options within the QDialOptions class that can be set to tune the data call for the client application's specific requirements. The above example will request a 9600 bps non-transparent connection. Consult the documentation for QDialOptions, and the 3GPP TS 27.007 standard for more information on the allowable data call options.

Once the data call transitions to the Connected state, the device() method can be used to gain access to the raw data on the call:

    void Caller::dataStateChanged(const QPhoneCall& call)
    {
        if (call.callType() == "Data" && call.state() == QPhoneCall::Connected) {
            dataDevice = call.device();
            connect(dataDevice, SIGNAL(readyRead()), this, SLOT(dataReady()));
            connect(dataDevice, SIGNAL(closed()), this, SLOT(dataClosed()));
        }
    }

    void Caller::dataReady()
    {
        char buffer[1024];
        qint64 len;
        while ( ( len = dataDevice->read( buffer, sizeof(buffer) ) ) > 0 ) {
            // There is new data ready to process.
            // ...
        }
    }

    void Caller::dataClosed()
    {
        // The data call has ended - the device is now closed.
        dataDevice = 0;
    }

    void Caller::dataWrite( const char *buf, int len )
    {
        // Send the specified data on the data call if it is still active.
        if (dataDevice)
            dataDevice->write(buf, len);
    }

Watching for an Incoming Voice Call

Client applications can watch for incoming calls by listening on the QPhoneCallManager::newCall() signal and checking the new call's state against QPhoneCall::Incoming:

    mgr = new QPhoneCallManager(this);
    connect(mgr, SIGNAL(newCall(QPhoneCall)), this, SLOT(newCall(QPhoneCall)));

    void Caller::newCall(const QPhoneCall& call)
    {
        if ( call.state() == QPhoneCall::Incoming &&
             call.callType() == "Voice" ) {
            status->setText("Incoming call from " + call.number());
        }
    }

Monitoring the State of All Calls

Programs that implement dialer functionality need to display information about all calls that are currently active within the system. The QPhoneCallManager class can be used for this purpose.

The QPhoneCallManager::calls() method provides a list of all currently active calls. The QPhoneCallManager::newCall() signal notifies listeners whenever a new call is added to the list, even if it was created by another client application.

Behind the Scenes: Making a Voice Call

We will now walk through the first example in more detail, explaining what happens within Qt Extended to effect a voice call.

    QPhoneCallManager mgr;
    QPhoneCall call = mgr.create( "Voice" );

The QPhoneCallManager::create() function first calls QPhoneCallManager::services() to locate a telephony service that can handle calls of the specified type, Voice. The service information is stored in the value space (see Value Space) under the /Communications/QPhoneCallProvider/CallTypes directory. A typical arrangement is as follows:

ServiceSupported Call Types
modemVoice, Data, Fax, Video, IP
voipVoIP

In this case, the modem service supports the desired Voice call type. By convention, call types start with an upper case letter, and service names start with a lower case letter.

If more than one service were to support a call type, then QPhoneCallManager::services() will return the first that it finds. The caller can explicitly request a particular service in the call to QPhoneCallManager::create():

    QPhoneCallManager mgr;
    QPhoneCall call = mgr.create( "Voice", "modem" );

Once the service has been located, QPhoneCallManager::create() calls QPhoneCallManagerPrivate::create() in qphonecallmanager.cpp. This method constructs a QPhoneCallPrivate instance, wraps it with a QPhoneCall reference, and returns it to the caller.

The constructor for QPhoneCallPrivate in qphonecall.cpp attaches to two Qt Extended IPC channels:

    QPE/Communications/QPhoneCallProvider/Request
    QPE/Communications/QPhoneCallProvider/Response

The first Qt Extended IPC channel is used to send requests to the telephony service to perform call operations. The second Qt Extended IPC channel is used to receive state information about the call back from the telephony service.

The next step is to dial the call:

    void Caller::dial()
    {
        QPhoneCall call = mgr->create("Voice");

        QDialOptions dialOptions;
        dialOptions.setNumber(numberToDial->text());

        call.dial(dialOptions);

        call.connectStateChanged(this, SLOT(stateChanged(QPhoneCall)));
    }

After initializing the local state of QPhoneCallPrivate, QPhoneCall::dial() sends the Qt Extended IPC message dial(QString,QString,QString,QDialOptions) to the Request channel.

In the telephony service, the Qt Extended IPC message is received by QPhoneCallProvider::dial() in qphonecallprovider.cpp. It filters the message to check that it is intended for that service (all call providers listen on the same channel), and then calls QPhoneCallProvider::create() to create a QPhoneCallImpl object to represent the server-side instance of the phone call.

The QPhoneCallProvider::create() method is implemented differently for each telephony service. For AT-based modems, it is overridden by QModemCallProvider::create() in qmodemcallprovider.cpp, which then creates a QModemCall object.

Once the server-side instance has been created, QPhoneCallProvider::dial() calls QPhoneCallImpl::dial(), which in our case is overridden by QModemCall::dial() in qmodemcall.cpp. QModemCall::dial() issues the ATD command to the modem and the dial starts.

As the call progresses, the QModemCall class will advertise state changes by calling the QPhoneCallImpl::setState() method. This will cause the QPhoneCallProvider::stateChanged() slot to be invoked, which in turn calls QPhoneCallProvider::sendState().

The QPhoneCallProvider::sendState() method in qphonecallprovider.cpp sends a Qt Extended IPC message on the Response channel, which will return back to the client application in the QPhoneCallPrivate::callStateChanged() method in qphonecall.cpp. This will cause the stateChanged() signal to be emitted on QPhoneCall, notifying the client application.

See also QPhoneCall, QPhoneCallManager, QDialOptions, QPhoneCallProvider, QModemService, QModemCallProvider, and QModemCall.


Copyright © 2009 Trolltech Trademarks
Qt Extended 4.4.3