00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "cport.h"
00037 #include "exception.h"
00038
00039 #include <fstream>
00040
00041 namespace rcg
00042 {
00043
00044 CPort::CPort(std::shared_ptr<const GenTLWrapper> _gentl, void **_port) : gentl(_gentl)
00045 {
00046 port=_port;
00047 }
00048
00049 void CPort::Read(void *buffer, int64_t addr, int64_t length)
00050 {
00051 size_t size=static_cast<size_t>(length);
00052
00053 if (*port != 0)
00054 {
00055 if (gentl->GCReadPort(*port, addr, buffer, &size) != GenTL::GC_ERR_SUCCESS)
00056 {
00057 throw GenTLException("CPort::Read()", gentl);
00058 }
00059
00060 if (size != static_cast<size_t>(length))
00061 {
00062 throw GenTLException("CPort::Read(): Returned size not as expected");
00063 }
00064 }
00065 else
00066 {
00067 throw GenTLException("CPort::Read(): Port has been closed");
00068 }
00069 }
00070
00071 void CPort::Write(const void *buffer, int64_t addr, int64_t length)
00072 {
00073 size_t size=static_cast<size_t>(length);
00074
00075 if (*port != 0)
00076 {
00077 if (gentl->GCWritePort(*port, addr, buffer, &size) != GenTL::GC_ERR_SUCCESS)
00078 {
00079 throw GenTLException("CPort::Write()", gentl);
00080 }
00081
00082 if (size != static_cast<size_t>(length))
00083 {
00084 throw GenTLException("CPort::Write(): Returned size not as expected");
00085 }
00086 }
00087 else
00088 {
00089 throw GenTLException("CPort::Write(): Port has been closed");
00090 }
00091 }
00092
00093 GenApi::EAccessMode CPort::GetAccessMode() const
00094 {
00095 if (*port != 0)
00096 {
00097 return GenApi::RW;
00098 }
00099
00100 return GenApi::NA;
00101 }
00102
00103 namespace
00104 {
00105
00106 inline std::string toLower(const std::string &s, size_t start, size_t size)
00107 {
00108 std::ostringstream out;
00109
00110 size_t end=std::min(s.size(), start+size);
00111
00112 while (start < end)
00113 {
00114 out << static_cast<char>(std::tolower(s[start++]));
00115 }
00116
00117 return out.str();
00118 }
00119
00120 }
00121
00122 std::shared_ptr<GenApi::CNodeMapRef> allocNodeMap(std::shared_ptr<const GenTLWrapper> gentl,
00123 void *port, CPort *cport, const char *xml)
00124 {
00125 std::shared_ptr<GenApi::CNodeMapRef> nodemap(new GenApi::CNodeMapRef());
00126
00127 try
00128 {
00129
00130
00131 uint32_t n=0;
00132 if (gentl->GCGetNumPortURLs(port, &n) != GenTL::GC_ERR_SUCCESS)
00133 {
00134 throw GenTLException("allocNodeMap()", gentl);
00135 }
00136
00137 if (n == 0)
00138 {
00139 return std::shared_ptr<GenApi::CNodeMapRef>();
00140 }
00141
00142
00143
00144 GenTL::INFO_DATATYPE type;
00145 char tmp[1024]="";
00146 size_t size=sizeof(tmp);
00147
00148 if (gentl->GCGetPortURLInfo(port, 0, GenTL::URL_INFO_URL, &type, tmp, &size) !=
00149 GenTL::GC_ERR_SUCCESS)
00150 {
00151 throw GenTLException("allocNodeMap()", gentl);
00152 }
00153
00154
00155
00156 std::string url=tmp;
00157 if (toLower(url, 0, 6) == "local:")
00158 {
00159
00160
00161 int i=6;
00162 if (url.compare(i, 3, "///") == 0)
00163 {
00164 i+=3;
00165 }
00166
00167 std::stringstream in(url.substr(i));
00168 std::string name, saddress, slength;
00169
00170 std::getline(in, name, ';');
00171 std::getline(in, saddress, ';');
00172 std::getline(in, slength, ';');
00173
00174 uint64_t address=std::stoull(saddress, 0, 16);
00175 size_t length=static_cast<size_t>(std::stoull(slength, 0, 16));
00176
00177
00178
00179 std::unique_ptr<char[]> buffer(new char[length+1]);
00180
00181 if (gentl->GCReadPort(port, address, buffer.get(), &length) != GenTL::GC_ERR_SUCCESS)
00182 {
00183 throw GenTLException("allocNodeMap()", gentl);
00184 }
00185
00186 buffer.get()[length]='\0';
00187
00188
00189
00190 if (xml != 0)
00191 {
00192 std::ofstream out(xml, std::ios::binary);
00193
00194 out.rdbuf()->sputn(buffer.get(), length);
00195 }
00196
00197
00198
00199 if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
00200 {
00201 nodemap->_LoadXMLFromZIPData(buffer.get(), length);
00202 }
00203 else
00204 {
00205 gcstring xml=buffer.get();
00206 nodemap->_LoadXMLFromString(xml);
00207 }
00208 }
00209 else if (toLower(url, 0, 5) == "file:")
00210 {
00211
00212
00213 int i=6;
00214 if (url.compare(i, 3, "///") == 0)
00215 {
00216 i+=3;
00217 }
00218
00219 std::string name=url.substr(i);
00220
00221
00222
00223 if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
00224 {
00225 gcstring file=name.c_str();
00226 nodemap->_LoadXMLFromZIPFile(file);
00227 }
00228 else
00229 {
00230 gcstring file=name.c_str();
00231 nodemap->_LoadXMLFromFile(file);
00232 }
00233 }
00234 else
00235 {
00236 throw GenTLException(("allocNodeMap(): Cannot interpret URL: "+url).c_str());
00237 }
00238
00239
00240
00241 size=sizeof(tmp);
00242
00243 if (gentl->GCGetPortInfo(port, GenTL::PORT_INFO_PORTNAME, &type, tmp, &size) !=
00244 GenTL::GC_ERR_SUCCESS)
00245 {
00246 throw GenTLException("allocNodeMap()", gentl);
00247 }
00248
00249 gcstring portname=tmp;
00250 if (!nodemap->_Connect(cport, portname))
00251 {
00252 throw GenTLException((std::string("allocNodeMap(): Cannot connect port: ")+tmp).c_str());
00253 }
00254 }
00255 catch (const GENICAM_NAMESPACE::GenericException &ex)
00256 {
00257 throw GenTLException(ex.what());
00258 }
00259
00260 return nodemap;
00261 }
00262
00263 }