Libqmi - glib callback function not getting called

386 Views Asked by At

I am new to libqmi and wanted to start by just opening a new device. But the callback function is never getting called and therefore no device object returned.

I running the code on Ubuntu 64 Bit.

On this website: https://developer.gnome.org/gio/stable/GAsyncResult.html

I found how this should be handled and programmed it that way, but it still doesn't work.

#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);
    }
}

When I run this code the output is:

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

The code in the callback function is never called.

Update 1

I simplified the code and changed some things that I oversaw on the gnome reference site, like a static callback function. But this doesn't work either

#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;
}

The new output:

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

Does anyone has a clue why this is not working?

1

There are 1 best solutions below

0
On

As Philip said (hey Philip!), you're missing the main loop. The qmi_device_new() function is an method that finishes asynchronously, and once finished, the result of the operation is provided in the callback function you provide. In order for the asynchronous function to even do something, you need to have a GMainLoop running for as long as your program logic runs.