EPOLL监测不到/sys/class目录下的读写事件?
//-------------------------------------------------------------------------------------
int GetWifiConnectionStatus()
{
char buf[32] = {0};
FILE *fp = fopen("/sys/class/net/wlan0/operstate", "r");
if (!fp){ printf ("/sys/class/net/wlan0/operstate\n");
return 0;
}
fread (buf, sizeof(char), sizeof(buf)-1, fp); //printf ("buf:%s\n", buf);
fclose(fp);
if (0 == strncmp("up", buf, 2)){ //printf ("opersatate:%s\n", buf);
return 1;
}
return 0;
}
#define MAX_WIFI_EVENT 3
//-------------------------------------------------------------------------------------
void *ThrFunWaitWifiEvent(int bCW)
{
int fd = open("/sys/class/net/wlan0/operstate", O_RDONLY);
if (fd < 0){ printf ("open /sys/class/net/wlan0/operstate failed\n");
return -1;
}
int epfd, nfds;
struct epoll_event ev, events[MAX_WIFI_EVENT]; // ev用于注册事件,数组用于返回要处理的事件
epfd = epoll_create(1); //只需要监听一个描述符——标准输出
ev.data.fd = fd;
ev.events = EPOLLIN | EPOLLET; //监听读状态同时设置ET模式
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); //注册epoll事件
while (1){
nfds = epoll_wait(epfd, events, MAX_WIFI_EVENT, -1);
for (int i = 0; i < nfds; i++){
if (events[i].events & EPOLLOUT) printf ("operstatte EPOLLOUT\n");
if (events[i].events & EPOLLIN) printf ("operstatte EPOLLIN\n");
if (events[i].data.fd == fd){
if (GetWifiConnectionStatus()){
SetReporter(REPORTER_NETWORK_CONNECTED, 1);
} else {
SetReporter(REPORTED_NETWORK_DISCONNECTED, 1);
}
}
}
}
if (fd>0){
close(fd);
fd = -1;
}
}
|