OSDN Git Service

安定性調査中。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / liveview / NikonLiveViewControl.java
index f707877..cfb3eda 100644 (file)
@@ -14,6 +14,7 @@ import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommunication;
 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.PtpIpResponseReceiver;
 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.liveview.IPtpIpLiveViewImageCallback;
+import net.osdn.gokigen.a01d.camera.utils.SimpleLogDumper;
 import net.osdn.gokigen.a01d.liveview.liveviewlistener.IImageDataReceiver;
 import net.osdn.gokigen.a01d.liveview.liveviewlistener.ILiveViewListener;
 
@@ -21,7 +22,10 @@ import java.util.Arrays;
 import java.util.Map;
 
 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_AFDRIVE;
+import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_CHECK_EVENT;
 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_DEVICE_READY;
+import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_DEVICE_PROP1;
+import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_GET_DEVICE_PROP2;
 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_START_LIVEVIEW;
 import static net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages.SEQ_STOP_LIVEVIEW;
 
@@ -185,7 +189,7 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
                 //SimpleLogDumper.dump_bytes(" [LVLV] " + ": ", Arrays.copyOfRange(data, 0, (0 + 512)));
                 //dataReceiver.setImageData(data, metadata);
                 int offset = searchJpegHeader(data);
-                if (data.length > 8)
+                if ((data.length > 8)&&(offset < data.length))
                 {
                     dataReceiver.setImageData(Arrays.copyOfRange(data, offset, data.length), metadata);  // ヘッダ部分を切り取って送る
                 }
@@ -195,6 +199,8 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
         {
             e.printStackTrace();
         }
+        //commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_DEVICE_PROP1, 30, isDumpLog, 0, 0x1015, 4, 0x5007, 0x00, 0x00, 0x00));
+        commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHECK_EVENT, 30, isDumpLog, 0, 0x90c7, 0, 0x00, 0x00, 0x00, 0x00));
         commandIssued = false;
     }
 
@@ -234,15 +240,25 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
     @Override
     public void receivedMessage(int id, byte[] rx_body)
     {
-        Log.v(TAG, " NikonLiveViewControl::receivedMessage() ");
+        Log.v(TAG, " NikonLiveViewControl::receivedMessage() [id:" + id + "]");
         try
         {
             if (rx_body.length < 10)
             {
+                Log.v(TAG, " NikonLiveViewControl::receivedMessage() : BODY LENGTH IS TOO SHORT. SEND RETRY MESSAGE");
                 retrySendMessage(id);
                 return;
             }
+/**/
             int responseCode = (rx_body[8] & 0xff) + ((rx_body[9] & 0xff) * 256);
+            if (id == SEQ_CHECK_EVENT)
+            {
+                // 応答にはデータが含まれているので....受信データの末尾を拾う
+                //SimpleLogDumper.dump_bytes("CHECK EVENT", Arrays.copyOfRange(rx_body, 0, Math.min(rx_body.length, 128)));
+                responseCode = (rx_body[rx_body.length - 6] & 0xff) + ((rx_body[rx_body.length - 5] & 0xff) * 256);
+            }
+/**/
+            //int responseCode = (rx_body[rx_body.length - 6] & 0xff) + ((rx_body[rx_body.length - 5] & 0xff) * 256);
             if (responseCode != 0x2001)
             {
                 // NG応答を受信...同じコマンドを再送する
@@ -251,7 +267,7 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
                 return;
             }
 
-            Log.v(TAG, String.format(" OK REPLY (ID : %d) ", id));
+            Log.v(TAG, String.format(" ----- OK REPLY (ID : %d) ----- ", id));
             if (id == SEQ_START_LIVEVIEW)
             {
                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 30, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
@@ -260,10 +276,23 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
             {
                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 30, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));
             }
-            else
+            else if (id == SEQ_CHECK_EVENT)
+            {
+                commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 35, isDumpLog));
+            }
+            else if (id == SEQ_GET_DEVICE_PROP1)
+            {
+                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_DEVICE_PROP2, 30, isDumpLog, 0, 0x1015, 4, 0xd100, 0x00, 0x00, 0x00));
+            }
+            else if (id == SEQ_GET_DEVICE_PROP2)
+            {
+                commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 35, isDumpLog));
+            }
+            else  // SEQ_AFDRIVE
             {
                 // ライブビューの開始。
-                startLiveviewImpl();
+                //startLiveviewImpl();
+                commandIssuer.enqueueCommand(new NikonLiveViewRequestMessage(imageReceiver, 80, isDumpLog));
             }
         }
         catch (Exception e)
@@ -272,10 +301,23 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
         }
     }
 
+    private void waitSleep()
+    {
+        try
+        {
+            Thread.sleep(delayMs);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
     private void retrySendMessage(int id)
     {
         try
         {
+            waitSleep();
             if (id == SEQ_START_LIVEVIEW)
             {
                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_START_LIVEVIEW, 30, isDumpLog, 0, 0x9201, 0, 0x00, 0x00, 0x00, 0x00));
@@ -284,6 +326,18 @@ public class NikonLiveViewControl  implements ILiveViewControl, ILiveViewListene
             {
                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_READY, 30, isDumpLog, 0, 0x90c8, 0, 0x00, 0x00, 0x00, 0x00));
             }
+            else if (id == SEQ_CHECK_EVENT)
+            {
+                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHECK_EVENT, 30, isDumpLog, 0, 0x90c7, 0, 0x00, 0x00, 0x00, 0x00));
+            }
+            else if (id == SEQ_GET_DEVICE_PROP1)
+            {
+                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_DEVICE_PROP1, 30, isDumpLog, 0, 0x5007, 4, 0x5007, 0x00, 0x00, 0x00));
+            }
+            else if (id == SEQ_GET_DEVICE_PROP2)
+            {
+                commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_DEVICE_PROP2, 30, isDumpLog, 0, 0x1015, 4, 0xd100, 0x00, 0x00, 0x00));
+            }
             else
             {
                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_AFDRIVE, 30, isDumpLog, 0, 0x90c1, 0, 0x00, 0x00, 0x00, 0x00));