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
6 import net.osdn.gokigen.pkremote.R
7 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraConnection
8 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusReceiver
9 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.IPtpIpInterfaceProvider
10 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback
11 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpMessages
12 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.PtpIpCommandGeneric
13 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.status.PtpIpStatusChecker
15 class CanonCameraConnectSequenceForPlaybackType1(val context: Activity, val cameraStatusReceiver: ICameraStatusReceiver, val cameraConnection: ICameraConnection, val interfaceProvider: IPtpIpInterfaceProvider, val statusChecker: PtpIpStatusChecker) : Runnable, IPtpIpCommandCallback, IPtpIpMessages
17 private val isDumpLog = true
18 private val commandIssuer = interfaceProvider.commandPublisher
19 //private var requestMessageCount = 0
25 Log.v(TAG, " CanonCameraConnectSequenceForPlaybackType1 START!")
28 val issuer = interfaceProvider.commandPublisher
29 if (!issuer.isConnected)
31 if (!interfaceProvider.commandCommunication.connect())
34 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED)
35 cameraConnection.alertConnectingFailed(context.getString(R.string.dialog_title_connect_failed_canon))
41 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...")
47 sendRegistrationMessage()
52 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED)
53 cameraConnection.alertConnectingFailed(e.message)
57 override fun onReceiveProgress(currentBytes: Int, totalBytes: Int, body: ByteArray?)
59 Log.v(TAG, " $currentBytes/$totalBytes")
62 override fun isReceiveMulti(): Boolean
67 @ExperimentalUnsignedTypes
68 override fun receivedMessage(id: Int, rx_body: ByteArray)
72 IPtpIpMessages.SEQ_REGISTRATION -> if (checkRegistrationMessage(rx_body)) {
73 sendInitEventRequest(rx_body)
75 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_error_message))
77 IPtpIpMessages.SEQ_EVENT_INITIALIZE -> if (checkEventInitialize(rx_body)) {
78 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0)
79 commandIssuer.enqueueCommand(PtpIpCommandGeneric(this, IPtpIpMessages.SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41))
81 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_error_message))
83 IPtpIpMessages.SEQ_OPEN_SESSION -> {
84 Log.v(TAG, " SEQ_OPEN_SESSION ")
85 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0)
86 commandIssuer.enqueueCommand(PtpIpCommandGeneric(this, IPtpIpMessages.SEQ_INIT_SESSION, isDumpLog, 0, 0x902f))
88 IPtpIpMessages.SEQ_INIT_SESSION -> {
89 Log.v(TAG, " SEQ_INIT_SESSION ")
90 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0)
91 commandIssuer.enqueueCommand(PtpIpCommandGeneric(this, IPtpIpMessages.SEQ_CHANGE_REMOTE, isDumpLog, 0, 0x9114, 4, 0x15))
93 IPtpIpMessages.SEQ_CHANGE_REMOTE -> {
94 Log.v(TAG, " SEQ_CHANGE_REMOTE ")
95 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0)
96 commandIssuer.enqueueCommand(PtpIpCommandGeneric(this, IPtpIpMessages.SEQ_SET_EVENT_MODE, isDumpLog, 0, 0x9115, 4, 0x02))
98 IPtpIpMessages.SEQ_SET_EVENT_MODE -> {
99 Log.v(TAG, " SEQ_SET_EVENT_MODE ")
100 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0)
101 Log.v(TAG, " SEQ_DEVICE_PROPERTY_FINISHED ")
102 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0)
104 Log.v(TAG, "CHANGED MODE : DONE.")
107 Log.v(TAG, "RECEIVED UNKNOWN ID : $id")
108 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_receive_unknown_message))
113 private fun sendRegistrationMessage()
115 Log.v(TAG, " sendRegistrationMessage() ")
117 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_start), false, false, 0)
118 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start))
119 //commandIssuer.enqueueCommand(CanonRegistrationMessage(this))
122 @ExperimentalUnsignedTypes
123 private fun sendInitEventRequest(receiveData: ByteArray)
125 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_start_2), false, false, 0)
126 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2))
129 var eventConnectionNumber: Int = receiveData[8].toUByte().toInt() and 0xff
130 eventConnectionNumber += (receiveData[9].toUByte().toInt() and 0xff shl 8)
131 eventConnectionNumber += (receiveData[10].toUByte().toInt() and 0xff shl 16)
132 eventConnectionNumber += (receiveData[11].toUByte().toInt() and 0xff shl 24)
133 statusChecker.setEventConnectionNumber(eventConnectionNumber)
134 interfaceProvider.cameraStatusWatcher.startStatusWatch(interfaceProvider.statusListener)
135 commandIssuer.enqueueCommand(PtpIpCommandGeneric(this, IPtpIpMessages.SEQ_OPEN_SESSION, isDumpLog, 0, 0x1002, 4, 0x41))
143 private fun checkRegistrationMessage(receiveData: ByteArray?): Boolean
145 // データ(Connection Number)がないときにはエラーと判断する
146 return !(receiveData == null || receiveData.size < 12)
149 private fun checkEventInitialize(receiveData: ByteArray?): Boolean
151 Log.v(TAG, "checkEventInitialize() ")
152 return receiveData != null
155 private fun connectFinished()
160 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_connected), false, false, 0)
166 interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_connected), false, false, 0)
175 private fun onConnectNotify()
179 val thread = Thread {
181 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected))
182 cameraStatusReceiver.onCameraConnected()
183 Log.v(TAG, " onConnectNotify()")
195 private const val TAG = "CanonConnectSeq.1"