![]() |
TTPA (Écran)
1.1
BTS SN-IR La Salle Avignon 2018
|
Assure la réception des trames via le Bluetooth.
#include <communicationbluetooth.h>
Types publics | |
enum | Mode { Scrutation, Evenement } |
Mode de gestion du port de communication. Plus de détails... | |
Connecteurs publics | |
void | main () |
Le corps du thread assurant la réception des données via le Bluetooth. | |
void | finir () |
Met fin au Thread. | |
void | terminer () |
Termine le thread. | |
void | lirePort () |
Lit les données disponibles sur le port Bluetooth. | |
void | surveillerConnexion () |
Surveille l'état du service RFCOMM. | |
Signaux | |
void | nouvellesDonneesRecues (QString donneesRecues) |
Signale les données reçues sur le port Blutooth. | |
void | deconnecterJoueur () |
Signale une déconnexion du Bluetooth. | |
void | connecterJoueur () |
Signale une connexion du Bluetooth. | |
void | setNomPeripherique (QString nom) |
Affiche le nom du périférique connecté | |
Fonctions membres publiques | |
CommunicationBluetooth (QString nomPort="rfcomm0", Mode mode=Scrutation, QObject *parent=0) | |
Constructeur. | |
~CommunicationBluetooth () | |
Destructeur. | |
void | ouvrir () |
Ouvre et configure le port série. | |
void | fermer () |
Ferme le port série. | |
int | getEtatRFCOMM () |
Retourne l'état du port RFCOMM. | |
Fonctions membres privées | |
void | msleep (unsigned long sleepMS) |
Temporise l'exécution du Thread. | |
void | annuler () |
Termine la temporisation en cours. | |
void | attendrePeriode () |
Attend une durée PERIODE_SURVEILLANCE. | |
void | demarrerRFCOMM () |
Démarre le service RFCOMM. | |
void | redemarrerRFCOMM () |
Redémarre le service RFCOMM. | |
QString | lireEtatRFCOMM () |
Lit l'état du service RFCOMM. | |
QString | lireEtatServiceRFCOMM () |
void | arreterRFCOMM () |
Arrête le service RFCOMM. | |
void | recupererNomBluetooth () |
Récupère le nom de l'appareil connécté | |
Attributs privés | |
QextSerialPort * | m_pPortSerie |
Agrégation vers la classe QextSerialPort. | |
bool | fini |
état du Thread. | |
QMutex | mutex |
pour gérer les temporisations | |
QWaitCondition | waitCondition |
pour les temporisations | |
int | periode |
la périodicité du thread | |
int | etatRFCOMM |
état du service RFCOMM. | |
QTimer * | timerSurveillance |
pour surveiller périodiquement l'état de la connexsion Bluetooth |
{ Scrutation, Evenement };
CommunicationBluetooth::CommunicationBluetooth | ( | QString | nomPort = "rfcomm0" , |
CommunicationBluetooth::Mode | mode = Scrutation , |
||
QObject * | parent = 0 |
||
) | [explicit] |
nomPort | QString le nom du fichier de périphérique Bluetooth (par défaut rfcomm0) |
parent | QObject Adresse de l'objet Qt parent (par défaut 0) |
Références demarrerRFCOMM(), m_pPortSerie, periode, Scrutation, surveillerConnexion(), et timerSurveillance.
: QObject(parent), fini(false), periode(PERIODE_SURVEILLANCE), etatRFCOMM(RFCOMM_ARRETE) { #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this; #endif demarrerRFCOMM(); // cf. méthode main() if(mode == CommunicationBluetooth::Scrutation) { m_pPortSerie = new QextSerialPort(QextSerialPort::Polling, this); m_pPortSerie->setPortName(QString("/dev/") + nomPort); surveillerConnexion(); #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Mode Polling (par scrutation)"); #endif } else /* CommunicationBluetooth::Evenement */ { m_pPortSerie = new QextSerialPort(QextSerialPort::EventDriven, this); m_pPortSerie->setPortName(QString("/dev/") + nomPort); surveillerConnexion(); timerSurveillance = new QTimer(this); timerSurveillance->setInterval(periode); connect(timerSurveillance, SIGNAL(timeout()), this, SLOT(surveillerConnexion())); timerSurveillance->start(); #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Mode EventDriven (par évènement)"); #endif } }
Références arreterRFCOMM(), et fermer().
{ #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this; #endif fermer(); arreterRFCOMM(); }
CommunicationBluetooth::annuler | ( | ) | [inline, private] |
Référencé par finir().
{ waitCondition.wakeAll(); }
CommunicationBluetooth::arreterRFCOMM | ( | ) | [private] |
Référencé par ~CommunicationBluetooth().
{ FILE *resultat; resultat = popen("sudo systemctl stop rfcomm.service 2> /dev/null", "r"); pclose(resultat); }
CommunicationBluetooth::attendrePeriode | ( | ) | [private] |
Références msleep(), et periode.
Référencé par surveillerConnexion().
CommunicationBluetooth::connecterJoueur | ( | ) | [signal] |
Référencé par surveillerConnexion().
CommunicationBluetooth::deconnecterJoueur | ( | ) | [signal] |
Référencé par surveillerConnexion().
CommunicationBluetooth::demarrerRFCOMM | ( | ) | [private] |
Référencé par CommunicationBluetooth(), et surveillerConnexion().
{ FILE *resultat; resultat = popen("sudo systemctl start rfcomm.service 2> /dev/null", "r"); pclose(resultat); }
Références lirePort(), et m_pPortSerie.
Référencé par surveillerConnexion(), et ~CommunicationBluetooth().
{ #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this; #endif if (m_pPortSerie->isOpen()) { if(m_pPortSerie->queryMode() == QextSerialPort::EventDriven) disconnect(m_pPortSerie, SIGNAL(readyRead()), this, SLOT(lirePort())); m_pPortSerie->close(); } }
CommunicationBluetooth::finir | ( | ) | [slot] |
Références etatRFCOMM.
{ return etatRFCOMM; }
CommunicationBluetooth::lireEtatRFCOMM | ( | ) | [private] |
Référencé par surveillerConnexion().
{ FILE *resultat; char ligne[1024]; QString reponse; // lit l'état de la connexion resultat = popen("rfcomm -a", "r"); fgets(ligne, 1024, resultat); while (! feof(resultat)) { reponse += ligne; fgets(ligne, 1024, resultat); } pclose(resultat); return reponse; }
QString CommunicationBluetooth::lireEtatServiceRFCOMM | ( | ) | [private] |
Référencé par surveillerConnexion().
{ FILE *resultat; char ligne[1024]; QString reponse; // lit l'état de la connexion resultat = popen("sudo systemctl status rfcomm.service 2> /dev/null", "r"); fgets(ligne, 1024, resultat); while (! feof(resultat)) { reponse += ligne; fgets(ligne, 1024, resultat); } pclose(resultat); return reponse; }
CommunicationBluetooth::lirePort | ( | ) | [slot] |
Références m_pPortSerie, msleep(), et nouvellesDonneesRecues().
Référencé par fermer(), main(), et ouvrir().
{ if (!m_pPortSerie->isOpen()) return; QByteArray donnees; // lecture des données disponibles while (m_pPortSerie->bytesAvailable()) { donnees += m_pPortSerie->readAll(); this->msleep(20); // en ms //::usleep(100000); // cf. timeout } QString donneesRecues = QString(QString::fromUtf8(donnees.data())); if(!donneesRecues.isEmpty()) { #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this << QString::fromUtf8("Données reçues : ") << donneesRecues; #endif emit nouvellesDonneesRecues(donneesRecues); } }
CommunicationBluetooth::main | ( | ) | [slot] |
Références fini, lirePort(), m_pPortSerie, et surveillerConnexion().
{ #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this << QString::fromUtf8("Communication Bluetooth démarrée"); #endif if(m_pPortSerie->queryMode() == QextSerialPort::Polling) { // On interroge périodiquement le port de communication et on sureveille l'état de la (connexion) while(!fini) { lirePort(); surveillerConnexion(); } } else { // On crée une boucle d'événements nécessaire pour gérer les signaux (readyRead() et timeout() while(!fini) { QApplication::processEvents(); //QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, periode); } } #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this << QString::fromUtf8("Communication Bluetooth arrêtée"); #endif }
CommunicationBluetooth::msleep | ( | unsigned long | sleepMS | ) | [inline, private] |
sleepMS | unsigned long durée de la temporisation en millisecondes |
Référencé par attendrePeriode(), lirePort(), et surveillerConnexion().
{ waitCondition.wait(&mutex, sleepMS); }
CommunicationBluetooth::nouvellesDonneesRecues | ( | QString | donneesRecues | ) | [signal] |
donneesRecues |
Référencé par lirePort().
Références lirePort(), m_pPortSerie, et recupererNomBluetooth().
Référencé par surveillerConnexion().
{ if (m_pPortSerie->isOpen()) { #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Le port %1 est ouvert").arg(m_pPortSerie->portName()); #endif return; } #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QThread::currentThreadId() << this; #endif // ouverture du port m_pPortSerie->open(QIODevice::ReadWrite); if (!m_pPortSerie->isOpen()) { #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Le port %1 n'est pas ouvert").arg(m_pPortSerie->portName()); #endif return; } else #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Ouverture du port %1 réussie").arg(m_pPortSerie->portName()); #endif // configuration du port m_pPortSerie->setBaudRate(BAUD9600); m_pPortSerie->setDataBits(DATA_8); m_pPortSerie->setParity(PAR_NONE); m_pPortSerie->setStopBits(STOP_1); m_pPortSerie->setFlowControl(FLOW_OFF); if(m_pPortSerie->queryMode() == QextSerialPort::EventDriven) { if (m_pPortSerie->isOpen()) { connect(m_pPortSerie, SIGNAL(readyRead()), this, SLOT(lirePort())); recupererNomBluetooth(); } } }
CommunicationBluetooth::recupererNomBluetooth | ( | ) | [private] |
Références setNomPeripherique().
Référencé par ouvrir().
{ FILE *resultat; char ligne[1024]; QString reponse; resultat = popen("rfcomm -a", "r"); // récupération de l'adresse fgets(ligne, 1024, resultat); while (! feof(resultat)) { reponse += ligne; fgets(ligne, 1024, resultat); } pclose(resultat); QString adresseMAC; if (reponse.size() == 0) { qDebug() << Q_FUNC_INFO << "/!\\ NE PEUT PAS RECUPERER L'ADRESSE MAC /!\\"; return; } adresseMAC = reponse.mid(30, 17); QString commande = "hcitool name " + adresseMAC; reponse = ""; resultat = popen(commande.toAscii(), "r"); // récupération du nom fgets(ligne, 1024, resultat); while (! feof(resultat)) { reponse += ligne; fgets(ligne, 1024, resultat); } pclose(resultat); if (reponse.size() == 0) { qDebug() << Q_FUNC_INFO << "/!\\ NE PEUT PAS RECUPERER LE NOM DE L'APPAREIL /!\\"; return; } qDebug() << Q_FUNC_INFO << "Nom de l'appareil connecte : " << reponse.trimmed(); emit setNomPeripherique(reponse.trimmed()); }
CommunicationBluetooth::redemarrerRFCOMM | ( | ) | [private] |
Référencé par surveillerConnexion().
{ FILE *resultat; resultat = popen("sudo systemctl restart rfcomm.service 2> /dev/null", "r"); pclose(resultat); }
CommunicationBluetooth::setNomPeripherique | ( | QString | nom | ) | [signal] |
nom | QString nom du périférique |
Référencé par recupererNomBluetooth().
CommunicationBluetooth::surveillerConnexion | ( | ) | [slot] |
Références attendrePeriode(), connecterJoueur(), deconnecterJoueur(), demarrerRFCOMM(), etatRFCOMM, fermer(), lireEtatRFCOMM(), lireEtatServiceRFCOMM(), m_pPortSerie, msleep(), ouvrir(), redemarrerRFCOMM(), RFCOMM_ARRETE, RFCOMM_CONNECTE, et RFCOMM_FERME.
Référencé par CommunicationBluetooth(), et main().
{ if(m_pPortSerie->bytesAvailable()) return; attendrePeriode(); QString etat = lireEtatServiceRFCOMM(); if(!etat.isEmpty()) { if(etat.contains("inactive")) { #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Bluetooth rfcomm0 inactif"); #endif demarrerRFCOMM(); //return; etat = lireEtatServiceRFCOMM(); } if(etat.contains("active")) { #ifndef QT_NO_DEBUG //qDebug() << Q_FUNC_INFO << QString::fromUtf8("Bluetooth rfcomm0 actif"); #endif etat = lireEtatRFCOMM(); #ifndef QT_NO_DEBUG //qDebug() << Q_FUNC_INFO << etat << etatRFCOMM; #endif if(!etat.isEmpty()) { if(etat.contains("connected")) { if(etatRFCOMM != RFCOMM_CONNECTE) { etatRFCOMM = RFCOMM_CONNECTE; #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Bluetooth rfcomm0 connecté"); #endif ouvrir(); emit connecterJoueur(); return; } } else if(etat.contains("closed")) { if(etatRFCOMM != RFCOMM_FERME) { etatRFCOMM = RFCOMM_FERME; #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Bluetooth rfcomm0 fermé"); #endif fermer(); this->msleep(100); // en ms redemarrerRFCOMM(); emit deconnecterJoueur(); return; } } } else { if(etatRFCOMM != RFCOMM_ARRETE) { etatRFCOMM = RFCOMM_ARRETE; #ifndef QT_NO_DEBUG qDebug() << Q_FUNC_INFO << QString::fromUtf8("Aucune connexion Bluetooth rfcomm0"); #endif } } } } }
CommunicationBluetooth::terminer | ( | ) | [slot] |
{ }
int CommunicationBluetooth::etatRFCOMM [private] |
Référencé par getEtatRFCOMM(), et surveillerConnexion().
bool CommunicationBluetooth::fini [private] |
QextSerialPort* CommunicationBluetooth::m_pPortSerie [private] |
Référencé par CommunicationBluetooth(), fermer(), lirePort(), main(), ouvrir(), et surveillerConnexion().
QMutex CommunicationBluetooth::mutex [private] |
int CommunicationBluetooth::periode [private] |
Référencé par attendrePeriode(), et CommunicationBluetooth().
QTimer* CommunicationBluetooth::timerSurveillance [private] |
Référencé par CommunicationBluetooth().
QWaitCondition CommunicationBluetooth::waitCondition [private] |