libcyberradio  22.01.24
SyncTXClient.cpp
1 #include <LibCyberRadio/NDR651/SyncTXClient.h>
2 
3 namespace LibCyberRadio
4 {
5  namespace NDR651
6  {
7  SyncTXClient::SyncTXClient(
8  std::vector<TXClient *> txClients,
9  std::string hostname,
10  bool debug
11  ):
12  Debuggable(debug, "SyncTXClient"),
13  txClients(txClients),
14  hostname(hostname),
15  ducGroup(1),
16  rc(NULL),
17  isRunning(false)
18  {
19  // Put the TXClient objects in grouped mode
20  for (int i = 0; i < this->txClients.size(); i++)
21  {
22  this->txClients[i]->setGrouped(true);
23  }
24  // Create a radio controller (sends cmds to 651)
25  this->rc = new RadioController(this->hostname, 8617, debug);
26  }
27 
28  SyncTXClient::~SyncTXClient()
29  {
30  if (this->rc != NULL)
31  {
32  delete this->rc;
33  }
34 
35  // Iterate of client list and delete all TXClient objects
36  for (int i = 0; i < this->txClients.size(); i++)
37  {
38  delete this->txClients[i];
39  }
40  this->txClients.clear();
41  }
42 
43  // Starts transmission to radio
44  void SyncTXClient::start()
45  {
46  if (this->isRunning)
47  {
48  // Stop any previous clients
49  this->stop();
50  }
51  this->isRunning = true;
52 
53  // Set up DUC group to cover the DUCs managed by the
54  // member TXClient objects
55  this->debug("[start] Setting up DUC Group %d\n", ducGroup);
56  // -- Clear all DUC group members
57  this->rc->clearDUCG(ducGroup);
58  // -- Add DUCs from TXClients to DUC group
59  for (int i = 0; i < this->txClients.size(); i++)
60  {
61 // this->debug("[start] Adding DUC %d to DUC Group %d\n",
62 // this->txClients[i]->getDucChannel(),
63 // ducGroup);
64  this->rc->setDUCG(ducGroup, this->txClients[i]->getDucChannel(), true);
65  }
66 
67  // Disable the DUC group for now. We will enable it later when
68  // the managed TXClients pre-fill the DUC buffers enough.
69  this->debug("[start] Disabling DUC Group %d\n", ducGroup);
70  this->rc->setDUCGE(ducGroup, false);
71 
72  // Start the individual clients
73  for (int i = 0; i < this->txClients.size(); i++)
74  {
75  this->debug("[start] Starting TX Client for DUC %d\n",
76  this->txClients[i]->getDucChannel(),
77  ducGroup);
78  // Start the clients
79  this->txClients[i]->start();
80  }
81  this->waitingToEnableDUCGE = true;
82  }
83 
84  // Stops transmission to radio
85  void SyncTXClient::stop()
86  {
87  if (this->isRunning)
88  {
89  this->rc->setDUCGE(ducGroup, false);
90  for (int i = 0; i < this->txClients.size(); i++)
91  {
92  this->txClients[i]->stop();
93  }
94  this->isRunning = false;
95  }
96  }
97 
98  bool SyncTXClient::areAllDucsPaused(void) {
99  bool allDucsInPause = true;
100  for (int i = 0; i < this->txClients.size(); i++)
101  {
102  allDucsInPause &= this->txClients[i]->isDUCPaused();
103  }
104  return allDucsInPause;
105  }
106 
107  bool SyncTXClient::checkClientStatus(void) {
108  bool ducsStarted = false;
109  bool anyDucsInPause = false;
110  bool anyDucsReady = false;
111  bool allDucsReady = true;
112  for (int i = 0; i < this->txClients.size(); i++)
113  {
114  anyDucsInPause |= this->txClients[i]->isDUCPaused();
115  anyDucsReady |= !(this->txClients[i]->isDUCReady());
116  allDucsReady &= !(this->txClients[i]->isDUCReady());
117  }
118  // -- Proceed only if we had DUCs "in pause" waiting for pre-filling
119  if ( anyDucsInPause && allDucsReady )
120  {
121  this->debug("[sendFrames] DUCs are ready, enabling DUC Group %d", ducGroup);
122  this->rc->setDUCGE(ducGroup, true);
123  for (int i = 0; i < this->txClients.size(); i++)
124  {
125  this->txClients[i]->setDUCPaused(false);
126  }
127  ducsStarted = true;
128  }
129  return ducsStarted;
130  }
131 
132  bool SyncTXClient::sendFrameToClient(short *frame, unsigned int samplesPerFrame, int client) {
133  bool rv = false;
134  if (client < this->txClients.size()) {
135  this->txClients[client]->sendFrame(frame, samplesPerFrame);
136  rv = true;
137  }
138  return rv;
139  }
140 
141  void SyncTXClient::sendFrames(short **frames, unsigned int samplesPerFrame)
142  {
143  // When this method is called, the managed DUCs may be in the
144  // "paused" state, waiting to be pre-filled with data before we
145  // enable the DUC group.
146 
147  // -- First, check to see if any DUCs are in the "paused" state,
148  // waiting for pre-filling.
149  bool anyDucsInPause = false;
150  for (int i = 0; i < this->txClients.size(); i++)
151  {
152  anyDucsInPause |= this->txClients[i]->isDUCPaused();
153  }
154 
155  // -- Call send frame on each client. This will either pre-fill
156  // the buffer if paused, or send the data if not.
157  for (int i = 0; i < this->txClients.size(); i++)
158  {
159  this->txClients[i]->sendFrame(frames[i], samplesPerFrame);
160  }
161 
162  // -- Proceed only if we had DUCs "in pause" waiting for pre-filling
163  if (anyDucsInPause) {
164  // this->debug("[sendFrames] DUCs are paused, Checking to see if we should enable the DUC Group %d", ducGroup);
165  bool anyDucsReady = false;
166  for (int i = 0; i < this->txClients.size(); i++)
167  {
168  anyDucsReady |= this->txClients[i]->isDUCReady();
169  }
170  if (anyDucsReady) {
171  // this->debug("[sendFrames] DUCs are ready, enabling DUC Group %d", ducGroup);
172  this->rc->setDUCGE(ducGroup, true);
173  for (int i = 0; i < this->txClients.size(); i++)
174  {
175  this->txClients[i]->setDUCPaused(false);
176  }
177  }
178  }
179  // if ( anyDucsInPause )
180  // {
181  // // -- Check to see if any DUCs are pre-filled and ready to go.
182  // bool anyDucsReady = false;
183  // for (int i = 0; i < this->txClients.size(); i++)
184  // {
185  // anyDucsReady |= !(this->txClients[i]->isDUCPaused());
186  // }
187  // // If any DUCs are ready, then enable the DUC group.
188  // if (anyDucsReady)
189  // {
190  // this->debug("[sendFrames] DUCs are ready, enabling DUC Group %d", ducGroup);
191  // this->rc->setDUCGE(ducGroup, true);
192  // }
193  // }
194  }
195 
196  void SyncTXClient::setDucGroup(int ducGroup)
197  {
198  if (ducGroup >= 1 && ducGroup <= 4) {
199  this->ducGroup = ducGroup;
200  }
201  }
202 
203  int SyncTXClient::getDucGroup()
204  {
205  return(this->ducGroup);
206  }
207 
208  }
209 }
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
Defines functionality for LibCyberRadio applications.
Definition: App.h:23