嵌入式linux系统,给WIFI模块增加一个开关

嵌入式linux系统,给WIFI模块增加一个开关

    最近做一个项目,电力RTU通信管理主板,CPU选用流行的AM335X。公司之前的RTU监视采用LCD,LCD有主要三个缺陷,1、功耗大;2、容易损坏;3、操作不是很方便。后来公司决定采用WEB方案来开发监视界面,通过WIFI发布,手机接入热点后访问,那么手机/PAD就当HMI来使用。WIFI模块相对LCD成本低,手机/PAD操作也方便。

   但是遇到这样一个问题,电力设备现场调试完后,WIFI安全性就需要重视,当工作人员离场后,必须关闭WIFI,提高安全性。  

   后来查资料,通过linux的 down/up 命令可以实现wifi的开关功能,实现代码如下:

  

//============================================================================ 
//Function: ifconfig_ethx_down_API 
//Description: 关闭本地指定网卡 - eg: ifconfig eth0 down 
//Input: 
//Output: 
//Return: 
//Others: None 
//============================================================================ 
int8_t ifconfig_ethx_down(const u_int8_t *interface_name) 

int sock_fd; 
struct ifreq ifr; 
int selector; 

//传入参数合法性检测 
if(interface_name == NULL) 

return -1; 


//禁止关闭回环 
if(strncmp((char *)interface_name, (char *)"lo", 2) == 0) 

return 0; 


sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 
if(sock_fd < 0) 

return -2; 


sprintf(ifr.ifr_name, "%s", interface_name); 

if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) 

return -3; 


selector = IFF_UP; 
ifr.ifr_flags &= ~selector; 
if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) 

return -4; 


close( sock_fd ); 

return 0; 
}

//============================================================================ 
//Function: ifconfig_ethx_up_API 
//Description: 打开本地指定网卡 - eg: ifconfig eth0 up 
//Input: 
//Output: 
//Return: 
//Others: None 
//============================================================================ 
int8_t ifconfig_ethx_up(const u_int8_t *interface_name) 

int sock_fd; 
struct ifreq ifr; 
int selector; 

//传入参数合法性检测 
if(interface_name == NULL) 

return -1; 


sock_fd = socket(AF_INET, SOCK_DGRAM, 0); 
if(sock_fd < 0) 

return -2; 


sprintf(ifr.ifr_name, "%s", interface_name); 
if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) 

return -3; 


selector = (IFF_UP | IFF_RUNNING); 
ifr.ifr_flags |= selector; 
if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) 

return -4; 


close( sock_fd ); 

return 0; 
}

posted on 2018-06-07 11:06  雪铁龙998  阅读(58)  评论(0编辑  收藏  举报