1 #include "LibCyberRadio/NDR651/TXClient.h" 7 TXClient::TXClient(std::string radioHostName,
bool debug):
8 Debuggable(debug,
"TXClient"),
10 tenGbeIndex(INVALID_VALUE),
11 ducChannel(INVALID_VALUE),
13 ducRateIndex(INVALID_VALUE),
14 rfChannel(INVALID_VALUE),
17 ducFullThreshPercent(0.90),
18 ducEmptyThreshPercent(0.82),
20 txFreq(INVALID_VALUE),
23 radioHostName(radioHostName),
34 prefillSampleCount(0L)
37 this->rc =
new RadioController(radioHostName, 8617, debug);
46 if (this->statusRX != NULL)
48 delete this->statusRX;
50 if (this->packetizer != NULL)
52 delete this->packetizer;
57 std::string TXClient::getSourceMac()
59 if (this->packetizer != NULL)
64 memset(&ifr, 0x00,
sizeof(ifr));
65 strcpy(ifr.ifr_name, this->txInterfaceName.c_str());
66 ioctl(this->packetizer->getSocketFd(), SIOCGIFHWADDR, &ifr);
67 sprintf(macstring,
"%02x:%02x:%02x:%02x:%02x:%02x",
68 (
unsigned char)ifr.ifr_hwaddr.sa_data[0],
69 (
unsigned char)ifr.ifr_hwaddr.sa_data[1],
70 (
unsigned char)ifr.ifr_hwaddr.sa_data[2],
71 (
unsigned char)ifr.ifr_hwaddr.sa_data[3],
72 (
unsigned char)ifr.ifr_hwaddr.sa_data[4],
73 (
unsigned char)ifr.ifr_hwaddr.sa_data[5]);
74 return std::string(macstring);
79 std::string TXClient::getSourceIP()
81 if (this->packetizer != NULL)
84 ifr.ifr_addr.sa_family = AF_INET;
85 strncpy(ifr.ifr_name, this->txInterfaceName.c_str(), IFNAMSIZ-1);
86 ioctl(this->packetizer->getSocketFd(), SIOCGIFADDR, &ifr);
87 return std::string(inet_ntoa(((
struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
92 bool TXClient::validInputs(std::string &errors)
97 if (this->txInterfaceName.empty()) {
98 errors +=
"Tx Interface Name not configured\n";
101 if (this->tenGbeIndex == INVALID_VALUE) {
102 errors +=
"Ten GBE Index not configured.\n";
105 if (this->ducChannel == INVALID_VALUE) {
106 errors +=
"DUC Channel not configured.\n";
109 if (this->ducRateIndex == INVALID_VALUE) {
110 errors +=
"DUC Rate Index not configured.\n";
113 if (this->rfChannel == INVALID_VALUE) {
114 errors +=
"RF TX Channel not configured.\n";
117 if (this->txUdpPort == 0) {
118 errors +=
"TX UDP Port not configured.\n";
131 void TXClient::setGrouped(
bool isGrouped)
133 this->isGrouped = isGrouped;
136 bool TXClient::isDUCPaused()
138 return this->DUCPaused;
141 bool TXClient::setDUCPaused(
bool paused)
143 std::cout <<
"setDUCPaused( " << this->DUCPaused <<
" -> " << paused <<
" )" << std::endl;
144 this->DUCPaused = paused;
149 bool TXClient::isDUCReady()
152 return this->DUCReady;
155 unsigned int TXClient::getDucChannel()
157 return this->ducChannel;
161 void TXClient::disableRF()
164 this->debug(
"[disableRF] Called\n");
165 this->rc->setTXP(this->rfChannel,
false);
166 this->debug(
"[disableRF] Returning\n");
169 bool TXClient::setDUCChannel(
unsigned int ducChannel)
171 this->debug(
"[setDUCChannel] Called\n");
172 this->debug(
"[setDUCChannel] -- ducChannel = %u\n", ducChannel);
174 if (!this->isRunning)
176 this->ducChannel = ducChannel;
179 this->debug(
"[setDUCChannel] Returning %s\n", debugBool(ret) );
183 bool TXClient::setTxChannel(
unsigned int txChannel)
186 this->debug(
"[setTxChannel] Called\n");
187 this->debug(
"[setTxChannel] -- txChannel = %u\n", txChannel);
188 if (!this->isRunning) {
189 this->rfChannel = txChannel;
192 this->debug(
"[setTxChannel] Returning %s\n", debugBool(ret) );
197 bool TXClient::setDUCRateIndex(
unsigned int ducRateIndex)
204 this->debug(
"[setDUCRateIndex] Called\n");
205 this->debug(
"[setDUCRateIndex] -- ducRateIndex = %u\n", ducRateIndex);
206 boost::mutex::scoped_lock lock(this->objectAccessMutex);
207 bool ret = this->setDUCRateIndexUnlocked(ducRateIndex);
208 this->debug(
"[setDUCRateIndex] Returning %s\n", debugBool(ret) );
213 bool TXClient::setDUCFreq(
double ducFreq)
215 this->debug(
"[setDUCFreq] Called\n");
216 this->debug(
"[setDUCFreq] -- ducFreq = %f\n", ducFreq);
217 boost::mutex::scoped_lock lock(this->objectAccessMutex);
219 if (this->ducChannel != INVALID_VALUE)
221 result = this->rc->setDUCF(this->ducChannel, ducFreq);
224 this->ducFreq = ducFreq;
230 this->debug(
"[setDUCFreq] Not configured yet!\n");
232 this->debug(
"[setDUCFreq] Returning %s\n", debugBool(result) );
237 bool TXClient::setDUCAtten(
double ducAttenuation)
239 this->debug(
"[setDUCAtten] Called\n");
240 this->debug(
"[setDUCAtten] -- ducAttenuation = %f\n", ducAttenuation);
241 boost::mutex::scoped_lock lock(this->objectAccessMutex);
243 if (this->ducChannel != INVALID_VALUE)
245 result = this->rc->setDUCA(this->ducChannel, ducAttenuation);
248 this->ducAttenuation = ducAttenuation;
254 this->debug(
"[setDUCAtten] Not configured yet!\n");
256 this->debug(
"[setDUCAtten] Returning %s\n", debugBool(result) );
260 bool TXClient::setDUCParameters(
unsigned int ducIndex,
unsigned int ducRateIndex,
unsigned int txChannel)
262 this->debug(
"[setDUCParameters] Called\n");
263 this->debug(
"[setDUCParameters] -- ducIndex = %u\n", ducIndex);
264 this->debug(
"[setDUCParameters] -- ducRateIndex = %u\n", ducRateIndex);
265 this->debug(
"[setDUCParameters] -- txChannel = %u\n", txChannel);
266 boost::mutex::scoped_lock lock(this->objectAccessMutex);
271 bool success = this->setDUCChannel(ducIndex) &&
272 this->setDUCRateIndexUnlocked(ducRateIndex) &&
273 this->setTxChannel(txChannel);
274 this->debug(
"[setDUCParameters] Returning %s\n", debugBool(success) );
279 bool TXClient::setTxAtten(
double txAttenuation)
281 this->debug(
"[setTxAtten] Called\n");
282 this->debug(
"[setTxAtten] -- txAttenuation = %f\n", txAttenuation);
284 if (this->ducChannel != INVALID_VALUE)
286 result = this->rc->setTXA(this->rfChannel, txAttenuation);
292 this->txAttenuation = this->rc->getTXA(this->rfChannel);
295 this->debug(
"[setTxAtten] Returning %s\n", debugBool(result) );
300 bool TXClient::setTxFreq(
double txFreq)
302 this->debug(
"[setTxFreq] Called\n");
303 this->debug(
"[setTxFreq] -- txFreq = %f\n", txFreq);
305 if (this->rfChannel != INVALID_VALUE)
307 result = this->rc->setTXF(this->rfChannel, txFreq);
308 if (result ==
true) {
309 this->txFreq = txFreq;
312 this->debug(
"[setTxFreq] Returning %s\n", debugBool(result) );
317 bool TXClient::setTxInversion(
bool txInversion)
319 if (this->ducChannel == INVALID_VALUE)
return false;
320 bool result = this->rc->setTXINV(this->ducChannel, txInversion);
321 if (result ==
true) {
322 this->txInversion = txInversion;
327 bool TXClient::setEthernetInterface(
unsigned int tenGbeIndex,
const std::string &txInterfaceName,
unsigned short port)
329 if (!this->isRunning) {
330 this->tenGbeIndex = tenGbeIndex;
331 this->txInterfaceName = std::string(txInterfaceName);
332 this->txUdpPort = port;
339 void TXClient::start()
341 this->debug(
"[start] Called\n");
348 if (!this->validInputs(errors))
350 std::cerr <<
"ERRORS CONFIGURING TX CLIENT: " << errors << std::endl;
351 throw std::runtime_error(errors);
354 this->isRunning =
true;
355 this->debug(
"[start] Starting Transmit Client\n");
358 this->prefillSampleCount = ((
unsigned int )(0.50 * 67108860)) - ((
unsigned int )(0.50 * 67108860)%4);
361 this->packetizer =
new Packetizer(this->txInterfaceName, this->txUdpPort, this->ducRateIndex, this->debugOn);
362 this->packetizer->start();
365 this->statusRX =
new StatusReceiver(this->txInterfaceName, UDP_STATUS_BASE + this->ducChannel, this->debugOn,
false);
366 std::ostringstream statusRxName;
367 statusRxName <<
"StatusRx" << this->txUdpPort;
368 this->statusRX->setName(statusRxName.str());
371 this->debug(
"[start] Enabling TX\n");
378 this->debug(
"[start] Configuring DUC\n");
383 this->ducAttenuation,
386 this->DUCPaused ? 2 : 0,
389 this->debug(
"[start] Configuring DUC complete\n");
392 this->rc->setDUCHSPercent(
395 this->ducFullThreshPercent,
396 this->ducEmptyThreshPercent,
397 this->updatesPerSecond,
406 this->getSourceMac(),
407 this->statusRX->getUdpPort()
411 this->statusRX->start();
412 this->debug(
"[start] Returning");
415 void TXClient::stop(
bool disableRF)
419 this->debug(
"Stopping Transmit Client\n");
420 this->statusRX->interrupt();
421 this->rc->clearDUC(this->ducChannel);
422 this->rc->clearDUCHS(this->ducChannel);
423 this->isRunning =
false;
424 if (disableRF) this->disableRF();
428 if (this->packetizer != NULL)
430 delete this->packetizer;
431 this->packetizer = NULL;
435 if (this->statusRX != NULL)
437 delete this->statusRX;
438 this->statusRX = NULL;
442 void TXClient::sendFrame(
short * samples,
unsigned int samplesPerFrame)
447 if (this->prefillSampleCount > 0)
450 this->packetizer->sendFrame(samples);
451 this->prefillSampleCount -= samplesPerFrame;
452 if ((this->prefillSampleCount <= 0))
455 this->DUCReady =
true;
456 if (!this->isGrouped)
458 this->DUCPaused =
false;
464 this->ducAttenuation,
467 this->DUCPaused ? 2 : 0,
476 this->statusRX->blockUntilAvailable(samplesPerFrame);
479 this->packetizer->sendFrame(samples);
482 this->statusRX->sentNSamples(samplesPerFrame);
487 this->debug(
"WARNING: Called send frame without calling start()\n");
491 bool TXClient::pauseDUC(
bool paused)
493 this->debug(
"[pauseDUC] Called\n");
494 this->debug(
"[pauseDUC] -- paused = %s\n", this->debugBool(paused));
496 boost::mutex::scoped_lock lock(this->objectAccessMutex);
497 this->DUCPaused = paused;
498 if (this->ducChannel != INVALID_VALUE)
500 ret = this->rc->setDUCP(this->ducChannel, this->DUCPaused);
508 bool TXClient::setDUCRateIndexUnlocked(
unsigned int ducRateIndex)
515 this->debug(
"[setDUCRateIndexUnlocked] Called\n");
516 this->debug(
"[setDUCRateIndexUnlocked] -- ducRateIndex = %u\n", ducRateIndex);
518 if (this->ducChannel != INVALID_VALUE)
521 if ( this->ducRateIndex != INVALID_VALUE )
523 this->debug(
"[setDUCRateIndexUnlocked] Sending radio command\n");
524 result = this->rc->setDUC(
528 this->ducAttenuation,
531 this->DUCPaused ? 2 : 0,
536 this->debug(
"[setDUCRateIndexUnlocked] Skipping radio command\n");
537 this->ducRateIndex = ducRateIndex;
543 this->debug(
"[setDUCRateIndexUnlocked] Not configured yet!\n");
545 this->debug(
"[setDUCRateIndexUnlocked] Returning %s\n", debugBool(result) );
Defines functionality for LibCyberRadio applications.