Skip to content

Commit

Permalink
cmake: Allow setting mode with CMake
Browse files Browse the repository at this point in the history
The Wakaama modes (server, bootstrap server or client) can now be set
with CMake cache variables. Some CMake presets are provided to build,
run and test Wakaama with different mode configurations.
The example binaries that are needed for the integration tests are now
built with the CMake presets. Therefore the paths of the binaries were
adjusted in the integration tests.
  • Loading branch information
LukasWoodtli committed Jun 11, 2024
1 parent a720ebf commit f0e884c
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 35 deletions.
65 changes: 64 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,62 @@
"patch": 0
},
"configurePresets": [
{
"name": "base",
"description": "Basic preset settings",
"hidden": true,
"binaryDir": "${sourceDir}/build-presets/${presetName}"
},
{
"name": "server",
"description": "Build a server",
"inherits": "base",
"cacheVariables": {
"WAKAAMA_MODE_SERVER": {
"type": "BOOL",
"value": "ON"
},
"WAKAAMA_ENABLE_EXAMPLES": {
"type": "BOOL",
"value": "ON"
}
}
},
{
"name": "bootstrap_server",
"description": "Build a bootstrap server",
"inherits": "base",
"cacheVariables": {
"WAKAAMA_MODE_BOOTSTRAP_SERVER": {
"type": "BOOL",
"value": "ON"
},
"WAKAAMA_ENABLE_EXAMPLES": {
"type": "BOOL",
"value": "ON"
}
}
},
{
"name": "client",
"description": "Build a client",
"inherits": "base",
"cacheVariables": {
"WAKAAMA_MODE_CLIENT": {
"type": "BOOL",
"value": "ON"
},
"WAKAAMA_ENABLE_EXAMPLES": {
"type": "BOOL",
"value": "ON"
}
}
},
{
"name": "log_base",
"description": "Run the tests with corresponding log level",
"hidden": true,
"binaryDir": "${sourceDir}/build-presets/log_tests/${presetName}",
"inherits": "base",
"cacheVariables": {
"WAKAAMA_LOG_CUSTOM_HANDLER": {
"type": "BOOL",
Expand Down Expand Up @@ -57,6 +108,18 @@
}
],
"buildPresets": [
{
"name": "server",
"configurePreset": "server"
},
{
"name": "bootstrap_server",
"configurePreset": "bootstrap_server"
},
{
"name": "client",
"configurePreset": "client"
},
{
"name": "log_dbg",
"configurePreset": "log_dbg"
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ Several preprocessor definitions are supported:
- Endianness: Exactly one has to be defined.
- LWM2M_BIG_ENDIAN if your target platform uses big-endian format.
- LWM2M_LITTLE_ENDIAN if your target platform uses little-endian format.
- Mode: One or multiple modes have to be defined.
- LWM2M_CLIENT_MODE to enable LWM2M Client interfaces.
- LWM2M_SERVER_MODE to enable LWM2M Server interfaces.
- LWM2M_BOOTSTRAP_SERVER_MODE to enable LWM2M Bootstrap Server interfaces.
- LWM2M_BOOTSTRAP to enable LWM2M Bootstrap support in a LWM2M Client.
- LWM2M_SUPPORT_TLV to enable TLV payload support (implicit except for LWM2M 1.1 clients)
- LWM2M_SUPPORT_JSON to enable JSON payload support (implicit when defining LWM2M_SERVER_MODE)
Expand All @@ -93,9 +89,17 @@ Several preprocessor definitions are supported:
- LWM2M_VERSION_1_0 to support only version 1.0
Please note: Clients support only the specified version, while servers are backward compatible.
- LWM2M_RAW_BLOCK1_REQUESTS For low memory client devices where it is not possible to keep a large post or put request in memory to be parsed (typically a firmware write).
This option enable each unprocessed block 1 payload to be passed to the application, typically to be stored to a flash memory.
This option enable each unprocessed block 1 payload to be passed to the application, typically to be stored to a flash memory.
- LWM2M_COAP_DEFAULT_BLOCK_SIZE CoAP block size used by CoAP layer when performing block-wise transfers. Possible values: 16, 32, 64, 128, 256, 512 and 1024. Defaults to 1024.

### Mode

Wakaama supports multiple modes. At least one mode needs to be defined with CMake cache variables.

- WAKAAMA_MODE_SERVER to enable LWM2M Server interfaces.
- WAKAAMA_MODE_BOOTSTRAP_SERVER to enable LWM2M Bootstrap Server interfaces.
- WAKAAMA_MODE_CLIENT to enable LWM2M Client interfaces.

### Logging

The logging infrastructure can be configured with CMake cache variables (e.g. `cmake -DWAKAAMA_LOG_LEVEL=INFO`).
Expand Down
2 changes: 1 addition & 1 deletion examples/bootstrap_server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(bootstrap_server C)

include(../../wakaama.cmake)

if(WAKAAMA_ENABLE_EXAMPLES)
if(WAKAAMA_ENABLE_EXAMPLES AND WAKAAMA_MODE_BOOTSTRAP_SERVER)
add_executable(bootstrap_server bootstrap_info.c bootstrap_server.c bootstrap_info.h bootstrap_server.ini)
target_compile_definitions(bootstrap_server PRIVATE LWM2M_BOOTSTRAP_SERVER_MODE)
target_sources_wakaama(bootstrap_server)
Expand Down
3 changes: 2 additions & 1 deletion examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ project(lwm2mclient C)

include(../../wakaama.cmake)

if(WAKAAMA_ENABLE_EXAMPLES)
if(WAKAAMA_ENABLE_EXAMPLES AND WAKAAMA_MODE_CLIENT)

set(SOURCES
lwm2mclient.c
lwm2mclient.h
Expand Down
2 changes: 1 addition & 1 deletion examples/lightclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(lightclient C)

include(../../wakaama.cmake)

if(WAKAAMA_ENABLE_EXAMPLES)
if(WAKAAMA_ENABLE_EXAMPLES AND WAKAAMA_MODE_CLIENT)
add_executable(lightclient lightclient.c object_device.c object_security.c object_server.c object_test.c)
target_compile_definitions(lightclient PRIVATE LWM2M_CLIENT_MODE)
target_sources_wakaama(lightclient)
Expand Down
2 changes: 1 addition & 1 deletion examples/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(lwm2mserver C)

include(../../wakaama.cmake)

if(WAKAAMA_ENABLE_EXAMPLES)
if(WAKAAMA_ENABLE_EXAMPLES AND WAKAAMA_MODE_SERVER)
add_executable(lwm2mserver lwm2mserver.c)
target_compile_definitions(lwm2mserver PRIVATE LWM2M_SERVER_MODE)
target_sources_wakaama(lwm2mserver)
Expand Down
22 changes: 12 additions & 10 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ set(TEST_SOURCES
helper/log_handler.c
)

add_test_variant(
TARGET_NAME lwm2munittests_client_lwm2m_1_0
SOURCE_FILES ${TEST_SOURCES}
COMPILE_DEFINITIONS LWM2M_CLIENT_MODE LWM2M_VERSION_1_0
)
if(LWM2M_CLIENT_MODE AND NOT WAKAAMA_MODE_BOOTSTRAP_SERVER)
add_test_variant(
TARGET_NAME lwm2munittests_client_lwm2m_1_0
SOURCE_FILES ${TEST_SOURCES}
COMPILE_DEFINITIONS LWM2M_CLIENT_MODE LWM2M_VERSION_1_0
)

add_test_variant(
TARGET_NAME lwm2munittests_client_bootstrap
SOURCE_FILES ${TEST_SOURCES}
COMPILE_DEFINITIONS LWM2M_CLIENT_MODE LWM2M_BOOTSTRAP
)
add_test_variant(
TARGET_NAME lwm2munittests_client_bootstrap
SOURCE_FILES ${TEST_SOURCES}
COMPILE_DEFINITIONS LWM2M_CLIENT_MODE LWM2M_BOOTSTRAP
)
endif()

add_test_variant(
TARGET_NAME lwm2munittests_server_formats_no_float16_blocksize_64
Expand Down
29 changes: 15 additions & 14 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import pytest


BASE_PATH = str(Path(__file__).parent.absolute())
REPO_BASE_PATH = BASE_PATH + "/../../"


# pylint: disable=no-member
class HelperBase:
"""Provides helper methods for integration tests. Wraps "pexpect" API"""
Expand Down Expand Up @@ -67,14 +71,13 @@ class Lwm2mServer(HelperBase):
"""Server subclass of HelperBase"""

def __init__(self, arguments="", timeout=3, encoding="utf8"):
base_path = str(Path(__file__).parent.absolute())
self.pexpectobj = pexpect.spawn(base_path +
"/../../build-wakaama/examples/server/lwm2mserver " +
self.pexpectobj = pexpect.spawn(REPO_BASE_PATH +
"/build-presets/server/examples/server/lwm2mserver " +
arguments,
encoding=encoding,
timeout=timeout)
# pylint: disable=consider-using-with
self.pexpectobj.logfile = open(base_path +
self.pexpectobj.logfile = open(BASE_PATH +
"/lwm2mserver_log.txt",
"w",
encoding="utf-8")
Expand All @@ -84,14 +87,13 @@ class Lwm2mClient(HelperBase):
"""Client subclass of HelperBase"""

def __init__(self, arguments="", timeout=3, encoding="utf8"):
base_path = str(Path(__file__).parent.absolute())
self.pexpectobj = pexpect.spawn(base_path +
"/../../build-wakaama/examples/client/lwm2mclient "
self.pexpectobj = pexpect.spawn(REPO_BASE_PATH +
"/build-presets/client/examples/client/lwm2mclient "
+ arguments,
encoding=encoding,
timeout=timeout)
# pylint: disable=consider-using-with
self.pexpectobj.logfile = open(base_path +
self.pexpectobj.logfile = open(BASE_PATH +
"/lwm2mclient_log.txt",
"w",
encoding="utf-8")
Expand All @@ -101,15 +103,14 @@ class Lwm2mBootstrapServer(HelperBase):
"""Bootstrap-server subclass of HelperBase"""

def __init__(self, arguments="", timeout=3, encoding="utf8"):
base_path = str(Path(__file__).parent.absolute())
self.pexpectobj = pexpect.spawn(base_path +
"/../../build-wakaama/examples/"
self.pexpectobj = pexpect.spawn(REPO_BASE_PATH +
"/build-presets/bootstrap_server/examples/"
"bootstrap_server/bootstrap_server "
+ arguments,
encoding=encoding,
timeout=timeout)
# pylint: disable=consider-using-with
self.pexpectobj.logfile = open(base_path +
self.pexpectobj.logfile = open(BASE_PATH +
"/lwm2mbootstrapserver_log.txt",
"w",
encoding="utf-8")
Expand Down Expand Up @@ -144,8 +145,8 @@ def lwm2mclient(request):
@pytest.fixture
def lwm2mbootstrapserver():
"""Provide bootstrap_server instance."""
bootstrapserver = Lwm2mBootstrapServer("-f examples/bootstrap_server/bootstrap_server.ini",
timeout=13)
ini_file = REPO_BASE_PATH + "/examples/bootstrap_server/bootstrap_server.ini"
bootstrapserver = Lwm2mBootstrapServer(f"-f {ini_file}", timeout=13)
bootstrapserver.wait_for_text(">")
yield bootstrapserver
bootstrapserver.quit()
1 change: 1 addition & 0 deletions tools/ci/run_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function run_clang_format() {

function run_clean() {
rm -rf build-wakaama
rm -rf build-presets
}

function run_cmake_format() {
Expand Down
27 changes: 26 additions & 1 deletion wakaama.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ set(WAKAAMA_TOP_LEVEL_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}")
set(WAKAAMA_EXAMPLE_DIRECTORY "${WAKAAMA_TOP_LEVEL_DIRECTORY}/examples")
set(WAKAAMA_EXAMPLE_SHARED_DIRECTORY "${WAKAAMA_EXAMPLE_DIRECTORY}/shared")

# Mode
option(WAKAAMA_MODE_SERVER "Enable LWM2M Server interfaces" OFF)
option(WAKAAMA_MODE_BOOTSTRAP_SERVER "Enable LWM2M Bootstrap Server interfaces" OFF)
option(WAKAAMA_MODE_CLIENT "Enable LWM2M Client interfaces" OFF)

# Logging
set(WAKAAMA_LOG_LEVEL
LOG_DISABLED
Expand All @@ -28,8 +33,22 @@ set(WAKAAMA_LOG_MAX_MSG_TXT_SIZE
# Possibility to disable the examples
option(WAKAAMA_ENABLE_EXAMPLES "Build all the example applications" ON)

# Set the defines for Wakaama mode configuration
function(set_mode_defines target)
# Mode
if(WAKAAMA_MODE_CLIENT)
target_compile_definitions(${target} PUBLIC LWM2M_CLIENT_MODE)
endif()
if(WAKAAMA_MODE_SERVER)
target_compile_definitions(${target} PUBLIC LWM2M_SERVER_MODE)
endif()
if(WAKAAMA_MODE_BOOTSTRAP_SERVER)
target_compile_definitions(${target} PUBLIC LWM2M_BOOTSTRAP_SERVER_MODE)
endif()
endfunction()

# Set the defines for logging configuration
function(set_defines target)
function(set_logging_defines target)
# Logging
target_compile_definitions(${target} PUBLIC LWM2M_LOG_LEVEL=LWM2M_${WAKAAMA_LOG_LEVEL})

Expand All @@ -40,6 +59,12 @@ function(set_defines target)
target_compile_definitions(${target} PUBLIC LWM2M_LOG_MAX_MSG_TXT_SIZE=${WAKAAMA_LOG_MAX_MSG_TXT_SIZE})
endfunction()

# Set all the requested defines on target
function(set_defines target)
set_mode_defines(${target})
set_logging_defines(${target})
endfunction()

# Add data format source files to an existing target.
#
# Separated from target_sources_wakaama() for testability reasons.
Expand Down

0 comments on commit f0e884c

Please sign in to comment.