Skip to content

Commit

Permalink
Refacor whole structure
Browse files Browse the repository at this point in the history
Now there's a central class GigEVision which keeps singelton objects for the
GenICam, Gvcp and Gvsp managers, so every subsystem can access the
other through that class.
Reimplemented the manager classes to have an implementation file, and not be
header-only.
  • Loading branch information
hirsch committed Aug 14, 2009
1 parent a80c25e commit 7da3dab
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 330 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
cmake_minimum_required(VERSION 2.6)
project(GigEV4Linux)

set(SOURCES Main.cpp
Gvsp.cpp
Gvcp.cpp
GigEVision.cpp
GenICam.cpp)

set(HEADERS Gvsp.h
Gvcp.h
GenICam.h
GigEVision.h)


include_directories(. /usr/include /usr/include/c++/4.3 thirdparty/GenICam/include)
add_definitions(-DGENICAM_ROOT_PATH=\"${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/GenICam/\")

Expand All @@ -12,7 +24,7 @@ find_library(PNG_LIBRARY png REQUIRED)
find_library(GENICAM_BASE_LIBRARY genicam_base REQUIRED thirdparty/GenICam/lib64)
find_library(GENICAM_API_LIBRARY genicam_api REQUIRED thirdparty/GenICam/lib64)
#find_library(GENICAM_MATHPARSER_LIBRARY genicam_base REQUIRED thirdparty/GenICam/lib64)
add_executable(camtest Main.cpp Gvsp.h Gvcp.h GenICam.h)
add_executable(camtest ${SOURCES} ${HEADERS})

target_link_libraries(camtest ${Boost_THREAD_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${ZIP_LIBRARY}
${PNG_LIBRARY} ${GENICAM_BASE_LIBRARY} ${GENICAM_API_LIBRARY})
133 changes: 133 additions & 0 deletions GenICam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <cstdlib>
#include <exception>
#include <sstream>
#include <iostream>
#include <fstream>
#include <functional>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <zip.h>

#include "GigEVision.h"
#include "Gvcp.h"
#include "GenICam.h"

GenICamManager::GenICamManager(GigEVision& gev):
m_gev(gev)
{

}

void GenICamManager::ReadXmlFile()
{
std::vector<uint8_t> data = m_gev.Gvcp().ReadBlock(GENICAM_ZIPFILEINFO_ADDRESS, 512);
std::string sTxt(data.begin(), data.end());

std::vector<std::string> aParts;
boost::algorithm::split(aParts, sTxt, boost::algorithm::is_any_of(";"));

if(aParts.size() != 3)
throw std::runtime_error("Error getting genicam file location on camera");

int nAddr, nSize;
std::stringstream ss;
ss << std::hex << aParts[1];
ss >> nAddr;
ss.clear();
ss << std::hex << aParts[2];
ss >> nSize;

data = m_gev.Gvcp().ReadBlock(nAddr, nSize);

// TODO: find a way to decompress the zip-file in menory without writing the file to disk
// TODO: use a platform independent path
std::string sFilename("/tmp/genicam.zip");
std::string retData;

try
{
std::ofstream os(sFilename.c_str(), std::ios::binary);
os.write((char*)(&data[0]), data.size());
os.flush();

struct zip* zz = zip_open(sFilename.c_str(), 0, 0);
if(zz == 0)
throw std::runtime_error("Error opening genicam zipfile");

struct zip_file* zf = zip_fopen_index(zz, 0, 0);
if(zf == 0)
throw std::runtime_error("Error opening file in zip archive");

boost::array<uint8_t, 1024> buff;

int nRead = 0;
while((nRead = zip_fread(zf, buff.data(), buff.size())) != 0)
std::copy(buff.begin(), buff.begin()+nRead, std::back_inserter(retData));

// clean up file from disk
boost::filesystem::remove(sFilename);
}
catch(std::exception& e)
{
boost::filesystem::remove(sFilename);
throw e;
}

setenv("GENICAM_ROOT_V1_1", GENICAM_ROOT_PATH, true);
m_cam._LoadXMLFromString(retData.c_str());
}

uint32_t GenICamManager::GetAddress(const std::string& sKey)
{
GenApi::INode* pN = m_cam._GetNode(sKey.c_str());
uint32_t nAddr = 0;
if(pN != 0)
{
GenICam::gcstring val, att;
if(pN->GetProperty("Address", val, att))
{
nAddr = boost::lexical_cast<uint32_t>(val);
}
if(pN->GetProperty("pAddress", val, att) && val.length() > 0)
{
std::string sTmp(val);
std::vector<std::string> parts;
boost::algorithm::split(parts, sTmp, boost::algorithm::is_any_of("\t"));
for(size_t i=0; i<parts.size(); ++i)
{
GenApi::INode* pAddrNode = m_cam._GetNode(parts[i].c_str());
assert(pAddrNode);
GenICam::gcstring sAddr, sAtt;
assert(pAddrNode->GetProperty("Value", sAddr, sAtt));
nAddr += boost::lexical_cast<uint32_t>(sAddr);
}
}
}
else
throw std::runtime_error(str(boost::format("Can't find address for '%1%'") % sKey));

return nAddr;
}

void GenICamManager::PrintNodes(bool bWithProperties) const
{
GenApi::NodeList_t nodes;
m_cam._GetNodes(nodes);

for(GenApi::NodeList_t::const_iterator it = nodes.begin(); it!=nodes.end(); ++it)
{
std::cout << "Node: " << (*it)->GetName(true) << std::endl;
if(bWithProperties)
{
GenICam::gcstring_vector props;
(*it)->GetPropertyNames(props);
for(GenICam::gcstring_vector::const_iterator pit = props.begin(); pit!=props.end(); ++pit)
std::cout << "\tProp: " << *pit << std::endl;
}
}
}


130 changes: 9 additions & 121 deletions GenICam.h
Original file line number Diff line number Diff line change
@@ -1,137 +1,25 @@
#ifndef GENICAM_H
#define GENICAM_H

#include <cstdlib>
#include <exception>
#include <sstream>
#include <iostream>
#include <functional>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <zip.h>

#include <string>
#include "GenApi/GenApi.h"
#include "Gvcp.h"

// forwarding
class GigEVision;

class GenICamManager
{
public:
GenICamManager(GvcpManager& gvcp):
m_gvcp(gvcp)
{

}

void ReadXmlFile()
{
std::vector<uint8_t> data = m_gvcp.ReadBlock(GENICAM_ZIPFILEINFO_ADDRESS, 512);
std::string sTxt(data.begin(), data.end());

std::vector<std::string> aParts;
boost::algorithm::split(aParts, sTxt, boost::algorithm::is_any_of(";"));

if(aParts.size() != 3)
throw std::runtime_error("Error getting genicam file location on camera");

int nAddr, nSize;
std::stringstream ss;
ss << std::hex << aParts[1];
ss >> nAddr;
ss.clear();
ss << std::hex << aParts[2];
ss >> nSize;

data = m_gvcp.ReadBlock(nAddr, nSize);

// TODO: find a way to decompress the zip-file in menory without writing the file to disk
// TODO: use a platform independent path
std::string sFilename("/tmp/genicam.zip");
std::string retData;

try
{
std::ofstream os(sFilename.c_str(), std::ios::binary);
os.write((char*)(&data[0]), data.size());
os.flush();

struct zip* zz = zip_open(sFilename.c_str(), 0, 0);
if(zz == 0)
throw std::runtime_error("Error opening genicam zipfile");

struct zip_file* zf = zip_fopen_index(zz, 0, 0);
if(zf == 0)
throw std::runtime_error("Error opening file in zip archive");

boost::array<uint8_t, 1024> buff;

int nRead = 0;
while((nRead = zip_fread(zf, buff.data(), buff.size())) != 0)
std::copy(buff.begin(), buff.begin()+nRead, std::back_inserter(retData));

// clean up file from disk
boost::filesystem::remove(sFilename);
}
catch(std::exception& e)
{
boost::filesystem::remove(sFilename);
throw e;
}

setenv("GENICAM_ROOT_V1_1", GENICAM_ROOT_PATH, true);
m_cam._LoadXMLFromString(retData.c_str());
}

uint32_t GetAddress(const std::string& sKey)
{
GenApi::INode* pN = m_cam._GetNode(sKey.c_str());
uint32_t nAddr = 0;
if(pN != 0)
{
GenICam::gcstring val, att;
if(pN->GetProperty("Address", val, att))
{
nAddr = boost::lexical_cast<uint32_t>(val);
}
if(pN->GetProperty("pAddress", val, att) && val.length() > 0)
{
std::string sTmp(val);
std::vector<std::string> parts;
boost::algorithm::split(parts, sTmp, boost::algorithm::is_any_of("\t"));
for(size_t i=0; i<parts.size(); ++i)
{
GenApi::INode* pAddrNode = m_cam._GetNode(parts[i].c_str());
assert(pAddrNode);
GenICam::gcstring sAddr, sAtt;
assert(pAddrNode->GetProperty("Value", sAddr, sAtt));
nAddr += boost::lexical_cast<uint32_t>(sAddr);
}
}
}
else
throw std::runtime_error(str(boost::format("Can't find address for '%1%'") % sKey));
GenICamManager(GigEVision& gev);

return nAddr;
}
void ReadXmlFile();

void PrintNodes() const
{
GenApi::NodeList_t nodes;
m_cam._GetNodes(nodes);
uint32_t GetAddress(const std::string& sKey);

for(GenApi::NodeList_t::const_iterator it = nodes.begin(); it!=nodes.end(); ++it)
{
std::cout << "Node: " << (*it)->GetName(true) << std::endl;
GenICam::gcstring_vector props;
(*it)->GetPropertyNames(props);
for(GenICam::gcstring_vector::const_iterator pit = props.begin(); pit!=props.end(); ++pit)
std::cout << "\tProp: " << *pit << std::endl;
}
}
void PrintNodes(bool bWithProperties=false) const;

private:
GvcpManager& m_gvcp;
GigEVision& m_gev;
GenApi::CNodeMapRef m_cam;
};

Expand Down
13 changes: 13 additions & 0 deletions GigEVision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "GenICam.h"
#include "Gvcp.h"
#include "Gvsp.h"

#include "GigEVision.h"

GigEVision::GigEVision():
m_genicam(new GenICamManager(*this)),
m_gvcp(new GvcpManager(*this)),
m_gvsp(new GvspManager(*this, 12221))
{ }


30 changes: 30 additions & 0 deletions GigEVision.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef GIGEVISION_H
#define GIGEVISION_H

#include <memory>
#include <boost/asio.hpp>

// forwarding
class GenICamManager;
class GvcpManager;
class GvspManager;

class GigEVision
{
public:
GigEVision();

GenICamManager& GenICam() { return *m_genicam; }
GvcpManager& Gvcp() { return *m_gvcp; }
GvspManager& Gvsp() { return *m_gvsp; }
boost::asio::io_service& Service() { return m_service; }

private:
boost::asio::io_service m_service;
std::auto_ptr<GenICamManager> m_genicam;
std::auto_ptr<GvcpManager> m_gvcp;
std::auto_ptr<GvspManager> m_gvsp;
};


#endif // GIGEVISION_H
Loading

0 comments on commit 7da3dab

Please sign in to comment.