diff --git a/communication/communication.cpp b/communication/communication.cpp index fecf9f8..4ccda4c 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -46,19 +46,15 @@ bool Communication::close() { } bool Communication::sendPacket(std::vector& bytes) { - // std::cout << "\nWriting " << bytes.size() << " bytes\n" << std::hex; - // for(size_t i = 0; i < bytes.size(); i++) - // std::cout << std::setw(2) << std::setfill('0') << (int)bytes[i] << (i % 16 == 15 ? '\n' : ' '); - // std::cout << '\n' << std::endl << std::dec; + // This is here so that other communication types (like multichannel) can override it return rawWrite(bytes); } bool Communication::sendCommand(Command cmd, std::vector arguments) { - auto msg = std::make_shared(); - msg->network = Network::NetID::Main51; - msg->data = std::move(arguments); - msg->data.insert(msg->data.begin(), (uint8_t)cmd); - auto packet = encoder->encode(msg); + std::vector packet; + if(!encoder->encode(packet, cmd, arguments)) + return false; + return sendPacket(packet); } diff --git a/communication/encoder.cpp b/communication/encoder.cpp index c08bc47..0a320ad 100644 --- a/communication/encoder.cpp +++ b/communication/encoder.cpp @@ -2,8 +2,10 @@ using namespace icsneo; -std::vector Encoder::encode(const std::shared_ptr& message) { +bool Encoder::encode(std::vector& result, const std::shared_ptr& message) { bool shortFormat = false; + result.clear(); + switch(message->network.getType()) { // case Network::Type::CAN: { // if(message->data.size() < 24) @@ -39,7 +41,8 @@ std::vector Encoder::encode(const std::shared_ptr& message) { (uint8_t)size, // Size, little endian 16-bit (uint8_t)(size >> 8) }); - return packetizer->packetWrap(message->data, shortFormat); + result = packetizer->packetWrap(message->data, shortFormat); + return true; } else { shortFormat = true; } @@ -49,11 +52,11 @@ std::vector Encoder::encode(const std::shared_ptr& message) { // We expect the network byte to be populated already in data, but not the length uint16_t length = uint16_t(message->data.size()) - 1; message->data.insert(message->data.begin(), {(uint8_t)length, (uint8_t)(length >> 8)}); + break; } default: - break; + return false; } - break; } if(shortFormat) { @@ -71,14 +74,15 @@ std::vector Encoder::encode(const std::shared_ptr& message) { }); } - return packetizer->packetWrap(message->data, shortFormat); + result = packetizer->packetWrap(message->data, shortFormat); + return true; } -std::vector Encoder::encode(Command cmd, std::vector arguments) { +bool Encoder::encode(std::vector& result, Command cmd, std::vector arguments) { auto msg = std::make_shared(); msg->network = Network::NetID::Main51; - msg->data.resize(arguments.size() + 1); + msg->data.reserve(arguments.size() + 1); msg->data.push_back((uint8_t)cmd); msg->data.insert(msg->data.end(), std::make_move_iterator(arguments.begin()), std::make_move_iterator(arguments.end())); - return encode(msg); + return encode(result, msg); } \ No newline at end of file diff --git a/communication/include/encoder.h b/communication/include/encoder.h index 6f76ef5..be487b8 100644 --- a/communication/include/encoder.h +++ b/communication/include/encoder.h @@ -18,9 +18,9 @@ namespace icsneo { class Encoder { public: Encoder(std::shared_ptr packetizerInstance) : packetizer(packetizerInstance) {} - std::vector encode(const std::shared_ptr& message); - std::vector encode(Command cmd, bool boolean) { return encode(cmd, std::vector({ (uint8_t)boolean })); } - std::vector encode(Command cmd, std::vector arguments = {}); + bool encode(std::vector& result, const std::shared_ptr& message); + bool encode(std::vector& result, Command cmd, bool boolean) { return encode(result, cmd, std::vector({ (uint8_t)boolean })); } + bool encode(std::vector& result, Command cmd, std::vector arguments = {}); private: std::shared_ptr packetizer; diff --git a/device/device.cpp b/device/device.cpp index 346dbc0..967d94b 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -210,6 +210,9 @@ void Device::updateLEDState() { * This old command type is not really used anywhere else. */ msg->data = {0x00, 0x06, uint8_t(ledState)}; - auto packet = com->encoder->encode(msg); + std::vector packet; + if(!com->encoder->encode(packet, msg)) + return; + com->sendPacket(packet); } \ No newline at end of file