Skip to content

Commit

Permalink
inverter.cpp: avoid mess with simultaneous read/write operations
Browse files Browse the repository at this point in the history
ned-kelly#77

When many inverter_poller at the same time send commands, the result is undefined.

This locks exclusively the configuration file, while the process is running,
so that simultaneous instances have to wait until the file is released.

For some reason locking the device /dev/hidraw0 does not work for me,
I get sometimes the output from previous runs.  I suspect that when
cInverter::query() does exceed the 2s timeout, it quits, and leaves the
response of its commands on the wire.  For fd = /dev/hidraw0
    tcflush(fd, TCOFLUSH);
has no effect.

As a matter of fact, on my system I have increased the timeout in
cInverter::query() to 15s and now it does always work correctly.

Also the program, when called with -1, could exit, while the thread
is receiving data, this leaves the next invocation with some
ready-data, which it is not expecting.
  • Loading branch information
dilyanpalauzov committed Apr 9, 2022
1 parent d75ab47 commit b0d4b26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 1 addition & 2 deletions sources/inverter-cli/inverter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <syslog.h>
#include <unistd.h>
#include "inverter.h"
Expand Down Expand Up @@ -199,7 +198,7 @@ void cInverter::poll() {
ups_qpiws_changed = true;
}
}

if (quit_thread) return;
sleep(5);
}
}
Expand Down
10 changes: 8 additions & 2 deletions sources/inverter-cli/inverter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ___INVERTER_H
#define ___INVERTER_H

#include <atomic>
#include <thread>
#include <mutex>

Expand All @@ -16,6 +17,8 @@ class cInverter {

std::string device;
std::mutex m;
std::thread t1;
std::atomic_bool quit_thread{false};

void SetMode(char newmode);
bool CheckCRC(unsigned char *buff, int len);
Expand All @@ -26,8 +29,11 @@ class cInverter {
cInverter(std::string devicename, int qpiri, int qpiws, int qmod, int qpigs);
void poll();
void runMultiThread() {
std::thread t1(&cInverter::poll, this);
t1.detach();
t1 = std::thread(&cInverter::poll, this);
}
void terminateThread() {
quit_thread = true;
t1.join();
}

string *GetQpiriStatus();
Expand Down
14 changes: 11 additions & 3 deletions sources/inverter-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <sys/file.h>

#include "main.h"
#include "tools.h"
Expand Down Expand Up @@ -165,13 +166,17 @@ int main(int argc, char* argv[]) {
runOnce = true;
}
lprintf("INVERTER: Debug set");
const char *settings;

// Get the rest of the settings from the conf file
if( access( "./inverter.conf", F_OK ) != -1 ) { // file exists
getSettingsFile("./inverter.conf");
settings = "./inverter.conf";
} else { // file doesn't exist
getSettingsFile("/etc/inverter/inverter.conf");
settings = "/etc/inverter/inverter.conf";
}
getSettingsFile(settings);
int fd = open(settings, O_RDWR);
while (flock(fd, LOCK_EX)) sleep(1);

bool ups_status_changed(false);
ups = new cInverter(devicename,qpiri,qpiws,qmod,qpigs);
Expand Down Expand Up @@ -328,6 +333,7 @@ int main(int argc, char* argv[]) {
delete reply2;

if(runOnce) {
ups->terminateThread();
// Do once and exit instead of loop endlessly
lprintf("INVERTER: All queries complete, exiting loop.");
exit(0);
Expand All @@ -338,7 +344,9 @@ int main(int argc, char* argv[]) {
sleep(1);
}

if (ups)
if (ups) {
ups->terminateThread();
delete ups;
}
return 0;
}

0 comments on commit b0d4b26

Please sign in to comment.