1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-07 05:38:13 +02:00

get free slot always before actually installing a pbw. fixes #17 and #18

This commit is contained in:
Andreas Shimokawa 2015-04-07 23:57:12 +02:00
parent ebdf514c0e
commit 90fe75e044
4 changed files with 44 additions and 28 deletions

View File

@ -166,7 +166,10 @@ public class BluetoothCommunicationService extends Service {
case APP_INFO: case APP_INFO:
Log.i(TAG, "Got command for APP_INFO"); Log.i(TAG, "Got command for APP_INFO");
GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd; GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd;
mGBDevice.setFreeAppSlot(appInfoCmd.freeSlot); if (mGBDevice.getType() == GBDevice.Type.PEBBLE) {
((PebbleIoThread) mGBDeviceIoThread).setInstallSlot(appInfoCmd.freeSlot);
}
Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST);
int appCount = appInfoCmd.apps.length; int appCount = appInfoCmd.apps.length;
appInfoIntent.putExtra("app_count", appCount); appInfoIntent.putExtra("app_count", appCount);
@ -351,10 +354,9 @@ public class BluetoothCommunicationService extends Service {
mGBDeviceIoThread.write(mGBDeviceProtocol.encodeAppDelete(id, index)); mGBDeviceIoThread.write(mGBDeviceProtocol.encodeAppDelete(id, index));
} else if (action.equals(ACTION_INSTALL_PEBBLEAPP)) { } else if (action.equals(ACTION_INSTALL_PEBBLEAPP)) {
String uriString = intent.getStringExtra("app_uri"); String uriString = intent.getStringExtra("app_uri");
int slot = mGBDevice.getFreeAppSlot(); if (uriString != null) {
if (uriString != null && slot != -1) { Log.i(TAG, "will try to install app");
Log.i(TAG, "will try to install app in slot " + slot); ((PebbleIoThread) mGBDeviceIoThread).installApp(Uri.parse(uriString));
((PebbleIoThread) mGBDeviceIoThread).installApp(Uri.parse(uriString), slot);
} }
} else if (action.equals(ACTION_START)) { } else if (action.equals(ACTION_START)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running")); startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
@ -444,6 +446,7 @@ public class BluetoothCommunicationService extends Service {
private enum PebbleAppInstallState { private enum PebbleAppInstallState {
UNKNOWN, UNKNOWN,
APP_WAIT_SLOT,
APP_START_INSTALL, APP_START_INSTALL,
APP_WAIT_TOKEN, APP_WAIT_TOKEN,
APP_UPLOAD_CHUNK, APP_UPLOAD_CHUNK,
@ -512,6 +515,12 @@ public class BluetoothCommunicationService extends Service {
try { try {
if (mmIsInstalling) { if (mmIsInstalling) {
switch (mmInstallState) { switch (mmInstallState) {
case APP_WAIT_SLOT:
if (mmInstallSlot != -1) {
mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
continue;
}
break;
case APP_START_INSTALL: case APP_START_INSTALL:
Log.i(TAG, "start installing app binary"); Log.i(TAG, "start installing app binary");
mmSTM32CRC.reset(); mmSTM32CRC.reset();
@ -541,7 +550,7 @@ public class BluetoothCommunicationService extends Service {
mmInstallSlot = -1; mmInstallSlot = -1;
} }
writeInstallApp(mmPebbleProtocol.encodeUploadStart(type, (byte)mmInstallSlot, binarySize), -1); writeInstallApp(mmPebbleProtocol.encodeUploadStart(type, (byte) mmInstallSlot, binarySize));
mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN; mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN;
break; break;
case APP_WAIT_TOKEN: case APP_WAIT_TOKEN:
@ -556,7 +565,7 @@ public class BluetoothCommunicationService extends Service {
if (bytes != -1) { if (bytes != -1) {
mmSTM32CRC.addData(buffer, bytes); mmSTM32CRC.addData(buffer, bytes);
writeInstallApp(mmPebbleProtocol.encodeUploadChunk(mmAppInstallToken, buffer, bytes), -1); writeInstallApp(mmPebbleProtocol.encodeUploadChunk(mmAppInstallToken, buffer, bytes));
mmAppInstallToken = -1; mmAppInstallToken = -1;
mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN; mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN;
} else { } else {
@ -565,7 +574,7 @@ public class BluetoothCommunicationService extends Service {
} }
break; break;
case APP_UPLOAD_COMMIT: case APP_UPLOAD_COMMIT:
writeInstallApp(mmPebbleProtocol.encodeUploadCommit(mmAppInstallToken, mmSTM32CRC.getResult()), -1); writeInstallApp(mmPebbleProtocol.encodeUploadCommit(mmAppInstallToken, mmSTM32CRC.getResult()));
mmAppInstallToken = -1; mmAppInstallToken = -1;
mmInstallState = PebbleAppInstallState.APP_WAIT_COMMMIT; mmInstallState = PebbleAppInstallState.APP_WAIT_COMMMIT;
break; break;
@ -577,7 +586,7 @@ public class BluetoothCommunicationService extends Service {
} }
break; break;
case APP_UPLOAD_COMPLETE: case APP_UPLOAD_COMPLETE:
writeInstallApp(mmPebbleProtocol.encodeUploadComplete(mmAppInstallToken), -1); writeInstallApp(mmPebbleProtocol.encodeUploadComplete(mmAppInstallToken));
if (++mmCurrentFileIndex < mmFilesToInstall.length) { if (++mmCurrentFileIndex < mmFilesToInstall.length) {
mmInstallState = PebbleAppInstallState.APP_START_INSTALL; mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
} else { } else {
@ -585,7 +594,7 @@ public class BluetoothCommunicationService extends Service {
} }
break; break;
case APP_REFRESH: case APP_REFRESH:
writeInstallApp(mmPebbleProtocol.encodeAppRefresh(mmInstallSlot), -1); writeInstallApp(mmPebbleProtocol.encodeAppRefresh(mmInstallSlot));
mmPBWReader = null; mmPBWReader = null;
mmIsInstalling = false; mmIsInstalling = false;
mmZis = null; mmZis = null;
@ -700,14 +709,19 @@ public class BluetoothCommunicationService extends Service {
mmAppInstallToken = token; mmAppInstallToken = token;
} }
private void writeInstallApp(byte[] bytes, int length) { public void setInstallSlot(int slot) {
if (mmIsInstalling) {
mmInstallSlot = slot;
}
}
private void writeInstallApp(byte[] bytes) {
if (!mmIsInstalling) { if (!mmIsInstalling) {
return; return;
} }
if (length == -1) { int length = bytes.length;
length = bytes.length;
}
Log.i(TAG, "got bytes for writeInstallApp()" + length); Log.i(TAG, "got bytes for writeInstallApp()" + length);
/*
final char[] hexArray = "0123456789ABCDEF".toCharArray(); final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[length * 2]; char[] hexChars = new char[length * 2];
for (int j = 0; j < length; j++) { for (int j = 0; j < length; j++) {
@ -716,6 +730,7 @@ public class BluetoothCommunicationService extends Service {
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; hexChars[j * 2 + 1] = hexArray[v & 0x0F];
} }
Log.i(TAG, new String(hexChars)); Log.i(TAG, new String(hexChars));
*/
try { try {
mmOutStream.write(bytes); mmOutStream.write(bytes);
mmOutStream.flush(); mmOutStream.flush();
@ -723,13 +738,13 @@ public class BluetoothCommunicationService extends Service {
} }
} }
public void installApp(Uri uri, int slot) { public void installApp(Uri uri) {
if (mmIsInstalling) { if (mmIsInstalling) {
return; return;
} }
mmInstallState = PebbleAppInstallState.APP_START_INSTALL; write(mmPebbleProtocol.encodeAppInfoReq()); // do this here to get run() out of its blocking read
mmInstallState = PebbleAppInstallState.APP_WAIT_SLOT;
mmInstallURI = uri; mmInstallURI = uri;
mmInstallSlot = slot;
mmIsInstalling = true; mmIsInstalling = true;
} }

View File

@ -6,7 +6,6 @@ public class GBDevice {
private final Type type; private final Type type;
private String firmwareVersion = null; private String firmwareVersion = null;
private State state = State.NOT_CONNECTED; private State state = State.NOT_CONNECTED;
private byte freeAppSlot = -1;
public GBDevice(String address, String name, Type type) { public GBDevice(String address, String name, Type type) {
this.address = address; this.address = address;
@ -62,14 +61,6 @@ public class GBDevice {
return type; return type;
} }
public void setFreeAppSlot(byte freeAppSlot) {
this.freeAppSlot = freeAppSlot;
}
public byte getFreeAppSlot() {
return freeAppSlot;
}
public enum State { public enum State {
NOT_CONNECTED, NOT_CONNECTED,
CONNECTING, CONNECTING,

View File

@ -31,13 +31,12 @@ public class PebbleAppInstallerActivity extends Activity {
debugTextView = (TextView) findViewById(R.id.debugTextView); debugTextView = (TextView) findViewById(R.id.debugTextView);
installButton = (Button) findViewById(R.id.installButton); installButton = (Button) findViewById(R.id.installButton);
debugTextView.setText("contents:\n");
final Uri uri = getIntent().getData(); final Uri uri = getIntent().getData();
PBWReader pbwReader = new PBWReader(uri, getApplicationContext()); PBWReader pbwReader = new PBWReader(uri, getApplicationContext());
GBDeviceApp app = pbwReader.getGBDeviceApp(); GBDeviceApp app = pbwReader.getGBDeviceApp();
if (pbwReader != null && app != null) { if (pbwReader != null && app != null) {
debugTextView.setText("This is just a test, you cant install anything yet \n\n" + app.getName() + " Version " + app.getVersion() + " by " + app.getCreator() + "\n"); debugTextView.setText("THIS IS HIGHLY EXPERIMENTAL PROCEED AT YOUR OWN RISK\n\n\n" + app.getName() + " Version " + app.getVersion() + " by " + app.getCreator() + "\n");
installButton.setEnabled(true); installButton.setEnabled(true);
installButton.setOnClickListener(new View.OnClickListener() { installButton.setOnClickListener(new View.OnClickListener() {
@Override @Override

View File

@ -116,6 +116,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
static final short LENGTH_PREFIX = 4; static final short LENGTH_PREFIX = 4;
static final short LENGTH_SETTIME = 5; static final short LENGTH_SETTIME = 5;
static final short LENGTH_GETTIME = 1;
static final short LENGTH_REMOVEAPP = 9; static final short LENGTH_REMOVEAPP = 9;
static final short LENGTH_REFRESHAPP = 5; static final short LENGTH_REFRESHAPP = 5;
static final short LENGTH_PHONEVERSION = 17; static final short LENGTH_PHONEVERSION = 17;
@ -204,6 +205,16 @@ public class PebbleProtocol extends GBDeviceProtocol {
return buf.array(); return buf.array();
} }
public byte[] encodeGetTime() {
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_GETTIME);
buf.order(ByteOrder.BIG_ENDIAN);
buf.putShort(LENGTH_GETTIME);
buf.putShort(ENDPOINT_TIME);
buf.put(TIME_GETTIME);
return buf.array();
}
public byte[] encodeSetCallState(String number, String name, GBCommand command) { public byte[] encodeSetCallState(String number, String name, GBCommand command) {
String[] parts = {number, name}; String[] parts = {number, name};
byte pebbleCmd; byte pebbleCmd;