OSDN Git Service

NikonのLV、すこし改善。
authorMRSa <mrsa@myad.jp>
Sun, 18 Oct 2020 08:35:50 +0000 (17:35 +0900)
committerMRSa <mrsa@myad.jp>
Sun, 18 Oct 2020 08:35:50 +0000 (17:35 +0900)
app/src/main/java/net/osdn/gokigen/a01d/camera/nikon/wrapper/liveview/NikonLiveViewImageReceiver.kt
app/src/main/java/net/osdn/gokigen/a01d/camera/ptpip/wrapper/command/PtpIpCommandPublisher.kt

index 1d781d7..ad6e1cc 100644 (file)
@@ -10,8 +10,8 @@ import java.util.*
 class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallback) : IPtpIpCommandCallback
 {
     private val isDumpLog = true
-    private var received_total_bytes = 0
-    private var received_remain_bytes = 0
+    private var receivedTotalBytes = 0
+    private var receivedRemainBytes = 0
     private var receivedFirstData = false
     private var byteStream = ByteArrayOutputStream()
 
@@ -19,16 +19,16 @@ class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallba
     {
         if (rx_body == null)
         {
-            Log.v(TAG, " MSG BODY IS NULL.")
+            Log.v(TAG, " MSG BODY IS NULL. $id")
             return
         }
         if (isReceiveMulti)
         {
-            receivedMessage_multi(id, rx_body)
+            receivedMessageMulti(id, rx_body)
         }
         else
         {
-            receivedMessage_single(id, rx_body)
+            receivedMessageSingle(id, rx_body)
         }
     }
 
@@ -45,15 +45,15 @@ class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallba
         cutHeader(rx_body)
     }
 
-    private fun receivedMessage_single(id: Int, rx_body: ByteArray)
+    private fun receivedMessageSingle(id: Int, rx_body: ByteArray)
     {
         try
         {
-            Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes.")
+            Log.v(TAG, "receivedMessage_single() : " + rx_body.size + " bytes. id:$id")
             if ((isDumpLog)&&(rx_body.size > 64))
             {
-                SimpleLogDumper.dump_bytes(" LV (FIRST) : ", Arrays.copyOfRange(rx_body, 0, 64))
-                SimpleLogDumper.dump_bytes(" LV (-END-) : ", Arrays.copyOfRange(rx_body, rx_body.size - 64, rx_body.size))
+                SimpleLogDumper.dump_bytes(" LV (FIRST) : ", rx_body.copyOfRange(0, 64))
+                SimpleLogDumper.dump_bytes(" LV (-END-) : ", rx_body.copyOfRange(rx_body.size - 64, rx_body.size))
             }
             callback.onCompleted(rx_body, null)
         }
@@ -63,16 +63,16 @@ class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallba
         }
     }
 
