summaryrefslogtreecommitdiff
path: root/daemon/unpacker.h
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2014-11-30 17:27:08 +0100
committerJavier <dev.git@javispedro.com>2014-11-30 17:37:19 +0100
commit49f1261bf9d635d5e3d881e87a93ed4e76abfe90 (patch)
tree4432ecd771a3aca533914d02f9947cb497493395 /daemon/unpacker.h
parentd55d1d472d5876f90dd95301d9f3b6bef6f4c494 (diff)
allow receiving responses to commands in watchconnector
* the skeleton is in place for watchconnector to allow query->response messages. I've used call/cc style because it is impossible to make QBluetoothSocket synchronous (waitForReadyRead() is a no-op) * remove watchcommands, instead create musicmanager to listen for the music endpoint. The other (simpler) endpoints are now listened in watchconnector itself. hangupAll() slot is moved to voicecallmanager. * instead of emitting signals for each received message, listeners can now register for receiving messages targeted towards a given endpoint * when reading from bluetoothsocket, properly handle short reads * remove useless 'watch' namespace * create appmanager, which mantains a database of installed apps (installed on the phone, that is; watch installed apps will come later) * all the *Managers are now instantiated by the main Manager itself * introduce Unpacker helper class for decoding watch messages * implement getAppbankStatus and getAppbankUuids messages and response parsers * remove file logging for now (20MB is bad for eMMC!) * use dbus object path /org/pebbled instead of /
Diffstat (limited to 'daemon/unpacker.h')
-rw-r--r--daemon/unpacker.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/daemon/unpacker.h b/daemon/unpacker.h
new file mode 100644
index 0000000..94908cb
--- /dev/null
+++ b/daemon/unpacker.h
@@ -0,0 +1,87 @@
+#ifndef UNPACKER_H
+#define UNPACKER_H
+
+#include <QtEndian>
+#include <QByteArray>
+#include <QString>
+#include <QUuid>
+
+class Unpacker
+{
+public:
+ Unpacker(const QByteArray &data);
+
+ template <typename T>
+ T read();
+
+ QString readFixedString(int n);
+
+ QUuid readUuid();
+
+ void skip(int n);
+
+ bool bad() const;
+
+private:
+ const uchar * p();
+ bool checkBad(int n = 0);
+
+ const QByteArray &_buf;
+ int _offset;
+ bool _bad;
+};
+
+inline Unpacker::Unpacker(const QByteArray &data)
+ : _buf(data), _offset(0), _bad(false)
+{
+}
+
+template <typename T>
+inline T Unpacker::read()
+{
+ if (checkBad(sizeof(T))) return 0;
+ const uchar *u = p();
+ _offset += sizeof(T);
+ return qFromBigEndian<T>(u);
+}
+
+inline QString Unpacker::readFixedString(int n)
+{
+ if (checkBad(n)) return QString();
+ const char *u = &_buf.constData()[_offset];
+ _offset += n;
+ return QString::fromUtf8(u, strnlen(u, n));
+}
+
+inline QUuid Unpacker::readUuid()
+{
+ if (checkBad(16)) return QString();
+ _offset += 16;
+ return QUuid::fromRfc4122(_buf.mid(_offset - 16, 16));
+}
+
+inline void Unpacker::skip(int n)
+{
+ _offset += n;
+ checkBad();
+}
+
+inline bool Unpacker::bad() const
+{
+ return _bad;
+}
+
+inline const uchar * Unpacker::p()
+{
+ return reinterpret_cast<const uchar *>(&_buf.constData()[_offset]);
+}
+
+inline bool Unpacker::checkBad(int n)
+{
+ if (_offset + n > _buf.size()) {
+ _bad = true;
+ }
+ return _bad;
+}
+
+#endif // UNPACKER_H