Remove TODO file
[remote/remote-mci.git] / diku_mch / DeviceManager.cc
blob3666ed52072d4edbdbf3a62db3c75076e2d85164
1 #include "DeviceManager.h"
3 namespace remote { namespace diku_mch {
5 struct delMote : public std::unary_function<std::pair<const std::string, Mote *>,void>
7 void operator()(std::pair<const std::string, Mote *> x) { delete x.second; }
8 };
10 struct invalidate : public std::unary_function<std::pair<const std::string, Mote *>,void>
12 void operator()(std::pair<const std::string, Mote *> x)
13 { if (x.second) x.second->invalidate(); }
16 void DeviceManager::refresh(std::string devicePath)
18 /* Invalidate all existing motes. */
19 std::for_each(motes.begin(), motes.end(), invalidate());
21 /* Clear new motes and delete and clear lost motes. */
22 newMotes.clear();
23 std::for_each(lostMotes.begin(), lostMotes.end(), delMote());
24 lostMotes.clear();
26 /* Read all mote devices */
27 readMoteDevices(devicePath);
29 /* Finally, clean up the motes that were lost since the last
30 * refresh based on whether they have been revalidated. */
31 motemap_t::iterator moteI = motes.begin();
32 while (moteI != motes.end()) {
33 if (moteI->second) {
34 if (!moteI->second->isValid()) {
35 lostMotes.insert(*moteI);
36 motes.erase(moteI);
38 } else {
39 motes.erase(moteI);
41 moteI++;
45 void DeviceManager::readMoteDevices(std::string devicePath)
47 struct dirent *dentry;
48 DIR *deviceDir;
50 deviceDir = opendir(devicePath.c_str());
51 if (!deviceDir) {
52 /* FIXME: Print error message if the error is not ENOENT. */
53 return;
56 while ((dentry = readdir(deviceDir))) {
57 std::string mac = dentry->d_name;
59 if (mac == "." || mac == "..")
60 continue;
62 updateMote(mac, devicePath + "/" + mac + "/");
65 closedir(deviceDir);
68 /** Update the mote device.
69 * Search for an existing mote based on the MAC given in the serial
70 * number. */
71 void DeviceManager::updateMote(std::string mac, std::string directory)
73 motemap_t::iterator m = motes.find(mac);
74 if (m != motes.end()) {
75 // we found a mote with the same MAC address
76 // just make sure the info is up-to-date
77 m->second->validate();
79 } else {
80 Mote *mote = new Mote(mac, directory);
82 if (mote->isValid()) {
83 // this must be a new mote, add it to the collection
84 motemap_t::value_type elm(mac, mote);
85 motes.insert(elm);
86 newMotes.insert(elm);
88 } else {
89 delete mote;