@@ -16,7 +16,6 @@ import io.runtime.mcumgr.exception.McuMgrException
16
16
import io.runtime.mcumgr.image.McuMgrImage
17
17
import java.io.IOException
18
18
19
-
20
19
val UpgradeModes =
21
20
mapOf (
22
21
1 to FirmwareUpgradeManager .Mode .TEST_AND_CONFIRM ,
@@ -66,11 +65,36 @@ class DeviceUpgrade(
66
65
transport.release()
67
66
}
68
67
69
- fun uriToByteArray (uri : Uri ): ByteArray? {
68
+ private fun uriToByteArray (uri : Uri ): ByteArray? {
70
69
val inputStream = context.contentResolver.openInputStream(uri) ? : return null
71
70
return inputStream.use { it.readBytes() }
72
71
}
73
72
73
+ private fun extractImagesFrom (updateBundleUri : Uri ): ImageSet {
74
+ val type = context.contentResolver.getType(updateBundleUri)
75
+ val binData = uriToByteArray(updateBundleUri) ? : throw IOException (" Failed to read update file" )
76
+
77
+ if (type == " application/zip" ) {
78
+ return extractImagesFromZipFile(binData)
79
+ } else {
80
+ return extractImagesFromBinFile(binData)
81
+ }
82
+ }
83
+
84
+ private fun extractImagesFromBinFile (binData : ByteArray ): ImageSet {
85
+ // Check if the BIN file is valid.
86
+ McuMgrImage .getHash(binData)
87
+
88
+ val binaries = ImageSet ()
89
+ binaries.add(binData)
90
+
91
+ return binaries
92
+ }
93
+
94
+ private fun extractImagesFromZipFile (zipData : ByteArray ): ImageSet {
95
+ return ZipPackage (zipData).getBinaries();
96
+ }
97
+
74
98
private fun doUpdate (updateBundleUri : Uri ) {
75
99
val estimatedSwapTime = updateOptions.estimatedSwapTime * 1000
76
100
val modeInt = updateOptions.upgradeMode ? : 1
@@ -79,32 +103,10 @@ class DeviceUpgrade(
79
103
val settings = Settings .Builder ().setEstimatedSwapTime(estimatedSwapTime).build()
80
104
81
105
try {
82
- val byteArray = uriToByteArray(updateBundleUri) ? : return
83
-
84
- try {
85
- // Check if the BIN file is valid.
86
- McuMgrImage .getHash(byteArray)
87
-
88
- val stream = context.contentResolver.openInputStream(updateBundleUri)
89
- val images = ByteArray (stream!! .available())
90
- stream.read(images)
91
-
92
- dfuManager.setMode(upgradeMode)
93
- dfuManager.start(images, settings)
94
- } catch (e: Exception ) {
95
- try {
96
- val zip = ZipPackage (byteArray)
97
- val images: ImageSet = zip.getBinaries();
98
- dfuManager.setMode(upgradeMode)
99
- dfuManager.start(images, settings)
100
- } catch (e1: Exception ) {
101
- e1.printStackTrace()
102
- disconnectDevice()
103
- withSafePromise { promise ->
104
- promise.reject(ReactNativeMcuMgrException .fromMcuMgrException(e1))
105
- }
106
- }
107
- }
106
+ val images = extractImagesFrom(updateBundleUri)
107
+
108
+ dfuManager.setMode(upgradeMode)
109
+ dfuManager.start(images, settings)
108
110
} catch (e: IOException ) {
109
111
e.printStackTrace()
110
112
disconnectDevice()
0 commit comments