From d955a8f3995900529f38e3496527ef028024714a Mon Sep 17 00:00:00 2001
From: Filip Matusiak <filip.matusiak@iopsys.eu>
Date: Wed, 4 Jan 2023 14:31:57 +0100
Subject: [PATCH] Add cntlr_ubus_dbg

Signed-off-by: Filip Matusiak <filip.matusiak@iopsys.eu>
---
 src/Makefile         |   1 +
 src/cntlr.c          |   3 +
 src/cntlr.h          |   1 +
 src/cntlr_ubus_dbg.c | 129 +++++++++++++++++++++++++++++++++++++++++++
 src/cntlr_ubus_dbg.h |  14 +++++
 5 files changed, 148 insertions(+)
 create mode 100644 src/cntlr_ubus_dbg.c
 create mode 100644 src/cntlr_ubus_dbg.h

diff --git a/src/Makefile b/src/Makefile
index bfc4c3a1..4a8e01d7 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -11,6 +11,7 @@ OBJS = \
 	wifi_opclass.o \
 	allsta.o \
 	cntlr_ubus.o \
+	cntlr_ubus_dbg.o \
 	cntlr.o \
 	cntlr_map.o \
 	cntlr_tlv.o \
diff --git a/src/cntlr.c b/src/cntlr.c
index 53e1c75a..6b2bd605 100644
--- a/src/cntlr.c
+++ b/src/cntlr.c
@@ -36,6 +36,7 @@
 #include "cntlr.h"
 #include "allsta.h"
 #include "cntlr_ubus.h"
+#include "cntlr_ubus_dbg.h"
 #include "cntlr_map.h"
 #include "cntlr_cmdu.h"
 #include "steer_module.h"
@@ -1670,6 +1671,7 @@ static void cntlr_start(atimer_t *t)
 	if (c->state == CNTLR_INIT) {
 		c->state = CNTLR_START;
 		cntlr_publish_object(c, "map.controller");
+		cntlr_publish_dbg_object(c, "map.controller.dbg");
 	}
 }
 
@@ -1993,6 +1995,7 @@ out_exit:
 	cntlr_clean_nodelist(c);
 	ubus_unregister_event_handler(ctx, &c->evh);
 	cntlr_remove_object(c);
+	cntlr_remove_dbg_object(c);
 	cmdu_ackq_free(&c->cmdu_ack_q);
 	cntlr_config_clean(&c->cfg);
 	uloop_done();
diff --git a/src/cntlr.h b/src/cntlr.h
index d9e62293..21922a32 100644
--- a/src/cntlr.h
+++ b/src/cntlr.h
@@ -283,6 +283,7 @@ struct controller {
 	unsigned char almac[6];
 	void *comm;
 	struct ubus_object obj;
+	struct ubus_object obj_dbg; /* ubus debug object */
 	struct ubus_context *ubus_ctx;
 	struct ubus_event_handler evh;
 	atimer_t heartbeat;
diff --git a/src/cntlr_ubus_dbg.c b/src/cntlr_ubus_dbg.c
new file mode 100644
index 00000000..38f944e1
--- /dev/null
+++ b/src/cntlr_ubus_dbg.c
@@ -0,0 +1,129 @@
+/*
+ * cntlr_ubus_dbg.c - for testing purpose only
+ *
+ * Copyright (C) 2022 IOPSYS Software Solutions AB. All rights reserved.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <arpa/inet.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 <uci.h>
+
+#include <easy/easy.h>
+#include <cmdu.h>
+#include <i1905_wsc.h>
+#include <1905_tlvs.h>
+#include <easymesh.h>
+#include <map_module.h>
+
+#include <wifidefs.h>
+#include "wifi_dataelements.h"
+
+#include "timer.h"
+#include "utils/utils.h"
+#include "utils/debug.h"
+#include "config.h"
+#include "cntlr.h"
+#include "allsta.h"
+#include "cntlr_map.h"
+#include "cntlr_ubus.h"
+#include "cntlr_tlv.h"
+
+#include "cntlr_tlv.h"
+#include "cntlr_cmdu.h"
+
+#include "cntlr_acs.h"
+
+#define OBJECT_INVALID	((uint32_t)-1)
+
+#ifndef MAP_CNTLR_DISABLE_UBUS_DBG
+
+static int cntlr_debug(struct ubus_context *ctx, struct ubus_object *obj,
+			struct ubus_request_data *req, const char *method,
+			struct blob_attr *msg)
+{
+	/* does nothing */
+	return 0;
+}
+
+int cntlr_publish_dbg_object(struct controller *c, const char *objname)
+{
+	struct ubus_object *obj;
+	struct ubus_object_type *obj_type;
+	struct ubus_method *obj_methods;
+	struct ubus_method m[] = {
+		UBUS_METHOD_NOARG("debug", cntlr_debug),
+	};
+	int num_methods = ARRAY_SIZE(m);
+	int ret;
+
+	obj = &c->obj_dbg;
+	memset(obj, 0, sizeof(*obj));
+
+	obj_type = calloc(1, sizeof(struct ubus_object_type));
+	if (!obj_type)
+		return -1;
+
+	obj_methods = calloc(num_methods, sizeof(struct ubus_method));
+	if (!obj_methods) {
+		free(obj_type);
+		return -1;
+	}
+
+	obj->name = objname;
+	memcpy(obj_methods, m, num_methods * sizeof(struct ubus_method));
+	obj->methods = obj_methods;
+	obj->n_methods = num_methods;
+
+	obj_type->name = obj->name;
+	obj_type->n_methods = obj->n_methods;
+	obj_type->methods = obj->methods;
+	obj->type = obj_type;
+
+	ret = ubus_add_object(c->ubus_ctx, obj);
+	if (ret) {
+		err("Failed to add '%s' err = %s\n",
+				objname, ubus_strerror(ret));
+		free(obj_methods);
+		free(obj_type);
+		return ret;
+	}
+
+	info("Published '%s' object\n", objname);
+
+	return 0;
+}
+
+void cntlr_remove_dbg_object(struct controller *c)
+{
+	if (c->ubus_ctx && c->obj_dbg.id != OBJECT_INVALID) {
+		ubus_remove_object(c->ubus_ctx, &c->obj_dbg);
+		free(c->obj_dbg.type);
+		free((void *) c->obj_dbg.methods);
+	}
+}
+#else
+int cntlr_publish_dbg_object(struct controller *c, const char *objname)
+{
+	return 0;
+}
+
+void cntlr_remove_object(struct controller *c)
+{
+	return;
+}
+#endif /* MAP_CNTLR_DISABLE_UBUS_DBG */
diff --git a/src/cntlr_ubus_dbg.h b/src/cntlr_ubus_dbg.h
new file mode 100644
index 00000000..f2d72ac8
--- /dev/null
+++ b/src/cntlr_ubus_dbg.h
@@ -0,0 +1,14 @@
+/*
+ * cntlr_ubus_dbg.h - for testing purpose only
+ *
+ * Copyright (C) 2022 IOPSYS Software Solutions AB. All rights reserved.
+ *
+ */
+
+#ifndef CNTLR_UBUS_DBG_H
+#define CNTLR_UBUS_DBG_H
+
+int cntlr_publish_dbg_object(struct controller *c, const char *objname);
+void cntlr_remove_dbg_object(struct controller *c);
+
+#endif /* CNTLR_UBUS_DBG_H */
-- 
GitLab