-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io.cpp
60 lines (50 loc) · 1.38 KB
/
file_io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "file_io.h"
#include <array>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <string>
#include <sys/types.h>
#include <unistd.h>
namespace dynamixel::file_io {
auto read(int _fd, size_t maxReadBytes) -> std::vector<std::byte> {
std::vector<std::byte> rxBuf(maxReadBytes);
size_t bytesRead = 0;
do {
ssize_t r = ::read(_fd, rxBuf.data() + bytesRead, rxBuf.size() - bytesRead);
if (r == -1) {
if (errno == EAGAIN) {
break;
}
throw std::runtime_error(std::string{"unexpected read error: "} + strerror(errno) + " (" + std::to_string(errno) + ")");
} else if (r == 0) {
break;
}
bytesRead += r;
} while (bytesRead < maxReadBytes);
rxBuf.resize(bytesRead);
return rxBuf;
}
size_t flushRead(int _fd) {
size_t bytesRead {0};
std::array<uint8_t, 4096> dummy;
ssize_t r {1};
while (r != -1 and r != 0) {
bytesRead += r;
r = ::read(_fd, dummy.data(), dummy.size()); // read flush
}
return bytesRead;
}
void write(int _fd, std::vector<std::byte> const& txBuf) {
uint32_t bytesWritten = 0;
const size_t count = txBuf.size();
do {
ssize_t w = ::write(_fd, txBuf.data() + bytesWritten, count - bytesWritten);
if (w == -1) {
throw std::runtime_error(std::string{"write to the dyanmixel bus failed: "} + strerror(errno) + " (" + std::to_string(errno) + ")");
}
bytesWritten += w;
} while (bytesWritten < count);
}
}