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
; }
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. */
23 std::for_each(lostMotes
.begin(), lostMotes
.end(), delMote());
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()) {
34 if (!moteI
->second
->isValid()) {
35 lostMotes
.insert(*moteI
);
45 void DeviceManager::readMoteDevices(std::string devicePath
)
47 struct dirent
*dentry
;
50 deviceDir
= opendir(devicePath
.c_str());
52 /* FIXME: Print error message if the error is not ENOENT. */
56 while ((dentry
= readdir(deviceDir
))) {
57 std::string mac
= dentry
->d_name
;
59 if (mac
== "." || mac
== "..")
62 updateMote(mac
, devicePath
+ "/" + mac
+ "/");
68 /** Update the mote device.
69 * Search for an existing mote based on the MAC given in the serial
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();
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
);