[Refactor ubus to enable lua5.3 support]

[Description]
Refactor ubus to enable lua5.3 support

[Release-log]
N/A

diff --git a/recipes-connectivity/ubus/files/0001-lua5.3-support.patch b/recipes-connectivity/ubus/files/0001-lua5.3-support.patch
new file mode 100644
index 0000000..c003ef5
--- /dev/null
+++ b/recipes-connectivity/ubus/files/0001-lua5.3-support.patch
@@ -0,0 +1,96 @@
+From 24b5aae13f1fe1ae90eab5fbd8e62df72681d065 Mon Sep 17 00:00:00 2001
+From: sah4009 <peter.deherdt@softathome.com>
+Date: Mon, 16 Sep 2019 09:22:16 +0000
+Subject: [PATCH] Lua 5.3 support
+
+---
+ lua/CMakeLists.txt |  3 ++-
+ lua/ubus.c         | 23 +++++++++++++++++++----
+ 2 files changed, 21 insertions(+), 5 deletions(-)
+
+diff --git a/lua/CMakeLists.txt b/lua/CMakeLists.txt
+index e4821bf..5dae67a 100644
+--- a/lua/CMakeLists.txt
++++ b/lua/CMakeLists.txt
+@@ -8,7 +8,7 @@ IF(NOT LUA_CFLAGS)
+ 	FIND_PROGRAM(PKG_CONFIG pkg-config)
+ 	IF(PKG_CONFIG)
+ 		EXECUTE_PROCESS(
+-			COMMAND pkg-config --silence-errors --cflags lua5.1
++			COMMAND pkg-config --silence-errors --cflags lua5.3
+ 			OUTPUT_VARIABLE LUA_CFLAGS
+ 			OUTPUT_STRIP_TRAILING_WHITESPACE
+ 		)
+@@ -45,6 +45,7 @@ IF(BUILD_LUA)
+ 		PREFIX ""
+ 	)
+ 	TARGET_LINK_LIBRARIES(ubus_lua ubus)
++	TARGET_LINK_LIBRARIES(ubus_lua -llua)
+ 
+ 	INSTALL(TARGETS ubus_lua
+ 		LIBRARY DESTINATION ${LUAPATH}
+diff --git a/lua/ubus.c b/lua/ubus.c
+index 86dcc50..6d7ba00 100644
+--- a/lua/ubus.c
++++ b/lua/ubus.c
+@@ -23,6 +23,11 @@
+ #define MODNAME		"ubus"
+ #define METANAME	MODNAME ".meta"
+ 
++#if LUA_VERSION_NUM > 501
++#define luaL_optint luaL_optinteger
++#define lua_objlen lua_rawlen
++#endif
++
+ static lua_State *state;
+ 
+ struct ubus_lua_connection {
+@@ -275,7 +280,7 @@ ubus_lua_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p)
+ 	lua_State *L = (lua_State *)p;
+ 
+ 	lua_pushstring(L, o->path);
+-	lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
++	lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
+ }
+ 
+ static int
+@@ -572,11 +577,11 @@ static int ubus_lua_add(lua_State *L)
+ 			if (obj){
+ 				ubus_add_object(c->ctx, obj);
+ 
+-                                /* allow future reference of ubus obj */
++                /* allow future reference of ubus obj */
+ 				lua_pushstring(state,"__ubusobj");
+ 				lua_pushlightuserdata(state, obj);
+ 				lua_settable(state,-3);
+-                        }
++            }
+ 		}
+ 		lua_pop(L, 1);
+ 	}
+@@ -973,11 +978,21 @@ luaopen_ubus(lua_State *L)
+ 	lua_setfield(L, -2, "__index");
+ 
+ 	/* fill metatable */
++#if LUA_VERSION_NUM > 501
++	luaL_setfuncs(L, ubus, 0);
++#else
+ 	luaL_register(L, NULL, ubus);
+-	lua_pop(L, 1);
++#endif
+ 
+ 	/* create module */
++#if LUA_VERSION_NUM > 501
++	lua_newtable(L);
++    luaL_setfuncs (L, ubus, 0);
++    lua_pushvalue(L, -1);
++    lua_setglobal(L, MODNAME);
++#else
+ 	luaL_register(L, MODNAME, ubus);
++#endif
+ 
+ 	/* set some enum defines */
+ 	lua_pushinteger(L, BLOBMSG_TYPE_ARRAY);
+-- 
+2.26.2
+
diff --git a/recipes-connectivity/ubus/ubus_git.bb b/recipes-connectivity/ubus/ubus_git.bb
index 682b412..5edafb7 100644
--- a/recipes-connectivity/ubus/ubus_git.bb
+++ b/recipes-connectivity/ubus/ubus_git.bb
@@ -5,20 +5,64 @@
 SECTION = "base"
 DEPENDS = "json-c libubox"
 
