Skip to content

Commit

Permalink
Complete signal API.
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 Sep 11, 2024
1 parent d63b784 commit ece1e0c
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DSE_SCHEMA_VERSION ?= 1.2.8
export DSE_SCHEMA_URL ?= $(DSE_SCHEMA_REPO)/releases/download/v$(DSE_SCHEMA_VERSION)/dse-schemas.tar.gz

DSE_NCODEC_REPO ?= https://github.com/boschglobal/dse.standards
DSE_NCODEC_VERSION ?= 1.0.7
DSE_NCODEC_VERSION ?= 1.0.8
export DSE_NCODEC_URL ?= $(DSE_NCODEC_REPO)/archive/refs/tags/v$(DSE_NCODEC_VERSION).zip


Expand Down
2 changes: 1 addition & 1 deletion doc/content/apis/modelc/examples/model_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct {

static inline double* _index(ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.scalar == NULL) log_fatal("Signal not found (%s:%s)", v, s);
return idx.scalar;
}
Expand Down
2 changes: 1 addition & 1 deletion dse/mocks/simmock.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void simmock_load(SimMock* mock)

void* handle = dlopen(
model->mi->model_definition.full_path, RTLD_NOW | RTLD_LOCAL);
if (handle == NULL) log_notice("ERROR: dlopen call: %s", dlerror());
if (handle == NULL) log_error("ERROR: dlopen call: %s", dlerror());
assert_non_null(handle);
model->vtable.create = dlsym(handle, MODEL_CREATE_FUNC_NAME);
model->vtable.step = dlsym(handle, MODEL_STEP_FUNC_NAME);
Expand Down
20 changes: 11 additions & 9 deletions dse/modelc/examples/binary/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
typedef struct {
SignalVector* sv;
uint32_t index;
char* buffer;
uint8_t* buffer;
uint32_t buffer_size;
} BinarySignalDesc;

