zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package ymodem;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import BaoWen.Dell_55AA01;
import BaoWen.Dell_55AA02;
import BaoWen.Dell_55AA03;
import BaoWen.Dell_55AA0A;
import Frame.AnchorSet;
import PbuliClass.ShowMessage;
import PbuliClass.Systems;
import tbDataModel_Dell.Tools;
public class Update implements Runnable{    
    public static final int SOH = 0X01;        //128×Ö½ÚÊý¾Ý°ü
    public static final int STX = 0X02;        //1024×Ö½ÚÊý¾Ý°ü
    public static final int EOT = 0X04;        //½áÊø´«Êä
    public static final int ACK = 0x06;        //»ØÓ¦
    public static final int NAK = 0X15;        //²»»ØÓ¦
    public static final int CA = 0X18;            //´«ÊäÖÕÖ¹
    public static final int C = 0x43;            //ÇëÇóÊý¾Ý°ü
    static public int num = 0;                        //Ö¡ÐòºÅ
    public int mport;                            //±¾µØ¶Ë¿Ú
    static public int errorNum = 100;            //´íÎó´ÎÊý
    static boolean update_star=false;
    static byte[] startBytes;//»ñÈ¡Æðʼ֡Êý¾Ý°ü
    static byte[][] dataBytes ;//»ñÈ¡Êý¾ÝÖ¡Êý¾Ý°ü
    static byte[] endBytes ;//½áÊøÖ¡Êý¾Ý°ü
    static String ip;//Ä¿±êÉ豸ipµØÖ·
    static int port;//Ä¿±êÉ豸¶Ë¿Ú
    static  String path;
    static int zusai=0;
    private static DatagramSocket socket;
    static byte[] dat;
    static DatagramPacket packet;
    static String nowip="";
    static int nowport;
 
    public static void chushihua() {
        if(socket==null) {
            int port=Integer.parseInt(Systems.sys().getBaoliu1());
            try {
                socket =new DatagramSocket(port);    
                new Update().startThread();
            } catch (SocketException e) {
                ShowMessage.zidingyi(port+"¶Ë¿Ú±»Õ¼ÓÃ");
            }
        }
    }
 
 
 
    public static void shengji(String ip1,int port1,String path1,byte[] byt) throws IOException {
        chushihua();
        ip=ip1;
        port=port1;
        fasongdata(byt,ip,port);
        AnchorSet.addmesg("·¢ËÍÉý¼¶ÇëÇó");        
        path=path1;
        UDPSendUpdateFile(ip,port, path);
        AnchorSet.setShengjimoshi(true);
    }
 
    /**Æô¶¯Ï̵߳ķ½·¨*/
    public void startThread() {
        Thread t=new Thread(this);
        t.start();
    }
 
    public void run() {
        while(true) {
            if(AnchorSet.isShengjimoshi()) {
                kaishishengji();                
            }else {
                getRecDat();
            }
 
        }
    }
 
