Merge pull request #7 from berlin-open-wireless-lab/feature/read_rssi_libiwinfo

Feature/read rssi libiwinfo
This commit is contained in:
Polynomdivision 2017-10-27 08:34:13 +02:00 committed by GitHub
commit e469f2e730
5 changed files with 55 additions and 49 deletions

View file

@ -42,7 +42,7 @@ SET(SOURCES
utils/utils.c include/rssi.h utils/rssi.c) utils/utils.c include/rssi.h utils/rssi.c)
SET(LIBS SET(LIBS
ubox ubus json-c blobmsg_json config uci gcrypt ssl crypto) ubox ubus json-c blobmsg_json config uci gcrypt ssl crypto iwinfo)
ADD_EXECUTABLE(dawn ${SOURCES} utils/dawn_uci.c include/dawn_uci.h) ADD_EXECUTABLE(dawn ${SOURCES} utils/dawn_uci.c include/dawn_uci.h)

View file

@ -4,7 +4,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
int get_rssi_from_iwinfo(__uint8_t *client_addr); int get_rssi_iwinfo(__uint8_t *client_addr);
#endif //DAWN_RSSI_H #endif //DAWN_RSSI_H

View file

@ -14,6 +14,7 @@
#include "networksocket.h" #include "networksocket.h"
#include "ubus.h" #include "ubus.h"
#include "dawn_uci.h" #include "dawn_uci.h"
#include "rssi.h"
#define BUFSIZE 17 #define BUFSIZE 17
#define BUFSIZE_DIR 256 #define BUFSIZE_DIR 256

View file

@ -138,11 +138,13 @@ void kick_clients(uint8_t bssid[], uint32_t id) {
} }
// update rssi // update rssi
int rssi = get_rssi_from_iwinfo(client_array[j].client_addr); int rssi = get_rssi_iwinfo(client_array[j].client_addr);
if (rssi != INT_MIN) { if (rssi != INT_MIN) {
pthread_mutex_unlock(&probe_array_mutex); pthread_mutex_unlock(&probe_array_mutex);
if (!probe_array_update_rssi(client_array[j].bssid_addr, client_array[j].client_addr, rssi)) { if (!probe_array_update_rssi(client_array[j].bssid_addr, client_array[j].client_addr, rssi)) {
printf("Failed to update RSSI!\n"); printf("Failed to update RSSI!\n");
} else {
printf("RSSI UPDATED: RSSI: %d\n\n", rssi);
} }
pthread_mutex_lock(&probe_array_mutex); pthread_mutex_lock(&probe_array_mutex);

View file

@ -1,8 +1,11 @@
#include "rssi.h" #include "rssi.h"
#include <limits.h> #include <limits.h>
#include <iwinfo.h>
#include <dirent.h>
#include "utils.h" #include "utils.h"
#include "ubus.h"
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
@ -10,61 +13,60 @@ int call_iwinfo(char *client_addr);
int parse_rssi(char *iwinfo_string); int parse_rssi(char *iwinfo_string);
int get_rssi_from_iwinfo(__uint8_t *client_addr) { int get_rssi(const char *ifname, uint8_t *client_addr);
char mac_buf[20];
sprintf(mac_buf, MACSTR, MAC2STR(client_addr));
char mac_buf_conv[20];
convert_mac(mac_buf, mac_buf_conv); #define IWINFO_BUFSIZE 24 * 1024
return call_iwinfo(mac_buf_conv); int get_rssi_iwinfo(__uint8_t *client_addr) {
}
int call_iwinfo(char *client_addr) { DIR *dirp;
// TODO: REFACTOR THIS! USE NET LINK... LOOK AT IWINFO struct dirent *entry;
dirp = opendir(hostapd_dir_glob); // error handling?
FILE *fp; if (!dirp) {
char path[1035]; fprintf(stderr, "No hostapd sockets!\n");
return -1;
}
int rssi = INT_MIN; int rssi = INT_MIN;
int command_length = 68;
char iwinfo_command[command_length];
char *first_command = "(iwinfo wlan0 assoc && iwinfo wlan1 assoc) | grep ";
size_t length_first_command = strlen(first_command);
memcpy(iwinfo_command, first_command, length_first_command);
memcpy(iwinfo_command + length_first_command, client_addr, strlen(client_addr));
iwinfo_command[command_length - 1] = '\0';
printf("iwinfo command:\n%s\n", iwinfo_command);
fp = popen(iwinfo_command, "r"); while ((entry = readdir(dirp)) != NULL) {
if (fp == NULL) { if (entry->d_type == DT_SOCK) {
printf("Failed to run command\n"); rssi = get_rssi(entry->d_name, client_addr);
exit(1); if(rssi != INT_MIN)
break;
}
} }
closedir(dirp);
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path) - 1, fp) != NULL) {
rssi = parse_rssi(path);
}
/* close */
pclose(fp);
return rssi; return rssi;
} }
int parse_rssi(char *iwinfo_string) { int get_rssi(const char *ifname, uint8_t *client_addr){
char cut_1[] = " ";
char cut_2[] = "dBm"; int i, len;
char *p_1 = strstr(iwinfo_string, cut_1); char buf[IWINFO_BUFSIZE];
char *p_2 = strstr(iwinfo_string, cut_2); struct iwinfo_assoclist_entry *e;
int rssi = INT_MIN; const struct iwinfo_ops *iw;
if (p_1 != NULL && p_2 != NULL) {
int length = (int) (p_2 - p_1); iw = iwinfo_backend(ifname);
char dest[length + 1];
memcpy(dest, p_1, (int) (p_2 - p_1)); if (iw->assoclist(ifname, buf, &len))
dest[length] = '\0'; {
rssi = atoi(dest); printf("No information available\n");
return INT_MIN;
} }
return rssi; else if (len <= 0)
{
printf("No station connected\n");
return INT_MIN;
}
for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
{
e = (struct iwinfo_assoclist_entry *) &buf[i];
if(mac_is_equal(client_addr, e->mac))
return e->signal;
}
return INT_MIN;
} }