Skip to content

Commit

Permalink
MINIFICPP-1007 NaNoFi ecu integration with c2 over coap including win…
Browse files Browse the repository at this point in the history
…dows support
  • Loading branch information
Murtuza Shareef authored and msharee9 committed Nov 28, 2019
1 parent 8a2bb1b commit fc2b828
Show file tree
Hide file tree
Showing 52 changed files with 6,270 additions and 115 deletions.
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ include_directories(thirdparty/ut)
set(UUID_FOUND "YES" CACHE STRING "" FORCE)
set(UUID_LIBRARIES "uuid" CACHE STRING "" FORCE)


if (DISABLE_CURL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_CURL")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_CURL")
Expand All @@ -274,7 +273,6 @@ if(NOT DISABLE_CURL)
set(CURL_C_FLAGS "${CMAKE_C_FLAGS}")
set(CURL_CXX_FLAGS "${CMAKE_CXX_FLAGS}")


get_property(LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)

if ("${LIB64}" STREQUAL "TRUE" AND (NOT WIN32 AND NOT APPLE))
Expand Down Expand Up @@ -469,12 +467,12 @@ if (ENABLE_ALL OR ENABLE_GPS)
createExtension(GPS-EXTENSION "GPS EXTENSIONS" "Enables LibGPS Functionality and the GetGPS processor." "extensions/gps" "${TEST_DIR}/gps-tests")
endif()

option(ENABLE_COAP "Enables the CoAP extension." OFF)
option(ENABLE_COAP "Enables the CoAP extension." ON)
if (ENABLE_ALL OR ENABLE_COAP STREQUAL "ON")
createExtension(COAP-EXTENSION "COAP EXTENSIONS" "Enables LibCOAP Functionality." "extensions/coap" "extensions/coap/tests/")
if( NOT DISABLE_CURL)
add_dependencies(minifi-coap minifi-http-curl)
endif()
add_dependencies(minifi-coap minifi-http-curl)
endif()
endif()

if (WIN32)
Expand Down
4 changes: 2 additions & 2 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"configurations": [
{
"name": "x64-Release",
"generator": "Visual Studio 15 2017",
"generator": "Visual Studio 15 2017 Win64",
"configurationType": "RelWithDebInfo",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
Expand Down Expand Up @@ -73,7 +73,7 @@
},
{
"name": "ENABLE_COAP",
"value": "OFF"
"value": "ON"
},
{
"name": "DISABLE_LIBARCHIVE",
Expand Down
81 changes: 49 additions & 32 deletions extensions/coap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,20 @@ include_directories(protocols nanofi controllerservice server)
include_directories(../http-curl/)

file(GLOB CSOURCES "nanofi/*.c")
file(GLOB SOURCES "*.cpp" "protocols/*.cpp" "processors/*.cpp" "controllerservice/*.cpp" "server/*.cpp" )
file(GLOB SOURCES "*.cpp" "protocols/*.cpp" "controllerservice/*.cpp" "server/*.cpp" )

add_library(nanofi-coap-c STATIC ${CSOURCES})
set(BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(DIR "${BASE_DIR}/thirdparty/libcoap-src")
add_library(minifi-coap STATIC ${SOURCES})
set_property(TARGET minifi-coap PROPERTY POSITION_INDEPENDENT_CODE ON)

if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(minifi-coap "${CMAKE_THREAD_LIBS_INIT}")
endif()
if (NOT WIN32)
set(BYPRODUCT "${BASE_DIR}/thirdparty/libcoap-src/.libs/libcoap-2.a")

set(BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}/extensions/coap")
# determine version of GNUTLSs
set(BYPRODUCT "${BASE_DIR}/extensions/coap/thirdparty/libcoap-src/.libs/libcoap-2.a")
set(DIR "${BASE_DIR}/extensions/coap/thirdparty/libcoap-src")
ExternalProject_Add(
ExternalProject_Add(
coap-external
GIT_REPOSITORY "https://github.com/obgm/libcoap.git"
GIT_TAG "00486a4f46e0278dd24a8ff3411416ff420cde29"
PREFIX "${BASE_DIR}/extensions/coap/thirdparty/libcoap"
GIT_TAG "00486a4f46e0278dd24a8ff3411416ff420cde29"
PREFIX "${BASE_DIR}/thirdparty/libcoap"
BUILD_IN_SOURCE true
SOURCE_DIR "${DIR}"
BUILD_COMMAND make
Expand All @@ -52,27 +47,50 @@ endif()
PATCH_COMMAND ./autogen.sh && ./configure --disable-examples --disable-dtls --disable-tests --disable-documentation
STEP_TARGETS build
EXCLUDE_FROM_ALL TRUE
)
add_definitions("-DWITH_POSIX=1")

add_library(coaplib STATIC IMPORTED)
set_target_properties(coaplib PROPERTIES IMPORTED_LOCATION "${BYPRODUCT}")
add_dependencies(coaplib coap-external)
set(COAP_FOUND "YES" CACHE STRING "" FORCE)
set(COAP_INCLUDE_DIRS "${DIR}/include" CACHE STRING "" FORCE)
set(COAP_LIBRARIES coaplib CACHE STRING "" FORCE)
set(COAP_LIBRARY coaplib CACHE STRING "" FORCE)
set(COAP_LIBRARY coaplib CACHE STRING "" FORCE)
target_link_libraries(minifi-coap ${CMAKE_DL_LIBS})
)
add_definitions("-DWITH_POSIX=1")
add_library(coap STATIC IMPORTED)
set_target_properties(coap PROPERTIES IMPORTED_LOCATION "${BYPRODUCT}")
add_dependencies(coap coap-external)
set(COAP_FOUND "YES" CACHE STRING "" FORCE)
set(COAP_INCLUDE_DIRS "${DIR}/include" CACHE STRING "" FORCE)
include_directories(${COAP_INCLUDE_DIRS})
else()
configure_file(DownloadCoapCMakeLists.txt.in "${CMAKE_CURRENT_BINARY_DIR}/coap_download/CMakeLists.txt" @ONLY)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/libcoap_windows.patch" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/coap_download")
execute_process(COMMAND "${CMAKE_COMMAND}" -G "Visual Studio 15 2017 Win64" . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/coap_download")
execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/coap_download")

include_directories(${COAP_INCLUDE_DIRS})
set(COAP_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/coap_download/libcoap-src")

file(GLOB COAP_SOURCE_FILES "${COAP_SOURCE_DIR}/src/*.c")
list(REMOVE_ITEM COAP_SOURCE_FILES ${COAP_SOURCE_DIR}/src/coap_io_lwip.c)

configure_file(${COAP_SOURCE_DIR}/coap_config.h.windows ${COAP_SOURCE_DIR}/coap_config.h)
configure_file(${COAP_SOURCE_DIR}/include/coap2/coap.h.windows ${COAP_SOURCE_DIR}/include/coap2/coap.h)

add_library(coap STATIC ${COAP_SOURCE_FILES})

target_include_directories(coap PUBLIC "${COAP_SOURCE_DIR}")
target_include_directories(coap PUBLIC "${COAP_SOURCE_DIR}/include")
target_include_directories(coap PUBLIC "${COAP_SOURCE_DIR}/include/coap2")

endif()
set(COAP_LIBRARIES coap CACHE STRING "" FORCE)

add_library(nanofi-coap-c STATIC ${CSOURCES})
add_dependencies(minifi-coap nanofi-coap-c)
add_dependencies(nanofi-coap-c coap)
target_link_libraries(nanofi-coap-c PUBLIC ${COAP_LIBRARIES} ${CMAKE_DL_LIBS})
target_link_libraries(minifi-coap PUBLIC nanofi-coap-c PRIVATE ${CMAKE_DL_LIBS})

if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(minifi-coap PUBLIC "${CMAKE_THREAD_LIBS_INIT}")
endif()

target_link_libraries (nanofi-coap-c ${COAP_LIBRARIES})
target_link_libraries (nanofi-coap-c ${HTTP-CURL})
target_link_libraries (minifi-coap nanofi-coap-c ${COAP_LIBRARIES})
if (WIN32)
set_target_properties(minifi-coap PROPERTIES
LINK_FLAGS "/WHOLEARCHIVE"
LINK_FLAGS "/WHOLEARCHIVE"
)
elseif (APPLE)
set_target_properties(minifi-coap PROPERTIES
Expand All @@ -85,5 +103,4 @@ else ()
endif ()

SET (COAP-EXTENSION minifi-coap PARENT_SCOPE)
register_extension(minifi-coap)

register_extension(minifi-coap)
19 changes: 19 additions & 0 deletions extensions/coap/DownloadCoapCMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.0)
include(ExternalProject)

find_package(Patch REQUIRED)

set(PATCH_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(LIBCOAP_SRC_DIR "${CMAKE_CURRENT_BINARY_DIR}/libcoap-src")

ExternalProject_Add(
libcoap-external
GIT_REPOSITORY "https://github.com/obgm/libcoap.git"
GIT_TAG "d6c25a9a0757af9d13842b7aae9713136e720567"
#GIT_TAG "00486a4f46e0278dd24a8ff3411416ff420cde29"
SOURCE_DIR "${LIBCOAP_SRC_DIR}"
PATCH_COMMAND "${Patch_EXECUTABLE}" "-p1" "-i" "${PATCH_DIR}/libcoap_windows.patch"
BUILD_COMMAND ""
INSTALL_COMMAND ""
CONFIGURE_COMMAND ""
)
13 changes: 13 additions & 0 deletions extensions/coap/libcoap_windows.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/coap_config.h.windows b/coap_config.h.windows
index dbbe509..c281161 100644
--- a/coap_config.h.windows
+++ b/coap_config.h.windows
@@ -67,7 +67,7 @@
#define snprintf _snprintf
#endif

-#define HAVE_OPENSSL 1
+//#define HAVE_OPENSSL 1

/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "libcoap-developers@lists.sourceforge.net"
1 change: 0 additions & 1 deletion extensions/coap/nanofi/coap_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ extern "C" {
#endif

#include <coap2/coap.h>
#include <netdb.h>
#include "coap_message.h"
#include "coap_functions.h"

Expand Down
23 changes: 20 additions & 3 deletions extensions/coap/nanofi/coap_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,22 @@ int create_endpoint_context(coap_context_t **ctx, const char *node, const char *
hints.ai_family = AF_UNSPEC; // ipv4 or ipv6
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;

#ifdef WIN32
WSADATA wsadata;
int err = WSAStartup(MAKEWORD(2, 2), &wsadata);
if (err != 0) {
return NULL;
}
#endif

int getaddrres = getaddrinfo(node, port, &hints, &result);
if (getaddrres != 0) {
perror("getaddrinfo");
return -1;
perror("getaddrinfo");
#ifdef WIN32
WSACleanup();
#endif
return -1;
}

for (interface_itr = result; interface_itr != NULL; interface_itr = interface_itr->ai_next) {
Expand All @@ -105,12 +117,18 @@ int create_endpoint_context(coap_context_t **ctx, const char *node, const char *

if (*ctx && ep_udp) {
freeaddrinfo(result);
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
}
}

freeaddrinfo(result);
#ifdef WIN32
WSACleanup();
#endif
return -2;
}

Expand Down Expand Up @@ -191,7 +209,6 @@ int resolve_address(const struct coap_str_const_t *server, struct sockaddr *dest

if (error != 0) {
perror("getaddrinfo");

return error;
}

Expand Down
11 changes: 6 additions & 5 deletions extensions/coap/nanofi/coap_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
extern "C" {
#endif

#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

typedef unsigned char method_t;

#include "coap2/coap.h"
#include "coap2/uri.h"
#include "coap2/address.h"
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
#include "coap_message.h"


typedef struct {
void (*data_received)(void *receiver_context, struct coap_context_t *ctx, CoapMessage *const);
void (*received_error)(void *receiver_context, struct coap_context_t *ctx, unsigned int code);
Expand Down
1 change: 1 addition & 0 deletions extensions/coap/nanofi/coap_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CoapServerContext * const create_server(const char * const server_hostname, cons
memset(server, 0x00, sizeof(CoapServerContext));
if (create_endpoint_context(&server->ctx, server_hostname, port)) {
free_server(server);
return NULL;
}

return server;
Expand Down
4 changes: 4 additions & 0 deletions extensions/coap/protocols/CoapC2Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
#include "coap2/uri.h"
#include "coap2/address.h"
#include <stdio.h>

#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include <string.h>
#include "protocols/RESTSender.h"

Expand Down
4 changes: 4 additions & 0 deletions extensions/coap/server/CoapServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace nifi {
namespace minifi {
namespace coap {

#ifdef WIN32
#undef DELETE
#endif

enum METHOD {
GET,
POST,
Expand Down
3 changes: 3 additions & 0 deletions extensions/coap/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
#

if (NOT WIN32)
file(GLOB CURL_UNIT_TESTS "unit/*.cpp")
file(GLOB CURL_INTEGRATION_TESTS "*.cpp")

Expand Down Expand Up @@ -58,3 +59,5 @@ add_test(NAME CoapC2VerifyHeartbeat COMMAND CoapC2VerifyHeartbeat "${TEST_RESOUR
else()
message("-- CoAP tests rely on minifi-http-curl, so they will not be built...")
endif()

endif()

0 comments on commit fc2b828

Please sign in to comment.