OpcUaCanOpen
CANopen OPC-UA server
CanLibLoaderLin.cpp
Go to the documentation of this file.
1 
23 #include "CanLibLoaderLin.h"
24 #include <string>
25 #include <stdexcept>
26 #include <sstream>
27 #include <boost/filesystem.hpp>
28 #include <LogIt.h>
29 
30 #include "dlfcn.h"
31 //typedef void* (*f_CCanAccess)();
32 
33 namespace CanModule
34 {
35 
37 
39 : CanLibLoader(libName), p_dynamicLibrary(0)
40 {
41  LOG(Log::TRC, lh) << "inherited logItComponentHandle= " << lh;
42  dynamicallyLoadLib(libName);
44 }
46  dlclose(p_dynamicLibrary);
47 }
48 
50 {
51  //The library is loaded through dlopen (Linux API)
52  std::ostringstream ss;
53  ss << "lib" << libName << "can.so";
54  LOG(Log::DBG, lh) << "Proceeding to load library " << ss.str();
55  p_dynamicLibrary = dlopen(ss.str().c_str(), RTLD_NOW);
56  //We check for errors while loading the library
57  if (p_dynamicLibrary == 0)
58  {
59  char *err = dlerror();
60  if (err) {
61  std::ostringstream msg;
62  msg << "Error: could not load dynamic library ["<<ss.str()<<"], current working directory ["<<boost::filesystem::current_path()<<"] error: "<<err;
63  LOG(Log::ERR, lh) << msg.str();
64  throw std::runtime_error(msg.str());
65  }
66  }
67 }
68 
76 {
77  f_canAccess *canAccess;
78  canAccess = (f_canAccess*)dlsym(p_dynamicLibrary,"getCanBusAccess");
79  // We check for errors again. If there is an error the library is released from memory.
80  char *err = dlerror();
81  if (err) {
82  LOG(Log::ERR, lh) << "Error: could not locate the function, error: [" << err << "]";
83  throw std::runtime_error("Error: could not locate the function");//TODO: add library name to message
84  }
85  if (!canAccess)
86  {
87  dlclose(p_dynamicLibrary);
88  LOG(Log::ERR, lh) << "Error: could not locate the function, error: [" << err << "]";
89  throw std::runtime_error("Error: could not locate the function");//TODO: add library name to message
90  }
91  // We call the function getHalAccess we got from the library. This will give us a pointer to an object, which we store.
92  return canAccess();
93 }
94 }