Encoder allowed to fail, preparation for upcoming CAN transmit
parent
92839c22a5
commit
912b11ce30
|
|
@ -46,19 +46,15 @@ bool Communication::close() {
|
|||
}
|
||||
|
||||
bool Communication::sendPacket(std::vector<uint8_t>& 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<uint8_t> arguments) {
|
||||
auto msg = std::make_shared<Message>();
|
||||
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<uint8_t> packet;
|
||||
if(!encoder->encode(packet, cmd, arguments))
|
||||
return false;
|
||||
|
||||
return sendPacket(packet);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
using namespace icsneo;
|
||||
|
||||
std::vector<uint8_t> Encoder::encode(const std::shared_ptr<Message>& message) {
|
||||
bool Encoder::encode(std::vector<uint8_t>& result, const std::shared_ptr<Message>& 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<uint8_t> Encoder::encode(const std::shared_ptr<Message>& 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<uint8_t> Encoder::encode(const std::shared_ptr<Message>& 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<uint8_t> Encoder::encode(const std::shared_ptr<Message>& message) {
|
|||
});
|
||||
}
|
||||
|
||||
return packetizer->packetWrap(message->data, shortFormat);
|
||||
result = packetizer->packetWrap(message->data, shortFormat);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Encoder::encode(Command cmd, std::vector<uint8_t> arguments) {
|
||||
bool Encoder::encode(std::vector<uint8_t>& result, Command cmd, std::vector<uint8_t> arguments) {
|
||||
auto msg = std::make_shared<Message>();
|
||||
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);
|
||||
}
|
||||
|
|
@ -18,9 +18,9 @@ namespace icsneo {
|
|||
class Encoder {
|
||||
public:
|
||||
Encoder(std::shared_ptr<Packetizer> packetizerInstance) : packetizer(packetizerInstance) {}
|
||||
std::vector<uint8_t> encode(const std::shared_ptr<Message>& message);
|
||||
std::vector<uint8_t> encode(Command cmd, bool boolean) { return encode(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
|
||||
std::vector<uint8_t> encode(Command cmd, std::vector<uint8_t> arguments = {});
|
||||
bool encode(std::vector<uint8_t>& result, const std::shared_ptr<Message>& message);
|
||||
bool encode(std::vector<uint8_t>& result, Command cmd, bool boolean) { return encode(result, cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
|
||||
bool encode(std::vector<uint8_t>& result, Command cmd, std::vector<uint8_t> arguments = {});
|
||||
|
||||
private:
|
||||
std::shared_ptr<Packetizer> packetizer;
|
||||
|
|
|
|||
|
|
@ -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<uint8_t> packet;
|
||||
if(!com->encoder->encode(packet, msg))
|
||||
return;
|
||||
|
||||
com->sendPacket(packet);
|
||||
}
|
||||
Loading…
Reference in New Issue