diff options
Diffstat (limited to 'metawatch')
-rw-r--r-- | metawatch/metawatch.cpp | 199 | ||||
-rw-r--r-- | metawatch/metawatch.h | 47 | ||||
-rw-r--r-- | metawatch/metawatch.pro | 5 | ||||
-rw-r--r-- | metawatch/metawatchdigital.cpp | 4 | ||||
-rw-r--r-- | metawatch/metawatchdigital.h | 2 | ||||
-rw-r--r-- | metawatch/metawatchdigitalsimulator.cpp | 4 | ||||
-rw-r--r-- | metawatch/metawatchscanner.cpp | 18 | ||||
-rw-r--r-- | metawatch/metawatchscanner.h | 12 |
8 files changed, 47 insertions, 244 deletions
diff --git a/metawatch/metawatch.cpp b/metawatch/metawatch.cpp index 782e47f..7487c13 100644 --- a/metawatch/metawatch.cpp +++ b/metawatch/metawatch.cpp @@ -13,10 +13,6 @@ const char MetaWatch::btnToWatch[8] = { 0, 1, 2, 3, 5, 6, -1, -1 }; -const int MetaWatch::connectRetryTimes[] = { - 5, 10, 30, 60, 120, 300 -}; - const quint8 MetaWatch::bitRevTable[16] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 }; @@ -79,23 +75,17 @@ const quint16 MetaWatch::crcTable[256] = { #endif MetaWatch::MetaWatch(ConfigKey* settings, QObject* parent) : - Watch(parent), + BluetoothWatch(QBluetoothAddress(settings->value("address").toString()), parent), _settings(settings->getSubkey(QString(), this)), _idleTimer(new QTimer(this)), _ringTimer(new QTimer(this)), _watchTime(), _watchBattery(0), _watchCharging(false), _currentMode(IdleMode), _paintMode(IdleMode), _paintEngine(0), - _connectRetries(0), _connected(false), - _connectTimer(new QTimer(this)), - _connectAlignedTimer(new QSystemAlignedTimer(this)), - _localDev(new QBluetoothLocalDevice(this)), - _socket(0), _sendTimer(new QTimer(this)) { // Read current device settings connect(_settings, SIGNAL(subkeyChanged(QString)), SLOT(settingChanged(QString))); - _address = QBluetoothAddress(settings->value("address").toString()); _notificationTimeout = settings->value("notification-timeout", 15).toInt(); _24hMode = settings->value("24h-mode", false).toBool(); _dayMonthOrder = settings->value("day-month-order", false).toBool(); @@ -113,34 +103,12 @@ MetaWatch::MetaWatch(ConfigKey* settings, QObject* parent) : _ringTimer->setInterval(DelayBetweenRings); connect(_ringTimer, SIGNAL(timeout()), SLOT(timedRing())); - _connectTimer->setSingleShot(true); - _connectAlignedTimer->setSingleShot(true); - connect(_connectTimer, SIGNAL(timeout()), SLOT(timedReconnect())); - connect(_connectAlignedTimer, SIGNAL(timeout()), SLOT(timedReconnect())); - _sendTimer->setInterval(DelayBetweenMessages); connect(_sendTimer, SIGNAL(timeout()), SLOT(timedSend())); - - // Connect other signals - connect(_localDev, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), SLOT(localDevModeChanged(QBluetoothLocalDevice::HostMode))); - - // Check to see if we can connect right away - if (_localDev->isValid() && - _localDev->hostMode() != QBluetoothLocalDevice::HostPoweredOff) { - // Do an initial connection attempt after a short delay - // (To give time for other plugins to initialize, etc.) - scheduleConnect(); - } else { - qDebug() << "Not starting MetaWatch connection because BT is off"; - } } MetaWatch::~MetaWatch() { - if (_socket) { - _socket->close(); - delete _socket; - } if (_paintEngine) { delete _paintEngine; } @@ -165,11 +133,6 @@ QStringList MetaWatch::buttons() const return _buttonNames; } -bool MetaWatch::isConnected() const -{ - return _connected; -} - bool MetaWatch::busy() const { return !_connected || @@ -299,6 +262,26 @@ void MetaWatch::ungrabButton(Mode mode, Button button) disableButton(mode, button, PressAndRelease); } +void MetaWatch::setupBluetoothWatch() +{ + _partialReceived.type = NoMessage; + _partialReceived.data.clear(); + _currentMode = IdleMode; + _paintMode = IdleMode; + + // Configure the watch according to user preferences + updateWatchProperties(); + + // Sync watch date & time + setDateTime(QDateTime::currentDateTime()); +} + +void MetaWatch::desetupBluetoothWatch() +{ + _toSend.clear(); + _sendTimer->stop(); +} + quint16 MetaWatch::calcCrc(const QByteArray &data, int size) { quint16 remainder = 0xFFFF; @@ -327,66 +310,6 @@ quint16 MetaWatch::calcCrc(const Message& msg) return calcCrc(data, msgSize + 4); } -void MetaWatch::scheduleConnect() -{ - if (_connected || - _connectAlignedTimer->isActive() || _connectTimer->isActive()) { - // Already connected or already scheduled to connect. - return; - } - - _connectRetries = 0; - _connectTimer->start(100); -} - -void MetaWatch::scheduleRetryConnect() -{ - if (_connected || - _connectAlignedTimer->isActive() || _connectTimer->isActive()) { - // Already connected or already scheduled to connect. - return; - } - - int timeToNextRetry; - if (_connectRetries >= connectRetryTimesSize) { - timeToNextRetry = connectRetryTimes[connectRetryTimesSize - 1]; - } else { - timeToNextRetry = connectRetryTimes[_connectRetries]; - _connectRetries++; // Increase the number of connection attemps - } - - qDebug() << "Backing off for" << timeToNextRetry << "seconds for next retry"; - _connectAlignedTimer->start(timeToNextRetry / 2, timeToNextRetry * 2); - if (_connectAlignedTimer->lastError() != QSystemAlignedTimer::NoError) { - // Hopefully a future version of QSystemAlignedTimer implements this fallback - // For now, we have to do it ourselves. - qDebug() << "Note: using plain QTimer for retry"; - _connectTimer->start(timeToNextRetry * 1000); - } -} - -void MetaWatch::unscheduleConnect() -{ - _connectAlignedTimer->stop(); - _connectTimer->stop(); -} - -void MetaWatch::connectToWatch() -{ - delete _socket; //Delete socket from previous connect if any. - _socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket); - - connect(_socket, SIGNAL(connected()), SLOT(socketConnected())); - connect(_socket, SIGNAL(disconnected()), SLOT(socketDisconnected())); - connect(_socket, SIGNAL(readyRead()), SLOT(socketData())); - connect(_socket, SIGNAL(error(QBluetoothSocket::SocketError)), - SLOT(socketError(QBluetoothSocket::SocketError))); - connect(_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), - SLOT(socketState(QBluetoothSocket::SocketState))); - - _socket->connectToService(_address, 1, QIODevice::ReadWrite | QIODevice::Unbuffered); -} - void MetaWatch::send(const Message &msg) { _toSend.enqueue(msg); @@ -706,89 +629,11 @@ void MetaWatch::settingChanged(const QString &key) } } -void MetaWatch::localDevModeChanged(QBluetoothLocalDevice::HostMode state) -{ - qDebug() << "Local bluetooth device mode changed to" << state; - if (state == QBluetoothLocalDevice::HostPoweredOff) { - // Host bluetooth was powered down - // Assume the socket has been disconnected - socketDisconnected(); - // Cancel any pending connection attempts - unscheduleConnect(); - } else { - // Host bluetooth might have been powered up - if (!_connected) { - scheduleConnect(); - } - } -} - -void MetaWatch::socketConnected() -{ - if (!_connected) { - qDebug() << "connected"; - - _connected = true; - _connectRetries = 0; - _partialReceived.type = NoMessage; - _partialReceived.data.clear(); - _currentMode = IdleMode; - _paintMode = IdleMode; - - // Configure the watch according to user preferences - updateWatchProperties(); - - // Sync watch date & time - setDateTime(QDateTime::currentDateTime()); - - // Call the MetaWatch Model-specific setup routines - handleWatchConnected(); - - emit connected(); - } -} - -void MetaWatch::socketDisconnected() -{ - // Signal disconnection if necessary - if (_connected) { - qDebug() << "disconnected"; - - _connected = false; - _toSend.clear(); - _sendTimer->stop(); - - emit disconnected(); - } - - // Setup reconnection attempt if necessary - if (_localDev->hostMode() != QBluetoothLocalDevice::HostPoweredOff) { - scheduleRetryConnect(); - } -} - -void MetaWatch::socketData() +void MetaWatch::dataReceived() { realReceive(false); } -void MetaWatch::socketError(QBluetoothSocket::SocketError error) -{ - qWarning() << "Socket error:" << error; - // Seems that sometimes a disconnection event may not be generated. - socketDisconnected(); -} - -void MetaWatch::socketState(QBluetoothSocket::SocketState state) -{ - qDebug() << "socket is in" << state; -} - -void MetaWatch::timedReconnect() -{ - connectToWatch(); -} - void MetaWatch::timedSend() { // If there are packets to be sent... diff --git a/metawatch/metawatch.h b/metawatch/metawatch.h index a9cf58b..d671635 100644 --- a/metawatch/metawatch.h +++ b/metawatch/metawatch.h @@ -3,12 +3,12 @@ #include <QtCore/QQueue> #include <QtCore/QTimer> -#include <QtCore/QSettings> #include <QtConnectivity/QBluetoothAddress> #include <QtConnectivity/QBluetoothSocket> #include <QtConnectivity/QBluetoothLocalDevice> #include <QtSystemInfo/QSystemAlignedTimer> #include <sowatch.h> +#include <sowatchbt.h> namespace sowatch { @@ -20,7 +20,7 @@ using QTM_PREPEND_NAMESPACE(QBluetoothLocalDevice); class MetaWatchPaintEngine; -class MetaWatch : public Watch +class MetaWatch : public BluetoothWatch { Q_OBJECT @@ -99,7 +99,7 @@ public: QString model() const; QStringList buttons() const; - bool isConnected() const; + bool busy() const; void setDateTime(const QDateTime& dateTime); @@ -177,40 +177,22 @@ protected: /** The framebuffers for each of the watch modes */ QImage _image[3]; - // Timers to retry the connection when the watch is not found. - static const int connectRetryTimesSize = 6; - static const int connectRetryTimes[connectRetryTimesSize]; - short _connectRetries; - bool _connected; - QTimer* _connectTimer; - QSystemAlignedTimer* _connectAlignedTimer; - - // Connection stuff - QBluetoothLocalDevice* _localDev; - QBluetoothAddress _address; - QBluetoothSocket* _socket; - /** The "packets to be sent" asynchronous queue **/ QQueue<Message> _toSend; QTimer* _sendTimer; Message _partialReceived; - /** Used to calculate CRC */ + // Watch connect/disconnect handling + void setupBluetoothWatch(); + void desetupBluetoothWatch(); + + // Message passing + /** Used to calculate CRC fields in message header */ static const quint8 bitRevTable[16]; static const quint16 crcTable[256]; static quint16 calcCrc(const QByteArray& data, int size); static quint16 calcCrc(const Message& msg); - /** Start the initial connection attempt, reset failed connection timers. */ - void scheduleConnect(); - /** Schedule an new connection attempt, consider the current one failed. */ - void scheduleRetryConnect(); - /** Cancel any pending connection attempts. */ - void unscheduleConnect(); - - /** Attempt a connection to the watch right now. */ - virtual void connectToWatch(); - /** Sends a message to the watch. Does not block. */ virtual void send(const Message& msg); /** Sends a message to the watch if a message of the same type is not @@ -239,18 +221,9 @@ protected: void handleButtonEventMessage(const Message& msg); void handleBatteryVoltageMessage(const Message& msg); - /** To be overriden; should configure a newly connected watch. */ - virtual void handleWatchConnected() = 0; - private slots: void settingChanged(const QString& key); - void localDevModeChanged(QBluetoothLocalDevice::HostMode state); - void socketConnected(); - void socketDisconnected(); - void socketData(); - void socketError(QBluetoothSocket::SocketError error); - void socketState(QBluetoothSocket::SocketState error); - void timedReconnect(); + void dataReceived(); void timedSend(); void timedRing(); diff --git a/metawatch/metawatch.pro b/metawatch/metawatch.pro index 536ccee..2c7af94 100644 --- a/metawatch/metawatch.pro +++ b/metawatch/metawatch.pro @@ -36,10 +36,13 @@ res_files.files += res/graphics res/fonts qml_files.files += qml/com qml/metawatch-digital-config.qml LIBS += -L$$OUT_PWD/../libsowatch/ -lsowatch - INCLUDEPATH += $$PWD/../libsowatch DEPENDPATH += $$PWD/../libsowatch +LIBS += -L$$OUT_PWD/../libsowatchbt/ -lsowatchbt +INCLUDEPATH += $$PWD/../libsowatchbt +DEPENDPATH += $$PWD/../libsowatchbt + !isEmpty(MEEGO_VERSION_MAJOR)|maemo5 { QMAKE_RPATHDIR += /opt/sowatch/lib target.path = /opt/sowatch/lib/drivers diff --git a/metawatch/metawatchdigital.cpp b/metawatch/metawatchdigital.cpp index e099f76..e3f8431 100644 --- a/metawatch/metawatchdigital.cpp +++ b/metawatch/metawatchdigital.cpp @@ -235,8 +235,10 @@ QImage MetaWatchDigital::iconForNotification(const Notification *n) } } -void MetaWatchDigital::handleWatchConnected() +void MetaWatchDigital::setupBluetoothWatch() { + MetaWatch::setupBluetoothWatch(); // Call generic setup + // Grab all of the buttons that are of interest to us // We do not grab the F button, as it triggers the LED. grabButton(IdleMode, BtnA); // Required for app-switch diff --git a/metawatch/metawatchdigital.h b/metawatch/metawatchdigital.h index dde017a..1e1a62b 100644 --- a/metawatch/metawatchdigital.h +++ b/metawatch/metawatchdigital.h @@ -28,7 +28,7 @@ public: void update(Mode mode, const QList<QRect>& rects = QList<QRect>()); protected: - void handleWatchConnected(); + void setupBluetoothWatch(); private: // Idle screen: notifications unread count diff --git a/metawatch/metawatchdigitalsimulator.cpp b/metawatch/metawatchdigitalsimulator.cpp index c20e8c5..f808515 100644 --- a/metawatch/metawatchdigitalsimulator.cpp +++ b/metawatch/metawatchdigitalsimulator.cpp @@ -116,6 +116,8 @@ void MetaWatchDigitalSimulator::vibrate(bool on) void MetaWatchDigitalSimulator::connectToWatch() { + // Skip BluetoothWatch connection stuff + if (!_connected && _form) { qDebug() << "simulator connected"; @@ -123,7 +125,7 @@ void MetaWatchDigitalSimulator::connectToWatch() _currentMode = IdleMode; _paintMode = IdleMode; - handleWatchConnected(); + MetaWatchDigital::setupBluetoothWatch(); emit connected(); } diff --git a/metawatch/metawatchscanner.cpp b/metawatch/metawatchscanner.cpp index e560195..3742092 100644 --- a/metawatch/metawatchscanner.cpp +++ b/metawatch/metawatchscanner.cpp @@ -7,23 +7,9 @@ QTM_USE_NAMESPACE using namespace sowatch; MetaWatchScanner::MetaWatchScanner(QObject *parent) : - WatchScanner(parent), - _agent(new QBluetoothServiceDiscoveryAgent(this)) + BluetoothWatchScanner(parent) { - _agent->setUuidFilter(QBluetoothUuid::SerialPort); - connect(_agent, SIGNAL(finished()), this, SIGNAL(finished())); - connect(_agent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), - this, SLOT(handleDiscoveredService(QBluetoothServiceInfo))); -} - -void MetaWatchScanner::start() -{ - if (_agent->isActive()) { - _agent->stop(); - } - _agent->start(); - qDebug() << "started metawatch bluetooth scan"; - emit started(); + setUuidFilter(QBluetoothUuid::SerialPort); } void MetaWatchScanner::handleDiscoveredService(const QBluetoothServiceInfo &info) diff --git a/metawatch/metawatchscanner.h b/metawatch/metawatchscanner.h index a41633a..6cafe3e 100644 --- a/metawatch/metawatchscanner.h +++ b/metawatch/metawatchscanner.h @@ -2,25 +2,17 @@ #define METAWATCHSCANNER_H #include <sowatch.h> -#include <QtConnectivity/QBluetoothServiceDiscoveryAgent> +#include <sowatchbt.h> namespace sowatch { -using QTM_PREPEND_NAMESPACE(QBluetoothServiceDiscoveryAgent); -using QTM_PREPEND_NAMESPACE(QBluetoothServiceInfo); - -class MetaWatchScanner : public WatchScanner +class MetaWatchScanner : public BluetoothWatchScanner { Q_OBJECT public: explicit MetaWatchScanner(QObject *parent = 0); - void start(); - -private: - QBluetoothServiceDiscoveryAgent *_agent; - private slots: void handleDiscoveredService(const QBluetoothServiceInfo& info); }; |