-    private fun receivedMessage_multi(id: Int, rx_body: ByteArray)
+    private fun receivedMessageMulti(id: Int, rx_body: ByteArray)
     {
         try
         {
-            Log.v(TAG, " receivedMessage_multi()  id[$id] size : ${rx_body.size} ")
+            Log.v(TAG, " receivedMessage_multi()  id[$id] size : ${rx_body.size}  id:$id")
 
             callback.onCompleted(byteStream.toByteArray(), null)
             receivedFirstData = false
-            received_remain_bytes = 0
-            received_total_bytes = 0
+            receivedRemainBytes = 0
+            receivedTotalBytes = 0
             byteStream.reset()
         }
         catch (e: Exception)
@@ -85,74 +85,74 @@ class NikonLiveViewImageReceiver(private var callback: IPtpIpLiveViewImageCallba
     private fun cutHeader(rx_body: ByteArray)
     {
         val length = rx_body.size
-        var data_position = 0
+        var dataPosition = 0
         if (!receivedFirstData)
         {
             // データを最初に読んだとき。ヘッダ部分を読み飛ばす
             receivedFirstData = true
-            data_position = rx_body[0].toUByte().toInt()
+            dataPosition = rx_body[0].toUByte().toInt()
             if (isDumpLog)
             {
-                Log.v(TAG, " FIRST DATA POS. : $data_position len: $length ");
-                SimpleLogDumper.dump_bytes(" [sXXs]", Arrays.copyOfRange(rx_body, 0, (32)));
+                Log.v(TAG, " FIRST DATA POS. : $dataPosition len: $length ");
+                SimpleLogDumper.dump_bytes(" [sXXs]", rx_body.copyOfRange(0, (32)))
             }
         }
         else
         {
             // 2回目以降の受信データ
-            if (received_remain_bytes > 0)
+            if (receivedRemainBytes > 0)
             {
                 // データの読み込みが途中だった場合...
-                if (length < received_remain_bytes)
+                if (length < receivedRemainBytes)
                 {
                     // 全部コピーする、足りないバイト数は残す
-                    received_remain_bytes = received_remain_bytes - length
-                    received_total_bytes = received_total_bytes + rx_body.size
+                    receivedRemainBytes -= length
+                    receivedTotalBytes += rx_body.size
                     byteStream.write(rx_body, 0, rx_body.size)
                     return
                 }
                 else
                 {
-                    byteStream.write(rx_body, data_position, received_remain_bytes)
-                    data_position = received_remain_bytes
-                    received_remain_bytes = 0
+                    byteStream.write(rx_body, dataPosition, receivedRemainBytes)
+                    dataPosition = receivedRemainBytes
+                    receivedRemainBytes = 0
                 }
             }
         }
-        while (data_position <= length - 12)
+        while (dataPosition <= length - 12)
         {
-            val body_size = (rx_body[data_position].toUByte()).toInt() + ((rx_body[data_position + 1].toUByte()).toInt() * 256) + ((rx_body[data_position + 2].toUByte()).toInt() * 256 * 256) + ((rx_body[data_position + 3].toUByte()).toInt() * 256 * 256 * 256)
+            val body_size = (rx_body[dataPosition].toUByte()).toInt() + ((rx_body[dataPosition + 1].toUByte()).toInt() * 256) + ((rx_body[dataPosition + 2].toUByte()).toInt() * 256 * 256) + ((rx_body[dataPosition + 3].toUByte()).toInt() * 256 * 256 * 256)
 
-            Log.v(TAG, " XX body_size : ${body_size} [$data_position] ($length)  aa: ${rx_body[data_position].toUByte().toInt()}  ${rx_body[data_position + 1].toUByte().toInt()} + ${rx_body[data_position + 2].toUByte().toInt()}")
+            Log.v(TAG, " XX body_size : ${body_size} [$dataPosition] ($length)  aa: ${rx_body[dataPosition].toUByte().toInt()}  ${rx_body[dataPosition + 1].toUByte().toInt()} + ${rx_body[dataPosition + 2].toUByte().toInt()}")
             SimpleLogDumper.dump_bytes("XX", Arrays.copyOfRange(rx_body, 0, 32))
             if (body_size <= 12)
             {
-                Log.v(TAG, " ----- BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.size + " ")
+                Log.v(TAG, " ----- BODY SIZE IS SMALL : " + dataPosition + " (" + body_size + ") [" + receivedRemainBytes + "] " + rx_body.size + " ")
                 break
             }
 
             // 受信データ(のヘッダ部分)をダンプする
-            Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
-            if (data_position + body_size > length)
+            Log.v(TAG, " RX DATA : " + dataPosition + " (" + body_size + ") [" + receivedRemainBytes + "] (" + receivedTotalBytes + ")");
+            if (dataPosition + body_size > length)
             {
                 // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
-                val copysize = length - (data_position + 12)
-                byteStream.write(rx_body, data_position + 12, copysize)
-                received_remain_bytes = body_size - copysize - 12 // マイナス12は、ヘッダ分
-                received_total_bytes = received_total_bytes + copysize
+                val copysize = length - (dataPosition + 12)
+                byteStream.write(rx_body, dataPosition + 12, copysize)
+                receivedRemainBytes = body_size - copysize - 12 // マイナス12は、ヘッダ分
+                receivedTotalBytes += copysize
                 //Log.v(TAG, " ----- copy : " + (data_position + (12)) + " " + copysize + " remain : " + received_remain_bytes + "  body size : " + body_size);
                 break
             }
             try
             {
-                byteStream.write(rx_body, data_position + 12, body_size - 12)
-                data_position = data_position + body_size
-                received_total_bytes = received_total_bytes + 12
+                byteStream.write(rx_body, dataPosition + 12, body_size - 12)
+                dataPosition += body_size
+                receivedTotalBytes += 12
                 //Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - (12)) + " remain : " + received_remain_bytes);
             }
             catch (e: Exception)
             {
-                Log.v(TAG, "  pos : $data_position  size : $body_size length : $length")
+                Log.v(TAG, "  pos : $dataPosition  size : $body_size length : $length")
                 e.printStackTrace()
             }
         }
index 3616bff..6988b1f 100644 (file)
@@ -338,24 +338,24 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
         return (if (callback != null && callback.isReceiveMulti)
         {
             // 受信したら逐次「受信したよ」と応答するパターン
-            receive_multi(command, delayMs)
+            receiveMulti(command, delayMs)
         }
         else
         {
-            receive_single(command, delayMs)
+            receiveSingle(command, delayMs)
         })
         //  受信した後、すべてをまとめて「受信したよ」と応答するパターン
     }
 
-    private fun receive_single(command: IPtpIpCommand, delayMs: Int): Boolean
+    private fun receiveSingle(command: IPtpIpCommand, delayMs: Int): Boolean
     {
         val isDumpReceiveLog = command.dumpLog()
         val id = command.id
         val callback = command.responseCallback()
         try
         {
-            val receive_message_buffer_size = BUFFER_SIZE
-            val byte_array = ByteArray(receive_message_buffer_size)
+            val receiveMessageBufferSize = BUFFER_SIZE
+            val byteArray = ByteArray(receiveMessageBufferSize)
             val inputStream = socket?.getInputStream()
             if (inputStream == null)
             {
@@ -365,8 +365,8 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
             }
 
             // 初回データが受信バッファにデータが溜まるまで待つ...
-            var read_bytes = waitForReceive(inputStream, delayMs, command.maxRetryCount())
-            if (read_bytes < 0)
+            var readBytes = waitForReceive(inputStream, delayMs, command.maxRetryCount())
+            if (readBytes < 0)
             {
                 // リトライオーバー...
                 Log.v(TAG, " RECEIVE : RETRY OVER...")
@@ -380,17 +380,17 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
 
             // 受信したデータをバッファに突っ込む
             val byteStream = ByteArrayOutputStream()
-            while (read_bytes > 0)
+            while (readBytes > 0)
             {
-                read_bytes = inputStream.read(byte_array, 0, receive_message_buffer_size)
-                if (read_bytes <= 0)
+                readBytes = inputStream.read(byteArray, 0, receiveMessageBufferSize)
+                if (readBytes <= 0)
                 {
-                    Log.v(TAG, " RECEIVED MESSAGE FINISHED ($read_bytes)")
+                    Log.v(TAG, " RECEIVED MESSAGE FINISHED ($readBytes)")
                     break
                 }
-                byteStream.write(byte_array, 0, read_bytes)
+                byteStream.write(byteArray, 0, readBytes)
                 sleep(delayMs)
-                read_bytes = inputStream.available()
+                readBytes = inputStream.available()
             }
             val outputStream = cutHeader(byteStream)
             receivedAllMessage(isDumpReceiveLog, id, outputStream.toByteArray(), callback)
@@ -415,7 +415,7 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
         callback?.receivedMessage(id, body)
     }
 
-    private fun receive_multi(command: IPtpIpCommand, delayMs: Int): Boolean
+    private fun receiveMulti(command: IPtpIpCommand, delayMs: Int): Boolean
     {
         //int estimatedSize = command.estimatedReceiveDataSize();
         var maxRetryCount = command.maxRetryCount()
@@ -459,16 +459,18 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
                     // 受信サイズ異常の場合...
                     if (receivedLength > 0)
                     {
-                        SimpleLogDumper.dump_bytes("WRONG DATA : ", Arrays.copyOfRange(byteArray, 0, Math.min(receivedLength, 64)))
+                        SimpleLogDumper.dump_bytes("WRONG DATA : ", byteArray.copyOfRange(0, Math.min(receivedLength, 64)))
                     }
                     Log.v(TAG, " WRONG LENGTH. : $targetLength READ : $receivedLength bytes.")
                     callback?.receivedMessage(id, null)
-                    return false
+                    //return (false)
+                    // データが不足しているので、もう一度受信待ち
+                    return (true)
                 }
             }
 
             Log.v(TAG, "  -=-=-=- 1st CALL : read_bytes : " + readBytes + "(" + receivedLength + ") : target_length : " + targetLength + "  buffer SIZE : " + byteArray.size)
-            callback?.onReceiveProgress(receivedLength, targetLength, Arrays.copyOfRange(byteArray, 0, receivedLength))
+            callback?.onReceiveProgress(receivedLength, targetLength, byteArray.copyOfRange(fromIndex = 0, toIndex = receivedLength))
 
             run {
                 sleep(delayMs)
@@ -488,7 +490,7 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
                 }
                 receivedLength += readBytes
 
-                callback?.onReceiveProgress(receivedLength, targetLength, Arrays.copyOfRange(byteArray, 0, readBytes))
+                callback?.onReceiveProgress(receivedLength, targetLength, byteArray.copyOfRange(0, readBytes))
                 maxRetryCount = command.maxRetryCount()
 
                 run {
@@ -510,7 +512,7 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
         {
             e.printStackTrace()
         }
-        return false
+        return (false)
     }
 
     private fun parseDataLength(byte_array: ByteArray, read_bytes: Int): Int
@@ -523,11 +525,12 @@ class PtpIpCommandPublisher(private val ipAddress : String, private val portNumb
             {
                 if (byte_array[offset + 4].toUByte().toInt() == 0x07)
                 {
-                    // 前の応答が入っていると考える...
-                    offset = 14
+                    // 前の応答が入っていると考える... レングスバイト分読み飛ばす
+                    offset = byte_array[offset].toUByte().toInt()
                 }
                 if (byte_array[offset + 4].toUByte().toInt() == 0x09)
                 {
+                    // データバイト...
                     lenlen = (byte_array[offset + 15].toUByte().toInt() and 0xff shl 24) + (byte_array[offset + 14].toUByte().toInt() and 0xff shl 16) + (byte_array[offset + 13].toUByte().toInt() and 0xff shl 8) + (byte_array[offset + 12].toUByte().toInt() and 0xff)
                 }
             }