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;
|
}
|
|
|
}
|