27 #include <boost/bind.hpp> 39 using boost::asio::ip::udp;
46 using serialization::BufferReader;
47 using serialization::BufferWriter;
49 IOScanner::IOScanner(io_service& io_service,
string hostname)
50 : socket_(io_service), hostname_(hostname)
52 cout <<
"Opening UDP socket... ";
56 boost::asio::placeholders::error,
57 boost::asio::placeholders::bytes_transferred));
58 cout <<
"done." << endl;
63 cout <<
"Sending List Identity Request... ";
64 udp::resolver r(
socket_.get_io_service());
65 udp::resolver::query q(udp::v4(),
hostname_,
"44818");
66 udp::endpoint receiver_endpoint = *r.resolve(q);
70 BufferWriter w(buffer(d));
72 socket_.send_to(buffer(d, w.getByteCount()), receiver_endpoint);
73 cout <<
"done." << endl;
77 std::size_t num_bytes)
81 cerr <<
"Error receiving list identity response message" << endl;
87 BufferReader r(buffer(
recv_buf_, num_bytes));
90 if (r.getByteCount() != num_bytes)
92 cerr <<
"Warning: packet received with " << num_bytes <<
93 " bytes, but only " << r.getByteCount() <<
" bytes used" << endl;
98 cerr <<
"Reply received with wrong command. Expected " 108 cerr <<
"Warning: Non-zero status received: " << pkt.
getHeader().
status << endl;
112 cerr <<
"Warning: Non-zero sender context received: " 117 cerr <<
"Warning: Non-zero options received: " << pkt.
getHeader().
options << endl;
125 cerr <<
"No items in list identity payload!" << endl;
130 cerr <<
"Warning: more than one item in list identity payload " << payload.
getItemCount() << endl;
135 cerr <<
"Error: Payload response received with the wrong item type. Expected: " 137 payload.
getItems().at(0).getItemType() << endl;
142 payload.
getItems().at(0).getDataAs(
id);
144 cout <<
"=== Received ID Message ===" << endl;
145 cout <<
"Encapsulation Protocol Version: " << (int)
id.encap_protocol_version << endl;
146 cout <<
"Address: " << inet_ntoa(
id.sockaddr.sin_addr) <<
" : " << ntohs(
id.sockaddr.sin_port) << endl;
147 cout <<
"Vendor ID: " << (int)
id.vendor_id << endl;
148 cout <<
"Device Type: " << (int)
id.device_type << endl;
149 cout <<
"Product Code: " << (int)
id.product_code << endl;
150 cout <<
"Revision: " << (int)
id.revision[0] <<
"." << (
int)
id.revision[1] << endl;
151 cout <<
"Status: " << (int)
id.status << endl;
152 cout <<
"Serial Number: " << (int)
id.serial_number << endl;
153 cout <<
"Product Name: " <<
id.product_name << endl;
154 cout <<
"State: " << (int)
id.state << endl;
156 catch (std::length_error e)
158 printf(
"ERROR: Packet too short for identity response\n");
165 cout <<
"Waiting for responses." << endl;
166 socket_.get_io_service().run();
EncapHeader & getHeader()
Writer & serialize(Writer &writer) const
udp::endpoint device_endpoint_
Reader & deserialize(Reader &reader, size_t length)
void getPayloadAs(Serializable &result)
void handleListIdentityResponse(const boost::system::error_code &ec, std::size_t num_bytes)
void sendListIdentityRequest()
boost::array< EIP_BYTE, 4 *1024 > recv_buf_
vector< CPFItem > & getItems()
EIP_UINT getItemCount() const