Move protocols/types.h content to remote.h
[remote/remote-mci.git] / mch / MoteHost.h
blob88ac5da9cd4dadbe88799b9efcef6b6581c1bde0
1 #ifndef REMOTE_MCH_MOTEHOST_H
2 #define REMOTE_MCH_MOTEHOST_H
4 #include "remote.h"
6 #include "protocols/tcputil.h"
7 #include "protocols/Message.h"
8 #include "protocols/motecontrol/MoteMsg.h"
9 #include "protocols/host_server/HostMsg.h"
10 #include "protocols/host_server/MsgMoteConnectionInfoList.h"
11 #include "protocols/host_server/MsgMoteAddresses.h"
13 #include "mch/DeviceManager.h"
14 #include "mch/Mote.h"
16 namespace remote { namespace mch {
18 using namespace remote::protocols;
19 using namespace remote::protocols::host_server;
20 using namespace remote::protocols::motecontrol;
22 /** Mote control host.
24 * This class connects the various MCH parts into a mote host.
25 * On startup it will connect to the mote control server and
26 * start listening for new control message. It will also open
27 * the mote plug pipe to receive events from external scripts
28 * when new motes are attached and detached. Finally, it will
29 * listen to the TTY of all motes and forward mote console data
30 * to the mote control server.
32 class MoteHost
34 public:
35 /** Start mote control host.
37 * @param argc Number of command line arguments.
38 * @param argv String array of command line arguments.
39 * @return The result code of the terminating mote host.
41 static int main(int argc, char **argv);
43 private:
44 /** Establish connection to server. */
45 static void lookForServer();
47 /** Main event loop. */
48 static void serviceLoop();
50 /** Handle mote plug/unplug event. */
51 static void handlePlugEvent();
53 /** Handle incoming message from mote control server. */
54 static void handleMessage();
56 /** Handle command request for mote.
58 * @param mote The mote to which the request belongs.
59 * @param addresses The address information of the mote.
60 * @param request The request to handle.
62 static void handleRequest(Mote *mote, MsgMoteAddresses& addresses, MsgRequest& request);
64 /** Handle serial data for mote.
66 * @param mote The mote which has incoming serial data.
68 static void handleMoteData(Mote *mote);
70 /** Send request confirmation message.
72 * @param mote The mote for which the request was addressed.
73 * @param command The request command that was executed.
74 * @param result The result of the command execution.
76 static void confirmRequest(Mote *mote, uint8_t command, result_t result);
78 /** Get the status code of a mote.
80 * @param mote The mote for which to get the status code.
81 * @return The mote status code.
83 static status_t getMoteStatus(Mote *mote);
85 /** Prepare plug event message payload.
87 * @param motelist The mote map from which prepare the message.
88 * @param info The message payload which to prepare.
89 * @return True if the payload should be sent.
91 static bool makeMoteInfoList(motemap_t& motelist, MsgMoteConnectionInfoList& infolist);
93 /** Rebuild select() file descriptor set.
95 * @param fdset The file descriptor set to rebuild.
96 * @return The number of file descriptors in the fdset.
98 static int rebuildFdSet(fd_set& fdset);
100 /** Exit handler to be used with atexit.
102 static void handleExit();
104 /** Handle received signal
106 * @param signo The number of the received signal.
108 static void handleSignal(int signo);
110 /** Install signal handlers
112 * Setup handlers to receive exit signals.
114 static void installSignalHandlers();
116 /** Shutdown the mote host?
118 * @return True if the mote host should shutdown.
120 static inline bool exitDaemon()
122 return exitSignal != 0;
125 static int clientsock; /**< Server connection socket. */
126 static int plugpipe; /**< Plug event pipe. */
127 static Message msg; /**< Message manager. */
128 static DeviceManager devices; /**< Mote device manager. */
129 static int exitSignal; /**< Caught exit signal or zero. */
131 /* Option values. */
132 static bool daemonize;
133 static std::string pidFile;
134 static std::string serverHost;
135 static uint16_t serverPort;
136 static uint64_t retryInterval;
137 static std::string eventPipe;
138 static std::string devicePath;
143 #endif