Expand All @@ -32,7 +32,7 @@ typedef struct {
static inline double* _index_scalar(
ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.scalar == NULL) log_fatal("Signal not found (%s:%s)", v, s);
return idx.scalar;
}
Expand All @@ -41,7 +41,7 @@ static inline double* _index_scalar(
static inline BinarySignalDesc _index_binary(
ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.binary == NULL) log_fatal("Signal not found (%s:%s)", v, s);

BinarySignalDesc ret = {
Expand Down Expand Up @@ -74,30 +74,32 @@ ModelDesc* model_create(ModelDesc* model)

static inline int _format_message(BinarySignalDesc* b, int v)
{
return snprintf(b->buffer, b->buffer_size, "count is %d", v);
return snprintf((char*)b->buffer, b->buffer_size, "count is %d", v);
}

int model_step(ModelDesc* model, double* model_time, double stop_time)
{
ExtendedModelDesc* m = (ExtendedModelDesc*)model;

/* Print the binary signal. */
uint8_t* buffer;
size_t len;
signal_read(m->binary.message.sv, m->binary.message.index, &buffer, &len);
log_info("Message (%s) : %s",
m->binary.message.sv->signal[m->binary.message.index],
m->binary.message.sv->binary[m->binary.message.index]);
m->binary.message.sv->signal[m->binary.message.index], buffer);

/* Scalar signals. */
*(m->scalars.counter) += 1;
/* Binary signals. */
int len = _format_message(&(m->binary.message), (int)*(m->scalars.counter));
if (len >= (int)(m->binary.message.buffer_size - 1)) {
len = _format_message(&(m->binary.message), (int)*(m->scalars.counter));
if (len >= (m->binary.message.buffer_size - 1)) {
m->binary.message.buffer = realloc(m->binary.message.buffer, len + 1);
m->binary.message.buffer_size = len + 1;
_format_message(&(m->binary.message), (int)*(m->scalars.counter));
}
signal_reset(m->binary.message.sv, m->binary.message.index);
signal_append(m->binary.message.sv, m->binary.message.index,
m->binary.message.buffer, strlen(m->binary.message.buffer) + 1);
m->binary.message.buffer, strlen((char*)m->binary.message.buffer) + 1);

*model_time = stop_time;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion dse/modelc/examples/doc/apis/model_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct {

static inline double* _index(ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.scalar == NULL) log_fatal("Signal not found (%s:%s)", v, s);
return idx.scalar;
}
Expand Down
2 changes: 1 addition & 1 deletion dse/modelc/examples/extended/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct {

static inline double* _index(ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.scalar == NULL) log_fatal("Signal not found (%s:%s)", v, s);
return idx.scalar;
}
Expand Down
6 changes: 3 additions & 3 deletions dse/modelc/examples/gateway/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ int main(int argc, char** argv)
sv->name, i, sv->length[i], sv->buffer_size[i],
sv->binary[i], sv->signal[i]);
/* Exchange/update the gateway signal. */
char buffer[100];
snprintf(buffer, sizeof(buffer), "st=%f,index=%d",
uint8_t buffer[100];
snprintf((char*)buffer, sizeof(buffer), "st=%f,index=%d",
model_time, i);
signal_reset(sv, i);
signal_append(sv, i, buffer, strlen(buffer) + 1);
signal_append(sv, i, buffer, strlen((char*)buffer) + 1);
}
} else {
/* Scalar vector. */
Expand Down
2 changes: 1 addition & 1 deletion dse/modelc/examples/ncodec/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct {

static inline NCODEC* _index(ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.binary == NULL) log_fatal("Signal not found (%s:%s)", v, s);

NCODEC* nc = signal_codec(idx.sv, idx.signal);
Expand Down
6 changes: 3 additions & 3 deletions dse/modelc/examples/runtime/sim/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ typedef struct {

static inline NCODEC* _index(ExtendedModelDesc* m, const char* v, const char* s)
{
ModelSignalIndex idx = m->model.index((ModelDesc*)m, v, s);
ModelSignalIndex idx = signal_index((ModelDesc*)m, v, s);
if (idx.binary == NULL) log_fatal("Signal not found (%s:%s)", v, s);

NCODEC* nc = idx.sv->codec(idx.sv, idx.signal);
NCODEC* nc = signal_codec(idx.sv, idx.signal);
if (nc == NULL) log_fatal("NCodec object not available (%s:%s)", v, s);

return nc;
Expand Down Expand Up @@ -71,7 +71,7 @@ int model_step(ModelDesc* model, double* model_time, double stop_time)

/* Scalar signals. */
ModelSignalIndex counter =
m->model.index((ModelDesc*)m, "scalar_vector", "counter");
signal_index((ModelDesc*)m, "scalar_vector", "counter");
if (counter.scalar == NULL) return -EINVAL;
*(counter.scalar) += 1;

Expand Down
2 changes: 1 addition & 1 deletion dse/modelc/examples/simer/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ModelDesc* model_create(ModelDesc* model)
if (getenv("COUNTER_NAME")) {
counter_name = getenv("COUNTER_NAME");
}
m->counter = m->model.index((ModelDesc*)m, "data", counter_name);
m->counter = signal_index((ModelDesc*)m, "data", counter_name);
if (m->counter.scalar == NULL) log_fatal("Signal not found (%s)", counter_name);

/* Set initial values. */
Expand Down
8 changes: 6 additions & 2 deletions dse/modelc/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef DSE_MODELC_MODEL_H_
#define DSE_MODELC_MODEL_H_

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

Expand Down Expand Up @@ -188,7 +189,6 @@ DLL_PUBLIC const char* model_instance_annotation(


/* Signal Interface. */

typedef int (*BinarySignalAppendFunc)(
SignalVector* sv, uint32_t index, void* data, uint32_t len);
typedef int (*BinarySignalResetFunc)(SignalVector* sv, uint32_t index);
Expand Down Expand Up @@ -249,8 +249,12 @@ typedef struct SignalVector {


/* Provided by ModelC (virtual methods of SignalVectorVTable). */
DLL_PUBLIC ModelSignalIndex signal_index(
ModelDesc* m, const char* vname, const char* name);
DLL_PUBLIC int signal_read(
SignalVector* sv, uint32_t index, uint8_t** data, size_t* len);
DLL_PUBLIC int signal_append(
SignalVector* sv, uint32_t index, void* data, uint32_t len);
SignalVector* sv, uint32_t index, uint8_t* data, size_t len);
DLL_PUBLIC int signal_reset(SignalVector* sv, uint32_t index);
DLL_PUBLIC int signal_release(SignalVector* sv, uint32_t index);
DLL_PUBLIC void* signal_codec(SignalVector* sv, uint32_t index);
Expand Down
12 changes: 6 additions & 6 deletions dse/modelc/model/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,11 @@ NULL
: The specified annotation was not found.
*/
const char* model_annotation(ModelDesc* m, const char* name)
const char* model_annotation(ModelDesc* model, const char* name)
{
if (m && m->mi) {
if (model && model->mi) {
YamlNode* a = dse_yaml_find_node(
m->mi->model_definition.doc, "metadata/annotations");
model->mi->model_definition.doc, "metadata/annotations");
if (a) return dse_yaml_get_scalar(a, name);
}
return NULL;
Expand Down Expand Up @@ -618,10 +618,10 @@ NULL
: The specified annotation was not found.
*/
const char* model_instance_annotation(ModelDesc* m, const char* name)
const char* model_instance_annotation(ModelDesc* model, const char* name)
{
if (m && m->mi) {
YamlNode* a = dse_yaml_find_node(m->mi->spec, "annotations");
if (model && model->mi) {
YamlNode* a = dse_yaml_find_node(model->mi->spec, "annotations");
if (a) return dse_yaml_get_scalar(a, name);
}
return NULL;
Expand Down
95 changes: 91 additions & 4 deletions dse/modelc/model/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,93 @@ void model_sv_destroy(SignalVector* sv)
}


/**
signal_index
============
A model may use this method to index a signal that is contained within the
Signal Vectors of the Model Descriptor.
Parameters
----------
model (ModelDesc*)
: The Model Descriptor object representing an instance of this model.
vname (const char*)
: The name (alias) of the Signal Vector.
name (const char*)
: The name of the signal within the Signal Vector. When set to NULL the index
will match on Signal Vector (vanme) only.
Returns
-------
ModelSignalIndex
: An index. When valid, either the `scalar` or `binary` fields will be set to
a valid pointer (i.e. not NULL). When `sname` is not specified the index will
contain a valid pointer to a Signal Vector object only (i.e. both `scalar`
and `binary` will be set to NULL).
*/
ModelSignalIndex signal_index(
ModelDesc* model, const char* vname, const char* name)
{
if (model && model->index) {
return model->index(model, vname, name);
}
return (ModelSignalIndex){};
}


/**
signal_read
===========
Read data from the specified binary signal. Returns pointer to the internal
binary signal buffers.
Parameters
----------
sv (SignalVector*)
: The Signal Vector object containing the signal.
index (uint32_t)
: Index of the signal in the Signal Vector object.
data (uint8_t*)
: Address/pointer to the data of the binary signal.
len (size_t)
: Length of the data.
Returns
-------
0
: The operation completed without error.
-EINVAL (-22)
: Bad arguments.
<>0
: Indicates an error condition. Inspect `errno` for additional information.
*/
inline int signal_read(
SignalVector* sv, uint32_t index, uint8_t** data, size_t* len)
{
if (data == NULL || len == NULL) return -EINVAL;
*data = NULL;
*len = 0;

if (sv && index < sv->count && sv->is_binary) {
*data = (uint8_t*)sv->binary[index];
*len = (size_t)sv->length[index];
return 0;
} else {
return -EINVAL;
}
}


/**
signal_append
=============
Expand All @@ -452,10 +539,10 @@ sv (SignalVector*)
index (uint32_t)
: Index of the signal in the Signal Vector object.
data (void*)
data (uint8_t*)
: Address/pointer to the data which should be appended to the binary signal.
len (uint32_t)
len (size_t)
: Length of the provided data buffer being appended.
Returns
Expand All @@ -473,11 +560,11 @@ Returns
: Indicates an error condition. Inspect `errno` for additional information.
*/
inline int signal_append(
SignalVector* sv, uint32_t index, void* data, uint32_t len)
SignalVector* sv, uint32_t index, uint8_t* data, size_t len)
{
if (sv && index < sv->count && sv->is_binary) {
if (sv->vtable.append) {
return sv->vtable.append(sv, index, data, len);
return sv->vtable.append(sv, index, (void*)data, len);
} else {
return -ENOSYS;
}
Expand Down
1 change: 0 additions & 1 deletion extra/external/oss_repos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# OSS Projects can be listed here, even if they are not used by the
# External Projects, to maintain an accurate inventory of OSS Projects.

set(ExternalProject__DSE_NCODEC__URL https://github.com/boschglobal/dse.standards/archive/refs/tags/v1.0.8.tar.gz)
set(ExternalProject__EVENT__URL https://github.com/libevent/libevent/archive/refs/tags/release-2.1.12-stable.tar.gz)
set(ExternalProject__YAML__URL https://github.com/yaml/libyaml/archive/0.2.5.tar.gz)
set(ExternalProject__MSGPACK__URL https://github.com/msgpack/msgpack-c/archive/refs/tags/cpp-3.3.0.tar.gz)
Expand Down
4 changes: 2 additions & 2 deletions tests/cmocka/model/interface/test_model_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ void test_model_api__binary_stream_reset(void** state)
mock, BINARY_INST_NAME, b_checks, ARRAY_SIZE(b_checks), NULL);
/* Inject some binary data, if reset is called, this will be ignored. */
SignalVector* sv = mock->sv_network_tx;
char foobar[100] = "foobar";
unsigned char foobar[100] = "foobar";
for (int j = 0; j < 2; j++) {
signal_reset(sv, j);
signal_append(sv, j, foobar, strlen(foobar) + 1);
signal_append(sv, j, foobar, strlen((char*)foobar) + 1);
}
/* Step the model. */
assert_int_equal(simmock_step(mock, true), 0);
Expand Down
Loading

0 comments on commit ece1e0c

Please sign in to comment.