Skip to content

Commit

Permalink
automatically unmap PDO if destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias-luedtke committed Sep 13, 2016
1 parent 89b52df commit 84e9ffc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions canopen_master/include/canopen_master/canopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/thread/condition_variable.hpp>
#include <boost/chrono/system_clocks.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/function.hpp>

namespace canopen{

Expand Down Expand Up @@ -86,9 +87,14 @@ class PDOMapper{
class PDO {
protected:
void parse_and_set_mapping(const boost::shared_ptr<ObjectStorage> &storage, const uint16_t &com_index, const uint16_t &map_index, const bool &read, const bool &write);
boost::function<void()> unmap;
can::Frame frame;
uint8_t transmission_type;
std::vector< boost::shared_ptr<Buffer> >buffers;
public:
~PDO(){
if(unmap) unmap();
}
};

struct TPDO: public PDO{
Expand Down
1 change: 1 addition & 0 deletions canopen_master/include/canopen_master/objdict.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ class ObjectStorage{
}

std::pair<ObjectDict::Key, size_t> map(uint16_t index, uint8_t sub_index, const ReadDelegate & read_delegate, const WriteDelegate & write_delegate);
void unmap(const ObjectDict::Key &key);

template<typename T> Entry<T> entry(uint16_t index){
return entry<T>(ObjectDict::Key(index));
Expand Down
9 changes: 9 additions & 0 deletions canopen_master/src/objdict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ std::pair<ObjectDict::Key, size_t> ObjectStorage::map(uint16_t index, uint8_t su
}
}

void ObjectStorage::unmap(const ObjectDict::Key &key){
boost::mutex::scoped_lock lock(mutex_);
boost::unordered_map<ObjectDict::Key, boost::shared_ptr<Data> >::iterator it = storage_.find(key);

if(it != storage_.end()){
it->second->set_delegates(read_delegate_, write_delegate_);
}
}

ObjectStorage::ObjectStorage(boost::shared_ptr<const ObjectDict> dict, uint8_t node_id, ReadDelegate read_delegate, WriteDelegate write_delegate)
:read_delegate_(read_delegate), write_delegate_(write_delegate), dict_(dict), node_id_(node_id){
assert(dict_);
Expand Down
1 change: 1 addition & 0 deletions canopen_master/src/pdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void PDOMapper::PDO::parse_and_set_mapping(const boost::shared_ptr<ObjectStorage
if(read || write) wd = ObjectStorage::WriteDelegate(b.get(), &Buffer::write); // set writer for buffer setup or as write delegate

std::pair<ObjectDict::Key, size_t> m = storage->map(param.index, param.sub_index, rd, wd);
unmap = boost::bind(&ObjectStorage::unmap, storage, m.first);
assert(m.second == param.length/8);
}

Expand Down

0 comments on commit 84e9ffc

Please sign in to comment.