From acfeacedfc46e04ce7873d7e7c5fc674496d2349 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Wed, 3 Oct 2018 17:04:55 -0400 Subject: [PATCH] Encoder works but needs cleanup, GS checksum is not working properly somehow --- communication/communication.cpp | 12 +++--- communication/decoder.cpp | 9 ++++- communication/encoder.cpp | 17 ++++++++- .../filter/include/main51messagefilter.h | 2 +- device/device.cpp | 6 ++- device/idevicesettings.cpp | 37 ++++++++----------- device/include/idevicesettings.h | 1 + platform/windows/include/devices.h | 22 +++++------ 8 files changed, 63 insertions(+), 43 deletions(-) diff --git a/communication/communication.cpp b/communication/communication.cpp index cbbe168..301ae1c 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -46,10 +46,10 @@ 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; + // 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; return rawWrite(bytes); } @@ -57,7 +57,7 @@ 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); + msg->data.insert(msg->data.begin(), {(uint8_t)cmd, (uint8_t)(uint16_t(cmd) >> 8)}); auto packet = encoder->encode(msg); return sendPacket(packet); } @@ -134,7 +134,7 @@ void Communication::readTask() { if(packetizer->input(readBytes)) { for(auto& packet : packetizer->output()) { auto msg = decoder->decodePacket(packet); - std::cout << "Got packet for " << msg->network << ", calling " << messageCallbacks.size() << " callbacks" << std::endl; + //std::cout << "Got packet for " << msg->network << ", calling " << messageCallbacks.size() << " callbacks" << std::endl; for(auto& cb : messageCallbacks) { // We might have closed while reading or processing if(!closing) { cb.second.callIfMatch(msg); diff --git a/communication/decoder.cpp b/communication/decoder.cpp index cd01889..a9f22b6 100644 --- a/communication/decoder.cpp +++ b/communication/decoder.cpp @@ -51,6 +51,13 @@ std::shared_ptr Decoder::decodePacket(const std::shared_ptr& pa memcpy(msg->pcbSerial, packet->data.data() + 15, sizeof(msg->pcbSerial)); return msg; } + break; + default: + auto msg = std::make_shared(); + msg->network = packet->network; + msg->command = Command(packet->data[0]); + msg->data.insert(msg->data.begin(), packet->data.begin() + 1, packet->data.end()); + return msg; } break; } @@ -64,7 +71,7 @@ std::shared_ptr Decoder::decodePacket(const std::shared_ptr& pa */ uint16_t length = packet->data[0] | (packet->data[1] << 8); packet->network = Network(packet->data[2] & 0xF); - std::cout << "Got an old format packet, decoding against " << packet->network << std::endl; + //std::cout << "Got an old format packet, decoding against " << packet->network << std::endl; packet->data.erase(packet->data.begin(), packet->data.begin() + 3); if(packet->data.size() != length) packet->data.resize(length); diff --git a/communication/encoder.cpp b/communication/encoder.cpp index 9100c24..fdf5183 100644 --- a/communication/encoder.cpp +++ b/communication/encoder.cpp @@ -25,13 +25,26 @@ std::vector Encoder::encode(const std::shared_ptr& message) { default: switch(message->network.getNetID()) { case Network::NetID::Main51: - if(message->data.size() <= 0xF) + if(message->data.size() > 0xF) { + // Main51 can be sent as a long message without setting the NetID to RED first + // Size in long format is the size of the entire packet + // So +1 for AA header, +1 for short format header, and +2 for long format size + uint16_t size = uint16_t(message->data.size()) + 1 + 1 + 2; + size += 1; // Even though we are not including the NetID bytes, the device expects them to be counted in the length + message->data.insert(message->data.begin(), { + (uint8_t)Network::NetID::Main51, // 0x0B for long message + (uint8_t)size, // Size, little endian 16-bit + (uint8_t)(size >> 8) + }); + return packetizer->packetWrap(message->data, shortFormat); + } else { shortFormat = true; + } break; case Network::NetID::RED_OLDFORMAT: { // See the decoder for an explanation // We expect the network byte to be populated already in data, but not the length - uint16_t length = message->data.size() - 1; + uint16_t length = uint16_t(message->data.size()) - 1; message->data.insert(message->data.begin(), {(uint8_t)length, (uint8_t)(length >> 8)}); } default: diff --git a/communication/message/filter/include/main51messagefilter.h b/communication/message/filter/include/main51messagefilter.h index d1b2ee0..cf09bab 100644 --- a/communication/message/filter/include/main51messagefilter.h +++ b/communication/message/filter/include/main51messagefilter.h @@ -17,7 +17,7 @@ public: bool match(const std::shared_ptr& message) const { if(!MessageFilter::match(message)) { - std::cout << "message filter did not match base for " << message->network << std::endl; + //std::cout << "message filter did not match base for " << message->network << std::endl; return false; } const auto main51Message = std::dynamic_pointer_cast(message); diff --git a/device/device.cpp b/device/device.cpp index 103361d..ed99c52 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -150,8 +150,12 @@ bool Device::open() { return false; } - if(settings) + bool settingsNecessary = bool(settings); // Check if the shared_ptr exists + if(settingsNecessary) { settings->refresh(); + if(!settings || !settings->ok()) + return false; + } return true; } diff --git a/device/idevicesettings.cpp b/device/idevicesettings.cpp index 1e2019d..686706e 100644 --- a/device/idevicesettings.cpp +++ b/device/idevicesettings.cpp @@ -88,24 +88,20 @@ bool IDeviceSettings::send() { bytestream.resize(6 + structSize); bytestream[0] = GS_VERSION; bytestream[1] = GS_VERSION >> 8; - bytestream[2] = structSize; - bytestream[3] = structSize >> 8; + bytestream[2] = (uint8_t)structSize; + bytestream[3] = (uint8_t)(structSize >> 8); uint16_t gs_checksum = CalculateGSChecksum(settings); - bytestream[4] = gs_checksum; - bytestream[5] = gs_checksum >> 8; + bytestream[4] = (uint8_t)gs_checksum; + bytestream[5] = (uint8_t)(gs_checksum >> 8); memcpy(bytestream.data() + 6, getRawStructurePointer(), structSize); + com->sendCommand(Command::SetSettings, bytestream); - std::shared_ptr msg = com->waitForMessageSync(std::make_shared(), std::chrono::milliseconds(3000)); - if(!msg) - std::cout << "No response from device for set settings" << std::endl; - if(msg && msg->data[1] != 1) - std::cout << "Response was incorrect for set settings: " << (int)msg->data[1] << std::endl; - if(!msg || msg->data[1] != 1) { // The second byte of the response carries the result, 1 being success - refresh(); // Refresh our buffer with what the device has - return false; - } - - return true; + std::shared_ptr msg = com->waitForMessageSync(std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000)); + + if(!msg || msg->data[0] != 1) + refresh(); // Refr + + return (msg && msg->data[0] == 1); // Device sends 0x01 for success } bool IDeviceSettings::commit() { @@ -113,13 +109,12 @@ bool IDeviceSettings::commit() { return false; com->sendCommand(Command::SaveSettings); - std::shared_ptr msg = com->waitForMessageSync(Main51MessageFilter(Command::SaveSettings), std::chrono::milliseconds(500)); - if(!msg || msg->data[1] != 1) { // The second byte of the response carries the result, 1 being success - refresh(); // Refresh our buffer with what the device has - return false; - } + std::shared_ptr msg = com->waitForMessageSync(std::make_shared(Command::SaveSettings), std::chrono::milliseconds(5000)); + + if(!msg || msg->data[0] != 1) + refresh(); // Refresh our buffer with what the device has, whether we were successful or not - return true; + return (msg && msg->data[0] == 1); // Device sends 0x01 for success } bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { diff --git a/device/include/idevicesettings.h b/device/include/idevicesettings.h index 787eeea..2673224 100644 --- a/device/include/idevicesettings.h +++ b/device/include/idevicesettings.h @@ -282,6 +282,7 @@ public: IDeviceSettings(std::shared_ptr com, size_t size) : com(com), structSize(size) {} virtual ~IDeviceSettings() {} + bool ok() { return settingsLoaded; } void refresh(); // Get from device bool send(); // Send to device, device keeps settings in volatile RAM until power cycle diff --git a/platform/windows/include/devices.h b/platform/windows/include/devices.h index 290ebe3..5219fcf 100644 --- a/platform/windows/include/devices.h +++ b/platform/windows/include/devices.h @@ -1,18 +1,18 @@ #ifndef __DEVICES_WINDOWS_H_ #define __DEVICES_WINDOWS_H_ -#include "device/neoobd2pro/include/neoobd2pro.h" -#include "device/neoobd2sim/include/neoobd2sim.h" -#include "device/neovifire/include/neovifire.h" +// #include "device/neoobd2pro/include/neoobd2pro.h" +// #include "device/neoobd2sim/include/neoobd2sim.h" +// #include "device/neovifire/include/neovifire.h" #include "device/neovifire2/include/neovifire2eth.h" #include "device/neovifire2/include/neovifire2usb.h" -#include "device/plasion/include/neoviion.h" -#include "device/plasion/include/neoviplasma.h" -#include "device/radgalaxy/include/radgalaxy.h" -#include "device/radstar2/include/radstar2.h" -#include "device/radsupermoon/include/radsupermoon.h" -#include "device/valuecan3/include/valuecan3.h" -#include "device/valuecan4/include/valuecan4.h" -#include "device/vividcan/include/vividcan.h" +// #include "device/plasion/include/neoviion.h" +// #include "device/plasion/include/neoviplasma.h" +// #include "device/radgalaxy/include/radgalaxy.h" +// #include "device/radstar2/include/radstar2.h" +// #include "device/radsupermoon/include/radsupermoon.h" +// #include "device/valuecan3/include/valuecan3.h" +// #include "device/valuecan4/include/valuecan4.h" +// #include "device/vividcan/include/vividcan.h" #endif \ No newline at end of file