Use different TTY paths for control and console data
[remote/remote-mci.git] / diku_mch / Mote.cc
blob11fd2f036fcf164fdac81f57fe3c29450164e5a1
1 #include "Mote.h"
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),
15 controlCmd(NONE)
17 validate();
18 if (isvalid && !setupTty(STOP))
19 isvalid = false;
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))
27 return false;
29 if (cmd == START || cmd == STOP || cmd == RESET)
30 return power(cmd) == SUCCESS;
32 return true;
36 bool Mote::isValid()
38 return isvalid;
41 void Mote::invalidate()
43 isvalid = false;
46 void Mote::validate()
48 isvalid = true;
49 imagefile = directory + "image";
51 programmer = directory + "programmer";
52 if (programmer == "" || !File::exists(programmer))
53 isvalid = false;
55 path = File::readFile(directory + "path");
56 if (path == "")
57 isvalid = false;
59 platform = File::readFile(directory + "platform");
60 if (platform == "")
61 isvalid = false;
63 ttyData = File::readLink(directory + "tty/data");
64 if (ttyData == "")
65 isvalid = false;
67 ttyControl = File::readLink(directory + "tty/control");
68 if (ttyControl == "")
69 isvalid = false;
71 if (!isvalid)
72 Log::warn("Mote %s is invalid", mac.c_str());
76 result_t Mote::start()
78 return power(START);
81 result_t Mote::stop()
83 return power(STOP);
86 result_t Mote::reset()
88 return power(RESET);
91 result_t Mote::power(const std::string cmd)
93 bool resetting = cmd == RESET;
95 if (!isOpen())
96 return FAILURE;
98 if (!resetting || controlDTR(isRunning)) {
99 bool enable = resetting ? !isRunning : cmd == STOP;
101 if (controlDTR(enable)) {
102 isRunning = !enable;
103 return SUCCESS;
106 /* Mirror that the first reset DTR change succeeded. */
107 if (resetting)
108 isRunning = !isRunning;
111 Log::error("Failed to %s mote %s: %s",
112 cmd.c_str(), mac.c_str(), strerror(errno));
113 closeTty();
114 return FAILURE;
118 result_t Mote::program(std::string tos, const uint8_t *image, uint32_t imagelen)
120 if (hasChild())
121 return FAILURE;
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(),
131 NULL
133 char * const envp[] = {
134 (char *) mac_env.c_str(),
135 (char *) tos_env.c_str(),
136 (char *) platform_env.c_str(),
137 NULL
140 Log::info("Programming mote %s", mac.c_str());
142 if (runChild(args, envp)) {
143 controlCmd = PROGRAM;
144 return SUCCESS;
147 remove(imagefile.c_str());
150 return FAILURE;
153 result_t Mote::cancelProgramming()
155 if (getControlCommand() != PROGRAM)
156 return FAILURE;
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());
169 if (!setupTty(cmd))
170 success = false;
172 controlCmd = NONE;
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;
183 return MOTE_STOPPED;
186 const std::string& Mote::getControlCommand()
188 return hasChild() ? controlCmd : NONE;
192 const std::string& Mote::getMac()
194 return mac;
197 const std::string& Mote::getDevicePath()
199 return path;
202 const std::string& Mote::getPlatform()
204 return platform;