Refactor ICSChecksum and packetWrap into the Packetizer
parent
aa25ba1728
commit
28de70aa05
|
|
@ -14,24 +14,6 @@ using namespace icsneo;
|
||||||
|
|
||||||
int Communication::messageCallbackIDCounter = 1;
|
int Communication::messageCallbackIDCounter = 1;
|
||||||
|
|
||||||
uint8_t Communication::ICSChecksum(const std::vector<uint8_t>& data) {
|
|
||||||
uint32_t checksum = 0;
|
|
||||||
for(auto i = 0; i < data.size(); i++)
|
|
||||||
checksum += data[i];
|
|
||||||
checksum = ~checksum;
|
|
||||||
checksum++;
|
|
||||||
return (uint8_t)checksum;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint8_t>& Communication::packetWrap(std::vector<uint8_t>& data, bool addChecksum) {
|
|
||||||
if(addChecksum)
|
|
||||||
data.push_back(ICSChecksum(data));
|
|
||||||
data.insert(data.begin(), 0xAA);
|
|
||||||
if(align16bit && data.size() % 2 == 1)
|
|
||||||
data.push_back('A');
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Communication::open() {
|
bool Communication::open() {
|
||||||
if(isOpen)
|
if(isOpen)
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -62,7 +44,7 @@ bool Communication::close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Communication::sendPacket(std::vector<uint8_t>& bytes) {
|
bool Communication::sendPacket(std::vector<uint8_t>& bytes) {
|
||||||
return impl->write(Communication::packetWrap(bytes));
|
return impl->write(packetizer->packetWrap(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Communication::sendCommand(Command cmd, std::vector<uint8_t> arguments) {
|
bool Communication::sendCommand(Command cmd, std::vector<uint8_t> arguments) {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include "communication/include/command.h"
|
#include "communication/include/command.h"
|
||||||
#include "communication/include/network.h"
|
#include "communication/include/network.h"
|
||||||
#include "communication/include/packet.h"
|
#include "communication/include/packet.h"
|
||||||
|
#include "communication/include/packetizer.h"
|
||||||
|
#include "communication/include/messagedecoder.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
@ -16,8 +18,6 @@ namespace icsneo {
|
||||||
|
|
||||||
class Communication {
|
class Communication {
|
||||||
public:
|
public:
|
||||||
static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
|
|
||||||
|
|
||||||
Communication(std::shared_ptr<ICommunication> com, std::shared_ptr<Packetizer> p, std::shared_ptr<MessageDecoder> md) : impl(com), packetizer(p), decoder(md) {}
|
Communication(std::shared_ptr<ICommunication> com, std::shared_ptr<Packetizer> p, std::shared_ptr<MessageDecoder> md) : impl(com), packetizer(p), decoder(md) {}
|
||||||
virtual ~Communication() { close(); }
|
virtual ~Communication() { close(); }
|
||||||
|
|
||||||
|
|
@ -26,7 +26,6 @@ public:
|
||||||
virtual void spawnThreads();
|
virtual void spawnThreads();
|
||||||
virtual void joinThreads();
|
virtual void joinThreads();
|
||||||
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
|
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
|
||||||
std::vector<uint8_t>& packetWrap(std::vector<uint8_t>& data, bool addChecksum = true);
|
|
||||||
bool sendPacket(std::vector<uint8_t>& bytes);
|
bool sendPacket(std::vector<uint8_t>& bytes);
|
||||||
|
|
||||||
virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
|
virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector<uint8_t>({ (uint8_t)boolean })); }
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef __PACKETIZER_H_
|
#ifndef __PACKETIZER_H_
|
||||||
#define __PACKETIZER_H_
|
#define __PACKETIZER_H_
|
||||||
|
|
||||||
#include "communication/include/communication.h"
|
#include "communication/include/packet.h"
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
@ -10,8 +10,14 @@ namespace icsneo {
|
||||||
|
|
||||||
class Packetizer {
|
class Packetizer {
|
||||||
public:
|
public:
|
||||||
|
static uint8_t ICSChecksum(const std::vector<uint8_t>& data);
|
||||||
|
std::vector<uint8_t>& packetWrap(std::vector<uint8_t>& data);
|
||||||
|
|
||||||
bool input(const std::vector<uint8_t>& bytes);
|
bool input(const std::vector<uint8_t>& bytes);
|
||||||
std::vector<std::shared_ptr<Packet>> output();
|
std::vector<std::shared_ptr<Packet>> output();
|
||||||
|
|
||||||
|
bool disableChecksum = false; // Even for short packets
|
||||||
|
bool align16bit = true; // Not needed for Gigalog, Galaxy, etc and newer
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class ReadState {
|
enum class ReadState {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ bool MultiChannelCommunication::sendCommand(Command cmd, std::vector<uint8_t> ar
|
||||||
for(auto& b : arguments)
|
for(auto& b : arguments)
|
||||||
bytes.push_back(b);
|
bytes.push_back(b);
|
||||||
bytes.insert(bytes.begin(), 0xB | ((uint8_t)bytes.size() << 4));
|
bytes.insert(bytes.begin(), 0xB | ((uint8_t)bytes.size() << 4));
|
||||||
bytes = Communication::packetWrap(bytes);
|
bytes = packetizer->packetWrap(bytes);
|
||||||
bytes.insert(bytes.begin(), {(uint8_t)CommandType::HostPC_to_Vnet1, (uint8_t)bytes.size(), (uint8_t)(bytes.size() >> 8)});
|
bytes.insert(bytes.begin(), {(uint8_t)CommandType::HostPC_to_Vnet1, (uint8_t)bytes.size(), (uint8_t)(bytes.size() >> 8)});
|
||||||
return rawWrite(bytes);
|
return rawWrite(bytes);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,24 @@
|
||||||
|
|
||||||
using namespace icsneo;
|
using namespace icsneo;
|
||||||
|
|
||||||
|
uint8_t Packetizer::ICSChecksum(const std::vector<uint8_t>& data) {
|
||||||
|
uint32_t checksum = 0;
|
||||||
|
for(auto i = 0; i < data.size(); i++)
|
||||||
|
checksum += data[i];
|
||||||
|
checksum = ~checksum;
|
||||||
|
checksum++;
|
||||||
|
return (uint8_t)checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t>& Packetizer::packetWrap(std::vector<uint8_t>& data) {
|
||||||
|
if(!disableChecksum)
|
||||||
|
data.push_back(ICSChecksum(data));
|
||||||
|
data.insert(data.begin(), 0xAA);
|
||||||
|
if(align16bit && data.size() % 2 == 1)
|
||||||
|
data.push_back('A');
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
bool Packetizer::input(const std::vector<uint8_t>& inputBytes) {
|
bool Packetizer::input(const std::vector<uint8_t>& inputBytes) {
|
||||||
bool haveEnoughData = true;
|
bool haveEnoughData = true;
|
||||||
bytes.insert(bytes.end(), inputBytes.begin(), inputBytes.end());
|
bytes.insert(bytes.end(), inputBytes.begin(), inputBytes.end());
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "platform/windows/include/pcap.h"
|
#include "platform/windows/include/pcap.h"
|
||||||
#include "communication/include/network.h"
|
#include "communication/include/network.h"
|
||||||
#include "communication/include/communication.h"
|
#include "communication/include/communication.h"
|
||||||
|
#include "communication/include/packetizer.h"
|
||||||
#include <pcap.h>
|
#include <pcap.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#pragma comment(lib, "IPHLPAPI.lib")
|
#pragma comment(lib, "IPHLPAPI.lib")
|
||||||
|
|
@ -108,9 +109,9 @@ std::vector<neodevice_t> PCAP::FindByProduct(int product) {
|
||||||
requestPacket.payload.reserve(4);
|
requestPacket.payload.reserve(4);
|
||||||
requestPacket.payload = {
|
requestPacket.payload = {
|
||||||
((1 << 4) | (uint8_t)Network::NetID::Main51), // Packet size of 1 on NETID_MAIN51
|
((1 << 4) | (uint8_t)Network::NetID::Main51), // Packet size of 1 on NETID_MAIN51
|
||||||
(uint8_t)Communication::Command::RequestSerialNumber
|
(uint8_t)Command::RequestSerialNumber
|
||||||
};
|
};
|
||||||
requestPacket.payload.push_back(Communication::ICSChecksum(requestPacket.payload));
|
requestPacket.payload.push_back(Packetizer::ICSChecksum(requestPacket.payload));
|
||||||
requestPacket.payload.insert(requestPacket.payload.begin(), 0xAA);
|
requestPacket.payload.insert(requestPacket.payload.begin(), 0xAA);
|
||||||
|
|
||||||
auto bs = requestPacket.getBytestream();
|
auto bs = requestPacket.getBytestream();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue