-
Notifications
You must be signed in to change notification settings - Fork 197
/
AdsFile.cpp
150 lines (133 loc) · 4.58 KB
/
AdsFile.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: MIT
/**
Copyright (c) 2020 - 2022 Beckhoff Automation GmbH & Co. KG
*/
#include "AdsFile.h"
#include <iostream>
#include <list>
#pragma pack( push, 1)
/**
* @brief This structure describes file information reveived via ADS
*
* Calling ReadWriteReqEx2 with IndexGroup == SYSTEMSERVICE_FFILEFIND
* will return ADS file information in the provided readData buffer.
* The data of that information is structured as TcFileFindData.
*/
struct TcFileFindData {
uint32_t hFile;
uint32_t dwFileAttributes;
uint64_t nReserved1[5];
char cFileName[260];
char unused[14];
uint16_t nReserved2;
bool isDirectory(void) const
{
return 0x10 & dwFileAttributes;
}
void letoh(void)
{
hFile = bhf::ads::letoh(hFile);
dwFileAttributes = bhf::ads::letoh(dwFileAttributes);
}
};
#pragma pack( pop )
static bool FindNext(const AdsDevice& route,
TcFileFindData& child,
const size_t length = 0,
const char* const path = nullptr)
{
const auto error = route.ReadWriteReqEx2(
SYSTEMSERVICE_FFILEFIND,
child.hFile,
sizeof(child),
&child,
length,
path,
nullptr);
// We reached the last child
// If there is no more file in the current path the ADS service will
// return ads error code 1804 so we can break and exit as expected.
if (error && (error != 1804)) {
throw AdsException(error);
}
// TwinCAT sends data in little endian so we have to convert it here
child.letoh();
return error == 1804;
}
static bool FindFirst(const AdsDevice& route, TcFileFindData& item, const std::string& path)
{
enum FFILEFIND : uint32_t {
GENERIC = 1 << 0,
};
item.hFile = FFILEFIND::GENERIC;
return FindNext(route, item, path.length(), path.c_str());
}
AdsFile::AdsFile(const AdsDevice& route, const std::string& filename, const uint32_t flags)
: m_Route(route),
m_Handle(route.OpenFile(filename, flags))
{}
void AdsFile::Delete(const AdsDevice& route, const std::string& filename, const uint32_t flags)
{
auto error = route.ReadWriteReqEx2(SYSTEMSERVICE_FDELETE,
flags,
0,
nullptr,
filename.length(),
filename.c_str(),
nullptr);
if (error) {
throw AdsException(error);
}
}
int AdsFile::Find(const AdsDevice& route, const std::string& basePath, const size_t maxdepth, std::ostream& os)
{
struct Path {
size_t depth;
std::string path;
};
std::list<struct Path> pendingDirs{{ 0, basePath} };
while (!pendingDirs.empty()) {
auto path = pendingDirs.front().path;
auto depth = pendingDirs.front().depth;
pendingDirs.pop_front();
TcFileFindData parent;
if (FindFirst(route, parent, path)) {
return 1804;
}
// Path exists print it and prepare traversing
os << path << '\n';
if (parent.isDirectory() && (depth < maxdepth)) {
// Finding files in a directory is a bit weird. We get only one entry per call and for
// every call we pass the last found item to get the next. The first item is special.
// To get the children of a directory we have to append '/*'to the path of the directory.
for (auto last = FindFirst(route, parent, path + "/*"); !last; last = FindNext(route, parent)) {
if (parent.isDirectory()) {
pendingDirs.push_back({depth + 1, path + '/' + parent.cFileName});
} else {
os << path << '/' << parent.cFileName << '\n';
}
}
}
}
return 0;
}
void AdsFile::Read(const size_t size, void* data, uint32_t& bytesRead) const
{
auto error = m_Route.ReadWriteReqEx2(SYSTEMSERVICE_FREAD,
*m_Handle,
size,
data,
0,
nullptr,
&bytesRead);
if (error) {
throw AdsException(error);
}
}
void AdsFile::Write(const size_t size, const void* data) const
{
auto error = m_Route.ReadWriteReqEx2(SYSTEMSERVICE_FWRITE, *m_Handle, 0, nullptr, size, data, nullptr);
if (error) {
throw AdsException(error);
}
}