UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-05-17)
usNetworkServer.h
1 #ifndef US_NETWORK_GRABBER_H
2 #define US_NETWORK_GRABBER_H
3 
4 #include <usPortaConfig.h>
5 
6 #include <QtCore/QDataStream>
7 #include <QtCore/QDateTime>
8 #include <QtCore/QFile>
9 #include <QtCore/QString>
10 #include <QtNetwork/QTcpServer>
11 #include <QtNetwork/QTcpSocket>
12 #include <QtWidgets/QApplication>
13 
14 // for URect
15 #define COMPILE_PORTA_DEMO
16 
17 // Ultrasonix SDK includes
18 #include <ImagingModes.h>
19 #include <porta_params_def.h>
20 #include <porta.h>
21 #include <utx_opt.h>
22 
23 #if USTK_PORTA_VERSION_MAJOR > 5
24 #include <porta_def.h>
25 #include <porta_wrapper.h>
26 #define RfMode 12
27 #else
28 #include <porta_std_includes.h>
29 #endif
30 
31 #include <cmath>
32 #include <ctime>
33 #include <fstream>
34 #include <iostream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 #include <usPortaConfig.h>
40 #include <visp3/core/vpException.h>
41 #include <visp3/core/vpIoTools.h>
42 
43 class usNetworkServer : public QObject
44 {
45  Q_OBJECT
46 
47 public:
48  // Following headers must be the same in the client grabber !
49 
50  // header sent by the client to init porta
52  usInitHeaderIncomming() : headerId(1) {} // set header Id to 1 by default
53  int headerId; // to differenciate usInitHeaderIncomming (=1) / usAcquisitionParameters (=2) /
54  // usRunControlHeaderSent(=3)
55 
56  // probe / slot selection
57  int probeId; // available probes : 4DC7 (id = 15) , C5-2 (id = 10)
58  int slotId; // 3 slots on ultrasonix station (0 : top, 2 : middle, 1 : botom)
59 
60  int imagingMode; // see ImagingModes.h
61  };
62 
63  // header sent by the client to update porta config
65  usUpdateHeaderIncomming() : headerId(2) {} // set header Id to 1 by default
66  int headerId; // to differenciate usInitHeaderIncomming (=1) / usUpdateHeaderIncomming (=2)
67 
75  int sector;
80  };
81 
82  // header sent to the client to confirm porta is initialized
84  usInitHeaderConfirmation() : headerId(1) {} // set header Id to 1 by default
85  int headerId; // to differenciate usInitHeaderConfirmation (=1) / usImageHeader (=2)
86 
87  int initOk; // 1 if init ok, 0 otherwise
88  int probeId; // unique code for each probe (4DC7 = 15, C 50-62 = 10)
89  };
90 
91  //
92  struct usImageHeader {
93  usImageHeader() : headerId(2) {} // set header Id to 2 by default
94  int headerId; // to differenciate usInitHeaderConfirmation (=1) / usImageHeader (=2)
95 
96  quint32 frameCount;
97  quint64 timeStamp;
98 
99  double dataRate; // fps
100 
101  int dataLength; // image size in bytes
102  int ss;
103 
105 
108 
109  double pixelWidth; // width of a pixel of a post-scan frame (meters)
110  double pixelHeight; // height of a pixel of a post-scan frame (meters)
111 
114 
117  unsigned int scanLineNumber;
119 
120  double degPerFrame;
122  double motorRadius;
124  };
125 
126  explicit usNetworkServer(QObject *parent = 0);
128 
129  QTcpSocket *getSocket();
130  porta *getPorta();
131 
133 
134  unsigned char *postScanImage;
135 
136  char *beginImage;
137 
139  bool biPlane;
140 
141  // for bi-plane
142  unsigned char *secondBiplaneImage;
143 
145 
147 
148  void useProbeConfigFile(std::string configFileName);
149 
150  void setVerbose();
151 
152  void setUSMVersion(int usmVersion);
153 
154  void setPCIVersion(int pciVersion);
155 
156 public slots:
157  void startServerSlot();
158  void stopServerSlot();
159 
160 signals:
162 
163 private slots:
164  // Called automatically when a client attempts to connect
165  void acceptTheConnection();
166 
167  // Called automatically when client has closed the connection
168  void connectionAboutToClose();
169 
170  // to get the name of the xml settings file corresponding to a probe Id
171  QString getProbeSettingsFromId(int probeId);
172 
173  // Called automatically when data sent by a client is fully available to the server
174  void readIncomingData();
175 
176  void writeOnSocketSlot();
177 
178  void quitApp();
179 
180 private:
181  // Variable(socket) to store listening tcpserver
182  QTcpServer tcpServer;
183 
184  // Variable(socket) to store newly established connection with the client
185  QTcpSocket *connectionSoc;
186 
187  void initPorta(usInitHeaderIncomming header);
188  bool updatePorta(usUpdateHeaderIncomming header);
189  void writeInitAcquisitionParameters(QDataStream &out, int imagingMode, int probeId);
190  void writeUpdateAcquisitionParameters(QDataStream &stream, usUpdateHeaderIncomming header, int probeId);
191 
192  porta *m_porta;
193  imagingMode m_currentImagingMode;
194 
195  usInitHeaderConfirmation confirmHeader;
196 
197  bool initWithoutUpdate;
198 
199  bool usingProbeConfigFile;
200  std::vector<std::pair<int, std::string> > probeConfigFileNames;
201 
202  bool verboseMode;
203 
204  int m_usmVersion;
205  int m_pciVersion;
206 };
207 
208 #endif // US_NETWORK_GRABBER_H
QTcpSocket * getSocket()
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