Skip to content

Commit

Permalink
Improve building process
Browse files Browse the repository at this point in the history
fixed the build process according to comments on PR
  • Loading branch information
LukasKarel committed Nov 21, 2021
1 parent c525638 commit e30ca0c
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
path = examples/shared/dtls/tinydtls
url = https://github.com/eclipse/tinydtls.git
[submodule "examples/shared/mbedtls"]
path = examples/shared/mbedtls
path = examples/shared/dtls/mbedtls
url = https://github.com/ARMmbed/mbedtls
16 changes: 14 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ cmake_minimum_required(VERSION 3.13)

project(wakaama C)

option(DTLS_MBEDTLS "Build wakaama with mbedtls support" OFF)
option(DTLS_TINYDTLS "Build wakaama with tinydtls support" OFF)

if(DTLS_MBEDTLS AND DTLS_TINYDTLS)
message(FATAL_ERROR "Cant build wakaama with multiple DTLS libraries.")
endif()

set(DTLS DTLS_MBEDTLS OR DTLS_TINYDTLS)

# Enable "test" target
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
enable_testing()
enable_testing()


if(DTLS)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/examples/shared/dtls)
endif()

add_subdirectory(tests)
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Options:
* ``make``
* ``./lwm2mclient [Options]``

DTLS feature requires the tinydtls submodule. To include it, on the first run,
DTLS feature requires either **tinydtls** or **mbedtls** submodule. To include it, on the first run,
use the following commands to retrieve the sources:
* ``git submodule init``
* ``git submodule update``
Expand All @@ -145,7 +145,13 @@ You need to install autoconf and automake to build with tinydtls.

Build with tinydtls:
* Create a build directory and change to that.
* ``cmake -DDTLS=1 [wakaama directory]/examples/client``
* ``cmake -DDTLS_TINYDTLS=1 [wakaama directory]/examples/client``
* ``make``
* ``./lwm2mclient [Options]``

Build with mbedtls:
* Create a build directory and change to that.
* ``cmake -DDTLS_MBEDTLS=1 [wakaama directory]/examples/client``
* ``make``
* ``./lwm2mclient [Options]``

Expand Down
13 changes: 2 additions & 11 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ cmake_minimum_required(VERSION 3.13)
add_compile_definitions(_POSIX_C_SOURCE=200809)
add_compile_options(-pedantic)

if(DTLS_MBEDTLS)
option(ENABLE_PROGRAMS "Build mbed TLS programs." OFF)
option(ENABLE_TESTING "Build mbed TLS tests." OFF)
# Use custom config file for Mbed TLS
add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/shared/dtls/config-ccm-psk-tls1_2.h")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/shared/dtls/mbedtls)
else()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lightclient)
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lightclient)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/bootstrap_server)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/client)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/server)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/server)
10 changes: 6 additions & 4 deletions examples/bootstrap_server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ cmake_minimum_required(VERSION 3.13)

project(bootstrap_server C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported.")
endif()

include(${CMAKE_CURRENT_LIST_DIR}/../../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../coap/coap.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../data/data.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../shared/shared.cmake)

if(DTLS)
message(STATUS "bootstrap_server does not support DTLS")
list(REMOVE_ITEM SHARED_DEFINITIONS DTLS)
endif()

add_compile_definitions(LWM2M_BOOTSTRAP_SERVER_MODE)
add_compile_definitions(SHARED_DEFINITIONS)

include_directories(${WAKAAMA_HEADERS_DIR} ${COAP_HEADERS_DIR} ${DATA_HEADERS_DIR} ${WAKAAMA_SOURCES_DIR} ${SHARED_INCLUDE_DIRS})

Expand Down
10 changes: 7 additions & 3 deletions examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.13)

project(lwm2mclient C)

option(DTLS "Enable DTLS" OFF)

include(${CMAKE_CURRENT_LIST_DIR}/../../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../coap/coap.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../data/data.cmake)
Expand Down Expand Up @@ -40,8 +38,14 @@ SET(SOURCES
)

add_executable(${PROJECT_NAME} ${SOURCES} ${WAKAAMA_SOURCES} ${COAP_SOURCES} ${DATA_SOURCES} ${SHARED_SOURCES})
target_compile_definitions(${PROJECT_NAME} PUBLIC ${SHARED_DEFINITIONS})

