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