2017-09-17 18:45:03 +03:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-03-21 13:21:37 +02:00
|
|
|
#if defined(__linux__)
|
2018-03-28 20:46:27 +03:00
|
|
|
#include <errno.h>
|
2017-09-17 17:18:17 +03:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <linux/wireless.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-09-24 16:33:01 +03:00
|
|
|
#include "../util.h"
|
2017-09-17 17:18:17 +03:00
|
|
|
|
|
|
|
const char *
|
|
|
|
wifi_perc(const char *iface)
|
|
|
|
{
|
2017-10-24 12:03:17 +03:00
|
|
|
int i, cur;
|
|
|
|
float perc;
|
|
|
|
int total = 70; /* the max of /proc/net/wireless */
|
2017-09-17 17:18:17 +03:00
|
|
|
char *p, *datastart;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char status[5];
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
|
|
|
|
fp = fopen(path, "r");
|
|
|
|
if (fp == NULL) {
|
2018-03-28 20:46:27 +03:00
|
|
|
fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
|
2017-09-17 17:18:17 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = fgets(status, 5, fp);
|
|
|
|
fclose(fp);
|
|
|
|
if(!p || strcmp(status, "up\n") != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen("/proc/net/wireless", "r");
|
|
|
|
if (fp == NULL) {
|
2018-03-28 20:46:27 +03:00
|
|
|
fprintf(stderr, "fopen '/proc/net/wireless': %s\n", strerror(errno));
|
2017-09-17 17:18:17 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (i < 2 || !p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((datastart = strstr(buf, iface)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
datastart = (datastart+(strlen(iface)+1));
|
2017-10-24 12:03:17 +03:00
|
|
|
sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &cur);
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2017-10-24 12:03:17 +03:00
|
|
|
perc = (float)cur / total * 100.0;
|
|
|
|
|
|
|
|
return bprintf("%.0f", perc);
|
2017-09-17 17:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
wifi_essid(const char *iface)
|
|
|
|
{
|
|
|
|
static char id[IW_ESSID_MAX_SIZE+1];
|
|
|
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
struct iwreq wreq;
|
|
|
|
|
|
|
|
memset(&wreq, 0, sizeof(struct iwreq));
|
|
|
|
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
|
|
|
|
snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
|
|
|
|
|
|
|
|
if (sockfd == -1) {
|
2018-03-28 20:46:27 +03:00
|
|
|
fprintf(stderr, "socket 'AF_INET': %s\n", strerror(errno));
|
2017-09-17 17:18:17 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wreq.u.essid.pointer = id;
|
|
|
|
if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
|
2018-03-28 20:46:27 +03:00
|
|
|
fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
|
2018-01-07 00:37:09 +02:00
|
|
|
close(sockfd);
|
2017-09-17 17:18:17 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
if (strcmp(id, "") == 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return id;
|
|
|
|
}
|
2018-03-19 00:26:13 +02:00
|
|
|
#endif
|