aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2012-01-05 02:11:13 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2012-01-05 02:11:13 +0000
commit608d693c89f76e242e5491bfa6317d9774637e9f (patch)
treeb8a6d912de95eb83ec2881b47928d356f2c7d3e0 /src
parent9bccaac7efbf7221704154149e5df45e7567c831 (diff)
downloadverteco-608d693c89f76e242e5491bfa6317d9774637e9f.tar.gz
Add comments
Diffstat (limited to 'src')
-rw-r--r--src/modbuslog.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/modbuslog.c b/src/modbuslog.c
index be7a4d1..9424936 100644
--- a/src/modbuslog.c
+++ b/src/modbuslog.c
@@ -42,10 +42,13 @@
#define START_ADDRESS 0
#define NUMBER_REGISTERS 14
+
+// handle SIGALRM by resetting it
void minute_check(int signum) {
alarm(60);
}
+// get mac address of primary interface eth0
char *mac_address() {
int s;
struct ifreq ifr;
@@ -74,6 +77,8 @@ int main(int argc, char *argv[]) {
int DEBUG = 0;
int k;
+
+ // check the argv array for strings matching -d
for (k = 1; k < argc; k++) {
if (strcmp(argv[k], "-d") == 0) {
DEBUG = 1;
@@ -92,6 +97,8 @@ int main(int argc, char *argv[]) {
config_init(&cfg);
+ // attempt to read config
+ // loads entire file to memory and destroys file descriptor
if (!config_read_file(&cfg, configfile)) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
@@ -100,6 +107,7 @@ int main(int argc, char *argv[]) {
return -1;
}
+ // die if core config file options aren't there
if (!(config_lookup_string(&cfg, "modbus.device", &modbus_device_address)
&& config_lookup_int(&cfg, "modbus.baud", &modbus_baud_rate)
&& config_lookup_int(&cfg, "modbus.data_bits", &modbus_data_bits)
@@ -123,10 +131,14 @@ int main(int argc, char *argv[]) {
int rc;
int i;
+ // SIGALRM used to wake for loop up every 60 secs
+ // sleep puts whole thread to sleep which isn't what we want
+ // other methods involve CPU spinlocks which are inefficient
signal(SIGALRM, minute_check);
alarm(60);
for (;;) {
+ // block until SIGARLM
select(0, NULL, NULL, NULL, NULL);
time_t t = time(NULL);
@@ -138,9 +150,12 @@ int main(int argc, char *argv[]) {
readings = config_lookup(&cfg, "reading");
+ // find number of required readings
unsigned int num_readings = config_setting_length(readings);
int i;
+
+ // cycle through each reading and pull info from config file
for (i = 0; i < num_readings; ++i) {
config_setting_t *register_element = config_setting_get_elem(
readings, i);
@@ -176,10 +191,12 @@ int main(int argc, char *argv[]) {
else
intervalduration = intervalvalue * 60;
+ // if we match the required time for the reading
if (unixtime_min % intervalduration == 0) {
//printf("specified minute. %d %d\n", unixtime_min,
// intervalduration);
+ // attempt to create new modbus connection
ctx = modbus_new_rtu(modbus_device_address, modbus_baud_rate,
modbus_parity[0], modbus_data_bits, modbus_stop_bits);
@@ -199,6 +216,8 @@ int main(int argc, char *argv[]) {
}
int retry = 0;
+
+ // handle timeouts and retries
do {
switch (registertype) {
@@ -227,13 +246,14 @@ int main(int argc, char *argv[]) {
//MODBUS_GET_LOW_BYTE(data);
//MODBUS_SET_INT16_TO_INT8(tab_int8, index, value);
- //<mac address>_YYYY_MM_DD_HH_MM_SS.log
+ // round forward to next midnight
time_t unixtime_day = ((((int) t) / 86400 * 86400) + 86400);
struct tm midnight = *localtime(&unixtime_day);
char filename[50];
+ //<mac address>_YYYY_MM_DD_HH_MM_SS.log
sprintf(filename,
"/var/modbuslog/%s_%04i_%02i_%02i_%02i_%02i_%02i.log",
mac_address(), midnight.tm_year + 1900,
@@ -259,11 +279,14 @@ int main(int argc, char *argv[]) {
int16_t registervalue = 0;
int p;
+
+ // data arrives in words, split into bytes
for (p = 0; p < numregisters; p++) {
MODBUS_SET_INT16_TO_INT8(byte, p * 2, tab_reg[p]);
//registervalue += tab_reg[i] + tab_reg[i + 1];
}
+ // bitshifting magic
switch (numregisters) {
case 1:
registervalue = byte[0];
cCrohan1-2/+2 2015-04-01Describe changes made in snapshotJonathan McCrohan1-0/+3 2015-04-01New Upstream Snapshot (commit f2053b3)Jonathan McCrohan1-0/+6 2015-04-01Imported Upstream version 0+git20150208.f2053b3upstream/0+git20150208.f2053b3Jonathan McCrohan32-333/+483 2014-12-28Release 0+git20141218.b46a22c-1debian/0+git20141218.b46a22c-1Jonathan McCrohan1-2/+2 2014-12-28Updated AU DVB-T files; Thanks Brian Burch! (Closes LP: #1393280)Jonathan McCrohan1-0/+1 2014-12-28New Upstream Snapshot (commit b46a22c)Jonathan McCrohan1-0/+6 2014-12-28Imported Upstream version 0+git20141218.b46a22cupstream/0+git20141218.b46a22cJonathan McCrohan535-639/+317 2014-10-20Release 0+git20141009.d26b627-1debian/0+git20141009.d26b627-1Jonathan McCrohan1-2/+2 2014-10-20Fix up packaging to account for move to DVBv5Jonathan McCrohan2-2/+6 2014-10-20delete d/dtv-scan-files.install; Upstream now supplies a MakefileJonathan McCrohan2-4/+1 2014-10-15d/control: update Standards Version to 3.9.6Jonathan McCrohan2-1/+3 2014-10-15d/control: add Build-Depends on dvb-toolsJonathan McCrohan2-1/+2 2014-10-15New Upstream Snapshot (commit d26b627)Jonathan McCrohan1-0/+10 2014-10-15Imported Upstream version 0+git20141009.d26b627upstream/0+git20141009.d26b627Jonathan McCrohan2010-11120/+159271 2014-07-23Release 0+git20140611.14bd6c7-1debian/0+git20140611.14bd6c7-1Jonathan McCrohan1-2/+2 2014-07-23New Upstream Snapshot (commit 14bd6c7)Jonathan McCrohan1-2/+3 2014-07-23Imported Upstream version 0+git20140611.14bd6c7upstream/0+git20140611.14bd6c7Jonathan McCrohan7-11/+60 2014-05-13New Upstream Snapshot (commit 1246b27)Jonathan McCrohan1-0/+6 2014-05-13Imported Upstream version 0+git20140512.1246b27upstream/0+git20140512.1246b27Jonathan McCrohan391-301/+3983 2014-04-05Release 0+git20140326.cfc2975-1debian/0+git20140326.cfc2975-1Jonathan McCrohan1-2/+2 2014-04-05d/control: update Homepage (upstream has moved from Gitweb to cgit)Jonathan McCrohan2-1/+2 2014-04-05New Upstream Snapshot (commit cfc2975)Jonathan McCrohan1-0/+6 2014-04-05Imported Upstream version 0+git20140326.cfc2975upstream/0+git20140326.cfc2975Jonathan McCrohan118-656/+877 2014-01-16Release 0+git20140107.1850cf8-1debian/0+git20140107.1850cf8-1Jonathan McCrohan1-2/+2 2014-01-16Update Standards Version to 3.9.5Jonathan McCrohan2-1/+9