From 027a078cb76fd049296d32a44c51c2708e9e32e8 Mon Sep 17 00:00:00 2001
From: Zbigniew Bodek <zbigniew.bodek@huawei.com>
Date: Mon, 4 Jan 2021 18:17:18 +0000
Subject: [PATCH] Introduce initial recipe for Softbus

Softbus implements "Distributed Communication Subsystem".
This commit provides initial recipe to build softbus
using bitbake and link it to the foundation application.

The functionality hasn't been tested (only build) so when
Softbus is actually used some missing bits may appear.

Signed-off-by: Zbigniew Bodek <zbigniew.bodek@huawei.com>
---
 .../softbus-kits/softbus-kits_git.bb          | 17 +++++
 .../recipes-safwk/safwk/safwk_git.bb          |  4 +-
 ...rrect-number-of-arguments-to-mq_open.patch | 42 ++++++++++++
 .../recipes-softbus/softbus/softbus_git.bb    | 68 +++++++++++++++++++
 4 files changed, 129 insertions(+), 2 deletions(-)
 create mode 100644 meta-ohos-foundation/recipes-interfaces/softbus-kits/softbus-kits_git.bb
 create mode 100644 meta-ohos-foundation/recipes-softbus/softbus/softbus/0001-Fix-incorrect-number-of-arguments-to-mq_open.patch
 create mode 100644 meta-ohos-foundation/recipes-softbus/softbus/softbus_git.bb

