D-Bus入门(四)——QTDBUS代码,实现ofono代码下的python测试文件activite-context的功能

一、参考列表

meego-cellular-ofono-qt

connman-qt
meego-ux-setting中的cellularSetting部分

合计来说一共就这三个包含了,connman-server、connman-manager、org.ofono.connectcontext、org.ofono.connectmanager四种d-bus的各种接口的各种使用方法

dbus之xml文件参考地址

二、代码

    connectcontext.xml

system(qdbusxml2cpp -c ConnectionContext -p connectioncontext -N connectioncontext.xml)

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
	<interface name="org.ofono.ConnectionContext">
		<method name="GetProperties">
			<arg type="a{sv}" direction="out"/>
			<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/>
		</method>
		<method name="SetProperty">
			<arg type="s" direction="in"/>
			<arg type="v" direction="in"/>
		</method>
		<signal name="PropertyChanged">
			<arg type="s"/>
			<arg type="v"/>
		</signal>
	</interface>
</node>

connectmanager.xml

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
       <interface name="org.ofono.ConnectionManager">
                <method name="GetProperties">
                        <arg type="a{sv}" direction="out"/>
                        <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/>
                </method>
                <method name="SetProperty">
                        <arg type="s" direction="in"/>
                        <arg type="v" direction="in"/>
                </method>
                <method name="AddContext">
                        <arg type="s" direction="in"/>
                        <arg type="o" direction="out"/>
                </method>
                <method name="RemoveContext">
                        <arg type="o" direction="in"/>
                </method>
                <method name="DeactivateAll"/>
                <method name="GetContexts">
                        <arg type="a(oa{sv})" direction="out"/>
                        <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QArrayOfPathProperties"/>
                </method>
                <signal name="PropertyChanged">
                        <arg type="s"/>
                        <arg type="v"/>
                </signal>
                <signal name="ContextAdded">
                        <arg type="o"/>
                        <arg type="a{sv}"/>
                        <annotation name="com.trolltech.QtDBus.QtTypeName.In1" value="QVariantMap"/>
                </signal>
                <signal name="ContextRemoved">
                        <arg type="o"/>
                </signal>
        </interface>
</node>


由于QArrayOfPathProperties总是出错,所以放弃了使用XML的代理d-bus的实现方法。


ofonodbustest.cpp

#include "ofonodbustest.h"

ofonodbustest::ofonodbustest(QObject *parent) :
    QObject(parent)
{
}


QDBusArgument &operator<<(QDBusArgument &argument, const OfonoModemStruct &modem)
{
    argument.beginStructure();
    argument << modem.path << modem.properties;
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, OfonoModemStruct &modem)
{
    argument.beginStructure();
    argument >> modem.path >> modem.properties;
    argument.endStructure();
    return argument;
}


void ofonodbustest::setError(const QString& errorName, const QString& errorMessage)
{
    m_errorName = errorName;
    m_errorMessage = errorMessage;
}

void ofonodbustest::setPropertyResp()
{
    m_pendingProperty = QString();
    // emit nothing; we will get a PropertyChanged signal
}

