Linux C程序如何检测WIFI无线USB网卡是否可用?
最新做一个WIFI应用项目。如何检测WIFI USB设备是否插上了呢?特此共享。
第一种方法,采用读取文件的方式。在linux下,任何一种设备都可看成文件。通过分析相关文件信息,可得知WIFI设备是否存在;代码示例如下:
static void WIFI_Enum_Device(void)
{
char buff[1024];
FILE * fh;
/* Check if /proc/net/wireless is available */
fh = fopen(PROC_NET_WIRELESS, "r");
if(fh != NULL)
{
/* Success : use data from /proc/net/wireless */
/* Eat 2 lines of header */
fgets(buff, sizeof(buff), fh);
fgets(buff, sizeof(buff), fh);
/* Read each device line */
while(fgets(buff, sizeof(buff), fh))
{
char name[IFNAMSIZ + 1];
char *s;
/* Skip empty or almost empty lines. It seems that in some
* cases fgets return a line with only a newline. */
if((buff[0] == '\0') || (buff[1] == '\0'))
continue;
/* Extract interface name */
s = WIFI_Get_DeviceName(name, sizeof(name), buff);
if(!s)
{
/* Failed to parse, complain and continue */
#ifndef IW_RESTRIC_ENUM
fprintf(stderr, "Cannot parse " PROC_NET_DEV "\n");
#else
fprintf(stderr, "Cannot parse " PROC_NET_WIRELESS "\n");
#endif
}
else
/* Got it, save the name about this interface */
{//we always use the first detected device when doing first time detecting
if(s_DeviceCount == 0)
{
if(strcmp(s_Deviceinfo.DeviceName,name))
{
memset((char *)&s_Deviceinfo, 0, sizeof(DeviceInfo_t));
memcpy(s_Deviceinfo.DeviceName,name,IFNAMSIZ);
}
if(strlen(s_SavedDevice) == 0)//this is the first detected device when doing first time detecting, we save it
memcpy(s_SavedDevice,name,IFNAMSIZ);
}
else
{//there is more than one device, we should use the first detected
if(!strcmp(s_SavedDevice,name))
{
memset((char *)&s_Deviceinfo, 0, sizeof(DeviceInfo_t));
memcpy(s_Deviceinfo.DeviceName,name,IFNAMSIZ);
}
}
s_DeviceCount++;
}
}
fclose(fh);
}
}
static char* WIFI_Get_DeviceName(char * name, /* Where to store the name */
int nsize, /* Size of name buffer */
char * buf) /* Current position in buffer */
{
char * end;
/* Skip leading spaces */
while(isspace(*buf))
buf++;
#ifndef IW_RESTRIC_ENUM
/* Get name up to the last ':'. Aliases may contain ':' in them,
* but the last one should be the separator */
end = strrchr(buf, ':');
#else
/* Get name up to ": "
* Note : we compare to ": " to make sure to process aliased interfaces
* properly. Doesn't work on /proc/net/dev, because it doesn't guarantee
* a ' ' after the ':'*/
end = strstr(buf, ": ");
#endif
/* Not found ??? To big ??? */
if((end == NULL) || (((end - buf) + 1) > nsize))
return(NULL);
/* Copy */
memcpy(name, buf, (end - buf));
name[end - buf] = '\0';
/* Return value currently unused, just make sure it's non-NULL */
return(end);
}
RETURN_TYPE APP_WIFI_DetectDevice(void)
{
char command[50] = {'\0'};
s_DeviceCount = 0; //reset count
WIFI_Enum_Device();
s_LastDeviceCount = s_DeviceCount;
if(s_DeviceCount > 0)
{
sprintf(command,"ifconfig %s up",s_Deviceinfo.DeviceName);
system(command); //boot up the device firstly
return SYS_NOERROR;
}
else
return SYS_FAILED;
}
第二种方法,利用linux ioctl函数读取I/O接口的相关信息。
/*****************************************************************************
* Name : trid_char * APP_NetLink_GetIFFLAGS(char *NetDev )
* Description : Get net interface IFFLAGS
* Params : NetDev
* Returns : the string of the NetDev
* Author/date : Danny.Hu /2011.11.16
*****************************************************************************/
RETURN_TYPE APP_NetLink_GetIFFlags( trid_char *NetDev )
{
int fd = -1;
int InterfaceFlags;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, NetDev);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
printf("Cannot get control socket\n");
close(fd);
return SYS_FAILED;
}
else if( 0!=(ioctl(fd, SIOCGIFFLAGS, (char*)&ifr)) )
{
printf("Cannot get Network Interface Flags!\n");
close(fd);
return SYS_FAILED;
}
InterfaceFlags = ifr.ifr_flags;
printf("<");
if ( InterfaceFlags & IFF_UP) printf("Network %s is UP, ", NetDev);
if ( InterfaceFlags & IFF_BROADCAST) printf("Network %s is BCAST, ", NetDev);
if ( InterfaceFlags & IFF_MULTICAST) printf("Network %s is MCAST, ", NetDev);
if ( InterfaceFlags & IFF_LOOPBACK) printf("Network %s is LOOP, ", NetDev);
if ( InterfaceFlags & IFF_POINTOPOINT) printf("Network %s is P2P, ", NetDev);
printf(">\n");
close(fd);
return SYS_NOERROR;
}
Linux C程序如何检测WIFI无线USB网卡是否可用?的更多相关文章
- TL-WDN5200H无线usb网卡在Linux上的使用
买了个TL-WDN5200H无线usb网卡,但是发现它居然不支持Linux,但是我有时需要在Linux上使用,这就尴尬了.于是到网上搜索资料,终于解决了这个问题. 首先编译安装:https://git ...
- linux,windows下检测指定的IP地址是否可用或者检测IP地址冲突的3种方式(批处理程序,python程序,linux shell 批量ping)
本文中的脚本适用范围: 1)检测某些IP地址是否被占用: 2)检测网络中某些设备是否存活: 3)在分配新的ip地址之前,批量检测环境中是否存在冲突的机器 以上检测基于ICMP Ping报文,要求所有的 ...
- 最近在无线USB网卡投入比较大
第一次(40): 乐光N18网卡 17 5米USB延长线 10 DVD2 3 运费10 第二次(30): 8187L主板13 5DB/6DB全向天线 5 外壳FREE 运费12 第三次(20): 8D ...
- 【智能无线小车系列八】在树莓派上使用USB网卡
在这个腾“云”驾“物”(云:云计算,物:物联网)的时代,什么都可以没有,就是不能没有网络,树莓派也离不开它.本章节将详细介绍如何将树莓派接入互联网,因为有一些后期将要使用到的小软件需要联网进行下载和安 ...
- 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(二)
作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第二篇,主要讲述发送端程序的原理和过程. 第一 ...
- 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(一)
作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第一篇,作为前言和概述. 第二篇:基于Oran ...
- 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(三)
作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第三篇,主要讲述接收端程序的原理和过程. 第一 ...
- 【树莓派】【转】利用USB网卡配置树莓派为无线热点
由于Wifi很慢,基本不可用:树莓派有无线网卡,恰好看到有文章用树莓派来做无线热点,利用树莓派来共享无线网络.比较有用,转发后续尝试. 本文转自:https://www.embbnux.com/201 ...
- 如何在 Arch Linux 的终端里设定 WiFi 网络
如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...
随机推荐
- grub,mbr的那些事
今天遇到一个问题是:双系统为win10和Ubuntu.启动模式为mbr,当前可以启动win10,但不能启动Ubuntu.先利用easybcd重新添加了一个,想着依旧用win10的启动项,(此处可以参考 ...
- hdu 1134 Game of Connections
主要考察卡特兰数,大数乘法,除法…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=1134 #include<iostream>#include ...
- android 上下文菜单详解
本文使用xml来创建上下文菜单 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:andr ...
- kindeditor.net应用
1.网址:http://kindeditor.net/docs/usage.html
- [2-sat]HDOJ1824 Let's go home
中问题 题意略 和HDOJ 3062一样 这里 每个队员都有 选 和 不选 两种, 即 上篇所说的$x$和$x’$ 建图:队长(a)留下或者其余两名队员(b.c)同时留下 那么就是$a' \Right ...
- 如何禁用 radio ,设置为只读,不能选定
如何禁用 radio ,设置为只读,不能选定 禁用 radio ,设置为只读,不能选定: <input name="gender" type="radio" ...
- Asterisk服务安装配置和启动
Asterisk服务安装配置和启动 2014年11月4日 11:36 注意: 更新源的重要性 源的地址: http://fffo.blog.163.com/blog/static/2119130682 ...
- cdev_init函数
linux-2.6.22/include/linux/cdev.hstruct cdev { struct kobject kobj; // 每个 cdev 都是一个 kobje ...
- WPF中的Drawing
以前在用WinForm的时候,可以通过GDI+接口在窗体上动态绘制自定义的图形.在WPF中有没有对应的API呢,最近项目中用到了这个,在这里总结一下. WPF中的Drawing主要提供了几类API: ...
- 1742. Team building(dfs)
1742 最小的是找联通块数 最大的找环 一个环算一个 其它的数各算一个 #include <iostream> #include<cstdio> #include<cs ...