#pragma once #include "ur_modern_driver/log.h" #include "ur_modern_driver/parser.h" #include "ur_modern_driver/ur/state.h" #include "ur_modern_driver/ur/rt_state.h" #include "ur_modern_driver/ur/messages.h" template class URStateParser : public Parser { std::unique_ptr parse(BinParser &bp) { int32_t packet_size; message_type type; bp.parse(packet_size); bp.parse(type); if(type != message_type::ROBOT_STATE) { LOG_ERROR("Invalid message type recieved: %u", static_cast(type)); return std::unique_ptr(nullptr); } std::unique_ptr obj(new T); if(obj->parse_with(bp)) return obj; return std::unique_ptr(nullptr); } }; template class URRTStateParser : public Parser { std::unique_ptr parse(BinParser &bp) { int32_t packet_size = bp.peek(); if(!bp.check_size(packet_size)) { LOG_ERROR("Buffer len shorter than expected packet length"); return std::unique_ptr(nullptr); } bp.parse(packet_size); //consumes the peeked data std::unique_ptr obj(new T); if(obj->parse_with(bp)) return obj; return std::unique_ptr(nullptr); } }; class URMessageParser : public Parser { std::unique_ptr parse(BinParser &bp) { int32_t packet_size = bp.peek(); message_type type; if(!bp.check_size(packet_size)) { LOG_ERROR("Buffer len shorter than expected packet length"); return std::unique_ptr(nullptr); } bp.parse(packet_size); //consumes the peeked data bp.parse(type); if(type != message_type::ROBOT_MESSAGE) { LOG_ERROR("Invalid message type recieved: %u", static_cast(type)); return std::unique_ptr(nullptr); } uint64_t timestamp; uint8_t source; robot_message_type message_type; bp.parse(timestamp); bp.parse(source); bp.parse(message_type); std::unique_ptr obj(nullptr); switch(message_type) { case robot_message_type::ROBOT_MESSAGE_VERSION: VersionMessage *vm = new VersionMessage(); if(vm->parse_with(bp)) obj.reset(vm); break; } return obj; } };