]> www.average.org Git - pulsecounter.git/commitdiff
keep database config in a separate file
authorEugene Crosser <crosser@average.org>
Sat, 19 Dec 2015 19:38:16 +0000 (22:38 +0300)
committerEugene Crosser <crosser@average.org>
Sat, 19 Dec 2015 19:38:16 +0000 (22:38 +0300)
linux/dbstore.c
linux/dbstore.h
linux/pulsecounter.c

index 395654da09f681d35921206e0103d2decbf91093..b39235b1b2eb66c241840e2e9b4d223862555447 100644 (file)
@@ -1,11 +1,62 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <time.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <time.h>
+#include <ctype.h>
+#include <string.h>
 
 #include <mysql/mysql.h>
 
 #include "dbstore.h"
 
 
 #include <mysql/mysql.h>
 
 #include "dbstore.h"
 
+static char *host = NULL;
+static char *user = NULL;
+static char *pass = NULL;
+static char *dbnm = "watermeter";
+
+int dbconfig(char *conffile)
+{
+       FILE *fp = fopen(conffile, "r");
+       int rc = 0;
+       char buf[128];
+
+       if (!fp)
+               return 1;
+       while (fgets(buf, sizeof(buf), fp)) {
+               char *k, *v, *e;
+
+               e = buf + strlen(buf) - 1;
+               if (*e == '\n')
+                       *e = '\0';
+               else {
+                       /* line too long */
+                       rc = 1;
+                       break;
+               }
+               for (k = buf; k < e && isspace(k); k++) /*nothing*/ ;
+               if (*k == '#') break;
+               for (v = k; v < e && !isspace(v)
+                           && *v != ':' && *v != '='; v++) /*nothing*/ ;
+               if (v < e && (*v == ':' || *v == '=')) v++;
+               for (; v < e && (isspace(v) || *v == ':' || *v == '=')
+                                                       ; v++) /*nothing*/ ;
+               if (v >= e) {
+                       /* no value */
+                       rc = 1;
+                       break;
+               }
+               if      (!strcmp(k, "host"))     host = strdup(v);
+               else if (!strcmp(k, "user"))     user = strdup(v);
+               else if (!strcmp(k, "password")) pass = strdup(v);
+               else if (!strcmp(k, "database")) dbnm = strdup(v);
+               else {
+                       /* unknown key */
+                       rc = 1;
+                       break;
+               }
+       }
+       return rc;
+}
+
 int dbstore(uint8_t which, uint32_t val)
 {
        time_t t;
 int dbstore(uint8_t which, uint32_t val)
 {
        time_t t;
@@ -22,8 +73,7 @@ int dbstore(uint8_t which, uint32_t val)
        (void)gmtime_r(&t, &tm);
        (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
        mysql_init(&mysql);
        (void)gmtime_r(&t, &tm);
        (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
        mysql_init(&mysql);
-       if(!mysql_real_connect(&mysql, NULL, "pulsecounter",
-                               "xxxxxxxxxxxxx", "watermeter", 0, NULL, 0)) {
+       if(!mysql_real_connect(&mysql, host, user, pass, dbnm, 0, NULL, 0)) {
                fprintf(stderr, "mysql connect error: %s\n",
                        mysql_error(&mysql));
                return 1;
                fprintf(stderr, "mysql connect error: %s\n",
                        mysql_error(&mysql));
                return 1;
index 813f72ba134895c5817538dff75b2dd078e09ac8..8c2f847bec7ea94ed40ddb8d68fa66a2db10a410 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _DBSTORE_H
 #define _DBSTORE_H
 
 #ifndef _DBSTORE_H
 #define _DBSTORE_H
 
+extern int dbconfig(char *conffile);
 extern int dbstore(uint8_t which, uint32_t val);
 
 #endif
 extern int dbstore(uint8_t which, uint32_t val);
 
 #endif
index da0cbc994154c71020833c80fe3f6c6f37a8f9d9..5f629f14b96b41125c9023687e1502cb5b6eeded 100644 (file)
@@ -31,6 +31,7 @@ static char *opt_dst_type = NULL;
 static int opt_mtu = 0;
 static int opt_psm = 0;
 static char *opt_sec_level = NULL;
 static int opt_mtu = 0;
 static int opt_psm = 0;
 static char *opt_sec_level = NULL;
+static char *opt_dbconffile = NULL;
 
 static GMainLoop *event_loop;
 
 
 static GMainLoop *event_loop;
 
@@ -47,6 +48,8 @@ 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]"},
                "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,
+               "Specify file name with database configuration", "cfile"},
        { NULL },
 };
 
        { NULL },
 };
 
@@ -132,6 +135,7 @@ int main(int argc, char *argv[])
                          | G_LOG_FLAG_RECURSION, g_log_default_handler, NULL);
        opt_dst_type = g_strdup("public");
        opt_sec_level = g_strdup("low");
                          | G_LOG_FLAG_RECURSION, g_log_default_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)) {
        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 +144,16 @@ int main(int argc, char *argv[])
                got_error = TRUE;
                goto done;
        }
                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;
+       }
        while (1) {
                chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
                        opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);
        while (1) {
                chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
                        opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);
@@ -161,6 +175,7 @@ done:
        g_free(opt_src);
        g_free(opt_dst);
        g_free(opt_sec_level);
        g_free(opt_src);
        g_free(opt_dst);
        g_free(opt_sec_level);
+       g_free(opt_dbconffile);
        if (got_error)
                exit(EXIT_FAILURE);
        else
        if (got_error)
                exit(EXIT_FAILURE);
        else