嵌入式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;
}
?
??
轉載于:https://www.cnblogs.com/citroen/p/9149572.html
總結
以上是生活随笔為你收集整理的嵌入式linux系统,给WIFI模块增加一个开关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不厌其烦的四大集成电路
- 下一篇: mqtt 异步消息 长连接 解析