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
/*  Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Sergey
    Trofimov
 
    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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
 
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
 
public class DeviceInfo extends AbstractInfo {
    public final String deviceId;
    public final int profileVersion;
    /**
     * Mi Band firmware version identifier
     */
    public final int fwVersion;
    public final int hwVersion;
    public final int feature;
    public final int appearance;
    /**
     * Heart rate firmware version identifier
     */
    public final int fw2Version;
    private boolean test1AHRMode;
 
 
    private boolean isChecksumCorrect(byte[] data) {
        int crc8 = CheckSums.getCRC8(new byte[]{data[0], data[1], data[2], data[3], data[4], data[5], data[6]});
        return (data[7] & 255) == (crc8 ^ data[3] & 255);
    }
 
    public DeviceInfo(byte[] data) {
        super(data);
 
        if ((data.length == 16 || data.length == 20) && isChecksumCorrect(data)) {
            deviceId = String.format("%02X%02X%02X%02X%02X%02X%02X%02X", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
            profileVersion = getInt(data, 8);
            fwVersion = getInt(data, 12);
            hwVersion = data[6] & 255;
            appearance = data[5] & 255;
            feature = data[4] & 255;
            if (data.length == 20) {
                int s = 0;
                for (int i = 0; i < 4; ++i) {
                    s |= (data[16 + i] & 255) << i * 8;
                }
                fw2Version = s;
            } else {
                fw2Version = -1;
            }
        } else {
            deviceId = "crc error";
            profileVersion = -1;
            fwVersion = -1;
            hwVersion = -1;
            feature = -1;
            appearance = -1;
            fw2Version = -1;
        }
    }
 
    public static int getInt(byte[] data, int from, int len) {
        int ret = 0;
        for (int i = 0; i < len; ++i) {
            ret |= (data[from + i] & 255) << i * 8;
        }
        return ret;
    }
 
    private int getInt(byte[] data, int from) {
        return getInt(data, from, 4);
    }
 
    public int getFirmwareVersion() {
        return fwVersion;
    }
 
    public int getHeartrateFirmwareVersion() {
        if (test1AHRMode) {
            return fwVersion;
        }
        return fw2Version;
    }
 
    public int getProfileVersion() {
        return profileVersion;
    }
 
    public void setTest1AHRMode(boolean enableTestMode) {
        test1AHRMode = enableTestMode;
    }
 
    public boolean supportsHeartrate() {
        return isMili1S() || (test1AHRMode && isMili1A());
    }
 
    @Override
    public String toString() {
        return "DeviceInfo{" +
                "deviceId='" + deviceId + '\'' +
                ", profileVersion=" + profileVersion +
                ", fwVersion=" + fwVersion +
                ", hwVersion=" + hwVersion +
                ", feature=" + feature +
                ", appearance=" + appearance +
                ", fw2Version (hr)=" + fw2Version +
                '}';
    }
 
    public boolean isMili1() {
        return hwVersion == 2 || (feature == 0 && appearance == 0 && hwVersion == 8 && fw2Version == -1);
    }
 
    public boolean isMili1A() {
        return feature == 5 && appearance == 0 || feature == 0 && hwVersion == 208;
    }
 
    public boolean isMili1S() {
        // TODO: this is probably not quite correct, but hopefully sufficient for early 1S support
        return (feature == 4 && appearance == 0) || hwVersion == 4;
    }
 
    public boolean isAmazFit() {
        return hwVersion == 6;
    }
 
    public String getHwVersion() {
        if (isMili1()) {
            return MiBandConst.MI_1;
        }
        if (isMili1A()) {
            return MiBandConst.MI_1A;
        }
        if (isMili1S()) {
            return MiBandConst.MI_1S;
        }
        if (isAmazFit()) {
            return MiBandConst.MI_AMAZFIT;
        }
        return "?";
    }
}