tests: modules: nanopb: Add a custom nested library

Add a library to the Nanopb test to demonstrate building with a
dependency on the generated header files.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
diff --git a/tests/modules/nanopb/CMakeLists.txt b/tests/modules/nanopb/CMakeLists.txt
index bbe95de..702958a 100644
--- a/tests/modules/nanopb/CMakeLists.txt
+++ b/tests/modules/nanopb/CMakeLists.txt
@@ -17,3 +17,8 @@
 
 FILE(GLOB app_sources src/*.c)
 target_sources(app PRIVATE ${app_sources})
+
+# Process our own library
+add_subdirectory(lib)
+target_include_directories(app PRIVATE lib)
+target_link_libraries(app PRIVATE mylib)
diff --git a/tests/modules/nanopb/lib/CMakeLists.txt b/tests/modules/nanopb/lib/CMakeLists.txt
new file mode 100644
index 0000000..8de22b1
--- /dev/null
+++ b/tests/modules/nanopb/lib/CMakeLists.txt
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: Apache-2.0
+
+add_library(mylib lib.c)
+
+# Demonstrate that our library includes a generated header so we need the following dependency
+add_dependencies(mylib nanopb_generated_headers)
+
+# Add include directory to find the generated headers
+target_include_directories(mylib PRIVATE ${CMAKE_BINARY_DIR})
+
+# Link against zephyr
+target_link_libraries(mylib zephyr)
diff --git a/tests/modules/nanopb/lib/lib.c b/tests/modules/nanopb/lib/lib.c
new file mode 100644
index 0000000..083ae25
--- /dev/null
+++ b/tests/modules/nanopb/lib/lib.c
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023 Basalte bv
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "lib.h"
+
+void lib_fill_message(SimpleMessage *msg)
+{
+	/* Reversed numbers */
+	for (size_t i = 0; i < sizeof(msg->buffer); ++i) {
+		msg->buffer[i] = sizeof(msg->buffer) - i;
+	}
+}
diff --git a/tests/modules/nanopb/lib/lib.h b/tests/modules/nanopb/lib/lib.h
new file mode 100644
index 0000000..7193d1f
--- /dev/null
+++ b/tests/modules/nanopb/lib/lib.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2023 Basalte bv
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef __LIB_H_
+#define __LIB_H_
+
+#include <proto/simple.pb.h>
+
+/**
+ * Some sample library function that fills a SimpleMessage struct
+ */
+void lib_fill_message(SimpleMessage *msg);
+
+#endif
diff --git a/tests/modules/nanopb/src/main.c b/tests/modules/nanopb/src/main.c
index cd68298..798b11e 100644
--- a/tests/modules/nanopb/src/main.c
+++ b/tests/modules/nanopb/src/main.c
@@ -14,6 +14,8 @@
 #include <proto/simple.pb.h>
 #include <proto/complex.pb.h>
 
+#include "lib.h"
+
 ZTEST(nanopb_tests, test_nanopb_simple)
 {
 	uint8_t buffer[SimpleMessage_size];
@@ -68,4 +70,15 @@
 	zassert_equal(0, strcmp(msg.nested.name, "Test name"));
 }
 
+ZTEST(nanopb_tests, test_nanopb_lib)
+{
+	SimpleMessage msg = SimpleMessage_init_zero;
+
+	lib_fill_message(&msg);
+
+	for (size_t i = 0; i < sizeof(msg.buffer); ++i) {
+		zassert_equal(msg.buffer[i], sizeof(msg.buffer) - i);
+	}
+}
+
 ZTEST_SUITE(nanopb_tests, NULL, NULL, NULL, NULL, NULL);