Skip to content

Commit 1a3b6f6

Browse files
Nancy Zhangclaude
andcommitted
feat: Add libcbv2g_json_wrapper core + AppHandshake converter
Add a native C shared library (libcbv2g_json_wrapper) that provides a JSON-based API for EXI encoding/decoding via libcbv2g. This replaces the Java-based EXIficient codec dependency for EV simulation, eliminating the JRE requirement for embedded/Yocto deployments. This initial PR establishes the architecture and includes: - cbv2g_json_wrapper.h: Public API with error codes and namespace constants - cbv2g_json_wrapper.c: Dispatch logic, version/error functions - json_utils.c/.h: Shared JSON helper utilities (base64, hex, accessors) - apphand_converter.c: AppHandshake (SAP) protocol converter - converters.h: Internal converter declarations - CMakeLists.txt: Build system supporting both system-installed libcbv2g (Yocto) and source-tree builds - Bundled cJSON 1.7.19 (MIT license) in third_party/ The AppHandshake converter is included as the smallest protocol to validate the approach. DIN 70121, ISO 15118-2, and ISO 15118-20 converters will follow in subsequent PRs. Refs: EVerest/EVerest#118, EcoG-io#157, everest-demo#129 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Nancy Zhang <nancy.zhang@enteligent.com>
1 parent 7b89fa8 commit 1a3b6f6

File tree

9 files changed

+4480
-0
lines changed

9 files changed

