OpenWrt Forum Archive

Topic: Check Wifi interface and internet connection status using c program.

The content of this topic has been archived on 25 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi I want to check the internet connection status (internet is connected / not connected) and wifi interface is up/down.

This I have to do from the Application developed in C which is running inside OpenWRT.

I have used POPEN and SYSTEM functions for this purpose. But all these functions are not running inside infinite loop.

Gives segmentation fault or hangs after several iterations.

I prefer of uses any OpenWRT system call/function or flag or register etc., to avoid the above situation.

Can Anyone help me on this?

What code are u using exactly?

popen() and system() functions must work in a infinite loop without problems, do you make a sleep() of some seconds in each iteration?

(Last edited by iasimov on 22 Jun 2016, 21:09)

Hi

Thanks for the reply.

Now even adding sleep, it is giving same issue....

I am running this on LinkIt Smart MT7688.

It is running only for 338 iteration always.

The code below for your reference.
while(1)
{
if((output = popen("/sbin/ifconfig apcli0 | grep 'inet addr' | wc -l","r"))==(FILE *) NULL)
                {

                        (void) fprintf (stderr, "popen failed - ifconfig\n");
                        pclose(output);
                        continue;
                }
                fscanf(output,"%u",&i);
                pclose(output);


<one more popen function to check internet connection>
........
........
........
sleep(3);
}

remove the first pclose, you are trying to close a null pointer.

and if u have problems with popen, u can redirect the output to a tmp file in /tmp, using system(), and read with fopen.

i.e:
system("sbin/ifconfig apcli0 | grep 'inet addr' | wc -l > /tmp/test1.tmp");
fptr = fopen("/tmp/test1.tmp", "r");
etc...

(Last edited by iasimov on 23 Jun 2016, 08:25)

It is behaving in the same way....after doing the above too....

  system("ifconfig apcli0 | grep 'inet addr' | wc -l > /tmp/testwifi.tmp");
                output = fopen("/tmp/testwifi.tmp", "r");
                fscanf(output,"%u",&i);
                fclose(output);

The discussion might have continued from here.