Accessing kernel device from Qt application for Android

18 views
Skip to first unread message

Munkhtamir Oyumaa

unread,
Apr 3, 2021, 9:19:21 AMApr 3
to android-qt
Hello Guys,

I have question about how did you access to kernel device using ioctl when you use Qt Android.

The application can just open device but couldn't set configuration using "ioctl" when I run my code.

I have attached my code below.

int FMObject::openDevice(const char *devName)
{
    struct stat st;

    if(FM_FAILED == stat(devName, &st))
    {
        qDebug() << QString("Cannot identify %1: %2, %3")
                    .arg(devName)
                    .arg(errno)
                    .arg(strerror(errno));
        return FM_FAILED;
    }

    if(!S_ISCHR(st.st_mode)) {
        qDebug() << QString("%s is not a valid device").arg(devName);
        return FM_FAILED;
    }

    fd = open(devName, O_RDONLY | O_NONBLOCK);
    if(FM_FAILED == fd)
    {
        qDebug() << QString("Cannot open %1: %2, %3")
                    .arg(devName)
                    .arg(errno)
                    .arg(strerror(errno));
        if(EACCES == errno)
        {
            qDebug() << QString("Insufficient permissions on %1: %2, %3")
                        .arg(devName)
                        .arg(errno)
                        .arg(strerror(errno));
        }
        return FM_FAILED;
    }
    qDebug() << "FD number: " << fd;

    return FM_SUCCESS;
}

int FMObject::setFrequency(float frequency)
{
    qDebug() << "Passed frequency from QML: " << frequency;
    /* convert MHz to Hz*/
    int n_freq = (frequency * 1000000) / 62.5;

    freq.tuner = 0;
    freq.frequency = n_freq;
    freq.type = V4L2_TUNER_RADIO;

    if (ioctl(fd, VIDIOC_S_FREQUENCY, &freq) < 0)
    {
        qDebug() << "ioctl: set frequency";
        qDebug() << "We can't continue without a frequency. Aborting.\n";
        exit(1);
    }
    return FM_SUCCESS;
}

int FMObject::setStart()
{
    system("tinymix 38 0");
    system("tinymix 41 0");
    system("tinymix 48 0");
    system("tinymix 51 0");
    system("tinymix 39 1");
    seek.tuner = 0;
    seek.type = V4L2_TUNER_RADIO;
    seek.wrap_around = 1;

    control.id = V4L2_CID_AUDIO_MUTE;
    control.value = 0;
    if (ioctl(fd, VIDIOC_S_CTRL, &control) < 0) {
        qDebug() << "ioctl: set: mute off";
        qDebug() << "We can't continue without turns mute to off. Aborting";
//        exit(1);
    }
    if (ioctl(fd, VIDIOC_G_TUNER, &tuner) < 0) {
        qDebug() << "ioctl: set: get tuner";
        qDebug() << "We can't continue without a tuner. Aborting";
        exit(1);
    }
    control.id = V4L2_CID_AUDIO_VOLUME;
    control.value = 15;

    if (ioctl(fd, VIDIOC_S_CTRL, &control) < 0) {
        qDebug() << "ioctl: set: set volume";
        qDebug() << "Using the default volume level. Aborting";
        return FM_FAILED;
    }
    return FM_SUCCESS;
}

int FMObject::setDown()
{
    control.id = V4L2_CID_AUDIO_MUTE;
    control.value = 1;

    if (ioctl(fd, VIDIOC_S_CTRL, &control) < 0)
    {
        qDebug() << "radio disable";
        qDebug() << "Failed to disable the radio";
    }
    qDebug() << "Exiting..bye!\n";
    return FM_SUCCESS;
}

Reply all
Reply to author
Forward
0 new messages