diff --git a/meta-ohos-foundation/recipes-interfaces/softbus-kits/softbus-kits_git.bb b/meta-ohos-foundation/recipes-interfaces/softbus-kits/softbus-kits_git.bb
new file mode 100644
index 00000000..3593bdb9
--- /dev/null
+++ b/meta-ohos-foundation/recipes-interfaces/softbus-kits/softbus-kits_git.bb
@@ -0,0 +1,17 @@
+SUMMARY = "OHOS interface kits for Softbus"
+DESCRIPTION = "Interface kits for OHOS communication utility - Softbus"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://LICENSE;md5=98c2e72b17fae6c40fb14fd5e43b29ec"
+
+SRC_URI = "git://gitee.com/openharmony/communication_interfaces_kits_softbuskit_lite.git;protocol=https"
+
+PV = "1.0+git${SRCPV}"
+SRCREV = "40aec6fdcdeed401adc97451434b26919e80c69a"
+
+S = "${WORKDIR}/git"
+
+do_install () {
+	install -d ${D}${includedir}
+	install -m 0755 ${S}/discovery/*.h ${D}${includedir}/
+	install -m 0755 ${S}/transport/*.h ${D}${includedir}/
+}
diff --git a/meta-ohos-foundation/recipes-safwk/safwk/safwk_git.bb b/meta-ohos-foundation/recipes-safwk/safwk/safwk_git.bb
index 769370c9..d9f23d64 100644
--- a/meta-ohos-foundation/recipes-safwk/safwk/safwk_git.bb
+++ b/meta-ohos-foundation/recipes-safwk/safwk/safwk_git.bb
@@ -3,7 +3,7 @@ DESCRIPTION = "OHOS foundation main application"
 LICENSE = "Apache-2.0"
 LIC_FILES_CHKSUM = "file://LICENSE;md5=98c2e72b17fae6c40fb14fd5e43b29ec"
 
-DEPENDS += "utils-native-lite libsec hiviewdfx-innerkits-hilog hiviewdfx-hilog samgr samgr-kits"
+DEPENDS += "utils-native-lite libsec hiviewdfx-innerkits-hilog hiviewdfx-hilog samgr samgr-kits softbus"
 
 SRC_URI = "git://gitee.com/openharmony/distributedschedule_services_safwk_lite.git;protocol=https"
 
@@ -17,7 +17,7 @@ CFLAGS += "-DDEBUG_SERVICES_SAFWK_LITE"
 LDFLAGS += "-lrt"
 
 do_compile () {
-	${CC} ${CFLAGS} ${LDFLAGS} src/*.c -o ${B}/foundation -lsamgr -lsec -lhiviewdfx-hilog
+	${CC} ${CFLAGS} ${LDFLAGS} src/*.c -o ${B}/foundation -lsamgr -lsec -lhiviewdfx-hilog -lsoftbus
 }
 
 do_install () {
diff --git a/meta-ohos-foundation/recipes-softbus/softbus/softbus/0001-Fix-incorrect-number-of-arguments-to-mq_open.patch b/meta-ohos-foundation/recipes-softbus/softbus/softbus/0001-Fix-incorrect-number-of-arguments-to-mq_open.patch
new file mode 100644
index 00000000..f6be1f57
--- /dev/null
+++ b/meta-ohos-foundation/recipes-softbus/softbus/softbus/0001-Fix-incorrect-number-of-arguments-to-mq_open.patch
@@ -0,0 +1,42 @@
+From d1b564ce1c294be4752204bad8261316b8e56ba1 Mon Sep 17 00:00:00 2001
+From: Zbigniew Bodek <zbigniew.bodek@huawei.com>
+Date: Thu, 31 Dec 2020 16:24:24 +0000
+Subject: [PATCH] Fix incorrect number of arguments to mq_open()
+
+Related issue:
+https://gitee.com/openharmony/communication_services_softbus_lite/issues/I2BBKX
+
+According to man:
+If O_CREAT is specified in oflag, then two additional arguments
+must be supplied.
+
+os_adapter.c is missing "mode" parameter in mq_open(). It will compile
+for MUSL, which does not check the number of arguments passed through
+variadic argument but it doesn't mean it works as designed.
+
+Fix this issue as well as build for other libraries with built-in
+checks by introducing missing "mode" argument.
+
+File mode has been selected to 0600 (RW for owner) but is a subject
+to change if needed.
+
+Signed-off-by: Zbigniew Bodek <zbigniew.bodek@huawei.com>
+---
+ os_adapter/source/L1/os_adapter.c | 4 +++-
+ 1 file changed, 3 insertions(+), 1 deletion(-)
+
+diff --git a/os_adapter/source/L1/os_adapter.c b/os_adapter/source/L1/os_adapter.c
+index 98f91c9..9eb693a 100755
+--- a/os_adapter/source/L1/os_adapter.c
++++ b/os_adapter/source/L1/os_adapter.c
+@@ -79,7 +79,9 @@ int CreateMsgQue(const char *queueName,
+     newAttr.mq_flags = flags;
+     newAttr.mq_maxmsg = len;
+     newAttr.mq_msgsize = maxMsgSize;
+-    int mqd = mq_open(queueName, O_RDWR | O_CREAT, &newAttr);
++    /* Owner read and write permission - 0600 */
++    mode_t mode = (S_IRUSR | S_IWUSR);
++    int mqd = mq_open(queueName, O_RDWR | O_CREAT, mode, &newAttr);
+     if (mqd < 0) {
+         return -1;
+     }
diff --git a/meta-ohos-foundation/recipes-softbus/softbus/softbus_git.bb b/meta-ohos-foundation/recipes-softbus/softbus/softbus_git.bb
new file mode 100644
index 00000000..949d0892
--- /dev/null
+++ b/meta-ohos-foundation/recipes-softbus/softbus/softbus_git.bb
@@ -0,0 +1,68 @@
+SUMMARY = "OHOS Softbus"
+DESCRIPTION = "OHOS communication utility for distributed services"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://LICENSE;md5=98c2e72b17fae6c40fb14fd5e43b29ec"
+
+DEPENDS += "cjson libsec samgr softbus-kits iam-kits hichainsdk hichainsdk-innerkits mbedtls"
+
+SRC_URI = "git://gitee.com/openharmony/communication_services_softbus_lite.git;protocol=https \
+           file://0001-Fix-incorrect-number-of-arguments-to-mq_open.patch \
+           "
+
+PV = "1.0+git${SRCPV}"
+PVSHORT = '${@d.getVar("PV", False).split("+")[0]}'
+PVMAJOR = '${@d.getVar("PV", False).split(".")[0]}'
+
+SRCREV = "b3f7261e31d0be165979446fef4a6c415cc03f65"
+
+S = "${WORKDIR}/git"
+
+SB_SRCS = "${S}/discovery/coap/source/coap_discover.c		\
+		${S}/discovery/coap/source/json_payload.c	\
+		${S}/discovery/coap/source/nstackx_common.c	\
+		${S}/discovery/coap/source/nstackx_device.c	\
+		${S}/discovery/coap/source/coap_socket.c	\
+		${S}/discovery/coap/source/coap_adapter.c	\
+		${S}/os_adapter/source/L1/os_adapter.c		\
+		${S}/discovery/discovery_service/source/discovery_service.c	\
+		${S}/discovery/discovery_service/source/coap_service.c		\
+		${S}/discovery/discovery_service/source/common_info_manager.c	\
+		${S}/trans_service/source/libdistbus/tcp_session.c		\
+		${S}/trans_service/source/libdistbus/tcp_session_manager.c	\
+		${S}/trans_service/source/libdistbus/auth_conn_manager.c	\
+		${S}/trans_service/source/libdistbus/trans_lock.c		\
+		${S}/trans_service/source/utils/tcp_socket.c	\
+		${S}/trans_service/source/utils/message.c	\
+		${S}/trans_service/source/utils/aes_gcm.c	\
+		${S}/authmanager/source/auth_conn.c		\
+		${S}/authmanager/source/auth_interface.c	\
+		${S}/authmanager/source/msg_get_deviceid.c	\
+		${S}/authmanager/source/wifi_auth_manager.c	\
+		${S}/authmanager/source/bus_manager.c"
+
+SB_INC = "-I${S}/discovery/coap/include				\
+		-I${S}/os_adapter/include			\
+		-I${S}/discovery/discovery_service/include	\
+		-I${S}/authmanager/include			\
+		-I${S}/trans_service/include/libdistbus		\
+		-I${S}/trans_service/include/utils		\
+		-I${S}/trans_service/source/libdistbus		\
+		-I${S}/trans_service/source/utils"
+SB_INC += "-I${STAGING_INCDIR}/cjson"
+
+CFLAGS_SB = "${CFLAGS} ${SB_INC} -fPIC -shared"
+# Flags copied from the original OHOS build configuration. To be replaced.
+CFLAGS_SB += "-D_GNU_SOURCE -D_SCANTY_MEMORY_ -D__LINUX__"
+LDFLAGS_SB = "${LDFLAGS} -lsec -lcjson -lrt -L${B} -Wl,-soname,lib${PN}.so.${PVMAJOR}"
+
+do_compile () {
+	${CC} ${CFLAGS_SB} ${LDFLAGS_SB}	\
+		${SB_SRCS}			\		
+		-o ${B}/lib${PN}.so.${PVSHORT}
+
+}
+
+do_install () {
+	install -d ${D}${libdir}
+	oe_soinstall ${B}/lib${PN}.so.${PVSHORT} ${D}${libdir}
+}
-- 
GitLab