#include <sys/time.h>
#include <sys/queue.h>
#include <assert.h>
#define L2CAP_SOCKET_CHECKED
#include <bluetooth.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <usbhid.h>
#include "bthid_config.h"
#include "bthidd.h"
static int32_t write_pid_file (char const *file);
static int32_t remove_pid_file (char const *file);
static int32_t elapsed (int32_t tval);
static void sighandler (int32_t s);
static void usage (void);
static int32_t done = 0;
int32_t
main(int32_t argc, char *argv[])
{
struct bthid_server srv;
struct sigaction sa;
char const *pid_file = BTHIDD_PIDFILE;
char *ep;
int32_t opt, detach, tval, uinput;
memset(&srv, 0, sizeof(srv));
memset(&srv.bdaddr, 0, sizeof(srv.bdaddr));
detach = 1;
tval = 10;
uinput = 0;
while ((opt = getopt(argc, argv, "a:c:dH:hp:t:u")) != -1) {
switch (opt) {
case 'a':
if (!bt_aton(optarg, &srv.bdaddr)) {
struct hostent *he;
if ((he = bt_gethostbyname(optarg)) == NULL)
errx(1, "%s: %s", optarg, hstrerror(h_errno));
memcpy(&srv.bdaddr, he->h_addr, sizeof(srv.bdaddr));
}
break;
case 'c':
config_file = optarg;
break;
case 'd':
detach = 0;
break;
case 'H':
hids_file = optarg;
break;
case 'p':
pid_file = optarg;
break;
case 't':
tval = strtol(optarg, (char **) &ep, 10);
if (*ep != '\0' || tval <= 0)
usage();
break;
case 'u':
uinput = 1;
break;
case 'h':
default:
usage();
}
}
openlog(BTHIDD_IDENT, LOG_PID|LOG_PERROR|LOG_NDELAY, LOG_USER);
if (detach && daemon(0, 0) < 0) {
syslog(LOG_CRIT, "Could not become daemon. %s (%d)",
strerror(errno), errno);
exit(1);
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sighandler;
if (sigaction(SIGTERM, &sa, NULL) < 0 ||
sigaction(SIGHUP, &sa, NULL) < 0 ||
sigaction(SIGINT, &sa, NULL) < 0) {
syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
strerror(errno), errno);
exit(1);
}
sa.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &sa, NULL) < 0) {
syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
strerror(errno), errno);
exit(1);
}
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
if (sigaction(SIGCHLD, &sa, NULL) < 0) {
syslog(LOG_CRIT, "Could not install signal handlers. %s (%d)",
strerror(errno), errno);
exit(1);
}
if (read_config_file() < 0 || read_hids_file() < 0 ||
server_init(&srv) < 0 || write_pid_file(pid_file) < 0)
exit(1);
srv.uinput = uinput;
for (done = 0; !done; ) {
if (elapsed(tval))
client_rescan(&srv);
if (server_do(&srv) < 0)
break;
}
server_shutdown(&srv);
remove_pid_file(pid_file);
clean_config();
closelog();
return (0);
}
static int32_t
write_pid_file(char const *file)
{
FILE *pid;
assert(file != NULL);
if ((pid = fopen(file, "w")) == NULL) {
syslog(LOG_ERR, "Could not open file %s. %s (%d)",
file, strerror(errno), errno);
return (-1);
}
fprintf(pid, "%d", getpid());
fclose(pid);
return (0);
}
static int32_t
remove_pid_file(char const *file)
{
assert(file != NULL);
if (unlink(file) < 0) {
syslog(LOG_ERR, "Could not unlink file %s. %s (%d)",
file, strerror(errno), errno);
return (-1);
}
return (0);
}
static int32_t
elapsed(int32_t tval)
{
static struct timeval last = { 0, 0 };
struct timeval now;
gettimeofday(&now, NULL);
if (now.tv_sec - last.tv_sec >= tval) {
last = now;
return (1);
}
return (0);
}
static void
sighandler(int32_t s)
{
syslog(LOG_NOTICE, "Got signal %d, total number of signals %d",
s, ++ done);
}
static void
usage(void)
{
fprintf(stderr,
"Usage: %s [options]\n" \
"Where options are:\n" \
" -a address specify address to listen on (default ANY)\n" \
" -c file specify config file name\n" \
" -d run in foreground\n" \
" -H file specify known HIDs file name\n" \
" -h display this message\n" \
" -p file specify PID file name\n" \
" -t tval specify client rescan interval (sec)\n" \
" -u enable evdev protocol support\n" \
"", BTHIDD_IDENT);
exit(255);
}