    public static int getRecDat()  {        
        int a=-1;
        if(!AnchorSet.isShengjimoshi()) {
            dat = new byte[1024];
            packet = new DatagramPacket(dat,1024);
        }else {
            dat = new byte[100];
            packet = new DatagramPacket(dat,100);
        }
        try {
            socket.receive(packet);    
            Dell_55AA01.dell_gui();
            nowip=packet.getAddress().getHostAddress();
            nowport=packet.getPort();
            String ip=AnchorSet .getIschooseip();
            if(ip.equals(nowip)) {    
                int lenth=packet.getLength();
                byte[] adat=subBytes(packet.getData(),0, lenth);        
                String datas=Tools.Bytes2HexString(adat);
                int lenth2=datas.length();                
                dell_message(datas,lenth2);//´¦Àí±¨ÎÄÊý¾Ý
                if(AnchorSet.isShengjimoshi()) {
                    a=dat[0] & 0xff;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a;
    }
 
 
 
    /**Éý¼¶Ä£Ê½½ÓÊÕÊý¾Ý*/
    public static int kaishishengji() {    
        int eNum = 0;
        String ip=AnchorSet .getIschooseip();
        while (getRecDat() != C ) {//½ÓÊÕÇëÇó
            AnchorSet.addmesg("µÈ´ýÖÕ¶ËÏìÓ¦C...");
            return 0;
        }
        //·¢ËÍÆðʼ֡Êý¾Ý°ü
        AnchorSet.addmesg("·¢ËÍÆðʼÊý¾Ýto"+ip+":"+port);
        fasongdata(startBytes,ip,port);    
 
 
        while (getRecDat() != ACK) {//½ÓÊÕÏìÓ¦
            if (eNum ++ >= errorNum) {
                AnchorSet.addmesg("Æðʼ֡·¢ËÍʧ°Ü£¡");
                AnchorSet.setShengjimoshi(false);
                return 0;
            }
        }
        eNum = 0;        
        while (getRecDat() != C) {//½ÓÊÕÇëÇó
            if (eNum ++ >= errorNum) {
                AnchorSet.addmesg("δÊÕµ½CÉý¼¶Ê§°Ü");
                AnchorSet.setShengjimoshi(false);
                return 0;
            }
        }
 
        eNum = 0;
        AnchorSet.addmesg("»ñÈ¡ÇëÇó³É¹¦£¬¿ªÊ¼·¢ËÍÊý¾ÝÖ¡...");
        for (int i = 0; i < dataBytes.length; i++) {
            AnchorSet.addmesg("ÕýÔÚ·¢Ë͵Ú:"+(i+1)+"¸öÊý¾Ý°ü,×ܹ²:"+dataBytes.length+"¸ö  ");
            fasongdata(dataBytes[i],ip,port);
            while (getRecDat() != ACK) {
                if (eNum ++ >= errorNum) {
                    AnchorSet.addmesg("Êý¾Ý·¢ËÍʧ°Ü");
                    AnchorSet.setShengjimoshi(false);
                    return 0;
                }
            }
            eNum = 0;
            AnchorSet.addmesg("Êý¾Ý·¢Ëͳɹ¦");
        }
 
        eNum = 0;
        fasongdata(new byte[] {(byte)EOT},ip,port);        //µÚÒ»´ÎEOT        
        while (getRecDat() != ACK) {//½ÓÊÕÏìÓ¦                    
            if (eNum ++ >= errorNum) {
                AnchorSet.addmesg("µÚÒ»´ÎEOT·´À¡Ê§°Ü£¡");
                AnchorSet.setShengjimoshi(false);
                return 0;
            }
        }
 
        eNum = 0;
        fasongdata(new byte[] {(byte)EOT},ip,port);    //½ÓÊÕµ½NAK ÔÙ·¢Ò»´ÎµÚ¶þ´ÎEOT
        while (getRecDat() != ACK) {
            if (eNum ++ >= errorNum) {
                AnchorSet.addmesg("µÚ¶þ´ÎEOT·´À¡Ê§°Ü£¡");
                AnchorSet.setShengjimoshi(false);
                return 0;
            }
        }
 
        eNum = 0;    
        while (getRecDat() != C) {//½ÓÊÕÇëÇó
            if (eNum ++ >= errorNum) {
                AnchorSet.addmesg("δÊÕµ½CÉý¼¶Ê§°Ü");
                AnchorSet.setShengjimoshi(false);
                return 0;
            }
        }
        eNum = 0;
        fasongdata(endBytes,ip,port);
        AnchorSet.setShengjimoshi(false);
        ShowMessage.zidingyi(ip+":"+port+"Éý¼¶³É¹¦");
        AnchorSet.addmesg(ip+":"+port+"Éý¼¶³É¹¦");
        return 1;
 
    }
 
    /**½«Êý¾Ý·¢¸ø»ùÕ¾*/
    public static void fasongdata(byte[] data,String ipout,int portout) {        
        DatagramPacket packet=null;    
        InetAddress address=null;
        //Èç¹ûÊý¾ÝÀàÐÍÏàͬ
        try {
            address=InetAddress.getByName(ipout);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        packet=new DatagramPacket(data,data.length, address, portout);
 
        try {
            socket.send(packet);
//            String datas=Tools.Bytes2HexString(data).substring(0, packet.getLength()*2);
//            AnchorSet.addmesg("·¢:"+datas);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void dell_message(String datas,int lenth2) {                
        if(!AnchorSet.isShengjimoshi()) {
            if(datas.startsWith("55AA03") && lenth2==202) {
                Dell_55AA03.ok_55AA03_sj(nowip,datas);
            }else if(datas.startsWith("55AA02") && lenth2==32){
                Dell_55AA02.dellrgs(datas,nowip,nowport);
            }else if(datas.startsWith("55AA0A") && lenth2==48) {
                Dell_55AA0A.dell55aa0a(datas, nowip, nowport);
            }
        }
        AnchorSet.addmesg(nowip+":"+nowport+"ÊÕ: "+datas);
    }
 
 
    public static boolean isUpdate_star() {
        return update_star;
    }
 
 
    public static void setUpdate_star(boolean update_star) {
        Update.update_star = update_star;
    }
 
 
 
    /**
     * »ñÈ¡Êý¾ÝÖ¡Êý¾Ý
     * @param file    Îļþ
     * @return Êý¾ÝÖ¡Êý×é
     * @throws IOException 
     */
    public static byte[][] getData(File file) throws IOException{
        long len = file.length();
        int size = (int) Math.ceil(len/1024d);        //»ñÈ¡Êý¾ÝÖ¡¸öÊý
        byte[][] result = new byte[size][1029];            
        FileInputStream fis = new FileInputStream(file);
 
        for (int i = 0; i < size-1; i++) {            //Éú³ÉÊý¾ÝÖ¡
            result[i][0] = STX;                        //1024×Ö½ÚÊý¾Ý°üÍ·
            result[i][1] = (byte)(num ++);            //×Ô¶¯Ìî³äÖ¡ÐòºÅ
            if (num > 255) {                        //Ö¡ÐòºÅ×î´ó255
                num = 0;    
            }
            result[i][2] = (byte)~result[i][1];        //È¡·´Ö¡ÐòºÅ
            fis.read(result[i], 3, 1024);
            byte[] crc16 = CRC16.getCRC16(result[i],3);    //»ñÈ¡crc16УÑé
            result[i][result[i].length-2] = crc16[0];
            result[i][result[i].length-1] = crc16[1];    //Ìî³äcrc16УÑé
        }
 
        int endLen = (int)len % 1024;
        //Èç¹û×îºóÒ»Ö¡²»ÊÇÕû1024¸ö×Ö½Ú
        if (endLen > 0) {
            //Èç¹û×îºóÒ»Ö¡³¤¶ÈСÓÚµÈÓÚ128
            if (endLen <= 128) {
                //×îºóһ֡ʹÓÃ128×Ö½Ú´«Êä
                result[size-1] = new byte[133];
                result[size-1][0] = SOH;                        //128×Ö½ÚÊý¾Ý°üÍ·
                int n = fis.read(result[size-1], 3, 128);        //Ìî³ä128¸ö×Ö½ÚÊý¾Ý
                for(int i = n+3;i<131;i++) {                    //Ê£ÓàλÌî³ä0x1A
                    result[size-1][i] = 0x1A;
                }
            }else{
                //×îºóһ֡ʹÓÃ1024×Ö½Ú´«Êä
                result[size-1][0] = STX;                        //1024×Ö½ÚÊý¾Ý°üÍ·
 
                int n = fis.read(result[size-1], 3, 1024);        //Ìî³ä1024¸öÊý¾Ý
                for(int i = n+3;i<1027;i++) {                    //Ê£ÓàλÌî³ä0x1A
                    result[size-1][i] = 0x1A;
                }
            }
            result[size-1][1] = (byte)(num ++);                //×Ô¶¯Ìî³äÖ¡ÐòºÅ
            if (num > 255) {                                //Ö¡ÐòºÅ×î´ó255
                num = 0;    
            }
            result[size-1][2] = (byte)~result[size-1][1];    //È¡·´Ö¡ÐòºÅ
            byte[] crc16 = CRC16.getCRC16(result[size-1],3);    //»ñÈ¡crc16УÑé
            result[size-1][result[size-1].length-2] = crc16[0];
            result[size-1][result[size-1].length-1] = crc16[1];    //Ìî³äcrc16УÑé
        }
        fis.close();
        return result;
    }
 
    /**
     * »ñÈ¡Æðʼ֡Êý¾Ý
     * @param num Ö¡ÐòºÅ
     * @param file Òª·¢Ë͵ÄÎļþ
     * @return
     */
    public static byte[] getStartData(File file) {
        int index = 0;                            //Êý¾Ýϱê
        byte[] dat = new byte[133];
        byte[] nameBytes = file.getName().getBytes();                    //»ñÈ¡ÎļþÃû×Ö½ÚÊý×é
        byte[] lengthBytes = String.valueOf(file.length()).getBytes();  //Îļþ³¤¶È×Ö½ÚÊý×é
 
        dat[index++] = SOH;                        //SOH128×Ö½ÚÊý¾Ý°ü
        dat[index++] = (byte) num++;            //Ö¡ÐòºÅ
        dat[index++] = (byte) ~dat[1];            //È¡·´ÐòºÅ
        //×éºÏÎļþÃûÊý¾Ý
        for (int i = 0; i < nameBytes.length; i++) {
            dat[i+index] = nameBytes[i];        
        }
        index += nameBytes.length;                
        dat[index++] = 0x00;                    //ÎļþÃûºó²¹0x00
        //×éºÏÎļþ³¤¶ÈÊý¾Ý
        for (int i = 0; i < lengthBytes.length; i++) {
            dat[i+index] = lengthBytes[i];        
        }
        index += lengthBytes.length;    
        dat[index++] = 0x00;                    //Îļþ³¤¶Èºó²¹0x00
 
        byte[] crcDat = CRC16.getCRC16(dat,3);    //»ñÈ¡crc16УÑé
        dat[dat.length-2] = crcDat[0];            //Ìî³äcrc16УÑé
        dat[dat.length-1] = crcDat[1];    
        return dat;
    }
 
    /**
     * »ñÈ¡½áÊøÖ¡Êý¾Ý
     * @return
     */
    public static byte[] getEndData() {
        byte[] dat = new byte[133];
        dat[0] = SOH;
        dat[1] = (byte)0x00;
        dat[2] = (byte)0xFF;
        byte[] crcDat = CRC16.getCRC16(dat,3);    //»ñÈ¡crc16УÑé
        dat[dat.length-2] = crcDat[0];            //Ìî³äcrc16УÑé
        dat[dat.length-1] = crcDat[1];    
        return dat;
    }
 
 
 
    /**
     * ·¢ËÍÉý¼¶Îļþ
     * @param ip ¿Í»§¶ËIpµØÖ·
     * @param port ¿Í»§¶Ë¶Ë¿Ú
     * @param filePath ÎļþĿ¼
     * @return 0-ʧ°Ü 1-³É¹¦
     * @throws IOException IOÒì³£
     */
    public static void UDPSendUpdateFile(String ip1,int port1,String filePath) throws IOException  {
        ip = ip1;
        port = port1;
        File file = new File(filePath);
        if (!file.exists()) {                                //Èç¹ûÎļþ²»´æÔÚ
            ShowMessage.zidingyi("Îļþ²»´æÔÚ"+filePath);        //Å׳öÎļþÕÒ²»µ½Òì³£
            return;
        }
        num = 0;
        startBytes = getStartData(file);                //»ñÈ¡Æðʼ֡Êý¾Ý°ü
        dataBytes = getData(file);                    //»ñÈ¡Êý¾ÝÖ¡Êý¾Ý°ü
        endBytes = getEndData();        
    }
 
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }
 
 
}