#ifndef __COMMUNICATION_H_ #define __COMMUNICATION_H_ #include "communication/include/icommunication.h" #include "communication/include/command.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); virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector({ (uint8_t)boolean })); } virtual bool sendCommand(Command cmd, std::vector arguments = {}); bool getSettingsSync(std::vector& data, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); bool getSerialNumberSync(std::string& serial, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); int addMessageCallback(const MessageCallback& cb); bool removeMessageCallback(int id); std::shared_ptr waitForMessageSync(MessageFilter f = MessageFilter(), std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); 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