Skip to content

Commit

Permalink
Merge pull request #8 from timrulebosch/main
Browse files Browse the repository at this point in the history
Add example for the new Stream Interface.
  • Loading branch information
TGoeppel authored Nov 15, 2022
2 parents 32b9af0 + 0e7de53 commit 0b30504
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 15 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

###############
## Builder Images
BUILDER_DOCKER_IMAGES = flatc-builder python-builder
BUILDER_DOCKER_IMAGES = flatc-builder python-builder gcc-builder
FLATC_BUILDER_IMAGE ?= flatc-builder:latest
PYTHON_BUILDER_IMAGE ?= python-builder:latest
GCC_BUILDER_IMAGE ?= gcc-builder:latest


###############
Expand Down Expand Up @@ -89,11 +90,13 @@ $(FBS_SCHEMA_SOURCES):
mkdir -p $(FBS_OUT_DIR)/fbs/$$(basename $$(dirname $@))
mkdir -p $(FBS_OUT_DIR)/fbs/$$(basename $$(dirname $@))
mkdir -p $(FBS_OUT_DIR)/fbs/$$(basename $$(dirname $@))
mkdir -p $(FBS_OUT_DIR)/lua

# Generate Flatbuffers code.
$(FLATCC) -a $(FLATC_OPTIONS) -o $(FBS_OUT_DIR)/c/$(SCHEMA_LIB)/$$(basename $$(dirname $@)) $@
$(FLATC) --cpp $(FLATC_OPTIONS) --filename-suffix '' -o $(FBS_OUT_DIR)/cpp/$(SCHEMA_LIB)/$$(basename $$(dirname $@)) $@
$(FLATC) --python $(FLATC_OPTIONS) -o $(FBS_OUT_DIR)/python $@
$(FLATC) --lua $(FLATC_OPTIONS) -o $(FBS_OUT_DIR)/lua $@
cd $(FBS_OUT_DIR)/lua; $(FLATC) --lua $(FLATC_OPTIONS) $@
# Copy over the original Flatbuffer schemas.
cp $@ $(FBS_OUT_DIR)/fbs/$$(basename $$(dirname $@))

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The schemas can be compiled using a containerized build toolchain as follows:
$ git clone https://github.com/boschglobal/automotive-bus-schema.git
$ cd automotive-bus-schema

