1 | /*********************************************************************
|
---|
2 | // created: 2014/02/02
|
---|
3 | // filename: SickSocket.h
|
---|
4 | //
|
---|
5 | // author: Gerald Dherbomez
|
---|
6 | // Modified by Cyril Fougeray
|
---|
7 | // Copyright Heudiasyc UMR UTC/CNRS 6599
|
---|
8 | //
|
---|
9 | // version: $Id: $
|
---|
10 | //
|
---|
11 | // purpose: Defintion of the SickSocket class
|
---|
12 | // Management of the socket connection with Sick sensors
|
---|
13 | *********************************************************************/
|
---|
14 |
|
---|
15 | #ifndef _SICKSOCKET_H_
|
---|
16 | #define _SICKSOCKET_H_
|
---|
17 |
|
---|
18 | #include <cmath>
|
---|
19 | #include <QObject>
|
---|
20 | #include <QTcpSocket>
|
---|
21 | #include <QMutex>
|
---|
22 | #include <QEvent>
|
---|
23 |
|
---|
24 | #include "Pacpus/kernel/road_time.h"
|
---|
25 |
|
---|
26 | namespace pacpus {
|
---|
27 |
|
---|
28 | class AbstractSickSensor;
|
---|
29 |
|
---|
30 | // Sensors classes :
|
---|
31 | class SickLDMRSSensor;
|
---|
32 | class SickLMSSensor;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @brief The SickFrame class
|
---|
36 | */
|
---|
37 | class SickFrame
|
---|
38 | {
|
---|
39 | public:
|
---|
40 | /**
|
---|
41 | * @brief SickFrame constructor
|
---|
42 | */
|
---|
43 | SickFrame()
|
---|
44 | {
|
---|
45 | size = 0;
|
---|
46 | time = 0;
|
---|
47 | msg = NULL;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// Destructor
|
---|
51 | ~SickFrame()
|
---|
52 | {
|
---|
53 | delete[] msg; // check for NULL is not necessary
|
---|
54 | }
|
---|
55 |
|
---|
56 | qint64 size; //!< Size of incoming packet.
|
---|
57 | road_time_t time; //!< Time when packet is received.
|
---|
58 | char * msg; //!< Packet (raw data).
|
---|
59 | };
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * @brief The SickFrameEvent class
|
---|
63 | * QEvent that encapsulates packets.
|
---|
64 | */
|
---|
65 | class SickFrameEvent
|
---|
66 | : public QEvent
|
---|
67 | {
|
---|
68 | public:
|
---|
69 | /// Constructor
|
---|
70 | SickFrameEvent()
|
---|
71 | : QEvent((QEvent::Type)(QEvent::User + 522))
|
---|
72 | {}
|
---|
73 |
|
---|
74 | /// Destructor
|
---|
75 | ~SickFrameEvent()
|
---|
76 | {}
|
---|
77 |
|
---|
78 | /// Packet data
|
---|
79 | SickFrame * frame;
|
---|
80 | };
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @brief The SickSocket class
|
---|
84 | * Handles the ethernet connection with the remote sensor.
|
---|
85 | */
|
---|
86 | class SickSocket
|
---|
87 | : public QObject
|
---|
88 | {
|
---|
89 | Q_OBJECT
|
---|
90 |
|
---|
91 | public:
|
---|
92 | /// Constructor
|
---|
93 | SickSocket(AbstractSickSensor * parent);
|
---|
94 |
|
---|
95 | /// Destructor
|
---|
96 | ~SickSocket();
|
---|
97 |
|
---|
98 | public Q_SLOTS:
|
---|
99 | /// Enable the connection to the server
|
---|
100 | void connectToServer(QString host, int port);
|
---|
101 |
|
---|
102 | /// Warns about connection of the socket and launch configuration of the socket.
|
---|
103 | int socketConnected();
|
---|
104 |
|
---|
105 | /** Called when incoming data is received. Create an event and send it to the sensor's handler. @see AbstractSickComponent */
|
---|
106 | void socketReadyRead();
|
---|
107 |
|
---|
108 | /// Close the connection with the server
|
---|
109 | void closeSocket() { socket->close(); }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @brief sendToServer Sends data to the remote lidar.
|
---|
113 | * @param data Data to be sent, translated in ASCII.
|
---|
114 | */
|
---|
115 | void sendToServer(QString data);
|
---|
116 | Q_SIGNALS:
|
---|
117 | /// Asked for configuring sensor.
|
---|
118 | void configuration();
|
---|
119 |
|
---|
120 | protected slots:
|
---|
121 | /// Says to the user the connection is closed.
|
---|
122 | void socketConnectionClosed();
|
---|
123 |
|
---|
124 | /// Warns the user an error occured
|
---|
125 | void socketError(QAbstractSocket::SocketError e);
|
---|
126 |
|
---|
127 | private:
|
---|
128 | /// Socket
|
---|
129 | QTcpSocket *socket;
|
---|
130 |
|
---|
131 | /// Mutex to use socket resource.
|
---|
132 | QMutex mutex;
|
---|
133 |
|
---|
134 | /// Parent (contains slots to connect to in order to pass the received data).
|
---|
135 | AbstractSickSensor *myParent;
|
---|
136 | };
|
---|
137 |
|
---|
138 | } // namespace pacpus
|
---|
139 |
|
---|
140 | #endif // _SICKSOCKET_H_
|
---|