Skip to content

Commit d6fb2a4

Browse files
authored
Add connect to McuMgrTransport for use in FirmwareUpgradeManager. (#37)
1 parent e463627 commit d6fb2a4

File tree

4 files changed

+325
-197
lines changed

4 files changed

+325
-197
lines changed

mcumgr-ble/src/main/java/io/runtime/mcumgr/ble/McuMgrBleTransport.java

+52
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.content.Context;
1414
import android.os.Build;
1515
import android.support.annotation.NonNull;
16+
import android.support.annotation.Nullable;
1617
import android.util.Log;
1718

1819
import org.slf4j.Logger;
@@ -382,6 +383,57 @@ public void onRequestFailed(@NonNull final BluetoothDevice device, final int sta
382383
.enqueue();
383384
}
384385

386+
@Override
387+
public void connect(@Nullable final ConnectionCallback callback) {
388+
if (isConnected()) {
389+
if (callback != null) {
390+
callback.onConnected();
391+
}
392+
return;
393+
}
394+
connect(mDevice)
395+
.retry(3, 100)
396+
.done(new SuccessCallback() {
397+
@Override
398+
public void onRequestCompleted(@NonNull BluetoothDevice device) {
399+
notifyConnected();
400+
if (callback == null) {
401+
return;
402+
}
403+
callback.onConnected();
404+
}
405+
})
406+
.fail(new FailCallback() {
407+
@Override
408+
public void onRequestFailed(@NonNull BluetoothDevice device, int status) {
409+
if (callback == null) {
410+
return;
411+
}
412+
switch (status) {
413+
case REASON_DEVICE_DISCONNECTED:
414+
callback.onError(new McuMgrException("Device has disconnected"));
415+
break;
416+
case REASON_DEVICE_NOT_SUPPORTED:
417+
callback.onError(new McuMgrException("Device does not support SMP Service"));
418+
break;
419+
case REASON_REQUEST_FAILED:
420+
// This could be thrown only if the manager was requested to connect for
421+
// a second time and to a different device than the one that's already
422+
// connected. This may not happen here.
423+
callback.onError(new McuMgrException("Other device already connected"));
424+
break;
425+
case REASON_BLUETOOTH_DISABLED:
426+
callback.onError(new McuMgrException("Bluetooth adapter disabled"));
427+
break;
428+
default:
429+
callback.onError(new McuMgrException(GattError.parseConnectionError(status)));
430+
break;
431+
}
432+
}
433+
})
434+
.enqueue();
435+
}
436+
385437
@Override
386438
public void release() {
387439
disconnect().enqueue();

mcumgr-core/src/main/java/io/runtime/mcumgr/McuMgrTransport.java

+46-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package io.runtime.mcumgr;
99

1010
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1112

1213
import io.runtime.mcumgr.exception.McuMgrException;
1314
import io.runtime.mcumgr.response.McuMgrResponse;
@@ -20,18 +21,52 @@
2021
*/
2122
public interface McuMgrTransport {
2223

24+
/**
25+
* Receives connection state changes independent of explicit calls to
26+
* {@link #connect} or {@link #release}.
27+
* <p>
28+
* To add or remove an observer, use {@link #addObserver} and
29+
* {@link #removeObserver} respectively.
30+
*/
2331
interface ConnectionObserver {
2432
/**
25-
* A method called when the connection to the device has been established.
33+
* Called when the connection to the device has been established.
2634
*/
2735
void onConnected();
2836

2937
/**
30-
* A method called when the connection to the device has been lost.
38+
* Called when the connection to the device has been lost.
3139
*/
3240
void onDisconnected();
3341
}
3442

43+
/**
44+
* Receives callbacks from an explicit call to {@link #connect}.
45+
*/
46+
interface ConnectionCallback {
47+
/**
48+
* Called when connection attempt succeeds or is already open when {@link #connect} is
49+
* called.
50+
*/
51+
void onConnected();
52+
53+
/**
54+
* Called when the transporter has decided not to connect to the transporter at this time.
55+
*
56+
* This method is useful for transporters who do not wish to allow the caller of
57+
* {@link #connect} to manage the connection or would rather wait to connect until
58+
* necessary.
59+
*/
60+
void onDeferred();
61+
62+
/**
63+
* Called when the connection attempt has failed.
64+
*
65+
* @param t The connection failure reason.
66+
*/
67+
void onError(@NotNull Throwable t);
68+
}
69+
3570
/**
3671
* Gets the scheme for this transport (see {@link McuMgrScheme}).
3772
*
@@ -51,8 +86,7 @@ interface ConnectionObserver {
5186
* @throws McuMgrException thrown on error. Set the cause of the error if caused by a different
5287
* type of exception.
5388
*/
54-
@NotNull
55-
<T extends McuMgrResponse> T send(@NotNull byte[] payload, @NotNull Class<T> responseType)
89+
@NotNull <T extends McuMgrResponse> T send(@NotNull byte[] payload, @NotNull Class<T> responseType)
5690
throws McuMgrException;
5791

5892
/**
@@ -68,6 +102,14 @@ <T extends McuMgrResponse> T send(@NotNull byte[] payload, @NotNull Class<T> res
68102
<T extends McuMgrResponse> void send(@NotNull byte[] payload, @NotNull Class<T> responseType,
69103
@NotNull McuMgrCallback<T> callback);
70104

105+
/**
106+
* Connect the transporter to the remote device. The callback must be called if supplied, even
107+
* if the transport connection is already opened.
108+
*
109+
* @param callback An optional callback to receive the result of the connection attempt.
110+
*/
111+
void connect(@Nullable ConnectionCallback callback);
112+
71113
/**
72114
* Releases the transport connection. When the connection is already closed this method does
73115
* nothing.

0 commit comments

Comments
 (0)