diff options
-rw-r--r-- | metawatch/metawatch.cpp | 76 | ||||
-rw-r--r-- | metawatch/metawatch.h | 16 |
2 files changed, 90 insertions, 2 deletions
diff --git a/metawatch/metawatch.cpp b/metawatch/metawatch.cpp index b79f6fc..b9adad1 100644 --- a/metawatch/metawatch.cpp +++ b/metawatch/metawatch.cpp @@ -344,6 +344,39 @@ void MetaWatch::sendIfNotQueued(const Message& msg) send(msg); } +uint MetaWatch::nvalSize(NvalValue value) +{ + switch (value) { + case ReservedNval: + case LinkKey: + return 0; + case IdleBufferConfiguration: + return 1; + case TimeFormat: + case DateFormat: + return 1; + } + return 0; +} + +void MetaWatch::nvalWrite(NvalValue value, int data) +{ + uint id = static_cast<uint>(value); + int size = nvalSize(value); + Q_ASSERT(size > 0); + + // Do a read operation first to get the current value + // If the current value matches what we want, avoid rewriting it to flash. + Message msg(NvalOperation, QByteArray(3, 0), 2); + + msg.data[0] = id & 0xFF; + msg.data[1] = id >> 8; + msg.data[2] = size; + + _nvals[value] = data; + send(msg); +} + void MetaWatch::setVibrateMode(bool enable, uint on, uint off, uint cycles) { Message msg(SetVibrateMode, QByteArray(6, 0)); @@ -474,6 +507,9 @@ void MetaWatch::handleMessage(const Message &msg) case GetRealTimeClockResponse: handleRealTimeClockMessage(msg); break; + case NvalOperationResponse: + handleNvalOperationMessage(msg); + break; case StatusChangeEvent: handleStatusChangeMessage(msg); break; @@ -500,7 +536,7 @@ void MetaWatch::handleDeviceTypeMessage(const Message &msg) void MetaWatch::handleRealTimeClockMessage(const Message &msg) { - int year = ((msg.data[1] & 0xFF) << 8) | (msg.data[0] & 0xFF); + int year = ((msg.data[0] & 0xFF) << 8) | (msg.data[1] & 0xFF); int month = msg.data[2] & 0xFF; int day = msg.data[3] & 0xFF; QDate d(year, month, day); @@ -510,9 +546,45 @@ void MetaWatch::handleRealTimeClockMessage(const Message &msg) QTime t(hour, minute, second); _watchTime = QDateTime(d, t); + qDebug() << "got time from watch" << _watchTime; + emit dateTimeChanged(); } +void MetaWatch::handleNvalOperationMessage(const Message& msg) +{ + Q_ASSERT(msg.type == NvalOperationResponse); + + if (msg.data.size() < 2) { + qWarning() << "NVAL operation response too short"; + } + + uint id = ((msg.data[1] & 0xFF) << 8) | (msg.data[0] & 0xFF); + NvalValue value = static_cast<NvalValue>(id); + + qDebug() << "nval operation response for value" << value; + + switch (msg.options) { + case 0: // Success + break; + case 1: // Failure + qWarning() << "NVAL operation failed"; + return; + case 0x9: + qWarning() << "NVAL operation failed: Identifier not found"; + return; + case 0xA: + qWarning() << "NVAL operation failed: Operation failed"; + return; + case 0xC: + qWarning() << "NVAL operation failed: Bad Item length"; + return; + default: + qWarning() << "NVAL operation unknown response: " << msg.options; + return; + } +} + void MetaWatch::handleStatusChangeMessage(const Message &msg) { Q_UNUSED(msg); @@ -689,7 +761,7 @@ void MetaWatch::realSend(const Message &msg) data[msgSize+4] = crc & 0xFF; data[msgSize+5] = crc >> 8; - //qDebug() << "Sending" << data.toHex(); + //qDebug() << "sending" << data.toHex(); _socket->write(data); } diff --git a/metawatch/metawatch.h b/metawatch/metawatch.h index 685b2a7..d468bc1 100644 --- a/metawatch/metawatch.h +++ b/metawatch/metawatch.h @@ -61,6 +61,14 @@ public: ReadLightSensorResponse = 0x59 }; + enum NvalValue { + ReservedNval = 0, + LinkKey = 0x1, + IdleBufferConfiguration = 0x2, + TimeFormat = 0x2009, + DateFormat = 0x200a + }; + enum Mode { IdleMode = 0, ApplicationMode = 1, @@ -174,10 +182,14 @@ protected: { } }; + /** The "packets to be sent" asynchronous queue **/ QQueue<Message> _toSend; QTimer* _sendTimer; Message _partialReceived; + /** Pending nvals to be written once the read operation is finished. */ + QMap<NvalValue, int> _nvals; + static const quint8 bitRevTable[16]; static const quint16 crcTable[256]; static quint16 calcCrc(const QByteArray& data, int size); @@ -190,6 +202,9 @@ protected: */ void sendIfNotQueued(const Message& msg); + static uint nvalSize(NvalValue value); + void nvalWrite(NvalValue value, int data); + /* Some functions that wrap sending some watch messages. */ void setVibrateMode(bool enable, uint on, uint off, uint cycles); void updateLcdLine(Mode mode, const QImage& image, int line); @@ -204,6 +219,7 @@ protected: void handleMessage(const Message& msg); void handleDeviceTypeMessage(const Message& msg); void handleRealTimeClockMessage(const Message& msg); + void handleNvalOperationMessage(const Message& msg); void handleStatusChangeMessage(const Message& msg); void handleButtonEventMessage(const Message& msg); void handleBatteryVoltageMessage(const Message& msg); |