Afficheur-AREA  1.1
BTS SNIR LaSalle Avignon 2021
Communication.cpp
Aller à la documentation de ce fichier.
1 #include "Communication.h"
2 #include "Rencontre.h"
3 #include <QtDebug>
4 
21 Communication::Communication(QObject *parent) : QObject(parent), serveur(nullptr), socket(nullptr)
22 {
23  qDebug() << Q_FUNC_INFO;
24 
25  initialiser();
26 }
27 
34 {
35  qDebug() << Q_FUNC_INFO;
36 
37  arreter();
38 }
39 
46 {
47  // vérifier la présence du Bluetooth
48  if (peripheriqueLocal.isValid())
49  {
50  // activer le bluetooth
51  peripheriqueLocal.powerOn();
52 
53  // récupérer le nom du périphérique local
55  qDebug() << Q_FUNC_INFO << nomPeripheriqueLocal;
56 
57  // rendre le périphérique local découvrable et jumelable
58  peripheriqueLocal.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
59 
60  // connecter les signaux et les slots
61  connect(&peripheriqueLocal, SIGNAL(deviceConnected(QBluetoothAddress)), this, SLOT(connecterTerminalMobile(QBluetoothAddress)));
62  connect(&peripheriqueLocal, SIGNAL(deviceDisconnected(QBluetoothAddress)), this, SLOT(deconnecterTerminalMobile(QBluetoothAddress)));
63  connect(&peripheriqueLocal, SIGNAL(error(QBluetoothLocalDevice::Error)), this, SLOT(recevoirErreurBluetooth(QBluetoothLocalDevice::Error)));
64  }
65  else
66  qDebug() << Q_FUNC_INFO << "Bluetooth non disponible !";
67 }
68 
75 {
76  // vérifier la présence du Bluetooth
77  if (peripheriqueLocal.isValid() && serveur == nullptr)
78  {
79  // créer une socket serveur
80  serveur = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
81 
82  // connecter les signaux et les slots
83  connect(serveur, SIGNAL(newConnection()), this, SLOT(connecterSocket()));
84 
85  // placer le serveur en écoute
86  QBluetoothUuid uuid = QBluetoothUuid(uuidService);
87  serviceInfo = serveur->listen(uuid, nomService);
88 
89  qDebug() << Q_FUNC_INFO << "Attente de connexion";
90  }
91  else
92  qDebug() << Q_FUNC_INFO << "Bluetooth non disponible !";
93 }
94 
101 {
102  if (serveur == nullptr)
103  return;
104 
105  if (socket != nullptr)
106  {
108  }
109 
110  delete serveur;
111  serveur = nullptr;
112  qDebug() << Q_FUNC_INFO;
113 
114  peripheriqueLocal.setHostMode(QBluetoothLocalDevice::HostPoweredOff);
115 }
116 
123 {
124  socket = serveur->nextPendingConnection();
125  if (!socket)
126  return;
127 
128  qDebug() << Q_FUNC_INFO;
129  socket->open(QIODevice::ReadOnly);
130  connect(socket, SIGNAL(readyRead()), this, SLOT(lireTrame()));
131 
132  connect(socket, SIGNAL(readyRead()), this, SLOT(lireTrame()));
133  connect(socket, SIGNAL(disconnected()), this, SLOT(deconnecterSocket()));
134  connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(recevoirErreurSocket(QBluetoothSocket::SocketError)));
135 
136  emit socketConnectee();
137 }
138 
145 {
146  if (socket->isOpen())
147  socket->close();
148  delete socket;
149  socket = nullptr;
150  qDebug() << Q_FUNC_INFO;
151 
152  emit socketDeconnectee();
153 }
154 
161 void Communication::recevoirErreurSocket(QBluetoothSocket::SocketError erreurSocket)
162 {
163  qDebug() << Q_FUNC_INFO << erreurSocket;
164 }
165 
172 void Communication::connecterTerminalMobile(const QBluetoothAddress &adresse)
173 {
174  if (peripheriqueLocal.pairingStatus(adresse) == QBluetoothLocalDevice::Paired || peripheriqueLocal.pairingStatus(adresse) == QBluetoothLocalDevice::AuthorizedPaired)
175  {
176  qDebug() << Q_FUNC_INFO << adresse.toString() << "appairé";
177  }
178  else
179  qDebug() << Q_FUNC_INFO << adresse.toString() << "non appairé";
180 }
181 
188 void Communication::deconnecterTerminalMobile(const QBluetoothAddress &adresse)
189 {
190  qDebug() << Q_FUNC_INFO << adresse;
191 }
192 
199 void Communication::recevoirErreurBluetooth(QBluetoothLocalDevice::Error erreurBluetooth)
200 {
201  qDebug() << Q_FUNC_INFO << erreurBluetooth;
202 }
203 
204 
211 {
212  QByteArray donnees;
213 
214  donnees = socket->readAll();
215  qDebug() << Q_FUNC_INFO << donnees;
216 
217  trame += QString(donnees.data());
218  qDebug() << Q_FUNC_INFO << trame;
219 
220  if(trame.startsWith(ENTETE_TRAME) && trame.endsWith("\r\n"))
221  {
222  QStringList trames = trame.split("\r\n", QString::SkipEmptyParts);
223  qDebug() << Q_FUNC_INFO << trames;
224 
225  for(int i = 0; i < trames.count(); ++i)
226  {
227  qDebug() << Q_FUNC_INFO << i << trames[i];
228  infosTrame = trames[i].split(";");
230  }
231 
232  trame.clear();
233  }
234 }
235 
244 {
245  if(infosTrame.count() < CHAMP_TYPE_TRAME)
246  return false;
247 
248  qDebug() << Q_FUNC_INFO << infosTrame;
249 
250  switch(infosTrame[CHAMP_TYPE_TRAME].toInt())
251  {
253  emit(debutRencontre(infosTrame[NOM_CLUB_1], infosTrame[NOM_CLUB_2]));
254  break;
255 
257  if (infosTrame.count() == NB_CHAMPS_PARTIE_SIMPLE)
258  emit(creationPartieSimple(infosTrame));
259  else
260  emit(creationPartieDouble(infosTrame));
261  break;
262 
263  case TypeTrame::SCORE:
264  emit(nouveauScorePartie(infosTrame[ID_PARTIE].toInt(), infosTrame[POINTS_JOUEUR_A].toInt(), infosTrame[POINTS_JOUEUR_B].toInt(), infosTrame[MANCHES_JOUEUR_A].toInt(), infosTrame[MANCHES_JOUEUR_B].toInt()));
265  break;
266 
268  emit(changementEtatPartie(infosTrame[ID_PARTIE].toInt(),infosTrame[ETAT]));
269  break;
270 
271  case TypeTrame::NET:
272  emit(detectionNET(infosTrame[ID_PARTIE].toInt()));
273  break;
274 
276  emit(demandeTempsMort());
277  break;
278 
279  default:
280  qDebug() << Q_FUNC_INFO << "Type de trame inconnu !";
281  return false;
282  }
283 
284  return true;
285 }
286 
293 QString Communication::getTrame() const
294 {
295  return trame;
296 }
QString trame
Une trame extraite.
Definition: Communication.h:71
void recevoirErreurBluetooth(QBluetoothLocalDevice::Error erreurBluetooth)
Méthode slot de débug qui signale une erreur de Bluetooth.
#define POINTS_JOUEUR_A
Definition: Communication.h:27
#define MANCHES_JOUEUR_B
Definition: Communication.h:30
#define NOM_CLUB_1
Definition: Communication.h:22
void lireTrame()
Méthode slot qui lit les trames recues et les séparent en plusieurs trames afin de les traiter...
void recevoirErreurSocket(QBluetoothSocket::SocketError erreurSocket)
Méthode slot de débug qui signale une erreur du socket.
#define MANCHES_JOUEUR_A
Definition: Communication.h:29
QString nomPeripheriqueLocal
Nom du périphérique local.
Definition: Communication.h:70
~Communication()
Déstructeur de la classe Communication.
Déclaration de la classe Rencontre.
void changementEtatPartie(int idPartie, QString etatPartie)
Signal de début ou de fin de partie.
#define NB_CHAMPS_PARTIE_SIMPLE
Definition: Communication.h:34
static const QString uuidService(QStringLiteral("0000110a-0000-1000-8000-00805f9b34fb"))
void deconnecterTerminalMobile(const QBluetoothAddress &adresse)
Méthode slot de débug qui signale la déconnexion d&#39;un terminal mobile.
void detectionNET(int idPartie)
Signal de détection d&#39;une sequence de net.
void demarrer()
Méthode qui démarre le serveur Bluetooth.
bool traiterTrame(QStringList infosTrame)
Méthode qui raite la trame recue selon le champ TypeTrame.
QBluetoothServiceInfo serviceInfo
Informations sur le service bluetooth.
Definition: Communication.h:69
QBluetoothServer * serveur
Le serveur Bluetooth.
Definition: Communication.h:66
QString getTrame() const
Méthode qui retourne la derniere trame recue.
Déclaration de la classe Communication.
void socketDeconnectee()
Signal de déconnextion de socket.
void connecterSocket()
Méthode slot qui permet la connection au socket du serveur et connecte les signaux et slots...
QBluetoothSocket * socket
La socket de communication Bluetooth.
Definition: Communication.h:67
#define POINTS_JOUEUR_B
Definition: Communication.h:28
QString trames
Les trames recues.
Definition: Communication.h:72
void creationPartieDouble(QStringList infoTrame)
Signal de création d&#39;une partie double.
void creationPartieSimple(QStringList infoTrame)
Signal de création d&#39;une partie simple.
QBluetoothLocalDevice peripheriqueLocal
L&#39;interface Bluetooth de la Raspberry Pi.
Definition: Communication.h:68
#define ETAT
Definition: Communication.h:32
#define ID_PARTIE
Definition: Communication.h:25
void nouveauScorePartie(int idPartie, int scoreA, int scoreB, int nbManchesJoueurA, int nbManchesJoueurB)
Signal de rafraichissement du score d&#39;une partie.
QStringList infosTrame
Les informations transmises dans la trame.
Definition: Communication.h:73
void socketConnectee()
Signal de connextion de socket.
void arreter()
Méthode qui arrête le serveur Bluetooth.
static const QString nomService(QStringLiteral("Afficheur_AREA"))
void initialiser()
Méthode qui prépare la connexion Bluetooth en mode serveur.
void demandeTempsMort()
Signal de demande de temps mort.
void debutRencontre(QString club1, QString club2)
Signal de début de rencontre.
#define NOM_CLUB_2
Definition: Communication.h:23
#define CHAMP_TYPE_TRAME
Definition: Communication.h:20
Communication(QObject *parent=nullptr)
Constructeur de la classe Communication.
#define ENTETE_TRAME
Definition: Communication.h:19
void deconnecterSocket()
Méthode slot de déconnexion du socket.
void connecterTerminalMobile(const QBluetoothAddress &adresse)
Méthode slot de débug qui signale la connextion d&#39;un teminal mobile.