Move some functions in IspManager
Move the thread for flash in it's right place
This commit is contained in:
parent
ea3792f730
commit
abdc7fb019
4 changed files with 229 additions and 230 deletions
|
@ -18,6 +18,7 @@
|
|||
|
||||
package fr.mobdev.lpcprog.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
@ -49,6 +50,7 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
private long part_id;
|
||||
|
||||
@Override
|
||||
@SuppressLint("SetTextI18n")
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.device);
|
||||
|
@ -69,7 +71,7 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
}
|
||||
if(dev != null) {
|
||||
TextView name = (TextView) findViewById(R.id.name);
|
||||
name.setText("Name: " + dev.description);
|
||||
name.setText(getApplicationContext().getString(R.string.dev_name)+ dev.description);
|
||||
TextView vendor = (TextView) findViewById(R.id.vendor_id);
|
||||
vendor.setText(String.format("Vendor Id: %04x", dev.VID));
|
||||
TextView product = (TextView) findViewById(R.id.id);
|
||||
|
|
|
@ -299,7 +299,12 @@ class BinaryHolder extends RecyclerView.ViewHolder{
|
|||
flashView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
IspManager.getInstance(itemView.getContext()).flash(server,binary,flashListener);
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
IspManager.getInstance(itemView.getContext()).flashBinary(server,binary,flashListener);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -46,11 +46,12 @@ public class IspManager {
|
|||
private static final int MAX_DATA_BLOCK_SIZE = LINES_PER_BLOCK * LINE_DATA_LENGTH;
|
||||
private static final int UUENCODE_ADDED_VAL = 32;
|
||||
private static final int LINE_DATA_LENGTH_MAX = 45;
|
||||
|
||||
//defines commands and some answers
|
||||
private static final byte[] SYNC_START = "?".getBytes();
|
||||
private static final byte[] SYNC = "Synchronized\r\n".getBytes();
|
||||
private static final byte[] SYNC_OK = "OK\r\n".getBytes();
|
||||
private static final byte[] DATA_BLOCK_OK = "OK\r\n".getBytes();
|
||||
//private static final byte[] DATA_BLOCK_RESEND = "RESEND\r\n".getBytes();
|
||||
private static final byte[] SYNC_ECHO_OFF = "A 0\r\n".getBytes();
|
||||
private static final byte[] READ_UID = "N\r\n".getBytes();
|
||||
private static final byte[] READ_PART_ID = "J\r\n".getBytes();
|
||||
|
@ -63,7 +64,6 @@ public class IspManager {
|
|||
private Context context;
|
||||
private long part_id = 0;
|
||||
|
||||
|
||||
private IspManager(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
@ -98,68 +98,18 @@ public class IspManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean synchronize(int crystal_freq) {
|
||||
if (serialPort == null)
|
||||
return false;
|
||||
serialPort.purge(D2xxManager.FT_PURGE_RX);
|
||||
serialPort.purge(D2xxManager.FT_PURGE_TX);
|
||||
byte[] buf;
|
||||
if (serialPort.write(SYNC_START) != SYNC_START.length) {
|
||||
System.out.println("Start");
|
||||
return false;
|
||||
public boolean synchronizeIfPossible(int crystal_freq) {
|
||||
if (!synchronize(crystal_freq)) {
|
||||
serialPort.purge(D2xxManager.FT_PURGE_TX);
|
||||
serialPort.purge(D2xxManager.FT_PURGE_RX);
|
||||
String[] uids = readUid();
|
||||
if (uids == null) {
|
||||
System.out.println("uids null fuck");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
buf = new byte[SYNC.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
String result = new String(buf);
|
||||
String expected = new String(SYNC);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
serialPort.write("\r\n".getBytes());
|
||||
System.out.println("Start answer " + result);
|
||||
return false;
|
||||
}
|
||||
if (serialPort.write(SYNC) != SYNC.length) {
|
||||
System.out.println("SYNC write");
|
||||
return false;
|
||||
}
|
||||
clearBuffer(SYNC.length);
|
||||
buf = new byte[SYNC_OK.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
result = new String(buf);
|
||||
expected = new String(SYNC_OK);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
System.out.println("SYNC OK " + result);
|
||||
return false;
|
||||
}
|
||||
|
||||
String freq = String.valueOf(crystal_freq) + "\r\n";
|
||||
if (serialPort.write(freq.getBytes()) != freq.length()) {
|
||||
System.out.println("freq write");
|
||||
return false;
|
||||
}
|
||||
|
||||
clearBuffer(freq.length());
|
||||
buf = new byte[SYNC_OK.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
result = new String(buf);
|
||||
expected = new String(SYNC_OK);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
System.out.println("freq answer " + result);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (serialPort.write(SYNC_ECHO_OFF) != SYNC_ECHO_OFF.length) {
|
||||
System.out.println("Sync Echo Off write");
|
||||
return false;
|
||||
}
|
||||
|
||||
clearBuffer(SYNC_ECHO_OFF.length);
|
||||
buf = new byte[RETURN_CODE_SIZE];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
clearBuffer(REP_BUF);
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void clearBuffer(int size) {
|
||||
|
@ -262,169 +212,7 @@ public class IspManager {
|
|||
return version;
|
||||
}
|
||||
|
||||
private int sendCommand(byte[] cmd) {
|
||||
byte[] buf = new byte[RETURN_CODE_SIZE];
|
||||
if (serialPort.write(cmd) != cmd.length)
|
||||
return -5;
|
||||
int len = read(buf, buf.length, 500);
|
||||
if (len <= 0)
|
||||
return -4;
|
||||
int ret = parseRetCode(buf);
|
||||
if(ret > 9)
|
||||
read(buf,1,50);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int sendCommand(char c, int addr, int count) {
|
||||
int ret;
|
||||
int len;
|
||||
|
||||
byte[] buf = String.format(Locale.getDefault(),"%c %d %d\r\n",c,addr,count).getBytes();
|
||||
len = buf.length;
|
||||
if(len > REP_BUF) {
|
||||
len = REP_BUF;
|
||||
}
|
||||
int sent = serialPort.write(buf,len);
|
||||
if(len != sent) {
|
||||
return -5;
|
||||
}
|
||||
buf = new byte[RETURN_CODE_SIZE];
|
||||
len = read(buf,buf.length,500);
|
||||
if(len <= 0) {
|
||||
return -4;
|
||||
}
|
||||
ret = parseRetCode(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int read(byte[] buffer, int size, int timeout) {
|
||||
long startTime = System.nanoTime();
|
||||
long timeoutNano = timeout * 1000000;
|
||||
int sizeRead = 0;
|
||||
|
||||
ByteBuffer output = ByteBuffer.wrap(buffer, 0, size);
|
||||
|
||||
while (sizeRead < size) {
|
||||
int sizeToRead = serialPort.getQueueStatus();
|
||||
if (sizeRead + sizeToRead > size)
|
||||
sizeToRead = size - sizeRead;
|
||||
byte[] buf = new byte[sizeToRead];
|
||||
if (sizeToRead > 0) {
|
||||
int sizeReceived = serialPort.read(buf, sizeToRead, 50);
|
||||
if (sizeReceived < 0)
|
||||
return sizeReceived;
|
||||
sizeRead += sizeReceived;
|
||||
if (sizeReceived != 0)
|
||||
output.put(buf, 0, sizeReceived);
|
||||
}
|
||||
long time = System.nanoTime();
|
||||
if (time - startTime >= timeoutNano)
|
||||
break;
|
||||
}
|
||||
return sizeRead;
|
||||
}
|
||||
|
||||
private int parseRetCode(byte[] data) {
|
||||
String str = new String(data,0,2);
|
||||
str = str.replace("\r", "");
|
||||
str = str.replace("\n", "");
|
||||
if (!str.matches("[0-9 -+]*"))
|
||||
return -1;
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
public boolean synchronizeIfPossible(int crystal_freq) {
|
||||
if (!synchronize(crystal_freq)) {
|
||||
serialPort.purge(D2xxManager.FT_PURGE_TX);
|
||||
serialPort.purge(D2xxManager.FT_PURGE_RX);
|
||||
String[] uids = readUid();
|
||||
if (uids == null) {
|
||||
System.out.println("uids null fuck");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private int eraseFlash(Parts parts) {
|
||||
int ret = unlock();
|
||||
if (ret != 0) {
|
||||
System.out.println("unlock fail");
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < parts.flash_nb_sectors; i++) {
|
||||
//blank-check
|
||||
ret = send_cmd_sectors('I', i, i);
|
||||
if (ret == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
System.out.println("blank "+i+" fail");
|
||||
return ret;
|
||||
} else {
|
||||
byte buf[] = new byte[REP_BUF];
|
||||
read(buf, 40, 500);
|
||||
}
|
||||
//prepare-for-write
|
||||
ret = send_cmd_sectors('P', i, i);
|
||||
if (ret != 0) {
|
||||
System.out.println("prepare-for-write "+i+" fail");
|
||||
return ret;
|
||||
}
|
||||
//erase
|
||||
ret = send_cmd_sectors('E', i, i);
|
||||
if (ret != 0) {
|
||||
System.out.println("erase "+i+" fail");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int send_cmd_sectors(char cmd, int first_sector, int last_sector) {
|
||||
byte[] buf = new byte[RETURN_CODE_SIZE];
|
||||
int ret;
|
||||
int len;
|
||||
|
||||
if(last_sector < first_sector){
|
||||
return -6;
|
||||
}
|
||||
|
||||
String request = String.format(Locale.getDefault(),"%c %d %d\r\n",cmd, first_sector, last_sector);
|
||||
len = serialPort.write(request.getBytes());
|
||||
if(len != request.getBytes().length){
|
||||
return -5;
|
||||
}
|
||||
len = read(buf,buf.length,500);
|
||||
if(len <= 0){
|
||||
return -4;
|
||||
}
|
||||
|
||||
ret = parseRetCode(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int unlock() {
|
||||
int ret = sendCommand(UNLOCK);
|
||||
if(ret != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void flash(final Server server, final Binary binary, final FlashListener listener) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
flashBinary(server,binary,listener);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private int flashBinary(Server server, Binary binary, FlashListener listener) {
|
||||
public int flashBinary(Server server, Binary binary, FlashListener listener) {
|
||||
Parts parts = DatabaseManager.getInstance(context).getParts(part_id);
|
||||
if(parts == null) {
|
||||
listener.onError(context.getString(R.string.parts_missing));
|
||||
|
@ -504,6 +292,132 @@ public class IspManager {
|
|||
return ret;
|
||||
}
|
||||
|
||||
/**** ISP Commands *****/
|
||||
|
||||
private boolean synchronize(int crystal_freq) {
|
||||
if (serialPort == null)
|
||||
return false;
|
||||
serialPort.purge(D2xxManager.FT_PURGE_RX);
|
||||
serialPort.purge(D2xxManager.FT_PURGE_TX);
|
||||
byte[] buf;
|
||||
if (serialPort.write(SYNC_START) != SYNC_START.length) {
|
||||
System.out.println("Start");
|
||||
return false;
|
||||
}
|
||||
|
||||
buf = new byte[SYNC.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
String result = new String(buf);
|
||||
String expected = new String(SYNC);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
serialPort.write("\r\n".getBytes());
|
||||
System.out.println("Start answer " + result);
|
||||
return false;
|
||||
}
|
||||
if (serialPort.write(SYNC) != SYNC.length) {
|
||||
System.out.println("SYNC write");
|
||||
return false;
|
||||
}
|
||||
clearBuffer(SYNC.length);
|
||||
buf = new byte[SYNC_OK.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
result = new String(buf);
|
||||
expected = new String(SYNC_OK);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
System.out.println("SYNC OK " + result);
|
||||
return false;
|
||||
}
|
||||
|
||||
String freq = String.valueOf(crystal_freq) + "\r\n";
|
||||
if (serialPort.write(freq.getBytes()) != freq.length()) {
|
||||
System.out.println("freq write");
|
||||
return false;
|
||||
}
|
||||
|
||||
clearBuffer(freq.length());
|
||||
buf = new byte[SYNC_OK.length];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
result = new String(buf);
|
||||
expected = new String(SYNC_OK);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
System.out.println("freq answer " + result);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (serialPort.write(SYNC_ECHO_OFF) != SYNC_ECHO_OFF.length) {
|
||||
System.out.println("Sync Echo Off write");
|
||||
return false;
|
||||
}
|
||||
|
||||
clearBuffer(SYNC_ECHO_OFF.length);
|
||||
buf = new byte[RETURN_CODE_SIZE];
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
clearBuffer(REP_BUF);
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private int sendCommand(byte[] cmd) {
|
||||
byte[] buf = new byte[RETURN_CODE_SIZE];
|
||||
if (serialPort.write(cmd) != cmd.length)
|
||||
return -5;
|
||||
int len = read(buf, buf.length, 500);
|
||||
if (len <= 0)
|
||||
return -4;
|
||||
int ret = parseRetCode(buf);
|
||||
if(ret > 9)
|
||||
read(buf,1,50);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int sendCommand(char c, int addr, int count) {
|
||||
int ret;
|
||||
int len;
|
||||
|
||||
byte[] buf = String.format(Locale.getDefault(),"%c %d %d\r\n",c,addr,count).getBytes();
|
||||
len = buf.length;
|
||||
if(len > REP_BUF) {
|
||||
len = REP_BUF;
|
||||
}
|
||||
int sent = serialPort.write(buf,len);
|
||||
if(len != sent) {
|
||||
return -5;
|
||||
}
|
||||
buf = new byte[RETURN_CODE_SIZE];
|
||||
len = read(buf,buf.length,500);
|
||||
if(len <= 0) {
|
||||
return -4;
|
||||
}
|
||||
ret = parseRetCode(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int send_cmd_sectors(char cmd, int first_sector, int last_sector) {
|
||||
byte[] buf = new byte[RETURN_CODE_SIZE];
|
||||
int ret;
|
||||
int len;
|
||||
|
||||
if(last_sector < first_sector){
|
||||
return -6;
|
||||
}
|
||||
|
||||
String request = String.format(Locale.getDefault(),"%c %d %d\r\n",cmd, first_sector, last_sector);
|
||||
len = serialPort.write(request.getBytes());
|
||||
if(len != request.getBytes().length){
|
||||
return -5;
|
||||
}
|
||||
len = read(buf,buf.length,500);
|
||||
if(len <= 0){
|
||||
return -4;
|
||||
}
|
||||
|
||||
ret = parseRetCode(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int send_cmd_address(char c, int flash_addr, int ram_addr, int write_size) {
|
||||
int ret;
|
||||
int len;
|
||||
|
@ -607,10 +521,61 @@ public class IspManager {
|
|||
return ret;
|
||||
}
|
||||
|
||||
private int parseRetCode(byte[] data) {
|
||||
String str = new String(data,0,2);
|
||||
str = str.replace("\r", "");
|
||||
str = str.replace("\n", "");
|
||||
if (!str.matches("[0-9 -+]*"))
|
||||
return -1;
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
private int eraseFlash(Parts parts) {
|
||||
int ret = unlock();
|
||||
if (ret != 0) {
|
||||
System.out.println("unlock fail");
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < parts.flash_nb_sectors; i++) {
|
||||
//blank-check
|
||||
ret = send_cmd_sectors('I', i, i);
|
||||
if (ret == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
System.out.println("blank "+i+" fail");
|
||||
return ret;
|
||||
} else {
|
||||
byte buf[] = new byte[REP_BUF];
|
||||
read(buf, 40, 500);
|
||||
}
|
||||
//prepare-for-write
|
||||
ret = send_cmd_sectors('P', i, i);
|
||||
if (ret != 0) {
|
||||
System.out.println("prepare-for-write "+i+" fail");
|
||||
return ret;
|
||||
}
|
||||
//erase
|
||||
ret = send_cmd_sectors('E', i, i);
|
||||
if (ret != 0) {
|
||||
System.out.println("erase "+i+" fail");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int unlock() {
|
||||
int ret = sendCommand(UNLOCK);
|
||||
if(ret != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**** Miscellaneous Functions ****/
|
||||
|
||||
private int uu_encode(byte[] data_out, byte[] data, int offset, int orig_size) {
|
||||
//TODO
|
||||
System.out.println("uu "+offset+" "+orig_size);
|
||||
int new_size = 0;
|
||||
int pos = offset;
|
||||
while(pos < offset+orig_size) {
|
||||
|
@ -686,8 +651,7 @@ public class IspManager {
|
|||
val[5] = byteArrayToInt(data, 20);
|
||||
val[6] = byteArrayToInt(data, 24);
|
||||
|
||||
int sum = 0 - val[0] - val[1] - val[2] - val[3] - val[4] - val[5] - val[6];
|
||||
return sum;
|
||||
return 0 - val[0] - val[1] - val[2] - val[3] - val[4] - val[5] - val[6];
|
||||
}
|
||||
|
||||
private void fill_data_with_0(byte[] data, int offset, int flash_size) {
|
||||
|
@ -737,6 +701,33 @@ public class IspManager {
|
|||
return write_size;
|
||||
}
|
||||
|
||||
private int read(byte[] buffer, int size, int timeout) {
|
||||
long startTime = System.nanoTime();
|
||||
long timeoutNano = timeout * 1000000;
|
||||
int sizeRead = 0;
|
||||
|
||||
ByteBuffer output = ByteBuffer.wrap(buffer, 0, size);
|
||||
|
||||
while (sizeRead < size) {
|
||||
int sizeToRead = serialPort.getQueueStatus();
|
||||
if (sizeRead + sizeToRead > size)
|
||||
sizeToRead = size - sizeRead;
|
||||
byte[] buf = new byte[sizeToRead];
|
||||
if (sizeToRead > 0) {
|
||||
int sizeReceived = serialPort.read(buf, sizeToRead, 50);
|
||||
if (sizeReceived < 0)
|
||||
return sizeReceived;
|
||||
sizeRead += sizeReceived;
|
||||
if (sizeReceived != 0)
|
||||
output.put(buf, 0, sizeReceived);
|
||||
}
|
||||
long time = System.nanoTime();
|
||||
if (time - startTime >= timeoutNano)
|
||||
break;
|
||||
}
|
||||
return sizeRead;
|
||||
}
|
||||
|
||||
private int byteArrayToInt(byte[] array, int offset) {
|
||||
int value = 0;
|
||||
value |= (array[offset] & 0xff);
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
<string name="prepare_write_error">Prepare for write fail</string>
|
||||
<string name="write_ram_error">write data in ram fail</string>
|
||||
<string name="copy_to_flash_error">Copy to flash fail</string>
|
||||
<string name="dev_name">Name: </string>
|
||||
<array name="https">
|
||||
<item>http://</item>
|
||||
<item>https://</item>
|
||||
|
|
Loading…
Reference in a new issue