-SRC_URI = "git://git.openwrt.org/project/ubus.git"
+SRC_URI = "\
+          git://git.openwrt.org/project/ubus.git;protocol=https;nobranch=1; \
+          file://0001-lua5.3-support.patch \
+          "
 
 SRCREV = "2bebf93cd3343fe49f22a05ef935e460d2d44f67"
-PV = "1.0.0+git${SRCPV}"
+
+PV = "git${SRCPV}"
+
+ABI_VERSION = "1.0"
 
 S = "${WORKDIR}/git"
 
 inherit cmake pkgconfig
 
-EXTRA_OECMAKE = "-DBUILD_EXAMPLES=OFF -DBUILD_LUA=OFF -DBUILD_STATIC=ON"
+#from class/openwrt-lua
+OECMAKE_C_FLAGS += "-DLUA_COMPAT_5_3"
+EXTRA_OECMAKE += "-DLUAPATH=${libdir}/lua/5.3"
+
+FILES_${PN}  += "${datadir}/lua/"
+FILES_${PN}-dbg  += "${libdir}/lua/.debug"
+
+DEPENDS += "lua"
+OECMAKE_C_FLAGS += "-I${STAGING_INCDIR}/lua5.3"
+CFLAGS += "-I${STAGING_INCDIR}/lua5.3"
+
+do_configure_prepend () {
+    if [ -e "${S}/CMakeLists.txt" ] ; then
+        sed -i -e \
+        "s:ARCHIVE DESTINATION lib:ARCHIVE DESTINATION \${CMAKE_INSTALL_LIBDIR}:g" \
+        -e "s:LIBRARY DESTINATION lib:LIBRARY DESTINATION \${CMAKE_INSTALL_LIBDIR}:g" \
+        ${S}/CMakeLists.txt
+    fi
+}
+
+EXTRA_OECMAKE += "\
+                -DCMAKE_INSTALL_LIBDIR:PATH=/lib \
+                "
 
 do_install_append () {
     install -dm 0755 ${D}/sbin
     ln -s /usr/sbin/ubusd ${D}/sbin/ubusd
+
+    install -m 0755 ${S}/lua/publisher.lua ${D}${bindir}
+    install -m 0755 ${S}/lua/subscriber.lua ${D}${bindir}
+    install -m 0755 ${S}/lua/test_client.lua ${D}${bindir}
+    install -m 0755 ${S}/lua/test.lua ${D}${bindir}
 }
 
-TOOLCHAIN = "gcc"
+PACKAGES =+ "\
+            lib${PN}${ABI_VERSION} \
+            lib${PN}-lua \
+            lib${PN}-examples \
+            ${PN}d \
+            "
+
+FILES_SOLIBSDEV = ""
+FILES_lib${PN}${ABI_VERSION} += "/lib/libubus.so"
+FILES_lib${PN}-lua += "${libdir}/lua/5.3/*"
+FILES_lib${PN}-examples += "${bindir}/*.lua"
+FILES_${PN}d += "${sbindir} ${base_sbindir}"
diff --git a/recipes-connectivity/ubus/ubus_git.bbappend b/recipes-connectivity/ubus/ubus_git.bbappend
index 9b6236c..65939de 100644
--- a/recipes-connectivity/ubus/ubus_git.bbappend
+++ b/recipes-connectivity/ubus/ubus_git.bbappend
@@ -84,10 +84,3 @@
         sed -i -e 's,@SBINDIR@,${sbindir},g' \
             ${D}${systemd_unitdir}/system/ubusd.service
 }
-
-PACKAGES += "\
-    ${PN}d \
-"
-
-FILES_${PN}d += "${sbindir} ${base_sbindir}"
-FILES_${PN}_remove += "/usr/sbin/* /sbin/*"