Start work on device settings communication

This commit is contained in:
Paul Hollinsky
2018-09-24 16:57:14 -04:00
parent af4f4894f5
commit da4d9e46be
9 changed files with 574 additions and 13 deletions
+50 -1
View File
@@ -4,6 +4,8 @@
#include <queue>
#include <iomanip>
#include <cstring>
#include <mutex>
#include <condition_variable>
#include "communication/include/messagedecoder.h"
#include "communication/include/packetizer.h"
@@ -67,10 +69,34 @@ bool Communication::sendCommand(Communication::Command cmd, std::vector<uint8_t>
bytes.push_back((uint8_t)cmd);
for(auto& b : arguments)
bytes.push_back(b);
bytes.insert(bytes.begin(), 0xB | ((uint8_t)bytes.size() << 4));
bytes.insert(bytes.begin(), (uint8_t)Network::NetID::Main51 | ((uint8_t)bytes.size() << 4));
return sendPacket(bytes);
}
bool Communication::getSettingsSync(std::vector<uint8_t>& data, std::chrono::milliseconds timeout) {
sendCommand(Command::GetSettings);
std::shared_ptr<Message> msg = waitForMessageSync(MessageFilter(Network::NetID::RED_READ_BAUD_SETTINGS), timeout);
if(!msg)
return false;
data = std::move(msg->data);
return true;
}
bool Communication::getSerialNumberSync(std::string& serial, std::chrono::milliseconds timeout) {
sendCommand(Command::RequestSerialNumber);
std::shared_ptr<Message> msg = waitForMessageSync(MessageFilter(Network::NetID::Main51), timeout);
if(!msg)
return false;
std::cout << "Got " << msg->data.size() << " bytes" << std::endl;
for(size_t i = 0; i < msg->data.size(); i++) {
std::cout << std::hex << (int)msg->data[i] << ' ' << std::dec;
if(i % 16 == 15)
std::cout << std::endl;
}
}
int Communication::addMessageCallback(const MessageCallback& cb) {
messageCallbacks.insert(std::make_pair(messageCallbackIDCounter, cb));
return messageCallbackIDCounter++;
@@ -85,6 +111,29 @@ bool Communication::removeMessageCallback(int id) {
}
}
std::shared_ptr<Message> Communication::waitForMessageSync(MessageFilter f, std::chrono::milliseconds timeout) {
std::mutex m;
std::condition_variable cv;
std::shared_ptr<Message> returnedMessage;
int cb = addMessageCallback(MessageCallback([&m, &returnedMessage, &cv](std::shared_ptr<Message> message) {
{
std::lock_guard<std::mutex> lk(m);
returnedMessage = message;
}
cv.notify_one();
}, f));
// We have now added the callback, wait for it to return from the other thread
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, timeout, [&returnedMessage]{ return !!returnedMessage; }); // `!!shared_ptr` checks if the ptr has a value
// We don't actually check that we got a message, because either way we want to remove the callback (since it should only happen once)
removeMessageCallback(cb);
// Then we either will return the message we got or we will return the empty shared_ptr, caller responsible for checking
return returnedMessage;
}
void Communication::readTask() {
std::vector<uint8_t> readBytes;
Packetizer packetizer;
+7 -1
View File
@@ -30,13 +30,19 @@ public:
enum class Command : uint8_t {
EnableNetworkCommunication = 0x07,
RequestSerialNumber = 0xA1
RequestSerialNumber = 0xA1,
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ
SaveSettings = 0xA6
};
virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
virtual bool sendCommand(Command cmd, std::vector<uint8_t> arguments = {});
bool getSettingsSync(std::vector<uint8_t>& data, std::chrono::milliseconds timeout = std::chrono::milliseconds(10));
bool getSerialNumberSync(std::string& serial, std::chrono::milliseconds timeout = std::chrono::milliseconds(10));
int addMessageCallback(const MessageCallback& cb);
bool removeMessageCallback(int id);
std::shared_ptr<Message> waitForMessageSync(MessageFilter f = MessageFilter(), std::chrono::milliseconds timeout = std::chrono::milliseconds(10));
void setAlign16Bit(bool enable) { align16bit = enable; }
+72 -7
View File
@@ -29,6 +29,32 @@ public:
OP_Ethernet1 = 17,
OP_Ethernet2 = 18,
OP_Ethernet3 = 19,
// START Device Command Returns
// When we send a command, the device returns on one of these, depending on command
RED_EXT_MEMORYREAD = 20,
RED_INT_MEMORYREAD = 21,
RED_DFLASH_READ = 22,
RED_SDCARD_READ = 23,
CAN_ERRBITS = 24,
RED_DFLASH_WRITE_DONE = 25,
RED_WAVE_CAN1_LOGICAL = 26,
RED_WAVE_CAN2_LOGICAL = 27,
RED_WAVE_LIN1_LOGICAL = 28,
RED_WAVE_LIN2_LOGICAL = 29,
RED_WAVE_LIN1_ANALOG = 30,
RED_WAVE_LIN2_ANALOG = 31,
RED_WAVE_MISC_ANALOG = 32,
RED_WAVE_MISCDIO2_LOGICAL = 33,
RED_NETWORK_COM_ENABLE_EX = 34,
RED_NEOVI_NETWORK = 35,
RED_READ_BAUD_SETTINGS = 36,
RED_OLDFORMAT = 37,
RED_SCOPE_CAPTURE = 38,
RED_HARDWARE_EXCEP = 39,
RED_GET_RTC = 40,
// END Device Command Returns
ISO3 = 41,
HSCAN2 = 42,
HSCAN3 = 44,
@@ -38,8 +64,8 @@ public:
LIN2 = 48,
LIN3 = 49,
LIN4 = 50,
MOST = 51,
Red_App_Error = 52,
// MOST = 51, Old and unused
RED_App_Error = 52,
CGI = 53,
Reset_Status = 54,
FB_Status = 55,
@@ -143,7 +169,6 @@ public:
case NetID::FlexRay2a:
case NetID::FlexRay2b:
return Type::FlexRay;
case NetID::MOST:
case NetID::MOST25:
case NetID::MOST50:
case NetID::MOST150:
@@ -196,6 +221,48 @@ public:
return "Ethernet 2";
case NetID::OP_Ethernet3:
return "Ethernet 3";
case NetID::RED_EXT_MEMORYREAD:
return "RED_EXT_MEMORYREAD";
case NetID::RED_INT_MEMORYREAD:
return "RED_INT_MEMORYREAD";
case NetID::RED_DFLASH_READ:
return "RED_DFLASH_READ";
case NetID::RED_SDCARD_READ:
return "RED_SDCARD_READ";
case NetID::CAN_ERRBITS:
return "CAN_ERRBITS";
case NetID::RED_DFLASH_WRITE_DONE:
return "RED_DFLASH_WRITE_DONE";
case NetID::RED_WAVE_CAN1_LOGICAL:
return "RED_WAVE_CAN1_LOGICAL";
case NetID::RED_WAVE_CAN2_LOGICAL:
return "RED_WAVE_CAN2_LOGICAL";
case NetID::RED_WAVE_LIN1_LOGICAL:
return "RED_WAVE_LIN1_LOGICAL";
case NetID::RED_WAVE_LIN2_LOGICAL:
return "RED_WAVE_LIN2_LOGICAL";
case NetID::RED_WAVE_LIN1_ANALOG:
return "RED_WAVE_LIN1_ANALOG";
case NetID::RED_WAVE_LIN2_ANALOG:
return "RED_WAVE_LIN2_ANALOG";
case NetID::RED_WAVE_MISC_ANALOG:
return "RED_WAVE_MISC_ANALOG";
case NetID::RED_WAVE_MISCDIO2_LOGICAL:
return "RED_WAVE_MISCDIO2_LOGICAL";
case NetID::RED_NETWORK_COM_ENABLE_EX:
return "RED_NETWORK_COM_ENABLE_EX";
case NetID::RED_NEOVI_NETWORK:
return "RED_NEOVI_NETWORK";
case NetID::RED_READ_BAUD_SETTINGS:
return "RED_READ_BAUD_SETTINGS";
case NetID::RED_OLDFORMAT:
return "RED_OLDFORMAT";
case NetID::RED_SCOPE_CAPTURE:
return "RED_SCOPE_CAPTURE";
case NetID::RED_HARDWARE_EXCEP:
return "RED_HARDWARE_EXCEP";
case NetID::RED_GET_RTC:
return "RED_GET_RTC";
case NetID::ISO3:
return "ISO 3";
case NetID::HSCAN2:
@@ -214,10 +281,8 @@ public:
return "LIN 3";
case NetID::LIN4:
return "LIN 4";
case NetID::MOST:
return "MOST";
case NetID::Red_App_Error:
return "Red App Error";
case NetID::RED_App_Error:
return "App Error";
case NetID::CGI:
return "CGI";
case NetID::Reset_Status: