From 34b7163a759038edbbc0304be36abad05b9bc9b4 Mon Sep 17 00:00:00 2001
From: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
Date: Fri, 21 Feb 2025 10:10:45 +0000
Subject: [PATCH] Align with bbfdm new design

---
 src/Makefile         | 11 +++--------
 src/interfacestack.c | 14 ++++----------
 src/ip.c             | 41 ++++++++++++++++++-----------------------
 src/net_plugin.c     |  4 ++++
 4 files changed, 29 insertions(+), 41 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index a64c266..e005826 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,8 +1,6 @@
 LIBNETMNGR = libnetmngr.so
-LIBIFACESTACK = libinterface_stack.so
 
-LIBNETMNGROBJS = net_plugin.o common.o
-LIBIFACESTACKOBJS := interfacestack.o
+LIBNETMNGROBJS = net_plugin.o common.o interfacestack.o
 
 LIB_CFLAGS = $(CFLAGS) -Wall -Werror -fstrict-aliasing -g
 LIB_LDFLAGS = $(LDFLAGS)
@@ -47,14 +45,11 @@ endif
 %.o: %.c
 	$(CC) $(LIB_CFLAGS) $(FPIC) -c -o $@ $<
 
-all: $(LIBNETMNGR) $(LIBIFACESTACK)
+all: $(LIBNETMNGR)
 
 $(LIBNETMNGR): $(LIBNETMNGROBJS)
 	$(CC) $(LIB_CFLAGS) $(LIB_LDFLAGS) -shared -o $@ $^
 
-$(LIBIFACESTACK): $(LIBIFACESTACKOBJS)
-	$(CC) $(LIB_CFLAGS) $(LIB_LDFLAGS) -shared -o $@ $^
-
 clean:
-	rm -f *.o $(LIBNETMNGR) $(LIBIFACESTACK)
+	rm -f *.o $(LIBNETMNGR)
 
diff --git a/src/interfacestack.c b/src/interfacestack.c
index d0d238c..3f27838 100644
--- a/src/interfacestack.c
+++ b/src/interfacestack.c
@@ -127,8 +127,8 @@ static int create_interface_stack_instance(struct dmctx *dmctx, DMNODE *parent_n
 	dmuci_get_value_by_section_string(s, inst_alias, &data->HigherAlias);
 	dmuci_get_value_by_section_string(s, "LowerLayers", &LowerLayer);
 
-	data->LowerLayer = get_value_by_reference(dmctx, LowerLayer);
-	data->LowerAlias = get_lower_alias_value(data->LowerLayer);
+	data->LowerLayer = LowerLayer; //get_value_by_reference(dmctx, LowerLayer);
+	data->LowerAlias = get_lower_alias_value(data->LowerLayer); //TODO
 
 	inst = handle_instance_without_section(dmctx, parent_node, ++(*curr_inst));
 
@@ -324,12 +324,6 @@ static int get_InterfaceStack_LowerAlias(char *refparam, struct dmctx *ctx, void
 /**********************************************************************************************************************************
 *                                            OBJ & PARAM DEFINITION
 ***********************************************************************************************************************************/
-DM_MAP_OBJ tDynamicObj[] = {
-/* parentobj, nextobject, parameter */
-{"Device.", tDeviceInterfaceStackObj, tDeviceInterfaceStackParams},
-{0}
-};
-
 /* *** Device. *** */
 DMOBJ tDeviceInterfaceStackObj[] = {
 /* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys, version*/
@@ -347,8 +341,8 @@ DMLEAF tDeviceInterfaceStackParams[] = {
 DMLEAF tInterfaceStackParams[] = {
 /* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/
 {"Alias", &DMWRITE, DMT_STRING, get_InterfaceStack_Alias, set_InterfaceStack_Alias, BBFDM_BOTH, DM_FLAG_UNIQUE},
-{"HigherLayer", &DMREAD, DMT_STRING, get_InterfaceStack_HigherLayer, NULL, BBFDM_BOTH},
-{"LowerLayer", &DMREAD, DMT_STRING, get_InterfaceStack_LowerLayer, NULL, BBFDM_BOTH},
+{"HigherLayer", &DMREAD, DMT_STRING, get_InterfaceStack_HigherLayer, NULL, BBFDM_BOTH, DM_FLAG_REFERENCE},
+{"LowerLayer", &DMREAD, DMT_STRING, get_InterfaceStack_LowerLayer, NULL, BBFDM_BOTH, DM_FLAG_REFERENCE},
 {"HigherAlias", &DMREAD, DMT_STRING, get_InterfaceStack_HigherAlias, NULL, BBFDM_BOTH},
 {"LowerAlias", &DMREAD, DMT_STRING, get_InterfaceStack_LowerAlias, NULL, BBFDM_BOTH},
 {0}
diff --git a/src/ip.c b/src/ip.c
index d7051b0..10e0d43 100644
--- a/src/ip.c
+++ b/src/ip.c
@@ -1318,34 +1318,29 @@ static int get_IPInterface_LastChange(char *refparam, struct dmctx *ctx, void *d
 
 static int get_IPInterface_LowerLayers(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
 {
-	dmuci_get_value_by_section_string(((struct dm_data *)data)->dmmap_section, "LowerLayers", value);
+	char buf[1024] = {0};
 
-	if ((*value)[0] == '\0') {
-		char buf[1024] = {0};
-
-		char *device = get_device(section_name(((struct dm_data *)data)->config_section));
-		if (DM_STRLEN(device) == 0) {
-			dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "device", &device);
-			if (DM_STRLEN(device) == 0)
-				return 0;
-		}
-
-		bbfdm_get_references(ctx, MATCH_FIRST, "Device.PPP.Interface.", "Name", device, buf, sizeof(buf));
-		bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet."BBF_VENDOR_PREFIX"MACVLAN.", "Name", device, buf, sizeof(buf));
-		bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet.VLANTermination.", "Name", device, buf, sizeof(buf));
-		bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet.Link.", "Name", device, buf, sizeof(buf));
-
-		if ((DM_STRLEN(device) > 5) && DM_LSTRNCMP(device, "gre", 3) == 0) {
-			// gre device name is of the form gre4-<iface> or gre6-<iface>
-			bbfdm_get_references(ctx, MATCH_FIRST, "Device.GRE.Tunnel.*.Interface.", "Name", device + 5, buf, sizeof(buf));
-		}
+	char *device = get_device(section_name(((struct dm_data *)data)->config_section));
+	if (DM_STRLEN(device) == 0) {
+		dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "device", &device);
+		if (DM_STRLEN(device) == 0)
+			return 0;
+	}
 
-		// Store LowerLayers value
-		dmuci_set_value_by_section(((struct dm_data *)data)->dmmap_section, "LowerLayers", buf);
+	bbfdm_get_references(ctx, MATCH_FIRST, "Device.PPP.Interface.", "Name", device, buf, sizeof(buf));
+	bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet."BBF_VENDOR_PREFIX"MACVLAN.", "Name", device, buf, sizeof(buf));
+	bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet.VLANTermination.", "Name", device, buf, sizeof(buf));
+	bbfdm_get_references(ctx, MATCH_FIRST, "Device.Ethernet.Link.", "Name", device, buf, sizeof(buf));
 
-		*value = dmstrdup(buf);
+	if ((DM_STRLEN(device) > 5) && DM_LSTRNCMP(device, "gre", 3) == 0) {
+		// gre device name is of the form gre4-<iface> or gre6-<iface>
+		bbfdm_get_references(ctx, MATCH_FIRST, "Device.GRE.Tunnel.*.Interface.", "Name", device + 5, buf, sizeof(buf));
 	}
 
+	// Store LowerLayers value
+	dmuci_set_value_by_section(((struct dm_data *)data)->dmmap_section, "LowerLayers", buf);
+
+	*value = dmstrdup(buf);
 	return 0;
 }
 
diff --git a/src/net_plugin.c b/src/net_plugin.c
index 0584f43..1690ebf 100644
--- a/src/net_plugin.c
+++ b/src/net_plugin.c
@@ -34,6 +34,8 @@
 #include "ipv6rd.h"
 #endif
 
+#include "interfacestack.h" //TODO
+
 DM_MAP_OBJ tDynamicObj[] = {
 /* parentobj, nextobject, parameter */
 
@@ -61,5 +63,7 @@ DM_MAP_OBJ tDynamicObj[] = {
 {"Device.", tDeviceIPv6rdObj, NULL},
 #endif
 
+{"Device.", tDeviceInterfaceStackObj, tDeviceInterfaceStackParams},
+
 {0}
 };
-- 
GitLab