diff options
author | Bjørn Mork <bjorn@mork.no> | 2019-05-24 15:49:59 +0200 |
---|---|---|
committer | Bjørn Mork <bjorn@mork.no> | 2019-05-24 15:49:59 +0200 |
commit | 7be09931bfee37199ef669f37e3fc270988fa569 (patch) | |
tree | de0503e874a9e7c479febb1f1e2c626950839aa7 | |
parent | 9252539dac52559d1efac99571bf8d9acf6b1c58 (diff) |
wip - to be continued
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r-- | obinsectd.c | 99 |
1 files changed, 85 insertions, 14 deletions
diff --git a/obinsectd.c b/obinsectd.c index 9f4c292..3784ec8 100644 --- a/obinsectd.c +++ b/obinsectd.c @@ -39,13 +39,16 @@ #ifndef VERSION #define VERSION "unknown" #endif -#define DESCRIPTION "obinsectd (" VERSION ")" +#define CONFIG_FILE "/etc/obinsect.conf" #define MQTT_BROKER "localhost" #define MQTT_PORT 1883 #define MQTTS_PORT 8883 #define MQTT_KEEPALIVE 60 +/* parsed configuration */ +static json_object *cfg = NULL; + #define BUFSIZE (1024 * 2) @@ -1076,12 +1079,34 @@ static json_object *normalize(json_object *json) return ret; } +static json_object *get_value(json_object *val, json_object *json, json_object *normal, unsigned char *raw, size_t rawlen) +{ + char *type; + + if (json_object_is_type(val, json_type_array)) { + return NULL; // FIXME! + } + type = json_object_get_string(val); + if (!strncmp(key, "rawhexdump", 4)) { + return NULL; // FIXME! + } else if (!strncmp(key, "full", 4)) { + return json_object_get(json); + } else if (!strncmp(key, "normal", 4)) { + return json_object_get(normal); + } else if (json_object_object_get_ex(normal, key, &val)) // OBIS codes and timestamp + return json_object_get(val); + } + return NULL; +} + // FIMXE: topic, qos and variable mapping should be configurable - json config file? static int publish(struct mosquitto *mosq, json_object *json, json_object *normal, unsigned char *raw, size_t rawlen) { static int mid = 1; const char *pub; + char *t; size_t publen; + json_object *topics, *val; print_packet("*** raw packet:\n", raw, rawlen); debug("*** json:\n%s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY)); @@ -1089,9 +1114,22 @@ static int publish(struct mosquitto *mosq, json_object *json, json_object *norma // * requires libjson-c version 0.13+ // pub = json_object_to_json_string_length(normal, JSON_C_TO_STRING_PLAIN, &publen); - pub = json_object_to_json_string_ext(normal, JSON_C_TO_STRING_PLAIN); - publen = strlen(pub); - mosquitto_publish(mosq, &mid, "/foo/bar", publen, pub, 0, false); + + /* nothing published unless configured */ + if (!cfg || !json_object_object_get_ex(cfg, "topicmap", &topics)) + return 0; + + json_object_object_foreach(topics, t, val) { + val = get_value(val, json, normal, raw, rawlen); + if (!val) + continue; + publen = strlen(pub); + pub = json_object_to_json_string_ext(val, JSON_C_TO_STRING_PLAIN); + publen = strlen(pub); + mosquitto_publish(mosq, &mid, t, publen, pub, 0, false); + json_object_put(val); + } + return 0; } @@ -1185,9 +1223,27 @@ skipframe: return 0; } + +static json_object *read_config(const char *fname, unsigned char *buf, size_t bufsize) +{ + int fd = open(fname, O_RDONLY); + ssize_t len; + + if (fd < 0) + return NULL; + + len = read(fd, buf, buflen); + close(fd); + if (len <= 0) + return NULL; + + return json_tokener_parse(buf); +} + static struct option main_options[] = { { "help", 0, 0, 'h' }, + { "config", 0, 0, 'c' }, { "debug", 0, 0, 'd' }, { "broker", 1, 0, 'b' }, @@ -1215,8 +1271,8 @@ static void usage(const char *prog) int maj, min, rev; mosquitto_lib_version(&maj, &min, &rev); - printf("%s version %s, using libmosquitto %u.%u.%u\n", prog, VERSION, maj, min, rev); - printf("Usage: %s [-d] -s device\n", prog); + printf("%s version %s, using libmosquitto %u.%u.%u and libjson-c %s\n\n", prog, VERSION, maj, min, rev, json_c_version()); + printf("Usage: %s [-d] [-c configfile] -s device\n", prog); printf(" [-b hostname] [-p port] [-u username [-P password]]\n"); #ifdef WITH_TLS printf(" [-i id] [-k keepalive] [--insecure]\n"); @@ -1225,19 +1281,20 @@ static void usage(const char *prog) printf(" [-i id] [-k keepalive]\n"); #endif - printf(" -d : enable debugging\n"); - printf(" -s : serial device connected to M-Bus. E.g /dev/ttyUSB0. Use '-' to read from stdin\n"); + printf(" -c : Configuration file. Default: %s\n", CONFIG_FILE); + printf(" -d : Enable debugging\n"); + printf(" -s : Serial device connected to M-Bus. E.g /dev/ttyUSB0. Use '-' to read from stdin\n"); printf("\nMQTT client options:\n"); printf(" -b : Broker hostname or IP address. Default: %s\n", MQTT_BROKER); - printf(" -i : Client Id. A random Id is generated by default\n"); + printf(" -i : Client Id. A random id is generated by default\n"); printf(" -k : Keepalive in seconds. Default: %u\n", MQTT_KEEPALIVE); printf(" -p : Broker TCP port. Default: %u (%u when using MQTTS)\n", MQTT_PORT, MQTTS_PORT); printf(" -P : Password. Default is no authentication\n"); printf(" -u : Username\n"); #ifdef WITH_TLS - printf("\nOnly for MQTTS (MQTT over TLS):\n"); + printf("\nMQTT over TLS (MQTTS):\n"); printf(" --insecure : Do not validate brokername against certificate\n"); printf(" --cafile : Trusted CA certificates PEM file\n"); printf(" --capath : Trusted CA certificates directory\n"); @@ -1245,14 +1302,16 @@ static void usage(const char *prog) printf(" --key : Client private key\n"); #endif -} + printf("\nExample: %s -s /dev/ttyUSB0 -b broker.example.com\n", prog); +} int main(int argc, char *argv[]) { int opt, serfd = -1, ret = 0; static unsigned char *buf = NULL; + char *cfgfile = CONFIG_FILE; // mosquitto client struct mosquitto *mosq = NULL; // mosquitto opts @@ -1272,8 +1331,7 @@ int main(int argc, char *argv[]) bool insecure = false; #endif - fprintf(stderr, "%s\n", DESCRIPTION); - while ((opt = getopt_long(argc, argv, "?hdb:i:k:p:P:s:u", main_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "?hdb:c:i:k:p:P:s:u", main_options, NULL)) != -1) { switch(opt) { case '?': case 'h': @@ -1285,6 +1343,9 @@ int main(int argc, char *argv[]) case 'b': broker = optarg; break; + case 'c': + cfgfile = optarg; + break; case 'i': mqttid = optarg; break; @@ -1323,7 +1384,12 @@ int main(int argc, char *argv[]) } } - /* initialize mqtt client and read buffer */ + /* print banner */ + fprintf(stderr, "%s version %s\n", argv[0], VERSION); + + + + /* initialize mqtt client and read buffer */ mosquitto_lib_init(); mosq = mosquitto_new(mqttid, clean_session, NULL); buf = malloc(BUFSIZE); @@ -1333,6 +1399,11 @@ int main(int argc, char *argv[]) goto err; } + /* read config file */ + cfg = read_config(cfgfile, buf, BUFSIZE); + if (!cfg) + fprintf(stderr, "Failed to parse '%s' - will not publish anything to '%s'\n", cfgfile, broker); + /* configure broker connection */ if (mqttuser) mosquitto_username_pw_set(mosq, mqttuser, mqttpw); |