Skip to content

Commit

Permalink
expose frame and rcv_frame in C++ submaster (commaai#76)
Browse files Browse the repository at this point in the history
* expose rcv_frame in C++ submaster

* expose frame too
  • Loading branch information
adeebshihadeh authored Aug 1, 2020
1 parent 33c2780 commit 10a25c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
16 changes: 9 additions & 7 deletions messaging/messaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,36 @@ class Poller {
};

class SubMaster {
public:
public:
SubMaster(const std::initializer_list<const char *> &service_list,
const char *address = nullptr, const std::initializer_list<const char *> &ignore_alive = {});
int update(int timeout = 1000);
inline bool allAlive(const std::initializer_list<const char *> &service_list = {}) { return all_(service_list, false, true); }
inline bool allValid(const std::initializer_list<const char *> &service_list = {}) { return all_(service_list, true, false); }
inline bool allAliveAndValid(const std::initializer_list<const char *> &service_list = {}) { return all_(service_list, true, true); }
bool updated(const char *name) const;
void drain();
cereal::Event::Reader &operator[](const char *name);
~SubMaster();

private:
uint64_t frame = 0;
bool updated(const char *name) const;
uint64_t rcv_frame(const char *name) const;
cereal::Event::Reader &operator[](const char *name);

private:
bool all_(const std::initializer_list<const char *> &service_list, bool valid, bool alive);
Poller *poller_ = nullptr;
uint64_t frame_ = 0;
struct SubMessage;
std::map<SubSocket *, SubMessage *> messages_;
std::map<std::string, SubMessage *> services_;
};

class PubMaster {
public:
public:
PubMaster(const std::initializer_list<const char *> &service_list);
inline int send(const char *name, capnp::byte *data, size_t size) { return sockets_.at(name)->send((char *)data, size); }
int send(const char *name, capnp::MessageBuilder &msg);
~PubMaster();

private:
private:
std::map<std::string, PubSocket *> sockets_;
};
33 changes: 23 additions & 10 deletions messaging/socketmaster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
#include <time.h>
#include "messaging.hpp"
#include "services.h"

#ifdef __APPLE__
#define CLOCK_BOOTTIME CLOCK_MONOTONIC
#endif

static inline uint64_t nanos_since_boot() {
struct timespec t;
clock_gettime(CLOCK_BOOTTIME, &t);
return t.tv_sec * 1000000000ULL + t.tv_nsec;
}

static const service *get_service(const char *name) {
for (const auto &it : services) {
if (strcmp(it.name, name) == 0) return &it;
}
return nullptr;
}

static inline bool inList(const std::initializer_list<const char *> &list, const char *value) {
for (auto &v : list) {
if (strcmp(value, v) == 0) return true;
Expand All @@ -24,7 +28,7 @@ static inline bool inList(const std::initializer_list<const char *> &list, const
}

class MessageContext {
public:
public:
MessageContext() { ctx_ = Context::create(); }
~MessageContext() { delete ctx_; }
Context *ctx_;
Expand Down Expand Up @@ -53,18 +57,18 @@ SubMaster::SubMaster(const std::initializer_list<const char *> &service_list, co
assert(socket != 0);
poller_->registerSocket(socket);
SubMessage *m = new SubMessage{
.socket = socket,
.freq = serv->frequency,
.ignore_alive = inList(ignore_alive, name),
.allocated_msg_reader = malloc(sizeof(capnp::FlatArrayMessageReader)),
.buf = kj::heapArray<capnp::word>(1024)};
.socket = socket,
.freq = serv->frequency,
.ignore_alive = inList(ignore_alive, name),
.allocated_msg_reader = malloc(sizeof(capnp::FlatArrayMessageReader)),
.buf = kj::heapArray<capnp::word>(1024)};
messages_[socket] = m;
services_[name] = m;
}
}

int SubMaster::update(int timeout) {
if (++frame_ == UINT64_MAX) frame_ = 1;
if (++frame == UINT64_MAX) frame = 1;
for (auto &kv : messages_) kv.second->updated = false;

int updated = 0;
Expand All @@ -89,7 +93,7 @@ int SubMaster::update(int timeout) {
m->event = m->msg_reader->getRoot<cereal::Event>();
m->updated = true;
m->rcv_time = current_time;
m->rcv_frame = frame_;
m->rcv_frame = frame;
m->valid = m->event.getValid();

++updated;
Expand Down Expand Up @@ -126,8 +130,17 @@ void SubMaster::drain() {
}
}

bool SubMaster::updated(const char *name) const { return services_.at(name)->updated; }
cereal::Event::Reader &SubMaster::operator[](const char *name) { return services_.at(name)->event; };
bool SubMaster::updated(const char *name) const {
return services_.at(name)->updated;
}

uint64_t SubMaster::rcv_frame(const char *name) const {
return services_.at(name)->rcv_frame;
}

cereal::Event::Reader &SubMaster::operator[](const char *name) {
return services_.at(name)->event;
};

SubMaster::~SubMaster() {
delete poller_;
Expand Down

0 comments on commit 10a25c8

Please sign in to comment.