44 #include <boost/python.hpp>
45namespace bp = boost::python;
81 taps_ =
reinterpret_cast<uint8_t*
>(malloc(
sizeof(uint8_t) * tapCnt_));
87 gettimeofday(&lastRxTime_, NULL);
88 gettimeofday(&lastTxTime_, NULL);
109double ru::Prbs::updateTime(
struct timeval* last) {
118 gettimeofday(&now, NULL);
120 timersub(&now, last, &per);
122 if (timercmp(&per, &cmp, >)) {
123 ret =
static_cast<float>(per.tv_sec) +
static_cast<float>(per.tv_usec) / 1e6;
124 gettimeofday(last, NULL);
132void ru::Prbs::setWidth(uint32_t width) {
133 if ((width > (MaxBytes * 8)) || (width % 32) != 0)
throw(
rogue::GeneralError(
"Prbs::setWidth",
"Invalid width."));
136 std::lock_guard<std::mutex> lockT(pMtx_);
139 byteWidth_ = width / 8;
140 minSize_ = byteWidth_ * 3;
144void ru::Prbs::setTaps(uint32_t tapCnt, uint8_t* taps) {
148 std::lock_guard<std::mutex> lockT(pMtx_);
152 taps_ =
reinterpret_cast<uint8_t*
>(malloc(
sizeof(uint8_t) * tapCnt));
154 for (i = 0; i < tapCnt_; i++) taps_[i] = taps[i];
160void ru::Prbs::setTapsPy(boost::python::object p) {
163 if (PyObject_GetBuffer(p.ptr(), &pyBuf, PyBUF_SIMPLE) < 0)
166 setTaps(pyBuf.len,
reinterpret_cast<uint8_t*
>(pyBuf.buf));
167 PyBuffer_Release(&pyBuf);
173void ru::Prbs::sendCount(
bool state) {
175 std::lock_guard<std::mutex> lockT(pMtx_);
180void ru::Prbs::flfsr(uint8_t* data) {
187 for (x = 0; x < tapCnt_; x++) {
188 iByte = taps_[x] / 8;
190 lsbIn ^= ((data[iByte] >> iBit) & 0x1);
193 for (x = 0; x < byteWidth_; x++) {
194 msbOut = data[x] & 0x80;
202void ru::Prbs::runThread() {
203 txLog_->logThreadId();
213 txLog_->error(
"Frame generation failed, stopping: %s", e.
what());
217 if ( txPeriod_ > 0 ) usleep(txPeriod_);
222void ru::Prbs::enable(uint32_t size) {
224 if (((size % byteWidth_) != 0) || size < minSize_)
throw rogue::GeneralError(
"Prbs::enable",
"Invalid frame size");
226 if (txThread_ == NULL) {
229 txThread_ =
new std::thread(&Prbs::runThread,
this);
233 pthread_setname_np(txThread_->native_handle(),
"PrbsTx");
239void ru::Prbs::disable() {
240 if (txThread_ != NULL) {
250bool ru::Prbs::getRxEnable() {
255void ru::Prbs::setRxEnable(
bool en) {
263uint32_t ru::Prbs::getRxErrors() {
264 return (rxErrCount_);
268uint32_t ru::Prbs::getRxCount() {
273uint64_t ru::Prbs::getRxBytes() {
278uint32_t ru::Prbs::getTxErrors() {
279 return (txErrCount_);
283uint32_t ru::Prbs::getTxCount() {
288double ru::Prbs::getRxRate() {
293double ru::Prbs::getRxBw() {
298double ru::Prbs::getTxRate() {
303uint32_t ru::Prbs::getTxPeriod() {
308void ru::Prbs::setTxPeriod(uint32_t value) {
313double ru::Prbs::getTxBw() {
318uint64_t ru::Prbs::getTxBytes() {
323void ru::Prbs::checkPayload(
bool state) {
328void ru::Prbs::genPayload(
bool state) {
334void ru::Prbs::resetCount() {
347void ru::Prbs::genFrame(uint32_t size) {
350 uint32_t frSeq[MaxBytes / 4];
351 uint32_t frSize[MaxBytes / 4];
352 uint32_t wCount[MaxBytes / 4];
353 uint8_t data[MaxBytes];
358 std::lock_guard<std::mutex> lock(pMtx_);
361 if (((size % byteWidth_) != 0) || size < minSize_)
365 memset(frSize, 0, MaxBytes);
366 frSize[0] = (size / byteWidth_) - 1;
369 memset(frSeq, 0, MaxBytes);
373 memset(wCount, 0, MaxBytes);
376 fr = reqFrame(size,
true);
377 fr->setPayload(size);
379 frIter = fr->begin();
380 frEnd = frIter + size;
392 std::memcpy(data, frSeq, byteWidth_);
395 while (frIter != frEnd) {
413 if ((per = updateTime(&lastTxTime_)) > 0.0) {
414 txRate_ =
static_cast<double>(txCount_ - lastTxCount_) / per;
415 txBw_ =
static_cast<double>(txBytes_ - lastTxBytes_) / per;
416 lastTxCount_ = txCount_;
417 lastTxBytes_ = txBytes_;
425 uint32_t frSeq[MaxBytes / 4];
426 uint32_t frSize[MaxBytes / 4];
432 uint8_t expData[MaxBytes];
439 while (!rxEnable_) usleep(10000);
442 std::lock_guard<std::mutex> lock(pMtx_);
444 size = frame->getPayload();
445 frIter = frame->begin();
446 frEnd = frame->end();
449 if (frame->getError()) {
450 rxLog_->warning(
"Frame error field is set: 0x%" PRIx8, frame->getError());
456 if (((size % byteWidth_) != 0) || size < minSize_) {
457 rxLog_->warning(
"Size violation size=%" PRIu32
", count=%" PRIu32, size, rxCount_);
465 rxSeq_ = frSeq[0] + 1;
469 expSize = (frSize[0] + 1) * byteWidth_;
474 if ((expSize != size) || (frSeq[0] != 0 && expSeq != 0 && frSeq[0] != expSeq)) {
475 rxLog_->warning(
"Bad header. expSize=%" PRIu32
" gotSize=%" PRIu32
" expSeq=%" PRIu32
" gotSeq=%" PRIu32
476 " nxtSeq=%" PRIu32
" count=%" PRIu32,
490 std::memcpy(expData, frSeq, byteWidth_);
494 while (frIter != frEnd) {
497 if (!std::equal(frIter, frIter + byteWidth_, expData)) {
500 "Bad value at index %" PRIu32
". count=%" PRIu32
", size=%" PRIu32,
503 (size / byteWidth_) - 1);
505 for (x = 0; x < byteWidth_; x++) {
508 "\n %" PRIu32
":%" PRIu32
" Got=0x%" PRIx8
" Exp=0x%" PRIx8,
513 snprintf(debugA + strlen(debugA),
sizeof(debugA) - strlen(debugA),
"%s", debugB);
515 rxLog_->warning(debugA);
519 frIter += byteWidth_;
527 if ((per = updateTime(&lastRxTime_)) > 0.0) {
528 rxRate_ =
static_cast<double>(rxCount_ - lastRxCount_) / per;
529 rxBw_ =
static_cast<double>(rxBytes_ - lastRxBytes_) / per;
530 lastRxCount_ = rxCount_;
531 lastRxBytes_ = rxBytes_;
535void ru::Prbs::setup_python() {
540 bp::docstring_options doc_options(
true,
false,
false);
542 bp::class_<ru::Prbs, ru::PrbsPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
544 "PRBS generator/checker that can act as both stream master and slave.\n\n"
545 "The engine can generate deterministic pseudo-random frames and verify\n"
546 "received frames for bit-error testing. It supports both one-shot frame\n"
547 "generation and optional background transmit mode.",
548 bp::init<>(
"Construct a PRBS generator/checker with default taps and width."))
549 .def(
"genFrame", &ru::Prbs::genFrame, bp::args(
"size"),
"Generate and transmit one PRBS frame.")
553 "Enable periodic background PRBS frame generation using the configured TX period.")
554 .def(
"disable", &ru::Prbs::disable,
"Disable periodic background PRBS frame generation.")
555 .def(
"setWidth", &ru::Prbs::setWidth, bp::args(
"width"),
"Set the PRBS generator/checker width in bits.")
557 &ru::Prbs::setTapsPy,
559 "Configure the PRBS LFSR tap positions from a bytes-like object.")
560 .def(
"getRxEnable", &ru::Prbs::getRxEnable,
"Return whether receive-side checking is enabled.")
561 .def(
"setRxEnable", &ru::Prbs::setRxEnable, bp::args(
"state"),
"Enable or disable receive-side checking.")
562 .def(
"getRxErrors", &ru::Prbs::getRxErrors,
"Return the receive-side error count.")
563 .def(
"getRxCount", &ru::Prbs::getRxCount,
"Return the total received frame count.")
564 .def(
"getRxRate", &ru::Prbs::getRxRate,
"Return the computed receive frame rate in frames/second.")
565 .def(
"getRxBw", &ru::Prbs::getRxBw,
"Return the computed receive bandwidth in bytes/second.")
566 .def(
"getRxBytes", &ru::Prbs::getRxBytes,
"Return the total received payload byte count.")
567 .def(
"getTxErrors", &ru::Prbs::getTxErrors,
"Return the transmit-side error count.")
568 .def(
"getTxCount", &ru::Prbs::getTxCount,
"Return the total transmitted frame count.")
569 .def(
"getTxBytes", &ru::Prbs::getTxBytes,
"Return the total transmitted payload byte count.")
570 .def(
"getTxPeriod", &ru::Prbs::getTxPeriod,
"Return the periodic transmit interval in microseconds.")
572 &ru::Prbs::setTxPeriod,
574 "Set the periodic transmit interval in microseconds.")
575 .def(
"getTxRate", &ru::Prbs::getTxRate,
"Return the computed transmit frame rate in frames/second.")
576 .def(
"getTxBw", &ru::Prbs::getTxBw,
"Return the computed transmit bandwidth in bytes/second.")
578 &ru::Prbs::checkPayload,
580 "Enable or disable PRBS payload checking on received frames.")
582 &ru::Prbs::genPayload,
584 "Enable or disable PRBS payload generation on transmitted frames.")
585 .def(
"resetCount", &ru::Prbs::resetCount,
"Reset transmit and receive counters.")
587 &ru::Prbs::sendCount,
589 "Enable or disable insertion of frame counters into generated payloads.");
591 bp::implicitly_convertible<ru::PrbsPtr, ris::SlavePtr>();
592 bp::implicitly_convertible<ru::PrbsPtr, ris::MasterPtr>();
Generic Rogue exception type.
char const * what() const
Returns exception text for standard exception handling.
RAII helper that releases the Python GIL for a scope.
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Random-access byte iterator across a Frame payload.
static void fromFrame(rogue::interfaces::stream::FrameIterator &iter, uint32_t size, void *dst)
Copies bytes from a frame iterator to a destination pointer.
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
static void toFrame(rogue::interfaces::stream::FrameIterator &iter, uint32_t size, void *src)
Copies bytes from a source pointer into a frame iterator.
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
std::shared_ptr< rogue::utilities::Prbs > PrbsPtr