Flash LPC management
This commit is contained in:
parent
8e30b52b66
commit
f9f50262b5
39 changed files with 1314 additions and 167 deletions
|
@ -1,13 +1,25 @@
|
|||
package fr.mobdev.lpcprog.activity;
|
||||
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.View;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.mobdev.lpcprog.adapters.BinariesAdapter;
|
||||
import fr.mobdev.lpcprog.listener.NetworkListener;
|
||||
import fr.mobdev.lpcprog.managers.DatabaseManager;
|
||||
import fr.mobdev.lpcprog.managers.IspManager;
|
||||
import fr.mobdev.lpcprog.R;
|
||||
import fr.mobdev.lpcprog.managers.NetworkManager;
|
||||
import fr.mobdev.lpcprog.objects.Server;
|
||||
import fr.mobdev.lpcprog.objects.USBDevice;
|
||||
import fr.mobdev.lpcprog.managers.UsbCommManager;
|
||||
|
||||
|
@ -15,12 +27,18 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
|
||||
private UsbCommManager comm;
|
||||
private USBDevice dev;
|
||||
private List<Server> servers;
|
||||
private long part_id;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.device);
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.device_toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
servers = new ArrayList<>();
|
||||
comm = UsbCommManager.getInstance(this);
|
||||
part_id = 0;
|
||||
List<USBDevice> devices = comm.getDevices();
|
||||
int pid = getIntent().getIntExtra("PID",-1);
|
||||
int vid = getIntent().getIntExtra("VID",-1);
|
||||
|
@ -40,6 +58,12 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
product.setText(String.format("Product id: %04x", dev.PID));
|
||||
}
|
||||
doUsb();
|
||||
ProgressBar bar = (ProgressBar) findViewById(R.id.progress_browse);
|
||||
bar.setVisibility(View.GONE);
|
||||
|
||||
RecyclerView list = (RecyclerView) findViewById(R.id.bin_list);
|
||||
list.setLayoutManager(new LinearLayoutManager(this));
|
||||
updateRepositories();
|
||||
}
|
||||
|
||||
private void doUsb(){
|
||||
|
@ -48,7 +72,7 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
@Override
|
||||
public void run() {
|
||||
boolean conn = comm.openConnection(dev);
|
||||
IspManager manager = IspManager.getInstance();
|
||||
IspManager manager = IspManager.getInstance(DeviceActivity.this);
|
||||
String uidStr = "No UID Found or error";
|
||||
String bootStr = "No Boot Version Found or error";
|
||||
String partidStr = "No Part ID Found or error";
|
||||
|
@ -71,6 +95,8 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
}
|
||||
if(partid != null){
|
||||
partidStr = String.format("Part Id %08x",Long.parseLong(partid));
|
||||
part_id = Long.parseLong(partid);
|
||||
IspManager.getInstance(DeviceActivity.this).setPartId(part_id);
|
||||
}
|
||||
}
|
||||
updateIDS(uidStr,partidStr,bootStr);
|
||||
|
@ -94,4 +120,79 @@ public class DeviceActivity extends AppCompatActivity {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateRepositories(){
|
||||
System.out.println("Update Repo");
|
||||
NetworkListener listener = new NetworkListener() {
|
||||
@Override
|
||||
public void startBinaries() {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ProgressBar bar = (ProgressBar) findViewById(R.id.progress_browse);
|
||||
bar.setVisibility(View.VISIBLE);
|
||||
RecyclerView list = (RecyclerView) findViewById(R.id.bin_list);
|
||||
list.setVisibility(View.GONE);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startServer(Server server) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endServer(Server server) {
|
||||
if(!server.binaries.isEmpty())
|
||||
servers.add(server);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endBinaries() {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ProgressBar bar = (ProgressBar) findViewById(R.id.progress_browse);
|
||||
bar.setVisibility(View.GONE);
|
||||
RecyclerView list = (RecyclerView) findViewById(R.id.bin_list);
|
||||
list.setVisibility(View.VISIBLE);
|
||||
BinariesAdapter adapter = new BinariesAdapter(servers, DeviceActivity.this);
|
||||
list.setAdapter(adapter);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(final String error) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ProgressBar bar = (ProgressBar) findViewById(R.id.progress_browse);
|
||||
bar.setVisibility(View.GONE);
|
||||
final Snackbar msg = Snackbar.make(findViewById(R.id.bin_list), error,Snackbar.LENGTH_INDEFINITE);
|
||||
msg.setAction(R.string.retry_browse, new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
msg.dismiss();
|
||||
updateRepositories();
|
||||
}
|
||||
});
|
||||
msg.show();
|
||||
List<List<Server>> localServers = DatabaseManager.getInstance(DeviceActivity.this).getServers();
|
||||
servers.addAll(localServers.get(0));
|
||||
servers.addAll(localServers.get(1));
|
||||
servers.addAll(localServers.get(2));
|
||||
RecyclerView list = (RecyclerView) findViewById(R.id.bin_list);
|
||||
list.setVisibility(View.VISIBLE);
|
||||
BinariesAdapter adapter = new BinariesAdapter(servers,DeviceActivity.this);
|
||||
list.setAdapter(adapter);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
NetworkManager.getInstance(listener,this).browseBinaries();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,289 @@
|
|||
package fr.mobdev.lpcprog.adapters;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.DialogInterface;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import fr.mobdev.lpcprog.R;
|
||||
import fr.mobdev.lpcprog.listener.DownloadListener;
|
||||
import fr.mobdev.lpcprog.listener.FlashListener;
|
||||
import fr.mobdev.lpcprog.managers.DatabaseManager;
|
||||
import fr.mobdev.lpcprog.managers.IspManager;
|
||||
import fr.mobdev.lpcprog.managers.NetworkManager;
|
||||
import fr.mobdev.lpcprog.objects.Binary;
|
||||
import fr.mobdev.lpcprog.objects.Server;
|
||||
|
||||
public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
|
||||
private static final int SECTION = 0;
|
||||
private static final int FIELD = 1;
|
||||
|
||||
private List<Server> servers;
|
||||
private int count;
|
||||
private Activity activity;
|
||||
|
||||
public BinariesAdapter(List<Server> servers, Activity activity) {
|
||||
this.servers = servers;
|
||||
this.activity = activity;
|
||||
countElements();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view;
|
||||
if (viewType == SECTION) {
|
||||
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.server_section, parent, false);
|
||||
return new SectionHolder(view);
|
||||
} else {
|
||||
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.binary_item, parent, false);
|
||||
return new BinaryHolder(view);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||
if (holder instanceof SectionHolder) {
|
||||
SectionHolder sectionHolder = (SectionHolder) holder;
|
||||
int section = getSection(position);
|
||||
System.out.println("pos " + position + " section " + section);
|
||||
String sectionName = servers.get(section).url.toString();
|
||||
sectionHolder.setupSection(sectionName);
|
||||
} else if (holder instanceof BinaryHolder) {
|
||||
BinaryHolder binaryHolder = (BinaryHolder) holder;
|
||||
|
||||
int section = getSection(position);
|
||||
int pos = getRealPosition(position);
|
||||
Server server = servers.get(section);
|
||||
Binary binary = servers.get(section).binaries.get(pos);
|
||||
binaryHolder.setupBinary(server,binary,activity);
|
||||
}
|
||||
}
|
||||
|
||||
private void countElements() {
|
||||
int size = 0;
|
||||
for (int i = 0; i < servers.size(); i++) {
|
||||
if (servers.get(i).binaries.size() > 0) {
|
||||
size++;
|
||||
size += servers.get(i).binaries.size();
|
||||
}
|
||||
}
|
||||
count = size;
|
||||
}
|
||||
|
||||
private int getRealPosition(int fakePosition) {
|
||||
System.out.println("get real " + fakePosition);
|
||||
fakePosition -= 1;
|
||||
for (int i = 0; i < servers.size(); i++) {
|
||||
int serverSize = servers.get(i).binaries.size();
|
||||
if (fakePosition - serverSize < 0)
|
||||
return fakePosition;
|
||||
if (serverSize > 0) {
|
||||
fakePosition -= 1;
|
||||
fakePosition -= serverSize;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getSection(int fakePosition) {
|
||||
int prev_size = 1;
|
||||
for (int i = 0; i < servers.size(); i++) {
|
||||
int serverSize = servers.get(i).binaries.size();
|
||||
if (fakePosition < prev_size + serverSize)
|
||||
return i;
|
||||
if (servers.get(i).binaries.size() > 0) {
|
||||
prev_size += 1;
|
||||
prev_size += serverSize;
|
||||
}
|
||||
}
|
||||
return -1;//error
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
int nbValues = 0;
|
||||
for (int i = 0; i < servers.size(); i++) {
|
||||
if (position == i + nbValues)
|
||||
return SECTION;
|
||||
else if (position < i + nbValues)
|
||||
break;
|
||||
nbValues += servers.get(i).binaries.size();
|
||||
}
|
||||
return FIELD;
|
||||
}
|
||||
}
|
||||
|
||||
class BinaryHolder extends RecyclerView.ViewHolder{
|
||||
DownloadListener listener;
|
||||
public BinaryHolder(View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
|
||||
public void setupBinary(final Server server, final Binary binary, final Activity activity){
|
||||
|
||||
TextView name = (TextView) itemView.findViewById(R.id.binary_name);
|
||||
TextView version = (TextView) itemView.findViewById(R.id.binary_version);
|
||||
|
||||
final LinearLayout downloadedLayout = (LinearLayout) itemView.findViewById(R.id.layout_downloaded);
|
||||
final ImageView deleteView = (ImageView) itemView.findViewById(R.id.delete_download);
|
||||
final ImageView flashView = (ImageView) itemView.findViewById(R.id.flash_download);
|
||||
final ImageView downloadView = (ImageView) itemView.findViewById(R.id.download);
|
||||
final ImageView stopView = (ImageView) itemView.findViewById(R.id.cancel_download);
|
||||
final ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.progress_download);
|
||||
|
||||
name.setText(binary.name);
|
||||
version.setText("V "+binary.version);
|
||||
if(binary.isDownloaded) {
|
||||
downloadView.setVisibility(View.GONE);
|
||||
downloadedLayout.setVisibility(View.VISIBLE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
} else if(binary.isDownloading){
|
||||
downloadView.setVisibility(View.GONE);
|
||||
downloadedLayout.setVisibility(View.GONE);
|
||||
stopView.setVisibility(View.VISIBLE);
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
downloadView.setVisibility(View.VISIBLE);
|
||||
downloadedLayout.setVisibility(View.GONE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
listener = new DownloadListener() {
|
||||
|
||||
@Override
|
||||
public void downloadCanceled(Server server, Binary binary) {
|
||||
binary.isDownloading = false;
|
||||
binary.progress = 0;
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
downloadView.setVisibility(View.VISIBLE);
|
||||
downloadedLayout.setVisibility(View.GONE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadSuccessful(Server server, Binary binary) {
|
||||
binary.isDownloading = false;
|
||||
binary.progress = 0;
|
||||
binary.isDownloaded = true;
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
downloadView.setVisibility(View.GONE);
|
||||
downloadedLayout.setVisibility(View.VISIBLE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadError(Server server, Binary binary, final String error) {
|
||||
binary.isDownloading = false;
|
||||
binary.progress = 0;
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
downloadView.setVisibility(View.VISIBLE);
|
||||
downloadedLayout.setVisibility(View.GONE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
Snackbar.make(activity.findViewById(R.id.bin_list), error,Snackbar.LENGTH_LONG);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(Server server, Binary binary, final int progress) {
|
||||
binary.progress = progress;
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
progressBar.setProgress(progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
downloadView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
binary.isDownloading = true;
|
||||
binary.progress = 0;
|
||||
NetworkManager.getInstance(null,itemView.getContext()).retrieveBinary(server,binary,listener);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
deleteView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
|
||||
builder.setMessage(itemView.getContext().getString(R.string.delete_binary_message)+" "+binary.name+" "+
|
||||
itemView.getContext().getString(R.string.version)+" "+binary.version+" ?")
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
//delete server from database and update the view
|
||||
File file = new File(itemView.getContext().getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename);
|
||||
if(file.exists())
|
||||
file.delete();
|
||||
binary.isDownloaded = false;
|
||||
DatabaseManager.getInstance(itemView.getContext()).deleteBinary(binary.id);
|
||||
//file and reset all downloaded items
|
||||
downloadView.setVisibility(View.VISIBLE);
|
||||
downloadedLayout.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
stopView.setVisibility(View.GONE);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
dialog.cancel();
|
||||
}
|
||||
});
|
||||
AlertDialog alert = builder.create();
|
||||
alert.show();
|
||||
}
|
||||
});
|
||||
|
||||
final FlashListener flashListener = new FlashListener() {
|
||||
@Override
|
||||
public void onError(String string) {
|
||||
System.out.println("flash_error "+string);
|
||||
}
|
||||
};
|
||||
|
||||
flashView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
IspManager.getInstance(itemView.getContext()).flash(server,binary,flashListener);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package fr.mobdev.lpcprog.listener;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
import fr.mobdev.lpcprog.objects.Binary;
|
||||
import fr.mobdev.lpcprog.objects.Server;
|
||||
|
||||
public interface DownloadListener extends EventListener {
|
||||
void downloadCanceled(Server server, Binary binary);
|
||||
void downloadSuccessful(Server server, Binary binary);
|
||||
void downloadError(Server server, Binary binary, String string);
|
||||
void progress(Server server, Binary binary, int progress);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package fr.mobdev.lpcprog.listener;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
public interface FlashListener extends EventListener{
|
||||
|
||||
void onError(String string);
|
||||
}
|
|
@ -29,9 +29,4 @@ public interface NetworkListener extends EventListener{
|
|||
void endServer(Server server);
|
||||
void endBinaries();
|
||||
void onError(String string);
|
||||
void downloadCanceled(Server server, Binary binary);
|
||||
void downloadSuccessful(Server server, Binary binary);
|
||||
void downloadFailed(Server server, Binary binary);
|
||||
void downloadError(Server server, Binary binary, String string);
|
||||
void progress(Server server, Binary binary, int progress);
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ import java.net.URL;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.mobdev.lpcprog.objects.Binary;
|
||||
import fr.mobdev.lpcprog.objects.Parts;
|
||||
import fr.mobdev.lpcprog.objects.Server;
|
||||
|
||||
public class DatabaseManager extends SQLiteOpenHelper {
|
||||
|
@ -47,14 +49,31 @@ public class DatabaseManager extends SQLiteOpenHelper {
|
|||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("Create table if not exists binaries (" +
|
||||
"id integer primary key autoincrement, name varchar(1024), short_hash varchar(1024), real_short_hash varchar(1024), date INTEGER, storage_duration INTEGER ,thumb TEXT, token varchar(1024));");
|
||||
"id integer primary key autoincrement, server_id INTEGER, name varchar(1024), filename varchar(1024), version INTEGER, path varchar(1024), sha1 varchar(1024));");
|
||||
db.execSQL("Create table if not exists servers (" +
|
||||
"id integer primary key autoincrement, url varchar(1024), isActive INTEGER, attempted INTEGER);");
|
||||
db.execSQL("Create table if not exists definitions (" +
|
||||
"id integer primary key autoincrement, part_id INTEGER, part_name varchar(1024), fl_base_addr INTEGER, fl_size INTEGER, fl_nb_sect INTEGER," +
|
||||
"reset_vector_offset INTEGER, ram_base_addr INTEGER, ram_size INTEGER, ram_buf_off INTEGER, ram_buf_size INTEGER, UU_encode INTEGER);");
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("url","https://wiki.mob-dev.fr/files/");
|
||||
values.put("isActive",1);
|
||||
values.put("attempted",0);
|
||||
db.insert("servers",null,values);
|
||||
|
||||
values.clear();
|
||||
values.put("part_id",0x3640C02B);
|
||||
values.put("part_name","LPC1224FBD48/101");
|
||||
values.put("fl_base_addr",0x0);
|
||||
values.put("fl_size",0x8000);
|
||||
values.put("fl_nb_sect",8);
|
||||
values.put("reset_vector_offset",0x04);
|
||||
values.put("ram_base_addr",0x10000000);
|
||||
values.put("ram_size",0x1000);
|
||||
values.put("ram_buf_off",0x800);
|
||||
values.put("ram_buf_size",0x400);
|
||||
values.put("UU_encode",true);
|
||||
db.insert("definitions",null,values);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -106,6 +125,7 @@ public class DatabaseManager extends SQLiteOpenHelper {
|
|||
int isActive = cursor.getInt(col++);
|
||||
server.isActive = isActive != 0;
|
||||
server.attempted = cursor.getInt(col);
|
||||
server.binaries = getBinaries(server.id);
|
||||
if (server.isActive && server.attempted == 0)
|
||||
active.add(server);
|
||||
else if (server.isActive && server.attempted != 0)
|
||||
|
@ -120,6 +140,91 @@ public class DatabaseManager extends SQLiteOpenHelper {
|
|||
return list;
|
||||
}
|
||||
|
||||
private List<Binary> getBinaries(long id) {
|
||||
List<Binary> binaries = new ArrayList<>();
|
||||
String whereClause = "server_id = ?";
|
||||
String[] whereArgs = new String[1];
|
||||
whereArgs[0] = String.valueOf(id);
|
||||
Cursor cursor = getReadableDatabase().query("binaries", null, whereClause, whereArgs, null, null, null);
|
||||
while (cursor.moveToNext()){
|
||||
System.out.println("Found a binary in db");
|
||||
int col = 0;
|
||||
Binary b = new Binary();
|
||||
b.id = cursor.getLong(col++);
|
||||
col++;//ignore server id
|
||||
b.name = cursor.getString(col++);
|
||||
b.filename = cursor.getString(col++);
|
||||
b.version = cursor.getInt(col++);
|
||||
b.path = cursor.getString(col++);
|
||||
b.sha1 = cursor.getString(col);
|
||||
b.isDownloaded = true;
|
||||
b.progress = 0;
|
||||
b.isDownloading = false;
|
||||
binaries.add(b);
|
||||
}
|
||||
cursor.close();
|
||||
return binaries;
|
||||
}
|
||||
|
||||
public void serverFail(Server server) {
|
||||
server.attempted++;
|
||||
if(server.attempted == 5){
|
||||
server.attempted = 0;
|
||||
server.isActive = false;
|
||||
}
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("attempted",server.attempted);
|
||||
values.put("isActive",server.isActive);
|
||||
String whereClause = "id = ?";
|
||||
String[] whereArgs = new String[1];
|
||||
whereArgs[0] = String.valueOf(server.id);
|
||||
getWritableDatabase().update("servers",values,whereClause,whereArgs);
|
||||
}
|
||||
|
||||
public void addBinary(Server server, Binary binary) {
|
||||
System.out.println("add binary");
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("server_id",server.id);
|
||||
values.put("name",binary.name);
|
||||
values.put("filename",binary.filename);
|
||||
values.put("version",binary.version);
|
||||
values.put("path",binary.path);
|
||||
values.put("sha1",binary.sha1);
|
||||
values.put("name",binary.name);
|
||||
getWritableDatabase().insert("binaries",null,values);
|
||||
}
|
||||
|
||||
public void deleteBinary(long id) {
|
||||
String whereClause = "id = ?";
|
||||
String[] whereArgs = new String[1];
|
||||
whereArgs[0] = String.valueOf(id);
|
||||
getWritableDatabase().delete("binaries",whereClause,whereArgs);
|
||||
}
|
||||
|
||||
public Parts getParts(long part_id) {
|
||||
Parts parts = null;
|
||||
String whereClause = "part_id = ?";
|
||||
String[] whereArgs = new String[1];
|
||||
whereArgs[0] = String.valueOf(part_id);
|
||||
System.out.println("part_id "+part_id);
|
||||
Cursor cursor = getReadableDatabase().query("definitions", null, whereClause, whereArgs, null, null, null);
|
||||
if (cursor.moveToNext()) {
|
||||
parts = new Parts();
|
||||
int col = 0;
|
||||
parts.id = cursor.getLong(col++);
|
||||
parts.part_id = cursor.getInt(col++);
|
||||
parts.part_name = cursor.getString(col++);
|
||||
parts.flash_base_addr = cursor.getInt(col++);
|
||||
parts.flash_size = cursor.getInt(col++);
|
||||
parts.flash_nb_sectors = cursor.getInt(col++);
|
||||
parts.reset_vector_offset = cursor.getInt(col++);
|
||||
parts.ram_base_addr = cursor.getInt(col++);
|
||||
parts.ram_size = cursor.getInt(col++);
|
||||
parts.ram_buffer_offset = cursor.getInt(col++);
|
||||
parts.ram_buffer_size = cursor.getInt(col++);
|
||||
parts.uuencode = cursor.getInt(col) == 1;
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,54 +1,77 @@
|
|||
package fr.mobdev.lpcprog.managers;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.ftdi.j2xx.D2xxManager;
|
||||
import com.ftdi.j2xx.FT_Device;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Locale;
|
||||
|
||||
import fr.mobdev.lpcprog.R;
|
||||
import fr.mobdev.lpcprog.listener.FlashListener;
|
||||
import fr.mobdev.lpcprog.objects.Binary;
|
||||
import fr.mobdev.lpcprog.objects.Parts;
|
||||
import fr.mobdev.lpcprog.objects.Server;
|
||||
import fr.mobdev.lpcprog.objects.USBDevice;
|
||||
|
||||
public class IspManager {
|
||||
|
||||
//defines
|
||||
private static final int REP_BUF = 100;
|
||||
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\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 int SERIAL_BUF_SIZE = 1300;
|
||||
private static final int LINE_DATA_LENGTH = 45;
|
||||
private static final int LINES_PER_BLOCK = 20;
|
||||
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;
|
||||
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();
|
||||
private static final byte[] READ_BOOT_VERSION = "K\r\n".getBytes();
|
||||
private static final byte[] UNLOCK = "U 23130\r\n".getBytes();
|
||||
|
||||
//members
|
||||
private static IspManager instance = null;
|
||||
private FT_Device serialPort = null;
|
||||
private Context context;
|
||||
private long part_id = 0;
|
||||
|
||||
private IspManager(){
|
||||
|
||||
private IspManager(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public static IspManager getInstance(){
|
||||
if(instance == null)
|
||||
instance = new IspManager();
|
||||
public static IspManager getInstance(Context context) {
|
||||
if (instance == null)
|
||||
instance = new IspManager(context);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean setupDevice(USBDevice device, int baudRate){
|
||||
public void setPartId(long part){
|
||||
part_id = part;
|
||||
}
|
||||
|
||||
public boolean setupDevice(USBDevice device, int baudRate) {
|
||||
serialPort = device.device;
|
||||
if(serialPort != null)
|
||||
if (serialPort != null)
|
||||
serialPort.resetDevice();
|
||||
else
|
||||
System.out.println("device is null fuck");
|
||||
|
||||
if(serialPort!=null && serialPort.isOpen()){
|
||||
if (serialPort != null && serialPort.isOpen()) {
|
||||
serialPort.setBaudRate(baudRate);
|
||||
byte dc1=0x11;
|
||||
byte dc3=0x13;
|
||||
serialPort.setFlowControl(D2xxManager.FT_FLOW_XON_XOFF,dc1,dc3);
|
||||
serialPort.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8,D2xxManager.FT_STOP_BITS_1,D2xxManager.FT_PARITY_NONE);
|
||||
byte dc1 = 0x11;
|
||||
byte dc3 = 0x13;
|
||||
serialPort.setFlowControl(D2xxManager.FT_FLOW_XON_XOFF, dc1, dc3);
|
||||
serialPort.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8, D2xxManager.FT_STOP_BITS_1, D2xxManager.FT_PARITY_NONE);
|
||||
byte l = 0x2;
|
||||
serialPort.setLatencyTimer(l);
|
||||
return true;
|
||||
|
@ -56,174 +79,164 @@ public class IspManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean synchronize(int crystal_freq){
|
||||
if(serialPort == null)
|
||||
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){
|
||||
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);
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
String result = new String(buf);
|
||||
String expected = new String(SYNC);
|
||||
if(result.compareTo(expected) != 0) {
|
||||
printbuffer(buf);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
serialPort.write("\r\n".getBytes());
|
||||
System.out.println("Start answer "+result);
|
||||
System.out.println("Start answer " + result);
|
||||
return false;
|
||||
}
|
||||
if(serialPort.write(SYNC) != SYNC.length) {
|
||||
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);
|
||||
serialPort.read(buf, buf.length, 500);
|
||||
result = new String(buf);
|
||||
expected = new String(SYNC_OK);
|
||||
if(result.compareTo(expected) != 0) {
|
||||
printbuffer(buf);
|
||||
System.out.println("SYNC OK "+result);
|
||||
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()) {
|
||||
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);
|
||||
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);
|
||||
printbuffer(buf);
|
||||
if (result.compareTo(expected) != 0) {
|
||||
System.out.println("freq answer " + result);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(serialPort.write(SYNC_ECHO_OFF) != SYNC_ECHO_OFF.length) {
|
||||
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[3];
|
||||
int ret = serialPort.read(buf,buf.length,500);
|
||||
System.out.println(String.format(Locale.getDefault(),"%d %02x %02x %02x",ret,buf[0],buf[1],buf[2]));
|
||||
int ret = serialPort.read(buf, buf.length, 500);
|
||||
System.out.println(String.format(Locale.getDefault(), "%d %02x %02x %02x", ret, buf[0], buf[1], buf[2]));
|
||||
return true;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void printbuffer(byte[] buffer){
|
||||
for(byte b : buffer){
|
||||
System.out.println(String.format("%02x",b));
|
||||
}
|
||||
}
|
||||
|
||||
private void clearBuffer(int size){
|
||||
if(serialPort == null)
|
||||
private void clearBuffer(int size) {
|
||||
if (serialPort == null)
|
||||
return;
|
||||
|
||||
byte[] buf = new byte[size];
|
||||
int readed = read(buf,size,500);
|
||||
System.out.println("end "+(size-readed));
|
||||
int readed = read(buf, size, 500);
|
||||
System.out.println("end " + (size - readed));
|
||||
}
|
||||
|
||||
public String[] readUid(){
|
||||
if(serialPort == null)
|
||||
public String[] readUid() {
|
||||
if (serialPort == null)
|
||||
return null;
|
||||
int ret = sendCommand(READ_UID,null);
|
||||
int ret = sendCommand(READ_UID, null);
|
||||
|
||||
if(ret != 0){
|
||||
System.out.println("Send Command "+ret);
|
||||
if (ret != 0) {
|
||||
System.out.println("Send Command " + ret);
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] buf = new byte[REP_BUF];
|
||||
|
||||
int size = read(buf,50,500);
|
||||
for(int i = 0; i < size; i++){
|
||||
int size = read(buf, 50, 500);
|
||||
for (int i = 0; i < size; i++) {
|
||||
System.out.println(String.format("%02x", buf[i]));
|
||||
}
|
||||
if(size<=0){
|
||||
System.out.println("Send size "+size);
|
||||
if (size <= 0) {
|
||||
System.out.println("Send size " + size);
|
||||
return null;
|
||||
}
|
||||
String[] uids = new String[4];
|
||||
String buffer = new String(buf);
|
||||
|
||||
if(buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n","");
|
||||
else if(!buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n","\r");
|
||||
if (buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n", "");
|
||||
else if (!buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n", "\r");
|
||||
|
||||
for(int i = 0; i < 4; i++){
|
||||
if(buffer.contains("\r")) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (buffer.contains("\r")) {
|
||||
uids[i] = buffer.substring(0, buffer.indexOf("\r"));
|
||||
buffer = buffer.substring(buffer.indexOf("\r")+1);
|
||||
buffer = buffer.substring(buffer.indexOf("\r") + 1);
|
||||
}
|
||||
}
|
||||
System.out.println("uids ok");
|
||||
return uids;
|
||||
}
|
||||
|
||||
public String readPartId(){
|
||||
if(serialPort == null)
|
||||
public String readPartId() {
|
||||
if (serialPort == null)
|
||||
return null;
|
||||
int ret = sendCommand(READ_PART_ID,null);
|
||||
if(ret != 0)
|
||||
int ret = sendCommand(READ_PART_ID, null);
|
||||
if (ret != 0)
|
||||
return null;
|
||||
byte[] buf = new byte[REP_BUF];
|
||||
int size = read(buf,20,500);
|
||||
if(size <= 0)
|
||||
int size = read(buf, 20, 500);
|
||||
if (size <= 0)
|
||||
return null;
|
||||
String partId = new String(buf);
|
||||
partId = partId.substring(0,partId.indexOf("\r\n"));
|
||||
if(partId.contains("\r"))
|
||||
partId = partId.replace("\r","");
|
||||
if(partId.contains("\n"))
|
||||
partId = partId.replace("\n","");
|
||||
partId = partId.substring(0, partId.indexOf("\r\n"));
|
||||
if (partId.contains("\r"))
|
||||
partId = partId.replace("\r", "");
|
||||
if (partId.contains("\n"))
|
||||
partId = partId.replace("\n", "");
|
||||
return partId;
|
||||
}
|
||||
|
||||
public String[] readBootVersion(){
|
||||
if(serialPort == null)
|
||||
public String[] readBootVersion() {
|
||||
if (serialPort == null)
|
||||
return null;
|
||||
int ret = sendCommand(READ_BOOT_VERSION,null);
|
||||
if(ret != 0){
|
||||
System.out.println("Send Command "+ret);
|
||||
int ret = sendCommand(READ_BOOT_VERSION, null);
|
||||
if (ret != 0) {
|
||||
System.out.println("Send Command " + ret);
|
||||
return null;
|
||||
}
|
||||
byte[] buf = new byte[REP_BUF];
|
||||
|
||||
int size = read(buf,50,500);
|
||||
int size = read(buf, 50, 500);
|
||||
|
||||
if(size <= 0){
|
||||
System.out.println("Receive "+size);
|
||||
if (size <= 0) {
|
||||
System.out.println("Receive " + size);
|
||||
return null;
|
||||
}
|
||||
String[] version = new String[2];
|
||||
String buffer = new String(buf);
|
||||
|
||||
if(buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n","");
|
||||
else if(!buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n","\r");
|
||||
if (buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n", "");
|
||||
else if (!buffer.contains("\r") && buffer.contains("\n"))
|
||||
buffer = buffer.replace("\n", "\r");
|
||||
|
||||
for(int i = 0; i < 2; i++){
|
||||
if(buffer.contains("\r")) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (buffer.contains("\r")) {
|
||||
version[i] = buffer.substring(0, buffer.indexOf("\r"));
|
||||
buffer = buffer.substring(buffer.indexOf("\r")+1);
|
||||
buffer = buffer.substring(buffer.indexOf("\r") + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -231,33 +244,58 @@ public class IspManager {
|
|||
return version;
|
||||
}
|
||||
|
||||
public int sendCommand(byte[] cmd, String[] args){
|
||||
private int sendCommand(byte[] cmd, String[] args) {
|
||||
if(args != null)//FIXME
|
||||
return -1;
|
||||
byte[] buf = new byte[3];
|
||||
if(serialPort.write(cmd) != cmd.length)
|
||||
if (serialPort.write(cmd) != cmd.length)
|
||||
return -5;
|
||||
int len = read(buf, buf.length,500);
|
||||
System.out.println("Len "+len);
|
||||
for(int i = 0; i < 3; i++){
|
||||
int len = read(buf, buf.length, 500);
|
||||
System.out.println("Len " + len);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
System.out.println(String.format("%02x", buf[i]));
|
||||
}
|
||||
if(len <= 0)
|
||||
if (len <= 0)
|
||||
return -4;
|
||||
return parseRetCode(buf);
|
||||
}
|
||||
|
||||
public int read(byte[] buffer, int size, int timeout){
|
||||
private int sendCommand(byte[] cmd, char c, int addr, int count) {
|
||||
int ret = 0;
|
||||
int len = 0;
|
||||
|
||||
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[3];
|
||||
len = read(buf,3,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);
|
||||
ByteBuffer output = ByteBuffer.wrap(buffer, 0, size);
|
||||
|
||||
while(sizeRead < size){
|
||||
while (sizeRead < size) {
|
||||
int sizeToRead = serialPort.getQueueStatus();
|
||||
if(sizeRead + sizeToRead > size)
|
||||
if (sizeRead + sizeToRead > size)
|
||||
sizeToRead = size - sizeRead;
|
||||
byte[] buf = new byte[sizeToRead];
|
||||
if(sizeToRead > 0) {
|
||||
if (sizeToRead > 0) {
|
||||
int sizeReceived = serialPort.read(buf, sizeToRead, 50);
|
||||
if (sizeReceived < 0)
|
||||
return sizeReceived;
|
||||
|
@ -266,21 +304,22 @@ public class IspManager {
|
|||
output.put(buf, 0, sizeReceived);
|
||||
}
|
||||
long time = System.nanoTime();
|
||||
if(time - startTime >= timeoutNano)
|
||||
if (time - startTime >= timeoutNano)
|
||||
break;
|
||||
}
|
||||
return sizeRead;
|
||||
}
|
||||
|
||||
private int parseRetCode(byte[] data){
|
||||
private int parseRetCode(byte[] data) {
|
||||
String str = new String(data);
|
||||
str = str.replace("\r\n","");
|
||||
|
||||
str = str.replace("\r\n", "");
|
||||
if (!str.matches("[0-9 -+]*"))
|
||||
return -1;
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
public boolean synchronizeIfPossible(int crystal_freq) {
|
||||
if(!synchronize(crystal_freq)) {
|
||||
if (!synchronize(crystal_freq)) {
|
||||
serialPort.purge(D2xxManager.FT_PURGE_TX);
|
||||
serialPort.purge(D2xxManager.FT_PURGE_RX);
|
||||
String[] uids = readUid();
|
||||
|
@ -293,26 +332,413 @@ public class IspManager {
|
|||
return true;
|
||||
}
|
||||
|
||||
/*private int byteArrayToInt(byte[] array, int offset){
|
||||
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[3];
|
||||
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,3,500);
|
||||
if(len <= 0){
|
||||
return -4;
|
||||
}
|
||||
|
||||
ret = parseRetCode(buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int unlock() {
|
||||
int ret = sendCommand(UNLOCK,null);
|
||||
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) {
|
||||
Parts parts = DatabaseManager.getInstance(context).getParts(part_id);
|
||||
if(parts == null) {
|
||||
listener.onError(context.getString(R.string.parts_missing));
|
||||
return -1;
|
||||
}
|
||||
int sector_size = parts.flash_size / parts.flash_nb_sectors;
|
||||
long ram_addr = parts.ram_base_addr + parts.ram_buffer_offset;
|
||||
boolean uuencode = parts.uuencode;
|
||||
byte data[];
|
||||
|
||||
//Sanity Checks
|
||||
if (ram_addr > parts.ram_base_addr + parts.ram_size) {
|
||||
listener.onError(context.getString(R.string.buffer_out_ram));
|
||||
return -2;
|
||||
}
|
||||
|
||||
long write_size = calcWriteSize(sector_size, parts.ram_buffer_size);
|
||||
if (write_size == 0) {
|
||||
listener.onError(context.getString(R.string.block_size_null));
|
||||
return -3;
|
||||
}
|
||||
|
||||
int ret = eraseFlash(parts);
|
||||
if (ret != 0) {
|
||||
listener.onError(context.getString(R.string.cant_erase_flash));
|
||||
return -4;
|
||||
}
|
||||
|
||||
|
||||
data = new byte[parts.flash_size];
|
||||
//copy file buffer to data array
|
||||
File file = new File(context.getApplicationInfo().dataDir + "/" + server.id + "/" + binary.path + binary.filename);
|
||||
int size = copy_buffer(file, data, parts.flash_size);
|
||||
if (size <= 0) {
|
||||
System.out.println("Size " + size);
|
||||
listener.onError(context.getString(R.string.copy_file));
|
||||
return -5;
|
||||
}
|
||||
|
||||
fill_data_with_0(data, size, parts.flash_size);
|
||||
int cksum = calc_binary_checksum(data);
|
||||
setChecksum(data, 28, cksum);
|
||||
if(!verifyChecksum(data,cksum)) {
|
||||
listener.onError(context.getString(R.string.checksum_failed));
|
||||
return -6;
|
||||
}
|
||||
long blocks = (size / write_size) + ((size % write_size == 0) ? 0 : 1);
|
||||
if (blocks * write_size > parts.flash_size) {
|
||||
listener.onError(context.getString(R.string.flash_outside_end));
|
||||
}
|
||||
|
||||
for(int i = 0; i < blocks; i++){
|
||||
int current_sector = (int)(i*write_size)/sector_size;
|
||||
int flash_addr = (int)(parts.flash_base_addr + (i*write_size));
|
||||
//prepare-for-write
|
||||
ret = send_cmd_sectors('P',current_sector,current_sector);
|
||||
if(ret != 0){
|
||||
listener.onError(context.getString(R.string.prepare_write_error)+" "+i);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = send_buf_to_ram(data,(int)(i*write_size),(int)ram_addr,(int)write_size, uuencode);
|
||||
if(ret != 0) {
|
||||
listener.onError(context.getString(R.string.write_ram_error)+" "+i);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//write-to-ram
|
||||
ret = send_cmd_address('C', flash_addr, (int)ram_addr, (int) write_size);
|
||||
if(ret != 0){
|
||||
listener.onError(context.getString(R.string.copy_to_flash_error)+" "+i);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int send_cmd_address(char c, int flash_addr, int ram_addr, int write_size) {
|
||||
int ret;
|
||||
int len;
|
||||
byte[] buf = new byte[3];
|
||||
String request = String.format(Locale.getDefault(),"%c %d %d %d\r\n",c,flash_addr,ram_addr,write_size);
|
||||
len = serialPort.write(request.getBytes());
|
||||
if(len != request.getBytes().length){
|
||||
System.out.println("cmd addr write");
|
||||
return -5;
|
||||
}
|
||||
len = read(buf,3,500);
|
||||
if (len <= 0) {
|
||||
System.out.println("cmd addr read");
|
||||
return -4;
|
||||
}
|
||||
ret = parseRetCode(buf);
|
||||