3 namespace remote
{ namespace diku_mch
{
5 using namespace remote::util
;
7 const std::string
Mote::NONE
= "none";
8 const std::string
Mote::START
= "start";
9 const std::string
Mote::STOP
= "stop";
10 const std::string
Mote::RESET
= "reset";
11 const std::string
Mote::PROGRAM
= "program";
13 Mote::Mote(std::string
& p_mac
, std::string
& p_directory
)
14 : SerialControl(), mac(p_mac
), directory(p_directory
), isRunning(false),
18 if (isvalid
&& !setupTty(STOP
))
21 Log::info("Mote %s (%s) @ %s", mac
.c_str(), platform
.c_str(), path
.c_str());
24 bool Mote::setupTty(const std::string cmd
)
26 if (!openTty(ttyData
))
29 if (cmd
== START
|| cmd
== STOP
|| cmd
== RESET
)
30 return power(cmd
) == SUCCESS
;
41 void Mote::invalidate()
49 imagefile
= directory
+ "image";
51 programmer
= directory
+ "programmer";
52 if (programmer
== "" || !File::exists(programmer
))
55 path
= File::readFile(directory
+ "path");
59 platform
= File::readFile(directory
+ "platform");
63 ttyData
= File::readLink(directory
+ "tty/data");
67 ttyControl
= File::readLink(directory
+ "tty/control");
72 Log::warn("Mote %s is invalid", mac
.c_str());
76 result_t
Mote::start()
86 result_t
Mote::reset()
91 result_t
Mote::power(const std::string cmd
)
93 bool resetting
= cmd
== RESET
;
98 if (!resetting
|| controlDTR(isRunning
)) {
99 bool enable
= resetting
? !isRunning
: cmd
== STOP
;
101 if (controlDTR(enable
)) {
106 /* Mirror that the first reset DTR change succeeded. */
108 isRunning
= !isRunning
;
111 Log::error("Failed to %s mote %s: %s",
112 cmd
.c_str(), mac
.c_str(), strerror(errno
));
118 result_t
Mote::program(std::string tos
, const uint8_t *image
, uint32_t imagelen
)
123 if (File::writeFile(imagefile
, image
, imagelen
)) {
124 std::string mac_env
= "macaddress=" + mac
;
125 std::string tos_env
= "tosaddress=" + tos
;
126 std::string platform_env
= "platform=" + platform
;
127 char * const args
[] = {
128 (char *) programmer
.c_str(),
129 (char *) ttyControl
.c_str(),
130 (char *) imagefile
.c_str(),
133 char * const envp
[] = {
134 (char *) mac_env
.c_str(),
135 (char *) tos_env
.c_str(),
136 (char *) platform_env
.c_str(),
140 Log::info("Programming mote %s", mac
.c_str());
142 if (runChild(args
, envp
)) {
143 controlCmd
= PROGRAM
;
147 remove(imagefile
.c_str());
153 result_t
Mote::cancelProgramming()
155 if (getControlCommand() != PROGRAM
)
158 return getChildResult(true);
161 result_t
Mote::getChildResult(bool force
)
163 bool success
= endChild(force
);
164 bool afterProgramming
= controlCmd
== PROGRAM
;
165 const std::string cmd
= afterProgramming
? STOP
: NONE
;
167 if (afterProgramming
)
168 remove(imagefile
.c_str());
173 return success
? SUCCESS
: FAILURE
;
177 status_t
Mote::getStatus()
179 if (hasChild() && controlCmd
== PROGRAM
)
180 return MOTE_PROGRAMMING
;
181 if (!isOpen()) return MOTE_UNAVAILABLE
;
182 if (isRunning
) return MOTE_RUNNING
;
186 const std::string
& Mote::getControlCommand()
188 return hasChild() ? controlCmd
: NONE
;
192 const std::string
& Mote::getMac()
197 const std::string
& Mote::getDevicePath()
202 const std::string
& Mote::getPlatform()