Skip to content

Commit 3459223

Browse files
philips77bgiori
authored andcommitted
Migrate to BLE Library v2; New Sample App
All changes made in this commit are courtesy of @philips77 Major Additions/Changes ------------------------- * Migrates the old nordic Android-BLE-Library v1 to v2 and overhauls the McuManager BLE scheme implementation. * Add new (more functional) sample app * Update FirmwareUpgradeManager to be cleaner and more configurable. * Implement FileSystemManager * Add additional functionality to McuMgrTransport for connection observation and shutdown. Full list of commits from @philips77 squashed in merge: ------------------------------------------------------- * Android Studio 3.1.2 * Switching to external BLE Library v2 * Migrating to BLE Library v2 * Code cleanup, comments, warnings etc. * Fix Mcu Mgr Transport to match BLE lib changes * Refactoring in the sample app * Reverting access modifiers * Minor: code and comments cleanup * Increasing polling interval to 7 sec + minor changes * Improved packet length calculation * Refactoring, FirmwareUpgradeController added * MTU fixed again * Documentation added * Bug fix: looping with the same MTU fixed * Exceptions improved * BLE transport migrated to reflect BLE Lib changes, release method added to the mcu mgr transport interface. * Licenses added * Comments * Response field made public * Resource files removed from the library * Annotations added, MTU moved to McuManager * MTU moved to McuManager: Image and FirmwareUpgradeManager updated, erase() fixed * Annotations added, Bug fixed in McuMgrBleManager: calling onError if rc != 0, Refactoring * Warning hidden * FsManager added * Sample app rebuilt. FsManager not yet supported. * Tabs to Spaces * Line wrapping * Handling notification timeouts and disconnection * Support for handling Bluetooth disabled event * Bug fixed: cancelling upload * Annotations added * Adding State and Connection observers * Android Studio 3.1.3 * State observers removed * Basic and Advanced views on Image tab, Disabling controls when the device is busy, Reset moved to Image->Advanced tab * Keeping only English resources * Work indicator added * Tabs to spaces * continueUpload -> continueTransfer * UI for FsManager, other bug fixes * Moving common code to base class * Test file generation * Displaying graphical files as images * Destination added to Files Upload * New lines * Comments * Minor refactoring in EchoFragment * Hello! as initial text for Echo * Saving recent file names * Unused imports removed, warnings suppressed. * Bugs fixed: value validation added * Bug fixed: Recreating MainActivity fails as BluetoothDevice wasn't saved. * Image Manager: Erase state command added * McuMgr responses comments. * Initially, move the cursor to the end. * Adjusting to library changes * BLE Library imported from jcenter * Constraint-layout 1.1.2 * License changed to Apache 2.0 * Bug fixed: Ble Library must be available for the app. * Bug fixed * Bug fix: confirm slot 0 in test mode * BLE Library v2.0-alpha2 * BLE Library v2.0-alpha3 * License of the Sample app changed to Apache 2.0 * Zephyr icon fixed * Other device icons * Feature: option to filter device list by UUID and RSSI. * Info text updated * Bug fixed: formatting stats fixed * BLE Library version 2.0-alpha4 * Improved device filtering * Vector drawables set using scrCompat * Improved UI for smaller phones * BLE Library version 2.0-alpha5 * LogCat disabled * Bug fixed: Buttons get invalid state after screen rotation during upload * Bug fixed: Displaying long echos fixed * Bug fixed: Scanning progress bar fixed on Lollipop
1 parent e5031b5 commit 3459223

File tree

197 files changed

+10630
-4158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+10630
-4158
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ buildscript {
1212
jcenter()
1313
}
1414
dependencies {
15-
classpath 'com.android.tools.build:gradle:3.1.2'
15+
classpath 'com.android.tools.build:gradle:3.1.3'
1616
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
1717
}
1818
}

mcumgr-android-lib/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ android {
2828
}
2929

