Move protocols/types.h content to remote.h
[remote/remote-mci.git] / mch / SerialControl.h
blob2cab82ec3d2858600d7f642df2afe080b188fc8b
1 #ifndef REMOTE_MCH_SERIALCONTROL_H_
2 #define REMOTE_MCH_SERIALCONTROL_H_
4 #include "remote.h"
6 namespace remote { namespace mch {
8 /** Serial Connection Control
10 * This class provides a low-level interface for managing a serial
11 * connection to a mote, such as opening and closing TTYs as well
12 * as starting and ending child processes operating on the mote TTYs.
14 class SerialControl
16 public:
17 /** Create serial control. */
18 SerialControl();
20 /** Destroy serial control. */
21 ~SerialControl();
23 /** Create and run child process.
25 * @param args The program and args.
26 * @param envp The process environment.
27 * @return True if the start was created.
29 bool runChild(char * const args[], char * const envp[]);
31 /** Read buffer from the serial port.
33 * @param buf The buffer start position.
34 * @param len The buffer maximum length.
35 * @return The number of read bytes.
37 ssize_t readBuf(char *buf, size_t len);
39 /** Write buffer to the serial port.
41 * @param buf The buffer start position.
42 * @param len The buffer length.
43 * @return The number of written bytes.
45 ssize_t writeBuf(const char *buf, size_t len);
47 /** Get the file descriptor for the serial port.
49 * @return The serial port being controlled.
51 int getFd();
53 /** Is the serial control port open?
55 * @return True if the port is open.
57 bool isOpen();
59 protected:
60 /** Does any children exist?
62 * @return True if the mote has a child.
64 bool hasChild();
66 /** Open serial control for TTY
68 * This method opens the request TTY. The platform string is used
69 * for determining the proper baud rate.
71 * @param platform The platform of the mote.
72 * @param tty The path to the TTY device.
73 * @return True if the TTY was successfully opened.
75 bool openTty(const std::string platform, const std::string tty);
77 /** Close serial control for opened TTY. */
78 void closeTty();
80 /** Get exit result of child.
82 * @param killChild Force the child to end by killing it.
83 * @return True if the child exited successfully.
85 bool endChild(bool killChild);
87 /** Enable or disable DTR.
89 * The DTR channel is wired to the RESET pin on the dig528-2 motes.
90 * Using this hardware configuration motes can be stopped and started
91 * by enabling/disabling it respectively.
93 * @param enable Set the DTR pin high.
94 * @return True if the operation succeeded.
96 bool controlDTR(bool enable);
98 int port; /**< Serial file descriptor (or -1). */
99 pid_t childPid; /**< Process ID of child (or -1). */
100 struct termios oldsertio; /**< Saved terminal settings. */
105 #endif