Commit 8e701664 authored by Anjan Chanda's avatar Anjan Chanda

support as standalone exec build too

parent 928d1566
......@@ -4,7 +4,18 @@ CFLAGS += -I. -I../../include -I../../utils -I../../core -O2 -Wall -g -pthread
PLUGIN = wfa_delm.so
OBJS = delm.o delm_utils.o delm_ubus.o delm_dumper.o delm_getter.o
ifeq ($(CONFIG_WFA_DELM_STANDALONE),)
CFLAGS += -DWFA_DELM_STANDALONE
OBJS += main.o ../../utils/timer.o
EXEC = wfa_delm
LIBS += -luci -lubus -lubox -ljson-c -lblobmsg_json \
-lnl-genl-3 -lnl-3 \
-lwifi-5 -leasy
all: $(EXEC)
else
all: $(PLUGIN)
endif
%.o: %.c
$(CC) $(CFLAGS) -fPIC -c $< -o $@
......@@ -13,5 +24,8 @@ $(PLUGIN): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -shared \
-Wl,-soname,$@ -o $@ $^
$(EXEC): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
clean:
rm -f *.o *.so*
......@@ -65,7 +65,7 @@ void refresh_timer_cb(atimer_t *t)
timer_set(t, p->refresh_int);
}
static int delm_exit(void *priv)
int delm_exit(void *priv)
{
struct delm_private *p = (struct delm_private *)priv;
......@@ -82,12 +82,18 @@ static int delm_exit(void *priv)
int delm_init(void **priv, void *cfg)
{
struct delm_private *p;
uint32_t collect_int;
int ret;
p = calloc(1, sizeof(struct delm_private));
if (!p)
return -1;
if (cfg) {
collect_int = *(uint32_t *)cfg;
if (collect_int > 0)
p->refresh_int = collect_int;
}
ret = delm_spawn_getter(p);
if (ret) {
......@@ -96,11 +102,10 @@ int delm_init(void **priv, void *cfg)
ret = delm_publish_object(p, DELM_OBJNAME);
fprintf(stderr, "%s: Init refresh timer...\n", __func__);
timer_init(&p->t, refresh_timer_cb);
p->refresh_int = 30000; /* TODO: collection time */
timer_set(&p->t, p->refresh_int);
if (p->refresh_int)
timer_set(&p->t, p->refresh_int);
*priv = p;
......
......@@ -165,8 +165,8 @@ int delm_dump_opclass_scanres(struct wifi_scanres_opclass_element *op,
int i;
fprintf(stderr, "%s:%d opclass = %d num_channels_scanned = %d\n",
__func__, __LINE__, op->opclass, op->num_channels_scanned);
/* fprintf(stderr, "%s:%d opclass = %d num_channels_scanned = %d\n",
__func__, __LINE__, op->opclass, op->num_channels_scanned); */
blobmsg_add_u32(bb, "OperatingClass", op->opclass);
blobmsg_add_u32(bb, "NumberOfChannelScans", op->num_channels_scanned);
......
......@@ -46,6 +46,12 @@ int delm_get_sta_all(const char *ifname, struct wifi_sta_element **stalist,
if (ret)
return -1;
if (nr == 0) {
*num_stas = 0;
*stalist = NULL;
return 0;
}
out = calloc(nr, sizeof(struct wifi_sta_element));
if (!out)
return -1;
......@@ -84,7 +90,9 @@ int delm_get_sta_all(const char *ifname, struct wifi_sta_element **stalist,
}
*num_stas = nr;
*stalist = out;
return 0;
}
......@@ -425,8 +433,6 @@ void *delm_getter_run(void *data)
struct delm_private *p = (struct delm_private *)data;
struct wifi_data_element *dm, *dm_old;
struct wifi_netdev ifs[WIFI_DEVICE_MAX_NUM];
//int num_radio = 0;
//int i, j;
int ret;
pthread_mutex_lock(&p->lock);
......@@ -445,7 +451,7 @@ void *delm_getter_run(void *data)
return NULL;
}
/* XXX: This getter for this device only. So, num_devices = 1.
/* This getter for this device only. So, num_devices = 1.
* For Multi-AP network, multiple getters will be there.
*/
dm = calloc(1, sizeof(struct wifi_data_element) +
......@@ -467,7 +473,7 @@ void *delm_getter_run(void *data)
p->getter_running = 0;
dm_old = p->dm;
p->dm = dm;
fprintf(stderr, "%s: Updating DM to 0x%p\n", __func__, p->dm);
fprintf(stderr, "%s: update DM to 0x%p\n", __func__, p->dm);
memset(p->ifs, 0, sizeof(ifs));
memcpy(p->ifs, ifs, sizeof(ifs));
......
/*
* main.c
* WiFi Data Elements main (for standalone build).
*
* Copyright (C) 2020 IOPSYS Software Solutions AB. All rights reserved.
*
* Author: anjan.chanda@iopsys.eu
*
* See LICENSE file for license related information.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <json-c/json.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>
#include "mapagent.h"
extern int delm_init(void **priv, void *cfg);
extern int delm_exit(void *priv);
struct wifi_netdev wifi_ifs[WIFI_DEVICE_MAX_NUM];
static void wifi_status_cb(struct ubus_request *req, int type,
struct blob_attr *msg)
{
struct wifi_netdev *ifs = (struct wifi_netdev *)req->priv;
struct json_object *radio_array;
struct json_object *json_msg;
int i, j, k, len;
char *json_str;
json_str = blobmsg_format_json(msg, true);
if (!json_str)
return;
json_msg = json_tokener_parse(json_str);
if (!json_msg)
goto out_str;
if (!json_object_is_type(json_msg, json_type_object))
goto out_json;
json_object_object_get_ex(json_msg, "radios", &radio_array);
len = json_object_array_length(radio_array);
if (len > WIFI_DEVICE_MAX_NUM)
len = WIFI_DEVICE_MAX_NUM;
for (i = 0; i < len; i++) {
struct json_object *radio_obj, *radio_obj_name;
struct json_object *fh_obj, *fh_obj_name;
struct json_object *bk_obj, *bk_obj_name;
struct json_object *fh_array;
struct json_object *bk_array;
int fh_len, bk_len;
const char *radio_name;
radio_obj = json_object_array_get_idx(radio_array, i);
json_object_object_get_ex(radio_obj, "name", &radio_obj_name);
radio_name = json_object_get_string(radio_obj_name);
strncpy(ifs[i].radio, radio_name, 15);
json_object_object_get_ex(radio_obj, "accesspoints", &fh_array);
fh_len = json_object_array_length(fh_array);
for (j = 0; j < fh_len; j++) {
const char *fh_name;
fh_obj = json_object_array_get_idx(fh_array, j);
json_object_object_get_ex(fh_obj, "ifname", &fh_obj_name);
fh_name = json_object_get_string(fh_obj_name);
strncpy(ifs[i].iface[j].name, fh_name, 15);
ifs[i].iface[j].mode = WIFI_IFACE_FH;
}
json_object_object_get_ex(radio_obj, "backhauls", &bk_array);
bk_len = json_object_array_length(bk_array);
for (k = 0; k < bk_len; k++) {
const char *bk_name;
bk_obj = json_object_array_get_idx(bk_array, k);
json_object_object_get_ex(bk_obj, "ifname", &bk_obj_name);
bk_name = json_object_get_string(bk_obj_name);
strncpy(ifs[i].iface[k + j].name, bk_name, 15);
ifs[i].iface[k + j].mode = WIFI_IFACE_BK;
}
}
out_json:
json_object_put(json_msg);
out_str:
free(json_str);
}
static int get_wifi_interfaces(struct ubus_context *ctx, struct wifi_netdev *ifs)
{
struct blob_buf bb = {};
uint32_t id;
int ret;
ret = ubus_lookup_id(ctx, "wifi", &id);
if (ret) {
fprintf(stderr, "wifi object lookup failed. %s\n",
ubus_strerror(ret));
return -1;
}
blob_buf_init(&bb, 0);
ret = ubus_invoke(ctx, id, "status", bb.head,
wifi_status_cb, ifs, 2 * 1000);
blob_buf_free(&bb);
return ret;
}
int agent_get_wifi_interfaces(struct wifi_netdev *ifs)
{
int i, j;
memcpy(ifs, wifi_ifs, sizeof(wifi_ifs));
fprintf(stderr, "===============================================\n");
for (i = 0; i < WIFI_DEVICE_MAX_NUM; i++) {
j = 0;
fprintf(stderr, "[%d] Radio = %s Iface = ", i, ifs[i].radio);
while (ifs[i].iface[j].name[0] != '\0') {
fprintf(stderr, "%s ", ifs[i].iface[j].name);
j++;
}
fprintf(stderr, "\n");
}
fprintf(stderr, "===============================================\n");
return 0;
}
static void usage()
{
fprintf(stderr, "Usage: wfa_delm [options]\n");
fprintf(stderr, "\n");
fprintf(stderr, "options:\n");
fprintf(stderr, " -h usage\n");
fprintf(stderr, " -t <seconds>, collection interval in seconds\n");
fprintf(stderr, "\n");
}
int main(int argc, char **argv)
{
struct ubus_context *ubus_ctx;
int collect_int = 0;
void *delm_handle;
int ret;
int ch;
while ((ch = getopt(argc, argv, "ht:")) != -1) {
switch (ch) {
case 'h':
usage();
exit(0);
case 't':
collect_int = atoi(optarg);
collect_int *= 1000; /* to msecs */
break;
default:
break;
}
}
argc -= optind;
argv += optind;
uloop_init();
ubus_ctx = ubus_connect(NULL);
if (!ubus_ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop(ubus_ctx);
ret = get_wifi_interfaces(ubus_ctx, wifi_ifs);
if (ret) {
fprintf(stderr, "wfa_delm: Failed to get wifi interfaces.\n");
goto out_exit;
}
ret = delm_init(&delm_handle, &collect_int);
if (ret) {
fprintf(stderr, "wfa_delm: Failed to start.\n");
return ret;
}
uloop_run();
out_exit:
ubus_free(ubus_ctx);
uloop_done();
delm_exit(delm_handle);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment