Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions iso15118/shared/cbv2g_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
cmake_minimum_required(VERSION 3.14)
project(cbv2g_json_wrapper VERSION 1.0.0 LANGUAGES C)

# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Build as shared library
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Try to find libcbv2g via CMake package first (Yocto build)
find_package(cbv2g QUIET)

if(cbv2g_FOUND)
message(STATUS "Found libcbv2g via find_package")
set(USE_SYSTEM_LIBCBV2G ON)
else()
message(STATUS "libcbv2g package not found, looking for source directory")
set(USE_SYSTEM_LIBCBV2G OFF)

# Find libcbv2g source directory
set(LIBCBV2G_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../../libcbv2g"
CACHE PATH "Path to libcbv2g source directory")

if(NOT EXISTS "${LIBCBV2G_ROOT}/include/cbv2g")
message(FATAL_ERROR "libcbv2g not found. Either install the cbv2g package "
"or set LIBCBV2G_ROOT to the source directory.")
endif()
endif()

# cJSON library (embedded)
add_library(cjson STATIC
third_party/cJSON/cJSON.c
)
target_include_directories(cjson PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
)

# Wrapper sources — only AppHandshake converter for now.
# DIN, ISO-2, and ISO-20 converters will be added in subsequent PRs.
set(WRAPPER_SOURCES
src/cbv2g_json_wrapper.c
src/json_utils.c
src/apphand_converter.c
)

if(USE_SYSTEM_LIBCBV2G)
# Using system-installed libcbv2g
add_library(cbv2g_json_wrapper SHARED ${WRAPPER_SOURCES})

target_include_directories(cbv2g_json_wrapper
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
)

target_link_libraries(cbv2g_json_wrapper
PRIVATE
cjson
cbv2g::cbv2g
)
else()
# Build libcbv2g sources directly into our library
set(CBVG_COMMON_SOURCES
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes_decoder.c
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes_encoder.c
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes.c
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_bitstream.c
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_header.c
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_types_decoder.c
)

set(CBVG_APPHAND_SOURCES
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Datatypes.c
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Decoder.c
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Encoder.c
)

set(CBVG_TP_SOURCES
${LIBCBV2G_ROOT}/lib/cbv2g/exi_v2gtp.c
)

add_library(cbv2g_json_wrapper SHARED
${CBVG_COMMON_SOURCES}
${CBVG_APPHAND_SOURCES}
${CBVG_TP_SOURCES}
${WRAPPER_SOURCES}
)

target_include_directories(cbv2g_json_wrapper
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
${LIBCBV2G_ROOT}/include
)

target_link_libraries(cbv2g_json_wrapper
PRIVATE
cjson
)
endif()

# Set library version
set_target_properties(cbv2g_json_wrapper PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)

# Install rules
install(TARGETS cbv2g_json_wrapper
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)

install(FILES include/cbv2g_json_wrapper.h
DESTINATION include
)

# Optional: Build test executable
option(BUILD_TESTS "Build test executable" OFF)
if(BUILD_TESTS)
add_executable(cbv2g_wrapper_test
tests/test_main.c
)
target_link_libraries(cbv2g_wrapper_test
cbv2g_json_wrapper
)
endif()
107 changes: 107 additions & 0 deletions iso15118/shared/cbv2g_wrapper/include/cbv2g_json_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2024 Contributors to EVerest
*
* cbv2g_json_wrapper.h - JSON-based API wrapper for libcbv2g
*
* This library provides a JSON-based interface to libcbv2g's EXI encoding/decoding
* functionality, designed to replace the Java-based EXIficient codec in Josev.
*/

#ifndef CBV2G_JSON_WRAPPER_H
#define CBV2G_JSON_WRAPPER_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Version information */
#define CBV2G_JSON_WRAPPER_VERSION_MAJOR 1
#define CBV2G_JSON_WRAPPER_VERSION_MINOR 0
#define CBV2G_JSON_WRAPPER_VERSION_PATCH 0
#define CBV2G_JSON_WRAPPER_VERSION "1.0.0"

/* Error codes */
#define CBV2G_SUCCESS 0
#define CBV2G_ERROR_INVALID_PARAM -1
#define CBV2G_ERROR_BUFFER_TOO_SMALL -2
#define CBV2G_ERROR_ENCODING_FAILED -3
#define CBV2G_ERROR_DECODING_FAILED -4
#define CBV2G_ERROR_UNKNOWN_NAMESPACE -5
#define CBV2G_ERROR_JSON_PARSE -6
#define CBV2G_ERROR_JSON_GENERATE -7
#define CBV2G_ERROR_UNKNOWN_MESSAGE -8
#define CBV2G_ERROR_INTERNAL -9

/* Namespace constants matching Josev's Namespace enum */
#define NS_DIN_MSG_DEF "urn:din:70121:2012:MsgDef"
#define NS_ISO_V2_MSG_DEF "urn:iso:15118:2:2013:MsgDef"
#define NS_ISO_V20_BASE "urn:iso:std:iso:15118:-20"
#define NS_ISO_V20_COMMON_MSG "urn:iso:std:iso:15118:-20:CommonMessages"
#define NS_ISO_V20_AC "urn:iso:std:iso:15118:-20:AC"
#define NS_ISO_V20_DC "urn:iso:std:iso:15118:-20:DC"
#define NS_ISO_V20_WPT "urn:iso:std:iso:15118:-20:WPT"
#define NS_ISO_V20_ACDP "urn:iso:std:iso:15118:-20:ACDP"
#define NS_SAP "urn:iso:15118:2:2010:AppProtocol"

/**
* @brief Encode a JSON message to EXI format
*
* @param json_message JSON string containing the message to encode
* @param namespace Namespace string identifying the protocol/schema
* @param output_buffer Buffer to receive the EXI-encoded bytes
* @param buffer_size Size of the output buffer
* @param output_length Pointer to receive the actual length of encoded data
*
* @return CBV2G_SUCCESS on success, negative error code on failure
*/
int cbv2g_encode(const char* json_message,
const char* namespace,
uint8_t* output_buffer,
size_t buffer_size,
size_t* output_length);

/**
* @brief Decode EXI data to JSON format
*
* @param exi_data EXI-encoded byte array
* @param exi_length Length of the EXI data
* @param namespace Namespace string identifying the protocol/schema
* @param output_json Buffer to receive the JSON string
* @param buffer_size Size of the output buffer
*
* @return CBV2G_SUCCESS on success, negative error code on failure
*/
int cbv2g_decode(const uint8_t* exi_data,
size_t exi_length,
const char* namespace,
char* output_json,
size_t buffer_size);

/**
* @brief Get the version string of this library
*
* @return Version string (e.g., "1.0.0")
*/
const char* cbv2g_get_version(void);

/**
* @brief Get the last error message
*
* @return Error message string, or empty string if no error
*/
const char* cbv2g_get_last_error(void);

/**
* @brief Clear the last error message
*/
void cbv2g_clear_error(void);

#ifdef __cplusplus
}
#endif

#endif /* CBV2G_JSON_WRAPPER_H */
Loading