diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/McuManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/McuManager.java index 2af427f1..ccb4dcb0 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/McuManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/McuManager.java @@ -288,7 +288,7 @@ public T send(@NotNull byte[] data, @NotNull Class * @throws McuMgrException if the payload map could not be serialized into CBOR. See cause. */ @NotNull - public static byte[] buildPacket(McuMgrScheme scheme, int op, int flags, int groupId, + public static byte[] buildPacket(@NotNull McuMgrScheme scheme, int op, int flags, int groupId, int sequenceNum, int commandId, @Nullable Map payloadMap) throws McuMgrException { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/McuMgrHeader.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/McuMgrHeader.java index 8788057a..74bc1e88 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/McuMgrHeader.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/McuMgrHeader.java @@ -20,6 +20,7 @@ */ @SuppressWarnings({"unused", "WeakerAccess"}) public class McuMgrHeader { + public final static int HEADER_LENGTH = 8; private int mOp; @@ -38,6 +39,7 @@ public McuMgrHeader(int op, int flags, int len, int groupId, int sequenceNum, in mCommandId = commandId; } + @NotNull public byte[] toBytes() { return build(mOp, mFlags, mLen, mGroupId, mSequenceNum, mCommandId); } @@ -90,14 +92,25 @@ public void setCommandId(int commandId) { this.mCommandId = commandId; } + @NotNull @Override public String toString() { - return "Header (Op: " + mOp + ", Flags: " + mFlags + ", Len: " + mLen + ", Group: " + mGroupId + ", Seq: " + mSequenceNum + ", Command: " + mCommandId + ")"; + return "Header (Op: " + mOp + ", Flags: " + mFlags + ", Len: " + mLen + ", Group: " + + mGroupId + ", Seq: " + mSequenceNum + ", Command: " + mCommandId + ")"; } - public static McuMgrHeader fromBytes(byte[] header) { - if (header == null || header.length != HEADER_LENGTH) { - return null; + /** + * Parse the mcumgr header from a byte array. + * This function will parse the first 8 bytes from the array, discounting any additional bytes. + * + * @param header the byte array to parse the header from. + * @return The parsed mcumgr header. + * @throws IllegalArgumentException when the byte array length is less than 8 bytes + */ + @NotNull + public static McuMgrHeader fromBytes(@NotNull byte[] header) { + if (header.length < HEADER_LENGTH) { + throw new IllegalArgumentException("Failed to parse mcumgr header from bytes; too short - length=" + header.length); } int op = ByteUtil.byteArrayToUnsignedInt(header, 0, Endian.BIG, 1); int flags = ByteUtil.byteArrayToUnsignedInt(header, 1, Endian.BIG, 1); diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpHeader.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpHeader.java index 9ba7696c..675e8808 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpHeader.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpHeader.java @@ -30,6 +30,7 @@ public CoreDumpHeader(int magic, int size) { * @return The core dump header. * @throws IOException If the magic number was invalid. */ + @NotNull public static CoreDumpHeader fromBytes(@NotNull byte[] data) throws IOException { return fromBytes(data, OFFSET); } @@ -41,6 +42,7 @@ public static CoreDumpHeader fromBytes(@NotNull byte[] data) throws IOException * @return The core dump header. * @throws IOException If the magic number was invalid. */ + @NotNull public static CoreDumpHeader fromBytes(@NotNull byte[] data, int offset) throws IOException { int magic = ByteUtil.byteArrayToUnsignedInt(data, offset, Endian.LITTLE, 4); if (magic != CoreDump.MAGIC) { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlv.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlv.java index 373c61fe..c040e394 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlv.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlv.java @@ -15,6 +15,7 @@ public class CoreDumpTlv { private static final int OFFSET = 8; + @NotNull private List mEntries; public CoreDumpTlv(@NotNull List entries) { @@ -81,6 +82,11 @@ public List getEntriesOfType(int type) { return entries; } + /** + * Get the list of core dump TLV entries. + * @return the list of TLV entries + */ + @NotNull public List getEntries() { return mEntries; } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlvEntry.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlvEntry.java index 9f430271..33fd7a50 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlvEntry.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/crash/CoreDumpTlvEntry.java @@ -22,7 +22,6 @@ public class CoreDumpTlvEntry { private static final int MIN_SIZE = 8; private int mType; // uint8_t - //private byte __pad; // uint8_t private int mLength; // uint16_t private long mOff; // uint32_t @@ -77,6 +76,7 @@ public static CoreDumpTlvEntry fromBytes(@NotNull byte[] data, int offset) throw return new CoreDumpTlvEntry(type, length, off, value); } + @NotNull @Override public String toString() { return String.format("{type=%s, length=%s, off=%s, value=%s}", mType, mLength, mOff, diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImage.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImage.java index 3e0eef5a..647c586f 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImage.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImage.java @@ -19,7 +19,7 @@ * image header and type-length-value trailer for image meta-data. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ @SuppressWarnings({"WeakerAccess", "unused"}) public class McuMgrImage { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageHeader.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageHeader.java index 59d6e568..f797e087 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageHeader.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageHeader.java @@ -18,7 +18,7 @@ * bootloader. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ @SuppressWarnings({"unused", "WeakerAccess"}) public class McuMgrImageHeader { @@ -26,42 +26,54 @@ public class McuMgrImageHeader { private static final int IMG_HEADER_MAGIC = 0x96f3b83d; private static final int IMG_HEADER_MAGIC_V1 = 0x96f3b83c; - private static final int HEADER_LENGTH = 4 + 4 + 2 + 2 + 4 + 4 + 4; + private static final int HEADER_LENGTH = 24; private int mMagic; private int mLoadAddr; private short mHdrSize; - private short __mPad1; private int mImgSize; private int mFlags; + @NotNull private McuMgrImageVersion mVersion; - private int __mPad2; - private McuMgrImageHeader() {} + private McuMgrImageHeader(int magic, + int loadAddr, + short hdrSize, + int imgSize, + int flags, + @NotNull McuMgrImageVersion version) { + mMagic = magic; + mLoadAddr = loadAddr; + mHdrSize = hdrSize; + mImgSize = imgSize; + mFlags = flags; + mVersion = version; + } + @NotNull public static McuMgrImageHeader fromBytes(@NotNull byte[] b) throws McuMgrException { return fromBytes(b, 0); } + @NotNull public static McuMgrImageHeader fromBytes(@NotNull byte[] b, int offset) throws McuMgrException { if (b.length - offset < getSize()) { throw new McuMgrException("The byte array is too short to be a McuMgrImageHeader"); } - McuMgrImageHeader header = new McuMgrImageHeader(); - header.mMagic = ByteUtil.byteArrayToUnsignedInt(b, offset, Endian.LITTLE, 4); + int magic = ByteUtil.byteArrayToUnsignedInt(b, offset, Endian.LITTLE, 4); - if (header.mMagic != IMG_HEADER_MAGIC && header.mMagic != IMG_HEADER_MAGIC_V1) { - throw new McuMgrException("Wrong magic number: header=" + header.mMagic + ", magic=" + + if (magic != IMG_HEADER_MAGIC && magic != IMG_HEADER_MAGIC_V1) { + throw new McuMgrException("Wrong magic number: header=" + magic + ", magic=" + IMG_HEADER_MAGIC + " or " + IMG_HEADER_MAGIC_V1); } - header.mLoadAddr = ByteUtil.byteArrayToUnsignedInt(b, 4 + offset, Endian.LITTLE, 4); - header.mHdrSize = (short) ByteUtil.byteArrayToUnsignedInt(b, 8 + offset, Endian.LITTLE, 2); - header.mImgSize = ByteUtil.byteArrayToUnsignedInt(b, 12 + offset, Endian.LITTLE, 4); - header.mFlags = ByteUtil.byteArrayToUnsignedInt(b, 16 + offset, Endian.LITTLE, 4); - header.mVersion = McuMgrImageVersion.fromBytes(b, 20 + offset); + int loadAddr = ByteUtil.byteArrayToUnsignedInt(b, 4 + offset, Endian.LITTLE, 4); + short hdrSize = (short) ByteUtil.byteArrayToUnsignedInt(b, 8 + offset, Endian.LITTLE, 2); + int imgSize = ByteUtil.byteArrayToUnsignedInt(b, 12 + offset, Endian.LITTLE, 4); + int flags = ByteUtil.byteArrayToUnsignedInt(b, 16 + offset, Endian.LITTLE, 4); + McuMgrImageVersion version = McuMgrImageVersion.fromBytes(b, 20 + offset); - return header; + return new McuMgrImageHeader(magic, loadAddr, hdrSize, imgSize, flags, version); } public static int getSize() { @@ -88,6 +100,7 @@ public int getFlags() { return mFlags; } + @NotNull public McuMgrImageVersion getVersion() { return mVersion; } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageVersion.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageVersion.java index 7301f552..2eb2e774 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageVersion.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/McuMgrImageVersion.java @@ -6,6 +6,8 @@ package io.runtime.mcumgr.image; +import org.jetbrains.annotations.NotNull; + import io.runtime.mcumgr.exception.McuMgrException; import io.runtime.mcumgr.util.ByteUtil; import io.runtime.mcumgr.util.Endian; @@ -15,33 +17,40 @@ * bootloader. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ @SuppressWarnings("unused") public class McuMgrImageVersion { + private byte mMajor; private byte mMinor; private short mRevision; private int mBuildNum; - private McuMgrImageVersion() {} + private McuMgrImageVersion(byte major, byte minor, short revision, int buildNum) { + mMajor = major; + mMinor = minor; + mRevision = revision; + mBuildNum = buildNum; + } - public static McuMgrImageVersion fromBytes(byte[] b) throws McuMgrException { + @NotNull + public static McuMgrImageVersion fromBytes(@NotNull byte[] b) throws McuMgrException { return fromBytes(b, 0); } - public static McuMgrImageVersion fromBytes(byte[] b, int offset) throws McuMgrException { + @NotNull + public static McuMgrImageVersion fromBytes(@NotNull byte[] b, int offset) throws McuMgrException { if (b.length - offset < getSize()) { throw new McuMgrException("The byte array is too short to be a McuMgrImageVersion"); } - McuMgrImageVersion version = new McuMgrImageVersion(); - version.mMajor = b[offset++]; - version.mMinor = b[offset++]; - version.mRevision = (short) ByteUtil.byteArrayToUnsignedInt(b, offset, Endian.LITTLE, 2); - version.mBuildNum = ByteUtil.byteArrayToUnsignedInt(b, offset + 2, Endian.LITTLE, 4); + byte major = b[offset++]; + byte minor = b[offset++]; + short revision = (short) ByteUtil.byteArrayToUnsignedInt(b, offset, Endian.LITTLE, 2); + int buildNum = ByteUtil.byteArrayToUnsignedInt(b, offset + 2, Endian.LITTLE, 4); - return version; + return new McuMgrImageVersion(major, minor, revision, buildNum); } public static int getSize() { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlv.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlv.java index 9ea3b57a..46e107de 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlv.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlv.java @@ -20,7 +20,7 @@ * Mynewt bootloader. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ @SuppressWarnings({"unused", "WeakerAccess"}) public class McuMgrImageTlv { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvInfo.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvInfo.java index f2157153..45831458 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvInfo.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvInfo.java @@ -15,7 +15,7 @@ * Mynewt bootloader. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ @SuppressWarnings("unused") public class McuMgrImageTlvInfo { diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvTrailerEntry.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvTrailerEntry.java index 840afbc1..20544bba 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvTrailerEntry.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/image/tlv/McuMgrImageTlvTrailerEntry.java @@ -17,7 +17,7 @@ * Apache Mynewt bootloader. *

* For more info about McuBoot and image format see: - * https://runtimeco.github.io/mcuboot/design.html + * https://juullabs-oss.github.io/mcuboot/design.html */ public class McuMgrImageTlvTrailerEntry { public final byte type; diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/CrashManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/CrashManager.java index 60d27eea..9ba04939 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/CrashManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/CrashManager.java @@ -53,6 +53,7 @@ public CrashManager(@NotNull McuMgrTransport transporter) { * @return The response * @throws McuMgrException on failure. */ + @NotNull public McuMgrResponse test(@NotNull Test test) throws McuMgrException { HashMap payloadMap = new HashMap<>(); payloadMap.put("t", test.toString()); diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/FsManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/FsManager.java index 46bcac21..679f719c 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/FsManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/FsManager.java @@ -135,6 +135,7 @@ public McuMgrFsUploadResponse upload(@NotNull String name, @NotNull byte[] data, /* * Build the upload payload map. */ + @NotNull private HashMap buildUploadPayload(@NotNull String name, @NotNull byte[] data, int offset) { // Get the length of data (in bytes) to put into the upload packet. This calculated as: // min(MTU - packetOverhead, imageLength - uploadOffset) @@ -175,6 +176,7 @@ private HashMap buildUploadPayload(@NotNull String name, @NotNul * @return The object used to control this upload. * @see TransferController */ + @NotNull public TransferController fileUpload(@NotNull String name, @NotNull byte[] data, @NotNull UploadCallback callback) { return startUpload(new FileUpload(name, data, callback)); } @@ -215,6 +217,7 @@ protected UploadResponse write(@NotNull byte[] data, int offset) throws McuMgrEx * @return The object used to control this upload. * @see TransferController */ + @NotNull public TransferController fileDownload(@NotNull String name, @NotNull byte[] data, @NotNull DownloadCallback callback) { return startDownload(new FileDownload(name, callback)); } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/ImageManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/ImageManager.java index bda67df1..e769233f 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/ImageManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/ImageManager.java @@ -149,6 +149,7 @@ public McuMgrImageUploadResponse upload(@NotNull byte[] data, int offset) throws /* * Build the upload payload. */ + @NotNull private HashMap buildUploadPayload(@NotNull byte[] data, int offset) { // Get chunk of image data to send int dataLength = Math.min(mMtu - calculatePacketOverhead(data, offset), data.length - offset); @@ -387,6 +388,7 @@ public McuMgrResponse coreErase() throws McuMgrException { * @see TransferController * @see CoreDump */ + @NotNull public TransferController coreDownload(@NotNull DownloadCallback callback) { return startDownload(new CoreDownload(callback)); } @@ -422,6 +424,7 @@ public DownloadResponse read(int offset) throws McuMgrException { * @return The object used to control this upload. * @see TransferController */ + @NotNull public TransferController imageUpload(@NotNull byte[] imageData, @NotNull UploadCallback callback) { return startUpload(new ImageUpload(imageData, callback)); } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/LogManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/LogManager.java index 3444a04c..9cf58ac9 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/LogManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/managers/LogManager.java @@ -127,7 +127,6 @@ public McuMgrLogResponse show(@Nullable String logName, @Nullable Long minIndex, return send(OP_READ, ID_READ, payloadMap, McuMgrLogResponse.class); } - /** * Clear the logs on a device (asynchronous). * @@ -221,6 +220,7 @@ public McuMgrLogListResponse logsList() throws McuMgrException { * * @return A mapping of log name to state. */ + @NotNull public synchronized Map getAll() { HashMap logStates = new HashMap<>(); try { @@ -259,10 +259,8 @@ public synchronized Map getAll() { * @param state The log state to collect logs from. * @return The log state with updated next index and entry list. */ - public State getAllFromState(State state) { - if (state == null) { - throw new NullPointerException("State must not be null!"); - } + @NotNull + public State getAllFromState(@NotNull State state) { // Loop until we run out of entries or encounter a problem while (true) { // Get the next set of entries for this log @@ -309,7 +307,8 @@ public State getAllFromState(State state) { * @param state The state to get logs from. * @return The show response. */ - public McuMgrLogResponse showNext(State state) { + @Nullable + public McuMgrLogResponse showNext(@NotNull State state) { LOG.debug("Show logs: name={}, nextIndex={}", state.getName(), state.getNextIndex()); try { McuMgrLogResponse response = show(state.getName(), state.getNextIndex(), null); diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/response/McuMgrResponse.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/response/McuMgrResponse.java index 2c674148..63a77c59 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/response/McuMgrResponse.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/response/McuMgrResponse.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,7 +24,7 @@ import io.runtime.mcumgr.exception.McuMgrCoapException; import io.runtime.mcumgr.util.CBOR; -@SuppressWarnings({"WeakerAccess", "unused"}) +@SuppressWarnings("unused") @JsonIgnoreProperties(ignoreUnknown = true) public class McuMgrResponse { @@ -52,11 +53,6 @@ public class McuMgrResponse { */ private McuMgrHeader mHeader; - /** - * The return code (enum) for this response. For the raw return code use the "rc" property. - */ - private McuMgrErrorCode mReturnCode; - /** * McuMgr payload for this response. This does not include the McuMgr header for standard * schemes and does not include the CoAP header for CoAP schemes. @@ -77,6 +73,7 @@ public McuMgrResponse() { * * @return The string representation of the response payload. */ + @NotNull @Override public String toString() { try { @@ -92,7 +89,7 @@ public String toString() { * * @return The McuMgrHeader. */ - @NotNull + @Nullable public McuMgrHeader getHeader() { return mHeader; } @@ -103,12 +100,7 @@ public McuMgrHeader getHeader() { * @return Mcu Manager return code. */ public int getReturnCodeValue() { - if (mReturnCode == null) { - LOG.warn("Response does not contain a McuMgr return code."); - return 0; - } else { - return mReturnCode.value(); - } + return rc; } /** @@ -117,7 +109,7 @@ public int getReturnCodeValue() { * @return The return code enum. */ public McuMgrErrorCode getReturnCode() { - return mReturnCode; + return McuMgrErrorCode.valueOf(rc); } /** @@ -150,6 +142,7 @@ public byte[] getBytes() { * * @return The payload bytes. */ + @Nullable public byte[] getPayload() { return mPayload; } @@ -191,16 +184,13 @@ public int getCoapCode() { * @param bytes packet bytes. * @param header McuMgrHeader. * @param payload McuMgr CBOR payload. - * @param rc the return code. */ void initFields(@NotNull McuMgrScheme scheme, @NotNull byte[] bytes, - @NotNull McuMgrHeader header, @NotNull byte[] payload, - @NotNull McuMgrErrorCode rc) { + @NotNull McuMgrHeader header, @NotNull byte[] payload) { mScheme = scheme; mBytes = bytes; mHeader = header; mPayload = payload; - mReturnCode = rc; } /** @@ -214,6 +204,7 @@ void initFields(@NotNull McuMgrScheme scheme, @NotNull byte[] bytes, * @throws IOException Error parsing response. * @throws IllegalArgumentException If the scheme is CoAP. */ + @NotNull public static T buildResponse(@NotNull McuMgrScheme scheme, @NotNull byte[] bytes, @NotNull Class type) @@ -227,8 +218,7 @@ public static T buildResponse(@NotNull McuMgrScheme s // Initialize response and set fields T response = CBOR.toObject(payload, type); - McuMgrErrorCode rc = McuMgrErrorCode.valueOf(response.rc); - response.initFields(scheme, bytes, header, payload, rc); + response.initFields(scheme, bytes, header, payload); return response; } @@ -249,6 +239,7 @@ public static T buildResponse(@NotNull McuMgrScheme s * @throws IOException if parsing the payload into the object (type T) failed * @throws McuMgrCoapException if the CoAP code class indicates a CoAP error response */ + @NotNull public static T buildCoapResponse(@NotNull McuMgrScheme scheme, @NotNull byte[] bytes, @NotNull byte[] header, @@ -263,8 +254,7 @@ public static T buildCoapResponse(@NotNull McuMgrSche } T response = CBOR.toObject(payload, type); - McuMgrErrorCode rc = McuMgrErrorCode.valueOf(response.rc); - response.initFields(scheme, bytes, McuMgrHeader.fromBytes(header), payload, rc); + response.initFields(scheme, bytes, McuMgrHeader.fromBytes(header), payload); int code = (codeClass * 100) + codeDetail; response.setCoapCode(code); return response; diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Download.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Download.java index e5fa5d67..111f473f 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Download.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Download.java @@ -1,6 +1,7 @@ package io.runtime.mcumgr.transfer; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import io.runtime.mcumgr.McuMgrErrorCode; @@ -67,7 +68,7 @@ public void onProgressChanged(int current, int total, long timestamp) { } @Override - public void onFailed(McuMgrException e) { + public void onFailed(@NotNull McuMgrException e) { if (mCallback != null) { mCallback.onDownloadFailed(e); } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferCallback.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferCallback.java index 117842ca..66359f9a 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferCallback.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferCallback.java @@ -1,5 +1,7 @@ package io.runtime.mcumgr.transfer; +import org.jetbrains.annotations.NotNull; + import io.runtime.mcumgr.exception.McuMgrException; public interface TransferCallback { @@ -11,7 +13,7 @@ public interface TransferCallback { * @param timestamp the timestamp of when the response was received. */ void onProgressChanged(int current, int total, long timestamp); - void onFailed(McuMgrException e); + void onFailed(@NotNull McuMgrException e); void onCompleted(); void onCanceled(); } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferManager.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferManager.java index c0be1b73..107980db 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferManager.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/TransferManager.java @@ -31,6 +31,7 @@ protected TransferManager(int groupId, @NotNull McuMgrTransport transporter) { * @param upload The upload to start. * @return The controller used to pause, resume, or cancel the upload. */ + @NotNull public TransferController startUpload(@NotNull Upload upload) { return startTransfer(upload); } @@ -43,10 +44,12 @@ public TransferController startUpload(@NotNull Upload upload) { * @param download The upload to start. * @return The controller used to pause, resume, or cancel the download. */ + @NotNull public TransferController startDownload(@NotNull Download download) { return startTransfer(download); } + @NotNull private synchronized TransferController startTransfer(@NotNull final Transfer transfer) { final TransferCallable transferCallable = new TransferCallable(transfer); diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Upload.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Upload.java index a82613a7..b5becfc1 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Upload.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/Upload.java @@ -53,7 +53,7 @@ public void onProgressChanged(int current, int total, long timestamp) { } @Override - public void onFailed(McuMgrException e) { + public void onFailed(@NotNull McuMgrException e) { if (mCallback != null) { mCallback.onUploadFailed(e); } diff --git a/mcumgr-core/src/main/java/io/runtime/mcumgr/util/ByteUtil.java b/mcumgr-core/src/main/java/io/runtime/mcumgr/util/ByteUtil.java index 95996899..7510308a 100644 --- a/mcumgr-core/src/main/java/io/runtime/mcumgr/util/ByteUtil.java +++ b/mcumgr-core/src/main/java/io/runtime/mcumgr/util/ByteUtil.java @@ -198,15 +198,18 @@ public static long byteArrayToUnsignedLong(@NotNull byte[] data, // String //****************************************************************** - public static String byteArrayToHex(byte[] a) { + @NotNull + public static String byteArrayToHex(@NotNull byte[] a) { return byteArrayToHex(a, 0, a.length, "%02x "); } - public static String byteArrayToHex(byte[] a, String format) { + @NotNull + public static String byteArrayToHex(@NotNull byte[] a, String format) { return byteArrayToHex(a, 0, a.length, format); } - public static String byteArrayToHex(byte[] a, int offset, int length, String format) { + @NotNull + public static String byteArrayToHex(@NotNull byte[] a, int offset, int length, String format) { StringBuilder sb = new StringBuilder(a.length * 2); for (int i = offset; i < length; i++) { sb.append(String.format(format, a[i]));