+4480
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(cbv2g_json_wrapper VERSION 1.0.0 LANGUAGES C)
3+
4+
# Set C standard
5+
set(CMAKE_C_STANDARD 99)
6+
set(CMAKE_C_STANDARD_REQUIRED ON)
7+
8+
# Build as shared library
9+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
10+
11+
# Try to find libcbv2g via CMake package first (Yocto build)
12+
find_package(cbv2g QUIET)
13+
14+
if(cbv2g_FOUND)
15+
message(STATUS "Found libcbv2g via find_package")
16+
set(USE_SYSTEM_LIBCBV2G ON)
17+
else()
18+
message(STATUS "libcbv2g package not found, looking for source directory")
19+
set(USE_SYSTEM_LIBCBV2G OFF)
20+
21+
# Find libcbv2g source directory
22+
set(LIBCBV2G_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../../libcbv2g"
23+
CACHE PATH "Path to libcbv2g source directory")
24+
25+
if(NOT EXISTS "${LIBCBV2G_ROOT}/include/cbv2g")
26+
message(FATAL_ERROR "libcbv2g not found. Either install the cbv2g package "
27+
"or set LIBCBV2G_ROOT to the source directory.")
28+
endif()
29+
endif()
30+
31+
# cJSON library (embedded)
32+
add_library(cjson STATIC
33+
third_party/cJSON/cJSON.c
34+
)
35+
target_include_directories(cjson PUBLIC
36+
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
37+
)
38+
39+
# Wrapper sources — only AppHandshake converter for now.
40+
# DIN, ISO-2, and ISO-20 converters will be added in subsequent PRs.
41+
set(WRAPPER_SOURCES
42+
src/cbv2g_json_wrapper.c
43+
src/json_utils.c
44+
src/apphand_converter.c
45+
)
46+
47+
if(USE_SYSTEM_LIBCBV2G)
48+
# Using system-installed libcbv2g
49+
add_library(cbv2g_json_wrapper SHARED ${WRAPPER_SOURCES})
50+
51+
target_include_directories(cbv2g_json_wrapper
52+
PUBLIC
53+
${CMAKE_CURRENT_SOURCE_DIR}/include
54+
PRIVATE
55+
${CMAKE_CURRENT_SOURCE_DIR}/src
56+
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
57+
)
58+
59+
target_link_libraries(cbv2g_json_wrapper
60+
PRIVATE
61+
cjson
62+
cbv2g::cbv2g
63+
)
64+
else()
65+
# Build libcbv2g sources directly into our library
66+
set(CBVG_COMMON_SOURCES
67+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes_decoder.c
68+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes_encoder.c
69+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_basetypes.c
70+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_bitstream.c
71+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_header.c
72+
${LIBCBV2G_ROOT}/lib/cbv2g/common/exi_types_decoder.c
73+
)
74+
75+
set(CBVG_APPHAND_SOURCES
76+
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Datatypes.c
77+
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Decoder.c
78+
${LIBCBV2G_ROOT}/lib/cbv2g/app_handshake/appHand_Encoder.c
79+
)
80+
81+
set(CBVG_TP_SOURCES
82+
${LIBCBV2G_ROOT}/lib/cbv2g/exi_v2gtp.c
83+
)
84+
85+
add_library(cbv2g_json_wrapper SHARED
86+
${CBVG_COMMON_SOURCES}
87+
${CBVG_APPHAND_SOURCES}
88+
${CBVG_TP_SOURCES}
89+
${WRAPPER_SOURCES}
90+
)
91+
92+
target_include_directories(cbv2g_json_wrapper
93+
PUBLIC
94+
${CMAKE_CURRENT_SOURCE_DIR}/include
95+
PRIVATE
96+
${CMAKE_CURRENT_SOURCE_DIR}/src
97+
${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
98+
${LIBCBV2G_ROOT}/include
99+
)
100+
101+
target_link_libraries(cbv2g_json_wrapper
102+
PRIVATE
103+
cjson
104+
)
105+
endif()
106+
107+
# Set library version
108+
set_target_properties(cbv2g_json_wrapper PROPERTIES
109+
VERSION ${PROJECT_VERSION}
110+
SOVERSION ${PROJECT_VERSION_MAJOR}
111+
)
112+
113+
# Install rules
114+
install(TARGETS cbv2g_json_wrapper
115+
LIBRARY DESTINATION lib
116+
ARCHIVE DESTINATION lib
117+
)
118+
119+
install(FILES include/cbv2g_json_wrapper.h
120+
DESTINATION include
121+
)
122+
123+
# Optional: Build test executable
124+
option(BUILD_TESTS "Build test executable" OFF)
125+
if(BUILD_TESTS)
126+
add_executable(cbv2g_wrapper_test
127+
tests/test_main.c
128+
)
129+
target_link_libraries(cbv2g_wrapper_test
130+
cbv2g_json_wrapper
131+
)
132+
endif()
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2024 Contributors to EVerest
4+
*
5+
* cbv2g_json_wrapper.h - JSON-based API wrapper for libcbv2g
6+
*
7+
* This library provides a JSON-based interface to libcbv2g's EXI encoding/decoding
8+
* functionality, designed to replace the Java-based EXIficient codec in Josev.
9+
*/
10+
11+
#ifndef CBV2G_JSON_WRAPPER_H
12+
#define CBV2G_JSON_WRAPPER_H
13+
14+
#include <stdint.h>
15+
#include <stddef.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/* Version information */
22+
#define CBV2G_JSON_WRAPPER_VERSION_MAJOR 1
23+
#define CBV2G_JSON_WRAPPER_VERSION_MINOR 0
24+
#define CBV2G_JSON_WRAPPER_VERSION_PATCH 0
25+
#define CBV2G_JSON_WRAPPER_VERSION "1.0.0"
26+
27+
/* Error codes */
28+
#define CBV2G_SUCCESS 0
29+
#define CBV2G_ERROR_INVALID_PARAM -1
30+
#define CBV2G_ERROR_BUFFER_TOO_SMALL -2
31+
#define CBV2G_ERROR_ENCODING_FAILED -3
32+
#define CBV2G_ERROR_DECODING_FAILED -4
33+
#define CBV2G_ERROR_UNKNOWN_NAMESPACE -5
34+
#define CBV2G_ERROR_JSON_PARSE -6
35+
#define CBV2G_ERROR_JSON_GENERATE -7
36+
#define CBV2G_ERROR_UNKNOWN_MESSAGE -8
37+
#define CBV2G_ERROR_INTERNAL -9
38+
39+
/* Namespace constants matching Josev's Namespace enum */
40+
#define NS_DIN_MSG_DEF "urn:din:70121:2012:MsgDef"
41+
#define NS_ISO_V2_MSG_DEF "urn:iso:15118:2:2013:MsgDef"
42+
#define NS_ISO_V20_BASE "urn:iso:std:iso:15118:-20"
43+
#define NS_ISO_V20_COMMON_MSG "urn:iso:std:iso:15118:-20:CommonMessages"
44+
#define NS_ISO_V20_AC "urn:iso:std:iso:15118:-20:AC"
45+
#define NS_ISO_V20_DC "urn:iso:std:iso:15118:-20:DC"
46+
#define NS_ISO_V20_WPT "urn:iso:std:iso:15118:-20:WPT"
47+
#define NS_ISO_V20_ACDP "urn:iso:std:iso:15118:-20:ACDP"
48+
#define NS_SAP "urn:iso:15118:2:2010:AppProtocol"
49+
50+
/**
51+
* @brief Encode a JSON message to EXI format
52+
*
53+
* @param json_message JSON string containing the message to encode
54+
* @param namespace Namespace string identifying the protocol/schema
55+
* @param output_buffer Buffer to receive the EXI-encoded bytes
56+
* @param buffer_size Size of the output buffer
57+
* @param output_length Pointer to receive the actual length of encoded data
58+
*
59+
* @return CBV2G_SUCCESS on success, negative error code on failure
60+
*/
61+
int cbv2g_encode(const char* json_message,
62+
const char* namespace,
63+
uint8_t* output_buffer,
64+
size_t buffer_size,
65+
size_t* output_length);
66+
67+
/**
68+
* @brief Decode EXI data to JSON format
69+
*
70+
* @param exi_data EXI-encoded byte array
71+
* @param exi_length Length of the EXI data
72+
* @param namespace Namespace string identifying the protocol/schema
73+
* @param output_json Buffer to receive the JSON string
74+
* @param buffer_size Size of the output buffer
75+
*
76+
* @return CBV2G_SUCCESS on success, negative error code on failure
77+
*/
78+
int cbv2g_decode(const uint8_t* exi_data,
79+
size_t exi_length,
80+
const char* namespace,
81+
char* output_json,
82+
size_t buffer_size);
83+
84+
/**
85+
* @brief Get the version string of this library
86+
*
87+
* @return Version string (e.g., "1.0.0")
88+
*/
89+
const char* cbv2g_get_version(void);
90+
91+
/**
92+
* @brief Get the last error message
93+
*
94+
* @return Error message string, or empty string if no error
95+
*/
96+
const char* cbv2g_get_last_error(void);
97+
98+
/**
99+
* @brief Clear the last error message
100+
*/
101+
void cbv2g_clear_error(void);
102+
103+
#ifdef __cplusplus
104+
}
105+
#endif
106+
107+
#endif /* CBV2G_JSON_WRAPPER_H */

0 commit comments

Comments
 (0)