Skip to content

Commit

Permalink
Network Codec support.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (CC/EMT2) <[email protected]>
  • Loading branch information
timrulebosch committed Oct 26, 2023
1 parent f4ddc57 commit a3e2a8d
Show file tree
Hide file tree
Showing 18 changed files with 807 additions and 47 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ super-linter:
--env RUN_LOCAL=true \
--env DEFAULT_BRANCH=main \
--env IGNORE_GITIGNORED_FILES=true \
--env FILTER_REGEX_EXCLUDE="(dse/modelc/examples/apis/.*|doc/content/apis/modelc/examples/.*)" \
--env VALIDATE_CPP=true \
--env VALIDATE_YAML=true \
github/super-linter:slim-v5
Expand Down
6 changes: 3 additions & 3 deletions doc/content/docs/devel/modelc_debug.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: "Model C Debug Techniques"
linkTitle: "Debug"
linkTitle: "ModelC Debug"
draft: false
tags:
- Developer
- ModelC
- GDB
github_repo: "https://github.com/boschglobal/dse.modelc"
github_subdir: "doc/content/developer"
path_base_for_github_subdir: "content/docs/developer/"
github_subdir: "doc"
path_base_for_github_subdir: "content/docs/devel/"
---

## GDB
Expand Down
103 changes: 103 additions & 0 deletions doc/content/docs/devel/modelc_ncodec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: "Model C with Network Codec"
linkTitle: "ModelC NCodec"
draft: false
tags:
- Developer
- ModelC
github_repo: "https://github.com/boschglobal/dse.modelc"
github_subdir: "doc"
path_base_for_github_subdir: "content/docs/devel/"
---

## Network Codec

The Model C Library integrates the [DSE Network Codec](https://github.com/boschglobal/dse.standards/tree/main/dse/ncodec) implementation of the [Automotive Bus schemas](https://github.com/boschglobal/automotive-bus-schema).


### Build Integration

The Network Codec integration repackages the necessary include files with the Model C Library packages. No additional build integration is required.


### Configuration of Binary Signals

Binary signals are configured with a MIME Type. The Signal Vector integration with the Network Codec will automatically open `codec` objects for each supported MIME Type that is supported (by the codec). Additional configuration of the `codec` objects can be done with the `ncodec_config()` API function.

```yaml
kind: SignalGroup
metadata:
name: network
labels:
channel: network_vector
annotations:
vector_type: binary
spec:
signals:
- signal: can_bus
annotations:
mime_type: application/x-automotive-bus; interface=stream; type=frame; bus=can; schema=fbs; bus_id=1; node_id=2; interface_id=3
```
Additional configuration information is available [here](https://github.com/boschglobal/dse.standards/blob/main/dse/ncodec/libs/automotive-bus/README.md). Especially the behaviour of `bus_id`,`node_id`and `interface_id` configuration items are described.


Configuration items can also be set at runtime with the `ncodec_config()` API as the following example shows:

```c
#include <dse/modelc/model.h>
#include <dse/ncodec/codec.h>
static void _setup_node_id(SignalVector* sv, uint32_t idx)
{
const char* v = sv->annotation(sv, idx, "node_id");
if (v) {
NCODEC* nc = sv->codec(sv, idx);
ncodec_config(nc, (struct NCodecConfigItem){
.name = "node_id",
.value = v,
});
}
}
```

### Usage in Model Code

The Network Codec integration is fairly easy to use. The general approach is as follows:

```c
#include <dse/modelc/model.h>
#include <dse/ncodec/codec.h>
void do_bus_rx(SignalVector* sv, uint32_t idx)
{
NCODEC* nc = sv->codec(sv, idx);
while (1) {
NCodecMessage msg = {};
len = ncodec_read(nc, &msg);
if (len < 0) break;
put_rx_frame_to_queue(msg.frame_id, msg.buffer, msg.len);
}
}
void do_bus_tx(SignalVector* sv, uint32_t idx)
{
uint32_t id;
uint8_t* msg;
size_t len;
NCODEC* nc = sv->codec(sv, idx);
while (get_tx_frame_from_queue(&id, &msg, &len)) {
ncodec_write(nc, &(struct NCodecMessage){
.frame_id = id,
.buffer = msg,
.len = len,
});
}
ncodec_flush(nc);
}
```
28 changes: 28 additions & 0 deletions dse/modelc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ set(DSE_CLIB_SOURCE_FILES
set(DSE_CLIB_INCLUDE_DIR "${DSE_CLIB_SOURCE_DIR}/..")


# External Project - DSE Network Codec
# ------------------------------------
set(DSE_NCODEC_SOURCE_DIR "$ENV{EXTERNAL_BUILD_DIR}/dse_ncodec")
set(DSE_NCODEC_BINARY_DIR "$ENV{EXTERNAL_BUILD_DIR}/dse_ncodec")
find_library(DSE_NCODEC_LIB
NAMES
libautomotive-bus-codec.a
PATHS
${DSE_NCODEC_BINARY_DIR}
REQUIRED
NO_DEFAULT_PATH
)
add_library(dse_ncodec STATIC IMPORTED GLOBAL)
set_target_properties(dse_ncodec
PROPERTIES
IMPORTED_LOCATION "${DSE_NCODEC_LIB}"
INTERFACE_INCLUDE_DIRECTORIES "${DSE_NCODEC_SOURCE_DIR}"
)
set(DSE_NCODEC_INCLUDE_DIR "${DSE_NCODEC_SOURCE_DIR}")


# External Projects (targets common to sub projects)
# =================
Expand Down Expand Up @@ -274,6 +294,14 @@ install(
PATTERN "*fmi*" EXCLUDE
PATTERN "*process*" EXCLUDE
)
install(
FILES
${DSE_NCODEC_SOURCE_DIR}/dse/ncodec/codec.h
DESTINATION
${CMAKE_INSTALL_INCLUDEDIR}/dse/ncodec
COMPONENT
modelc
)
install(
FILES
${DSE_MODELC_PUBLIC_HEADERS}
Expand Down
4 changes: 4 additions & 0 deletions dse/modelc/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ typedef int (*BinarySignalAppendFunc)(
SignalVector* sv, uint32_t index, void* data, uint32_t len);
typedef int (*BinarySignalResetFunc)(SignalVector* sv, uint32_t index);
typedef int (*BinarySignalReleaseFunc)(SignalVector* sv, uint32_t index);
typedef void* (*BinarySignalCodecFunc)(SignalVector* sv, uint32_t index);
typedef const char* (*SignalAnnotationGetFunc)(
SignalVector* sv, uint32_t index, const char* name);

Expand All @@ -168,6 +169,7 @@ typedef struct SignalVectorVTable {
BinarySignalResetFunc reset;
BinarySignalReleaseFunc release;
SignalAnnotationGetFunc annotation;
BinarySignalCodecFunc codec;
} SignalVectorVTable;

typedef struct SignalVector {
Expand All @@ -187,13 +189,15 @@ typedef struct SignalVector {
uint32_t* length; /* Length of binary object. */
uint32_t* buffer_size; /* Size of allocated buffer. */
const char** mime_type;
void** ncodec; /* Network Codec objects. */
};
};
/* Helper functions. */
BinarySignalAppendFunc append;
BinarySignalResetFunc reset;
BinarySignalReleaseFunc release;
SignalAnnotationGetFunc annotation;
BinarySignalCodecFunc codec;
/* Reference data. */
ModelInstanceSpec* mi;
} SignalVector;
Expand Down
4 changes: 4 additions & 0 deletions dse/modelc/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ project(ModelC_Model_API)
add_library(model_api OBJECT
gateway.c
model.c
ncodec.c
schema.c
signal.c
${DSE_NCODEC_SOURCE_DIR}/dse/ncodec/codec.c
)
target_include_directories(model_api
PRIVATE
${DSE_CLIB_INCLUDE_DIR}
${DSE_NCODEC_INCLUDE_DIR}
${YAML_SOURCE_DIR}/include
../../..
)
target_link_libraries(model_api
PRIVATE
dse_ncodec
yaml
m
$<$<BOOL:${WIN32}>:ws2_32>
Expand Down
6 changes: 4 additions & 2 deletions dse/modelc/model/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ void model_function_destroy(ModelFunction* model_function)
if (_mfc && _mfc->signal_value_double)
free(_mfc->signal_value_double);
if (_mfc && _mfc->signal_value_binary) {
for (uint32_t _ = 0; _ < _mfc->signal_count; _++)
free((void*)_mfc->signal_value_binary[_]);
for (uint32_t _ = 0; _ < _mfc->signal_count; _++) {
if ((void*)_mfc->signal_value_binary[_])
free((void*)_mfc->signal_value_binary[_]);
}
free(_mfc->signal_value_binary);
}
if (_mfc && _mfc->signal_value_binary_size)
Expand Down
Loading

0 comments on commit a3e2a8d

Please sign in to comment.