#ifndef __COMMUNICATION_H_ #define __COMMUNICATION_H_ #include "communication/include/icommunication.h" #include "communication/include/network.h" #include "communication/include/messagecallback.h" #include #include #include #include #include #include namespace icsneo { class Communication { public: static uint8_t ICSChecksum(const std::vector& data); Communication(std::shared_ptr com) : impl(com) {} virtual ~Communication() { close(); } bool open(); bool close(); virtual void spawnThreads(); virtual void joinThreads(); bool rawWrite(const std::vector& bytes) { return impl->write(bytes); } std::vector& packetWrap(std::vector& data, bool addChecksum = true); bool sendPacket(std::vector& bytes); enum class Command : uint8_t { EnableNetworkCommunication = 0x07, RequestSerialNumber = 0xA1 }; virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector({ (uint8_t)boolean })); } virtual bool sendCommand(Command cmd, std::vector arguments = {}); int addMessageCallback(const MessageCallback& cb); bool removeMessageCallback(int id); void setAlign16Bit(bool enable) { align16bit = enable; } class Packet { public: Network network; std::vector data; }; protected: std::shared_ptr impl; static int messageCallbackIDCounter; std::map messageCallbacks; std::atomic closing{false}; private: bool isOpen = false; bool align16bit = true; // Not needed for Gigalog, Galaxy, etc and newer std::thread readTaskThread; void readTask(); }; }; #endif