Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
ColumnLimit: 80
DerivePointerAlignment: false
PointerAlignment: Left
IndentWidth: 4
TabWidth: 4
UseTab: Never
BreakBeforeBraces: Stroustrup
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 2
CaseSensitive: true
- Regex: '^((<|")(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '<[[:alnum:].]+>'
Priority: 4
- Regex: '.*'
Priority: 1
SortPriority: 0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build/
.lock*
.waf*
deps/
.vscode/
94 changes: 94 additions & 0 deletions examples/cpp/transport/SerialCobsTransportTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "types/example_t.hpp"
#include "zcm/transport/cobs_serial/generic_serial_cobs_transport.h"
#include "zcm/zcm-cpp.hpp"
#include <sys/time.h>

#include <cassert>
#include <iostream>
#include <queue>

using namespace std;
using namespace zcm;

#define PUBLISH_DT (1e6) / (5)
#define MIN(A, B) ((A) < (B)) ? (A) : (B)
#define MAX_FIFO 12

queue<uint8_t> fifo;

static size_t get(uint8_t* data, size_t nData, void* usr)
{
size_t n = MIN(MAX_FIFO, nData);
n = MIN(fifo.size(), n);

for (size_t i = 0; i < n; ++i) {
data[i] = fifo.front();
fifo.pop();
}

return n;
}

static size_t put(const uint8_t* data, size_t nData, void* usr)
{
size_t n = MIN(MAX_FIFO - fifo.size(), nData);
// cout << "Put " << n << " bytes" << endl;

for (size_t i = 0; i < n; ++i) fifo.push(data[i]);

return n;
}

static uint64_t utime(void* usr)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}

class Handler {
int64_t lastHost = 0;

public:
void handle(const ReceiveBuffer* rbuf, const string& chan,
const example_t* msg)
{
cout << "Message received" << endl;

if (msg->timestamp <= lastHost || rbuf->recv_utime < lastHost) {
assert("ERROR: utime mismatch. This should never happen");
}
lastHost = msg->timestamp;
}
};

int main(int argc, const char* argv[])
{
ZCM zcmLocal(zcm_trans_generic_serial_cobs_create(&get, &put, NULL, &utime,
NULL, 1024, 1024 * 5));

example_t example = {};
example.num_ranges = 1;
example.ranges.resize(1);
example.ranges.at(0) = 1;

Handler handler;
auto sub = zcmLocal.subscribe("EXAMPLE", &Handler::handle, &handler);

uint64_t nextPublish = 0;
while (true) {
uint64_t now = utime(NULL);
if (now > nextPublish) {
cout << "Publishing" << endl;
example.timestamp = now;
zcmLocal.publish("EXAMPLE", &example);
nextPublish = now + PUBLISH_DT;
}

zcmLocal.handleNonblock();
}

zcmLocal.unsubscribe(sub);

return 0;
}
4 changes: 4 additions & 0 deletions examples/cpp/transport/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ def build(ctx):
ctx.program(target = 'generic-serial',
use = 'default zcm examplezcmtypes_cpp',
source = 'SerialTransportTest.cpp')

ctx.program(target = 'generic-cobs-serial',
use = 'default zcm examplezcmtypes_cpp',
source = 'SerialCobsTransportTest.cpp')
Loading