1

In the code below I am getting info about my cell modem. The result is the data that I expect. Now I need to convert "result" to a QString so I can process the data and get the object path or just extract the Object Path directly. I have tried various ways to convert result but they either throw a unable to convert qdbusmessage error or return a empty string. Can anyone point me in the right direction. Thanks in advance

  QDBusInterface interface( "org.ofono",
                                "/",
                                "org.ofono.Manager",
                                QDBusConnection::systemBus() );

      QDBusMessage result = interface.call( "GetModems");
   qDebug() << "we got a" << result ;
//the last thing I tried was
QString eventReceivedName= result.arguments().at(0).value<QString>();//makes a empty string

This is the output from qDebug and it is what I am expecting.

QDBusMessage(type=MethodReturn, service=":1.4", signature="a(oa{sv})", contents=([Argument: a(oa{sv}) {[Argument: (oa{sv}) [ObjectPath: /hfp/org/bluez/hci0/dev_XX_0D_XX_81_XX_98], [Argument: a{sv} {"Online" = [Variant(bool): false], "Powered" = [Variant(bool): false], "Lockdown" = [Variant(bool): false], "Emergency" = [Variant(bool): false], "Interfaces" = [Variant(QStringList): {}], "Features" = [Variant(QStringList): {}], "Name" = [Variant(QString): "moto g power"], "Type" = [Variant(QString): "hfp"]}]]}]) )
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Freecat
  • 29
  • 5
  • Do you want the same as you get through the log, but in a string instead? Or do you want a piece of the reply? When you get the string, what will you do with it? What is the actual problem you try to solve by getting the string? – Some programmer dude May 15 '22 at 17:06
  • All I really need is the object path if there is a simple way to get it "/hfp/org/bluez/hci0/dev_XX_0D_XX_81_XX_98" but one big string would work just fine and seems like it would be the easy way to do it since I all ready know how to extract the object path from the info shown in the log. – Freecat May 15 '22 at 19:50
  • 1
    Re. `"All I really need is the object path if there is a simple way to get it"`: Doesn't [`QDBusMessage::path`](https://doc.qt.io/qt-6/qdbusmessage.html#path) give you precisely that? Or have I misunderstood? – G.M. May 15 '22 at 20:24
  • When I tried `QString myPath=result.path();` I got a empty string. Is there no way to just convert the whole output into a string? Then I can get any info I need from it. Plus I would like to know how to do that conversion anyway. Thanks for the help everyone. – Freecat May 15 '22 at 23:07
  • I was able to check the type and it returns -1 which is "unknown type" but I am not sure if that is why I can't convert the output or not. – Freecat May 17 '22 at 20:37
  • This is what d-feet shows for GetModems `GetModems () ↦ (Array of [Struct of (Object Path, Dict of {String, Variant})] modems)` – Freecat May 17 '22 at 20:53

1 Answers1

0

So after blowing off mowing the yard and spending a couple more hours on google I finally determined that the single argument that is in "result" is a key, value, map. Then with a little more searching I found the code to extract the data from the map. It returns /hfp/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX Exactly what I needed. The working code is below. Thanks for the replies everyone.

QDBusInterface interface( "org.ofono",
                                    "/",
                                    "org.ofono.Manager",
                                    QDBusConnection::systemBus() );

        QDBusMessage result = interface.call( "GetModems");

        QList<QVariant> args = result.arguments();
               const QDBusArgument &arg = args[0].value<QDBusArgument>();

               arg.beginMap();
               while (!arg.atEnd()) {
                   QString key;
                   QDBusVariant value;
                   arg.beginMapEntry();
                   arg >> key >> value;
                   arg.endMapEntry();

                   qDebug() << key;//could get the value as well with value.variant() 
               }
               arg.endMap();
Freecat
  • 29
  • 5