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.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...
随机推荐
- Unity3D脚本中文系列教程(三)
http://dong2008hong.blog.163.com/blog/static/4696882720140302323886/ Unity3D脚本中文系列教程(二) 示,属性不被序列化或显示 ...
- Unity3D脚本中文系列教程(二)
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014030347910/ Unity3D脚本中文系列教程(一) .根据名称或标签定 ...
- Ubuntu12.04 + 虚拟机VMware 9 + Secure CRT + EditPlus 本地C++开发环境搭建
1.1 软件准备 虚拟机VMware 9 Ubuntu 12.04 Secure CRT EditPlus 1.2 安装VMware 9与Ubuntu 12.04 这两个软件安装,按部就班,这里就 ...
- java基础知识回顾之---java String final类之intern方法
public class StringObjectDemo { /** * @param args */ public static void main(String[] args) { String ...
- ***常见复杂SQL语句(含统计类SQL)
1.SQL统计某字段的出现次数 比如统计某个表中,姓名出现的次数:select name,count(*) from biao group by name having count(*) > 2 ...
- POJ 1026 Cipher(置换群)
题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...
- Ubuntu 取消sudo密码
需求:在Ubuntu下装了FQ代理goagent之后,为了goagent能够开机启动.因为goagent需要sudo权限,所以要去掉sudo密码. 要修改的文件位于/etc/sudoers,先备份: ...
- Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
1. 2. 3. 4. 5. 6. 7.
- React使用rAF动画介绍
一. <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF ...
- cmd命令行指定系统延迟关机时间
shutdown -s -t 3600 -c "想要显示的注释" -f 各参数的意思:-s 动作为关机 -t 3600 延迟3600秒关机 -c "想要显示的注释&quo ...