wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*  Copyright (C) 2023-2024 José Rebelo, Yoran Vulker
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi;
 
import static nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSppPacketV1.DATA_TYPE_PLAIN;
import static nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSppPacketV1.OPCODE_READ;
 
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
 
import androidx.annotation.Nullable;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdateDeviceInfo;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.PlainAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetProgressAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiChannelHandler.Channel;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
public class XiaomiSppSupport extends XiaomiConnectionSupport {
    private static final Logger LOG = LoggerFactory.getLogger(XiaomiSppSupport.class);
 
    AbstractBTBRDeviceSupport commsSupport = new AbstractBTBRDeviceSupport(LOG) {
        @Override
        public boolean useAutoConnect() {
            return mXiaomiSupport.useAutoConnect();
        }
 
        @Override
        public void onSocketRead(byte[] data) {
            XiaomiSppSupport.this.onSocketRead(data);
        }
 
        @Override
        protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
            // FIXME unsetDynamicState unsets the fw version, which causes problems..
            if (getDevice().getFirmwareVersion() == null) {
                getDevice().setFirmwareVersion(mXiaomiSupport.getCachedFirmwareVersion() != null ?
                        mXiaomiSupport.getCachedFirmwareVersion() :
                        "N/A");
            }
 
            builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
            builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.AUTHENTICATING, getContext()));
            builder.write(XiaomiSppPacketV1.newBuilder()
                    .channel(Channel.Version)
                    .needsResponse(true)
                    .opCode(OPCODE_READ)
                    .dataType(DATA_TYPE_PLAIN)
                    .frameSerial(0)
                    .build()
                    .encode(null, null));
            builder.add(new PlainAction() {
                @Override
                public boolean run(BluetoothSocket socket) {
                    mVersionResponseTimeoutHandler.postDelayed(new VersionTimeoutRunnable(), 5000L);
                    return true;
                }
            });
 
            return builder;
        }
 
        @Override
        protected UUID getSupportedService() {
            return XiaomiUuids.UUID_SERVICE_SERIAL_PORT_PROFILE;
        }
 
        @Override
        public void dispose() {
            mXiaomiSupport.onDisconnect();
            super.dispose();
        }
    };
 
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    private final XiaomiSupport mXiaomiSupport;
    private final Map<Channel, XiaomiChannelHandler> mChannelHandlers = new HashMap<>();
    private final Handler mVersionResponseTimeoutHandler = new Handler(Looper.getMainLooper());
    private AbstractXiaomiSppProtocol mProtocol = new XiaomiSppProtocolV1(this);
 
    public XiaomiSppSupport(final XiaomiSupport xiaomiSupport) {
        this.mXiaomiSupport = xiaomiSupport;
 
        mChannelHandlers.put(Channel.Version, this::handleVersionPacket);
        mChannelHandlers.put(Channel.ProtobufCommand, this.mXiaomiSupport::handleCommandBytes);
        mChannelHandlers.put(Channel.Activity, this.mXiaomiSupport.getHealthService().getActivityFetcher()::addChunk);
    }
 
    @Override
    public void setContext(GBDevice device, BluetoothAdapter adapter, Context context) {
        this.commsSupport.setContext(device, adapter, context);
    }
 
    @Override
    public boolean connect() {
        return commsSupport.connect();
    }
 
    @Override
    public void dispose() {
        commsSupport.dispose();
    }
 
    protected XiaomiAuthService getAuthService() {
        return mXiaomiSupport.getAuthService();
    }
 
    @Override
    public void onUploadProgress(final int textRsrc, final int progressPercent, final boolean ongoing) {
        try {
            final TransactionBuilder builder = commsSupport.createTransactionBuilder("send data upload progress");
            builder.add(new SetProgressAction(
                    commsSupport.getContext().getString(textRsrc),
                    ongoing,
                    progressPercent,
                    commsSupport.getContext()
            ));
            builder.queue(commsSupport.getQueue());
        } catch (final Exception e) {
            LOG.error("Failed to update progress notification", e);
        }
    }
 
    @Override
    public void runOnQueue(String taskName, Runnable runnable) {
        if (commsSupport == null) {
            LOG.error("commsSupport is null, unable to queue task");
            return;
        }
 
        final TransactionBuilder b = commsSupport.createTransactionBuilder("run task " + taskName + " on queue");
        b.add(new PlainAction() {
            @Override
            public boolean run(BluetoothSocket socket) {
                runnable.run();
                return true;
            }
        });
        b.queue(commsSupport.getQueue());
    }
 
    private void skipBuffer(int newStart) {
        final byte[] bufferState = buffer.toByteArray();
        buffer.reset();
 
        if (newStart < 0) {
            newStart = bufferState.length;
        }
 
        if (newStart >= bufferState.length) {
            return;
        }
 
        buffer.write(bufferState, newStart, bufferState.length - newStart);
    }
 
    private void processBuffer() {
        boolean shouldProcess = true;
        while (shouldProcess) {
            final byte[] bufferState = buffer.toByteArray();
            final AbstractXiaomiSppProtocol.ParseResult parseResult = mProtocol.processPacket(bufferState);
            LOG.debug("processBuffer(): protocol.processPacket() returned status {}", parseResult.status);
            int skipBytes;
 
            switch (parseResult.status) {
                case Incomplete:
                    skipBytes = 0;
                    shouldProcess = false;
                    break;
                case Complete:
                    skipBytes = parseResult.packetSize;
                    break;
                case Invalid:
                    skipBytes = mProtocol.findNextPacketOffset(bufferState);
                    if (skipBytes < 0) {
                        skipBytes = bufferState.length;
                    }
                    break;
                default:
                    throw new IllegalStateException(String.format("Unhandled parse state %s", parseResult.status));
            }
 
            if (skipBytes > 0) {
                LOG.debug("processBuffer(): skipping {} bytes for state {}", skipBytes, parseResult.status);
                skipBuffer(skipBytes);
            }
        }
    }
 
    public void onSocketRead(byte[] data) {
        try {
            buffer.write(data);
        } catch (IOException ex) {
            LOG.error("Exception while writing buffer: ", ex);
        }
 
        processBuffer();
    }
 
    protected void onPacketReceived(final Channel channel, final byte[] payload) {
        final XiaomiChannelHandler handler = mChannelHandlers.get(channel);
        if (handler != null) {
            handler.handle(payload);
        } else {
            LOG.warn("Unhandled SppPacket on channel {}", channel);
        }
    }
 
    @Override
    public void sendCommand(final String taskName, final XiaomiProto.Command command) {
        try {
            final TransactionBuilder builder = this.commsSupport.createTransactionBuilder("send " + taskName);
            sendCommand(builder, command);
            builder.queue(this.commsSupport.getQueue());
        } catch (final Exception ex) {
            LOG.error("Caught unexpected exception while sending command, device may not have been informed!", ex);
        }
    }
 
    public void sendCommand(final TransactionBuilder builder, final XiaomiProto.Command command) {
        LOG.debug("sendCommand(): encoded command for task '{}': {}", builder.getTransaction().getTaskName(), GB.hexdump(command.toByteArray()));
        if (command.getType() == XiaomiAuthService.COMMAND_TYPE) {
            builder.write(mProtocol.encodePacket(Channel.Authentication, command.toByteArray()));
        } else {
            builder.write(mProtocol.encodePacket(Channel.ProtobufCommand, command.toByteArray()));
        }
        // do not queue here, that's the job of the caller
    }
 
    public void sendDataChunk(final String taskName, final byte[] chunk, @Nullable final XiaomiCharacteristic.SendCallback callback) {
        LOG.debug("sendDataChunk(): encoded data chunk for task '{}': {}", taskName, GB.hexdump(chunk));
        this.commsSupport.createTransactionBuilder("send " + taskName)
            .write(mProtocol.encodePacket(Channel.Data, chunk))
            .queue(commsSupport.getQueue());
 
        if (callback != null) {
            // callback puts a SetProgressAction onto the queue
            callback.onSend();
        }
    }
 
    private void handleVersionPacket(final byte[] payloadBytes) {
        // remove timeout actions from handler
        mVersionResponseTimeoutHandler.removeCallbacksAndMessages(null);
 
        if (payloadBytes != null && payloadBytes.length > 0) {
            LOG.debug("Received SPP protocol version: {}", GB.hexdump(payloadBytes));
 
            // show in details
            final GBDeviceEventUpdateDeviceInfo event = new GBDeviceEventUpdateDeviceInfo("SPP_PROTOCOL: ", GB.hexdump(payloadBytes));
            mXiaomiSupport.evaluateGBDeviceEvent(event);
 
            // TODO handle different protocol versions
            if (payloadBytes[0] >= 2) {
                LOG.info("handleVersionPacket(): detected protocol version higher than 2, switching protocol");
                mProtocol = new XiaomiSppProtocolV2(this);
            }
        }
 
        if (mProtocol.initializeSession()) {
            mXiaomiSupport.getAuthService().startEncryptedHandshake();
        }
    }
 
    class VersionTimeoutRunnable implements Runnable {
        @Override
        public void run() {
            LOG.warn("SPP protocol version request timed out");
            XiaomiSppSupport.this.handleVersionPacket(new byte[0]);
        }
    }
}