From 196e8f7c4e470b15e0eb89bc33dae1c905d8913a Mon Sep 17 00:00:00 2001 From: Jan-Philipp Litza Date: Sun, 4 Dec 2016 18:30:58 +0100 Subject: [PATCH] gluon-airtime: Improve code style, remove offset/current construction --- package/gluon-airtime/src/airtime-test.c | 18 ++++------- package/gluon-airtime/src/airtime.c | 41 ++++++------------------ package/gluon-airtime/src/airtime.h | 13 +++----- package/gluon-airtime/src/respondd.c | 36 ++++++++------------- 4 files changed, 34 insertions(+), 74 deletions(-) diff --git a/package/gluon-airtime/src/airtime-test.c b/package/gluon-airtime/src/airtime-test.c index 088a9c25..85773f9e 100644 --- a/package/gluon-airtime/src/airtime-test.c +++ b/package/gluon-airtime/src/airtime-test.c @@ -1,5 +1,4 @@ #include -#include /* sleep */ #include "airtime.h" void print_result(struct airtime_result *); @@ -12,21 +11,18 @@ int main(int argc, char *argv[]) { return 1; } - while (1) { - a = get_airtime(argv[1], argv[2]); - print_result(&a->radio0); - print_result(&a->radio1); - sleep(1); - } + a = get_airtime(argv[1], argv[2]); + print_result(&a->radio0); + print_result(&a->radio1); } void print_result(struct airtime_result *result){ printf("freq=%d\tnoise=%d\tbusy=%lld\tactive=%lld\trx=%lld\ttx=%lld\n", result->frequency, result->noise, - result->busy_time.current, - result->active_time.current, - result->rx_time.current, - result->tx_time.current + result->busy_time, + result->active_time, + result->rx_time, + result->tx_time ); } diff --git a/package/gluon-airtime/src/airtime.c b/package/gluon-airtime/src/airtime.c index 7d3fbed9..108d6d56 100644 --- a/package/gluon-airtime/src/airtime.c +++ b/package/gluon-airtime/src/airtime.c @@ -64,8 +64,7 @@ static struct airtime cur_airtime = { * @__NL80211_SURVEY_INFO_AFTER_LAST: internal use */ -static int survey_airtime_handler(struct nl_msg *msg, void *arg) -{ +static int survey_airtime_handler(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { @@ -76,7 +75,7 @@ static int survey_airtime_handler(struct nl_msg *msg, void *arg) struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct airtime_result *result = (struct airtime_result *) arg; - nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL); + nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_SURVEY_INFO]) { fprintf(stderr, "survey data missing!\n"); @@ -93,27 +92,12 @@ static int survey_airtime_handler(struct nl_msg *msg, void *arg) goto abort; } - uint64_t frequency = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); - - if (frequency != result->frequency) { - // channel changed, restart at zero - result->frequency = frequency; - result->active_time.offset = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]); - result->busy_time.offset = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]); - result->rx_time.offset = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]); - result->tx_time.offset = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]); - result->active_time.current = 0; - result->busy_time.current = 0; - result->rx_time.current = 0; - result->tx_time.current = 0; - } else { - result->active_time.current = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) - result->active_time.offset; - result->busy_time.current = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) - result->busy_time.offset; - result->rx_time.current = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) - result->rx_time.offset; - result->tx_time.current = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) - result->tx_time.offset; - } - - result->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); + result->frequency = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); + result->active_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]); + result->busy_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]); + result->rx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]); + result->tx_time = nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]); + result->noise = nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); abort: return NL_SKIP; @@ -121,10 +105,9 @@ abort: static int get_airtime_for_interface(struct airtime_result *result, const char *interface) { int error = 0; - int ctrl, ifx, flags; + int ctrl, ifx; struct nl_sock *sk = NULL; struct nl_msg *msg = NULL; - enum nl80211_commands cmd; #define CHECK(x) { if (!(x)) { fprintf(stderr, "airtime.c: error on line %d\n", __LINE__); error = 1; goto out; } } @@ -141,12 +124,8 @@ static int get_airtime_for_interface(struct airtime_result *result, const char * goto out; } - cmd = NL80211_CMD_GET_SURVEY; - flags = 0; - flags |= NLM_F_DUMP; - /* TODO: check return? */ - genlmsg_put(msg, 0, 0, ctrl, 0, flags, cmd, 0); + genlmsg_put(msg, 0, 0, ctrl, 0, NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifx); diff --git a/package/gluon-airtime/src/airtime.h b/package/gluon-airtime/src/airtime.h index 4a1347c1..c0c8bb93 100644 --- a/package/gluon-airtime/src/airtime.h +++ b/package/gluon-airtime/src/airtime.h @@ -2,18 +2,13 @@ #include -struct airtime_time { - uint64_t current; - uint64_t offset; -}; - struct airtime_result { + uint64_t active_time; + uint64_t busy_time; + uint64_t rx_time; + uint64_t tx_time; uint32_t frequency; uint8_t noise; - struct airtime_time active_time; - struct airtime_time busy_time; - struct airtime_time rx_time; - struct airtime_time tx_time; }; struct airtime { diff --git a/package/gluon-airtime/src/respondd.c b/package/gluon-airtime/src/respondd.c index 0f62afd9..cc4c0497 100644 --- a/package/gluon-airtime/src/respondd.c +++ b/package/gluon-airtime/src/respondd.c @@ -8,36 +8,26 @@ static const char const *wifi_0_dev = "client0"; static const char const *wifi_1_dev = "client1"; -void fill_airtime_json(struct airtime_result *air, struct json_object* wireless){ - struct json_object *result = NULL, *obj = NULL; +void fill_airtime_json(struct airtime_result *air, struct json_object *wireless) { + struct json_object *obj = NULL; obj = json_object_new_object(); if(!obj) - goto error; -#define JSON_ADD_INT64(value,key) {result = json_object_new_int64(value); json_object_object_add(obj,key,result);} - result = json_object_new_int(air->frequency); - if(!result) - goto error; - json_object_object_add(obj,"frequency",result); + return; - JSON_ADD_INT64(air->active_time.current,"active") - JSON_ADD_INT64(air->busy_time.current,"busy") - JSON_ADD_INT64(air->rx_time.current,"rx") - JSON_ADD_INT64(air->tx_time.current,"tx") + json_object_object_add(obj, "frequency", json_object_new_int(air->frequency)); + json_object_object_add(obj, "active", json_object_new_int64(air->active_time)); + json_object_object_add(obj, "busy", json_object_new_int64(air->busy_time)); + json_object_object_add(obj, "rx", json_object_new_int64(air->rx_time)); + json_object_object_add(obj, "tx", json_object_new_int64(air->tx_time)); + json_object_object_add(obj, "noise", json_object_new_int(air->noise)); - result = json_object_new_int(air->noise); - json_object_object_add(obj,"noise",result); - -error: - if(air->frequency >= 2400 && air->frequency < 2500) - json_object_object_add(wireless, "airtime24", obj); - else if (air->frequency >= 5000 && air->frequency < 6000) - json_object_object_add(wireless, "airtime5", obj); + json_object_array_add(wireless, obj); } static struct json_object *respondd_provider_statistics(void) { struct airtime *airtime = NULL; - struct json_object *result = NULL, *wireless = NULL; + struct json_object *result, *wireless; airtime = get_airtime(wifi_0_dev, wifi_1_dev); if (!airtime) @@ -47,8 +37,8 @@ static struct json_object *respondd_provider_statistics(void) { if (!result) return NULL; - wireless = json_object_new_object(); - if (!wireless){ + wireless = json_object_new_array(); + if (!wireless) { json_object_put(result); return NULL; }