linux下无线网卡测试,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;

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统下使用USB无线网卡的步骤如下: 1. 插入USB无线网卡:将无线网卡插入计算机的USB接口。一般情况下,Linux系统会自动检测并识别无线网卡。 2. 查看无线网卡:打开终端,并输入以下命令来查看系统是否已经正确识别无线网卡: ```shell lsusb ``` 这个命令会列出所有连接到计算机的USB设备,你应该能够在列表中找到你的无线网卡。 3. 安装驱动程序:如果系统无法自动识别无线网卡或者无法正常工作,你可能需要手动安装相应的驱动程序。首先,你需要确定你的无线网卡型号和制造商,然后在官方网站上找到对应的Linux驱动程序。下载后,按照驱动程序提供的安装指南进行安装。 4. 配置网络连接:一旦无线网卡被正确地识别和配置,你可以使用网络管理工具(如Network Manager或Wicd)来配置连接到无线网络。 - 网络管理工具通常提供图形界面,可以通过系统设置或应用程序菜单中找到。 - 在网络管理工具中,你可以搜索并找到可用无线网络,并输入密码或其他身份验证信息进行连接。 5. 测试网络连接:完成配置后,你可以尝试连接到无线网络并测试网络连接是否正常。你可以打开浏览器,访问一个网页,或者使用ping命令测试网络连接。 注意:根据不同的无线网卡型号和Linux发行版,上述步骤可能会有所不同。如果遇到问题,建议查阅无线网卡的制造商网站或者Linux社区中的相关文档和讨论。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值