Wed, 23 Apr 2014 21:29:37 +0200
Loop runs
/***************************************************************************** * Copyright (C) 2014 * * Michiel Broek <mbroek at mbse dot eu> * * This file is part of the mbsePi-apps * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * mbsePi-apps is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with EC-65K; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ #include "../lib/mbselib.h" #include "main.h" #define STATUS_CONNECTING 0 #define STATUS_CONNACK_RECVD 1 #define STATUS_WAITING 2 /* Global variables for use in callbacks. See sub_client.c for an example of * using a struct to hold variables for use in callbacks. */ static char *topic = NULL; //static char *message = NULL; //static long msglen = 0; static int qos = 0; //static int retain = 0; static int status = STATUS_CONNECTING; static int mid_sent = 0; static int last_mid = -1; static int last_mid_sent = -1; static bool connected = true; static bool disconnect_sent = false; static bool shutdown = false; extern bool debug; extern sys_config Config; int server(void); void help(void); void die(int); void help(void) { fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); fprintf(stdout, " -d --debug Debug on\n"); fprintf(stdout, " -h --help Display this help\n"); } void die(int onsig) { switch (onsig) { case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); break; case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); break; case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); break; default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } shutdown = true; } void my_connect_callback(struct mosquitto *mosq, void *obj, int result) { if (!result) { status = STATUS_CONNACK_RECVD; } else { syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result)); } } void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) { if (shutdown) { syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); connected = false; } else { /* * The remove server was brought down. We must keep running */ syslog(LOG_NOTICE, "Received DISCONNECT from %s but we want to run", Config.mosq_host); /* * We need a temp state */ } } void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) { fprintf(stdout, (char *)"my_publish_callback mid=%d\n", mid); last_mid_sent = mid; } void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) { syslog(LOG_NOTICE, "MQTT: %s", str); printf("MQTT: %s\n", str); } int main(int argc, char *argv[]) { int rc, c, i; while (1) { int option_index = 0; static struct option long_options[] = { {"debug", 0, 0, 'c'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "dh", long_options, &option_index); if (c == -1) break; switch (c) { case 'd': debug = true; break; case 'h': help(); return 1; } } openlog("thermometers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); syslog(LOG_NOTICE, "mbsePi-apps thermometers v%s starting", VERSION); if (rdconfig()) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "halted"); return 1; } /* * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored * but that's live. This daemon should only be stopped by SIGTERM. * Don't catch SIGCHLD. */ for (i = 0; i < NSIG; i++) { if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) signal(i, (void (*))die); } rc = server(); syslog(LOG_NOTICE, "Finished, rc=%d", rc); return rc; } int server(void) { char *id = NULL, *state = NULL; struct mosquitto *mosq = NULL; char hostname[256], buf[1024]; int rc, keepalive = 60; unsigned int max_inflight = 20; char err[1024]; w1_therm *tmp1, *old1; /* * Initialize mosquitto communication */ mosquitto_lib_init(); /* * Build MQTT id */ hostname[0] = '\0'; gethostname(hostname, 256); hostname[255] = '\0'; id = xstrcpy((char *)"thermometers/"); id = xstrcat(id, hostname); if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { /* * Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } mosq = mosquitto_new(id, true, NULL); if(!mosq) { switch(errno) { case ENOMEM: syslog(LOG_NOTICE, "mosquitto_new: Out of memory"); break; case EINVAL: syslog(LOG_NOTICE, "mosquitto_new: Invalid id"); break; } mosquitto_lib_cleanup(); return 1; } if (debug) { mosquitto_log_callback_set(mosq, my_log_callback); } /* * Set our will */ state = xstrcpy((char *)"clients/"); state = xstrcat(state, hostname); state = xstrcat(state, (char *)"/thermometers/state"); sprintf(buf, "0"); rc = mosquitto_will_set(mosq, state, strlen(buf), buf, qos, true); if (rc) { if (rc == MOSQ_ERR_INVAL) { syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid"); } else if (rc == MOSQ_ERR_NOMEM) { syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory"); } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) { syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size"); } mosquitto_lib_cleanup(); return rc; } mosquitto_max_inflight_messages_set(mosq, max_inflight); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); rc = mosquitto_connect(mosq, Config.mosq_host, Config.mosq_port, keepalive); if (rc) { if (rc == MOSQ_ERR_ERRNO) { strerror_r(errno, err, 1024); syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err); } else { syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc); } mosquitto_lib_cleanup(); return rc; } syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port); /* * Initialise is complete, report our presence state */ mosquitto_loop_start(mosq); sprintf(buf, "1"); rc = mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, 1); fprintf(stdout, (char *)"Enter loop, connected %d\n", connected); do { if (status == STATUS_CONNACK_RECVD) { /* * Sleep just log enough to keep the system load low. */ // usleep(1); /* * Here send our sensors values */ for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { old1 = tmp1->next; fprintf(stdout, "s: %s\n", tmp1->name); } usleep(15000000); if (shutdown) { /* * Final publish 0 to clients/<hostname>/thermometers/state */ sprintf(buf, "0"); mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true); free(topic); last_mid = mid_sent; status = STATUS_WAITING; } } else if (status == STATUS_WAITING) { fprintf(stdout, (char *)"Waiting\n"); if (last_mid_sent == last_mid && disconnect_sent == false) { mosquitto_disconnect(mosq); disconnect_sent = true; } usleep(100000); } rc = MOSQ_ERR_SUCCESS; } while(rc == MOSQ_ERR_SUCCESS && connected); fprintf(stdout, (char *)"Out of loop\n"); mosquitto_loop_stop(mosq, false); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }