]> average.org Git - pulsecounter.git/blobdiff - linux/pulsecounter.c
mark possible place that is losing events
[pulsecounter.git] / linux / pulsecounter.c
index 5f629f14b96b41125c9023687e1502cb5b6eeded..cc05da904ce1f3d3acb6f09a881a7dc2615ff43a 100644 (file)
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <stdbool.h>
 #include <time.h>
+#include <syslog.h>
 
 #include <glib.h>
 
@@ -32,6 +33,7 @@ static int opt_mtu = 0;
 static int opt_psm = 0;
 static char *opt_sec_level = NULL;
 static char *opt_dbconffile = NULL;
+static gboolean opt_daemon = FALSE;
 
 static GMainLoop *event_loop;
 
@@ -48,11 +50,33 @@ static GOptionEntry options[] = {
                "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
        { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
                "Set security level. Default: low", "[low | medium | high]"},
-       { "dbconfig", 'c', 0, G_OPTION_ARG_STRING, &opt_dbconffile,
+       { "dbconfig", 'c', 0, G_OPTION_ARG_FILENAME, &opt_dbconffile,
+               "Specify file name with database configuration", "cfile"},
+       { "daemon", 'd', 0, G_OPTION_ARG_NONE, &opt_daemon,
                "Specify file name with database configuration", "cfile"},
        { NULL },
 };
 
+void local_log_handler(const gchar *log_domain, GLogLevelFlags log_level,
+                       const gchar *message, gpointer log_context)
+{
+       int syslog_level;
+
+       switch (log_level) {
+       case G_LOG_LEVEL_CRITICAL:      syslog_level = LOG_CRIT;        break;
+       case G_LOG_LEVEL_ERROR:         syslog_level = LOG_ERR;         break;
+       case G_LOG_LEVEL_WARNING:       syslog_level = LOG_WARNING;     break;
+       case G_LOG_LEVEL_MESSAGE:       syslog_level = LOG_NOTICE;      break;
+       case G_LOG_LEVEL_INFO:          syslog_level = LOG_INFO;        break;
+       case G_LOG_LEVEL_DEBUG:         syslog_level = LOG_DEBUG;       break;
+       default:                        syslog_level = LOG_INFO;
+       }
+       if (!log_domain || (log_domain[0] == '\0'))
+               syslog(syslog_level, "%s", message);
+       else
+               syslog(syslog_level, "%s: %s", log_domain, message);
+}
+
 static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
                                gpointer user_data)
 {
@@ -72,12 +96,17 @@ static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
 
        handle = bt_get_le16(&pdu[1]);
        which = pdu[3];
-       if ((pdu[0] == 0x1b) && (handle == 0x0012) && (len == 9) &&
-           ((which == 1) || (which == 2))) {
+       if ((pdu[0] == 0x1b) && (handle == 0x0012) && (len == 9)) {
                uint32_t val = bt_get_le32(&pdu[5]);
-               g_debug("store: \"%hhu,%u\"\n", which, val);
-               if (dbstore(which, val))
-                       g_warning("error storing \"%hhu,%u\"\n", which, val);
+
+               if ((which == 1) || (which == 2)) {
+                       g_debug("store: \"%hhu,%u\"\n", which, val);
+                       if (dbstore(which, val))
+                               g_warning("error storing \"%hhu,%u\"\n",
+                                               which, val);
+               } else {
+                       g_debug("jitter: \"%hhu,%u\"\n", which, val);
+               }
        } else {
                time_t t;
                int i;
@@ -132,7 +161,7 @@ int main(int argc, char *argv[])
        gboolean got_error = FALSE;
 
        g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
-                         | G_LOG_FLAG_RECURSION, g_log_default_handler, NULL);
+                         | G_LOG_FLAG_RECURSION, local_log_handler, NULL);
        opt_dst_type = g_strdup("public");
        opt_sec_level = g_strdup("low");
        opt_dbconffile = g_strdup("/etc/pulsecounter.db");
@@ -154,6 +183,7 @@ int main(int argc, char *argv[])
                got_error = TRUE;
                goto done;
        }
+       if (opt_daemon) daemon(0, 0);
        while (1) {
                chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
                        opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);