Skip to content

Commit

Permalink
Extend NCodec interface with truncate() method.
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 6caad29 commit 4ebf80e
Show file tree
Hide file tree
Showing 9 changed files with 947 additions and 831 deletions.
124 changes: 62 additions & 62 deletions doc/content/apis/ncodec/examples/ncodec_api.c
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
// Copyright 2023 Robert Bosch GmbH
//
// SPDX-License-Identifier: Apache-2.0

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dse/ncodec/codec.h>


#define MIMETYPE "application/x-codec-example"


extern NCodecStreamVTable example_stream;
extern int stream_seek(NCODEC* nc, size_t pos, int op);


int main(int argc, char* argv[])
{
int rc;
static const char* greeting = "Hello World";

if (argc > 1) {
rc = ncodec_load(argv[1], NULL);
if (rc) {
printf("Load failed (rc %d)\n", rc);
return rc;
}
}

NCODEC* nc = ncodec_open(MIMETYPE, (void*)&example_stream);
if (nc == NULL) {
printf("Open failed (errno %d)\n", errno);
return errno;
}
ncodec_config(nc, (struct NCodecConfigItem){
.name = "name", .value = "simple network codec" });

/* Write a message to the Network Codec. */
ncodec_write(nc, &(struct NCodecMessage){ .frame_id = 42,
.buffer = (uint8_t*)greeting,
.len = strlen(greeting) });
ncodec_flush(nc);

/* Reposition to start of stream. */
stream_seek(nc, 0, NCODEC_SEEK_SET);

/* Read the response from the Network Codec. */
NCodecMessage msg = {};
rc = ncodec_read(nc, &msg);
if (rc > 0) {
printf("Message is: %s\n", (char*)msg.buffer);
} else {
printf("There was no message! (reason %d)\n", rc);
}

/* Close the Network Codec. */
ncodec_close(nc);

return 0;
}
// Copyright 2023 Robert Bosch GmbH
//
// SPDX-License-Identifier: Apache-2.0

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dse/ncodec/codec.h>


#define MIMETYPE "application/x-codec-example"


extern NCodecStreamVTable example_stream;
extern int stream_seek(NCODEC* nc, size_t pos, int op);


int main(int argc, char* argv[])
{
int rc;
static const char* greeting = "Hello World";

if (argc > 1) {
rc = ncodec_load(argv[1], NULL);
if (rc) {
printf("Load failed (rc %d)\n", rc);
return rc;
}
}

NCODEC* nc = ncodec_open(MIMETYPE, (void*)&example_stream);
if (nc == NULL) {
printf("Open failed (errno %d)\n", errno);
return errno;
}
ncodec_config(nc, (struct NCodecConfigItem){
.name = "name", .value = "simple network codec" });

/* Write a message to the Network Codec. */
ncodec_write(nc, &(struct NCodecMessage){ .frame_id = 42,
.buffer = (uint8_t*)greeting,
.len = strlen(greeting) });
ncodec_flush(nc);

/* Reposition to start of stream. */
stream_seek(nc, 0, NCODEC_SEEK_SET);

/* Read the response from the Network Codec. */
NCodecMessage msg = {};
rc = ncodec_read(nc, &msg);
if (rc > 0) {
printf("Message is: %s\n", (char*)msg.buffer);
} else {
printf("There was no message! (reason %d)\n", rc);
}

/* Close the Network Codec. */
ncodec_close(nc);

return 0;
}
38 changes: 30 additions & 8 deletions doc/content/apis/ncodec/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ center footer Dynamic Simulation Environment

```c
typedef struct NCodecConfigItem {
const char * name;
const char * value;
const char* name;
const char* value;
}
```

### NCodecInstance

```c
typedef struct NCodecInstance {
const char * mime_type;
const char* mime_type;
NCodecVTable codec;
NCodecStreamVTable * stream;
NCodecStreamVTable* stream;
}
```

Expand All @@ -97,7 +97,7 @@ typedef struct NCodecInstance {
```c
typedef struct NCodecMessage {
uint32_t frame_id;
uint8_t * buffer;
uint8_t* buffer;
size_t len;
}
```
Expand All @@ -124,6 +124,7 @@ typedef struct NCodecVTable {
NCodecWrite write;
NCodecRead read;
NCodecFlush flush;
NCodecTruncate truncate;
NCodecClose close;
}
```
Expand Down Expand Up @@ -212,7 +213,8 @@ hint (const char*)
: The Network Codec library way successfully loaded.

-1
: The Network Codec library could not be loaded. Inspect `errno` for more details.
: The Network Codec library could not be loaded. Inspect `errno` for more
details.



Expand Down Expand Up @@ -270,8 +272,8 @@ nc (NCODEC*)
: Network Codec object.

msg (NCodecMessage*)
: (out) The message representation to write to the Network Codec. Caller owns the
message buffer/memory.
: (out) The message representation to write to the Network Codec. Caller owns
the message buffer/memory.

#### Returns

Expand Down Expand Up @@ -314,6 +316,26 @@ NetworkConfigItem



### ncodec_truncate

#### Parameters

nc (NCODEC*)
: Network Codec object.

#### Returns

0
: The Network Codec internal buffers were truncated.

-ENOSTR
: The object represented by `nc` does not represent a valid stream.

-ENOSR
: No stream resource has been configured.



### ncodec_write

Write the provided message to the Network Codec object.
Expand Down
40 changes: 36 additions & 4 deletions dse/ncodec/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "codec.h" // NOLINT
#include "codec.h" // NOLINT


/**
Expand All @@ -25,7 +25,8 @@ Returns
: The Network Codec library way successfully loaded.
-1
: The Network Codec library could not be loaded. Inspect `errno` for more details.
: The Network Codec library could not be loaded. Inspect `errno` for more
details.
*/
extern int ncodec_load(const char* filename, const char* hint);

Expand Down Expand Up @@ -209,8 +210,8 @@ nc (NCODEC*)
: Network Codec object.
msg (NCodecMessage*)
: (out) The message representation to write to the Network Codec. Caller owns the
message buffer/memory.
: (out) The message representation to write to the Network Codec. Caller owns
the message buffer/memory.
Returns
-------
Expand Down Expand Up @@ -275,6 +276,37 @@ inline int ncodec_flush(NCODEC* nc)
}


/**
ncodec_truncate
===============
Parameters
----------
nc (NCODEC*)
: Network Codec object.
Returns
-------
0
: The Network Codec internal buffers were truncated.
-ENOSTR
: The object represented by `nc` does not represent a valid stream.
-ENOSR
: No stream resource has been configured.
*/
inline int ncodec_truncate(NCODEC* nc)
{
NCodecInstance* _nc = (NCodecInstance*)nc;
if (_nc && _nc->codec.truncate) {
return _nc->codec.truncate(nc);
} else {
return -ENOSTR;
}
}


/**
ncodec_close
============
Expand Down
Loading

0 comments on commit 4ebf80e

Please sign in to comment.