1 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.connection;
3 import android.app.Activity;
4 import android.graphics.Color;
5 import android.util.Log;
7 import androidx.annotation.NonNull;
9 import net.osdn.gokigen.pkremote.R;
10 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraConnection;
11 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusReceiver;
12 import net.osdn.gokigen.pkremote.camera.vendor.nikon.INikonInterfaceProvider;
13 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback;
14 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandPublisher;
15 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpMessages;
16 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
17 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.specific.CanonRegistrationMessage;
18 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.status.PtpIpStatusChecker;
20 public class NikonCameraConnectSequenceForPlayback implements Runnable, IPtpIpCommandCallback, IPtpIpMessages
22 private final String TAG = this.toString();
24 private final Activity context;
25 private final ICameraConnection cameraConnection;
26 private final ICameraStatusReceiver cameraStatusReceiver;
27 private final INikonInterfaceProvider interfaceProvider;
28 private final IPtpIpCommandPublisher commandIssuer;
29 private final PtpIpStatusChecker statusChecker;
30 private boolean isDumpLog = false;
32 NikonCameraConnectSequenceForPlayback(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull INikonInterfaceProvider interfaceProvider, @NonNull PtpIpStatusChecker statusChecker)
34 Log.v(TAG, " NikonCameraConnectSequenceForPlayback");
35 this.context = context;
36 this.cameraConnection = cameraConnection;
37 this.cameraStatusReceiver = statusReceiver;
38 this.interfaceProvider = interfaceProvider;
39 this.commandIssuer = interfaceProvider.getCommandPublisher();
40 this.statusChecker = statusChecker;
49 IPtpIpCommandPublisher issuer = interfaceProvider.getCommandPublisher();
50 if (!issuer.isConnected())
52 if (!interfaceProvider.getCommandCommunication().connect())
55 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
56 onConnectError(context.getString(R.string.dialog_title_connect_failed_nikon));
62 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
68 sendRegistrationMessage();
74 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
75 onConnectError(e.getLocalizedMessage());
79 private void onConnectError(String reason)
81 cameraConnection.alertConnectingFailed(reason);
85 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
87 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
91 public boolean isReceiveMulti()
97 public void receivedMessage(int id, byte[] rx_body)
101 case SEQ_REGISTRATION:
102 if (checkRegistrationMessage(rx_body))
104 sendInitEventRequest(rx_body);
108 onConnectError(context.getString(R.string.connect_error_message));
112 case SEQ_EVENT_INITIALIZE:
113 if (checkEventInitialize(rx_body))
115 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
116 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
120 onConnectError(context.getString(R.string.connect_error_message));
124 case SEQ_OPEN_SESSION:
125 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
126 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, isDumpLog, 0, 0x902f));
129 case SEQ_INIT_SESSION:
130 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
131 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHANGE_REMOTE, isDumpLog, 0, 0x9114, 4, 0x15));
134 case SEQ_CHANGE_REMOTE:
135 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
136 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_SET_EVENT_MODE, isDumpLog, 0, 0x902f, 4, 0x02));
139 case SEQ_SET_EVENT_MODE:
140 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
141 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
143 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
147 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
148 onConnectError(context.getString(R.string.connect_receive_unknown_message));
153 private void sendRegistrationMessage()
155 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
156 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
157 commandIssuer.enqueueCommand(new CanonRegistrationMessage(this));
160 private void sendInitEventRequest(byte[] receiveData)
162 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
163 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
166 int eventConnectionNumber = (receiveData[8] & 0xff);
167 eventConnectionNumber = eventConnectionNumber + ((receiveData[9] & 0xff) << 8);
168 eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
169 eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
170 statusChecker.setEventConnectionNumber(eventConnectionNumber);
171 interfaceProvider.getCameraStatusWatcher().startStatusWatch(null);
173 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
181 private boolean checkRegistrationMessage(byte[] receiveData)
183 // データ(Connection Number)がないときにはエラーと判断する
184 return (!((receiveData == null)||(receiveData.length < 12)));
187 private boolean checkEventInitialize(byte[] receiveData)
189 Log.v(TAG, "checkEventInitialize() ");
190 return (!(receiveData == null));
193 private void connectFinished()
198 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
203 //interfaceProvider.getAsyncEventCommunication().connect();
204 //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener()); ステータスの定期確認は実施しない
207 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
217 private void onConnectNotify()
221 final Thread thread = new Thread(new Runnable()
227 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
228 cameraStatusReceiver.onCameraConnected();
229 Log.v(TAG, " onConnectNotify()");