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>
This commit is contained in:
Pieter De Gendt 2023-11-08 14:56:32 +01:00 committed by Fabio Baltieri
commit e816fc5a81
5 changed files with 62 additions and 0 deletions

View file

@ -17,3 +17,8 @@ zephyr_nanopb_sources(app
FILE(GLOB app_sources src/*.c) FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources}) 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)

View file

@ -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)

View file

@ -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;
}
}

View file

@ -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

View file

@ -14,6 +14,8 @@
#include <proto/simple.pb.h> #include <proto/simple.pb.h>
#include <proto/complex.pb.h> #include <proto/complex.pb.h>
#include "lib.h"
ZTEST(nanopb_tests, test_nanopb_simple) ZTEST(nanopb_tests, test_nanopb_simple)
{ {
uint8_t buffer[SimpleMessage_size]; uint8_t buffer[SimpleMessage_size];
@ -68,4 +70,15 @@ ZTEST(nanopb_tests, test_nanopb_nested)
zassert_equal(0, strcmp(msg.nested.name, "Test name")); 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); ZTEST_SUITE(nanopb_tests, NULL, NULL, NULL, NULL, NULL);