mirror of https://github.com/sfeakes/AqualinkD.git
Serial debug & MQTT validation updates
parent
b55fffa28f
commit
c8480546cd
23
aq_serial.c
23
aq_serial.c
|
@ -48,7 +48,30 @@ void log_packet(unsigned char* packet, int length)
|
|||
logMessage(LOG_DEBUG, message_buffer);
|
||||
}
|
||||
|
||||
const char* get_packet_type(unsigned char* packet, int length)
|
||||
{
|
||||
if (length <= 0 )
|
||||
return "";
|
||||
|
||||
switch (packet[PKT_CMD]) {
|
||||
case CMD_ACK:
|
||||
return "Ack";
|
||||
break;
|
||||
case CMD_STATUS:
|
||||
return "Status";
|
||||
break;
|
||||
case CMD_MSG:
|
||||
case CMD_MSG_LONG:
|
||||
return "Message";
|
||||
break;
|
||||
case CMD_PROBE:
|
||||
return "Probe";
|
||||
break;
|
||||
default:
|
||||
return "Unknown";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Open and Initialize the serial communications port to the Aqualink RS8 device.
|
||||
|
|
|
@ -128,6 +128,7 @@ int get_packet(int file_descriptor, unsigned char* packet);
|
|||
//void close_serial_port(int file_descriptor, struct termios* oldtio);
|
||||
//void process_status(void const * const ptr);
|
||||
void process_status(unsigned char* ptr);
|
||||
const char* get_packet_type(unsigned char* packet, int length);
|
||||
|
||||
|
||||
|
||||
|
|
12
aqualinkd.c
12
aqualinkd.c
|
@ -491,7 +491,9 @@ void main_loop() {
|
|||
logMessage(LOG_ERR, "Bad packet length, reconnecting\n");
|
||||
blank_read = MAX_ZERO_READ_BEFORE_RECONNECT;
|
||||
} else if (packet_length == 0) {
|
||||
logMessage(LOG_DEBUG_SERIAL, "Nothing read on serial\n");
|
||||
#ifdef DEBUG_ALL_SERIAL
|
||||
logMessage(LOG_DEBUG_SERIAL, "Nothing read on serial\n");
|
||||
#endif
|
||||
blank_read++;
|
||||
} else if (packet_length > 0) {
|
||||
blank_read = 0;
|
||||
|
@ -499,16 +501,18 @@ void main_loop() {
|
|||
if (packet_length > 0 && packet_buffer[PKT_DEST] == _config_parameters.device_id) {
|
||||
send_ack(rs_fd, _aqualink_data.aq_command);
|
||||
_aqualink_data.aq_command = NUL;
|
||||
|
||||
// Process the packet. This includes deriving general status, and identifying
|
||||
// warnings and errors. If something changed, notify any listeners
|
||||
if (process_packet(packet_buffer, packet_length) != false) {
|
||||
broadcast_aqualinkstate(mgr.active_connections);
|
||||
}
|
||||
} else if (packet_length > 0) {
|
||||
// printf("packet not for us %02x\n",packet_buffer[PKT_DEST]);
|
||||
logMessage(LOG_DEBUG_SERIAL, "Received Packet, but not for us, ID is for 0x%02hhx\n",packet_buffer[PKT_DEST]);
|
||||
// printf("packet not for us %02x\n",packet_buffer[PKT_DEST]);
|
||||
}
|
||||
if (getLogLevel() >= LOG_DEBUG_SERIAL)
|
||||
logMessage(LOG_DEBUG_SERIAL, "Received Packet for ID 0x%02hhx of type %s %s\n",packet_buffer[PKT_DEST], get_packet_type(packet_buffer, packet_length),
|
||||
(packet_buffer[PKT_DEST] == _config_parameters.device_id)?" <-- Aqualinkd ID":"");
|
||||
|
||||
}
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
|
||||
|
|
|
@ -424,6 +424,7 @@ void action_websocket_request(struct mg_connection *nc, struct websocket_message
|
|||
|
||||
void action_mqtt_message(struct mg_connection *nc, struct mg_mqtt_message *msg) {
|
||||
int i;
|
||||
logMessage(LOG_DEBUG, "MQTT: topic %.*s %.2f\n",msg->topic.len, msg->topic.p, atof(msg->payload.p));
|
||||
//printf("Topic %.*s\n",msg->topic.len, msg->topic.p);
|
||||
// get the parts from the topic
|
||||
char *pt1 = (char *)&msg->topic.p[strlen(_aqualink_config->mqtt_aq_topic)+1];
|
||||
|
@ -441,7 +442,7 @@ void action_mqtt_message(struct mg_connection *nc, struct mg_mqtt_message *msg)
|
|||
}
|
||||
}
|
||||
|
||||
logMessage(LOG_DEBUG, "MQTT: topic %.*s %.2f\n",msg->topic.len, msg->topic.p, atof(msg->payload.p));
|
||||
//logMessage(LOG_DEBUG, "MQTT: topic %.*s %.2f\n",msg->topic.len, msg->topic.p, atof(msg->payload.p));
|
||||
//only care about topics with set at the end.
|
||||
//aqualinkd/Freeze/setpoint/set
|
||||
//aqualinkd/Filter_Pump/set
|
||||
|
@ -489,7 +490,8 @@ void action_mqtt_message(struct mg_connection *nc, struct mg_mqtt_message *msg)
|
|||
// Message is either a 1 or 0 for on or off
|
||||
int status = atoi(msg->payload.p);
|
||||
if ( status > 1 || status < 0) {
|
||||
logMessage(LOG_INFO, "MQTT: received unknown status of '%.*s' for '%s', Ignoring!\n", msg->payload.len, msg->payload.p, status, _aqualink_data->aqbuttons[i].name);
|
||||
logMessage(LOG_ERR, "MQTT: topic %.*s %.2f\n",msg->topic.len, msg->topic.p, atof(msg->payload.p));
|
||||
logMessage(LOG_ERR, "MQTT: received unknown status of '%.*s' for '%s', Ignoring!\n", msg->payload.len, msg->payload.p, _aqualink_data->aqbuttons[i].name);
|
||||
}
|
||||
else if ( (_aqualink_data->aqbuttons[i].led->state == OFF && status==0) ||
|
||||
(status == 1 && (_aqualink_data->aqbuttons[i].led->state == ON ||
|
||||
|
@ -665,10 +667,10 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||
logMessage(LOG_INFO, "MQTT: received (msg_id: %d), looks like my own message, ignoring\n", mqtt_msg->message_id);
|
||||
}
|
||||
// NSF Need to change strlen to a global so it's not executed every time we check a topic
|
||||
if (strncmp(mqtt_msg->topic.p, _aqualink_config->mqtt_aq_topic, strlen(_aqualink_config->mqtt_aq_topic)) == 0) {
|
||||
if (_aqualink_config->mqtt_aq_topic != NULL && strncmp(mqtt_msg->topic.p, _aqualink_config->mqtt_aq_topic, strlen(_aqualink_config->mqtt_aq_topic)) == 0) {
|
||||
action_mqtt_message(nc, mqtt_msg);
|
||||
}
|
||||
if (strncmp(mqtt_msg->topic.p, _aqualink_config->mqtt_dz_sub_topic, strlen(_aqualink_config->mqtt_dz_sub_topic)) == 0) {
|
||||
if (_aqualink_config->mqtt_dz_sub_topic != NULL && strncmp(mqtt_msg->topic.p, _aqualink_config->mqtt_dz_sub_topic, strlen(_aqualink_config->mqtt_dz_sub_topic)) == 0) {
|
||||
action_domoticz_mqtt_message(nc, mqtt_msg);
|
||||
}
|
||||
break;
|
||||
|
@ -690,7 +692,8 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||
|
||||
void start_mqtt(struct mg_mgr *mgr) {
|
||||
logMessage (LOG_NOTICE, "Starting MQTT client to %s\n", _aqualink_config->mqtt_server);
|
||||
if ( _aqualink_config->mqtt_server == NULL)
|
||||
if ( _aqualink_config->mqtt_server == NULL ||
|
||||
( _aqualink_config->mqtt_aq_topic == NULL && _aqualink_config->mqtt_dz_pub_topic == NULL && _aqualink_config->mqtt_dz_sub_topic == NULL) )
|
||||
return;
|
||||
|
||||
if (mg_connect(mgr, _aqualink_config->mqtt_server, ev_handler) == NULL) {
|
||||
|
|
Binary file not shown.
|
@ -8,13 +8,14 @@ web_directory=/var/www/aqualinkd/
|
|||
# Log to file, comment out if you do not want to log to file
|
||||
#log_file=/var/log/aqualinkd.log
|
||||
|
||||
# The log level. [DEBUG, INFO, NOTICE, WARNING, ERROR]
|
||||
# The log level. [DEBUG_DERIAL, DEBUG, INFO, NOTICE, WARNING, ERROR]
|
||||
# Pick the highest level, and all levels below will be sent to syslog.
|
||||
# your syslog settings may be set to only display messages above a certian level
|
||||
# in which case make sure you use the log_file settings to capture everything
|
||||
# you want when debugging
|
||||
# so, NOTICE also prints WARNING & ERROR
|
||||
# DEBUG would print everything possible
|
||||
# DEBUG_SERIAL would print everything possible
|
||||
|
||||
#log_level=DEBUG
|
||||
log_level=INFO
|
||||
#log_level=NOTICE
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
# aqualinkd.conf
|
||||
#
|
||||
|
||||
# The directory where the web files are stored
|
||||
|
||||
web_directory=/var/www/aqualinkd/
|
||||
|
||||
# Log to file, comment out if you do not want to log to file
|
||||
#log_file=/var/log/aqualinkd.log
|
||||
|
||||
# The log level. [DEBUG, INFO, NOTICE, WARNING, ERROR]
|
||||
# Pick the highest level, and all levels below will be sent to syslog.
|
||||
# your syslog settings may be set to only display messages above a certian level
|
||||
# in which case make sure you use the log_file settings to capture everything
|
||||
# you want when debugging
|
||||
# so, NOTICE also prints WARNING & ERROR
|
||||
# DEBUG would print everything possible
|
||||
log_level=DEBUG_SERIAL
|
||||
#log_level=INFO
|
||||
#log_level=NOTICE
|
||||
|
||||
# The socket port that the daemon listens to
|
||||
# If you change this from 80, remember to update aqualink.service.avahi
|
||||
socket_port=80
|
||||
|
||||
# The serial port the daemon access to read the Aqualink RS8
|
||||
serial_port=/dev/ttyUSB0
|
||||
|
||||
# mqtt stuff
|
||||
#mqtt_address = localhost:1883
|
||||
#mqtt_user = someusername
|
||||
#mqtt_passwd = somepassword
|
||||
#mqtt_dz_pub_topic = domoticz/in
|
||||
#mqtt_dz_sub_topic = domoticz/out
|
||||
#mqtt_aq_topic = aqualinkd
|
||||
|
||||
# The id of the Aqualink terminal device. Devices probed by RS8 master are:
|
||||
# 08-0b, 10-13, 18-1b, 20-23,
|
||||
#
|
||||
device_id=0x0a
|
||||
|
||||
# Light probramming mode. 0=safe mode, but slow.
|
||||
# any number greater is seconds to wait between button presses.
|
||||
# 0.4 seems to be the minimum. (workd for light modes below 10 presses)
|
||||
# 0.6 seems to work about 95% of the time, but above 20 presses can be hit or miss.
|
||||
# 0 will simply wait for the controler to send the response back before sending the next, so is equivelent to about 1.2
|
||||
light_programming_mode=0
|
||||
|
||||
# Domoticz ID's for temps.
|
||||
air_temp_dzidx=13
|
||||
pool_water_temp_dzidx=14
|
||||
spa_water_temp_dzidx=15
|
||||
|
||||
# Labels for standard butons (shown in web UI), and domoticz idx's
|
||||
button_01_label=Filter Pump
|
||||
button_01_dzidx=37
|
||||
|
||||
button_02_label=Spa Mode
|
||||
button_02_dzidx=38
|
||||
|
||||
button_03_label=Cleaner
|
||||
button_03_dzidx=39
|
||||
|
||||
button_04_label=Waterfall
|
||||
button_04_dzidx=40
|
||||
|
||||
button_05_label=Spa Blower
|
||||
button_05_dzidx=41
|
||||
|
||||
button_06_label=Pool Light
|
||||
button_06_dzidx=42
|
||||
|
||||
button_07_label=Spa Light
|
||||
button_07_dzidx=43
|
||||
|
||||
button_08_label=NONE
|
||||
button_08_dzidx=NONE
|
||||
|
||||
button_09_label=NONE
|
||||
button_09_dzidx=NONE
|
||||
|
||||
button_10_label=Pool Heater
|
||||
button_10_dzidx=44
|
||||
|
||||
button_11_label=Spa Heater
|
||||
button_11_dzidx=56
|
||||
|
||||
button_12_label=Solar Heater
|
||||
button_12_dzidx=NONE
|
|
@ -0,0 +1,440 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Shaun Feakes - All rights reserved
|
||||
*
|
||||
* You may use redistribute and/or modify this code under the terms of
|
||||
* the GNU General Public License version 2 as published by the
|
||||
* Free Software Foundation. For the terms of this license,
|
||||
* see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* You are free to use this software under the terms of the GNU General
|
||||
* Public License, 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.
|
||||
*
|
||||
* https://github.com/sfeakes/aqualinkd
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#ifndef _UTILS_C_
|
||||
#define _UTILS_C_
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
//#define MAXCFGLINE 265
|
||||
#define TIMESTAMP_LENGTH 30
|
||||
|
||||
|
||||
static bool _daemonise = false;
|
||||
static bool _log2file = false;
|
||||
static int _log_level = -1;
|
||||
static char *_log_filename = NULL;
|
||||
//static char _log_filename[256];
|
||||
|
||||
void setLoggingPrms(int level , bool deamonized, char* log_file)
|
||||
{
|
||||
_log_level = level;
|
||||
_daemonise = deamonized;
|
||||
|
||||
if (log_file == NULL || strlen(log_file) <= 0) {
|
||||
_log2file = false;
|
||||
} else {
|
||||
_log2file = true;
|
||||
_log_filename = log_file;
|
||||
//strcpy(_log_filename, log_file);
|
||||
}
|
||||
}
|
||||
|
||||
int getLogLevel()
|
||||
{
|
||||
return _log_level;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This function reports the error and
|
||||
* exits back to the shell:
|
||||
*/
|
||||
void displayLastSystemError (const char *on_what)
|
||||
{
|
||||
fputs (strerror (errno), stderr);
|
||||
fputs (": ", stderr);
|
||||
fputs (on_what, stderr);
|
||||
fputc ('\n', stderr);
|
||||
|
||||
if (_daemonise == TRUE)
|
||||
{
|
||||
logMessage (LOG_ERR, "%d : %s", errno, on_what);
|
||||
closelog ();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
From -- syslog.h --
|
||||
#define LOG_EMERG 0 // system is unusable
|
||||
#define LOG_ALERT 1 // action must be taken immediately
|
||||
#define LOG_CRIT 2 // critical conditions
|
||||
#define LOG_ERR 3 // error conditions
|
||||
#define LOG_WARNING 4 // warning conditions
|
||||
#define LOG_NOTICE 5 // normal but significant condition
|
||||
#define LOG_INFO 6 // informational
|
||||
#define LOG_DEBUG 7 // debug-level messages
|
||||
*/
|
||||
|
||||
char *elevel2text(int level)
|
||||
{
|
||||
switch(level) {
|
||||
case LOG_ERR:
|
||||
return "Error:";
|
||||
break;
|
||||
case LOG_WARNING:
|
||||
return "Warning:";
|
||||
break;
|
||||
case LOG_NOTICE:
|
||||
return "Notice:";
|
||||
break;
|
||||
case LOG_INFO:
|
||||
return "Info:";
|
||||
break;
|
||||
case LOG_DEBUG:
|
||||
default:
|
||||
return "Debug:";
|
||||
break;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
int text2elevel(char* level)
|
||||
{
|
||||
if (strcmp(level, "DEBUG_SERIAL") == 0) {
|
||||
return LOG_DEBUG_SERIAL;
|
||||
}
|
||||
if (strcmp(level, "DEBUG") == 0) {
|
||||
return LOG_DEBUG;
|
||||
}
|
||||
else if (strcmp(level, "INFO") == 0) {
|
||||
return LOG_INFO;
|
||||
}
|
||||
else if (strcmp(level, "WARNING") == 0) {
|
||||
return LOG_WARNING;
|
||||
}
|
||||
else if (strcmp(level, "NOTICE") == 0) {
|
||||
return LOG_NOTICE;
|
||||
}
|
||||
else if (strcmp(level, "INFO") == 0) {
|
||||
return LOG_INFO;
|
||||
}
|
||||
|
||||
return LOG_ERR;
|
||||
}
|
||||
|
||||
void timestamp(char* time_string)
|
||||
{
|
||||
time_t now;
|
||||
struct tm *tmptr;
|
||||
|
||||
time(&now);
|
||||
tmptr = localtime(&now);
|
||||
strftime(time_string, TIMESTAMP_LENGTH, "%b-%d-%y %H:%M:%S %p ", tmptr);
|
||||
}
|
||||
|
||||
void trimwhitespace(char *str)
|
||||
{
|
||||
char *end;
|
||||
end = str + strlen(str) - 1;
|
||||
while(end > str && isspace(*end)) end--;
|
||||
|
||||
*(end+1) = 0;
|
||||
}
|
||||
|
||||
char *cleanwhitespace(char *str)
|
||||
{
|
||||
char *end;
|
||||
|
||||
// Trim leading space
|
||||
while(isspace(*str)) str++;
|
||||
|
||||
if(*str == 0) // All spaces?
|
||||
return str;
|
||||
|
||||
// Trim trailing space
|
||||
end = str + strlen(str) - 1;
|
||||
while(end > str && isspace(*end)) end--;
|
||||
|
||||
// Write new null terminator
|
||||
*(end+1) = 0;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
char *cleanquotes(char *str)
|
||||
{
|
||||
char *end;
|
||||
// Trim leading whitespace
|
||||
//while(isspace(*str)) str++;
|
||||
//if(*str == 0) // All spaces?
|
||||
// return str;
|
||||
syslog(LOG_INFO, "String to clean %s\n", str);
|
||||
|
||||
while(*str=='"' || *str== '\'' || *str==' ') str++;
|
||||
if(*str == 0) // All spaces
|
||||
return str;
|
||||
|
||||
end = str + strlen(str) - 1;
|
||||
while(end > str && (*end=='"' || *end== '\'' || *end==' ')) end--;
|
||||
|
||||
// Write new null terminator
|
||||
*(end+1) = 0;
|
||||
|
||||
syslog(LOG_INFO, "String cleaned %s\n", str);
|
||||
|
||||
return str;
|
||||
}
|
||||
*/
|
||||
int cleanint(char*str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
|
||||
str = cleanwhitespace(str);
|
||||
return atoi(str);
|
||||
}
|
||||
|
||||
void test(int msg_level, char *msg)
|
||||
{
|
||||
char buffer[256];
|
||||
|
||||
sprintf(buffer,"Level %d | MsgLvl %d | Dmn %d | LF %d | %s - %s",_log_level,msg_level,_daemonise,_log2file,_log_filename,msg);
|
||||
if ( buffer[strlen(buffer)-1] != '\n') {
|
||||
strcat(buffer, "\n");
|
||||
}
|
||||
|
||||
int fp = open("/var/log/aqualink.log", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
if (fp != -1) {
|
||||
write(fp, buffer, strlen(buffer) );
|
||||
close(fp);
|
||||
} else {
|
||||
syslog(LOG_ERR, "Can't open file /var/log/aqualink.log");
|
||||
}
|
||||
}
|
||||
|
||||
void logMessage(int msg_level, char *format, ...)
|
||||
{
|
||||
char buffer[512];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
strncpy(buffer, " ", 8);
|
||||
vsprintf (&buffer[8], format, args);
|
||||
va_end(args);
|
||||
|
||||
//test(msg_level, buffer);
|
||||
//fprintf (stderr, buffer);
|
||||
|
||||
if (_log_level == -1) {
|
||||
fprintf (stderr, buffer);
|
||||
syslog (msg_level, "%s", &buffer[8]);
|
||||
closelog ();
|
||||
} else if (msg_level > _log_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_daemonise == TRUE)
|
||||
{
|
||||
syslog (msg_level, "%s", &buffer[8]);
|
||||
closelog ();
|
||||
//return;
|
||||
}
|
||||
|
||||
if (_log2file == TRUE && _log_filename != NULL) {
|
||||
int len;
|
||||
char *strLevel = elevel2text(msg_level);
|
||||
|
||||
strncpy(buffer, strLevel, strlen(strLevel));
|
||||
|
||||
len = strlen(buffer);
|
||||
|
||||
//printf( " '%s' last chrs '%d''%d'\n", buffer, buffer[len-1],buffer[len]);
|
||||
|
||||
if ( buffer[len-1] != '\n') {
|
||||
strcat(buffer, "\n");
|
||||
}
|
||||
|
||||
char time[TIMESTAMP_LENGTH];
|
||||
int fp = open(_log_filename, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
if (fp != -1) {
|
||||
timestamp(time);
|
||||
write(fp, time, strlen(time) );
|
||||
write(fp, buffer, strlen(buffer) );
|
||||
close(fp);
|
||||
} else {
|
||||
if (_daemonise == TRUE)
|
||||
syslog(LOG_ERR, "Can't open log file\n %s", buffer);
|
||||
else
|
||||
fprintf (stderr, "Can't open debug log\n %s", buffer);
|
||||
}
|
||||
}
|
||||
|
||||
if (_daemonise == FALSE) {
|
||||
if (msg_level == LOG_ERR) {
|
||||
fprintf(stderr, "%s", buffer);
|
||||
} else {
|
||||
printf("%s", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void daemonise (char *pidFile, void (*main_function) (void))
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
pid_t process_id = 0;
|
||||
pid_t sid = 0;
|
||||
|
||||
_daemonise = true;
|
||||
|
||||
/* Check we are root */
|
||||
if (getuid() != 0)
|
||||
{
|
||||
logMessage(LOG_ERR,"Can only be run as root\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int pid_file = open (pidFile, O_CREAT | O_RDWR, 0666);
|
||||
int rc = flock (pid_file, LOCK_EX | LOCK_NB);
|
||||
if (rc)
|
||||
{
|
||||
if (EWOULDBLOCK == errno)
|
||||
; // another instance is running
|
||||
//fputs ("\nAnother instance is already running\n", stderr);
|
||||
logMessage(LOG_ERR,"\nAnother instance is already running\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
process_id = fork ();
|
||||
// Indication of fork() failure
|
||||
if (process_id < 0)
|
||||
{
|
||||
displayLastSystemError ("fork failed!");
|
||||
// Return failure in exit status
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// PARENT PROCESS. Need to kill it.
|
||||
if (process_id > 0)
|
||||
{
|
||||
fp = fopen (pidFile, "w");
|
||||
|
||||
if (fp == NULL)
|
||||
logMessage(LOG_ERR,"can't write to PID file %s",pidFile);
|
||||
else
|
||||
fprintf(fp, "%d", process_id);
|
||||
|
||||
fclose (fp);
|
||||
logMessage (LOG_DEBUG, "process_id of child process %d \n", process_id);
|
||||
// return success in exit status
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
//unmask the file mode
|
||||
umask (0);
|
||||
//set new session
|
||||
sid = setsid ();
|
||||
if (sid < 0)
|
||||
{
|
||||
// Return failure
|
||||
displayLastSystemError("Failed to fork process");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// Change the current working directory to root.
|
||||
chdir ("/");
|
||||
// Close stdin. stdout and stderr
|
||||
close (STDIN_FILENO);
|
||||
close (STDOUT_FILENO);
|
||||
close (STDERR_FILENO);
|
||||
|
||||
// this is the first instance
|
||||
(*main_function) ();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int count_characters(const char *str, char character)
|
||||
{
|
||||
const char *p = str;
|
||||
int count = 0;
|
||||
|
||||
do {
|
||||
if (*p == character)
|
||||
count++;
|
||||
} while (*(p++));
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
bool text2bool(char *str)
|
||||
{
|
||||
str = cleanwhitespace(str);
|
||||
if (strcasecmp (str, "YES") == 0 || strcasecmp (str, "ON") == 0)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
char *bool2text(bool val)
|
||||
{
|
||||
if(val == TRUE)
|
||||
return "YES";
|
||||
else
|
||||
return "NO";
|
||||
}
|
||||
|
||||
// (50°F - 32) x .5556 = 10°C
|
||||
float degFtoC(float degF)
|
||||
{
|
||||
return ((degF-32) / 1.8);
|
||||
}
|
||||
// 30°C x 1.8 + 32 = 86°F
|
||||
float degCtoF(float degC)
|
||||
{
|
||||
return (degC * 1.8 + 32);
|
||||
}
|
||||
|
||||
#include <time.h>
|
||||
|
||||
void delay (unsigned int howLong) // Microseconds (1000000 = 1 second)
|
||||
{
|
||||
struct timespec sleeper, dummy ;
|
||||
|
||||
sleeper.tv_sec = (time_t)(howLong / 1000) ;
|
||||
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
|
||||
|
||||
nanosleep (&sleeper, &dummy) ;
|
||||
}
|
||||
|
||||
char* stristr(const char* haystack, const char* needle) {
|
||||
do {
|
||||
const char* h = haystack;
|
||||
const char* n = needle;
|
||||
while (tolower((unsigned char) *h) == tolower((unsigned char ) *n) && *n) {
|
||||
h++;
|
||||
n++;
|
||||
}
|
||||
if (*n == 0) {
|
||||
return (char *) haystack;
|
||||
}
|
||||
} while (*haystack++);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue