summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/watchconnection.h
blob: f2c3d5f1a3cc1c4985fc0aba1d9b4ae6d654b0c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef WATCHCONNECTION_H
#define WATCHCONNECTION_H

#include <QObject>
#include <QBluetoothAddress>
#include <QBluetoothSocket>
#include <QBluetoothLocalDevice>
#include <QtEndian>
#include <QPointer>
#include <QTimer>
#include <QFile>

class EndpointHandlerInterface;
class UploadManager;

class PebblePacket {
public:
    PebblePacket() {}
    virtual ~PebblePacket() = default;
    virtual QByteArray serialize() const = 0;
    QByteArray packString(const QString &string) const {
        QByteArray tmp = string.left(0xEF).toUtf8();
        QByteArray ret;
        ret.append((tmp.length() + 1) & 0xFF);
        ret.append(tmp);
        ret.append('\0');
        return ret;
    }
};

class Callback
{
public:
    QPointer<QObject> obj;
    QString method;
};

class WatchConnection : public QObject
{
    Q_OBJECT
public:

    enum Endpoint {
        EndpointUnknownEndpoint = 0,
        EndpointTime = 11,
        EndpointVersion = 16,
        EndpointPhoneVersion = 17,
        EndpointSystemMessage = 18,
        EndpointMusicControl = 32,
        EndpointPhoneControl = 33,
        EndpointApplicationMessage = 48,
        EndpointLauncher = 49,
        EndpointAppLaunch = 52,
        EndpointWatchLogs = 2000,
//        EndpointWatchPing = 2001,
        EndpointLogDump = 2002,
//        EndpointWatchReset = 2003,
//        EndpointWatchApp = 2004,
//        EndpointAppLogs = 2006,
        EndpointNotification = 3000,
//        watchEXTENSIBLE_NOTIFS = 3010, // Deprecated in 3.x
//        watchRESOURCE = 4000,
        EndpointFactorySettings = 5001,
        EndpointAppManager = 6000, // Deprecated in 3.x
        EndpointAppFetch = 6001, // New in 3.x
        EndpointDataLogging = 6778,
        EndpointScreenshot = 8000,
//        watchFILE_MANAGER = 8181,
//        watchCORE_DUMP = 9000,
//        watchAUDIO = 10000, // New in 3.x
        EndpointActionHandler = 11440,
        EndpointBlobDB = 45531, // New in 3.x
        EndpointSorting = 0xabcd,
        EndpointPutBytes = 0xbeef
    };

    enum SystemMessage {
        SystemMessageFirmwareAvailable = 0,
        SystemMessageFirmwareStart = 1,
        SystemMessageFirmwareComplete = 2,
        SystemMessageFirmwareFail = 3,
        SystemMessageFirmwareUpToDate = 4,
        SystemMessageFirmwareOutOfDate = 5,
        SystemMessageBluetoothStartDiscoverable = 6,
        SystemMessageBluetoothEndDiscoverable = 7
    };

    typedef QMap<int, QVariant> Dict;
    enum DictItemType {
        DictItemTypeBytes,
        DictItemTypeString,
        DictItemTypeUInt,
        DictItemTypeInt
    };

    enum UploadType {
        UploadTypeFirmware = 1,
        UploadTypeRecovery = 2,
        UploadTypeSystemResources = 3,
        UploadTypeResources = 4,
        UploadTypeBinary = 5,
        UploadTypeFile = 6,
        UploadTypeWorker = 7
    };
    enum UploadStatus {
        UploadStatusProgress,
        UploadStatusFailed,
        UploadStatusSuccess
    };

    explicit WatchConnection(QObject *parent = 0);
    UploadManager *uploadManager() const;

    void connectPebble(const QBluetoothAddress &pebble);
    bool isConnected();

    QByteArray buildData(QStringList data);
    QByteArray buildMessageData(uint lead, QStringList data);

    void writeToPebble(Endpoint endpoint, const QByteArray &data);
    void systemMessage(SystemMessage msg);

    bool registerEndpointHandler(Endpoint endpoint, QObject *handler, const QString &method);

signals:
    void watchConnected();
    void watchDisconnected();
    void watchConnectionFailed();

private:
    void scheduleReconnect();
    void reconnect();

private slots:
    void hostModeStateChanged(QBluetoothLocalDevice::HostMode state);
    void pebbleConnected();
    void pebbleDisconnected();
    void socketError(QBluetoothSocket::SocketError error);
    void readyRead();
//    void logData(const QByteArray &data);


private:
    QBluetoothAddress m_pebbleAddress;
    QBluetoothLocalDevice *m_localDevice;
    QBluetoothSocket *m_socket = nullptr;
    int m_connectionAttempts = 0;
    QTimer m_reconnectTimer;

    UploadManager *m_uploadManager;
    QHash<Endpoint, Callback> m_endpointHandlers;
};

#endif // WATCHCONNECTION_H