Go to the documentation of this file.00001
00026 #ifndef ODVA_ETHERNETIP_SESSION_H
00027 #define ODVA_ETHERNETIP_SESSION_H
00028
00029 #include <string>
00030 #include <gtest/gtest_prod.h>
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/make_shared.hpp>
00033 #include <boost/asio.hpp>
00034
00035 #include "odva_ethernetip/eip_types.h"
00036 #include "odva_ethernetip/socket/socket.h"
00037 #include "odva_ethernetip/serialization/serializable.h"
00038 #include "odva_ethernetip/serialization/serializable_primitive.h"
00039 #include "odva_ethernetip/encap_packet.h"
00040 #include "odva_ethernetip/cpf_packet.h"
00041 #include "odva_ethernetip/rr_data_response.h"
00042 #include "odva_ethernetip/path.h"
00043 #include "odva_ethernetip/connection.h"
00044
00045 using std::string;
00046 using boost::shared_ptr;
00047 using boost::make_shared;
00048
00049 namespace eip {
00050
00051 using serialization::Serializable;
00052 using serialization::SerializablePrimitive;
00053 using socket::Socket;
00054
00055 #define DEFAULT_VENDOR_ID 0x1234
00056 #define DEFAULT_SERIAL_NUM 0x6789
00057
00061 class Session
00062 {
00063 public:
00064
00072 Session(shared_ptr<Socket> socket, shared_ptr<Socket> io_socket,
00073 EIP_UINT vendor_id = DEFAULT_VENDOR_ID, EIP_UDINT serial_num = DEFAULT_SERIAL_NUM);
00074
00075 virtual ~Session();
00076
00083 void open(string hostname, string port = "44818", string io_port = "2222");
00084
00088 void close();
00089
00094 EIP_UDINT getSessionID()
00095 {
00096 return session_id_;
00097 }
00098
00106 void getSingleAttributeSerializable(EIP_USINT class_id, EIP_USINT instance_id,
00107 EIP_USINT attribute_id, Serializable& result);
00108
00116 template <typename T>
00117 T getSingleAttribute(EIP_USINT class_id, EIP_USINT instance_id, EIP_USINT attribute_id, T v)
00118 {
00119 SerializablePrimitive<T> data;
00120 getSingleAttributeSerializable(class_id, instance_id, attribute_id, data);
00121 return data.data;
00122 }
00123
00131 void setSingleAttributeSerializable(EIP_USINT class_id, EIP_USINT instance_id,
00132 EIP_USINT attribute_id, shared_ptr<Serializable> data);
00133
00141 template <typename T>
00142 void setSingleAttribute(EIP_USINT class_id, EIP_USINT instance_id,
00143 EIP_USINT attribute_id, T v)
00144 {
00145 shared_ptr< SerializablePrimitive<T> > data =
00146 make_shared< SerializablePrimitive<T> > (v);
00147 setSingleAttributeSerializable(class_id, instance_id, attribute_id, data);
00148 }
00149
00155 int createConnection(const EIP_CONNECTION_INFO_T& o_to_t,
00156 const EIP_CONNECTION_INFO_T& t_to_o);
00157
00162 void closeConnection(size_t n);
00163
00168 inline EIP_UINT getVendorID() const
00169 {
00170 return my_vendor_id_;
00171 }
00172
00177 inline EIP_UDINT getSerialNum() const
00178 {
00179 return my_serial_num_;
00180 }
00181
00187 const Connection& getConnection(size_t n)
00188 {
00189 return connections_[n];
00190 }
00191
00198 CPFPacket receiveIOPacket();
00199
00204 void sendIOPacket(CPFPacket& pkt);
00205
00206 private:
00207 FRIEND_TEST(SessionTest, test_create_connection);
00208
00209 shared_ptr<Socket> socket_;
00210 shared_ptr<Socket> io_socket_;
00211 EIP_UDINT session_id_;
00212 EIP_BYTE recv_buffer_[4*1024];
00213
00214 EIP_UINT my_vendor_id_;
00215 EIP_UDINT my_serial_num_;
00216 EIP_UINT next_connection_sn_;
00217 EIP_UDINT next_connection_id_;
00218
00219 vector<Connection> connections_;
00220
00226 void check_packet(EncapPacket& pkt, EIP_UINT exp_cmd);
00227
00234 EncapPacket sendCommand(EncapPacket& req);
00235
00245 RRDataResponse sendRRDataCommand(EIP_USINT service, const Path& path,
00246 shared_ptr<Serializable> data);
00247 };
00248
00249 }
00250
00251 #endif // ODVA_ETHERNETIP_SESSION_H