How to validate radio frequencies in a Linux radio device driver?

217 views Asked by At

I know that an unsigned number can never be less than 0 and there is no need to test for it. But I want to avoid bogus frequencies for the tuner.

/* freqKHz is in KHz (duh) */   
void videoinput_set_tuner_freq( videoinput_t *vidin, int freqKHz )
{
        unsigned long frequency = freqKHz;

        if( videoinput_has_tuner( vidin ) ) {
            if( frequency < 0 ) {
                /* Ignore bogus frequencies. */
                return;
        }

        frequency *= 16;

        vidin->tunerlow = (tuner.capability & V4L2_TUNER_CAP_LOW) ? 1 : 0;

        if( !vidin->tunerlow ) {
            frequency /= 1000; /* switch to MHz */
        }

        ...
}

Is it possible to make this check in another way (maybe with a limit)?

Come up with a better definition of a "bogus" frequency that a value that can't possibly exist.

The digital driver does not set the frequency if it has not changed since it was tuned. The analog tuner driver knows nothing about the frequency saved by the digital driver. When the frequency is set using the video4linux code, the hardware get changed but the digital driver's state does not get updated.

1

There are 1 answers

0
chux - Reinstate Monica On

1 As you are converting

unsigned long frequency = freqKHz;

performing a sign check make sense of

if( freqKHz < 0 ) {  // note: freqKHz
  /* Ignore bogus frequencies. */
  return;

2 A subsequent check for a reasonable bound, per you application, is also good practice. Of course the min check could be combine with the above.

if((frequency < MinFreq ) || (( frequency > MaxFreq ) {
  /* Ignore bogus frequencies. */
  return;

3 Recommend a rounded conversion:

frequency = (frequency + 1000/2) / 1000; /* switch to MHz */