Skip to content

Commit

Permalink
Merge pull request #54 from NordicSemiconductor/feature/displaying-mc…
Browse files Browse the repository at this point in the history
…umgr-params

Displaying mcumgr params
  • Loading branch information
philips77 authored May 13, 2022
2 parents 0443f54 + d0f6e0b commit 467547e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.runtime.mcumgr.exception.McuMgrErrorException;
import io.runtime.mcumgr.exception.McuMgrException;
import io.runtime.mcumgr.exception.McuMgrTimeoutException;
import io.runtime.mcumgr.managers.DefaultManager;
import io.runtime.mcumgr.response.McuMgrResponse;
import io.runtime.mcumgr.response.dflt.McuMgrParamsResponse;
import io.runtime.mcumgr.util.CBOR;
Expand Down Expand Up @@ -211,30 +212,56 @@ protected BleManagerGattCallback getGattCallback() {
return new McuMgrGattCallback();
}

//*******************************************************************************************
// Maximum SMP packet length.
//*******************************************************************************************

/**
* In order to send packets longer than MTU size, this library supports automatic splitting
* In order to send packets longer than MTU size, this library supports automatic segmentation
* of packets into at-most-MTU size chunks. This feature must be also supported by the target
* device, as it must merge received chunks into a single packet, based on the length field
* from the {@link io.runtime.mcumgr.McuMgrHeader}, included in the first chunk.
* <p>
* {@link io.runtime.mcumgr.managers.ImageManager} and
* {@link io.runtime.mcumgr.managers.FsManager} will automatically split the file into multiple
* SMP packets. This feature is about splitting SMP packet into chunks, not splitting data into
* SMP packets.
* device, as it must reassembly received chunks into full SMP packet, based on the length field
* from the {@link io.runtime.mcumgr.McuMgrHeader}, included in the first segment.
* <p>
* This method sets the maximum packet length supported by the target device.
* By default, this is be set to MTU - 3, which means that no splitting will be done.
* By default, this is be set to MTU - 3, which means that each BLE packet will contain the full
* SMP packet (header + CBOR-encoded data). For devices supporting reading McuMgr parameters
* (nRF Connect SDK 2.0+) this value is automatically obtained after connection using
* {@link DefaultManager#params()}.
* <p>
* Keep in mind, that before Android 5 requesting higher MTU was not supported. Setting the
* maximum length to a greater value is required on those devices in order to upgrade
* the firmware, send file or send any other SMP packet that is longer than 20 bytes.
*
* @since 1.3
* @param maxLength the maximum packet length.
*/
public void setDeviceSidePacketMergingSupported(int maxLength) {
public final void setMaxPacketLength(final int maxLength) {
mMaxPacketLength = maxLength;
}

/**
* Returns the maximum length of a SMP packet that can be transmitted over by the transport.
* @return the maximum
*/
public final int getMaxPacketLength() {
return mMaxPacketLength;
}

/**
* Sets the maximum packet length for the transport.
*
* Starting from version 1.3 the library can automatically obtain the value from a device build
* on nRF Connect SDK 2.0+ using the new {@link DefaultManager#params()} command. If the feature
* is not supported on the device the maximum length defaults to MTU-3 bytes.
*
* @param maxLength the maximum packet length.
* @deprecated Use {@link #setMaxPacketLength(int)} instead.
*/
@Deprecated
public void setDeviceSidePacketMergingSupported(int maxLength) {
setMaxPacketLength(maxLength);
}

//*******************************************************************************************
// Logging
//*******************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
break;
}
});
viewModel.getBufferParams().observe(getViewLifecycleOwner(), params -> {
if (params != null) {
final String text = getString(R.string.status_mcumgr_buffer_size, params.count, params.size);
binding.mcumgrBufferSize.setText(text);
} else {
binding.mcumgrBufferSize.setText(R.string.status_unknown);
}
});
viewModel.getBusyState().observe(getViewLifecycleOwner(), busy ->
binding.workIndicator.setVisibility(busy ? View.VISIBLE : View.GONE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public void onBondingFailed(@NonNull final BluetoothDevice device) {
setLoggingEnabled(true);
}

@Nullable
@NonNull
public LiveData<ConnectionState> getState() {
return connectionState;
}

@Nullable
@NonNull
public LiveData<BondingState> getBondingState() {
return bondingState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@

package io.runtime.mcumgr.sample.viewmodel.mcumgr;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import javax.inject.Inject;
import javax.inject.Named;

import androidx.lifecycle.Observer;
import io.runtime.mcumgr.McuMgrCallback;
import io.runtime.mcumgr.McuMgrTransport;
import io.runtime.mcumgr.ble.McuMgrBleTransport;
import io.runtime.mcumgr.exception.McuMgrException;
import io.runtime.mcumgr.managers.DefaultManager;
import io.runtime.mcumgr.response.dflt.McuMgrParamsResponse;
import io.runtime.mcumgr.sample.observable.BondingState;
import io.runtime.mcumgr.sample.observable.ConnectionState;
import io.runtime.mcumgr.sample.observable.ObservableMcuMgrBleTransport;
Expand All @@ -21,11 +30,45 @@ public class DeviceStatusViewModel extends McuMgrViewModel {
private final LiveData<ConnectionState> connectionStateLiveData;
private final LiveData<BondingState> bondStateLiveData;

private final MutableLiveData<McuMgrBufferParams> bufferLiveData = new MutableLiveData<>();
private final Observer<ConnectionState> connectionStateObserver = new Observer<ConnectionState>() {
@Override
public void onChanged(final ConnectionState connectionState) {
if (connectionState == ConnectionState.READY) {
defaultManager.params(new McuMgrCallback<McuMgrParamsResponse>() {
@Override
public void onResponse(@NotNull final McuMgrParamsResponse response) {
bufferLiveData.postValue(new McuMgrBufferParams(response));
}

@Override
public void onError(@NotNull final McuMgrException error) {
final McuMgrTransport transport = defaultManager.getTransporter();
if (transport instanceof McuMgrBleTransport) {
final McuMgrBleTransport bleTransport = (McuMgrBleTransport) transport;
final int maxPacketLength = bleTransport.getMaxPacketLength();
final McuMgrBufferParams mcuParams = new McuMgrBufferParams(maxPacketLength);
bufferLiveData.postValue(mcuParams);
} else {
bufferLiveData.postValue(null);
}
}
});
} else {
bufferLiveData.postValue(null);
}
}
};

private final DefaultManager defaultManager;

@Inject
DeviceStatusViewModel(final McuMgrTransport transport,
DeviceStatusViewModel(final DefaultManager manager,
@Named("busy") final MutableLiveData<Boolean> state) {
super(state);
defaultManager = manager;

final McuMgrTransport transport = manager.getTransporter();
if (transport instanceof ObservableMcuMgrBleTransport) {
connectionStateLiveData = ((ObservableMcuMgrBleTransport) transport).getState();
bondStateLiveData = ((ObservableMcuMgrBleTransport) transport).getBondingState();
Expand All @@ -45,6 +88,13 @@ public void onDisconnected() {
connectionStateLiveData = liveData;
bondStateLiveData = new MutableLiveData<>(BondingState.NOT_BONDED);
}
connectionStateLiveData.observeForever(connectionStateObserver);
}

@Override
protected void onCleared() {
connectionStateLiveData.removeObserver(connectionStateObserver);
super.onCleared();
}

public LiveData<ConnectionState> getConnectionState() {
Expand All @@ -55,4 +105,20 @@ public LiveData<BondingState> getBondState() {
return bondStateLiveData;
}

public LiveData<McuMgrBufferParams> getBufferParams() { return bufferLiveData; }

public static class McuMgrBufferParams {
public final int size;
public final int count;

private McuMgrBufferParams(@NonNull final McuMgrParamsResponse response) {
size = response.bufSize;
count = response.bufCount;
}

private McuMgrBufferParams(final int maxPacketLength) {
size = maxPacketLength;
count = 1;
}
}
}
38 changes: 29 additions & 9 deletions sample/src/main/res/layout/fragment_card_device_status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,58 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/connection_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/status_not_connected"
android:textAllCaps="true"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/connection_status_label"
app:layout_constraintTop_toTopOf="@+id/connection_status_label"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/bonding_status_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:text="@string/status_bond_label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/connection_status_label"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/connection_status"
android:id="@+id/bonding_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:text="@string/status_not_connected"
android:text="@string/status_not_bonded"
android:textAllCaps="true"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/connection_status_label"
app:layout_constraintTop_toTopOf="@+id/connection_status_label"/>
app:layout_constraintTop_toBottomOf="@+id/connection_status_label"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/bonding_status"
android:id="@+id/mcumgr_buffer_size_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:text="@string/status_mcumgr_buffer_size_label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bonding_status_label"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/mcumgr_buffer_size"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:text="@string/status_not_bonded"
android:textAllCaps="true"
android:text="@string/status_unknown"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/connection_status_label"
app:layout_constraintTop_toBottomOf="@+id/connection_status_label"/>
app:layout_constraintTop_toBottomOf="@+id/bonding_status_label"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
4 changes: 4 additions & 0 deletions sample/src/main/res/values/strings_device_status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="status_title">Device Status</string>
<string name="status_status_label">Connection status:</string>
<string name="status_bond_label">Bonding status:</string>
<string name="status_mcumgr_buffer_size_label">Buffer details:</string>

<string name="status_not_connected">Not connected</string>
<string name="status_connecting">Connecting…</string>
Expand All @@ -23,4 +24,7 @@
<string name="status_not_bonded">Not bonded</string>
<string name="status_bonding">Bonding…</string>
<string name="status_bonded">Bonded</string>

<string name="status_unknown">UNKNOWN</string>
<string name="status_mcumgr_buffer_size">%d x %d bytes</string>
</resources>

0 comments on commit 467547e

Please sign in to comment.