ITS
App2Fac.cpp
Go to the documentation of this file.
1 #include "include/App2Fac.h"
2 
3 App2Fac::App2Fac(QBluetoothServer *server, QBluetoothSocket *bt, ParameterDENM *denm)
4 {
5  rfcommServer = server;
6  btSocket = bt;
7  udpSocket = new QUdpSocket;
8 
9  udpSocket->open(QIODevice::WriteOnly);
10 // port = denm->reqPort();
11 // indicationAddress = QHostAddress(denm->eventIndicationAddress());
12 // indicationPort = denm->eventIndicationPort();
13 
14  QObject::connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
15 
16 }
17 
19 {
20  QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
21  if (!socket)
22  {
23  qWarning() << "Can't read bluetooth message";
24  return;
25  }
26  QNetworkDatagram datagram;
27 
28  datagram.setDestination(QHostAddress::LocalHost, port);
29 
30  qInfo() << "Reading data";
31  datagram.setData(socket->readAll());
32  qInfo() << datagram.data().toHex() << "received";
33  udpSocket->writeDatagram(datagram);
34 
35  int msgLen = -1;
36  char msgRep[MSG_LENGTH];
37  QElapsedTimer timer;
38  ActionID_t defaultAID;
39 
40  defaultAID.originatingStationID = 0;
41  defaultAID.sequenceNumber = 0;
42 
43  timer.start();
44 
45  while(msgLen == -1 && timer.elapsed() < 10000)
46  msgLen = udpSocket->readDatagram(msgRep, MSG_LENGTH);
47 
48  if(msgLen != -1)
49  {
50  AppDenmResultData result(QByteArray(msgRep, msgLen));
51 
52  if(result.failureNotification())
53  denmTriggerResult(false, result.actionID());
54  else
55  denmTriggerResult(true, result.actionID());
56  }
57  else
58  denmTriggerResult(false, defaultAID);
59 }
60 
62 {
63 // printf("New client connected\n");
64  btSocket = rfcommServer->nextPendingConnection();
65  if (!btSocket)
66  {
67 // qWarning() << "Can't get next connection";
68  return;
69  }
70 
71  qInfo() << "Bluetooth socket initialized";
72 
73  QObject::connect(btSocket, SIGNAL(readyRead()), this, SLOT(sendMessageToDENBasicService()));
74  QObject::connect(btSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
75 
76  emit newBTSocket(btSocket);
77 }
78 
80 {
81  QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
82  if (!socket)
83  return;
84  qDebug() << socket->peerAddress().toString() << "disconnect :(";
85 
86  if(btSocket)
87  {
88  if(btSocket->isOpen())
89  btSocket->close();
90  }
91 
92  socket->deleteLater();
93 }
94 
95 void App2Fac::denmTriggerResult(bool success, ActionID_t actionID)
96 {
97  quint8 mt = 0x11;
98  quint8 result = success;
99  QByteArray res;
100  QDataStream ds(&res, QIODevice::WriteOnly);
101 
102  ds << mt << result << (quint32)actionID.originatingStationID << (quint16)actionID.sequenceNumber;
103 
104  udpSocket->writeDatagram(QNetworkDatagram(res, indicationAddress, indicationPort));
105 }
#define MSG_LENGTH
StationID_t originatingStationID
Definition: ActionID.h:24
QUdpSocket * udpSocket
Definition: App2Fac.h:32
e_failureNotification * failureNotification() const
failureNotification Failure notitfication getter.
void denmTriggerResult(bool success, ActionID_t actionID)
Definition: App2Fac.cpp:95
SequenceNumber_t sequenceNumber
Definition: ActionID.h:25
void clientConnected()
Definition: App2Fac.cpp:61
void newBTSocket(QBluetoothSocket *bt)
App2Fac(QBluetoothServer *server, QBluetoothSocket *bt, ParameterDENM *denm)
Definition: App2Fac.cpp:3
QBluetoothServer * rfcommServer
Definition: App2Fac.h:33
void clientDisconnected()
Definition: App2Fac.cpp:79
QBluetoothSocket * btSocket
Definition: App2Fac.h:34
void sendMessageToDENBasicService()
Definition: App2Fac.cpp:18
ActionID_t actionID() const
actionID ActionID getter.
quint16 port
Definition: App2Fac.h:35
quint16 indicationPort
Definition: App2Fac.h:36
QHostAddress indicationAddress
Definition: App2Fac.h:37