Skip to content

Commit ef32ad2

Browse files
authored
CBOR Logging improvements (#77)
* CBOR updated * Logging sent and received CBOR messages parsed * Reverting changes in BLE transport * Logging CBOR messages * Logging improvements in sample app * Default logging level reverted * Restoring access modifier * Unused method removed * Suppressing deprecation warnings * Logging improvements in deprecated implementations * CBOR logging moved to BLE transport
1 parent f8e863d commit ef32ad2

File tree

9 files changed

+97
-38
lines changed

9 files changed

+97
-38
lines changed

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
import android.os.Build;
1515
import android.util.Log;
1616

17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
1720
import org.slf4j.Logger;
1821
import org.slf4j.LoggerFactory;
1922

2023
import java.util.LinkedList;
2124
import java.util.List;
2225
import java.util.UUID;
2326

24-
import androidx.annotation.NonNull;
25-
import androidx.annotation.Nullable;
2627
import io.runtime.mcumgr.McuMgrCallback;
28+
import io.runtime.mcumgr.McuMgrHeader;
2729
import io.runtime.mcumgr.McuMgrScheme;
2830
import io.runtime.mcumgr.McuMgrTransport;
2931
import io.runtime.mcumgr.ble.callback.SmpDataCallback;
@@ -34,6 +36,7 @@
3436
import io.runtime.mcumgr.exception.McuMgrException;
3537
import io.runtime.mcumgr.exception.McuMgrTimeoutException;
3638
import io.runtime.mcumgr.response.McuMgrResponse;
39+
import io.runtime.mcumgr.util.CBOR;
3740
import no.nordicsemi.android.ble.BleManager;
3841
import no.nordicsemi.android.ble.Request;
3942
import no.nordicsemi.android.ble.annotation.ConnectionPriority;
@@ -253,12 +256,32 @@ public <T extends McuMgrResponse> T send(@NonNull final byte[] payload,
253256

254257
// Send the request and wait for a notification in a synchronous way
255258
try {
259+
if (mLoggingEnabled) {
260+
try {
261+
log(Log.VERBOSE, "Sending "
262+
+ McuMgrHeader.fromBytes(payload).toString() + " CBOR "
263+
+ CBOR.toString(payload, McuMgrHeader.HEADER_LENGTH));
264+
} catch (Exception e) {
265+
// Ignore
266+
}
267+
}
256268
final SmpResponse<T> smpResponse = waitForNotification(mSmpCharacteristic)
257269
.merge(mSMPMerger)
258270
.trigger(writeCharacteristic(mSmpCharacteristic, payload).split())
259271
.timeout(30000)
260272
.await(new SmpResponse<>(responseType));
261273
if (smpResponse.isValid()) {
274+
if (mLoggingEnabled) {
275+
try {
276+
byte[] response = smpResponse.getRawData().getValue();
277+
//noinspection ConstantConditions
278+
log(Log.INFO, "Received "
279+
+ McuMgrHeader.fromBytes(response).toString() + " CBOR "
280+
+ CBOR.toString(response, McuMgrHeader.HEADER_LENGTH));
281+
} catch (Exception e) {
282+
// Ignore
283+
}
284+
}
262285
//noinspection ConstantConditions
263286
return smpResponse.getResponse();
264287
} else {
@@ -303,9 +326,35 @@ public void onRequestCompleted(@NonNull final BluetoothDevice device) {
303326
return;
304327
}
305328

329+
if (mLoggingEnabled) {
330+
try {
331+
log(Log.VERBOSE, "Sending "
332+
+ McuMgrHeader.fromBytes(payload).toString() + " CBOR "
333+
+ CBOR.toString(payload, McuMgrHeader.HEADER_LENGTH));
334+
} catch (Exception e) {
335+
// Ignore
336+
}
337+
}
306338
waitForNotification(mSmpCharacteristic)
307339
.merge(mSMPMerger)
308340
.with(new SmpDataCallback<T>(responseType) {
341+
@Override
342+
public void onDataReceived(@NonNull BluetoothDevice device,
343+
@NonNull Data data) {
344+
if (mLoggingEnabled) {
345+
try {
346+
byte[] response = data.getValue();
347+
//noinspection ConstantConditions
348+
log(Log.INFO, "Received "
349+
+ McuMgrHeader.fromBytes(response).toString() + " CBOR "
350+
+ CBOR.toString(response, McuMgrHeader.HEADER_LENGTH));
351+
} catch (Exception e) {
352+
// Ignore
353+
}
354+
}
355+
super.onDataReceived(device, data);
356+
}
357+
309358
@Override
310359
public void onResponseReceived(@NonNull BluetoothDevice device,
311360
@NonNull T response) {

mcumgr-ble/src/main/java/io/runtime/mcumgr/ble/callback/SmpDataCallback.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protected SmpDataCallback(@NonNull Class<T> responseType) {
2525
@Override
2626
public void onDataReceived(@NonNull BluetoothDevice device, @NonNull Data data) {
2727
try {
28+
//noinspection ConstantConditions
2829
T response = McuMgrResponse.buildResponse(McuMgrScheme.BLE, data.getValue(), responseType);
2930
onResponseReceived(device, response);
3031
} catch (final Exception e) {

mcumgr-core/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ dependencies {
3737
implementation 'org.slf4j:slf4j-api:1.7.30'
3838

3939
// Import CBOR parser
40-
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.11.0.rc1'
41-
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.0.rc1'
42-
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0.rc1'
40+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.11.0'
41+
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.0'
42+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
4343

4444
// Test
4545
testImplementation 'junit:junit:4.13'

mcumgr-core/src/main/java/io/runtime/mcumgr/dfu/FirmwareUpgradeManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private synchronized void fail(McuMgrException error) {
388388
}
389389

390390
private synchronized void cancelled(State state) {
391-
LOG.trace("Upgrade cancelled!");
391+
LOG.trace("Upgrade cancelled");
392392
mState = State.NONE;
393393
mPaused = false;
394394
mInternalCallback.onUpgradeCanceled(state);
@@ -579,7 +579,7 @@ public void onConnected() {
579579
public void onDisconnected() {
580580
mDefaultManager.getTransporter().removeObserver(mResetObserver);
581581

582-
LOG.trace("Device disconnected.");
582+
LOG.info("Device disconnected");
583583
Runnable reconnect = new Runnable() {
584584
@Override
585585
public void run() {
@@ -610,19 +610,19 @@ public void run() {
610610

611611
@Override
612612
public void onConnected() {
613-
LOG.trace("Reconnect successful.");
613+
LOG.info("Reconnect successful");
614614
continueUpgrade();
615615
}
616616

617617
@Override
618618
public void onDeferred() {
619-
LOG.trace("Reconnect deferred.");
619+
LOG.info("Reconnect deferred");
620620
continueUpgrade();
621621
}
622622

623623
@Override
624624
public void onError(@NotNull Throwable t) {
625-
LOG.trace("Reconnect failed.");
625+
LOG.error("Reconnect failed");
626626
fail(new McuMgrException(t));
627627
}
628628

@@ -740,7 +740,7 @@ public void onError(@NotNull McuMgrException e) {
740740
if (e instanceof McuMgrTimeoutException) {
741741
if (mAttempts++ < MAX_ATTEMPTS) {
742742
// Try again
743-
LOG.info("Connection timeout. Retrying...");
743+
LOG.warn("Connection timeout. Retrying...");
744744
verify();
745745
return;
746746
}

mcumgr-core/src/main/java/io/runtime/mcumgr/managers/FsManager.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import io.runtime.mcumgr.transfer.UploadCallback;
3333
import io.runtime.mcumgr.util.CBOR;
3434

35-
@SuppressWarnings({"WeakerAccess", "unused"})
35+
@SuppressWarnings({"WeakerAccess", "unused", "DeprecatedIsStillUsed", "deprecation"})
3636
public class FsManager extends TransferManager {
3737

3838
private final static Logger LOG = LoggerFactory.getLogger(FsManager.class);
@@ -291,7 +291,7 @@ public synchronized void download(@NotNull String name, @NotNull FileDownloadCal
291291
if (mTransferState == STATE_NONE) {
292292
mTransferState = STATE_DOWNLOADING;
293293
} else {
294-
LOG.debug("FsManager is not ready");
294+
LOG.warn("FsManager is not ready");
295295
return;
296296
}
297297

@@ -316,7 +316,7 @@ public synchronized void upload(@NotNull String name, @NotNull byte[] data,
316316
if (mTransferState == STATE_NONE) {
317317
mTransferState = STATE_UPLOADING;
318318
} else {
319-
LOG.debug("FsManager is not ready");
319+
LOG.warn("FsManager is not ready");
320320
return;
321321
}
322322

@@ -347,7 +347,7 @@ public synchronized void cancelTransfer() {
347347
if (mTransferState == STATE_NONE) {
348348
LOG.debug("File transfer is not in progress");
349349
} else if (mTransferState == STATE_PAUSED) {
350-
LOG.debug("Upload canceled!");
350+
LOG.info("Upload canceled");
351351
resetTransfer();
352352
if (mUploadCallback != null) {
353353
mUploadCallback.onUploadCanceled();
@@ -372,7 +372,7 @@ public synchronized void pauseTransfer() {
372372
if (mTransferState == STATE_NONE) {
373373
LOG.debug("File transfer is not in progress.");
374374
} else {
375-
LOG.debug("Upload paused.");
375+
LOG.info("Upload paused");
376376
mTransferState = STATE_PAUSED;
377377
}
378378
}
@@ -384,7 +384,7 @@ public synchronized void pauseTransfer() {
384384
@Deprecated
385385
public synchronized void continueTransfer() {
386386
if (mTransferState == STATE_PAUSED) {
387-
LOG.debug("Continuing transfer.");
387+
LOG.info("Continuing transfer...");
388388
if (mDownloadCallback != null) {
389389
mTransferState = STATE_DOWNLOADING;
390390
requestNext(mOffset);
@@ -432,7 +432,7 @@ private synchronized void resetTransfer() {
432432
private synchronized void sendNext(int offset) {
433433
// Check that the state is STATE_UPLOADING
434434
if (mTransferState != STATE_UPLOADING) {
435-
LOG.debug("Fs Manager is not in the UPLOADING state.");
435+
LOG.warn("Fs Manager is not in the UPLOADING state.");
436436
return;
437437
}
438438
upload(mFileName, mFileData, offset, mUploadCallbackImpl);
@@ -446,7 +446,7 @@ private synchronized void sendNext(int offset) {
446446
private synchronized void requestNext(int offset) {
447447
// Check that the state is STATE_UPLOADING
448448
if (mTransferState != STATE_DOWNLOADING) {
449-
LOG.debug("Fs Manager is not in the DOWNLOADING state.");
449+
LOG.warn("Fs Manager is not in the DOWNLOADING state.");
450450
return;
451451
}
452452
download(mFileName, offset, mDownloadCallbackImpl);
@@ -473,7 +473,7 @@ public void onResponse(@NotNull McuMgrFsUploadResponse response) {
473473

474474
// Check if upload hasn't been cancelled.
475475
if (mTransferState == STATE_NONE) {
476-
LOG.debug("Upload canceled!");
476+
LOG.info("Upload canceled");
477477
resetTransfer();
478478
mUploadCallback.onUploadCanceled();
479479
mUploadCallback = null;
@@ -489,7 +489,7 @@ public void onResponse(@NotNull McuMgrFsUploadResponse response) {
489489

490490
// Check if the upload has finished.
491491
if (mOffset == mFileData.length) {
492-
LOG.debug("Upload finished!");
492+
LOG.info("Upload finished");
493493
resetTransfer();
494494
mUploadCallback.onUploadFinished();
495495
mUploadCallback = null;
@@ -542,7 +542,7 @@ public void onResponse(@NotNull McuMgrFsDownloadResponse response) {
542542

543543
// Check if download hasn't been cancelled.
544544
if (mTransferState == STATE_NONE) {
545-
LOG.debug("Download canceled!");
545+
LOG.info("Download canceled");
546546
resetTransfer();
547547
mDownloadCallback.onDownloadCanceled();
548548
mDownloadCallback = null;
@@ -567,7 +567,7 @@ public void onResponse(@NotNull McuMgrFsDownloadResponse response) {
567567

568568
// Check if the download has finished.
569569
if (mOffset == mFileData.length) {
570-
LOG.debug("Download finished!");
570+
LOG.info("Download finished");
571571
byte[] data = mFileData;
572572
String fileName = mFileName;
573573
resetTransfer();

mcumgr-core/src/main/java/io/runtime/mcumgr/managers/ImageManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
*
5252
* @see FirmwareUpgradeManager
5353
*/
54-
@SuppressWarnings({"unused", "WeakerAccess"})
54+
@SuppressWarnings({"unused", "WeakerAccess", "deprecation", "DeprecatedIsStillUsed"})
5555
public class ImageManager extends TransferManager {
5656

5757
private final static Logger LOG = LoggerFactory.getLogger(ImageManager.class);
@@ -506,7 +506,7 @@ public synchronized void cancelUpload() {
506506
if (mUploadState == STATE_NONE) {
507507
LOG.debug("Image upload is not in progress");
508508
} else if (mUploadState == STATE_PAUSED) {
509-
LOG.debug("Upload canceled!");
509+
LOG.info("Upload canceled");
510510
resetUpload();
511511
mUploadCallback.onUploadCanceled();
512512
mUploadCallback = null;
@@ -523,7 +523,7 @@ public synchronized void pauseUpload() {
523523
if (mUploadState == STATE_NONE) {
524524
LOG.debug("Upload is not in progress.");
525525
} else {
526-
LOG.debug("Upload paused.");
526+
LOG.info("Upload paused");
527527
mUploadState = STATE_PAUSED;
528528
}
529529
}
@@ -535,7 +535,7 @@ public synchronized void pauseUpload() {
535535
@Deprecated
536536
public synchronized void continueUpload() {
537537
if (mUploadState == STATE_PAUSED) {
538-
LOG.debug("Continuing upload.");
538+
LOG.info("Continuing upload...");
539539
mUploadState = STATE_UPLOADING;
540540
sendNext(mUploadOffset);
541541
} else {

mcumgr-core/src/main/java/io/runtime/mcumgr/util/CBOR.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static String toString(byte[] data) throws IOException {
3636
return mapper.readTree(data).toString();
3737
}
3838

39+
public static String toString(byte[] data, int offset) throws IOException {
40+
ObjectMapper mapper = new ObjectMapper(sFactory);
41+
return mapper.readTree(data, offset, data.length - offset).toString();
42+
}
43+
3944
@SuppressWarnings("RedundantThrows")
4045
public static <T> String toString(T obj) throws IOException {
4146
ObjectMapper mapper = new ObjectMapper(sFactory);

sample/src/main/java/io/runtime/mcumgr/sample/observable/ObservableMcuMgrBleTransport.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void onBondingFailed(@NonNull final BluetoothDevice device) {
8585
mBondingState.setValue(BondingState.NOT_BONDED);
8686
}
8787
});
88+
setLoggingEnabled(true);
8889
}
8990

9091
@Nullable

0 commit comments

Comments
 (0)