OSDN Git Service

なんとなく問題点を明らかにする。(通信回り)
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / Connection.java
1 package net.osdn.gokigen.cameratest.fuji;
2
3 import android.app.Activity;
4 import android.graphics.PointF;
5 import android.util.Log;
6
7 import androidx.annotation.NonNull;
8
9 import net.osdn.gokigen.cameratest.fuji.statuses.FujiStatusChecker;
10 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
11 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusRequest;
12
13 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraProperties.FILM_SIMULATION;
14 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraProperties.IMAGE_ASPECT;
15 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.FILM_SIMULATION_MAX;
16 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.FILM_SIMULATION_MIN;
17 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.IMAGE_ASPECT_MAX;
18 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.IMAGE_ASPECT_MIN;
19
20 public class Connection implements IFujiStatusRequest
21 {
22     private final String TAG = toString();
23     private final MessageSequence sequence;
24     private final Communication comm;
25     private final FujiStatusChecker statusChecker;
26     private boolean pauseRequestStatus = false;
27
28     public Connection(@NonNull Activity activity, @NonNull ILiveViewImage imageViewer, @NonNull IFujiStatusNotify notify)
29     {
30         this.comm = new Communication(activity, imageViewer);
31         this.sequence = new MessageSequence();
32         this.statusChecker = new FujiStatusChecker(this, notify);
33     }
34
35     public boolean start_connect(boolean isBothLiveView)
36     {
37         boolean ret = false;
38
39         if (connect_to_camera(isBothLiveView))
40         {
41             ret = requestStatus();
42             if (ret)
43             {
44                 // 定期監視の開始
45                 statusChecker.start();
46             }
47         }
48         return (ret);
49     }
50
51     private boolean connect_to_camera(boolean isBothLiveView)
52     {
53         try
54         {
55             ReceivedDataHolder rx_bytes;
56             comm.connect_socket();
57
58             comm.send_to_camera(sequence.registration_message(), false, false);
59             rx_bytes = comm.receive_from_camera();
60             dump_bytes("Connect", 0, rx_bytes);
61
62             // 応答エラーかどうかをチェックする
63             if (rx_bytes.getData().length == 8)
64             {
65                 byte[] receiveData = rx_bytes.getData();
66                 if ((receiveData[0] == 0x05)&&(receiveData[1] == 0x00)&&(receiveData[2] == 0x00)&&(receiveData[3] == 0x00)&&
67                     (receiveData[4] == 0x19)&&(receiveData[5] == 0x20)&&(receiveData[6] == 0x00)&&(receiveData[7] == 0x00))
68                 {
69                     // 応答エラー...
70                     return (false);
71                 }
72                 return (false);
73             }
74
75             // start_messageを送信
76             comm.send_to_camera(sequence.start_message(), true, true);
77             rx_bytes = comm.receive_from_camera();
78             dump_bytes("Connect", 1, rx_bytes);
79
80             //  なんだろう?? (送信が必要なようだが)
81             comm.send_to_camera(sequence.start_message2(), true, true);
82             rx_bytes = comm.receive_from_camera();
83             dump_bytes("Connect", 2, rx_bytes);
84             byte[] rx_data1 = rx_bytes.getData();
85             if (rx_data1.length == (int)rx_data1[0])
86             {
87                 //// もう一度受信する...
88                 rx_bytes = comm.receive_from_camera();
89                 dump_bytes("Connect",99, rx_bytes);
90             }
91
92             // two_part messageを発行
93             comm.send_to_camera(sequence.start_message3_1(), true, false);
94             comm.send_to_camera(sequence.start_message3_2(), true, true);
95
96             rx_bytes = comm.receive_from_camera();
97             dump_bytes("Connect", 3, rx_bytes);
98
99             // remote mode
100             comm.send_to_camera(sequence.start_message4(), true, true);
101             rx_bytes = comm.receive_from_camera();
102 /*
103             dump_bytes("Connect", 4, rx_bytes);
104             rx_data1 = rx_bytes.getData();
105             if (rx_data1[4] == (byte) 0x02)
106             {
107                 //// 受信データが分割されている、、もう一度受信する
108                 rx_bytes = comm.receive_from_camera();
109                 dump_bytes("Connect", 98, rx_bytes);
110             }
111 */
112
113             if (!isBothLiveView)
114             {
115                 // two_part messageを発行
116                 comm.send_to_camera(sequence.start_message5_1(), true, false);
117                 comm.send_to_camera(sequence.start_message5_2(), true, true);
118                 rx_bytes = comm.receive_from_camera();
119                 dump_bytes("Connect", 5, rx_bytes);
120
121                 // ????
122                 comm.send_to_camera(sequence.status_request_message(), true, true);
123                 rx_bytes = comm.receive_from_camera();
124                 dump_bytes("Connect", 6, rx_bytes);
125                 rx_data1 = rx_bytes.getData();
126                 if (rx_data1[4] == (byte) 0x02)
127                 {
128                     //// 受信データが分割されている場合、、もう一度受信する
129                     rx_bytes = comm.receive_from_camera();
130                     dump_bytes("Connect", 97, rx_bytes);
131                 }
132
133                 // ????
134                 comm.send_to_camera(sequence.query_camera_capabilities(), true, true);
135                 rx_bytes = comm.receive_from_camera();
136                 dump_bytes("Connect", 7, rx_bytes);
137
138                 /*
139                 // ????
140                 comm.send_to_camera(sequence.status_request_message(), true, true);
141                 rx_bytes = comm.receive_from_camera();
142                 dump_bytes("Connect", 8, rx_bytes);
143 */
144             }
145
146             // リモート制御の開始!
147             comm.send_to_camera(sequence.camera_remote_message(), true, true);
148
149             // 応答OKの場合は、8バイト ({0x03, 0x00, 0x01, 0x20} + {0x10, 0x02, 0x00, 0x00} )が応答されるはず
150             rx_bytes = comm.receive_from_camera();
151             dump_bytes("Connect", 9, rx_bytes);
152
153             // 別のポートもオープンして動作を行う。 (1500ms程度待つといけるみたいだ...)
154             Thread.sleep(1500);
155             comm.start_stream();
156             comm.start_response();
157
158             Log.v(TAG, "connect_to_camera DONE.");
159             return (true);
160         }
161         catch (Exception e)
162         {
163             e.printStackTrace();
164         }
165         return (false);
166     }
167
168     public void reset_to_camera()
169     {
170         try
171         {
172             comm.send_to_camera(sequence.reset_message(), true, true);
173             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
174             dump_bytes("Reset", -1, rx_bytes);
175             statusChecker.stop();
176         }
177         catch (Exception e)
178         {
179             e.printStackTrace();
180         }
181     }
182
183     public void disconnect()
184     {
185         try
186         {
187             statusChecker.stop();
188         }
189         catch (Exception e)
190         {
191             e.printStackTrace();
192         }
193         try
194         {
195             comm.stop_stream();
196         }
197         catch (Exception e)
198         {
199             e.printStackTrace();
200         }
201         try
202         {
203             comm.stop_response();
204         }
205         catch (Exception e)
206         {
207             e.printStackTrace();
208         }
209         try
210         {
211             comm.disconnect_socket();
212         }
213         catch (Exception e)
214         {
215             e.printStackTrace();
216         }
217     }
218
219     @Override
220     public boolean requestStatus()
221     {
222         if (pauseRequestStatus)
223         {
224             // ステータス更新を一時停止する。
225             Log.v(TAG, "STATUS REQUEST IS PAUSED...");
226             return (false);
227         }
228         try
229         {
230             comm.send_to_camera(sequence.status_request_message(), true, true);
231             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
232             if (rx_bytes.getData().length > 0)
233             {
234                 // 受信したステータス情報を渡す
235                 statusChecker.statusReceived(rx_bytes);
236             }
237             dump_bytes("Status", 1, rx_bytes);
238 /*
239             byte[] rx_data1 = rx_bytes.getData();
240             if (rx_data1[4] == (byte) 0x02)
241             {
242                 //// 受信データが分割されている、、もう一度受信する
243                 rx_bytes = comm.receive_from_camera();
244                 dump_bytes("Status", 2, rx_bytes);
245                 if (rx_bytes.getData().length > 0)
246                 {
247                     // 受信したステータス情報を渡す
248                     statusChecker.statusReceived(rx_bytes);
249                 }
250             }
251 */
252             return (true);
253         }
254         catch (Exception e)
255         {
256             e.printStackTrace();
257         }
258         return (false);
259     }
260
261     private boolean dump_bytes(String header, int indexNumber, ReceivedDataHolder data)
262     {
263         int index = 0;
264         byte[] targetData = data.getData();
265         int receivedDataLength = targetData[0] + (targetData[1] << 8) + (targetData[2] << 16) + (targetData[3] << 24);
266         boolean checkLength = receivedDataLength <= targetData.length;
267         String logHead = (indexNumber < 0) ? header : header + " " + indexNumber;
268         StringBuffer message;
269         message = new StringBuffer();
270         for (byte item : data.getData())
271         {
272             index++;
273             message.append(String.format("%02x ", item));
274             if (index >= 8)
275             {
276                 Log.v(TAG, " RX [" + logHead + "] " + message);
277                 index = 0;
278                 message = new StringBuffer();
279             }
280         }
281         if (index != 0)
282         {
283             Log.v(TAG, " RX [" + logHead + "] "  + message);
284         }
285         //System.gc();
286         return (checkLength);
287     }
288
289     public boolean execute_focus_point(PointF point)
290     {
291         pauseRequestStatus = false;
292         try
293         {
294             byte x = (byte) (0x000000ff & Math.round(point.x));
295             byte y = (byte) (0x000000ff & Math.round(point.y));
296             Log.v(TAG, "DRIVE AF (" + x + "," + y + ")");
297             comm.send_to_camera(sequence.execute_focus_lock(x, y), true, true);
298             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
299             dump_bytes("Focus Point", -1, rx_bytes);
300         }
301         catch (Exception e)
302         {
303             e.printStackTrace();
304         }
305         pauseRequestStatus = false;
306         return (false);
307     }
308
309     public boolean execute_change_film_simulation()
310     {
311         try
312         {
313             // 現在の値を入手
314             int currentValue = statusChecker.getValue(FILM_SIMULATION);
315             currentValue++;
316             if (currentValue > FILM_SIMULATION_MAX)
317             {
318                 currentValue = FILM_SIMULATION_MIN;
319             }
320             return (updateProperty(FILM_SIMULATION, currentValue, true));
321         }
322         catch (Exception e)
323         {
324             e.printStackTrace();
325         }
326         return (false);
327     }
328
329     public boolean execute_change_image_aspect()
330     {
331         try
332         {
333             // 現在の値を入手
334             int currentValue = statusChecker.getValue(IMAGE_ASPECT);
335             currentValue++;
336             if (currentValue > IMAGE_ASPECT_MAX)
337             {
338                 currentValue = IMAGE_ASPECT_MIN;
339             }
340             return (updateProperty(IMAGE_ASPECT, currentValue, true));
341         }
342         catch (Exception e)
343         {
344             e.printStackTrace();
345         }
346         return (false);
347     }
348
349     public boolean execute_query_camera_capability()
350     {
351         pauseRequestStatus = true;
352         try
353         {
354             comm.send_to_camera(sequence.query_camera_capabilities(), true, true);
355             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
356             dump_bytes("Capability", -1, rx_bytes);
357         }
358         catch (Exception e)
359         {
360             e.printStackTrace();
361         }
362         pauseRequestStatus = false;
363         return (false);
364     }
365
366
367     public boolean execute_unlock_focus()
368     {
369         pauseRequestStatus = true;
370         try
371         {
372             comm.send_to_camera(sequence.execute_focus_unlock(), true, true);
373             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
374             dump_bytes("Unlock Focus", -1, rx_bytes);
375         }
376         catch (Exception e)
377         {
378             e.printStackTrace();
379         }
380         pauseRequestStatus = false;
381         return (false);
382     }
383
384     public boolean execute_shutter()
385     {
386         pauseRequestStatus = true;
387         try
388         {
389             comm.send_to_camera(sequence.execute_shutter_message(), true, true);
390             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
391             dump_bytes("Shutter", -1, rx_bytes);
392         }
393         catch (Exception e)
394         {
395             e.printStackTrace();
396         }
397         pauseRequestStatus = false;
398         return (false);
399     }
400
401     private boolean updateProperty(int commandCode, int setValue, boolean isShort)
402     {
403         pauseRequestStatus = true;
404         ReceivedDataHolder rx_bytes;
405         try {
406             byte high = (byte) ((0x0000ff00 & commandCode) >> 8);
407             byte low = (byte) (0x000000ff & commandCode);
408
409             byte data0 = (byte)((0xff000000 & setValue) >> 24);
410             byte data1 = (byte)((0x00ff0000 & setValue) >> 16);
411             byte data2 = (byte)((0x0000ff00 & setValue) >> 8);
412             byte data3 = (byte)((0x000000ff & setValue));
413
414             // two_part messageを発行
415             comm.send_to_camera(sequence.update_property_1(high, low), true, false);
416             comm.send_to_camera((isShort) ? sequence.update_property_2(data2, data3) : sequence.update_property_2(data0, data1, data2, data3), true, true);
417             rx_bytes = comm.receive_from_camera();
418             dump_bytes("Prop", -1, rx_bytes);
419         }
420         catch (Exception e)
421         {
422             e.printStackTrace();
423         }
424         pauseRequestStatus = false;
425         return (false);
426     }
427 }