00001 #include "epos_hardware/utils.h"
00002 #include <boost/foreach.hpp>
00003 #include <sstream>
00004
00005 #define MAX_STRING_SIZE 1000
00006
00007 bool SerialNumberFromHex(const std::string& str, uint64_t* serial_number) {
00008 std::stringstream ss;
00009 ss << std::hex << str;
00010 ss >> *serial_number;
00011 return true;
00012 }
00013
00014 int GetErrorInfo(unsigned int error_code, std::string* error_string) {
00015 char buffer[MAX_STRING_SIZE];
00016 int result;
00017 if(result = VCS_GetErrorInfo(error_code, buffer, MAX_STRING_SIZE)) {
00018 *error_string = buffer;
00019 }
00020 return result;
00021 }
00022
00023
00024 int GetDeviceNameList(std::vector<std::string>* device_names, unsigned int* error_code) {
00025 char buffer[MAX_STRING_SIZE];
00026 int end_of_selection;
00027 int result;
00028
00029 result = VCS_GetDeviceNameSelection(true, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00030 if(!result)
00031 return result;
00032 device_names->push_back(buffer);
00033
00034 while(!end_of_selection) {
00035 result = VCS_GetDeviceNameSelection(false, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00036 if(!result)
00037 return result;
00038 device_names->push_back(buffer);
00039 }
00040
00041 return 1;
00042 }
00043
00044 int GetProtocolStackNameList(const std::string device_name, std::vector<std::string>* protocol_stack_names, unsigned int* error_code) {
00045 char buffer[MAX_STRING_SIZE];
00046 int end_of_selection;
00047 int result;
00048
00049 result = VCS_GetProtocolStackNameSelection((char*)device_name.c_str(), true, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00050 if(!result)
00051 return result;
00052 protocol_stack_names->push_back(buffer);
00053
00054 while(!end_of_selection) {
00055 result = VCS_GetProtocolStackNameSelection((char*)device_name.c_str(), false, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00056 if(!result)
00057 return result;
00058 protocol_stack_names->push_back(buffer);
00059 }
00060
00061 return 1;
00062 }
00063
00064
00065 int GetInterfaceNameList(const std::string device_name, const std::string protocol_stack_name, std::vector<std::string>* interface_names, unsigned int* error_code) {
00066 char buffer[MAX_STRING_SIZE];
00067 int end_of_selection;
00068 int result;
00069
00070 result = VCS_GetInterfaceNameSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), true, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00071 if(!result)
00072 return result;
00073 interface_names->push_back(buffer);
00074
00075 while(!end_of_selection) {
00076 result = VCS_GetInterfaceNameSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), false, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00077 if(!result)
00078 return result;
00079 interface_names->push_back(buffer);
00080 }
00081
00082 return 1;
00083 }
00084
00085 int GetPortNameList(const std::string device_name, const std::string protocol_stack_name, const std::string interface_name, std::vector<std::string>* port_names, unsigned int* error_code) {
00086 char buffer[MAX_STRING_SIZE];
00087 int end_of_selection;
00088 int result;
00089
00090 result = VCS_GetPortNameSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), (char*)interface_name.c_str(), true, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00091 if(!result)
00092 return result;
00093 port_names->push_back(buffer);
00094
00095 while(!end_of_selection) {
00096 result = VCS_GetPortNameSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), (char*)interface_name.c_str(), false, buffer, MAX_STRING_SIZE, &end_of_selection, error_code);
00097 if(!result)
00098 return result;
00099 port_names->push_back(buffer);
00100 }
00101
00102 return 1;
00103 }
00104
00105 int GetBaudrateList(const std::string device_name, const std::string protocol_stack_name, const std::string interface_name,
00106 const std::string port_name, std::vector<unsigned int>* baudrates, unsigned int* error_code) {
00107 unsigned int baudrate;
00108 int end_of_selection;
00109 int result;
00110
00111 result = VCS_GetBaudrateSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), (char*)interface_name.c_str(), (char*)port_name.c_str(), true, &baudrate, &end_of_selection, error_code);
00112 if(!result)
00113 return result;
00114 baudrates->push_back(baudrate);
00115
00116 while(!end_of_selection) {
00117 result = VCS_GetBaudrateSelection((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), (char*)interface_name.c_str(), (char*)port_name.c_str(), false, &baudrate, &end_of_selection, error_code);
00118 if(!result)
00119 return result;
00120 baudrates->push_back(baudrate);
00121 }
00122
00123 return 1;
00124 }
00125
00126
00127
00128
00129
00130 EposFactory::EposFactory() {
00131 }
00132
00133 DeviceHandlePtr EposFactory::CreateDeviceHandle(const std::string device_name,
00134 const std::string protocol_stack_name,
00135 const std::string interface_name,
00136 const std::string port_name,
00137 unsigned int* error_code) {
00138 const std::string key = device_name + '/' + protocol_stack_name + '/' + interface_name + '/' + port_name;
00139 DeviceHandlePtr handle;
00140 if(!(handle = existing_handles[key].lock())) {
00141 void* raw_handle = VCS_OpenDevice((char*)device_name.c_str(), (char*)protocol_stack_name.c_str(), (char*)interface_name.c_str(), (char*)port_name.c_str(), error_code);
00142 if(!raw_handle)
00143 return DeviceHandlePtr();
00144
00145 handle = DeviceHandlePtr(new DeviceHandle(raw_handle));
00146 existing_handles[key] = handle;
00147 }
00148
00149 return handle;
00150 }
00151
00152 NodeHandlePtr EposFactory::CreateNodeHandle(const std::string device_name,
00153 const std::string protocol_stack_name,
00154 const std::string interface_name,
00155 const uint64_t serial_number,
00156 unsigned int* error_code) {
00157 std::vector<EnumeratedNode> nodes;
00158 EnumerateNodes(device_name, protocol_stack_name, interface_name, &nodes, error_code);
00159 BOOST_FOREACH(const EnumeratedNode& node, nodes) {
00160 if(node.serial_number == serial_number) {
00161 return CreateNodeHandle(node, error_code);
00162 }
00163 }
00164 return NodeHandlePtr();
00165 }
00166
00167 NodeHandlePtr EposFactory::CreateNodeHandle(const EnumeratedNode& node,
00168 unsigned int* error_code) {
00169 DeviceHandlePtr device_handle = CreateDeviceHandle(node.device_name, node.protocol_stack_name, node.interface_name, node.port_name, error_code);
00170 if(!device_handle)
00171 return NodeHandlePtr();
00172 return NodeHandlePtr(new NodeHandle(device_handle, node.node_id));
00173 }
00174
00175
00176
00177 int EposFactory::EnumerateNodes(const std::string device_name, const std::string protocol_stack_name, const std::string interface_name,
00178 const std::string port_name, std::vector<EnumeratedNode>* nodes, unsigned int* error_code) {
00179 DeviceHandlePtr handle;
00180 if(!(handle = CreateDeviceHandle(device_name, protocol_stack_name, interface_name, port_name, error_code))){
00181 return 0;
00182 }
00183 for(unsigned short i = 1; i < 127; ++i) {
00184 EnumeratedNode node;
00185 node.device_name = device_name;
00186 node.protocol_stack_name = protocol_stack_name;
00187 node.interface_name = interface_name;
00188 node.port_name = port_name;
00189 node.node_id = i;
00190 if(!VCS_GetVersion(handle->ptr, i, &node.hardware_version, &node.software_version, &node.application_number, &node.application_version, error_code)){
00191 return 1;
00192 }
00193 unsigned int bytes_read;
00194 if(!VCS_GetObject(handle->ptr, i, 0x2004, 0x00, &node.serial_number, 8, &bytes_read, error_code)){
00195 node.serial_number = 0;
00196 }
00197 nodes->push_back(node);
00198 }
00199 return 1;
00200 }
00201
00202
00203 int EposFactory::EnumerateNodes(const std::string device_name, const std::string protocol_stack_name, const std::string interface_name,
00204 std::vector<EnumeratedNode>* nodes, unsigned int* error_code) {
00205 std::vector<std::string> port_names;
00206 if(GetPortNameList(device_name, protocol_stack_name, interface_name, &port_names, error_code)) {
00207 BOOST_FOREACH(const std::string& port_name, port_names) {
00208 if(!EnumerateNodes(device_name, protocol_stack_name, interface_name, port_name, nodes, error_code)){
00209 return 0;
00210 }
00211 }
00212 return 1;
00213 }
00214 else
00215 return 0;
00216 }