void ofonodbustest::setPropertyErr(const QDBusError& error)
{
    QString prop = m_pendingProperty;
    setError(error.name(), error.message());
    m_pendingProperty = QString();
    emit setPropertyFailed(prop);
}
void ofonodbustest::enableTechnology(void)
{
    //Step1: Get Modems
    qDebug("Step1: Start to connect gprs/3g. ");
    QDBusReply<OfonoModemList> replyGetModems;
    OfonoModemList modemsGetModems;
    QDBusMessage requestGetModems;

    qDBusRegisterMetaType<OfonoModemStruct>();
    qDBusRegisterMetaType<OfonoModemList>();

    requestGetModems = QDBusMessage::createMethodCall("org.ofono", "/", "org.ofono.Manager", "GetModems");
    replyGetModems = QDBusConnection::systemBus().call(requestGetModems);
    modemsGetModems = replyGetModems;
    foreach(OfonoModemStruct modem, modemsGetModems) {
        m_modems << modem.path.path();
    }


    //Step2: Get contexts
    //2: "Python": contexts = connman.GetContexts()
    qDebug("Step2: Start to get contexts.");
    QDBusInterface interfaceOfonoConnman("org.ofono", m_modems.at(0), "org.ofono.ConnectionManager",QDBusConnection::systemBus());
    QDBusReply <OfonoModemList> replyGetContexts = interfaceOfonoConnman.call("GetContexts");
    OfonoModemList contexts = replyGetContexts.value();
    if(contexts.count() < 1)
    {
        //"Python": print "No context available"
        qDebug()<<"CellularSettings::ERROR: could not get active context";
    }



    //Step3: Set Powered->已验证以下代码可用,不过没什么作用
    // connman.SetProperty("Powered", dbus.Boolean(1))
//    qDebug("Step3: Set Powered.");
//    QList<QVariant> argumentList;
//    argumentList << qVariantFromValue(QString("Powered")) << qVariantFromValue(QDBusVariant(QVariant(true)));
//    qDebug("Step3: Set argumentList.");
//    QDBusReply<void> replyOfonoConnman = interfaceOfonoConnman.asyncCallWithArgumentList(QLatin1String("SetProperty"), argumentList);
//    if(replyOfonoConnman.error().isValid())
//    {
//        qDebug()<<replyOfonoConnman.error().message();
//    }


    //Step4: Active context
    qDebug("Step4: Active context.");
    ConnectionContext * contextCC = new ConnectionContext("org.ofono",contexts.at(0).path.path(),QDBusConnection::systemBus(),this);
    QDBusReply<void> replyContext = contextCC->SetProperty(QString("Active"),QDBusVariant(QVariant(true)));
    if(replyContext.error().isValid())
    {
        qDebug()<<replyContext.error().message();
    }
}

ofonodbustest.h

#ifndef OFONODBUSTEST_H
#define OFONODBUSTEST_H

#include "connectioncontext.h"
#include <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>
#include <QtCore/QDebug>
#include <QDir>

#define SET_PROPERTY_TIMEOUT 300000

struct OfonoModemStruct {
    QDBusObjectPath path;
    QVariantMap properties;
};

typedef QList<OfonoModemStruct> OfonoModemList;
Q_DECLARE_METATYPE(OfonoModemStruct)
Q_DECLARE_METATYPE(OfonoModemList)

class ofonodbustest : public QObject
{
    Q_OBJECT

public:
    explicit ofonodbustest(QObject *parent = 0);
    void enableTechnology(void);

signals:
    void setPropertyFailed(const QString &name);

public slots:
    void setError(const QString &errorName, const QString &errorMessage);

private slots:
    void setPropertyResp();
    void setPropertyErr(const QDBusError& error);

protected:
   QString m_errorName;
   QString m_errorMessage;

private:
    QString m_pendingProperty;
    QStringList m_modems;
};

#endif // OFONODBUSTEST_H

pro 文件


TEMPLATE = app

TARGET = activate-context-qt

OBJECTS_DIR = tmp
MOC_DIR = tmp

QT -= gui

QT +=   dbus \
        xml \
        xmlpatterns

CONFIG += qt \
        dbus 

SOURCES += \
    mian.cpp \
    ofonodbustest.cpp \
    connectioncontext.cpp

HEADERS += \
    ofonodbustest.h \
    connectioncontext.h  

OTHER_FILES +=  connectioncontext.xml

system(qdbusxml2cpp -c ConnectionContext -p connectioncontext -N connectioncontext.xml)
system(qdbusxml2cpp -c ConnectionManager -p connectionmanager -N connectionmanager.xml)












   

你可能感兴趣的:(xml,object,python,properties,测试,Signal)