1 package net.osdn.gokigen.a01d.camera.canon.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.a01d.R;
10 import net.osdn.gokigen.a01d.camera.ICameraConnection;
11 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
12 import net.osdn.gokigen.a01d.camera.canon.wrapper.command.messages.specific.CanonSetDevicePropertyValue;
13 import net.osdn.gokigen.a01d.camera.canon.wrapper.status.CanonStatusChecker;
14 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
15 import net.osdn.gokigen.a01d.camera.canon.wrapper.command.messages.specific.CanonRegistrationMessage;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
17 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
18 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages;
19 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
25 public class CanonCameraConnectSequence implements Runnable, IPtpIpCommandCallback, IPtpIpMessages
27 private final String TAG = this.toString();
29 private final Activity context;
30 private final ICameraConnection cameraConnection;
31 private final ICameraStatusReceiver cameraStatusReceiver;
32 private final IPtpIpInterfaceProvider interfaceProvider;
33 private final IPtpIpCommandPublisher commandIssuer;
34 private final CanonStatusChecker statusChecker;
35 private final boolean isDumpLog = false;
37 CanonCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull CanonStatusChecker statusChecker)
39 Log.v(TAG, " CanonCameraConnectSequence");
40 this.context = context;
41 this.cameraConnection = cameraConnection;
42 this.cameraStatusReceiver = statusReceiver;
43 this.interfaceProvider = interfaceProvider;
44 this.commandIssuer = interfaceProvider.getCommandPublisher();
45 this.statusChecker = statusChecker;
54 IPtpIpCommandPublisher issuer = interfaceProvider.getCommandPublisher();
55 if (!issuer.isConnected())
57 if (!interfaceProvider.getCommandCommunication().connect())
60 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED);
61 onConnectError(context.getString(R.string.dialog_title_connect_failed_canon));
67 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
73 sendRegistrationMessage();
79 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED);
80 onConnectError(e.getLocalizedMessage());
84 private void onConnectError(String reason)
86 cameraConnection.alertConnectingFailed(reason);
90 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
92 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
96 public boolean isReceiveMulti()
102 public void receivedMessage(int id, byte[] rx_body)
106 case SEQ_REGISTRATION:
107 if (checkRegistrationMessage(rx_body))
109 sendInitEventRequest(rx_body);
113 onConnectError(context.getString(R.string.connect_error_message));
117 case SEQ_EVENT_INITIALIZE:
118 if (checkEventInitialize(rx_body))
120 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
121 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
125 onConnectError(context.getString(R.string.connect_error_message));
129 case SEQ_OPEN_SESSION:
130 Log.v(TAG, " SEQ_OPEN_SESSION ");
131 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
132 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, isDumpLog, 0, 0x902f));
135 case SEQ_INIT_SESSION:
136 Log.v(TAG, " SEQ_INIT_SESSION ");
137 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
138 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHANGE_REMOTE, isDumpLog, 0, 0x9114, 4, 0x15));
141 case SEQ_CHANGE_REMOTE:
142 Log.v(TAG, " SEQ_CHANGE_REMOTE ");
143 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
144 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_SET_EVENT_MODE, isDumpLog, 0, 0x9115, 4, 0x02));
147 case SEQ_SET_EVENT_MODE:
148 Log.v(TAG, " SEQ_SET_EVENT_MODE ");
149 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
150 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_EVENT, isDumpLog, 0, 0x913d, 4, 0x0fff));
154 Log.v(TAG, " SEQ_GET_EVENT ");
155 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting6), false, false, 0);
156 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_EVENT1, isDumpLog, 0, 0x9033, 4, 0x00000000));
157 //commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_GET_EVENT1, isDumpLog, 0, 0x9033, 4, 0x00200000));
161 Log.v(TAG, " SEQ_GET_EVENT1 ");
162 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting7), false, false, 0);
163 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_INFORMATION, isDumpLog, 0, 0x1001));
166 case SEQ_DEVICE_INFORMATION:
167 Log.v(TAG, " SEQ_DEVICE_INFORMATION ");
168 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting8), false, false, 0);
169 //commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d1a6));
170 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d1a6));
171 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d169));
172 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d16a));
173 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d16b));
174 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_DEVICE_PROPERTY, isDumpLog, 0, 0x9127, 4, 0x0000d1af));
177 case SEQ_DEVICE_PROPERTY:
178 Log.v(TAG, " SEQ_DEVICE_PROPERTY ");
179 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting9), false, false, 0);
180 if ((rx_body[8] == (byte) 0x01)&&(rx_body[9] == (byte) 0x20))
182 // コマンドが受け付けられたときだけ次に進む!
189 commandIssuer.enqueueCommand(new CanonSetDevicePropertyValue(this, SEQ_SET_DEVICE_PROPERTY, isDumpLog, 0, 150, 0xd136, 0x00));
198 case SEQ_SET_DEVICE_PROPERTY:
199 Log.v(TAG, " SEQ_SET_DEVICE_PROPERTY ");
200 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting10), false, false, 0);
201 commandIssuer.enqueueCommand(new CanonSetDevicePropertyValue(this, SEQ_SET_DEVICE_PROPERTY_2, isDumpLog, 0, 150, 0xd136, 0x01));
204 case SEQ_SET_DEVICE_PROPERTY_2:
205 Log.v(TAG, " SEQ_SET_DEVICE_PROPERTY_2 ");
206 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting11), false, false, 0);
207 commandIssuer.enqueueCommand(new CanonSetDevicePropertyValue(this, SEQ_SET_DEVICE_PROPERTY_3, isDumpLog, 0, 150, 0xd136, 0x00));
210 case SEQ_SET_DEVICE_PROPERTY_3:
211 Log.v(TAG, " SEQ_SET_DEVICE_PROPERTY_3 ");
212 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting12), false, false, 0);
213 commandIssuer.enqueueCommand(new CanonSetDevicePropertyValue(this, SEQ_DEVICE_PROPERTY_FINISHED, isDumpLog, 0, 300, 0xd1b0, 0x08));
216 case SEQ_DEVICE_PROPERTY_FINISHED:
217 Log.v(TAG, " SEQ_DEVICE_PROPERTY_FINISHED ");
218 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
220 Log.v(TAG, "CHANGED MODE : DONE.");
224 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
225 onConnectError(context.getString(R.string.connect_receive_unknown_message));
230 private void sendRegistrationMessage()
232 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
233 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
234 commandIssuer.enqueueCommand(new CanonRegistrationMessage(this));
237 private void sendInitEventRequest(byte[] receiveData)
239 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
240 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
243 int eventConnectionNumber = (receiveData[8] & 0xff);
244 eventConnectionNumber = eventConnectionNumber + ((receiveData[9] & 0xff) << 8);
245 eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
246 eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
247 statusChecker.setEventConnectionNumber(eventConnectionNumber);
248 interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());
250 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41));
258 private boolean checkRegistrationMessage(byte[] receiveData)
260 // データ(Connection Number)がないときにはエラーと判断する
261 return (!((receiveData == null)||(receiveData.length < 12)));
264 private boolean checkEventInitialize(byte[] receiveData)
266 Log.v(TAG, "checkEventInitialize() ");
267 return (!(receiveData == null));
270 private void connectFinished()
275 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
281 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
291 private void onConnectNotify()
295 final Thread thread = new Thread(new Runnable()
301 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
302 cameraStatusReceiver.onCameraConnected();
303 Log.v(TAG, " onConnectNotify()");