1 package net.osdn.gokigen.a01d.camera.canon.wrapper.connection;
4 import android.app.Activity;
5 import android.graphics.Color;
6 import android.util.Log;
8 import androidx.annotation.NonNull;
10 import net.osdn.gokigen.a01d.R;
11 import net.osdn.gokigen.a01d.camera.ICameraConnection;
12 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
13 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
14 import net.osdn.gokigen.a01d.camera.canon.wrapper.command.messages.specific.CanonRegistrationMessage;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
17 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages;
18 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
19 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.status.PtpIpStatusChecker;
21 public class CanonCameraConnectSequence implements Runnable, IPtpIpCommandCallback, IPtpIpMessages
23 private final String TAG = this.toString();
25 private final Activity context;
26 private final ICameraConnection cameraConnection;
27 private final ICameraStatusReceiver cameraStatusReceiver;
28 private final IPtpIpInterfaceProvider interfaceProvider;
29 private final IPtpIpCommandPublisher commandIssuer;
30 private final PtpIpStatusChecker statusChecker;
31 private boolean isDumpLog = false;
33 CanonCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull PtpIpStatusChecker statusChecker)
35 Log.v(TAG, " CanonCameraConnectSequenceForPlayback");
36 this.context = context;
37 this.cameraConnection = cameraConnection;
38 this.cameraStatusReceiver = statusReceiver;
39 this.interfaceProvider = interfaceProvider;
40 this.commandIssuer = interfaceProvider.getCommandPublisher();
41 this.statusChecker = statusChecker;
50 IPtpIpCommandPublisher issuer = interfaceProvider.getCommandPublisher();
51 if (!issuer.isConnected())
53 if (!interfaceProvider.getCommandCommunication().connect())
56 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED);
57 onConnectError(context.getString(R.string.dialog_title_connect_failed_canon));
63 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
69 sendRegistrationMessage();
75 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED);
76 onConnectError(e.getLocalizedMessage());
80 private void onConnectError(String reason)
82 cameraConnection.alertConnectingFailed(reason);
86 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
88 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
92 public boolean isReceiveMulti()
98 public void receivedMessage(int id, byte[] rx_body)
102 case SEQ_REGISTRATION:
103 if (checkRegistrationMessage(rx_body))
105 sendInitEventRequest(rx_body);
109 onConnectError(context.getString(R.string.connect_error_message));
113 case SEQ_EVENT_INITIALIZE:
114 if (checkEventInitialize(rx_body))
116 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
117 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
121 onConnectError(context.getString(R.string.connect_error_message));
125 case SEQ_OPEN_SESSION:
126 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
127 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, isDumpLog, 0, 0x902f));
130 case SEQ_INIT_SESSION:
131 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
132 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHANGE_REMOTE, isDumpLog, 0, 0x9114, 4, 0x15));
135 case SEQ_CHANGE_REMOTE:
136 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
137 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_SET_EVENT_MODE, isDumpLog, 0, 0x9115, 4, 0x02));
140 case SEQ_SET_EVENT_MODE:
141 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
142 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_EVENT, isDumpLog, 0, 0x902f, 4, 0x02));
146 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
148 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
152 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
153 onConnectError(context.getString(R.string.connect_receive_unknown_message));
158 private void sendRegistrationMessage()
160 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
161 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
162 commandIssuer.enqueueCommand(new CanonRegistrationMessage(this));
165 private void sendInitEventRequest(byte[] receiveData)
167 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
168 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
171 int eventConnectionNumber = (receiveData[8] & 0xff);
172 eventConnectionNumber = eventConnectionNumber + ((receiveData[9] & 0xff) << 8);
173 eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
174 eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
175 statusChecker.setEventConnectionNumber(eventConnectionNumber);
176 interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());
178 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
186 private boolean checkRegistrationMessage(byte[] receiveData)
188 // データ(Connection Number)がないときにはエラーと判断する
189 return (!((receiveData == null)||(receiveData.length < 12)));
192 private boolean checkEventInitialize(byte[] receiveData)
194 Log.v(TAG, "checkEventInitialize() ");
195 return (!(receiveData == null));
198 private void connectFinished()
203 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
208 //interfaceProvider.getAsyncEventCommunication().connect();
209 //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener()); ステータスの定期確認は実施しない
212 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
222 private void onConnectNotify()
226 final Thread thread = new Thread(new Runnable()
232 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
233 cameraStatusReceiver.onCameraConnected();
234 Log.v(TAG, " onConnectNotify()");