1 #ifndef REMOTE_MCH_MOTEHOST_H
2 #define REMOTE_MCH_MOTEHOST_H
5 #include <sys/select.h>
11 #include "libutil/error.h"
14 #include "motecontrol/MoteMsg.h"
15 #include "diku_host_server/HostMsg.h"
16 #include "diku_host_server/MsgMoteConnectionInfoList.h"
17 #include "diku_host_server/MsgMoteAddresses.h"
18 #include "DeviceManager.h"
20 #include "Configuration.h"
22 namespace remote
{ namespace diku_mch
{
24 using namespace protocols
;
25 using namespace protocols::diku_host_server
;
26 using namespace protocols::motecontrol
;
28 /** Mote control host.
30 * This class connects the various MCH parts into a mote host.
31 * On startup it will connect to the mote control server and
32 * start listening for new control message. It will also open
33 * the mote plug pipe to receive events from external scripts
34 * when new motes are attached and detached. Finally, it will
35 * listen to the TTY of all motes and forward mote console data
36 * to the mote control server.
41 /** Start mote control host.
43 * @param argc Number of command line arguments.
44 * @param argv String array of command line arguments.
45 * @return The result code of the terminating mote host.
47 static int main(int argc
, char **argv
);
50 /** Establish connection to server. */
51 static void lookForServer();
53 /** Main event loop. */
54 static void serviceLoop();
56 /** Handle mote plug/unplug event. */
57 static void handlePlugEvent();
59 /** Handle incoming message from mote control server. */
60 static void handleMessage();
62 /** Handle command request for mote.
64 * @param mote The mote to which the request belongs.
65 * @param addresses The address information of the mote.
66 * @param request The request to handle.
68 static void handleRequest(Mote
*mote
, MsgMoteAddresses
& addresses
, MsgRequest
& request
);
70 /** Handle serial data for mote.
72 * @param mote The mote which has incoming serial data.
74 static void handleMoteData(Mote
*mote
);
76 /** Send request confirmation message.
78 * @param mote The mote for which the request was addressed.
79 * @param command The request command that was executed.
80 * @param result The result of the command execution.
82 static void confirmRequest(Mote
*mote
, uint8_t command
, result_t result
);
84 /** Prepare plug event message payload.
86 * @param motelist The mote map from which prepare the message.
87 * @param info The message payload which to prepare.
88 * @return True if the payload should be sent.
90 static bool makeMoteInfoList(motemap_t
& motelist
, MsgMoteConnectionInfoList
& infolist
);
92 /** Rebuild select() file descriptor set.
94 * @param fdset The file descriptor set to rebuild.
95 * @return The number of file descriptors in the fdset.
97 static int rebuildFdSet(fd_set
& fdset
);
99 /** Exit handler to be used with atexit.
101 static void handleExit();
103 /** Handle received signal
105 * @param signo The number of the received signal.
107 static void handleSignal(int signo
);
109 /** Install signal handlers
111 * Setup handlers to receive exit signals.
113 static void installSignalHandlers();
115 /** Shutdown the mote host?
117 * @return True if the mote host should shutdown.
119 static inline bool exitDaemon()
121 return exitSignal
!= 0;
124 static int clientsock
; /**< Server connection socket. */
125 static int plugpipe
; /**< Plug event pipe. */
126 static Message msg
; /**< Message manager. */
127 static DeviceManager devices
; /**< Mote device manager. */
128 static Configuration config
; /**< Option manager. */
129 static int exitSignal
; /**< Caught exit signal or zero. */