3 namespace remote
{ namespace diku_mch
{
5 using namespace remote::util
;
7 Mote::Mote(std::string
& p_mac
, std::string
& p_directory
)
8 : SerialControl(), mac(p_mac
), directory(p_directory
), isRunning(false)
11 if (isvalid
&& !setupTty())
14 Log::info("Mote %s (%s) @ %s", mac
.c_str(), platform
.c_str(), path
.c_str());
19 if (!openTty(ttyData
))
22 if (stop() == FAILURE
)
34 void Mote::invalidate()
42 imagefile
= directory
+ "image";
44 binProgram
= directory
+ "programmer";
45 if (!File::exists(binProgram
))
48 path
= File::readFile(directory
+ "path");
52 platform
= File::readFile(directory
+ "platform");
56 ttyData
= File::readLink(directory
+ "tty");
60 ttyControl
= File::readLink(directory
+ "tty");
65 Log::warn("Mote %s is invalid", mac
.c_str());
69 result_t
Mote::start()
71 return power("start");
79 result_t
Mote::reset()
81 return power("reset");
84 result_t
Mote::power(const std::string cmd
)
86 bool resetting
= cmd
== "reset";
91 if (!resetting
|| controlDTR(isRunning
)) {
92 bool enable
= resetting
? !isRunning
: cmd
== "stop";
94 if (controlDTR(enable
)) {
99 /* Mirror that the first reset DTR change succeeded. */
101 isRunning
= !isRunning
;
104 Log::error("Failed to %s mote %s: %s",
105 cmd
.c_str(), mac
.c_str(), strerror(errno
));
111 result_t
Mote::program(std::string tos
, const uint8_t *image
, uint32_t imagelen
)
116 if (File::writeFile(imagefile
, image
, imagelen
)) {
117 std::string mac_env
= "macaddress=" + mac
;
118 std::string tos_env
= "tosaddress=" + tos
;
119 std::string platform_env
= "platform=" + platform
;
120 char * const args
[] = {
121 (char *) binProgram
.c_str(),
122 (char *) ttyControl
.c_str(),
123 (char *) imagefile
.c_str(),
126 char * const envp
[] = {
127 (char *) mac_env
.c_str(),
128 (char *) tos_env
.c_str(),
129 (char *) platform_env
.c_str(),
133 Log::info("Programming mote %s", mac
.c_str());
135 if (runChild(args
, envp
)) {
136 controlCmd
= "program";
140 remove(imagefile
.c_str());
146 result_t
Mote::cancelProgramming()
150 if (controlCmd
!= "program")
153 success
= endChild(true);
154 remove(imagefile
.c_str());
156 return success
? SUCCESS
: FAILURE
;
159 result_t
Mote::getChildResult()
161 bool success
= endChild(false);
163 remove(imagefile
.c_str());
165 return success
? SUCCESS
: FAILURE
;
169 status_t
Mote::getStatus()
171 if (hasChild() && controlCmd
== "program")
172 return MOTE_PROGRAMMING
;
173 if (!isOpen()) return MOTE_UNAVAILABLE
;
174 if (isRunning
) return MOTE_RUNNING
;
178 const std::string
& Mote::getControlCommand()
184 const std::string
& Mote::getMac()
189 const std::string
& Mote::getDevicePath()
194 const std::string
& Mote::getPlatform()