Commit 12c1ecd0 authored by Niels Post's avatar Niels Post
Browse files

- Changed GET-TYPE configuration command to also get the name of the configurationvalue

- Response now stores its data on the heap so it can contain more bytes (for above change)
parent 411e2f6a
Pipeline #37 failed with stage
......@@ -41,11 +41,12 @@ enum MeasurementCommand {
/**
* Commands in the "Flash" category
*/
enum FlashCommand {
enum ConfigurationCommand {
SET_VALUE,
GET_VALUE,
GET_TYPE,
LOAD,
STORE,
PRINT_ALL
PRINT_ALL,
GET_INFO,
GET_CONFIGURATION_COUNT,
};
\ No newline at end of file
#pragma once
#undef min
#undef max
#include <cstdint>
#include <cstddef>
#include <memory>
/**
* Possible return codes for a wireless command.
......@@ -38,30 +40,63 @@ enum class SuccessCode : uint8_t {
* A response that is sent by the Robot to respond to a given \refitem{Command}
*/
struct Response {
public:
/// The message id of the original Command. This can be used to match a response to a sent command on the other side
uint8_t message_id;
/// ReturnCode for the message
SuccessCode successCode;
/// Should the message be broadcast or unicast?
bool broadcast = false;
private:
/// (Optional) the data returned from the Command
uint8_t data[4];
std::shared_ptr<uint8_t []> data;
/// Size of the data returned
uint8_t data_size;
/// Should the message be broadcast or unicast?
bool broadcast = false;
public:
/**
* Create a Response object
*/
Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr, bool _broadcast = false);
Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr,
bool _broadcast = false) : message_id(messageId),
successCode(returnCode),broadcast(_broadcast), data{data_length > 0 ? new uint8_t[data_length] : nullptr}, data_size(data_length) {
for (size_t i = 0; i < data_length; i++) {
data[i] = dat[i];
}
}
/**
* Create a Response object
*/
Response(SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr, bool _broadcast = false);
Response(SuccessCode returnCode, uint8_t data_length = 0, const uint8_t *dat = nullptr,
bool _broadcast = false): Response(0, returnCode, data_length, dat, _broadcast) {
}
/**
* Get the byte size of this command
* @return Total size of the command in bytes
*/
uint8_t size() const;
uint8_t size() const {
return 2 + data_size;
}
/**
* Write the message to a byte array.
*
* When calling this method, make sure the byte array is at least as big as the return value of size()
* @param write_to
*/
void write_bytes(uint8_t *write_to) const{
write_to[0] = message_id;
write_to[1] = static_cast<uint8_t>(successCode);
for(size_t i = 0; i < data_size; i++) {
write_to[i + 2] = data[i];
}
}
};
......@@ -84,4 +84,9 @@ public:
* Load the value from flash
*/
void load();
/**
* Get the name of the configuration parameter
*/
const char * get_name();
};
\ No newline at end of file
#pragma once
#include <Arduino.h>
#include <core/util/ArduinoSafe.hpp>
#include <OneBitDisplay.h>
#include <core/PMSVSettings.hpp>
......
#include <communication/commandcontroller/CommandController_Configuration.hpp>
#include <communication/CommandRegistry.hpp>
#include <cstring>
Response
CommandController_Configuration::handle(const Command &cmd, ResponseCallback callback) {
Response returnMessage{cmd.message_id, SuccessCode::SUCCESS};
switch (cmd.command_id) {
case SET_VALUE: {
auto id = cmd.parameters[0];
for (size_t i = 0; i < configuration_value_count; i++) {
if (configurationValues[i].get().getId() == id) {
configurationValues[i].get().set((void *) (&cmd.parameters[1]));
}
}
break;
}
case GET_VALUE: {
auto id = cmd.parameters[0];
returnMessage.successCode = SuccessCode::BAD_PARAMETERS;
for (size_t i = 0; i < configuration_value_count; i++) {
if (configurationValues[i].get().getId() == id) {
auto data = configurationValues[i].get().get();
returnMessage.data_size = data.second;
for (size_t j = 0; j < data.second; j++) {
returnMessage.data[j] = ((uint8_t *) data.first)[j];
}
returnMessage.successCode = SuccessCode::SUCCESS;
break;
}
}
switch (static_cast<ConfigurationCommand>(cmd.command_id)) {
case SET_VALUE: {
auto id = cmd.parameters[0];
for (size_t i = 0; i < configuration_value_count; i++) {
if (configurationValues[i].get().getId() == id) {
configurationValues[i].get().set((void *) (&cmd.parameters[1]));
}
}
return Response{cmd.message_id, SuccessCode::SUCCESS};
break;
}
case GET_VALUE: {
auto id = cmd.parameters[0];
for (size_t i = 0; i < configuration_value_count; i++) {
if (configurationValues[i].get().getId() == id) {
auto data = configurationValues[i].get().get();
return Response{cmd.message_id, SuccessCode::SUCCESS, data.second, (uint8_t *) data.first};
}
}
return Response{cmd.message_id, SuccessCode::BAD_PARAMETERS};
break;
}
case LOAD:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().load();
}
break;
case STORE:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().store();
}
break;
case PRINT_ALL:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().print_info();
}
break;
case GET_INFO: {
auto configurationId = cmd.parameters[0];
break;
}
case GET_TYPE: {
auto id = cmd.parameters[0];
returnMessage.successCode = SuccessCode::BAD_PARAMETERS;
for (size_t i = 0; i < configuration_value_count; i++) {
if (configurationValues[i].get().getId() == id) {
returnMessage.data[0] = configurationValues[i].get().getType();
returnMessage.data_size = 1;
returnMessage.successCode = SuccessCode::SUCCESS;
break;
}
}
break;
}
case LOAD:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().load();
}
break;
case STORE:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().store();
}
break;
case PRINT_ALL:
for (size_t i = 0; i < configuration_value_count; i++) {
configurationValues[i].get().print_info();
}
for(size_t i = 0; i <configuration_value_count; i++) {
if(configurationValues[i].get().getId() == configurationId) {
const char *name = configurationValues[i].get().get_name();
uint8_t data_size = strlen(name ) + 1;
uint8_t data[data_size];
data[0] = configurationValues[i].get().getType();
std::memcpy(&data[1], name, data_size-1);
return Response{cmd.message_id, SuccessCode::SUCCESS, data_size, data};
}
}
return Response{cmd.message_id, SuccessCode::BAD_PARAMETERS};
break;
default:
returnMessage.successCode = SuccessCode::UNKNOWN_COMMAND;
break;
}
return returnMessage;
break;
}
case GET_CONFIGURATION_COUNT:
return Response{cmd.message_id, SuccessCode::SUCCESS, 4, (uint8_t *) &configuration_value_count};
break;
default:
break;
}
return Response{cmd.message_id, SuccessCode::UNKNOWN_COMMAND};
}
uint8_t CommandController_Configuration::getCategoryID() {
return static_cast<uint8_t>(CommandCategory::CONFIGURATION);
return static_cast<uint8_t>(CommandCategory::CONFIGURATION);
}
std::pair<uint8_t, uint8_t> CommandController_Configuration::getParameterLimits(uint8_t command_id) {
switch (static_cast<FlashCommand>(command_id)) {
case SET_VALUE:
return {1, 5};
case GET_VALUE:
case GET_TYPE:
return {1, 1};
case LOAD:
case STORE:
case PRINT_ALL:
return {0, 0};
default:
return {255, 255};
switch (static_cast<ConfigurationCommand>(command_id)) {
case SET_VALUE:
return {1, 5};
case GET_VALUE:
return {1, 1};
case LOAD:
case STORE:
case PRINT_ALL:
return {0, 0};
case GET_INFO:
return {1, 1};
case GET_CONFIGURATION_COUNT:
return {0, 0};
default:
return {255, 255};
}
}
}
CommandController_Configuration::CommandController_Configuration(std::reference_wrapper<BaseConfigurationValue> *configurationValues,
size_t configurationValueCount) : configurationValues(
configurationValues), configuration_value_count(configurationValueCount) {
bool isFirstRun = BaseConfigurationValue::isFirstRun();
for (size_t i = 0; i < configuration_value_count; i++) {
if (isFirstRun) {
configurationValues[i].get().store();
} else {
configurationValues[i].get().load();
}
}
CommandController_Configuration::CommandController_Configuration(
std::reference_wrapper<BaseConfigurationValue> *configurationValues,
size_t configurationValueCount) : configurationValues(
configurationValues), configuration_value_count(configurationValueCount) {
bool isFirstRun = BaseConfigurationValue::isFirstRun();
for (size_t i = 0; i < configuration_value_count; i++) {
if (isFirstRun) {
configurationValues[i].get().store();
} else {
configurationValues[i].get().load();
}
}
if(isFirstRun) {
BaseConfigurationValue::removeFirstRun();
}
if (isFirstRun) {
BaseConfigurationValue::removeFirstRun();
}
}
......
......@@ -54,14 +54,16 @@ bool NRF24CommunicationBoundary::sendMessage(const Response &message) {
nrfRadio.setAutoAck(0, true);
}
bool sendStatus = nrfRadio.write(&message, message.size());
uint8_t data[message.size()];
message.write_bytes(data);
bool sendStatus = nrfRadio.write(data, message.size());
if(!message.broadcast) {
nrfRadio.setAutoAck(0, false);
}
nrfRadio.startListening();
Serial.println(sendStatus);
return sendStatus;
}
......
#include <core/communication/Response.hpp>
#include <cstddef>
Response::Response(uint8_t messageId, SuccessCode returnCode, uint8_t data_length, const uint8_t *dat, bool _broadcast)
: message_id(messageId),
successCode(returnCode), broadcast(_broadcast) {
for (size_t i = 0; i < 4; i++) {
data[i] = dat[i];
}
}
Response::Response(SuccessCode returnCode, uint8_t data_length, const uint8_t *dat, bool _broadcast): Response(0, returnCode, data_length, dat, _broadcast) {
}
uint8_t Response::size() const {
return 2 + data_size;
}
......@@ -47,4 +47,6 @@ void BaseConfigurationValue::load() {
set(flash_data);
}
const char *BaseConfigurationValue::get_name() {
return CONFIG_NAME;
}
......@@ -18,10 +18,11 @@
#include <utility>
////################################################################## Configuration ###################################
PMSVSettings default_settings{
0,
1.9f,
1.434684f,
1.4545f,
0,
180
......@@ -137,6 +138,8 @@ std::reference_wrapper<BaseConfigurationValue> configurationValues[]{
void setup() {
Serial.begin(9600);
Serial.println("SWEK");
// Set up Boundaries
CommandController_Configuration flashCommandController{configurationValues, lengthof(configurationValues)};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment