Prase rssi from iwinfo

This commit is contained in:
PolynomialDivision 2017-10-19 15:37:09 +02:00
parent de516c68d3
commit 6ac15a5bac
8 changed files with 216 additions and 5 deletions

78
src/utils/rssi.c Normal file
View file

@ -0,0 +1,78 @@
#include "rssi.h"
#include <limits.h>
#include "utils.h"
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
int call_iwinfo(char* client_addr);
int parse_rssi(char* iwinfo_string);
int get_rssi_from_iwinfo(__uint8_t* client_addr)
{
char mac_buf[20];
sprintf(mac_buf, "%x:%x:%x:%x:%x:%x", MAC2STR(client_addr));
char mac_buf_conv[20];
convert_mac(mac_buf, mac_buf_conv);
return call_iwinfo(mac_buf_conv);
}
int call_iwinfo(char* client_addr)
{
FILE *fp;
char path[1035];
/* Open the command for reading. */
// TODO: refactor this
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");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* 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;
}
int parse_rssi(char* iwinfo_string)
{
char cut_1[] = " ";
char cut_2[] = "dBm";
char *p_1 = strstr(iwinfo_string, cut_1);
char *p_2 = strstr(iwinfo_string, cut_2);
int rssi = INT_MIN;
if(p_1 != NULL && p_2 != NULL)
{
printf("Found substring: %s", p_2);
printf("Length: %d\n", p_2 - p_1);
int length = (int) (p_2 - p_1);
char dest[length + 1];
memcpy(dest, p_1, (int) (p_2 - p_1));
dest[length] = '\0';
rssi = atoi(dest);
printf("After cutting:\n%s\nInt:%d\n", dest, rssi);
}
return rssi;
}

View file

@ -611,7 +611,6 @@ static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr *
printf("UMDNS:\n%s", str);
}
int ubus_call_umdns()
{
u_int32_t id;
@ -622,5 +621,30 @@ int ubus_call_umdns()
int timeout = 1;
ubus_invoke(ctx_clients, id, "browse", NULL, ubus_umdns_cb, NULL, timeout * 1000);
return 0;
}
int ubus_send_probe_via_network(struct probe_entry_s probe_entry)
{
printf("Try to send new probe!!!\n");
static struct blob_buf b;
blob_buf_init(&b, 0);
blobmsg_add_macaddr(&b, "bssid", probe_entry.bssid_addr);
blobmsg_add_macaddr(&b, "address", probe_entry.client_addr);
blobmsg_add_macaddr(&b, "target", probe_entry.target_addr);
blobmsg_add_u32(&b, "signal", probe_entry.signal);
blobmsg_add_u32(&b, "freq", probe_entry.freq);
blobmsg_add_u8(&b, "ht_support", probe_entry.ht_support);
blobmsg_add_u8(&b, "vht_support", probe_entry.vht_support);
// send probe via network
char *str;
str = blobmsg_format_json(b.head, 1);
send_string_enc(str);
printf("SENDING NEW PROBE!!!: %s\n", str);
return 0;
}

View file

@ -23,4 +23,24 @@ int hwaddr_aton(const char *txt, uint8_t *addr) {
}
return 0;
}
}
int convert_mac(char* in, char* out) {
int i,j = 0;
for (i = 0; i < 6; i++) {
if(in[j+1] != ':' && in[j+1] != '\0') {
out[3 * i] = toupper(in[j]);
out[(3 * i) + 1] = toupper(in[j + 1]);
out[(3 * i) + 2] = in[j + 2];
j+= 3;
} else {
out[3 * i] = '0';
out[(3 * i) + 1] = toupper(in[j]);
out[(3 * i) + 2] = toupper(in[j+1]);
j += 2;
}
}
return 0;
}