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
/*  Copyright (C) 2024 Vitalii Tomin
 
    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.devices.huawei.packets;
 
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV;
 
public class FileUpload {
    public static final byte id = 0x28;
 
    public static class FileUploadParams {
        public byte file_id = 0;
        public String protocolVersion = "";
        public int app_wait_time = 0;
        public int bitmap_enable = 0;
        public int unit_size = 0;
        public int max_apply_data_size = 0;
        public int interval = 0;
        public int received_file_size = 0;
        public byte no_encrypt = 0;
    }
 
    public static class Filetype {
        public static final byte watchface = 1;
        public static final byte music = 2;
        public static final byte backgroundImage = 3;
        public static final byte app = 6;
        public static final byte firmware = 7;
    }
 
 
    public static class FileInfoSend {
        public static final byte id = 0x02;
 
        public static class Request extends HuaweiPacket {
 
            public Request(ParamsProvider paramsProvider,
                           int fileSize,
                           String fileName,
                           byte fileType,
                           String srcPackage,
                           String dstPackage,
                           String srcFingerprint,
                           String dstFingerprint
            ) {
                super(paramsProvider);
                this.serviceId = FileUpload.id;
                this.commandId = id;
 
                this.tlv = new HuaweiTLV()
                        .put(0x01, fileName)
                        .put(0x02, fileSize)
                        .put(0x03, fileType);
 
                if (fileType == Filetype.watchface) {
                    String watchfaceName = fileName.split("_")[0];
                    String watchfaceVersion = fileName.split("_")[1];
                    this.tlv.put(0x05, watchfaceName)
                            .put(0x06, watchfaceVersion);
                }
 
                if (srcPackage != null && dstPackage != null) {
                    this.tlv.put(0x08, srcPackage)
                            .put(0x09, dstPackage)
                            .put(0x0a)
                            .put(0x0b, srcFingerprint)
                            .put(0x0c, dstFingerprint);
                }
 
 
                this.complete = true;
            }
        }
 
        public static class Response extends HuaweiPacket {
            public int result = 0;
 
            public Response(ParamsProvider paramsProvider) {
                super(paramsProvider);
            }
 
            @Override
            public void parseTlv() throws HuaweiPacket.ParseException {
                this.result = this.tlv.getInteger(0x7f);
            }
        }
    }
 
    public static class FileHashSend {
        public static final byte id = 0x03;
 
        public static class Request extends HuaweiPacket {
 
            public Request(ParamsProvider paramsProvider,
                           byte[] hash,
                           byte fileId) {
                super(paramsProvider);
                this.serviceId = FileUpload.id;
                this.commandId = id;
 
                this.tlv = new HuaweiTLV()
                        .put(0x01, fileId)
                        .put(0x03, hash);
                this.complete = true;
            }
        }
 
        public static class Response extends HuaweiPacket {
            public byte fileId = 0;
 
            public Response(ParamsProvider paramsProvider) {
                super(paramsProvider);
            }
 
            @Override
            public void parseTlv() throws HuaweiPacket.ParseException {
                this.fileId = this.tlv.getByte(0x01);
            }
 
        }
    }
 
    public static class FileUploadConsultAck {
        public static final byte id = 0x04;
 
        public static class Request extends HuaweiPacket {
            public Request(ParamsProvider paramsProvider, byte noEncryption, byte fileId) {
                super(paramsProvider);
                this.serviceId = FileUpload.id;
                this.commandId = id;
                this.tlv = new HuaweiTLV()
                        .put(0x7f, 0x000186A0) //ok
                        .put(0x01, fileId);
                if (noEncryption == 1)
                    this.tlv.put(0x09, (byte) 0x01); // need on devices which generally encrypted, but files
                this.complete = true;
            }
        }
 
        public static class Response extends HuaweiPacket {
 
            public FileUploadParams fileUploadParams = new FileUploadParams();
 
            public Response(ParamsProvider paramsProvider) {
                super(paramsProvider);
            }
 
            @Override
            public void parseTlv() throws HuaweiPacket.ParseException {
                this.fileUploadParams.file_id = this.tlv.getByte(0x01);
                this.fileUploadParams.protocolVersion = this.tlv.getString(0x02);
                this.fileUploadParams.app_wait_time = this.tlv.getAsInteger(0x03);
                this.fileUploadParams.bitmap_enable = this.tlv.getAsInteger(0x04);
                this.fileUploadParams.unit_size = this.tlv.getAsInteger(0x05);
                this.fileUploadParams.max_apply_data_size = this.tlv.getAsInteger(0x06);
                this.fileUploadParams.interval = this.tlv.getAsInteger(0x07);
                this.fileUploadParams.received_file_size = this.tlv.getInteger(0x08);
                if (this.tlv.contains(0x09)) // optional for older devices
                    this.fileUploadParams.no_encrypt = this.tlv.getByte(0x09);
            }
        }
    }
 
    public static class FileNextChunkParams extends HuaweiPacket {
        public static final byte id = 0x05;
 
        public int bytesUploaded = 0;
        public int nextchunkSize = 0;
 
        public FileNextChunkParams(ParamsProvider paramsProvider) {
            super(paramsProvider);
            this.serviceId = FileUpload.id;
            this.commandId = id;
            this.complete = true;
        }
 
        @Override
        public void parseTlv() throws HuaweiPacket.ParseException {
            this.bytesUploaded = this.tlv.getInteger(0x02);
            this.nextchunkSize = this.tlv.getInteger(0x03);
        }
    }
 
    public static class FileNextChunkSend extends HuaweiPacket {
        public static final byte id = 0x06;
 
        public FileNextChunkSend(ParamsProvider paramsProvider) {
            super(paramsProvider);
            this.serviceId = FileUpload.id;
            this.commandId = id;
            this.complete = true;
        }
    }
 
    public static class FileUploadResult {
        public static final byte id = 0x07;
 
        public static class Request extends HuaweiPacket {
            public Request(ParamsProvider paramsProvider, byte fileId) {
                super(paramsProvider);
                this.serviceId = FileUpload.id;
                this.commandId = id;
                this.tlv = new HuaweiTLV()
                        .put(0x7f, 0x000186A0) //ok
                        .put(0x01, fileId);
 
                this.complete = true;
            }
        }
 
        public static class Response extends HuaweiPacket {
            byte status = 0;
 
            public Response(ParamsProvider paramsProvider) {
                super(paramsProvider);
            }
 
            @Override
            public void parseTlv() throws HuaweiPacket.ParseException {
                this.status = this.tlv.getByte(0x02);
            }
        }
    }
}