-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathScaPythonInterface.h
More file actions
270 lines (213 loc) · 6.99 KB
/
ScaPythonInterface.h
File metadata and controls
270 lines (213 loc) · 6.99 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file ScaPythonInterface.cxx
/// \brief Python SCA interface for the Slow Control library
///
/// \author Kostas Alexopoulos ([email protected])
#ifndef O2_ALF_SRC_SCAPYTHONINTERFACE_H
#define O2_ALF_SRC_SCAPYTHONINTERFACE_H
#include <boost/python.hpp>
#include "Alf/Sca.h"
#include "PythonInterface.h"
namespace o2
{
namespace alf
{
namespace python
{
namespace bp = boost::python;
typedef boost::variant<std::string, std::pair<std::string, uint32_t>, std::pair<std::string, std::pair<uint32_t, uint32_t>>> ScaArgsVariant;
/// Exposed python function docs
auto sScaInitDoc =
R"(Initializes an Swt object
Args:
card id: String containing PCI address (e.g. 42:0.0), serial-endpoint pair (e.g. 10241:1), sequence number (e.g. #2)
channel: Channel number to initially set)";
auto sScaSetChannelDoc =
R"(Sets the Slow Control channel
Args:
channel: Channel number to set)";
auto sScaScResetDoc =
R"(Resets Slow Control)";
auto sScaSvlResetDoc =
R"(Performs SVL reset)";
auto sScaSvlConnectDoc =
R"(Performs SVL connect)";
auto sScaExecuteCommandDoc =
R"(Execute an SCA command
Args:
command: SCA command (unsigned 32-bit int) to execute
data: SCA data (unsigned 32-bit int))";
auto sScaSequenceDoc =
R"(Execute an SCA sequence:
Args:
sequence: A list of tuples made up of:
operation: An operation(string) to perform
input: Input((unsigned 32-bit int, unsigned 32-bit int), int) to said operation
Operations and input types:
operation: "sc_reset"
input: no-input
operation: "svl_reset"
input: no-input
operation: "svl_connect"
input: no-input
operation: "wait"
input: wait_time (optional int)
operation: "command"
input: SCA command, data tuple (unsigned 32-bit int, unsigned 32-bit int)
lock: boolean to execute the sequence within an LLA session
lockTimeout: maximum wait time to aquire the LLA session lock in ms
Returns:
sequence: A list of tuples made up of:
operation: The operation carried out (string, same as input)
output: Output (string, (unsigned 32-bit int, unsigned 32-bit int), int) of said operation)
Output per operation:
operation: "sc_reset"
output: "" (string)
operation: "svl_reset"
output: "" (string)
operation: "svl_connect"
output: "" (string)
operation: "wait"
output: wait_time (int)
operation: "command"
output: a tuple of command and output (unsigned 32-bit int, unsigned 32-bit int)
)";
struct ScaArgsVariantConverter {
ScaArgsVariantConverter&
fromPython()
{
bp::converter::registry::push_back(
&ScaArgsVariantConverter::convertible,
&ScaArgsVariantConverter::construct,
bp::type_id<ScaArgsVariant>());
return *this;
}
static void* convertible(PyObject* obj)
{
return obj;
}
static void construct(PyObject* obj, bp::converter::rvalue_from_python_stage1_data* data)
{
bp::handle<> handle(bp::borrowed(obj));
void* storage = ((bp::converter::rvalue_from_python_storage<ScaArgsVariant>*)data)->storage.bytes;
ScaArgsVariant ret;
bp::extract<std::string> s(obj);
if (s.check()) {
ret = s();
} else {
bp::tuple tuple(bp::borrowed(obj));
bp::extract<uint32_t> i(tuple[1]);
if (!i.check()) {
ret = std::pair<std::string, std::pair<uint32_t, uint32_t>>(bp::extract<std::string>(tuple[0]),
bp::extract<std::pair<uint32_t, uint32_t>>(tuple[1]));
} else {
ret = bp::extract<std::pair<std::string, uint32_t>>(tuple);
}
}
new (storage) ScaArgsVariant(ret);
data->convertible = storage;
return;
}
};
class ScaInterface
{
public:
ScaInterface(std::string cardIdString, int linkId = -1)
{
mSca = std::make_shared<Sca>(cardIdString, linkId);
}
void setChannel(int gbtChannel)
{
mSca->setChannel(gbtChannel);
}
void scReset()
{
mSca->scReset();
}
void svlReset()
{
mSca->svlReset();
}
void svlConnect()
{
mSca->svlConnect();
}
Sca::CommandData executeCommand(uint32_t cmd, uint32_t data)
{
return mSca->executeCommand(cmd, data);
}
std::vector<std::pair<Sca::Operation, Sca::Data>> sequence(std::vector<ScaArgsVariant> sequence, bool lock = false, int lockTimeout = 0)
{
ScopedGILRelease s; // enable boost::python multi-threading
std::vector<std::pair<Sca::Operation, Sca::Data>> scaSequence;
for (const auto& v : sequence) {
scaSequence.push_back(boost::apply_visitor(ScaArgsVariantVisitor(), v));
}
return mSca->executeSequence(scaSequence, lock, lockTimeout);
}
std::vector<std::pair<Sca::Operation, Sca::Data>> sequenceDefault(std::vector<ScaArgsVariant> scaSeq)
{
return sequence(scaSeq);
}
private:
struct ScaArgsVariantVisitor
: public boost::static_visitor<std::pair<Sca::Operation, Sca::Data>> {
auto operator()(std::string v) const
{
return std::make_pair(Sca::StringToScaOperation(v), 0);
}
auto operator()(std::pair<std::string, uint32_t> v) const
{
return std::make_pair(Sca::StringToScaOperation(v.first), v.second);
}
auto operator()(std::pair<std::string, std::pair<uint32_t, uint32_t>> v) const
{
return std::make_pair(Sca::StringToScaOperation(v.first), Sca::CommandData{ v.second.first, v.second.second });
}
};
std::shared_ptr<Sca> mSca;
};
struct ScaOperationDataVectorToPythonListOfTuples {
static PyObject* convert(const std::vector<std::pair<Sca::Operation, Sca::Data>>& seqOut)
{
bp::list ret;
for (const auto& el : seqOut) {
ret.append(bp::make_tuple(Sca::ScaOperationToString(el.first), el.second));
}
return bp::incref(bp::object(ret).ptr());
}
};
struct ScaDataToPython {
struct ScaDataConvertVisitor : public boost::static_visitor<PyObject*> {
auto operator()(Sca::CommandData commandData) const
{
bp::tuple ret = bp::make_tuple(commandData.command, commandData.data);
return bp::incref(bp::object(ret).ptr());
}
auto operator()(int data) const
{
return bp::incref(bp::object(data).ptr());
}
auto operator()(std::string data) const
{
return bp::incref(bp::object(data).ptr());
}
};
static PyObject* convert(Sca::Data data)
{
return boost::apply_visitor(ScaDataConvertVisitor(), data);
}
};
} //namespace python
} //namespace alf
} //namespace o2
#endif // O2_ALF_SRC_SCAPYTHONINTERFACE_H