3030
dependencies {
31+
// Import the BLE Library
32+
api 'no.nordicsemi.android:ble:2.0-alpha5'
33+
3134
implementation fileTree(include: ['*.jar'], dir: 'libs')
3235

3336
// Test

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/McuManager.java

Lines changed: 145 additions & 94 deletions
Large diffs are not rendered by default.

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/McuMgrCallback.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77

88
package io.runtime.mcumgr;
99

10+
import android.support.annotation.NonNull;
11+
1012
import io.runtime.mcumgr.exception.McuMgrException;
1113
import io.runtime.mcumgr.response.McuMgrResponse;
1214

1315
/**
14-
* Callback for asynchronous Newt Manager commands.
16+
* Callback for asynchronous Mcu Manager commands.
1517
*/
1618
public interface McuMgrCallback<T extends McuMgrResponse> {
1719
/**
18-
* Newt Manager has received a response.
20+
* Mcu Manager has received a response.
1921
*
20-
* @param response the response
22+
* @param response the response.
2123
*/
22-
void onResponse(T response);
24+
void onResponse(@NonNull T response);
2325

2426
/**
25-
* Newt Manager has encountered a transport error while sending the command.
27+
* Mcu Manager has encountered a transport error while sending the command.
2628
*
27-
* @param error the error
29+
* @param error the error.
2830
*/
29-
void onError(McuMgrException error);
31+
void onError(@NonNull McuMgrException error);
3032
}

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/McuMgrErrorCode.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,53 @@
66

77
package io.runtime.mcumgr;
88

9+
import android.support.annotation.NonNull;
10+
911
/**
1012
* Almost all requests are responded to with an Mcu Manager return code in the response payload.
1113
* This value determines whether the request has been successful (rc = 0) or has failed (rc != 0).
12-
*
14+
* <p>
1315
* This return code is not supposed to be very descriptive and the actual error reason must be
1416
* determined based on the request and error code. Since McuManager errors are vague and often the
1517
* same error code could be caused by different reasons, the best way to debug errors here is
1618
* step through the handler on the device to determine the cause.
1719
*/
1820
public enum McuMgrErrorCode {
1921
/**
20-
* Success
22+
* Success.
2123
*/
2224
OK(0),
2325
/**
24-
* Unknown error
26+
* Unknown error.
2527
*/
2628
UNKNOWN(1),
2729
/**
28-
* The device has encountered an error due to running out of memory
30+
* The device has encountered an error due to running out of memory.
2931
*/
3032
NO_MEMORY(2),
3133
/**
32-
* The request header/payload is malformed or payload values are incorrect
34+
* The request header/payload is malformed or payload values are incorrect.
3335
*/
3436
IN_VALUE(3),
3537
/**
36-
* Timeout error
38+
* Timeout error.
3739
*/
3840
TIMEOUT(4),
3941
/**
4042
* No entry was found for the request. This commonly means that the command group has not been
41-
* enabled on the device, although the exact meaning
43+
* enabled on the device, although the exact meaning.
4244
*/
4345
NO_ENTRY(5),
4446
/**
45-
* The device is not currently in a state to handle the request
47+
* The device is not currently in a state to handle the request.
4648
*/
4749
BAD_STATE(6),
4850
/**
49-
* The response is too large
51+
* The response is too large.
5052
*/
5153
TOO_LARGE(7),
5254
/**
53-
* Command is not supported
55+
* Command is not supported.
5456
*/
5557
NOT_SUPPORTED(8),
5658
PER_USER(256);
@@ -67,9 +69,10 @@ public int value() {
6769

6870
@Override
6971
public String toString() {
70-
return "McuMgrError: " + super.toString() + "(" + mCode + ")";
72+
return super.toString() + " (" + mCode + ")";
7173
}
7274

75+
@NonNull
7376
public static McuMgrErrorCode valueOf(int error) {
7477
switch (error) {
7578
case 0:
@@ -93,7 +96,7 @@ public static McuMgrErrorCode valueOf(int error) {
9396
case 256:
9497
return PER_USER;
9598
default:
96-
return null;
99+
return UNKNOWN;
97100
}
98101
}
99102
}

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/McuMgrHeader.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package io.runtime.mcumgr;
99

10+
import android.support.annotation.NonNull;
11+
1012
import io.runtime.mcumgr.util.ByteUtil;
1113
import io.runtime.mcumgr.util.Endian;
1214

@@ -15,8 +17,8 @@
1517
* fields for optional values such as flags and sequence numbers. This class is used to parse
1618
* and build headers.
1719
*/
20+
@SuppressWarnings({"unused", "WeakerAccess"})
1821
public class McuMgrHeader {
19-
2022
public final static int HEADER_LENGTH = 8;
2123

2224
private int mOp;
@@ -87,31 +89,39 @@ public void setCommandId(int commandId) {
8789
this.mCommandId = commandId;
8890
}
8991

92+
@Override
93+
public String toString() {
94+
return "Header (Op: " + mOp + ", Flags: " + mFlags + ", Len: " + mLen + ", Group: " + mGroupId + ", Seq: " + mSequenceNum + ", Command: " + mCommandId + ")";
95+
}
96+
9097
public static McuMgrHeader fromBytes(byte[] header) {
9198
if (header == null || header.length != HEADER_LENGTH) {
9299
return null;
93100
}
94-
int op = ByteUtil.unsignedByteArrayToInt(header, 0, 1, Endian.BIG);
95-
int flags = ByteUtil.unsignedByteArrayToInt(header, 1, 1, Endian.BIG);
96-
int len = ByteUtil.unsignedByteArrayToInt(header, 2, 2, Endian.BIG);
97-
int groupId = ByteUtil.unsignedByteArrayToInt(header, 4, 2, Endian.BIG);
101+
int op = ByteUtil.unsignedByteArrayToInt(header, 0, 1, Endian.BIG);
102+
int flags = ByteUtil.unsignedByteArrayToInt(header, 1, 1, Endian.BIG);
103+
int len = ByteUtil.unsignedByteArrayToInt(header, 2, 2, Endian.BIG);
104+
int groupId = ByteUtil.unsignedByteArrayToInt(header, 4, 2, Endian.BIG);
98105
int sequenceNum = ByteUtil.unsignedByteArrayToInt(header, 6, 1, Endian.BIG);
99-
int commandId = ByteUtil.unsignedByteArrayToInt(header, 7, 1, Endian.BIG);
106+
int commandId = ByteUtil.unsignedByteArrayToInt(header, 7, 1, Endian.BIG);
100107
return new McuMgrHeader(op, flags, len, groupId, sequenceNum, commandId);
101108
}
102109

103110
/**
104111
* Builds a new manager header.
105112
*
106-
* @param op the operation for this packet: NMGR_WRITE, NMGR_WRITE_RSP, NMGR_READ, NMGR_READ_RSP
107-
* @param flags newt manager flags
108-
* @param len the length (this field is NOT required for all default newt manager commands)
113+
* @param op the operation for this packet: ({@link McuManager#OP_READ OP_READ},
114+
* {@link McuManager#OP_READ_RSP OP_READ_RSP}, {@link McuManager#OP_WRITE OP_WRITE},
115+
* {@link McuManager#OP_WRITE_RSP OP_WRITE_RSP}).
116+
* @param flags newt manager flags.
117+
* @param len the length (this field is NOT required for all default newt manager commands).
109118
* @param group the newt manager command group. Some groups such as GROUP_IMAGE must also
110-
* specify a subcommand ID.
111-
* @param sequence the newt manager sequence number
112-
* @param id the subcommand ID for certain groups
113-
* @return the built newt manager header
119+
* specify a sub-command ID.
120+
* @param sequence the newt manager sequence number.
121+
* @param id the sub-command ID for certain groups.
122+
* @return The built newt manager header.
114123
*/
124+
@NonNull
115125
public static byte[] build(int op, int flags, int len, int group, int sequence, int id) {
116126
return new byte[]{
117127
(byte) op,

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/McuMgrTransport.java

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package io.runtime.mcumgr;
99

10+
import android.support.annotation.NonNull;
11+
1012
import io.runtime.mcumgr.exception.McuMgrException;
1113
import io.runtime.mcumgr.response.McuMgrResponse;
1214

@@ -18,33 +20,72 @@
1820
*/
1921
public interface McuMgrTransport {
2022

23+
interface ConnectionObserver {
24+
/**
25+
* A method called when the connection to the device has been established.
26+
*/
27+
void onConnected();
28+
29+
/**
30+
* A method called when the connection to the device has been lost.
31+
*/
32+
void onDisconnected();
33+
}
34+
2135
/**
2236
* Gets the scheme for this transport (see {@link McuMgrScheme}).
23-
* @return the transport's scheme
37+
*
38+
* @return The transport's scheme.
2439
*/
40+
@NonNull
2541
McuMgrScheme getScheme();
2642

2743
/**
2844
* Send a synchronous Mcu Manager request. This method implementation should block until a
2945
* response has been received or a error has occurred.
30-
* @param payload the request packet data to send to the device
31-
* @param responseType the response type
32-
* @param <T> the response type
33-
* @return the response
46+
*
47+
* @param payload the request packet data to send to the device.
48+
* @param responseType the response type.
49+
* @param <T> the response type.
50+
* @return The response.
3451
* @throws McuMgrException thrown on error. Set the cause of the error if caused by a different
35-
* type of exception.
52+
* type of exception.
3653
*/
37-
<T extends McuMgrResponse> T send(byte[] payload, Class<T> responseType) throws McuMgrException;
54+
@NonNull
55+
<T extends McuMgrResponse> T send(@NonNull byte[] payload, @NonNull Class<T> responseType)
56+
throws McuMgrException;
3857

3958
/**
4059
* Send an asynchronous Mcu Manager request. This method should not be blocked. When the
4160
* response has been received or an error occurs, the appropriate method of the callback should
4261
* be called.
43-
* @param payload the request packet data to send to the device
44-
* @param responseType the response type
45-
* @param callback the callback to call on response or error
46-
* @param <T> the response type
62+
*
63+
* @param payload the request packet data to send to the device.
64+
* @param responseType the response type.
65+
* @param callback the callback to call on response or error.
66+
* @param <T> the response type.
67+
*/
68+
<T extends McuMgrResponse> void send(@NonNull byte[] payload, @NonNull Class<T> responseType,
69+
@NonNull McuMgrCallback<T> callback);
70+
71+
/**
72+
* Releases the transport connection. When the connection is already closed this method does
73+
* nothing.
74+
*/
75+
void release();
76+
77+
/**
78+
* Adds the connection observer. An observer will be notified whenever the connected device
79+
* gets connected or disconnected.
80+
*
81+
* @param observer the observer.
82+
*/
83+
void addObserver(@NonNull ConnectionObserver observer);
84+
85+
/**
86+
* Removes previously registered observer.
87+
*
88+
* @param observer the observer.
4789
*/
48-
<T extends McuMgrResponse> void send(byte[] payload, Class<T> responseType,
49-
McuMgrCallback<T> callback);
90+
void removeObserver(@NonNull ConnectionObserver observer);
5091
}

mcumgr-android-lib/src/main/java/io/runtime/mcumgr/ble/McuMgrBleCallbacks.java renamed to mcumgr-android-lib/src/main/java/io/runtime/mcumgr/ble/McuMgrBleCallbacksStub.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/*
2-
* Copyright (c) 2017-2018 Runtime Inc.
2+
* Copyright (c) 2018, Nordic Semiconductor
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
package io.runtime.mcumgr.ble;
88

99
import android.bluetooth.BluetoothDevice;
10-
import android.bluetooth.BluetoothGattCharacteristic;
11-
import android.bluetooth.BluetoothGattDescriptor;
1210

13-
import io.runtime.mcumgr.ble.manager.BleManagerCallbacks;
11+
import no.nordicsemi.android.ble.BleManagerCallbacks;
1412

1513
/**
1614
* <b>DO NOT PERFORM ANY BLOCKING OPERATIONS INSIDE THESE CALLBACKS!</b>
@@ -19,27 +17,7 @@
1917
* <p>
2018
* Callbacks from {@link McuMgrBleTransport}.
2119
*/
22-
public class McuMgrBleCallbacks implements BleManagerCallbacks {
23-
public void onCharacteristicRead(BluetoothDevice device, BluetoothGattCharacteristic characteristic) {
24-
}
25-
26-
public void onCharacteristicWrite(BluetoothDevice device, BluetoothGattCharacteristic characteristic) {
27-
}
28-
29-
public void onDescriptorRead(BluetoothDevice device, BluetoothGattDescriptor descriptor) {
30-
}
31-
32-
public void onDescriptorWrite(BluetoothDevice device, BluetoothGattDescriptor descriptor) {
33-
}
34-
35-
public void onCharacteristicNotified(BluetoothDevice device, BluetoothGattCharacteristic characteristic) {
36-
}
37-
38-
public void onCharacteristicIndicated(BluetoothDevice device, BluetoothGattCharacteristic characteristic) {
39-
}
40-
41-
public void onMtuChanged(int mtu) {
42-
}
20+
public class McuMgrBleCallbacksStub implements BleManagerCallbacks {
4321

4422
@Override
4523
public void onDeviceConnecting(BluetoothDevice device) {
@@ -58,7 +36,7 @@ public void onDeviceDisconnected(BluetoothDevice device) {
5836
}
5937

6038
@Override
61-
public void onLinklossOccur(BluetoothDevice device) {
39+
public void onLinkLossOccurred(BluetoothDevice device) {
6240
}
6341

6442
@Override
@@ -82,6 +60,10 @@ public void onBatteryValueReceived(BluetoothDevice device, int value) {
8260
public void onBondingRequired(BluetoothDevice device) {
8361
}
8462

63+
@Override
64+
public void onBondingFailed(BluetoothDevice device) {
65+
}
66+
8567
@Override
8668
public void onBonded(BluetoothDevice device) {
8769
}

0 commit comments

Comments
 (0)