Line data Source code
1 : /**
2 : * @file DFO.cpp
3 : *
4 : * Implementation of WIECApplication's generate_modules dal method
5 : *
6 : * This is part of the DUNE DAQ Software Suite, copyright 2023.
7 : * Licensing/copyright details are in the COPYING file that you should have
8 : * received with this code.
9 : */
10 :
11 :
12 : #include "ConfigObjectFactory.hpp"
13 : #include "conffwk/Configuration.hpp"
14 : #include "logging/Logging.hpp"
15 :
16 : #include "appmodel/NWDetDataReceiver.hpp"
17 : #include "confmodel/NetworkInterface.hpp"
18 : #include "confmodel/DetectorStream.hpp"
19 : #include "confmodel/GeoId.hpp"
20 :
21 : #include "appmodel/appmodelIssues.hpp"
22 : #include "appmodel/WIECApplication.hpp"
23 :
24 : #include "appmodel/WIBModule.hpp"
25 : #include "appmodel/WIBModuleConf.hpp"
26 : #include "appmodel/WIBSettings.hpp"
27 : #include "appmodel/HermesDataSender.hpp"
28 : #include "appmodel/HermesModule.hpp"
29 : #include "appmodel/HermesModuleConf.hpp"
30 : #include "appmodel/IpbusAddressTable.hpp"
31 : #include "confmodel/DetectorToDaqConnection.hpp"
32 :
33 :
34 : #include <string>
35 : #include <vector>
36 : #include <iostream>
37 : #include <fmt/core.h>
38 :
39 : namespace dunedaq {
40 : namespace appmodel {
41 :
42 : //-----------------------------------------------------------------------------
43 :
44 : std::vector<const confmodel::Resource*>
45 0 : WIECApplication::contained_resources() const {
46 0 : return to_resources(get_detector_connections());
47 : }
48 :
49 :
50 : void
51 0 : WIECApplication::generate_modules(std::shared_ptr<appmodel::ConfigurationHelper> helper) const
52 : {
53 0 : ConfigObjectFactory obj_fac(this);
54 0 : conffwk::Configuration* config = &this->configuration();
55 0 : const std::string& dbfile = this->config_object().contained_in();
56 :
57 0 : std::vector<const confmodel::DaqModule*> modules;
58 :
59 0 : std::map<std::string, std::vector<const appmodel::HermesDataSender*>> ctrlhost_sender_map;
60 :
61 :
62 : // uint16_t conn_idx = 0;
63 0 : for (auto d2d_conn : get_detector_connections()) {
64 :
65 : // Are we sure?
66 0 : if (helper->is_disabled(d2d_conn)) {
67 0 : TLOG_DEBUG(7) << "Ignoring disabled DetectorToDaqConnection " << d2d_conn->UID();
68 0 : continue;
69 0 : }
70 :
71 0 : TLOG_DEBUG(6) << "Processing DetectorToDaqConnection " << d2d_conn->UID();
72 :
73 : // Is this check necessary?
74 0 : if (d2d_conn->contained_resources().empty()) {
75 0 : throw(BadConf(ERS_HERE, "DetectorToDaqConnection does not contain senders or receivers"));
76 : }
77 :
78 0 : auto det_senders = d2d_conn->senders();
79 0 : auto det_receiver = d2d_conn->receiver();
80 :
81 : // Ensure that receiver is a nw_receiver
82 0 : const auto* nw_receiver = det_receiver->cast<appmodel::NWDetDataReceiver>();
83 :
84 0 : if ( !nw_receiver ) {
85 0 : throw(BadConf(ERS_HERE, fmt::format("WEICApplication requires NWDetDataReceiver, found {} of class {}", det_receiver->UID(), det_receiver->class_name())));
86 : }
87 :
88 : // Loop over senders
89 0 : for (const auto* sender : det_senders) {
90 :
91 0 : if (helper->is_disabled(sender)) {
92 0 : TLOG() << "Skipping disabled sender: " << sender->UID();
93 0 : continue;
94 0 : }
95 :
96 : // Check the sender type, must me a HermesSender
97 0 : const auto* hrms_sender = sender->cast<appmodel::HermesDataSender>();
98 0 : if (!hrms_sender ) {
99 0 : throw(BadConf(ERS_HERE, fmt::format("DataSender {} is not a appmodel::HermesDataSender", sender->UID())));
100 : }
101 :
102 0 : ctrlhost_sender_map[hrms_sender->get_control_host()].push_back(hrms_sender);
103 : }
104 :
105 :
106 0 : for( const auto& [ctrlhost, senders] : ctrlhost_sender_map ) {
107 :
108 : // Create WIBModule
109 0 : if ( this->get_wib_module_conf() ) {
110 :
111 0 : bool enable_fembs[4] = {false, false, false, false};
112 :
113 0 : for ( const auto* sender : senders ){
114 0 : for ( const auto* det_stream : sender->get_streams() ) {
115 : // Loop over streams for this sender
116 : // Retrieve stream_id and calculate the femb_id
117 0 : uint32_t stream_id = det_stream->get_geo_id()->get_stream_id();
118 0 : uint32_t femb_id = (stream_id & 0xf) / 2 + 2*((stream_id >> 6) & 0xf);
119 :
120 : // std::cout << std::format("stream {} -> femb {}", stream_id, femb_id) << std::endl;
121 :
122 : // Enable the femb if any of the associated streams is enabld
123 : // Senders in this senders list should be enabled, but better safe than sorry.
124 0 : enable_fembs[femb_id] |= helper->is_enabled(det_stream);
125 : }
126 : }
127 0 : std::string wib_uid = fmt::format("wib-ctrl-{}-{}", this->UID(), ctrlhost);
128 0 : conffwk::ConfigObject wib_obj = obj_fac.create("WIBModule", wib_uid);
129 0 : wib_obj.set_by_val<std::string>("wib_addr", fmt::format("{}://{}:{}", this->get_wib_module_conf()->get_communication_type(), ctrlhost, this->get_wib_module_conf()->get_communication_port()));
130 0 : wib_obj.set_by_val<bool>("enabled_femb0", enable_fembs[0]);
131 0 : wib_obj.set_by_val<bool>("enabled_femb1", enable_fembs[1]);
132 0 : wib_obj.set_by_val<bool>("enabled_femb2", enable_fembs[2]);
133 0 : wib_obj.set_by_val<bool>("enabled_femb3", enable_fembs[3]);
134 0 : wib_obj.set_obj("conf", &this->get_wib_module_conf()->get_settings()->config_object());
135 0 : modules.push_back(config->get<appmodel::WIBModule>(wib_obj));
136 0 : }
137 :
138 : // Create Hermes Modules
139 0 : if (this->get_hermes_module_conf()) {
140 0 : std::string hermes_uid = fmt::format("hermes-ctrl-{}-{}", this->UID(), ctrlhost);
141 0 : conffwk::ConfigObject hermes_obj = obj_fac.create("HermesModule", hermes_uid);
142 0 : hermes_obj.set_obj("address_table", &this->get_hermes_module_conf()->get_address_table()->config_object());
143 0 : hermes_obj.set_by_val<std::string>("uri", fmt::format("{}://{}:{}", this->get_hermes_module_conf()->get_ipbus_type(), ctrlhost, this->get_hermes_module_conf()->get_ipbus_port()));
144 0 : hermes_obj.set_by_val<uint32_t>("timeout_ms", this->get_hermes_module_conf()->get_ipbus_timeout_ms());
145 0 : hermes_obj.set_obj("destination", &nw_receiver->get_uses()->config_object());
146 :
147 0 : std::vector< const conffwk::ConfigObject * > links_obj;
148 0 : for ( const auto* sndr : senders ){
149 0 : links_obj.push_back(&sndr->config_object());
150 : }
151 0 : hermes_obj.set_objs("links", links_obj);
152 :
153 0 : modules.push_back(config->get<appmodel::HermesModule>(hermes_obj));
154 0 : }
155 :
156 :
157 : }
158 :
159 0 : }
160 :
161 0 : obj_fac.update_modules(modules);
162 0 : }
163 :
164 : } // namespace appmodel
165 : } // namespace dunedaq
|