Skip to content

Commit

Permalink
Passing MTU to service (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
philips77 committed Oct 17, 2018
1 parent c9f6bfc commit 9c374b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dfu/src/main/java/no/nordicsemi/android/dfu/BaseDfuImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
protected DfuProgressInfo mProgressInfo;
protected int mImageSizeInBytes;
protected int mInitPacketSizeInBytes;
private int mCurrentMtu;

protected class BaseBluetoothGattCallback extends DfuGattCallback {
// The Implementation object is created depending on device services, so after the device is connected and services were scanned.
Expand Down Expand Up @@ -171,6 +172,10 @@ public void onMtuChanged(final BluetoothGatt gatt, final int mtu, final int stat
logi("MTU changed to: " + mtu);
} else {
logw("Changing MTU failed: " + status + " (mtu: " + mtu + ")");
if (status == 4 /* Invalid PDU */ && mCurrentMtu > 23 && mCurrentMtu - 3 > mBuffer.length) {
mBuffer = new byte[mCurrentMtu - 3]; // Maximum payload size is MTU - 3 bytes
logi("MTU restored to: " + mCurrentMtu);
}
}
mRequestCompleted = true;
notifyLock();
Expand Down Expand Up @@ -270,6 +275,7 @@ public boolean initialize(final Intent intent, final BluetoothGatt gatt, final i

final int currentPart = intent.getIntExtra(DfuBaseService.EXTRA_PART_CURRENT, 1);
int totalParts = intent.getIntExtra(DfuBaseService.EXTRA_PARTS_TOTAL, 1);
mCurrentMtu = intent.getIntExtra(DfuBaseService.EXTRA_CURRENT_MTU, 23);

// Sending App together with SD or BL is not supported. It must be spilt into two parts.
if (fileType > DfuBaseService.TYPE_APPLICATION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public abstract class DfuBaseService extends IntentService implements DfuProgres
* (even if it has been set to a higher value before).
*/
public static final String EXTRA_MTU = "no.nordicsemi.android.dfu.extra.EXTRA_MTU";
/**
* This extra value will be used when MTU request returned with an error. That means, that
* MTU has been requested before and may not be changed again. This value will be used instead.
*/
public static final String EXTRA_CURRENT_MTU = "no.nordicsemi.android.dfu.extra.EXTRA_CURRENT_MTU";
/**
* Set this flag to true to enable experimental buttonless feature in Secure DFU. When the
* experimental Buttonless DFU Service is found on a device, the service will use it to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class DfuServiceInitiator {
private int numberOfPackets = 12;

private int mtu = 517;
private int currentMtu = 23;

private Parcelable[] legacyDfuUuids;
private Parcelable[] secureDfuUuids;
Expand Down Expand Up @@ -256,6 +259,28 @@ public DfuServiceInitiator setMtu(final int mtu) {
return this;
}

/**
* Sets the current MTU value. This method should be used only if the device is already
* connected and MTU has been requested before DFU service is started.
* The SoftDevice allows to change MTU only once, while the following requests fail with
* Invalid PDU error. In case this error is received, the MTU will be set to the value
* specified using this method. There is no verification of this value. If it's set to
* too high value, some of the packets will not be sent and DFU will not succeed.
* <p>
* By default value 23 is used for compatibility reasons.
* <p>
* Higher MTU values were supported since SDK 15.0.
*
* @param mtu the MTU value received in
* {@link android.bluetooth.BluetoothGattCallback#onMtuChanged(BluetoothGatt, int, int)} or
* {@link android.bluetooth.BluetoothGattServerCallback#onMtuChanged(BluetoothDevice, int)}.
* @return the builder
*/
public DfuServiceInitiator setCurrentMtu(final int mtu) {
this.currentMtu = mtu;
return this;
}

/**
* Disables MTU request.
* @return the builder
Expand Down Expand Up @@ -623,6 +648,7 @@ public DfuServiceController start(@NonNull final Context context, @NonNull final
intent.putExtra(DfuBaseService.EXTRA_FORCE_DFU, forceDfu);
if (mtu > 0)
intent.putExtra(DfuBaseService.EXTRA_MTU, mtu);
intent.putExtra(DfuBaseService.EXTRA_CURRENT_MTU, currentMtu);
intent.putExtra(DfuBaseService.EXTRA_UNSAFE_EXPERIMENTAL_BUTTONLESS_DFU, enableUnsafeExperimentalButtonlessDfu);
if (packetReceiptNotificationsEnabled != null) {
intent.putExtra(DfuBaseService.EXTRA_PACKET_RECEIPT_NOTIFICATIONS_ENABLED, packetReceiptNotificationsEnabled);
Expand Down

0 comments on commit 9c374b9

Please sign in to comment.