Skip to content

Commit 64f12c3

Browse files
committed
Switch to device locking in aspCommon.cpp (#14). Also, add more I2C stuff to aspSUB20.py.
1 parent ff3e61e commit 64f12c3

File tree

5 files changed

+482
-413
lines changed

5 files changed

+482
-413
lines changed

arx_control/aspCommon.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <thread>
44
#include <chrono>
55
#include <filesystem>
6+
#include <fcntl.h>
67
#include <sys/stat.h>
78

89
#include "aspCommon.hpp"
@@ -51,6 +52,8 @@ bool ATmega::open() {
5152
bool found = false;
5253
atmega::handle fd = -1;
5354
if( _sn.find("/dev") == 0 ) {
55+
std::cerr << "Warning: Running without device access locking" << std::endl;
56+
5457
struct stat sb;
5558
if( stat(_sn.c_str(), &sb) == -1 || !S_ISCHR(sb.st_mode) ) {
5659
return false;
@@ -87,6 +90,13 @@ bool ATmega::open() {
8790

8891
return found;
8992
} else {
93+
_lock = sem_open(_sn.c_str(), O_CREAT);
94+
if( _lock == SEM_FAILED ) {
95+
_lock = NULL;
96+
return false;
97+
}
98+
sem_wait(_lock);
99+
90100
for(std::string const& dev_name: atmega::find_devices()) {
91101
int open_attempts = 0;
92102
while( open_attempts < ATMEGA_OPEN_MAX_ATTEMPTS ) {
@@ -110,18 +120,18 @@ bool ATmega::open() {
110120

111121
int n = atmega::send_command(fd, &cmd, &resp, ATMEGA_OPEN_MAX_ATTEMPTS, ATMEGA_OPEN_WAIT_MS);
112122
if( (n > 0) && (resp.command & atmega::COMMAND_FAILURE) == 0 ) {
113-
std::string sn;
114-
for(int i=0; i<resp.size; i++) {
115-
sn.push_back((char) resp.buffer[i]);
116-
}
117-
118-
if( _sn.compare(sn) == 0 ) {
119-
found = true;
120-
_fd = fd;
121-
break;
122-
} else {
123-
_fd = -1;
124-
}
123+
std::string sn;
124+
for(int i=0; i<resp.size; i++) {
125+
sn.push_back((char) resp.buffer[i]);
126+
}
127+
128+
if( _sn.compare(sn) == 0 ) {
129+
found = true;
130+
_fd = fd;
131+
break;
132+
} else {
133+
_fd = -1;
134+
}
125135
}
126136
} catch(const std::exception& e) {}
127137

@@ -131,6 +141,12 @@ bool ATmega::open() {
131141
atmega::close(fd);
132142
}
133143
}
144+
145+
if( !found ) {
146+
sem_post(_lock);
147+
sem_close(_lock);
148+
_lock = NULL;
149+
}
134150
}
135151

136152
return found;

arx_control/aspCommon.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstring>
1515
#include <cstdint>
1616
#include <stdexcept>
17+
#include <semaphore.h>
1718

1819
#include "libatmega.hpp"
1920

@@ -46,6 +47,7 @@
4647
// Uncomment the next line to use input rather than the module outuput current
4748
//#define __USE_INPUT_CURRENT__
4849

50+
4951
// Get a list of all ATmega serial numbers
5052
std::list<std::string> list_atmegas();
5153

@@ -55,13 +57,18 @@ class ATmega {
5557
private:
5658
std::string _sn;
5759
atmega::handle _fd;
60+
sem_t* _lock;
5861

5962
public:
60-
ATmega(std::string sn): _sn(""), _fd(-1) {
63+
ATmega(std::string sn): _sn(""), _fd(-1), _lock(NULL) {
6164
_sn = sn;
6265
}
6366
~ATmega() {
6467
atmega::close(_fd);
68+
if( _lock != NULL ) {
69+
sem_post(_lock);
70+
sem_close(_lock);
71+
}
6572
}
6673
bool open();
6774
std::string get_version();

arx_control/libatmega/libatmega.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,5 @@ void atmega::close(atmega::handle fd) {
370370
if( fd >= 0 ) {
371371
::tcdrain(fd);
372372
::close(fd);
373-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
374373
}
375374
}

0 commit comments

Comments
 (0)