xbee.h
1 /*
2  * Copyright (C) 2015 ThomArmax (Thomas COIN)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Thomas COIN <esvcorp@gmail.com> 18/04/2015
19  */
20 
21 #ifndef XBEE_H
22 #define XBEE_H
23 
24 #include <QObject>
25 #include <QtSerialPort/QSerialPort>
26 #include <QtSerialPort/QSerialPortInfo>
27 
28 namespace QtXBee {
29 class XBeePacket;
30 class XBeeResponse;
31 class ATCommandResponse;
32 class ATCommand;
33 class ModemStatus;
34 class RemoteATCommandResponse;
35 
36 namespace Wpan {
37 class RxResponse64;
38 class RxResponse16;
39 class TxStatusResponse;
40 }
41 
42 namespace ZigBee {
43 class ZBTxStatusResponse;
44 class ZBRxResponse;
47 }
48 
49 using namespace QtXBee::Wpan;
50 using namespace QtXBee::ZigBee;
51 
52 class XBee : public QObject
53 {
54  Q_OBJECT
55 public:
56  enum Mode {
59  API2Mode
60  };
61 
62  explicit XBee (QObject *parent = 0);
63  XBee (const QString & serialPort, QObject * parent = 0);
64  ~XBee ();
65 
66  bool applyDefaultSerialPortConfig ();
67 
68  bool sendCommandAsync (const QByteArray & command);
69  XBeeResponse * sendSync (XBeePacket * packet);
70  ATCommandResponse * sendATCommandSync (ATCommand * command);
71  ATCommandResponse * sendATCommandSync (const QByteArray & atcommand);
72 
73  QByteArray sendCommandSync (const QByteArray & command);
74  void sendAsync (XBeePacket * packet);
75  void sendATCommandAsync (ATCommand *command);
76  void sendATCommandAsync (const QByteArray & data);
77 
78  bool setMode (const Mode mode);
79  Mode mode () const;
80 
81  bool setSerialPort (const QString & serialPort);
82  bool setSerialPort (const QString &serialPort,
83  const QSerialPort::BaudRate baudRate,
84  const QSerialPort::DataBits dataBits,
85  const QSerialPort::Parity parity,
86  const QSerialPort::StopBits stopBits,
87  const QSerialPort::FlowControl flowControl);
88  bool setSerialPortConfiguration (const QSerialPort::BaudRate baudRate,
89  const QSerialPort::DataBits dataBits,
90  const QSerialPort::Parity parity,
91  const QSerialPort::StopBits stopBits,
92  const QSerialPort::FlowControl flowControl);
93 
94  // Adressing
95  bool setDH (const quint32 dh);
96  bool setDL (const quint32 dl);
97  bool setMY (const quint16 my);
98  bool setMP (const quint16 mp);
99  bool setNC (const quint32 nc);
100  bool setSH (const quint32 sh);
101  bool setSL (const quint32 sl);
102  bool setNI (const QString & ni);
103  bool setSE (const quint8 se);
104  bool setDE (const quint8 de);
105  bool setCI (const quint8 ci);
106  bool setTO (const quint8 to);
107  bool setNP (const quint8 np);
108  bool setDD (const quint16 dd);
109  bool setCR (const quint8 cr);
110 
111  quint32 DH () const { return m_dh;}
112  quint32 DL () const { return m_dl;}
113  quint16 MY () const { return m_my;}
114  quint16 MP () const { return m_mp;}
115  quint32 NC () const { return m_nc;}
116  quint32 SH () const { return m_sh;}
117  quint32 SL () const { return m_sl;}
118  QString NI () const { return m_ni;}
119  quint8 SE () const { return m_se;}
120  quint8 DE () const { return m_de;}
121  quint8 CI () const { return m_ci;}
122  quint8 TO () const { return m_to;}
123  quint8 NO () const { return m_np;}
124  quint16 DD () const { return m_dd;}
125  quint8 CR () const {return m_cr;}
126 
127 signals:
128  void rawDataReceived (const QByteArray & data);
129  void receivedATCommandResponse (QtXBee::ATCommandResponse *response);
130  void receivedModemStatus (QtXBee::ModemStatus *response);
131  void receivedRemoteCommandResponse (QtXBee::RemoteATCommandResponse *response);
132  void receivedTransmitStatus (QtXBee::ZigBee::ZBTxStatusResponse *response);
133  void receivedRxIndicator (QtXBee::ZigBee::ZBRxResponse *response);
134  void receivedRxIndicatorExplicit (QtXBee::ZigBee::ZBExplicitRxResponse *response);
135  void receivedNodeIdentificationIndicator (QtXBee::ZigBee::ZBIONodeIdentificationResponse *response);
136  void receivedTransmitStatus (QtXBee::Wpan::TxStatusResponse *response);
137  void receivedRxResponse16 (QtXBee::Wpan::RxResponse16 * response);
138  void receivedRxResponse64 (QtXBee::Wpan::RxResponse64 * response);
139  // Addressing signals
140  void DHChanged (const quint32 dh);
141  void DLChanged (const quint32 dl);
142  void MYChanged (const quint16 my);
143  void MPChanged (const quint16 mp);
144  void NCChanged (const quint32 nc);
145  void SHChanged (const quint32 sh);
146  void SLChanged (const quint32 sl);
147  void NIChanged (const QString & ni);
148  void SEChanged (const quint8 se);
149  void DEChanged (const quint8 de);
150  void CIChanged (const quint8 ci);
151  void TOChanged (const quint8 to);
152  void NPChanged (const quint8 np);
153  void DDChanged (const quint16 dd);
154  void CRChanged (const quint8 cr);
156 public slots:
157  void loadAddressingProperties();
158  bool open();
159  bool close();
160 
161  void displayATCommandResponse (ATCommandResponse *digiMeshPacket);
162  void displayModemStatus (ModemStatus *digiMeshPacket);
163  void displayTransmitStatus (ZBTxStatusResponse *digiMeshPacket);
164  void displayRxIndicator (ZBRxResponse *digiMeshPacket);
165  void displayRxIndicatorExplicit (ZBExplicitRxResponse *digiMeshPacket);
166  void displayNodeIdentificationIndicator (ZBIONodeIdentificationResponse *digiMeshPacket);
167  void displayRemoteCommandResponse (RemoteATCommandResponse *digiMeshPacket);
168 
169 private slots:
170  void readData ();
171 
172 private:
173  XBeeResponse * processPacket (QByteArray packet, const bool async);
174  void processATCommandRespone (ATCommandResponse *rep);
175  bool startupCheck ();
176  QByteArray synchronousCmd (QByteArray cmd);
177  bool enterInCommandMode ();
178  bool exitCommandMode ();
179 
180 private:
181  QSerialPort * m_serial;
182  bool xbeeFound;
183  Mode m_mode;
184  QByteArray buffer;
185  quint16 m_frameIdCounter;
186 
187  // Adressing
188  quint32 m_dh;
189  quint32 m_dl;
190  quint16 m_my;
191  quint16 m_mp;
192  quint32 m_nc;
193  quint32 m_sh;
194  quint32 m_sl;
195  QString m_ni;
196  quint8 m_se;
197  quint8 m_de;
198  quint8 m_ci;
199  quint8 m_to;
200  quint8 m_np;
201  quint16 m_dd;
202  quint8 m_cr;
203 };
204 
205 } //END namespace
206 
207 #endif