Update to messaging and checksum

pull/46/head
shaun feakes 2018-03-16 12:18:15 -05:00
parent 891c915af6
commit 21ed0435b9
10 changed files with 54 additions and 459 deletions

View File

@ -889,7 +889,7 @@ bool waitForMessage(struct aqualinkdata *aq_data, char* message, int numMessageR
//logMessage(LOG_ERR, "Could not select MENU of Aqualink control panel\n");
logMessage(LOG_DEBUG, "Programming mode: did not find '%s'\n",message);
return false;
}
} else if (message != NULL)
logMessage(LOG_DEBUG, "Programming mode: found message '%s' in '%s'\n",message,aq_data->last_message);
return true;

View File

@ -238,9 +238,6 @@ void send_ack(int fd, unsigned char command)
}
// Reads the bytes of the next incoming packet, and
// returns when a good packet is available in packet
// fd: the file descriptor to read the bytes from
@ -335,6 +332,14 @@ int get_packet(int fd, unsigned char* packet)
}
}
if (generate_checksum(packet, index) != packet[index-3]){
logMessage(LOG_WARNING, "Serial read bad checksum, ignoring\n");
return 0;
} else if (index < AQ_MINPKTLEN) {
logMessage(LOG_WARNING, "Serial read too small, (but good checksum), need to understand this better\n");
return 0;
}
logMessage(LOG_DEBUG_SERIAL, "Serial read %d bytes\n",index);
// Return the packet length.
return index;

View File

@ -66,8 +66,8 @@ struct aqualinkdata
char date[AQ_MSGLEN];
char time[AQ_MSGLEN];
//char datestr[DATE_STRING_LEN];
char message[AQ_MSGLONGLEN]; // NSF just temp for PDA crap
char *last_message; // Be careful using this, can get core dumps.
char last_message[AQ_MSGLONGLEN+1]; // NSF just temp for PDA crap
//char *last_message; // Be careful using this, can get core dumps.
//char *display_message;
unsigned char raw_status[AQ_PSTLEN];
aqled aqualinkleds[TOTAL_LEDS];

View File

@ -269,8 +269,9 @@ void processMessage(char *message)
static bool _initWithRS = false;
static bool _gotREV = false;
// NSF replace message with msg
msg = cleanwhitespace(message);
_aqualink_data.last_message = msg;
msg = stripwhitespace(message);
strcpy(_aqualink_data.last_message, msg);
//_aqualink_data.last_message = _aqualink_data.message;
//_aqualink_data.display_message = NULL;
//aqualink_strcpy(_aqualink_data.message, msg);
@ -361,7 +362,7 @@ void processMessage(char *message)
aq_programmer(AQ_SEND_CMD, (char *)KEY_ENTER, &_aqualink_data);
}
else {
logMessage(LOG_DEBUG, "Ignoring '%s'\n",msg);
logMessage(LOG_DEBUG_SERIAL, "Ignoring '%s'\n",msg);
//_aqualink_data.display_message = msg;
}
@ -371,15 +372,16 @@ void processMessage(char *message)
void processPDAMessage(char *message)
{
static bool nextMessageTemp = false;
char *msg;
msg = cleanwhitespace(message);
_aqualink_data.last_message = msg;
msg = stripwhitespace(message);
strcpy(_aqualink_data.last_message, msg);
logMessage(LOG_INFO, "RS PDA Message :- '%s'\n",msg);
if (_aqualink_data.message != NULL && strncasecmp(_aqualink_data.message, "AIR", 3) == 0)
{
if (nextMessageTemp == true) {
nextMessageTemp = false;
_aqualink_data.temp_units = FAHRENHEIT;
// RS PDA Message :- '73` 66`'
_aqualink_data.air_temp = atoi(msg);
@ -388,12 +390,16 @@ void processPDAMessage(char *message)
//aq_programmer(AQ_SEND_CMD, (char *)KEY_ENTER, &_aqualink_data);
aq_programmer(AQ_PDA_INIT, NULL, &_aqualink_data);
} else if(strstr(msg, "REV ") != NULL) { // 'REV MMM'
aq_programmer(AQ_PDA_INIT, NULL, &_aqualink_data);
}
else if (strncasecmp(_aqualink_data.last_message, "AIR", 3) == 0) {
nextMessageTemp = true;
}
else if(strstr(msg, "REV ") != NULL) { // 'REV MMM'
//aq_programmer(AQ_PDA_INIT, NULL, &_aqualink_data);
}
//_aqualink_data.last_message = msg;
strncpy(_aqualink_data.message, msg,AQ_MSGLONGLEN);
strncpy(_aqualink_data.last_message, msg,AQ_MSGLONGLEN);
// We processed the next message, kick any threads waiting on the message.
kick_aq_program_thread(&_aqualink_data);
@ -410,7 +416,7 @@ bool process_packet(unsigned char* packet, int length)
// Check packet against last check if different.
if (memcmp(packet, last_packet, length) == 0) {
logMessage(LOG_DEBUG, "RS Received duplicate, ignoring.\n",length);
logMessage(LOG_DEBUG_SERIAL, "RS Received duplicate, ignoring.\n",length);
return rtn;
} else {
memcpy(last_packet, packet, length);

Binary file not shown.

Binary file not shown.

View File

@ -10,7 +10,7 @@
#include "aq_serial.h"
#include "utils.h"
#define SLOG_MAX 40
#define SLOG_MAX 80
#define PACKET_MAX 1000
typedef struct serial_id_log {

440
utils.1.c
View File

@ -1,440 +0,0 @@
/*
* 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;
}

23
utils.c
View File

@ -161,6 +161,7 @@ void trimwhitespace(char *str)
*(end+1) = 0;
}
//Move existing pointer
char *cleanwhitespace(char *str)
{
char *end;
@ -181,6 +182,28 @@ char *cleanwhitespace(char *str)
return str;
}
// Return new pointer
char *stripwhitespace(char *str)
{
char *end;
char *start = str;
// Trim leading space
while(isspace(*start)) start++;
if(*start == 0) // All spaces?
return start;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return start;
}
/*
char *cleanquotes(char *str)

View File

@ -40,6 +40,7 @@ char *elevel2text(int level);
char *cleanwhitespace(char *str);
//char *cleanquotes(char *str);
void trimwhitespace(char *str);
char *stripwhitespace(char *str);
int cleanint(char*str);
bool text2bool(char *str);
char *bool2text(bool val);