OSDN Git Service

72ea1d8959a465212f34704cc8f4f3ef5d8d1fee
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / ptpip / wrapper / connection / CanonCameraConnectSequenceForPlaybackType1.kt
1 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.connection
2
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
14
15 class CanonCameraConnectSequenceForPlaybackType1(val context: Activity, val cameraStatusReceiver: ICameraStatusReceiver, val cameraConnection: ICameraConnection, val interfaceProvider: IPtpIpInterfaceProvider, val statusChecker: PtpIpStatusChecker) : Runnable, IPtpIpCommandCallback, IPtpIpMessages
16 {
17     private val isDumpLog = true
18     private val commandIssuer = interfaceProvider.commandPublisher
19     //private var requestMessageCount = 0
20
21     override fun run()
22     {
23         try
24         {
25             Log.v(TAG, " CanonCameraConnectSequenceForPlaybackType1 START!")
26
27             // カメラとTCP接続
28             val issuer = interfaceProvider.commandPublisher
29             if (!issuer.isConnected)
30             {
31                 if (!interfaceProvider.commandCommunication.connect())
32                 {
33                     // 接続失敗...
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))
36                     return
37                 }
38             }
39             else
40             {
41                 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...")
42             }
43             // コマンドタスクの実行開始
44             issuer.start()
45
46             // 接続シーケンスの開始
47             sendRegistrationMessage()
48         }
49         catch (e: Exception)
50         {
51             e.printStackTrace()
52             interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.dialog_title_connect_failed_canon), false, true, Color.RED)
53             cameraConnection.alertConnectingFailed(e.message)
54         }
55     }
56
57     override fun onReceiveProgress(currentBytes: Int, totalBytes: Int, body: ByteArray?)
58     {
59         Log.v(TAG, " $currentBytes/$totalBytes")
60     }
61
62     override fun isReceiveMulti(): Boolean
63     {
64         return false
65     }
66
67     @ExperimentalUnsignedTypes
68     override fun receivedMessage(id: Int, rx_body: ByteArray)
69     {
70         when (id)
71         {
72             IPtpIpMessages.SEQ_REGISTRATION -> if (checkRegistrationMessage(rx_body)) {
73                 sendInitEventRequest(rx_body)
74             } else {
75                 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_error_message))
76             }
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))
80             } else {
81                 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_error_message))
82             }
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))
87             }
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))
92             }
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))
97             }
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)
103                 connectFinished()
104                 Log.v(TAG, "CHANGED MODE : DONE.")
105             }
106             else -> {
107                 Log.v(TAG, "RECEIVED UNKNOWN ID : $id")
108                 cameraConnection.alertConnectingFailed(context.getString(R.string.connect_receive_unknown_message))
109             }
110         }
111     }
112
113     private fun sendRegistrationMessage()
114     {
115         Log.v(TAG, " sendRegistrationMessage() ")
116
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))
120     }
121
122     @ExperimentalUnsignedTypes
123     private fun sendInitEventRequest(receiveData: ByteArray)
124     {
125         interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_start_2), false, false, 0)
126         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2))
127         try
128         {
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))
136         }
137         catch (e: Exception)
138         {
139             e.printStackTrace()
140         }
141     }
142
143     private fun checkRegistrationMessage(receiveData: ByteArray?): Boolean
144     {
145         // データ(Connection Number)がないときにはエラーと判断する
146         return !(receiveData == null || receiveData.size < 12)
147     }
148
149     private fun checkEventInitialize(receiveData: ByteArray?): Boolean
150     {
151         Log.v(TAG, "checkEventInitialize() ")
152         return receiveData != null
153     }
154
155     private fun connectFinished()
156     {
157         try
158         {
159             // 接続成功のメッセージを出す
160             interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_connected), false, false, 0)
161
162             // ちょっと待つ
163             Thread.sleep(1000)
164
165             // 接続成功!のメッセージを出す
166             interfaceProvider.informationReceiver.updateMessage(context.getString(R.string.connect_connected), false, false, 0)
167             onConnectNotify()
168         }
169         catch (e: Exception)
170         {
171             e.printStackTrace()
172         }
173     }
174
175     private fun onConnectNotify()
176     {
177         try
178         {
179             val thread = Thread {
180                 // カメラとの接続確立を通知する
181                 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected))
182                 cameraStatusReceiver.onCameraConnected()
183                 Log.v(TAG, " onConnectNotify()")
184             }
185             thread.start()
186         }
187         catch (e: Exception)
188         {
189             e.printStackTrace()
190         }
191     }
192
193     companion object
194     {
195         private const val TAG = "CanonConnectSeq.1"
196     }
197 }