Skip to content

Commit

Permalink
NCodec support for CAN FD.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Feb 2, 2024
1 parent 4ebf80e commit bf9dfcd
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 79 deletions.
5 changes: 3 additions & 2 deletions doc/content/apis/ncodec/examples/ncodec_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ int main(int argc, char* argv[])
.name = "name", .value = "simple network codec" });

/* Write a message to the Network Codec. */
ncodec_write(nc, &(struct NCodecMessage){ .frame_id = 42,
ncodec_write(nc, &(struct NCodecCanMessage){ .frame_id = 42,
.frame_type = CAN_EXTENDED_FRAME,
.buffer = (uint8_t*)greeting,
.len = strlen(greeting) });
ncodec_flush(nc);
Expand All @@ -47,7 +48,7 @@ int main(int argc, char* argv[])
stream_seek(nc, 0, NCODEC_SEEK_SET);

/* Read the response from the Network Codec. */
NCodecMessage msg = {};
NCodecCanMessage msg = {};
rc = ncodec_read(nc, &msg);
if (rc > 0) {
printf("Message is: %s\n", (char*)msg.buffer);
Expand Down
32 changes: 17 additions & 15 deletions doc/content/apis/ncodec/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ title Network Codec
package "Model Environment" {
interface "Signal Interface" as Sif
component "Model" as foo {
component "Model" as Model
interface "CodecVTable" as Cvt
component "Codec" as Codec
component "Model" as Model
interface "CodecVTable" as Cvt
component "Codec" as Codec
interface "StreamVTable" as Svt
}
component "Stream" as Stream
Expand Down Expand Up @@ -73,6 +73,17 @@ center footer Dynamic Simulation Environment

## Typedefs

### NCodecCanMessage

```c
typedef struct NCodecCanMessage {
uint32_t frame_id;
uint8_t* buffer;
size_t len;
NCodecCanFrameType frame_type;
}
```

### NCodecConfigItem

```c
Expand All @@ -92,16 +103,6 @@ typedef struct NCodecInstance {
}
```

### NCodecMessage

```c
typedef struct NCodecMessage {
uint32_t frame_id;
uint8_t* buffer;
size_t len;
}
```

### NCodecStreamVTable

```c
Expand Down Expand Up @@ -273,7 +274,8 @@ nc (NCODEC*)

msg (NCodecMessage*)
: (out) The message representation to write to the Network Codec. Caller owns
the message buffer/memory.
the message buffer/memory. Message type is defined by the codec
implementation.

#### Returns

Expand Down Expand Up @@ -351,7 +353,7 @@ nc (NCODEC*)

msg (NCodecMessage*)
: The message representation to write to the Network Codec. Caller owns the
message buffer/memory.
message buffer/memory. Message type is defined by the codec implementation.

#### Returns

Expand Down
Binary file modified doc/content/apis/ncodec/ncodec-component.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions dse/ncodec/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ nc (NCODEC*)
msg (NCodecMessage*)
: The message representation to write to the Network Codec. Caller owns the
message buffer/memory.
message buffer/memory. Message type is defined by the codec implementation.
Returns
-------
Expand Down Expand Up @@ -211,7 +211,8 @@ nc (NCODEC*)
msg (NCodecMessage*)
: (out) The message representation to write to the Network Codec. Caller owns
the message buffer/memory.
the message buffer/memory. Message type is defined by the codec
implementation.
Returns
-------
Expand Down
27 changes: 19 additions & 8 deletions dse/ncodec/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ title Network Codec
package "Model Environment" {
interface "Signal Interface" as Sif
component "Model" as foo {
component "Model" as Model
interface "CodecVTable" as Cvt
component "Codec" as Codec
component "Model" as Model
interface "CodecVTable" as Cvt
component "Codec" as Codec
interface "StreamVTable" as Svt
}
component "Stream" as Stream
Expand Down Expand Up @@ -139,11 +139,7 @@ typedef struct NCodecConfigItem {
const char* value;
} NCodecConfigItem;

typedef struct NCodecMessage {
uint32_t frame_id;
uint8_t* buffer;
size_t len;
} NCodecMessage;
typedef void NCodecMessage; /* Generic message container. */


typedef int NCodecLoad(const char* filename, const char* hint);
Expand Down Expand Up @@ -178,6 +174,21 @@ typedef struct NCodecInstance {

/** NCODEC API */

typedef enum NCodecCanFrameType {
CAN_BASE_FRAME = 0,
CAN_EXTENDED_FRAME = 1,
CAN_FD_BASE_FRAME = 2,
CAN_FD_EXTENDED_FRAME = 3,
} NCodecCanFrameType;

typedef struct NCodecCanMessage {
uint32_t frame_id;
uint8_t* buffer;
size_t len;
NCodecCanFrameType frame_type;
} NCodecCanMessage;


/* Implemented by Codec. */
DLL_PUBLIC NCodecCreate ncodec_create;

Expand Down
12 changes: 7 additions & 5 deletions dse/ncodec/example/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ int codec_write(NCODEC* nc, NCodecMessage* msg)
{
if (nc == NULL) return -ENOSTR;
if (msg == NULL) return -EINVAL;
NCodecCanMessage* _msg = (NCodecCanMessage*)msg;

__codec* _nc = (__codec*)nc;
_nc->c.stream->write(nc, msg->buffer, msg->len);
return msg->len;
_nc->c.stream->write(nc, _msg->buffer, _msg->len);
return _msg->len;
}

int codec_read(NCODEC* nc, NCodecMessage* msg)
{
if (nc == NULL) return -ENOSTR;
if (msg == NULL) return -EINVAL;
NCodecCanMessage* _msg = (NCodecCanMessage*)msg;

__codec* _nc = (__codec*)nc;

Expand All @@ -71,10 +73,10 @@ int codec_read(NCODEC* nc, NCodecMessage* msg)
/* Construct the response. */
strncat(data, " says ", buffer_len - strlen(data));
strncat(data, _nc->name, buffer_len);
msg->len = strlen(data);
_msg->len = strlen(data);
/* Return the message (buffer owned by stream, caller must duplicate). */
msg->buffer = (uint8_t*)data;
return msg->len;
_msg->buffer = (uint8_t*)data;
return _msg->len;
}

int codec_flush(NCODEC* nc)
Expand Down
5 changes: 3 additions & 2 deletions dse/ncodec/example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ int main(int argc, char* argv[])
.name = "name", .value = "simple network codec" });

/* Write a message to the Network Codec. */
ncodec_write(nc, &(struct NCodecMessage){ .frame_id = 42,
ncodec_write(nc, &(struct NCodecCanMessage){ .frame_id = 42,
.frame_type = CAN_EXTENDED_FRAME,
.buffer = (uint8_t*)greeting,
.len = strlen(greeting) });
ncodec_flush(nc);
Expand All @@ -47,7 +48,7 @@ int main(int argc, char* argv[])
stream_seek(nc, 0, NCODEC_SEEK_SET);

/* Read the response from the Network Codec. */
NCodecMessage msg = {};
NCodecCanMessage msg = {};
rc = ncodec_read(nc, &msg);
if (rc > 0) {
printf("Message is: %s\n", (char*)msg.buffer);
Expand Down
2 changes: 1 addition & 1 deletion dse/ncodec/libs/automotive-bus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ add_compile_options(${C_CXX_WARNING_FLAGS})
# External Project - Automotive Bus Schema
# -------------------------------------------
FetchContent_Declare(automotive-bus-schema
URL https://github.com/boschglobal/automotive-bus-schema/releases/download/v1.0.2/automotive-bus-schema.tar.gz
URL https://github.com/boschglobal/automotive-bus-schema/releases/download/v1.0.3/automotive-bus-schema.tar.gz
)
FetchContent_MakeAvailable(automotive-bus-schema)
set(SCHEMAS_SOURCE_DIR ${automotive-bus-schema_SOURCE_DIR}/flatbuffers/c)
Expand Down
32 changes: 18 additions & 14 deletions dse/ncodec/libs/automotive-bus/frame_can_fbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ static void finalize_stream(

int can_write(NCODEC* nc, NCodecMessage* msg)
{
ABCodecInstance* _nc = (ABCodecInstance*)nc;
ABCodecInstance* _nc = (ABCodecInstance*)nc;
NCodecCanMessage* _msg = (NCodecCanMessage*)msg;
if (_nc == NULL) return -ENOSTR;
if (msg == NULL) return -EINVAL;
if (_msg == NULL) return -EINVAL;
if (_nc->c.stream == NULL) return -ENOSR;

flatcc_builder_t* B = &_nc->fbs_builder;
Expand All @@ -61,10 +62,10 @@ int can_write(NCODEC* nc, NCodecMessage* msg)
ns(Stream_frames_push_start(B));
ns(CanFrame_start(B));
/* Encode the message. */
ns(CanFrame_frame_id_add(B, msg->frame_id));
ns(CanFrame_frame_id_add(B, _msg->frame_id));
ns(CanFrame_frame_type_add(B, _msg->frame_type));
ns(CanFrame_payload_add(
B, flatbuffers_uint8_vec_create(B, msg->buffer, msg->len)));
ns(CanFrame_frame_type_add(B, ns(FrameTypes_CanFrame)));
B, flatbuffers_uint8_vec_create(B, _msg->buffer, _msg->len)));
/* Add additional metadata. */
ns(CanFrame_bus_id_add(B, _nc->bus_id));
ns(CanFrame_node_id_add(B, _nc->node_id));
Expand All @@ -73,7 +74,7 @@ int can_write(NCODEC* nc, NCodecMessage* msg)
ns(Frame_f_CanFrame_add(B, ns(CanFrame_end(B))));
ns(Stream_frames_push_end(B));

return msg->len;
return _msg->len;
}


Expand Down Expand Up @@ -137,14 +138,16 @@ static void get_vector_from_message(NCODEC* nc)

int can_read(NCODEC* nc, NCodecMessage* msg)
{
ABCodecInstance* _nc = (ABCodecInstance*)nc;
ABCodecInstance* _nc = (ABCodecInstance*)nc;
NCodecCanMessage* _msg = (NCodecCanMessage*)msg;
if (_nc == NULL) return -ENOSTR;
if (msg == NULL) return -EINVAL;
if (_msg == NULL) return -EINVAL;
if (_nc->c.stream == NULL) return -ENOSR;

/* Reset the message, in case caller ignores the return value. */
msg->len = 0;
msg->buffer = NULL;
_msg->len = 0;
_msg->frame_type = CAN_BASE_FRAME;
_msg->buffer = NULL;

/* Process the stream/frames. */
if (_nc->msg_ptr == NULL) get_msg_from_stream(nc);
Expand All @@ -164,15 +167,16 @@ int can_read(NCODEC* nc, NCodecMessage* msg)
continue;

/* Return the message. */
msg->frame_id = ns(CanFrame_frame_id(can_frame));
_msg->frame_id = ns(CanFrame_frame_id(can_frame));
_msg->frame_type = ns(CanFrame_frame_type(can_frame));
flatbuffers_uint8_vec_t payload = ns(CanFrame_payload(can_frame));
msg->buffer =
_msg->buffer =
(uint8_t*)payload; // TODO think about this cast ... caller
// should not modify ... restrict?
msg->len = flatbuffers_uint8_vec_len(payload);
_msg->len = flatbuffers_uint8_vec_len(payload);
/* ... but don't forget to save the vector index either. */
_nc->vector_idx = _vi + 1;
return msg->len;
return _msg->len;
}

/* Next msg/vector? */
Expand Down
2 changes: 1 addition & 1 deletion dse/ncodec/libs/automotive-bus/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add_compile_options(${C_CXX_WARNING_FLAGS})
# External Project - Automotive Bus Schema
# -------------------------------------------
FetchContent_Declare(automotive-bus-schema
URL https://github.com/boschglobal/automotive-bus-schema/releases/download/v1.0.2/automotive-bus-schema.tar.gz
URL https://github.com/boschglobal/automotive-bus-schema/releases/download/v1.0.3/automotive-bus-schema.tar.gz
)
FetchContent_MakeAvailable(automotive-bus-schema)
set(SCHEMAS_SOURCE_DIR ${automotive-bus-schema_SOURCE_DIR}/flatbuffers/c)
Expand Down
Loading

0 comments on commit bf9dfcd

Please sign in to comment.