0

我是 libqmi 的新手,想从打开一个新设备开始。但是回调函数永远不会被调用,因此不会返回任何设备对象。

我在 Ubuntu 64 位上运行代码。

在这个网站上:https ://developer.gnome.org/gio/stable/GAsyncResult.html

我发现应该如何处理并以这种方式对其进行编程,但它仍然不起作用。

#include <iostream>
#include <libqmi-glib/libqmi-glib.h>
#include <gio/gio.h>

using namespace std;

void device_create_start(const char* device_file);
void device_create_stop(GObject* obj, GAsyncResult* res, gpointer data);

int something = 0;

int main()
{
    cout << "Start\n";
    device_create_start("/dev/cdc-wdm0");

    cout << "DEBUG: Something: " << something << "\n";
    cout << "Stop\n";

    return 0;
}

void device_create_start(const char* device_file)
{
    GFile* file = g_file_new_for_path(device_file);

    if(file)
    {
        GCancellable* cancellable = g_cancellable_new();
        GAsyncReadyCallback callback = device_create_stop;
        gpointer user_data = NULL;

        cout << "INFO: qmi_device_new starting!\n";
        qmi_device_new(file, cancellable, callback, user_data);
        cout << "INFO: qmi_device_new started!\n";

        cout <<  "INFO: Waiting!\n";
        usleep(10000);

        cout <<  "INFO: Is cancelled?: " << g_cancellable_is_cancelled(cancellable) << "\n";
        cout <<  "INFO: canceling!\n";
        g_cancellable_cancel(cancellable);
        cout <<  "INFO: Waiting again!\n";
        usleep(100000);
        cout <<  "INFO: Is cancelled?: " << g_cancellable_is_cancelled(cancellable) << "\n";

        something = 1;
    }
    else
    {
        cout << "ERROR: Could not create device file!\n";
    }
}
void device_create_stop(GObject* obj, GAsyncResult* res, gpointer data)
{
    cout << "INFO: device_create_stop\n";

    something = 2;

    cout << "INFO: qmi_device_new_finish starting\n";
    GError *error;
    QmiDevice* device = qmi_device_new_finish(res, &error);
    cout << "INFO: qmi_device_new_finish started\n";

    if(device == NULL)
    {
         cout << "ERROR: Could not create device!\n";
    }
    else
    {
        cout << "INFO: Device created!\n";
        //device_open(device);
    }
}

当我运行此代码时,输​​出为:

Start
INFO: qmi_device_new starting!
INFO: qmi_device_new started!
INFO: Waiting!
INFO: Is cancelled?: 0
INFO: canceling!
INFO: Waiting again!
INFO: Is cancelled?: 1
DEBUG: Something: 1
Stop

回调函数中的代码永远不会被调用。

更新 1

我简化了代码并更改了我在 gnome 参考站点上监督的一些内容,例如静态回调函数。但这也不起作用

#include <iostream>
#include <libqmi-glib/libqmi-glib.h>
#include <gio/gio.h>
#include <glib/gprintf.h>

using namespace std;

void device_create_start(const char* device_file);
static void device_create_stop(GObject* obj, GAsyncResult* res, gpointer data);

int something = 0;

int main()
{
    g_printf ("Start\n");
    device_create_start("/dev/cdc-wdm0");

    cout << "DEBUG: Something: " << something << "\n";

    while(true)
    {
        ;
    }

    cout << "Stop\n";

    return 0;
}
void device_create_start(const char* device_file)
{
    GFile* file = g_file_new_for_path(device_file);

    if(file)
    {
        cout << "INFO: qmi_device_new starting!\n";
        qmi_device_new(file, NULL, device_create_stop, NULL);
        cout << "INFO: qmi_device_new started!\n";
        something = 1;
    }
    else
    {
        cout << "ERROR: Could not create device!\n";
    }
}
static void device_create_stop(GObject* obj, GAsyncResult* res, gpointer data)
{
    g_printf ("Hurray!\n");
    something = 2;
}

新输出:

Start
INFO: qmi_device_new starting!
INFO: qmi_device_new started!
DEBUG: Something: 1

有谁知道为什么这不起作用?

4

1 回答 1

1

正如菲利普所说(嘿菲利普!),你错过了主循环。该qmi_device_new()函数是一种异步完成的方法,一旦完成,操作的结果就会在您提供的callback函数中提供。为了让异步函数做一些事情,只要你的程序逻辑运行,你就需要让 GMainLoop 运行。

于 2020-02-19T09:17:07.353 回答