glib - Libqmi - glib 回调函数没有被调用
问题描述
我是 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
有谁知道为什么这不起作用?
解决方案
正如菲利普所说(嘿菲利普!),你错过了主循环。该qmi_device_new()
函数是一种异步完成的方法,一旦完成,操作的结果就会在您提供的callback
函数中提供。为了让异步函数做一些事情,只要你的程序逻辑运行,你就需要让 GMainLoop 运行。
推荐阅读
- html - div 底部的文本
- javascript - 用另一个接口初始化一个接口
- python - 多个维度的 Top-k 评分
- android - android初始化中的内容提供者
- python - 使用高斯平滑具有许多峰值的曲线
- java - SAML登录后在Java中收集天蓝色的个人资料图片
- javascript - 无法在 Angular 项目中使用 bpmn.js、bpmn-viewer.js、differ.js 和 change-handler.js 文件
- java - 如何使用 java 在 kubernetes pod 中创建 mysql 转储文件
- openssl - 如何使openssl ocsp响应者忙
- python - 当 MQTT 客户端尝试在没有互联网的情况下连接时如何处理?