Commit f9f50262 authored by Schoumi's avatar Schoumi

Flash LPC management

parent 8e30b52b
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();
}
}
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);
}
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;
}
}
......@@ -7,7 +7,6 @@ import android.net.NetworkInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -15,11 +14,11 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.listener.DownloadListener;
import fr.mobdev.lpcprog.listener.NetworkListener;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
......@@ -29,14 +28,14 @@ public class NetworkManager {
private NetworkListener listener;
private static NetworkManager instance;
private Context context;
private boolean isCanceled;
private boolean downloadInProgress;
private List<Binary> cancelList;
private NetworkManager(NetworkListener listener, Context context)
{
this.listener = listener;
this.context = context;
this.isCanceled = false;
cancelList = new ArrayList<>();
}
public static NetworkManager getInstance(NetworkListener listener, Context context){
......@@ -70,17 +69,17 @@ public class NetworkManager {
listener.onError(context.getString(R.string.network_error));
}
public void retrieveBinary(final Server server, final Binary binary){
public void retrieveBinary(final Server server, final Binary binary, final DownloadListener downloadListener){
if(isConnectedToInternet(context)){
new Thread(new Runnable() {
@Override
public void run() {
getBinary(server, binary);
getBinary(server, binary,downloadListener);
}
}).start();
}
else
listener.onError(context.getString(R.string.network_error));
this.listener.onError(context.getString(R.string.network_error));
}
private void getBinariesList(){
......@@ -94,7 +93,7 @@ public class NetworkManager {
}
listener.startBinaries();
List<Server> serverInError = new ArrayList<>();
for(Server server : serversAvailable) {
listener.startServer(server);
try {
......@@ -106,20 +105,27 @@ public class NetworkManager {
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
System.out.println("Browse binaries");
while(line != null){
Binary b = parseLine(line);
if(b != null)
System.out.println("Found a binary "+b.name);
if(b != null && !server.contains(b)) {
server.addBinary(b);
DatabaseManager.getInstance(context).addBinary(server,b);
}
line = reader.readLine();
}
}
} catch (IOException e) {
e.printStackTrace();
DatabaseManager.getInstance(context).serverFail(server);
serverInError.add(server);
} finally {
listener.endServer(server);
}
}
if(serverInError.containsAll(servers))
listener.onError(context.getString(R.string.all_server_error));
listener.endBinaries();
}
......@@ -138,10 +144,13 @@ public class NetworkManager {
b.path = line.substring(0,line.indexOf(";"));
line = line.substring(line.indexOf(";")+1);
b.sha1 = line.substring(0,line.indexOf(";"));
b.isDownloaded = false;
b.isDownloading = false;
b.progress = 0;
return b;
}
private void getBinary(Server server, Binary binary){
private void getBinary(Server server, Binary binary, DownloadListener downloadListener){
try {
URL url = new URL(server.url.toString() + binary.path + binary.filename);
downloadInProgress = true;
......@@ -149,9 +158,8 @@ public class NetworkManager {
connection.setDoInput(true);
InputStream stream = connection.getInputStream();
File file = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename);
file.getParentFile().mkdirs();
if (!file.createNewFile() && !file.canWrite()){
listener.downloadError(server,binary,context.getString(R.string.cant_create_file));
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || !file.createNewFile() && !file.canWrite()){
downloadListener.downloadError(server,binary,context.getString(R.string.cant_create_file));
return;
}
FileOutputStream output = new FileOutputStream(file);
......@@ -161,34 +169,35 @@ public class NetworkManager {
long totalSizeReaded = 0;
int sizeReaded ;
while((sizeReaded = stream.read(data)) != -1){
if(isCanceled) {
isCanceled = false;
if(cancelList.contains(binary)) {
downloadInProgress = false;
listener.downloadCanceled(server,binary);
downloadListener.downloadCanceled(server,binary);
stream.close();
output.close();
cleanupFile(server,binary);
cancelList.remove(binary);
return;
}
totalSizeReaded += sizeReaded;
if(fileLength > 0){
listener.progress(server,binary, (int) totalSizeReaded * 100 / fileLength);
downloadListener.progress(server,binary, (int) totalSizeReaded * 100 / fileLength);
}
output.write(data,0,sizeReaded);
}
output.close();
if(checkFileSha1(server,binary))
listener.downloadSuccessful(server,binary);
downloadListener.downloadSuccessful(server,binary);
else
listener.downloadError(server,binary,context.getString(R.string.sha1_error));
downloadListener.downloadError(server,binary,context.getString(R.string.sha1_error));
}
}catch (IOException e) {
e.printStackTrace();
cleanupFile(server,binary);
listener.downloadFailed(server,binary);
DatabaseManager.getInstance(context).serverFail(server);
downloadListener.downloadError(server,binary, context.getString(R.string.unknown_error));
}
downloadInProgress = false;
isCanceled = false;
cancelList.remove(binary);
}
public boolean checkFileSha1(Server server, Binary binary){
......@@ -218,9 +227,9 @@ public class NetworkManager {
return f.delete();
}
public void cancelDownload(){
public void cancelDownload(Binary binary){
if(downloadInProgress)
isCanceled = true;
cancelList.add(binary);
}
private boolean isConnectedToInternet(Context context)
......
......@@ -7,4 +7,35 @@ public class Binary {
public int version;
public String path;
public String sha1;
public boolean isDownloaded;
public boolean isDownloading;
public int progress = 0;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Binary binary = (Binary) o;
if (version != binary.version) {
System.out.println("version");
return false;
}
if (!name.equals(binary.name)){
System.out.println("name");
return false;
}
if (!filename.equals(binary.filename)) {
System.out.println("filename");
return false;
}
if (!path.equals(binary.path)) {
System.out.println("path");
return false;
}
System.out.println("sha1 "+sha1.equals(binary.sha1));
return sha1.equals(binary.sha1);
}
}
package fr.mobdev.lpcprog.objects;
public class Parts {
public long id;
public long part_id;
public String part_name;
public int flash_base_addr;
public int flash_size;
public int flash_nb_sectors;
public int reset_vector_offset;
public int ram_base_addr;
public int ram_size;
public int ram_buffer_offset;
public int ram_buffer_size;
public boolean uuencode;
}
......@@ -9,9 +9,16 @@ public class Server {
public URL url;
public boolean isActive;
public int attempted;
public List<Binary> binaries = new ArrayList<>();
public List<Binary> binaries;
public void addBinary(Binary b) {
binaries.add(b);
}
public boolean contains(Binary b) {
for(Binary binary : binaries){
System.out.println("Binary equals?"+b.name+" "+b.equals(binary));
}
return binaries.contains(b);
}
}
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#33FF33"
android:endColor="#008000"
android:angle="270"
/>
</shape>
</clip>
</item>