26 #include <boost/bind.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/make_shared.hpp>
29 #include <boost/random/mersenne_twister.hpp>
30 #include <boost/random/uniform_int_distribution.hpp>
31 #include <console_bridge/console.h>
42 using boost::shared_ptr;
43 using boost::make_shared;
48 using serialization::BufferReader;
49 using serialization::BufferWriter;
53 : socket_(socket), io_socket_(io_socket), session_id_(0),
54 my_vendor_id_(vendor_id), my_serial_num_(serial_num)
57 boost::random::mt19937 gen;
58 gen.seed(std::time(0));
59 boost::random::uniform_int_distribution<> dist(0, 0xFFFF);
82 CONSOLE_BRIDGE_logInform(
"Resolving hostname and connecting socket");
87 CONSOLE_BRIDGE_logInform(
"Creating and sending the registration message");
88 shared_ptr<RegisterSessionData> reg_data = make_shared<RegisterSessionData>();
97 catch (std::length_error ex)
101 CONSOLE_BRIDGE_logError(
"Could not parse response when registering session: %s", ex.what());
102 throw std::runtime_error(
"Invalid response received registering session");
104 catch (std::logic_error ex)
108 CONSOLE_BRIDGE_logError(
"Error in registration response: %s", ex.what());
109 throw std::runtime_error(
"Error in registration response");
114 CONSOLE_BRIDGE_logWarn(
"Registration message received with wrong size. Expected %zu bytes, received %u",
118 bool response_valid =
false;
122 response_valid =
true;
124 catch (std::length_error ex)
126 CONSOLE_BRIDGE_logWarn(
"Registration message too short, ignoring");
128 catch (std::logic_error ex)
130 CONSOLE_BRIDGE_logWarn(
"Could not parse registration response: %s", ex.what());
135 CONSOLE_BRIDGE_logError(
"Error: Wrong Ethernet Industrial Protocol Version. Expected %u got %u",
139 throw std::runtime_error(
"Received wrong Ethernet IP Protocol Version on registration");
141 if (response_valid && reg_data->options != 0)
143 CONSOLE_BRIDGE_logWarn(
"Registration message included non-zero options flags: %u", reg_data->options);
147 CONSOLE_BRIDGE_logInform(
"Successfully opened session ID %zu",
session_id_);
153 CONSOLE_BRIDGE_logInform(
"Closing session");
159 CONSOLE_BRIDGE_logInform(
"Session closed");
168 CONSOLE_BRIDGE_logDebug(
"Sending Command");
171 CONSOLE_BRIDGE_logDebug(
"Waiting for response");
173 CONSOLE_BRIDGE_logDebug(
"Received response of %zu bytes", n);
181 CONSOLE_BRIDGE_logWarn(
"Packet received with %zu bytes, but only %zu bytes used", n, reader.
getByteCount());
193 CONSOLE_BRIDGE_logError(
"Reply received with wrong command. Expected %u received %u", exp_cmd,
195 throw std::logic_error(
"Reply received with wrong command");
200 throw std::logic_error(
"Zero session handle received on registration");
204 CONSOLE_BRIDGE_logError(
"Reply received with wrong session ID. Expected %zu, received %zu",
session_id_,
206 throw std::logic_error(
"Wrong session ID received for command");
210 CONSOLE_BRIDGE_logWarn(
"Non-zero status received: %zu", pkt.
getHeader().
status);
214 CONSOLE_BRIDGE_logWarn(
"Non-zero sender context received: %zu/%zu", pkt.
getHeader().
context[0],
219 CONSOLE_BRIDGE_logWarn(
"Non-zero options received: %zu", pkt.
getHeader().
options);
226 shared_ptr<Serializable> no_data;
228 Path(class_id, instance_id, attribute_id), no_data);
237 Path(class_id, instance_id, attribute_id), data);
241 shared_ptr<Serializable> data)
243 CONSOLE_BRIDGE_logDebug(
"Creating RR Data Request");
244 shared_ptr<RRDataRequest> req_data =
245 make_shared<RRDataRequest> (service, path, data);
254 catch (std::length_error ex)
256 CONSOLE_BRIDGE_logError(
"Response packet to RR command too short: %s", ex.what());
257 throw std::runtime_error(
"Packet response to RR Data Command too short");
259 catch (std::logic_error ex)
261 CONSOLE_BRIDGE_logError(
"Invalid response to RR command: %s", ex.what());
262 throw std::runtime_error(
"Invalid packet response to RR Data Command");
270 catch (std::length_error ex)
272 CONSOLE_BRIDGE_logError(
"Response data to RR command too short: %s", ex.what());
273 throw std::runtime_error(
"Response data to RR Command too short");
275 catch (std::logic_error ex)
277 CONSOLE_BRIDGE_logError(
"Invalid data to RR command: %s", ex.what());
278 throw std::runtime_error(
"Invalid data in response to RR command");
284 CONSOLE_BRIDGE_logWarn(
"Wrong service code returned for RR Data command. Expected: %d but received %d",
290 CONSOLE_BRIDGE_logError(
"RR Data Command failed with status %d", (
int)resp_data.
getGeneralStatus());
291 throw std::runtime_error(
"RR Data Command Failed");
312 CONSOLE_BRIDGE_logError(
"Received invalid response to forward open request");
313 throw std::logic_error(
"Forward Open Response Invalid");
322 shared_ptr<ForwardCloseRequest> req =
connections_[n].createForwardCloseRequest();
328 CONSOLE_BRIDGE_logError(
"Received invalid response to forward close request");
329 throw std::logic_error(
"Forward Close Response Invalid");
337 CONSOLE_BRIDGE_logDebug(
"Receiving IO packet");
339 CONSOLE_BRIDGE_logDebug(
"Received IO of %zu bytes", n);
347 CONSOLE_BRIDGE_logWarn(
"IO packet received with %zu bytes, but only %zu bytes used", n, reader.
getByteCount());
355 CONSOLE_BRIDGE_logDebug(
"Sending CPF Packet on IO Socket");