if (DTLS_MBEDTLS)
if (DTLS_TINYDTLS)
target_link_libraries(${PROJECT_NAME}
PRIVATE
tinydtls
)
elseif (DTLS_MBEDTLS)
target_link_libraries(${PROJECT_NAME}
PRIVATE
mbedtls
Expand Down
70 changes: 38 additions & 32 deletions examples/client/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include "lwm2mclient.h"
#include "liblwm2m.h"
#include "commandline.h"
#if defined(WITH_TINYDTLS) || defined(WITH_MBEDTLS)
#if defined(DTLS)
#include "dtlsconnection.h"
#endif

Expand Down Expand Up @@ -204,27 +204,30 @@ void *lwm2m_connect_server(uint16_t secObjInstID, void *userData) {

uri = get_server_uri(dataP->securityObjP, secObjInstID);

if (uri == NULL)
return NULL;
if (uri == NULL) return NULL;


// parse uri in the form "coaps://[host]:[port]"
if (0 == strncmp(uri, "coaps://", strlen("coaps://"))) {
host = uri + strlen("coaps://");
} else if (0 == strncmp(uri, "coap://", strlen("coap://"))) {
host = uri + strlen("coap://");
} else {
if (0==strncmp(uri, "coaps://", strlen("coaps://"))) {
host = uri+strlen("coaps://");
}
else if (0==strncmp(uri, "coap://", strlen("coap://"))) {
host = uri+strlen("coap://");
}
else {
goto exit;
}
port = strrchr(host, ':');
if (port == NULL)
goto exit;
if (port == NULL) goto exit;
// remove brackets
if (host[0] == '[') {
if (host[0] == '[')
{
host++;
if (*(port - 1) == ']') {
if (*(port - 1) == ']')
{
*(port - 1) = 0;
} else
goto exit;
}
else goto exit;
}
// split strings
*port = 0;
Expand All @@ -236,7 +239,7 @@ void *lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
goto exit;
}
if (securityMode == LWM2M_SECURITY_MODE_PRE_SHARED_KEY) {
#if defined(WITH_TINYDTLS) || defined(WITH_MBEDTLS)
#if defined(DTLS)
newConnP = (connection_t *)dtlsconnection_create(dataP->connLayer, secObjInstID, dataP->sock, host, port,
dataP->addressFamily);
#endif
Expand Down Expand Up @@ -789,12 +792,9 @@ void print_usage(void)
fprintf(stdout, " -c\t\tChange battery level over time.\r\n");
fprintf(stdout, " -S BYTES\tCoAP block size. Options: 16, 32, 64, 128, 256, 512, 1024. Default: %" PRIu16 "\r\n",
LWM2M_COAP_DEFAULT_BLOCK_SIZE);
#if defined WITH_TINYDTLS || defined WITH_MBEDTLS
fprintf(
stdout,
" -i STRING\tSet the device management or bootstrap server PSK identity. If not set use none secure mode\r\n");
fprintf(stdout, " -s HEXSTRING\tSet the device management or bootstrap server Pre-Shared-Key. If not set use none "
"secure mode\r\n");
#if defined DTLS
fprintf(stdout, " -i STRING\tSet the device management or bootstrap server PSK identity. If not set use none secure mode\r\n");
fprintf(stdout, " -s HEXSTRING\tSet the device management or bootstrap server Pre-Shared-Key. If not set use none secure mode\r\n");
#endif
fprintf(stdout, "\r\n");
}
Expand All @@ -820,7 +820,7 @@ int main(int argc, char *argv[])
#endif

char * pskId = NULL;
#if defined WITH_TINYDTLS || defined WITH_MBEDTLS
#if defined DTLS
char *psk = NULL;
#endif
uint16_t pskLen = -1;
Expand Down Expand Up @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
return 0;
}
break;
#if defined WITH_TINYDTLS || defined WITH_MBEDTLS
#if defined DTLS
case 'i':
opt++;
if (opt >= argc)
Expand Down Expand Up @@ -994,7 +994,7 @@ int main(int argc, char *argv[])
* Now the main function fill an array with each object, this list will be later passed to liblwm2m.
* Those functions are located in their respective object file.
*/
#if defined WITH_TINYDTLS || defined WITH_MBEDTLS
#if DTLS
if (psk != NULL)
{
pskLen = strlen(psk) / 2;
Expand Down Expand Up @@ -1028,7 +1028,7 @@ int main(int argc, char *argv[])

char serverUri[50];
int serverId = 123;
#if defined WITH_TINYDTLS || defined WITH_MBEDTLS
#if defined DTLS
sprintf (serverUri, "coaps://%s:%s", server, serverPort);
#else
sprintf (serverUri, "coap://%s:%s", server, serverPort);
Expand Down Expand Up @@ -1237,8 +1237,7 @@ int main(int argc, char *argv[])
#endif
prv_restore_objects(lwm2mH);
lwm2mH->state = STATE_INITIAL;
} else
return -1;
} else return -1;
#else
return -1;
#endif
Expand Down Expand Up @@ -1286,15 +1285,19 @@ int main(int argc, char *argv[])
else if (numBytes >= MAX_PACKET_SIZE)
{
fprintf(stderr, "Received packet >= MAX_PACKET_SIZE\r\n");
} else if (0 < numBytes) {
}
else if (0 < numBytes)
{
char s[INET6_ADDRSTRLEN];
in_port_t port;
connection_t *connP;
if (AF_INET == addr.ss_family) {
struct sockaddr_in *saddr = (struct sockaddr_in *)&addr;
inet_ntop(saddr->sin_family, &saddr->sin_addr, s, INET6_ADDRSTRLEN);
port = saddr->sin_port;
} else if (AF_INET6 == addr.ss_family) {
}
else if (AF_INET6 == addr.ss_family)
{
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&addr;
inet_ntop(saddr->sin6_family, &saddr->sin6_addr, s, INET6_ADDRSTRLEN);
port = saddr->sin6_port;
Expand All @@ -1307,13 +1310,16 @@ int main(int argc, char *argv[])
output_buffer(stderr, buffer, (size_t)numBytes, 0);

connP = connectionlayer_find_connection(data.connLayer, &addr, addrLen);
if (connP != NULL) {
if (connP != NULL)
{
/*
* Let liblwm2m respond to the query depending on the context
*/
connectionlayer_handle_packet(data.connLayer, &addr, addrLen, buffer, numBytes);
conn_s_updateRxStatistic(objArray[7], numBytes, false);
} else {
}
else
{
fprintf(stderr, "received bytes ignored!\r\n");
}
}
Expand Down Expand Up @@ -1352,7 +1358,7 @@ int main(int argc, char *argv[])
*/
if (g_quit == 1)
{
#ifdef WITH_TINYDTLS
#ifdef DTLS
free(pskBuffer);
#endif

Expand Down
10 changes: 6 additions & 4 deletions examples/lightclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ cmake_minimum_required(VERSION 3.13)

project(lightclient C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported." )
endif()

include(${CMAKE_CURRENT_LIST_DIR}/../../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../coap/coap.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../data/data.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../shared/shared.cmake)

if(DTLS)
message(STATUS "lightclient does not support DTLS")
list(REMOVE_ITEM SHARED_DEFINITIONS DTLS)
endif()

add_compile_definitions(SHARED_DEFINITIONS)
add_compile_definitions(LWM2M_CLIENT_MODE)

if(LWM2M_VERSION VERSION_GREATER "1.0")
Expand Down
10 changes: 6 additions & 4 deletions examples/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ cmake_minimum_required(VERSION 3.13)

project(lwm2mserver C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported.")
endif()

include(${CMAKE_CURRENT_LIST_DIR}/../../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../coap/coap.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../data/data.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../shared/shared.cmake)

if(DTLS)
message(STATUS "server does not support DTLS")
list(REMOVE_ITEM SHARED_DEFINITIONS DTLS)
endif()

add_compile_definitions(SHARED_DEFINITIONS)
add_compile_definitions(LWM2M_SERVER_MODE)

include_directories(${WAKAAMA_HEADERS_DIR} ${COAP_HEADERS_DIR} ${DATA_HEADERS_DIR} ${WAKAAMA_SOURCES_DIR} ${SHARED_INCLUDE_DIRS})
Expand Down
2 changes: 1 addition & 1 deletion examples/shared/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ addr6->s6_addr[12]; return y6to4 == ((struct sockaddr_in *)x)->sin_addr.s_addr;
}
}
*/
*/
Loading

0 comments on commit e30ca0c

Please sign in to comment.