Prepare MCH for handling other external commands than programming
[remote/remote-mci/issue-6.git] / diku_mch / Mote.cc
blob78cc5d39348f6c2bcd402c11ae2eaab1ae47be3a
1 #include "Mote.h"
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)
10 validate();
11 if (isvalid && !setupTty())
12 isvalid = false;
14 Log::info("Mote %s (%s) @ %s", mac.c_str(), platform.c_str(), path.c_str());
17 bool Mote::setupTty()
19 if (!openTty(ttyData))
20 return false;
22 if (stop() == FAILURE)
23 return false;
25 return true;
29 bool Mote::isValid()
31 return isvalid;
34 void Mote::invalidate()
36 isvalid = false;
39 void Mote::validate()
41 isvalid = true;
42 imagefile = directory + "image";
44 binProgram = directory + "programmer";
45 if (!File::exists(binProgram))
46 isvalid = false;
48 path = File::readFile(directory + "path");
49 if (path == "")
50 isvalid = false;
52 platform = File::readFile(directory + "platform");
53 if (platform == "")
54 isvalid = false;
56 ttyData = File::readLink(directory + "tty");
57 if (ttyData == "")
58 isvalid = false;
60 ttyControl = File::readLink(directory + "tty");
61 if (ttyControl == "")
62 isvalid = false;
64 if (!isvalid)
65 Log::warn("Mote %s is invalid", mac.c_str());
69 result_t Mote::start()
71 return power("start");
74 result_t Mote::stop()
76 return power("stop");
79 result_t Mote::reset()
81 return power("reset");
84 result_t Mote::power(const std::string cmd)
86 bool resetting = cmd == "reset";
88 if (!isOpen())
89 return FAILURE;
91 if (!resetting || controlDTR(isRunning)) {
92 bool enable = resetting ? !isRunning : cmd == "stop";
94 if (controlDTR(enable)) {
95 isRunning = !enable;
96 return SUCCESS;
99 /* Mirror that the first reset DTR change succeeded. */
100 if (resetting)
101 isRunning = !isRunning;
104 Log::error("Failed to %s mote %s: %s",
105 cmd.c_str(), mac.c_str(), strerror(errno));
106 closeTty();
107 return FAILURE;
111 result_t Mote::program(std::string tos, const uint8_t *image, uint32_t imagelen)
113 if (hasChild())
114 return FAILURE;
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(),
124 NULL
126 char * const envp[] = {
127 (char *) mac_env.c_str(),
128 (char *) tos_env.c_str(),
129 (char *) platform_env.c_str(),
130 NULL
133 Log::info("Programming mote %s", mac.c_str());
135 if (runChild(args, envp)) {
136 controlCmd = "program";
137 return SUCCESS;
140 remove(imagefile.c_str());
143 return FAILURE;
146 result_t Mote::cancelProgramming()
148 bool success;
150 if (controlCmd != "program")
151 return FAILURE;
153 success = endChild(true);
154 remove(imagefile.c_str());
155 setupTty();
156 return success ? SUCCESS : FAILURE;
159 result_t Mote::getChildResult()
161 bool success = endChild(false);
163 remove(imagefile.c_str());
164 setupTty();
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;
175 return MOTE_STOPPED;
178 const std::string& Mote::getControlCommand()
180 return controlCmd;
184 const std::string& Mote::getMac()
186 return mac;
189 const std::string& Mote::getDevicePath()
191 return path;
194 const std::string& Mote::getPlatform()
196 return platform;