26 #include <blackboard/remote.h>
27 #include <core/threading/thread.h>
28 #include <interfaces/SwitchInterface.h>
29 #include <utils/system/argparser.h>
30 #include <utils/system/signal.h>
31 #include <utils/time/time.h>
37 # include <libdaemon/dfork.h>
38 # include <libdaemon/dlog.h>
39 # include <libdaemon/dpid.h>
40 # include <sys/stat.h>
41 # include <sys/wait.h>
68 while (!(bb_ && bb_->is_alive() && switch_if_->is_valid())) {
70 printf(
"Lost connection to blackboard\n");
71 bb_->close(switch_if_);
76 printf(
"Trying to connect to remote BB...");
79 printf(
"succeeded\n");
90 if ((now - until_) >= 0) {
97 while (!switch_if_->msgq_empty()) {
100 if (msg->
value() > 0.0) {
101 beep_.beep_on(msg->
value());
111 float duration = fabs(msg->
duration());
112 float value = fabs(msg->
value());
117 beep_.beep_on(value);
124 switch_if_->msgq_pop();
148 usage(
const char *progname)
152 #ifdef HAVE_LIBDAEMON
156 daemon_retval_send(-1);
157 daemon_retval_done();
158 daemon_pid_file_remove();
162 daemonize(
int argc,
char **argv)
165 mode_t old_umask = umask(0);
168 daemon_retval_init();
171 if ((pid = daemon_fork()) < 0) {
178 if ((ret = daemon_retval_wait(20)) < 0) {
179 daemon_log(LOG_ERR,
"Could not recieve return value from daemon process.");
184 daemon_log(LOG_ERR,
"*** Daemon startup failed, see syslog for details. ***");
186 case 1: daemon_log(LOG_ERR,
"Daemon failed to close file descriptors");
break;
187 case 2: daemon_log(LOG_ERR,
"Daemon failed to create PID file");
break;
195 # ifdef DAEMON_CLOSE_ALL_AVAILABLE
196 if (daemon_close_all(-1) < 0) {
197 daemon_log(LOG_ERR,
"Failed to close all file descriptors: %s", strerror(errno));
199 daemon_retval_send(1);
205 if (daemon_pid_file_create() < 0) {
206 printf(
"Could not create PID file (%s).", strerror(errno));
207 daemon_log(LOG_ERR,
"Could not create PID file (%s).", strerror(errno));
210 daemon_retval_send(2);
215 daemon_retval_send(0);
217 daemon_log(LOG_INFO,
"Sucessfully started");
226 const char *fawkes_pid_file;
232 fawkes_daemon_pid_file_proc()
234 return fawkes_pid_file;
243 main(
int argc,
char **argv)
247 #ifdef HAVE_LIBDAEMON
252 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
253 if (argp->
arg(
"D") != NULL) {
254 fawkes_pid_file = argp->
arg(
"D");
255 daemon_pid_file_proc = fawkes_daemon_pid_file_proc;
261 if ((pid = daemon_pid_file_is_running()) < 0) {
262 daemon_log(LOG_ERR,
"Fawkes daemon not running.");
268 if ((ret = daemon_pid_file_kill_wait(SIGINT, 5)) < 0) {
269 daemon_log(LOG_WARNING,
"Failed to kill daemon");
271 return (ret < 0) ? 1 : 0;
276 return (daemon_pid_file_is_running() < 0);
280 if ((pid = daemon_pid_file_is_running()) >= 0) {
281 daemon_log(LOG_ERR,
"Daemon already running on (PID %u)", pid);
285 pid = daemonize(argc, argv);
296 printf(
"Daemonizing support is not available.\n"
297 "(libdaemon[-devel] was not available at compile time)\n");
311 SignalManager::register_handler(SIGINT, &beepd);
312 SignalManager::register_handler(SIGTERM, &beepd);
317 Thread::destroy_main();
319 #ifdef HAVE_LIBDAEMON
Simple speaker beep controller.
virtual void loop()
Code to execute in the thread.
void handle_signal(int signum)
Handle signals.
FawkesBeepDaemon()
Constructor.
Parse command line arguments.
const char * arg(const char *argn)
Get argument value.
bool has_arg(const char *argn)
Check if argument has been supplied.
The BlackBoard abstract class.
Base class for exceptions in Fawkes.
Interface for signal handling.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
EnableDurationMessage Fawkes BlackBoard Interface Message.
float value() const
Get value value.
float duration() const
Get duration value.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
SetMessage Fawkes BlackBoard Interface Message.
float value() const
Get value value.
bool is_enabled() const
Get enabled value.
SwitchInterface Fawkes BlackBoard Interface.
Thread class encapsulation of pthreads.
void start(bool wait=true)
Call this method to start the thread.
void join()
Join the thread.
A class for handling time.
Fawkes library namespace.