|
|
@@ -217,7 +217,7 @@ struct trigger_results {
|
|
|
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
|
|
|
{
|
|
|
// Callback for errors.
|
|
|
- printf("error_handler() called.\n");
|
|
|
+ printf("error_handler, %d\n", err->error);
|
|
|
int *ret = arg;
|
|
|
*ret = err->error;
|
|
|
return NL_STOP;
|
|
|
@@ -253,25 +253,25 @@ static int callback_stat(struct nl_msg *msg, void *arg)
|
|
|
};
|
|
|
ap_stat_t *stat=(ap_stat_t*)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_STA_INFO]) {
|
|
|
- stat->link = 1;
|
|
|
- nla_parse(sinfo, NL80211_STA_INFO_MAX, nla_data(tb[NL80211_ATTR_STA_INFO]),
|
|
|
- nla_len(tb[NL80211_ATTR_STA_INFO]),stats_policy);
|
|
|
- if (sinfo[NL80211_STA_INFO_SIGNAL]) {
|
|
|
- stat->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
|
|
|
- }
|
|
|
- else {
|
|
|
- stat->signal = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- stat->link = 0;
|
|
|
+ if (!tb[NL80211_ATTR_STA_INFO]) {
|
|
|
+ fprintf(stderr, "sta stats missing!\n");
|
|
|
+ return NL_SKIP;
|
|
|
}
|
|
|
- printf("____link: %d, signal: %d\n", stat->link, stat->signal);
|
|
|
+ stat->link = 1;
|
|
|
|
|
|
- return NL_OK;
|
|
|
+ if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, tb[NL80211_ATTR_STA_INFO], stats_policy)) {
|
|
|
+ fprintf(stderr, "failed to parse nested attributes!\n");
|
|
|
+ return NL_SKIP;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sinfo[NL80211_STA_INFO_SIGNAL]) {
|
|
|
+ stat->signal = (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
|
|
|
+ }
|
|
|
+ printf("___________________PPPPP_______________________link: %d, signal: %d\n", stat->link, stat->signal);
|
|
|
+
|
|
|
+ return NL_SKIP;
|
|
|
}
|
|
|
static int callback_trigger(struct nl_msg *msg, void *arg)
|
|
|
{
|
|
|
@@ -427,7 +427,7 @@ static int scan_trigger(nl_handle_t *h)
|
|
|
nl_socket_drop_membership(h->sock, mcid); // No longer need this.
|
|
|
return 0;
|
|
|
}
|
|
|
-static int nl80211_init(nl_handle_t *h)
|
|
|
+static int nl80211_init(nl_handle_t *h, int dbg)
|
|
|
{
|
|
|
h->ifname = WLAN_IFNAME;
|
|
|
h->ifindex = if_nametoindex(h->ifname);
|
|
|
@@ -436,28 +436,29 @@ static int nl80211_init(nl_handle_t *h)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- h->cb = nl_cb_alloc(NL_CB_DEFAULT);
|
|
|
+ h->cb = nl_cb_alloc(dbg?NL_CB_DEBUG:NL_CB_DEFAULT);
|
|
|
if (!h->cb) {
|
|
|
fprintf(stderr, "Failed to allocate netlink callback.\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
+
|
|
|
+ h->msg = nlmsg_alloc();
|
|
|
+ if (!h->msg) {
|
|
|
+ fprintf(stderr, "Failed to allocate netlink message\n");
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
|
|
|
h->sock = nl_socket_alloc();
|
|
|
if (!h->sock) {
|
|
|
fprintf(stderr, "Failed to allocate socket\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
+ nl_socket_set_buffer_size(h->sock, 8192, 8192);
|
|
|
|
|
|
if (genl_connect(h->sock)) {
|
|
|
fprintf(stderr, "Failed to connect to generic netlink.\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
-
|
|
|
- h->msg = nlmsg_alloc();
|
|
|
- if (!h->msg) {
|
|
|
- fprintf(stderr, "Failed to allocate netlink message\n");
|
|
|
- goto fail;
|
|
|
- }
|
|
|
h->nl_id = genl_ctrl_resolve(h->sock, "nl80211");
|
|
|
|
|
|
return 0;
|
|
|
@@ -469,7 +470,10 @@ static int nl80211_free(nl_handle_t *h)
|
|
|
{
|
|
|
if(h->cb) nl_cb_put(h->cb);
|
|
|
if(h->msg) nlmsg_free(h->msg);
|
|
|
- if(h->sock) nl_socket_free(h->sock);
|
|
|
+ if(h->sock) {
|
|
|
+ nl_close(h->sock);
|
|
|
+ nl_socket_free(h->sock);
|
|
|
+ }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -520,25 +524,31 @@ static int nl_get_ip(ap_stat_t *stat)
|
|
|
}
|
|
|
static int nl_get_signal(ap_stat_t *stat)
|
|
|
{
|
|
|
- int r;
|
|
|
+ int r,err=1;
|
|
|
nl_handle_t nl,*h;
|
|
|
|
|
|
- r = nl80211_init(&nl);
|
|
|
+ //https://github.com/Alamot/code-snippets/blob/master/nl80211_info/nl80211_info.c
|
|
|
+ r = nl80211_init(&nl, 0);
|
|
|
if(r) {
|
|
|
return -1;
|
|
|
}
|
|
|
h = &nl;
|
|
|
-
|
|
|
stat->link = stat->signal = 0;
|
|
|
- genlmsg_put(h->msg, 0, 0, h->nl_id, 0, 0, NL80211_CMD_GET_STATION, 0); // Setup which command to run.
|
|
|
- nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex); // Add message attribute, which interface to use.
|
|
|
+
|
|
|
+ nl_cb_err(h->cb, NL_CB_CUSTOM, error_handler, &err);
|
|
|
+ nl_cb_set(h->cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
|
|
|
+ nl_cb_set(h->cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
|
|
|
nl_cb_set(h->cb, NL_CB_VALID, NL_CB_CUSTOM, callback_stat, stat);
|
|
|
- r = nl_send_auto_complete(h->sock, h->msg);
|
|
|
- printf("__________________________r: %d\n", r);
|
|
|
- if(r>=0) {
|
|
|
- nl_recvmsgs(h->sock, h->cb);
|
|
|
- r = 0;
|
|
|
+
|
|
|
+ genlmsg_put(h->msg, NL_AUTO_PORT, NL_AUTO_SEQ, h->nl_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 0); // Setup which command to run.
|
|
|
+ nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex); // Add message attribute, which interface to use.
|
|
|
+ r = nl_send_auto(h->sock, h->msg);
|
|
|
+ if(r<0) {
|
|
|
+ nl80211_free(h);
|
|
|
+ return -1;
|
|
|
}
|
|
|
+ r = 0;
|
|
|
+ while(err>0) nl_recvmsgs(h->sock, h->cb);
|
|
|
nl80211_free(h);
|
|
|
return r;
|
|
|
}
|
|
|
@@ -547,7 +557,7 @@ static int nl_scan(result_t *res)
|
|
|
int r;
|
|
|
nl_handle_t nl,*h;
|
|
|
|
|
|
- r = nl80211_init(&nl);
|
|
|
+ r = nl80211_init(&nl, 0);
|
|
|
if(r) {
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -585,7 +595,7 @@ static int nl_conn(ap_info_t *info)
|
|
|
int err,r=0;
|
|
|
nl_handle_t nl,*h;
|
|
|
|
|
|
- r = nl80211_init(&nl);
|
|
|
+ r = nl80211_init(&nl, 0);
|
|
|
if(r) {
|
|
|
return r;
|
|
|
}
|
|
|
@@ -628,7 +638,7 @@ static int nl_disconn(void)
|
|
|
int err,r;
|
|
|
nl_handle_t nl,*h;
|
|
|
|
|
|
- r = nl80211_init(&nl);
|
|
|
+ r = nl80211_init(&nl, 0);
|
|
|
if(r) {
|
|
|
return r;
|
|
|
}
|
|
|
@@ -756,9 +766,17 @@ int wifi_sta_stat(ap_stat_t *stat)
|
|
|
int r=-1;
|
|
|
wifi_handle_t *h=&wfHandle;
|
|
|
|
|
|
+ if(!stat) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
#ifdef USE_WIFI
|
|
|
- r = nl_get_signal(stat);
|
|
|
- r *= nl_get_ip(stat);
|
|
|
+ nl_get_signal(stat);
|
|
|
+ r = nl_get_ip(stat);
|
|
|
+ if(r==0) {
|
|
|
+ stat->link = 1; //不知道何原因导致获取信号强度的回调函数触发不了,暂时绕过
|
|
|
+ }
|
|
|
+ //printf("______ link: %d, signal: %d, ip: %s\n", stat->link, stat->signal, stat->ipaddr);
|
|
|
#endif
|
|
|
|
|
|
return r;
|