# Build the toolchain.
# Build the toolchains.
$ make builders
...
$ docker images
Expand Down Expand Up @@ -69,6 +69,14 @@ Note that for C language the [flatcc compiler](https://google.github.io/flatbuff
After compiling the output can be integrated in the simulation model code. Please refer to the [FlatBuffers Tutorial](https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html) for example usage of compiled code, e.g. how to read or write data structures defined in a schema.


### Examples

This repository includes the following examples:

* [cmake](doc/examples/cmake.md) - An example of a CMake based project with the Signal Schema and MsgPack.
* [streams](doc/examples/streams.md) - How to use the Stream Interface Frame Schema with FlatBuffers.


## Dependencies

Schemas in this repository require:
Expand Down
73 changes: 73 additions & 0 deletions doc/examples/streams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!---
Copyright (c) 2022 for information on the respective copyright owner
see the NOTICE file and/or the repository https://github.com/boschglobal/automotive-bus-schema
SPDX-License-Identifier: Apache-2.0
-->


# Example: Stream Interface - Frame Schema (with FlatBuffers)

## Build the Toolchain Container Images

> Note: This may take some time.

```bash
$ git clone https://github.com/boschglobal/automotive-bus-schema.git
$ cd automotive-bus-schema
$ make builders
...
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-builder latest a5ef73ea66dc 16 minutes ago 858MB
flatc-builder latest f4bb83e2c0c9 18 minutes ago 324MB
gcc-builder latest 7f15adf64b0a 5 seconds ago 620MB
```


## Build and package the schemas

```bash
$ make
...
$ make dist
Distribution package files:
--------------------------
204K /vagrant/git/oss/automotive-bus-schema/dist/automotive-bus-schema.tar.gz
36K /vagrant/git/oss/automotive-bus-schema/dist/python/dist/automotive_bus_schema-devel-py3-none-any.whl
12K /vagrant/git/oss/automotive-bus-schema/dist/python/dist/automotive-bus-schema-devel.tar.gz
```


## Build and run the example

```bash
$ cd examples/streams
$ make
...
Sandbox files: - /tmp/repo/examples/streams/build/_out
--------------
312K build/_out/bin/frames

# Run the target executable.
$ make run
Process stream (length = 136)
Sending Node UID is: 49
CAN Frame:
frame_id = 42
payload = 66 6f 6f
length = 3
frame_type = 0
bus_id = 1
node_id = 0
interface_id = 0
CAN Frame:
frame_id = 24
payload = 62 61 72
length = 3
frame_type = 0
bus_id = 1
node_id = 0
interface_id = 0
```
43 changes: 43 additions & 0 deletions docker/gcc-builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM debian:bullseye-slim AS base


# Setup basic GCC and CMAKE toolchains
# ====================================
RUN apt-get -y update; \
apt-get -y upgrade; \
apt-get -y install --no-install-recommends \
autoconf \
automake \
binutils \
build-essential \
ca-certificates \
extra-cmake-modules \
curl \
gcc \
gcc-multilib \
g++ \
g++-multilib \
git \
jq \
less \
libtool \
make \
zip \
&& \
apt-get clean; \
rm -rf /var/lib/apt/lists/*

RUN curl -fSL https://github.com/Kitware/CMake/releases/download/v3.22.2/cmake-3.22.2-linux-x86_64.sh \
-o /tmp/cmake-install.sh \
&& chmod u+x /tmp/cmake-install.sh \
&& /tmp/cmake-install.sh --skip-license --prefix=/usr/local \
&& rm /tmp/cmake-install.sh



# =========================
# Construct the final image
# =========================
FROM base AS final

# CMD ["/bin/bash", "-c", "tail /dev/null -f"]
38 changes: 38 additions & 0 deletions examples/streams/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.21)

project(stream-frames-example)

include(GNUInstallDirs)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/_out)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -ggdb")
list(APPEND C_CXX_WARNING_FLAGS
-Wall
-W
-Wwrite-strings
-Wno-missing-field-initializers
-Wno-misleading-indentation
)
add_compile_options(${C_CXX_WARNING_FLAGS})

set(SCHEMAS_SOURCE_DIR ../../dist/automotive-bus-schema/flatbuffers/c)
set(FLATCC_SOURCE_DIR ${SCHEMAS_SOURCE_DIR}/automotive_bus_schema/flatcc/src)
set(FLATCC_INCLUDE_DIR ${SCHEMAS_SOURCE_DIR}/automotive_bus_schema/flatcc/include)


add_executable(frames
frames.c
${FLATCC_SOURCE_DIR}/builder.c
${FLATCC_SOURCE_DIR}/emitter.c
${FLATCC_SOURCE_DIR}/refmap.c
)
target_include_directories(frames
PRIVATE
${SCHEMAS_SOURCE_DIR}
${FLATCC_INCLUDE_DIR}
)
install(TARGETS frames)
38 changes: 38 additions & 0 deletions examples/streams/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) 2022 Robert Bosch GmbH

GCC_BUILDER_IMAGE ?= gcc-builder:latest

ifneq ($(CI), true)
DOCKER_BUILDER_CMD := docker run -it --rm \
--volume $$(pwd)/../..:/tmp/repo \
--workdir /tmp/repo/examples/streams \
$(GCC_BUILDER_IMAGE)
endif

.PHONY: build clean do-build run

default: build

build:
@${DOCKER_BUILDER_CMD} $(MAKE) do-build

do-build:
# Build from scratch if no build dir.
@if [ ! -d "build" ]; then \
mkdir -p build; \
cd build; \
cmake .. ; \
fi
# Build incremental.
cd build; make
cd build; make install
@echo ""
@echo "Sandbox files: - $$(pwd)/build/_out"
@echo "--------------"
@find build/_out/ -type f -name '*' -exec ls -sh --color=auto {} \;

run:
@build/_out/bin/frames

clean:
rm -rf build
Loading

0 comments on commit 0b30504

Please sign in to comment.