X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=blobdiff_plain;f=linux%2Fpulsecounter.c;h=89568c98597a972c45fb24e562c03642558e58a9;hp=da0cbc994154c71020833c80fe3f6c6f37a8f9d9;hb=757a8cd063d57fa1dc7628cac5e8cde9d45c10b9;hpb=bf310cc703ac0ed8be107c8f93b63d646b711f26 diff --git a/linux/pulsecounter.c b/linux/pulsecounter.c index da0cbc9..89568c9 100644 --- a/linux/pulsecounter.c +++ b/linux/pulsecounter.c @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -31,6 +32,8 @@ static char *opt_dst_type = NULL; 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; @@ -47,9 +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_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) { @@ -129,9 +156,10 @@ 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"); context = g_option_context_new(NULL); g_option_context_add_main_entries(context, options, NULL); if (!g_option_context_parse(context, &argc, &argv, &gerr)) { @@ -140,6 +168,17 @@ int main(int argc, char *argv[]) got_error = TRUE; goto done; } + if (!opt_dst) { + g_error("Destination MAC address must be specified"); + got_error = TRUE; + goto done; + } + if (dbconfig(opt_dbconffile)) { + g_error("Could not parse database configuration file"); + 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); @@ -161,6 +200,7 @@ done: g_free(opt_src); g_free(opt_dst); g_free(opt_sec_level); + g_free(opt_dbconffile); if (got_error) exit(EXIT_FAILURE); else