DPsim
InterfaceWorkerVillas.h
1 // SPDX-License-Identifier: Apache-2.0
2 
3 #pragma once
4 
5 #include <dpsim-models/PtrFactory.h>
6 #include <dpsim/InterfaceWorker.h>
7 
8 #include <villas/kernel/rt.hpp>
9 #include <villas/node.hpp>
10 #include <villas/node/exceptions.hpp>
11 #include <villas/node/memory.hpp>
12 #include <villas/pool.hpp>
13 #include <villas/sample.hpp>
14 #include <villas/signal.hpp>
15 #include <villas/signal_list.hpp>
16 
17 using namespace villas;
18 
19 namespace DPsim {
21  public SharedFactory<InterfaceWorkerVillas> {
22 
23 public:
24  using Ptr = std::shared_ptr<InterfaceWorkerVillas>;
25  using Sample = struct node::Sample;
26 
27  static UInt villasPriority;
28  static UInt villasAffinity;
29  static UInt villasHugePages;
30 
31 private:
32  static Bool villasInitialized;
33 
34  std::vector<
35  std::tuple<std::function<CPS::AttributeBase::Ptr(Sample *)>, UInt>>
36  mImports;
37  std::vector<std::tuple<std::function<void(CPS::AttributeBase::Ptr, Sample *)>,
38  UInt, Bool>>
39  mExports;
40 
41  // VILLASnode node to send / receive data to / from
42  String mNodeConfig;
43  node::Node *mNode;
44 
45  int mQueueLength;
46  int mSampleLength;
47  node::Pool mSamplePool;
48 
49  Sample *mLastSample;
50  int mSequence;
51 
52  std::map<int, node::Signal::Ptr> mExportSignals;
53  std::map<int, node::Signal::Ptr> mImportSignals;
54 
55 public:
56  InterfaceWorkerVillas(const String &nodeConfig, UInt queueLenght = 512,
57  UInt sampleLenght = 64);
58 
59  void open() override;
60  void close() override;
61 
62  void readValuesFromEnv(
63  std::vector<Interface::AttributePacket> &updatedAttrs) override;
64  void writeValuesToEnv(
65  std::vector<Interface::AttributePacket> &updatedAttrs) override;
66 
67  virtual void configureImport(UInt attributeId, const std::type_info &type,
68  UInt idx, const String &name = "",
69  const String &unit = "");
70  virtual void configureExport(UInt attributeId, const std::type_info &type,
71  UInt idx, Bool waitForOnWrite,
72  const String &name = "",
73  const String &unit = "");
74 
75  void printSignals() const;
76 
77 private:
78  void prepareNode();
79  void setupNodeSignals();
80  void initVillas() const;
81 };
82 } // namespace DPsim