Libqmi-glib回调函数未调用

问题描述 投票:0回答:1

我是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

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

glib libqmi
1个回答
1
投票

正如菲利普所说(嗨,菲利普!),您错过了主循环。 qmi_device_new()函数是一种异步完成的方法,完成后,操作结果将在您提供的callback函数中提供。为了使异步功能什至可以执行某些操作,您需要在程序逻辑运行期间一直运行GMainLoop。

© www.soinside.com 2019 - 2024. All rights reserved.