1 #include "usNetworkServer.h"
9 std::cout <<
"porta instance created" << std::endl;
20 void usNetworkServer::acceptTheConnection()
26 connectionSoc = tcpServer.nextPendingConnection();
29 connect(connectionSoc, SIGNAL(readyRead()),
this, SLOT(readIncomingData()));
32 connect(connectionSoc, SIGNAL(disconnected()),
this, SLOT(connectionAboutToClose()));
36 void usNetworkServer::readIncomingData()
46 in.setDevice(connectionSoc);
47 in.setVersion(QDataStream::Qt_5_0);
52 std::cout <<
"id received : " <<
id << std::endl;
56 if (m_porta->isImaging())
59 initWithoutUpdate =
true;
64 initPorta(headerInit);
68 QDataStream out(&block, QIODevice::WriteOnly);
69 out.setVersion(QDataStream::Qt_5_0);
71 out << confirmHeader.
initOk;
76 connectionSoc->write(block);
79 if (m_porta->isImaging())
82 initWithoutUpdate =
false;
97 std::cout <<
"received header UPDATE = " <<
id << std::endl;
98 std::cout <<
"transmitFrequency = " << headerUpdate.
transmitFrequency << std::endl;
99 std::cout <<
"samplingFrequency = " << headerUpdate.
samplingFrequency << std::endl;
100 std::cout <<
"imagingMode = " << headerUpdate.
imagingMode << std::endl;
101 std::cout <<
"postScanMode = " << headerUpdate.
postScanMode << std::endl;
102 std::cout <<
"postScanHeigh = " << headerUpdate.
postScanHeight << std::endl;
103 std::cout <<
"postScanWidtht = " << headerUpdate.
postScanWidth << std::endl;
104 std::cout <<
"imageDepth = " << headerUpdate.
imageDepth << std::endl;
105 std::cout <<
"sector = " << headerUpdate.
sector << std::endl;
106 std::cout <<
"activateMotor = " << headerUpdate.
activateMotor << std::endl;
107 std::cout <<
"motorPosition = " << headerUpdate.
motorPosition << std::endl;
108 std::cout <<
"framesPerVolume = " << headerUpdate.
framesPerVolume << std::endl;
109 std::cout <<
"degreesPerFrame = " << headerUpdate.
stepsPerFrame << std::endl;
111 if (updatePorta(headerUpdate)) {
114 QDataStream out(&block, QIODevice::WriteOnly);
115 out.setVersion(QDataStream::Qt_5_0);
117 out << confirmHeader.
initOk;
120 writeUpdateAcquisitionParameters(out, headerUpdate, confirmHeader.
probeId);
121 std::cout <<
"bytes written to confirm update" << connectionSoc->write(block) << std::endl;
123 }
else if (
id == 3) {
124 std::cout <<
"received header RUN/STOP = " <<
id << std::endl;
128 std::cout <<
"command run = " << run << std::endl;
132 m_porta->getProbeInfo(nfo);
133 if (initWithoutUpdate && nfo.motorized) {
134 #if USTK_PORTA_VERSION_MAJOR < 6
135 m_porta->setParam(prmMotorStatus, 0);
137 m_porta->setMotorActive(
false);
139 if (!m_porta->runImage()) {
140 std::cout <<
"porta run error" << std::endl;
142 std::cout <<
"porta run OK" << std::endl;
145 m_porta->stopImage();
146 #if USTK_PORTA_VERSION_MAJOR < 6
147 m_porta->setParam(prmMotorStatus, 0);
149 m_porta->setMotorActive(
false);
152 std::cout <<
"ERROR : unknown data received !" << std::endl;
157 void usNetworkServer::connectionAboutToClose()
160 std::cout <<
"Connection to client closed" << std::endl;
162 if (m_porta->isImaging())
163 m_porta->stopImage();
167 connectionSoc->close();
171 #if USTK_PORTA_VERSION_MAJOR > 5
172 int portaCallback(
void *param,
unsigned char *addr,
int blockIndex,
int)
174 bool portaCallback(
void *param,
unsigned char *addr,
int blockIndex,
int)
179 porta *portaInstance = server->
getPorta();
180 QTcpSocket *socket = server->
getSocket();
182 if (portaInstance->getCurrentMode() != BiplaneMode)
189 portaInstance->getParam(prmMotorStatus, motorStatus);
196 unsigned char *beginImage;
209 portaInstance->getMicronsPerPixel(0, mx, my);
212 if (portaInstance->getCurrentMode() == BiplaneMode) {
222 server->
imageHeader.
dataLength = portaInstance->getParam(prmBNumLines) * portaInstance->getParam(prmBNumSamples);
223 beginImage = addr + 4;
227 int rfSize = portaInstance->getParam(prmRfNumLines) * portaInstance->getParam(prmRfNumSamples);
229 beginImage = addr + 4;
236 server->
biPlane = portaInstance->getCurrentMode() == BiplaneMode;
244 #if USTK_PORTA_VERSION_MAJOR > 5
245 int portaCallbackImage(
void *param,
int id,
int)
247 std::cout <<
"new frame acquired" << std::endl;
249 porta *portaInstance = server->
getPorta();
250 QTcpSocket *socket = server->
getSocket();
252 if (portaInstance->getCurrentMode() != BiplaneMode)
259 portaInstance->getParam(prmMotorStatus, motorStatus);
265 unsigned char *beginImage;
278 portaInstance->getMicronsPerPixel(0, mx, my);
281 if (portaInstance->getCurrentMode() == BiplaneMode) {
283 std::cout <<
"getting 2nd image from bi plane" << std::endl;
289 server->
imageHeader.
dataLength = portaInstance->getParam(prmBNumLines) * portaInstance->getParam(prmBNumSamples);
291 beginImage =
new unsigned char[portaInstance->getParam(prmBNumLines) * portaInstance->getParam(prmBNumSamples) *
292 sizeof(
unsigned char)];
293 portaGetBwImagePrescan(0, beginImage);
300 server->
biPlane = portaInstance->getCurrentMode() == BiplaneMode;
319 if (m_porta->isConnected()) {
320 code = m_porta->getProbeID(header.
slotId);
321 std::cout <<
"probe Id " << code << std::endl;
324 if (m_porta->selectProbe(code) && m_porta->getProbeInfo(nfo)) {
326 if (m_porta->activateProbeConnector(header.
slotId)) {
328 if (m_porta->getProbeName(name, 1024, code)) {
329 std::cout <<
"detected probe : " << name << std::endl;
331 QString settingsPath = QString(USTK_PORTA_SETTINGS_PATH) + QString(
"presets/imaging/");
332 settingsPath += getProbeSettingsFromId(code);
333 m_porta->loadPreset(settingsPath.toStdString().c_str());
334 #if USTK_PORTA_VERSION_MAJOR > 5
338 if (!m_porta->initImagingMode((imagingMode)mode)) {
340 if (!m_porta->initImagingMode((imagingMode)header.
imagingMode)) {
342 std::cout <<
"error initializing imaging mode" << std::endl;
346 #if USTK_PORTA_VERSION_MAJOR > 5
348 m_porta->setRawDataCallback(portaCallback, (
void *)
this);
350 m_porta->setDisplayCallback(0, portaCallbackImage, (
void *)
this);
353 m_porta->setRawDataCallback(portaCallback, (
void *)
this);
357 m_porta->goToPosition(0);
358 #if USTK_PORTA_VERSION_MAJOR <= 5
359 m_porta->setParam(prmMotorStatus, 0);
361 m_porta->setMotorActive(
false);
368 m_currentImagingMode = (imagingMode)header.
imagingMode;
377 #if USTK_PORTA_VERSION_MAJOR <= 5
378 m_porta->setParam(prmBLineDensity, 128);
386 m_porta->getParam(prmBSector, sector);
388 imageHeader.
degPerFrame = m_porta->getParam(prmMotorSteps) * (double)(nfo.motorFov / 1000.0) / (double)nfo.motorSteps;
396 m_porta->getParam(prmMotorFrames, fpv);
405 #if USTK_PORTA_VERSION_MAJOR > 5
410 m_porta->getParam(prmBImageRect, rect);
416 #if USTK_PORTA_VERSION_MAJOR <= 5
417 m_porta->setParam(prmRfMode, 1);
425 #if USTK_PORTA_VERSION_MAJOR > 5
426 m_porta->setDisplayDimensions(0, rect.right, rect.bottom);
431 std::cout <<
"end init porta" << std::endl;
434 bool usNetworkServer::updatePorta(usUpdateHeaderIncomming header)
436 bool initImagingMode;
438 if (m_currentImagingMode != (imagingMode)header.imagingMode) {
440 initImagingMode = m_porta->initImagingMode((imagingMode)header.imagingMode);
441 if (!initImagingMode) {
442 std::cout <<
"error initializing imaging mode" << std::endl;
446 initImagingMode =
true;
448 if (initImagingMode) {
449 m_currentImagingMode = (imagingMode)header.imagingMode;
450 m_porta->setRawDataCallback(portaCallback, (
void *)
this);
453 m_porta->getProbeInfo(nfo);
457 if (!header.activateMotor) {
458 m_porta->setMotorActive(
true);
459 m_porta->goToPosition(header.motorPosition);
460 m_porta->setParam(prmMotorStatus, 0);
461 m_porta->setMotorActive(
false);
465 m_porta->setParam(prmMotorFrames, header.framesPerVolume);
466 m_porta->setParam(prmMotorSteps, header.stepsPerFrame);
467 m_porta->setParam(prmMotorStatus, 1);
468 m_porta->setMotorActive(
true);
473 if (!header.postScanMode) {
474 if ((imagingMode)header.imagingMode == BMode) {
475 #if USTK_PORTA_VERSION_MAJOR > 5
478 m_porta->setParam(prmRfMode, 0);
481 m_porta->getParam(prmBImageRect, rect);
484 }
else if ((imagingMode)header.imagingMode == RfMode) {
489 m_porta->setDisplayDimensions(0, header.postScanWidth, header.postScanHeight);
494 if ((imagingMode)header.imagingMode == BiplaneMode) {
495 m_porta->setDisplayDimensions(1, header.postScanWidth, header.postScanHeight);
501 if (m_porta->setParam(prmBImageDepth, header.imageDepth) && m_porta->setParam(prmBSector, header.sector) &&
502 m_porta->setParam(prmBTxFreq, header.transmitFrequency) &&
503 m_porta->setParam(prmBSamplingFreq, header.samplingFrequency)) {
506 m_porta->getParam(prmBSector, sector);
509 m_porta->getParam(prmMotorSteps) * (double)(nfo.motorFov / 1000.0) / (double)nfo.motorSteps;
512 m_porta->getParam(prmMotorFrames, fpv);
520 if (!header.postScanMode) {
521 if ((imagingMode)header.imagingMode == BMode) {
522 #if USTK_PORTA_VERSION_MAJOR > 5
527 m_porta->getParam(prmBImageRect, rect);
530 }
else if ((imagingMode)header.imagingMode == RfMode) {
536 if (header.postScanMode) {
539 }
else if (header.imagingMode == BMode) {
542 }
else if (header.imagingMode == RfMode) {
558 QString usNetworkServer::getProbeSettingsFromId(
int probeId)
563 if (usingProbeConfigFile) {
564 bool probeContainedInFile(
false);
565 for (
unsigned int i = 0; i < probeConfigFileNames.size(); i++) {
566 if (probeConfigFileNames.at(i).first == probeId) {
567 settingName = QString::fromStdString(probeConfigFileNames.at(i).second);
568 probeContainedInFile =
true;
571 if (!probeContainedInFile) {
572 throw vpException(vpException::fatalError, std::string(
"Cannot find current probe id in \
573 config file provided with --probeSettingsFile option."));
577 settingName = QString(
"FAST-General (C5-2 60mm).xml");
578 }
else if (probeId == 12) {
579 settingName = QString(
"GEN-General (BPL9-5 55mm).xml");
580 }
else if (probeId == 13) {
581 settingName = QString(
"GEN-General (BPC8-4 10mm).xml");
582 }
else if (probeId == 14) {
583 settingName = QString(
"GEN-General (PAXY).xml");
584 }
else if (probeId == 15) {
585 settingName = QString(
"GEN-General (4DC7-3 40mm).xml");
587 throw vpException(vpException::fatalError, std::string(
"Cannot compute current probe settings filename.\
588 Try to add a probe config file using --probeSettingsFile option at server startup."));
594 void usNetworkServer::writeInitAcquisitionParameters(QDataStream &stream,
int imagingMode,
int probeId)
596 int transmitFrequency;
597 int samplingFrequency;
598 bool postScanMode =
false;
599 int postScanHeight = 0;
600 int postScanWidth = 0;
603 bool activateMotor =
false;
604 int motorPosition = 0;
605 int framesPerVolume = 1;
606 int stepsPerFrame = 0;
607 int transmitFrequencyMin;
608 int samplingFrequencyMin;
609 int imagingModeMin = 0;
612 int motorPositionMin = 0;
613 int framesPerVolumeMin = 1;
614 int stepsPerFrameMin = 0;
615 int transmitFrequencyMax;
616 int samplingFrequencyMax;
617 int imagingModeMax = 27;
620 int motorPositionMax = 0;
621 int framesPerVolumeMax = 0;
622 int stepsPerFrameMax = 0;
626 motorPositionMax = 75;
627 framesPerVolumeMax = 31;
629 m_porta->getParam(prmMotorSteps, stepsPerFrame);
630 m_porta->getParam(prmMotorFrames, framesPerVolume);
633 m_porta->getParam(prmBTxFreq, transmitFrequency);
634 m_porta->getParam(prmBSamplingFreq, samplingFrequency);
635 m_porta->getParam(prmBImageDepth, imageDepth);
636 m_porta->getParam(prmBSector, sector);
641 activateMotor =
true;
644 m_porta->getParamMinMax(prmBTxFreq, transmitFrequencyMin, transmitFrequencyMax);
645 m_porta->getParamMinMax(prmBSamplingFreq, samplingFrequencyMin, samplingFrequencyMax);
646 m_porta->getParamMinMax(prmBImageDepth, imageDepthMin, imageDepthMax);
647 m_porta->getParamMinMax(prmBSector, sectorMin, sectorMax);
649 stream << transmitFrequency;
650 stream << samplingFrequency;
651 stream << imagingMode;
652 stream << postScanMode;
653 stream << postScanHeight;
654 stream << postScanWidth;
655 stream << imageDepth;
657 stream << activateMotor;
658 stream << motorPosition;
659 stream << framesPerVolume;
660 stream << stepsPerFrame;
661 stream << transmitFrequencyMin;
662 stream << samplingFrequencyMin;
663 stream << imagingModeMin;
664 stream << imageDepthMin;
666 stream << motorPositionMin;
667 stream << framesPerVolumeMin;
668 stream << stepsPerFrameMin;
669 stream << transmitFrequencyMax;
670 stream << samplingFrequencyMax;
671 stream << imagingModeMax;
672 stream << imageDepthMax;
674 stream << motorPositionMax;
675 stream << framesPerVolumeMax;
676 stream << stepsPerFrameMax;
679 void usNetworkServer::writeUpdateAcquisitionParameters(QDataStream &stream, usUpdateHeaderIncomming header,
int probeId)
682 int framesPerVolume = 1;
683 int stepsPerFrame = 0;
685 int transmitFrequencyMin;
686 int samplingFrequencyMin;
687 int imagingModeMin = 0;
690 int motorPositionMin = 0;
691 int framesPerVolumeMin = 1;
692 int stepsPerFrameMin = 0;
694 int transmitFrequencyMax;
695 int samplingFrequencyMax;
696 int imagingModeMax = 27;
699 int motorPositionMax = 0;
700 int framesPerVolumeMax = 1;
701 int stepsPerFrameMax = 0;
705 motorPositionMax = 75;
706 framesPerVolumeMax = 31;
708 m_porta->getParam(prmMotorSteps, stepsPerFrame);
709 m_porta->getParam(prmMotorFrames, framesPerVolume);
712 m_porta->getParam(prmBTxFreq, header.transmitFrequency);
713 m_porta->getParam(prmBSamplingFreq, header.samplingFrequency);
714 m_porta->getDisplayDimensions(0, header.postScanWidth, header.postScanHeight);
715 m_porta->getParam(prmBImageDepth, header.imageDepth);
716 m_porta->getParam(prmBSector, header.sector);
721 std::cout <<
"writeUpdateAcquisitionParameters(), motor status = " <<
motorStatus << std::endl;
723 m_porta->getParamMinMax(prmBTxFreq, transmitFrequencyMin, transmitFrequencyMax);
724 m_porta->getParamMinMax(prmBSamplingFreq, samplingFrequencyMin, samplingFrequencyMax);
725 m_porta->getParamMinMax(prmBImageDepth, imageDepthMin, imageDepthMax);
726 m_porta->getParamMinMax(prmBSector, sectorMin, sectorMax);
728 stream << header.transmitFrequency;
729 stream << header.samplingFrequency;
730 stream << header.imagingMode;
731 stream << header.postScanMode;
732 stream << header.postScanHeight;
733 stream << header.postScanWidth;
734 stream << header.imageDepth;
735 stream << header.sector;
736 stream << header.activateMotor;
737 stream << header.motorPosition;
738 stream << framesPerVolume;
739 stream << stepsPerFrame;
740 stream << transmitFrequencyMin;
741 stream << samplingFrequencyMin;
742 stream << imagingModeMin;
743 stream << imageDepthMin;
745 stream << motorPositionMin;
746 stream << framesPerVolumeMin;
747 stream << stepsPerFrameMin;
748 stream << transmitFrequencyMax;
749 stream << samplingFrequencyMax;
750 stream << imagingModeMax;
751 stream << imageDepthMax;
753 stream << motorPositionMax;
754 stream << framesPerVolumeMax;
755 stream << stepsPerFrameMax;
760 void usNetworkServer::writeOnSocketSlot()
765 std::cout <<
"writing header on socket" << std::endl;
768 QDataStream out(&block, QIODevice::WriteOnly);
769 out.setVersion(QDataStream::Qt_5_0);
793 std::cout <<
"writing image on socket" << std::endl;
803 quint64 dataWrittenSize = 0;
806 dataWrittenSize = connectionSoc->write(block);
808 dataWrittenSize = connectionSoc->write(block);
812 std::cout <<
"written data size = " << dataWrittenSize << std::endl;
818 QDataStream out(&block, QIODevice::WriteOnly);
819 out.setVersion(QDataStream::Qt_5_0);
843 std::cout <<
"writing 2nd image" << std::endl;
848 connectionSoc->write(block);
852 void usNetworkServer::quitApp()
854 connectionAboutToClose();
860 usingProbeConfigFile =
true;
864 if (!vpIoTools::checkFilename(configFileName)) {
865 std::cout <<
"file does not exist\n";
868 file.open(configFileName.c_str(), std::ifstream::in);
870 throw vpException(vpException::ioError, std::string(
"Error opening probe config file."));
875 std::string keyNum, keyFileName;
876 std::string::iterator it;
877 while (file.good()) {
878 std::getline(file, keyNum,
' ');
880 keyNum.erase(std::remove(keyNum.begin(), keyNum.end(),
' '), it);
882 probeId = stoi(keyNum);
883 std::getline(file, keyFileName,
'\n');
885 std::pair<int, std::string> newElement;
886 newElement.first = probeId;
887 newElement.second = keyFileName;
889 probeConfigFileNames.push_back(newElement);
898 char *portaFirmware = USTK_PORTA_FIRMWARE_PATH;
899 char *portaSettings = USTK_PORTA_SETTINGS_PATH;
900 char *portaLicense = USTK_PORTA_LICENSE_PATH;
906 bool initPortaSucess = m_porta->init(256 * 1024 * 1024, portaFirmware, portaSettings, portaLicense, portaLut, m_usmVersion,
907 m_pciVersion, highVoltage, 0, 64);
908 if (!initPortaSucess) {
909 std::cout <<
"error initializing porta sdk !" << std::endl;
915 connect(&tcpServer, SIGNAL(newConnection()),
this, SLOT(acceptTheConnection()));
918 QString portNum = QString::number(8080);
919 bool status = tcpServer.listen(QHostAddress::Any, portNum.toUShort());
922 if (status ==
true) {
923 std::cout <<
"TCP server Started\nServer now listening on port# " << portNum.toStdString() << std::endl;
925 std::cout <<
"TCP server start failure" << tcpServer.errorString().toStdString() << std::endl;
928 usingProbeConfigFile =
false;
940 m_usmVersion = usmVersion;
944 m_pciVersion = pciVersion;
void useProbeConfigFile(std::string configFileName)
void setPCIVersion(int pciVersion)
void writeOnSocketFromOtherThread()
void setUSMVersion(int usmVersion)
usNetworkServer(QObject *parent=0)
void writeOnSocketSignal()
unsigned char * secondBiplaneImage
unsigned char * postScanImage
usImageHeader imageHeader