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 moteI
->second
->closeTty();
36 lostMotes
.insert(*moteI
);
46 void DeviceManager::readMoteDevices(std::string devicePath
)
48 struct dirent
*dentry
;
51 deviceDir
= opendir(devicePath
.c_str());
53 /* FIXME: Print error message if the error is not ENOENT. */
57 while ((dentry
= readdir(deviceDir
))) {
58 std::string mac
= dentry
->d_name
;
60 if (mac
== "." || mac
== "..")
63 updateMote(mac
, devicePath
+ "/" + mac
+ "/");
69 /** Update the mote device.
70 * Search for an existing mote based on the MAC given in the serial
72 void DeviceManager::updateMote(std::string mac
, std::string directory
)
74 motemap_t::iterator m
= motes
.find(mac
);
75 if (m
!= motes
.end()) {
76 // we found a mote with the same MAC address
77 // just make sure the info is up-to-date
78 m
->second
->validate();
81 Mote
*mote
= new Mote(mac
, directory
);
83 if (mote
->isValid()) {
84 // this must be a new mote, add it to the collection
85 motemap_t::value_type
elm(mac
, mote
);