Skip to content

Commit

Permalink
uartpb: add a unique id for each Protobuf message definition
Browse files Browse the repository at this point in the history
Using a unique id for each Protobuf message allows libraries/applications
defining these messages to register a callback function associated
with each id.
This way, uartpb is not dependent of applications anymore, which had to
declare a Protobuf message gathering all known Protobuf messages.

This unique id will be sent first, followed by encoded Protobuf message.
The unique id can also be sent alone if the message does not required
any content (like a reset or trigger message).

This commit also updates all modules and applications affected by this
refactoring.

Fixes #67

Signed-off-by: Eric Courtois <eric.courtois@gmail.com>
  • Loading branch information
ecourtois committed Jul 24, 2022
1 parent 28ce0fc commit 35576c6
Show file tree
Hide file tree
Showing 32 changed files with 338 additions and 544 deletions.
15 changes: 0 additions & 15 deletions applications/app_test/PB_GameInputMessage.proto

This file was deleted.

16 changes: 0 additions & 16 deletions applications/app_test/PB_GameOutputMessage.proto

This file was deleted.

5 changes: 5 additions & 0 deletions applications/app_test/PB_Score.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message PB_Score {
int32 value = 1;
}
20 changes: 13 additions & 7 deletions applications/app_test/app_samples.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Firmware includes
#include "platform.hpp"
#include "trigonometry.h"
#include "uartpb_config.hpp"

#include "app_camp.hpp"
#include "app_samples.hpp"
Expand All @@ -16,9 +15,11 @@ namespace cogip {

namespace app {

static PB_OutputMessage _output_message;
static DetectedSamples _detected_samples;

constexpr cogip::uartpb::uuid_t sample_request_uuid = 3781855956;
constexpr cogip::uartpb::uuid_t sample_response_uuid = 1538397045;

static std::map<CampColor, SamplesMap *> _samples;

typedef struct {
Expand Down Expand Up @@ -57,6 +58,8 @@ void app_samples_init()
return;
}

pf_get_uartpb()->register_message_handler(sample_response_uuid, app_samples_process);

event_queue_init_detached(&_sample_queue);
_sample_event.super.list_node.next = nullptr;

Expand Down Expand Up @@ -170,9 +173,7 @@ const DetectedSamples & app_samples_detect(void)
{
event_queue_claim(&_sample_queue);
cogip::uartpb::UartProtobuf *uartpb = pf_get_uartpb();
_output_message.clear();
_output_message.set_req_samples(true);
uartpb->send_message(_output_message);
uartpb->send_message(sample_request_uuid);
sample_event_t *event = (sample_event_t *)event_wait(&_sample_queue);

auto & samples = event->pb_message.get_samples();
Expand Down Expand Up @@ -203,9 +204,14 @@ const DetectedSamples & app_samples_detect(void)
return _detected_samples;
}

void app_samples_process(const PB_Samples<APP_SAMPLES_MAX_DETECTED> &samples)
void app_samples_process(cogip::uartpb::ReadBuffer *buffer)
{
_sample_event.pb_message = samples;
EmbeddedProto::Error error = _sample_event.pb_message.deserialize(*buffer);
if (error != EmbeddedProto::Error::NO_ERRORS) {
std::cout << "Samples: Protobuf deserialization error: " << static_cast<int>(error) << std::endl;
return;
}

event_post(&_sample_queue, (event_t *)&_sample_event);
}

Expand Down
50 changes: 0 additions & 50 deletions applications/app_test/app_uartpb.cpp

This file was deleted.

1 change: 0 additions & 1 deletion applications/app_test/include/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "app_obstacles.hpp"
#include "app_samples.hpp"
#include "app_shell.hpp"
#include "app_uartpb.hpp"

/*
* Machine parameters
Expand Down
3 changes: 2 additions & 1 deletion applications/app_test/include/app_samples.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "obstacles/Circle.hpp"
#include "uartpb/ReadBuffer.hpp"

#include "PB_Samples.hpp"

Expand Down Expand Up @@ -117,7 +118,7 @@ void app_samples_init();

const DetectedSamples & app_samples_detect(void);

void app_samples_process(const PB_Samples<APP_SAMPLES_MAX_DETECTED> &samples);
void app_samples_process(cogip::uartpb::ReadBuffer *buffer);

}; // namespace app

Expand Down
14 changes: 0 additions & 14 deletions applications/app_test/include/app_uartpb.hpp

This file was deleted.

59 changes: 0 additions & 59 deletions applications/app_test/include/uartpb_config.hpp

This file was deleted.

15 changes: 0 additions & 15 deletions applications/cup2022/PB_GameInputMessage.proto

This file was deleted.

16 changes: 0 additions & 16 deletions applications/cup2022/PB_GameOutputMessage.proto

This file was deleted.

20 changes: 13 additions & 7 deletions applications/cup2022/app_samples.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Firmware includes
#include "platform.hpp"
#include "trigonometry.h"
#include "uartpb_config.hpp"

#include "app_camp.hpp"
#include "app_samples.hpp"
Expand All @@ -16,9 +15,11 @@ namespace cogip {

namespace app {

static PB_OutputMessage _output_message;
static DetectedSamples _detected_samples;

constexpr cogip::uartpb::uuid_t sample_request_uuid = 3781855956;
constexpr cogip::uartpb::uuid_t sample_response_uuid = 1538397045;

static std::map<CampColor, SamplesMap *> _samples;

typedef struct {
Expand Down Expand Up @@ -57,6 +58,8 @@ void app_samples_init()
return;
}

pf_get_uartpb()->register_message_handler(sample_response_uuid, app_samples_process);

event_queue_init_detached(&_sample_queue);
_sample_event.super.list_node.next = nullptr;

Expand Down Expand Up @@ -170,9 +173,7 @@ const DetectedSamples & app_samples_detect(void)
{
event_queue_claim(&_sample_queue);
cogip::uartpb::UartProtobuf *uartpb = pf_get_uartpb();
_output_message.clear();
_output_message.set_req_samples(true);
uartpb->send_message(_output_message);
uartpb->send_message(sample_request_uuid);
sample_event_t *event = (sample_event_t *)event_wait(&_sample_queue);

auto & samples = event->pb_message.get_samples();
Expand Down Expand Up @@ -203,9 +204,14 @@ const DetectedSamples & app_samples_detect(void)
return _detected_samples;
}

void app_samples_process(const PB_Samples<APP_SAMPLES_MAX_DETECTED> &samples)
void app_samples_process(cogip::uartpb::ReadBuffer *buffer)
{
_sample_event.pb_message = samples;
EmbeddedProto::Error error = _sample_event.pb_message.deserialize(*buffer);
if (error != EmbeddedProto::Error::NO_ERRORS) {
std::cout << "Samples: Protobuf deserialization error: " << static_cast<int>(error) << std::endl;
return;
}

event_post(&_sample_queue, (event_t *)&_sample_event);
}

Expand Down
50 changes: 0 additions & 50 deletions applications/cup2022/app_uartpb.cpp

This file was deleted.

1 change: 0 additions & 1 deletion applications/cup2022/include/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "app_obstacles.hpp"
#include "app_samples.hpp"
#include "app_shell.hpp"
#include "app_uartpb.hpp"

/*
* Machine parameters
Expand Down
Loading

0 comments on commit 35576c6

Please sign in to comment.