Rev 252 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | ligi | 1 | /******************************************************************************************************************************** |
2 | * |
||
3 | * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de ) |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | * see README for further Infos |
||
8 | * |
||
9 | * |
||
10 | *******************************************************************************************************************************/ |
||
11 | |||
12 | |||
13 | package org.ligi.ufo; |
||
14 | |||
15 | |||
16 | //#ifdef j2me |
||
17 | //# import javax.microedition.io.*; |
||
18 | //#endif |
||
19 | |||
20 | //#ifdef android |
||
21 | import android.util.Log; |
||
22 | //#endif |
||
23 | |||
24 | |||
25 | |||
26 | import java.io.*; |
||
27 | |||
28 | |||
29 | public class MKCommunicator |
||
219 | ligi | 30 | implements Runnable,DUBwiseDefinitions |
206 | ligi | 31 | { |
222 | ligi | 32 | |
33 | public int angle_nick=-4242; |
||
34 | public int angle_roll=-4242; |
||
219 | ligi | 35 | public byte bl_retrys=0; |
36 | public boolean init_bootloader=false; |
||
213 | ligi | 37 | |
38 | public byte lib_version_major=0; |
||
252 | ligi | 39 | public byte lib_version_minor=5; |
213 | ligi | 40 | |
41 | public String lib_version_str() |
||
42 | { |
||
43 | return "V"+lib_version_major+"."+lib_version_minor; |
||
44 | } |
||
45 | |||
46 | |||
206 | ligi | 47 | /***************** Section: public Attributes **********************************************/ |
48 | public boolean connected=false; // flag for the connection state |
||
49 | |||
50 | public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes |
||
51 | |||
52 | public final static int DATA_BUFF_LEN = 20; // in lines |
||
53 | |||
54 | public String[] data_buff; |
||
55 | |||
56 | // boolean do_log=false; |
||
57 | boolean do_log=true; |
||
58 | |||
59 | int data_buff_pos=0; |
||
60 | |||
61 | public byte user_intent=0; |
||
219 | ligi | 62 | public final static int[] crc16_table = { |
63 | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
||
64 | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
||
65 | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
||
66 | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
||
67 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
||
68 | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
||
69 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
||
70 | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
||
71 | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
||
72 | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
||
73 | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
||
74 | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
||
75 | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
||
76 | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
||
77 | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
||
78 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
||
79 | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
||
80 | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
||
81 | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
||
82 | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
||
83 | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
||
84 | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
||
85 | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
||
86 | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
||
87 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
||
88 | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
||
89 | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
||
90 | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
||
91 | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
||
92 | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
||
93 | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
||
94 | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 |
||
95 | }; |
||
206 | ligi | 96 | |
97 | public void log(String str) |
||
98 | { |
||
99 | //#ifdef android |
||
219 | ligi | 100 | if (do_log) Log.d("MK-Comm",str); |
206 | ligi | 101 | //#endif |
233 | ligi | 102 | System.out.println(str); |
206 | ligi | 103 | } |
104 | |||
105 | public int CRC16(int ch, int crc) |
||
219 | ligi | 106 | { |
107 | return crc16_table[((crc >> 8) ^ (ch)) & 0xFF] ^ (crc << 8); |
||
206 | ligi | 108 | } |
109 | |||
110 | |||
111 | public int conn_time_in_s() |
||
112 | { |
||
113 | if (connected) |
||
114 | return (int)((System.currentTimeMillis()-connection_start_time)/1000); |
||
115 | else |
||
116 | return 0; |
||
117 | } |
||
219 | ligi | 118 | public final static byte BOOTLOADER_STAGE_NONE=0; |
119 | public final static byte BOOTLOADER_STAGE_GOT_MKBL=1; |
||
206 | ligi | 120 | |
219 | ligi | 121 | byte bootloader_stage= BOOTLOADER_STAGE_NONE; |
206 | ligi | 122 | |
123 | public MKLCD LCD; |
||
124 | public MKVersion version; |
||
125 | public MKDebugData debug_data; |
||
126 | |||
219 | ligi | 127 | public int[] extern_control; |
128 | |||
206 | ligi | 129 | public MKGPSPosition gps_position; |
130 | |||
131 | public MKStickData stick_data; |
||
132 | public MKParamsParser params; |
||
133 | public MKWatchDog watchdog; |
||
134 | public MKProxy proxy=null; |
||
135 | public MKStatistics stats ; |
||
136 | // public DUBwiseDebug debug; |
||
137 | |||
138 | public UFOProber ufo_prober; |
||
139 | public long connection_start_time=-1; |
||
140 | |||
141 | |||
142 | public String error_str = null; |
||
143 | |||
144 | |||
145 | public final static int FC_SLAVE_ADDR = 'a'+1; |
||
146 | public final static int NAVI_SLAVE_ADDR = 'a'+2; |
||
147 | public final static int MK3MAG_SLAVE_ADDR = 'a'+3; |
||
148 | |||
149 | |||
150 | |||
151 | |||
152 | /****************** Section: private Attributes **********************************************/ |
||
153 | //#ifdef j2me |
||
154 | //# private javax.microedition.io.StreamConnection connection; |
||
155 | //#endif |
||
156 | |||
157 | //#ifdef android |
||
158 | // java.net.Socket connection; |
||
159 | java.net.Socket connection; |
||
160 | //#endif |
||
161 | |||
162 | |||
163 | private java.io.InputStream reader; |
||
164 | private java.io.OutputStream writer; |
||
165 | |||
166 | |||
167 | |||
168 | public String name; |
||
169 | // DUBwise root; |
||
170 | |||
171 | |||
172 | private boolean sending=false; |
||
173 | private boolean recieving=false; |
||
174 | |||
175 | |||
176 | |||
177 | /****************** Section: public Methods ************************************************/ |
||
178 | public MKCommunicator() |
||
179 | { |
||
180 | |||
181 | data_buff=new String[DATA_BUFF_LEN]; |
||
182 | for (int i=0;i<DATA_BUFF_LEN;i++) |
||
183 | data_buff[i]=""; |
||
184 | // debug=debug_; |
||
185 | // root=root_; |
||
186 | version=new MKVersion(); |
||
187 | debug_data=new MKDebugData(); |
||
188 | stick_data=new MKStickData(); |
||
189 | params=new MKParamsParser(); |
||
219 | ligi | 190 | extern_control=new int[EXTERN_CONTROL_LENGTH]; |
191 | extern_control[EXTERN_CONTROL_CONFIG]=1; |
||
192 | extern_control[EXTERN_CONTROL_FRAME]=1; |
||
193 | |||
206 | ligi | 194 | LCD= new MKLCD(this); |
195 | watchdog=new MKWatchDog(this); |
||
196 | gps_position=new MKGPSPosition(); |
||
197 | stats = new MKStatistics(); |
||
198 | proxy =new MKProxy(this); |
||
199 | ufo_prober=new UFOProber(); |
||
200 | new Thread( this ).start(); // fire up main Thread |
||
201 | } |
||
202 | |||
203 | |||
204 | |||
205 | public void write_raw(byte[] _data) |
||
206 | { |
||
207 | wait4send(); |
||
208 | sending=true; |
||
209 | try { |
||
210 | writer.write(_data,0,_data.length); |
||
211 | writer.flush(); |
||
212 | |||
213 | stats.bytes_out+=_data.length; |
||
214 | } |
||
215 | catch ( Exception e){} |
||
216 | sending=false; |
||
217 | } |
||
218 | |||
219 | public void do_proxy(String proxy_url) |
||
220 | { |
||
221 | proxy.connect(proxy_url); |
||
222 | } |
||
223 | |||
224 | // int port; |
||
225 | |||
226 | // URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
||
227 | public void connect_to(String _url,String _name) |
||
228 | { |
||
229 | // port=_port; |
||
230 | mk_url=_url; // remember URL for connecting / reconnecting later |
||
231 | name=_name; |
||
232 | force_disconnect=false; |
||
233 | connected=false; |
||
234 | } |
||
235 | |||
236 | public boolean ready() |
||
237 | { |
||
238 | return (connected&&(version.major!=-1)); |
||
239 | } |
||
240 | |||
241 | |||
242 | public String get_buff(int age) |
||
243 | { |
||
244 | |||
245 | age%=DATA_BUFF_LEN; |
||
246 | |||
219 | ligi | 247 | if (age<=data_buff_pos) |
206 | ligi | 248 | return ""+data_buff[data_buff_pos-age]; |
249 | else |
||
250 | return ""+data_buff[DATA_BUFF_LEN+data_buff_pos-age]; |
||
251 | |||
252 | |||
253 | } |
||
254 | /****************** Section: private Methods ************************************************/ |
||
255 | private void connect() |
||
256 | { |
||
257 | log("trying to connect to" + mk_url); |
||
258 | try{ |
||
259 | |||
260 | // old call |
||
261 | // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
||
262 | |||
263 | //#ifdef android |
||
264 | connection = (new java.net.Socket(mk_url.split(":")[0],Integer.parseInt(mk_url.split(":")[1]))); |
||
265 | //.Socket |
||
266 | |||
267 | reader=connection.getInputStream(); |
||
268 | writer=connection.getOutputStream(); |
||
269 | |||
270 | String magic="conn:foo bar\r\n"; |
||
271 | writer.write(magic.getBytes()); |
||
272 | writer.flush(); |
||
273 | |||
274 | //#else |
||
275 | |||
276 | //# connection = (StreamConnection) Connector.open(mk_url); |
||
277 | |||
278 | //# reader=connection.openInputStream(); |
||
279 | //# writer=connection.openOutputStream(); |
||
280 | |||
281 | //#endif |
||
282 | connection_start_time=System.currentTimeMillis(); |
||
283 | connected=true; // if we get here everything seems to be OK |
||
284 | |||
285 | stats.reset(); |
||
286 | |||
287 | log("connecting OK"); |
||
288 | } |
||
289 | catch (Exception ex) |
||
290 | { |
||
291 | // TODO difference fatal errors from those which will lead to reconnection |
||
292 | log("Problem connecting" + "\n" + ex); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | public int[] Decode64(byte[] in_arr, int offset,int len) |
||
297 | { |
||
298 | int ptrIn=offset; |
||
299 | int a,b,c,d,x,y,z; |
||
300 | int ptr=0; |
||
301 | |||
302 | int[] out_arr=new int[len]; |
||
303 | |||
304 | while(len!=0) |
||
305 | { |
||
306 | a=0; |
||
307 | b=0; |
||
308 | c=0; |
||
309 | d=0; |
||
310 | try { |
||
311 | a = in_arr[ptrIn++] - '='; |
||
312 | b = in_arr[ptrIn++] - '='; |
||
313 | c = in_arr[ptrIn++] - '='; |
||
314 | d = in_arr[ptrIn++] - '='; |
||
315 | } |
||
316 | catch (Exception e) {} |
||
317 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
318 | |||
319 | x = (a << 2) | (b >> 4); |
||
320 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
321 | z = ((c & 0x03) << 6) | d; |
||
322 | |||
323 | if((len--)!=0) out_arr[ptr++] = x; else break; |
||
324 | if((len--)!=0) out_arr[ptr++] = y; else break; |
||
325 | if((len--)!=0) out_arr[ptr++] = z; else break; |
||
326 | } |
||
327 | |||
328 | return out_arr; |
||
329 | |||
330 | } |
||
331 | |||
332 | public void wait4send() |
||
333 | { |
||
334 | while(sending) //||recieving) |
||
335 | sleep(50); |
||
336 | } |
||
337 | |||
338 | |||
339 | public void sleep(int time) |
||
340 | { |
||
341 | try { Thread.sleep(time); } |
||
342 | catch (Exception e) { } |
||
343 | } |
||
344 | |||
345 | // FC - Function Mappers |
||
346 | |||
347 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
||
348 | public void get_version() |
||
349 | { |
||
350 | stats.version_data_request_count++; |
||
351 | send_command(0,'v'); |
||
352 | } |
||
353 | |||
354 | public void set_gps_target(int longitude,int latitude) |
||
355 | { |
||
356 | int[] target=new int[8]; |
||
357 | target[0]= (0xFF)&(longitude<<24); |
||
358 | target[1]= (0xFF)&(longitude<<16); |
||
359 | target[2]= (0xFF)&(longitude<<8); |
||
360 | target[3]= (0xFF)&(longitude); |
||
361 | // send_command(0,'s',target); |
||
362 | } |
||
363 | |||
364 | // send a MotorTest request - params are the speed for each Motor |
||
365 | public void motor_test(int[] params) |
||
366 | { |
||
367 | stats.motortest_request_count++; |
||
368 | send_command(FC_SLAVE_ADDR,'t',params); |
||
369 | } |
||
370 | |||
219 | ligi | 371 | |
372 | public void send_extern_control() |
||
206 | ligi | 373 | { |
219 | ligi | 374 | |
375 | stats.external_control_request_count++; |
||
376 | send_command(FC_SLAVE_ADDR,'b',extern_control); |
||
206 | ligi | 377 | } |
378 | |||
219 | ligi | 379 | /* public void send_keys(int[] params) |
380 | { |
||
381 | send_command(FC_SLAVE_ADDR,'k',params); |
||
382 | }*/ |
||
383 | |||
206 | ligi | 384 | // get params |
385 | public void get_params(int id) |
||
386 | { |
||
387 | wait4send(); |
||
388 | send_command(FC_SLAVE_ADDR,'q',id+1); |
||
389 | stats.params_data_request_count++; |
||
390 | } |
||
391 | |||
392 | public void get_debug_name(int id) |
||
393 | { |
||
394 | |||
395 | wait4send(); |
||
396 | send_command(0,'a',id); |
||
397 | } |
||
398 | |||
399 | |||
400 | public void trigger_LCD_by_page(int page) |
||
401 | { |
||
402 | wait4send(); |
||
403 | send_command(0,'l',page); |
||
404 | stats.lcd_data_request_count++; |
||
405 | } |
||
406 | |||
407 | public void trigger_debug() |
||
408 | { |
||
409 | if (sending||recieving) return; // its not that important - can be dropped |
||
410 | send_command(0,'c'); |
||
411 | } |
||
412 | |||
413 | |||
216 | ligi | 414 | public void switch_todo() |
415 | { |
||
416 | sleep(50); |
||
417 | version=new MKVersion(); |
||
418 | LCD= new MKLCD(this); |
||
419 | debug_data=new MKDebugData(); |
||
206 | ligi | 420 | |
216 | ligi | 421 | } |
422 | |||
206 | ligi | 423 | public void switch_to_fc() |
424 | { |
||
425 | wait4send(); |
||
426 | send_command(NAVI_SLAVE_ADDR,'u',0); |
||
216 | ligi | 427 | switch_todo(); |
428 | |||
206 | ligi | 429 | } |
430 | |||
431 | |||
432 | public void switch_to_mk3mag() |
||
433 | { |
||
434 | wait4send(); |
||
435 | send_command(NAVI_SLAVE_ADDR ,'u',1); |
||
216 | ligi | 436 | switch_todo(); |
206 | ligi | 437 | } |
438 | |||
215 | ligi | 439 | public final static byte[] navi_switch_magic={27,27,0x55,(byte)0xAA,0,(byte)'\r'}; |
206 | ligi | 440 | public void switch_to_navi() |
441 | { |
||
442 | wait4send(); |
||
443 | sending=true; |
||
444 | try |
||
445 | { |
||
215 | ligi | 446 | writer.write(navi_switch_magic); |
206 | ligi | 447 | stats.bytes_out+=6; |
448 | writer.flush(); |
||
449 | } |
||
450 | catch (Exception e) { } |
||
451 | sending=false; |
||
452 | |||
216 | ligi | 453 | switch_todo(); |
454 | |||
206 | ligi | 455 | } |
456 | |||
457 | public String[] flash_msgs; |
||
458 | int msg_pos=0; |
||
459 | |||
460 | |||
461 | public boolean bootloader_intension_flash=false; |
||
224 | ligi | 462 | |
463 | public boolean bootloader_finish_ok=false; |
||
464 | |||
206 | ligi | 465 | public void jump_bootloader() |
466 | { |
||
467 | |||
224 | ligi | 468 | bootloader_finish_ok=false; |
206 | ligi | 469 | msg_pos=0; |
470 | bootloader_stage= BOOTLOADER_STAGE_NONE; |
||
471 | flash_msgs=new String[100]; |
||
472 | flash_msgs[msg_pos++]="Initiializing Bootloader"; |
||
473 | wait4send(); |
||
474 | sending=true; |
||
475 | |||
476 | try |
||
477 | { |
||
478 | int attempt=0; |
||
479 | |||
480 | while(bootloader_stage!= BOOTLOADER_STAGE_GOT_MKBL) |
||
481 | { |
||
482 | flash_msgs[msg_pos]="attempt "+attempt; |
||
483 | attempt++; |
||
484 | send_command_nocheck((byte)FC_SLAVE_ADDR,'R',new int[0]); |
||
219 | ligi | 485 | |
486 | try{ |
||
206 | ligi | 487 | writer.write( 27); |
488 | writer.flush(); |
||
489 | |||
490 | sleep(20); |
||
491 | |||
492 | writer.write( 0xAA); |
||
493 | writer.flush(); |
||
219 | ligi | 494 | } |
495 | catch (Exception e) { } |
||
206 | ligi | 496 | sleep((attempt%2==0)?80:800); //800 |
497 | } |
||
498 | msg_pos++; |
||
499 | } |
||
500 | |||
501 | catch (Exception e) { |
||
502 | flash_msgs[msg_pos++]="Exception:" +e.getMessage() ; |
||
503 | flash_msgs[msg_pos++]=e.toString() ; |
||
504 | } |
||
505 | |||
506 | new Thread( this ).start(); // fire up main Thread |
||
507 | } |
||
508 | |||
509 | |||
510 | public void get_error_str() |
||
511 | { |
||
512 | send_command(NAVI_SLAVE_ADDR,'e'); |
||
513 | } |
||
514 | |||
515 | public void trigger_rcdata() |
||
516 | { |
||
517 | send_command(FC_SLAVE_ADDR,'p'); |
||
518 | } |
||
519 | |||
520 | |||
223 | ligi | 521 | public void write_params(int to) |
206 | ligi | 522 | { |
223 | ligi | 523 | params.update_backup(to); |
242 | ligi | 524 | write_params_(to) ; |
525 | } |
||
526 | |||
527 | public void write_params_(int to) |
||
528 | { |
||
206 | ligi | 529 | wait4send(); |
229 | ligi | 530 | params.active_paramset=to; |
223 | ligi | 531 | send_command(FC_SLAVE_ADDR,'s',params.field_bak[to]); |
262 | ligi | 532 | |
206 | ligi | 533 | } |
534 | |||
262 | ligi | 535 | public void set_debug_interval(int interval) |
536 | { |
||
537 | send_command(FC_SLAVE_ADDR,'d',interval); |
||
538 | } |
||
206 | ligi | 539 | public void send_command(int modul,char cmd) |
540 | { |
||
541 | send_command(modul,cmd,new int[0]); |
||
542 | } |
||
543 | |||
262 | ligi | 544 | |
545 | |||
206 | ligi | 546 | public void send_command(int modul,char cmd,int param) |
547 | { |
||
548 | int[] params=new int[1]; |
||
549 | params[0]=param; |
||
550 | send_command(modul,cmd,params); |
||
551 | } |
||
552 | |||
553 | public void send_command_nocheck(byte modul,char cmd,int[] params) |
||
554 | { |
||
555 | // char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
||
556 | |||
557 | byte[] send_buff=new byte[3 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
||
558 | send_buff[0]='#'; |
||
559 | send_buff[1]=modul; |
||
560 | send_buff[2]=(byte)cmd; |
||
561 | |||
562 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
||
563 | { |
||
564 | int a = (param_pos*3<params.length)?params[param_pos*3]:0; |
||
565 | int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0; |
||
566 | int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0; |
||
567 | |||
568 | send_buff[3+param_pos*4] = (byte)((a >> 2)+'=' ); |
||
569 | send_buff[3+param_pos*4+1] = (byte)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
||
570 | send_buff[3+param_pos*4+2] = (byte)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
||
571 | send_buff[3+param_pos*4+3] = (byte)('=' + ( c & 0x3f)); |
||
572 | |||
573 | //send_buff[3+foo]='='; |
||
574 | } |
||
575 | |||
576 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
||
577 | { |
||
578 | int a = (foo<params.length) params[foo]; |
||
579 | int a = params[foo]; |
||
580 | |||
581 | //send_buff[3+foo]='='; |
||
582 | } |
||
583 | */ |
||
584 | try |
||
585 | { |
||
586 | int tmp_crc=0; |
||
587 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
||
588 | tmp_crc+=(int)send_buff[tmp_i]; |
||
589 | |||
590 | writer.write(send_buff,0,send_buff.length); |
||
591 | tmp_crc%=4096; |
||
592 | |||
593 | writer.write( (char)(tmp_crc/64 + '=')); |
||
594 | writer.write( (char)(tmp_crc%64 + '=')); |
||
595 | writer.write('\r'); |
||
596 | stats.bytes_out+=send_buff.length+3; |
||
597 | writer.flush(); |
||
598 | } |
||
599 | catch (Exception e) |
||
600 | { // problem sending data to FC |
||
601 | } |
||
602 | |||
603 | } |
||
604 | // send command to FC ( add crc and pack into pseudo Base64 |
||
605 | public void send_command(int modul,char cmd,int[] params) |
||
606 | { |
||
607 | // if (modul==0) return; |
||
608 | sending=true; |
||
609 | send_command_nocheck((byte)modul,cmd,params); |
||
610 | sending=false; |
||
611 | } |
||
612 | |||
613 | |||
614 | public int slave_addr=-1; |
||
615 | |||
616 | |||
617 | public int UBatt() |
||
618 | { |
||
619 | if (ufo_prober.is_mk()) |
||
620 | return debug_data.UBatt(); |
||
621 | else if (ufo_prober.is_navi()) |
||
622 | return gps_position.UBatt; |
||
623 | |||
624 | return -1; |
||
625 | |||
626 | } |
||
627 | |||
628 | |||
629 | |||
630 | public int SenderOkay() |
||
631 | { |
||
632 | if (ufo_prober.is_mk()) |
||
633 | return debug_data.SenderOkay(); |
||
634 | else if (ufo_prober.is_navi()) |
||
635 | return gps_position.SenderOkay; |
||
636 | |||
637 | return -1; |
||
638 | |||
639 | } |
||
640 | |||
641 | |||
262 | ligi | 642 | public int[][] debug_buff=null; |
643 | public int debug_buff_off=0; |
||
644 | public int debug_buff_len=0; |
||
645 | public int debug_buff_interval=0; |
||
646 | public int debug_buff_lastset=0; |
||
647 | public int debug_buff_max=1; |
||
648 | |||
649 | public int[] debug_buff_targets=null; |
||
650 | |||
651 | public void setup_debug_buff(int[] targets,int len,int interval) |
||
652 | { |
||
653 | debug_buff=new int[len][targets.length]; |
||
654 | |||
655 | debug_buff_off=0; |
||
656 | debug_buff_len=len; |
||
657 | debug_buff_interval=interval; |
||
658 | debug_buff_targets=targets; |
||
659 | debug_buff_max=1; |
||
660 | debug_buff_lastset=0; |
||
661 | } |
||
662 | |||
663 | public int chg_debug_max(int val) |
||
664 | { |
||
665 | if (val>debug_buff_max) |
||
666 | debug_buff_max=val; |
||
667 | if (-val>debug_buff_max) |
||
668 | debug_buff_max=-val; |
||
669 | return val; |
||
670 | } |
||
671 | |||
672 | public void destroy_debug_buff() |
||
673 | { |
||
674 | debug_buff_targets=null; |
||
675 | } |
||
676 | |||
206 | ligi | 677 | public void process_data(byte[] data,int len) |
678 | { |
||
679 | |||
680 | |||
681 | log("command " +(char)data[2] ); |
||
682 | switch((char)data[2]) |
||
683 | { |
||
684 | |||
685 | case 'A': // debug Data Names |
||
686 | stats.debug_names_count++; |
||
687 | debug_data.set_names_by_mk_data(Decode64(data,3,len-3)); |
||
688 | break; |
||
689 | |||
219 | ligi | 690 | case 'B': // external_control confirm frames |
691 | stats.external_control_confirm_frame_count++; |
||
692 | break; |
||
693 | |||
206 | ligi | 694 | case 'L': // LCD Data |
695 | stats.lcd_data_count++; |
||
696 | LCD.handle_lcd_data(Decode64(data,3,len-3)); |
||
697 | |||
698 | break; |
||
699 | |||
700 | case 'D': // debug Data |
||
701 | log("got debug data"); |
||
702 | stats.debug_data_count++; |
||
703 | debug_data.set_by_mk_data(Decode64(data,3,len-3),version); |
||
262 | ligi | 704 | |
705 | if (debug_buff_targets!=null) |
||
706 | { |
||
707 | for (int sp=0;sp<debug_buff_targets.length;sp++) |
||
708 | debug_buff[debug_buff_off][sp]=chg_debug_max(debug_data.analog[debug_buff_targets[sp]]); |
||
709 | if (debug_buff_off>debug_buff_lastset) |
||
710 | debug_buff_lastset=debug_buff_off; |
||
711 | |||
712 | debug_buff_off=(debug_buff_off+1)%debug_buff_len; |
||
713 | |||
714 | |||
715 | |||
716 | } |
||
206 | ligi | 717 | log("processed debug data"); |
718 | break; |
||
719 | |||
720 | case 'V': // Version Info |
||
721 | stats.version_data_count++; |
||
722 | slave_addr=data[1]; |
||
723 | |||
724 | switch(slave_addr) |
||
725 | { |
||
726 | case FC_SLAVE_ADDR: |
||
727 | ufo_prober.set_to_mk(); |
||
728 | break; |
||
729 | |||
730 | case NAVI_SLAVE_ADDR: |
||
731 | ufo_prober.set_to_navi(); |
||
732 | break; |
||
733 | |||
734 | case MK3MAG_SLAVE_ADDR: |
||
735 | // ufo_prober.set_to_mk(); |
||
736 | ufo_prober.set_to_mk3mag(); |
||
737 | break; |
||
738 | |||
739 | default: |
||
740 | ufo_prober.set_to_incompatible(); |
||
741 | break; |
||
742 | } |
||
743 | |||
744 | |||
745 | version.set_by_mk_data(Decode64(data,3,len-3)); |
||
746 | break; |
||
747 | |||
748 | case 'w': |
||
222 | ligi | 749 | int[] dec=Decode64(data,3,len-3); |
223 | ligi | 750 | angle_nick=MKHelper.parse_signed_int_2(dec[0],dec[1]); |
751 | angle_roll=MKHelper.parse_signed_int_2(dec[2],dec[3]); |
||
206 | ligi | 752 | stats.angle_data_count++; |
753 | |||
754 | break; |
||
755 | |||
756 | |||
757 | case 'Q': |
||
758 | if (ufo_prober.is_mk()) |
||
759 | { |
||
760 | stats.params_data_count++; |
||
761 | params.set_by_mk_data(Decode64(data,3,len-3)); |
||
762 | } |
||
763 | break; |
||
764 | |||
765 | case 'P': |
||
766 | stats.stick_data_count++; |
||
767 | stick_data.set_by_mk_data(Decode64(data,3,20)); |
||
768 | break; |
||
769 | |||
770 | |||
771 | case 'E': |
||
772 | int[] dec_data=Decode64(data,3,len-3); |
||
773 | error_str=""; |
||
774 | for(int foo=0;foo<20;foo++) |
||
775 | if (dec_data[foo]!=0) |
||
776 | error_str+=(char)dec_data[foo]; |
||
777 | break; |
||
778 | |||
779 | |||
780 | case 'O': |
||
781 | stats.navi_data_count++; |
||
782 | log("got navi data(" + len +"):"); |
||
783 | |||
784 | gps_position.set_by_mk_data(Decode64(data,3,len-3),version); |
||
785 | |||
786 | log("long:" + gps_position.Longitude); |
||
787 | log("lat:" + gps_position.Latitude); |
||
788 | |||
789 | break; |
||
790 | |||
791 | |||
792 | // Error from Navi |
||
793 | |||
794 | |||
795 | default: |
||
796 | stats.other_data_count++; |
||
797 | break; |
||
798 | |||
799 | } |
||
800 | |||
801 | } |
||
802 | |||
803 | public boolean force_disconnect=true; |
||
804 | |||
805 | public void close_connections(boolean force) |
||
806 | { |
||
807 | // if ((!force)&&root.canvas.do_vibra) root.vibrate(500); |
||
808 | force_disconnect=force; |
||
809 | try{ reader.close(); } |
||
810 | catch (Exception inner_ex) { } |
||
811 | |||
812 | try{ writer.close(); } |
||
813 | catch (Exception inner_ex) { } |
||
814 | |||
815 | //#ifdef j2me |
||
816 | //# try{ connection.close(); } |
||
817 | //# catch (Exception inner_ex) { } |
||
818 | //#endif |
||
819 | ufo_prober.set_to_none(); |
||
219 | ligi | 820 | stats.reset(); |
206 | ligi | 821 | connected=false; |
822 | version=new MKVersion(); |
||
823 | } |
||
824 | |||
825 | // Thread to recieve data from Connection |
||
826 | public void run() |
||
827 | { |
||
219 | ligi | 828 | boolean sigfail=false; |
206 | ligi | 829 | if (bootloader_stage==BOOTLOADER_STAGE_GOT_MKBL) |
830 | { |
||
831 | try { |
||
832 | flash_msgs[msg_pos++]="reading avr_sig"; |
||
833 | |||
834 | writer.write( 't'); |
||
835 | writer.flush(); |
||
836 | |||
837 | int avr_sig=reader.read(); |
||
219 | ligi | 838 | |
224 | ligi | 839 | //while (avr_sig==63) |
840 | // avr_sig=reader.read(); |
||
219 | ligi | 841 | |
206 | ligi | 842 | flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
843 | |||
224 | ligi | 844 | int avrsig_suff=reader.read(); |
845 | if (avrsig_suff!=0) |
||
846 | throw new Exception("val after avrsig is" +avrsig_suff +"should b 0"); |
||
206 | ligi | 847 | |
848 | if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
||
219 | ligi | 849 | { |
850 | sigfail=true; |
||
851 | throw new Exception("avr sig" + avr_sig + " unknown"); |
||
852 | } |
||
206 | ligi | 853 | |
854 | writer.write('T'); |
||
855 | // writer.flush(); |
||
856 | writer.write(avr_sig); // set devicetyp = 0x74 oder 0x76 |
||
857 | writer.flush(); |
||
858 | |||
859 | if (reader.read()!=0x0d) |
||
860 | throw new Exception("cant get buffer size"); |
||
861 | |||
862 | writer.write('V'); |
||
863 | writer.flush(); |
||
864 | |||
865 | int bl_version_major=reader.read(); |
||
866 | int bl_version_minor=reader.read(); |
||
867 | |||
868 | flash_msgs[msg_pos++]="BL Version " + bl_version_major+"."+bl_version_minor; |
||
869 | |||
870 | |||
871 | writer.write('b'); |
||
872 | writer.flush(); |
||
873 | |||
874 | if (reader.read()!='Y') |
||
875 | throw new Exception("cant get buffer size"); |
||
876 | |||
877 | int send_buff_size1=reader.read(); |
||
878 | int send_buff_size2=reader.read(); |
||
879 | int send_buff_size=send_buff_size1*0x100+send_buff_size2; |
||
880 | |||
881 | flash_msgs[msg_pos++]="BUFF Size:" + send_buff_size; |
||
882 | // if (send_buff_size>128) |
||
883 | // send_buff_size=128; |
||
224 | ligi | 884 | |
885 | // if (!bootloader_intension |
||
886 | if (bootloader_intension_flash) |
||
219 | ligi | 887 | { |
206 | ligi | 888 | |
219 | ligi | 889 | byte[] flash_buff =new byte[send_buff_size]; ///!! |
890 | |||
891 | String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
||
892 | flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
||
893 | |||
894 | |||
895 | InputStream in; |
||
896 | try { |
||
897 | in=this.getClass().getResourceAsStream(firmware_filename); |
||
898 | } |
||
899 | |||
900 | catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
||
901 | int firmware_size=-1; |
||
902 | try { |
||
903 | |||
904 | firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
||
905 | } |
||
906 | catch (Exception e) { throw new Exception(" .. cant read size"); } |
||
907 | |||
908 | |||
909 | |||
910 | int blocks2write=((firmware_size/send_buff_size)); |
||
911 | flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
||
912 | |||
913 | |||
914 | |||
915 | // if (true) throw new Exception("before erasing"); |
||
916 | |||
917 | // if (true) throw new Exception("before erasing" ); |
||
918 | |||
919 | flash_msgs[msg_pos++]="Erasing Flash .."; |
||
920 | writer.write('e'); |
||
206 | ligi | 921 | writer.flush(); |
922 | |||
219 | ligi | 923 | if (reader.read()!=0x0d) |
924 | throw new Exception("cant erase flash"); |
||
925 | |||
926 | flash_msgs[msg_pos]+="OK"; |
||
927 | |||
928 | |||
929 | writer.write('A'); |
||
930 | writer.write(0); |
||
931 | writer.write(0); |
||
932 | writer.flush(); |
||
933 | |||
934 | if (reader.read()!=0x0d) |
||
935 | throw new Exception("cant set addr"); |
||
936 | |||
937 | flash_msgs[msg_pos++]="addr set"; |
||
938 | |||
939 | |||
940 | // int blocks2write=((firmware_size/send_buff_size)); |
||
941 | if ((firmware_size%send_buff_size)>0) |
||
942 | blocks2write++; |
||
943 | |||
944 | for ( int block=0; block<blocks2write; block ++) |
||
206 | ligi | 945 | { |
219 | ligi | 946 | int hex_bytes_read=in.read(flash_buff,0,send_buff_size); |
947 | |||
948 | flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
||
949 | |||
950 | |||
951 | writer.write('B'); |
||
952 | writer.write((hex_bytes_read>>8)& 0xFF); |
||
953 | writer.write((hex_bytes_read)& 0xFF); |
||
954 | writer.write('F'); |
||
955 | writer.flush(); |
||
956 | |||
957 | |||
958 | writer.write(flash_buff,0,hex_bytes_read); |
||
959 | writer.flush(); |
||
960 | |||
961 | |||
962 | if (avr_sig==224) |
||
963 | { |
||
964 | int crc=0xFFFF; |
||
965 | for (int crc_pos=0;crc_pos<hex_bytes_read;crc_pos++) |
||
966 | crc=CRC16(flash_buff[crc_pos],crc); |
||
967 | writer.write(crc>>8); |
||
968 | writer.write(crc&0xff); |
||
969 | writer.flush(); |
||
970 | } |
||
206 | ligi | 971 | // flash_msgs[msg_pos]+="ok"; |
972 | // writer.flush(); |
||
973 | |||
974 | |||
975 | |||
219 | ligi | 976 | if (reader.read()!=0x0d) |
977 | throw new Exception("abort write at block"+block); |
||
978 | |||
979 | |||
980 | |||
981 | // sleep(1000); |
||
982 | } |
||
983 | // flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
||
984 | /* |
||
985 | |||
206 | ligi | 986 | int inp=0; |
987 | int block=0; |
||
988 | while (inp!=-1) |
||
989 | { |
||
990 | int flash_buff_pos=0; |
||
991 | int crc=0xFFFF; |
||
992 | |||
993 | while ((flash_buff_pos<send_buff_size)&&(inp!=-1)) |
||
994 | { |
||
995 | inp=in.read(); |
||
996 | if (inp!=-1) |
||
997 | { |
||
998 | crc=CRC16(inp,crc); |
||
999 | flash_buff[flash_buff_pos++]=(byte)inp; |
||
1000 | } |
||
1001 | } |
||
1002 | // flash_msgs[msg_pos]="block" + block + "size:"+flash_buff_pos; |
||
1003 | |||
1004 | block++; |
||
1005 | |||
1006 | boolean block_fin=false; |
||
1007 | |||
1008 | |||
1009 | while(!block_fin) |
||
1010 | { |
||
1011 | |||
1012 | writer.write('B'); |
||
1013 | writer.write((flash_buff_pos>>8)& 0xFF); |
||
1014 | writer.write((flash_buff_pos)& 0xFF); |
||
1015 | writer.write('F'); |
||
1016 | writer.flush(); |
||
1017 | |||
1018 | // int ret_v=-1; |
||
1019 | |||
1020 | writer.write(flash_buff,0,flash_buff_pos); |
||
1021 | flash_msgs[msg_pos]="bl:" + block + "si:"+flash_buff_pos ; |
||
1022 | |||
1023 | writer.flush(); |
||
1024 | // flash_msgs[msg_pos]+="wtc"; |
||
1025 | |||
1026 | |||
1027 | // append crc if navi |
||
1028 | if (avr_sig==224) |
||
1029 | { |
||
1030 | writer.write(crc>>8); |
||
1031 | writer.write(crc&0xff); |
||
1032 | writer.flush(); |
||
1033 | } |
||
1034 | // flash_msgs[msg_pos]+="ok"; |
||
1035 | // writer.flush(); |
||
1036 | // if (reader.read()!=0x0d) |
||
1037 | // throw new Exception("abort write at block"+block); |
||
1038 | |||
1039 | |||
1040 | //ret_v=reader.read(); |
||
1041 | // flash_msgs[msg_pos]="ret"+ret_v + "crc"+crc; |
||
1042 | |||
1043 | if (reader.read()==0x0d) |
||
1044 | block_fin=true; |
||
1045 | |||
1046 | } |
||
1047 | |||
1048 | } |
||
1049 | */ |
||
1050 | flash_msgs[++msg_pos]="written last block "; |
||
1051 | msg_pos++; |
||
1052 | flash_buff=null; |
||
1053 | |||
1054 | ufo_prober.set_to_none(); |
||
1055 | stats.reset(); |
||
1056 | version=new MKVersion(); |
||
1057 | System.gc(); |
||
1058 | } |
||
1059 | else // bootloader intension clear settings |
||
1060 | { |
||
1061 | |||
1062 | flash_msgs[msg_pos]="reset params .."; |
||
1063 | writer.write('B'); |
||
1064 | writer.write(0); |
||
1065 | writer.write(4); |
||
1066 | writer.write('E'); |
||
1067 | writer.flush(); |
||
1068 | |||
1069 | writer.write(0xFF); |
||
1070 | writer.write(0xFF); |
||
1071 | writer.write(0xFF); |
||
1072 | writer.write(0xFF); |
||
1073 | writer.flush(); |
||
1074 | flash_msgs[msg_pos++]+=" done"; |
||
1075 | } |
||
1076 | |||
1077 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
||
1078 | params=new MKParamsParser(); |
||
1079 | try{ |
||
1080 | writer.write('E'); |
||
1081 | writer.flush(); |
||
1082 | } |
||
1083 | catch (Exception e) { |
||
1084 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
||
1085 | } |
||
1086 | flash_msgs[msg_pos++]="Exit BL done" ; |
||
1087 | |||
224 | ligi | 1088 | bootloader_finish_ok=true; |
206 | ligi | 1089 | } |
1090 | |||
1091 | catch (Exception e) { |
||
1092 | flash_msgs[msg_pos++]="Fail:" +e.getMessage() ; |
||
1093 | |||
1094 | |||
1095 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
||
1096 | params=new MKParamsParser(); |
||
1097 | try{ |
||
1098 | writer.write('E'); |
||
1099 | writer.flush(); |
||
1100 | } |
||
1101 | catch (Exception e2) { |
||
1102 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
||
1103 | } |
||
1104 | flash_msgs[msg_pos++]="Exit BL done" ; |
||
219 | ligi | 1105 | if (sigfail&&(bl_retrys<3)) |
1106 | { |
||
1107 | bl_retrys++; |
||
1108 | init_bootloader=true; |
||
1109 | } |
||
1110 | close_connections(false); |
||
206 | ligi | 1111 | } |
1112 | |||
1113 | |||
1114 | sending=false; |
||
1115 | } |
||
1116 | |||
1117 | |||
1118 | byte[] data_set=new byte[1024]; |
||
1119 | int data_set_pos=0; |
||
1120 | byte[] data_in_buff=new byte[2048]; |
||
1121 | |||
1122 | int input; |
||
1123 | int pos=0; |
||
1124 | |||
1125 | |||
1126 | |||
1127 | |||
1128 | |||
1129 | log("Thread started"); |
||
1130 | while(true) |
||
1131 | { |
||
1132 | |||
1133 | if (!connected) |
||
1134 | { |
||
1135 | if (!force_disconnect) connect(); |
||
1136 | sleep(100); |
||
1137 | } |
||
1138 | else |
||
1139 | try{ |
||
1140 | |||
1141 | /* |
||
1142 | while(sending) |
||
1143 | {try { Thread.sleep(50); } |
||
1144 | catch (Exception e) { } |
||
1145 | } |
||
1146 | */ |
||
1147 | |||
1148 | recieving=true; |
||
1149 | int read_count =reader.read(data_in_buff,0,reader.available()); |
||
262 | ligi | 1150 | // log("Connected - reading data " + read_count); |
206 | ligi | 1151 | // pos=0; |
1152 | input=0; |
||
1153 | //data_buff[data_buff_pos]=""; |
||
1154 | // recieve data-set |
||
1155 | if (read_count==0) sleep(50); |
||
1156 | |||
1157 | // int read_count =reader.read(data_in_buff,0,reader.available()); |
||
1158 | stats.bytes_in+=read_count; |
||
1159 | if (read_count>0) |
||
1160 | { |
||
1161 | log("read" + read_count + " ds_pos" + data_set_pos); |
||
1162 | |||
1163 | for ( pos=0;pos<read_count;pos++) |
||
1164 | { |
||
1165 | if (data_in_buff[pos]==13) |
||
1166 | { |
||
233 | ligi | 1167 | data_buff[data_buff_pos]=new String(data_set, 0, data_set_pos); |
206 | ligi | 1168 | data_buff_pos++; |
1169 | data_buff_pos%=DATA_BUFF_LEN; |
||
1170 | |||
1171 | |||
1172 | try{process_data(data_set,data_set_pos); } |
||
1173 | catch (Exception e) |
||
1174 | { |
||
1175 | log(".. problem processing"); |
||
1176 | log(e.toString()); |
||
1177 | } |
||
1178 | |||
1179 | |||
1180 | |||
1181 | |||
1182 | proxy.write(data_set,0,data_set_pos); |
||
1183 | // proxy.writer.write('\r'); |
||
1184 | //proxy.writer.write('\n'); |
||
1185 | //proxy.writer.flush(); |
||
1186 | /* |
||
1187 | if (proxy!=null) |
||
1188 | { |
||
1189 | |||
1190 | |||
1191 | |||
1192 | } |
||
1193 | */ |
||
1194 | data_set_pos=0; |
||
1195 | |||
1196 | } |
||
1197 | else |
||
1198 | { |
||
1199 | data_set[data_set_pos++]=data_in_buff[pos]; |
||
1200 | |||
1201 | |||
1202 | |||
1203 | if ( (data_set_pos>4) && (data_set[data_set_pos-4]==(byte)'M') && (data_set[data_set_pos-3]==(byte)'K') && (data_set[data_set_pos-2]==(byte)'B') && (data_set[data_set_pos-1]==(byte)'L')) |
||
1204 | { |
||
1205 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
||
1206 | return; |
||
1207 | } |
||
1208 | |||
1209 | } |
||
1210 | |||
1211 | } |
||
1212 | |||
1213 | |||
1214 | } |
||
1215 | else |
||
1216 | sleep(50); |
||
1217 | /* |
||
1218 | while ((input != 13)) //&&(input!=-1)) |
||
1219 | { |
||
1220 | { |
||
1221 | //log("pre read"); |
||
1222 | log(""+reader.available()); |
||
1223 | input = reader.read() ; |
||
1224 | log("Byte rcv" + input +"pos"+ pos); |
||
1225 | |||
1226 | proxy.write(input); |
||
1227 | |||
1228 | data_buff[data_buff_pos]+=(char)input; |
||
1229 | |||
1230 | if ((data_buff[data_buff_pos].length()>3)&&(data_buff[data_buff_pos].substring(data_buff[data_buff_pos].length()-4,data_buff[data_buff_pos].length()).equals("MKBL"))) |
||
1231 | { |
||
1232 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
||
1233 | return; |
||
1234 | } |
||
1235 | if (input==-1) throw new Exception("disconnect"); |
||
1236 | else |
||
1237 | { |
||
1238 | stats.bytes_in++; |
||
1239 | data_set[pos]=input; |
||
1240 | pos++; |
||
1241 | } |
||
1242 | } |
||
1243 | |||
1244 | } |
||
1245 | |||
1246 | |||
1247 | |||
1248 | data_buff_pos++; |
||
1249 | data_buff_pos%=DATA_BUFF_LEN; |
||
1250 | recieving=false; |
||
1251 | log("Data recieved (" + pos + "Bytes)"); |
||
1252 | log("processing .."); |
||
1253 | */ |
||
1254 | |||
1255 | /* |
||
1256 | if (proxy!=null) |
||
1257 | { |
||
1258 | proxy.writer.write('\r'); |
||
1259 | proxy.writer.write('\n'); |
||
1260 | proxy.writer.flush(); |
||
1261 | } |
||
1262 | */ |
||
1263 | /*if (pos>5) |
||
1264 | { |
||
1265 | try{process_data(data_set,pos); } |
||
1266 | catch (Exception e) |
||
1267 | { |
||
1268 | log(".. problem processing"); |
||
1269 | log(e.toString()); |
||
1270 | } |
||
1271 | |||
1272 | log(".. processing done"); |
||
1273 | } |
||
1274 | */ |
||
1275 | } |
||
1276 | catch (Exception ex) |
||
1277 | { |
||
1278 | log("Problem reading from MK -> closing conn"); |
||
1279 | log(ex.toString()); |
||
1280 | // close the connection |
||
1281 | close_connections(false); |
||
1282 | } |
||
1283 | |||
1284 | // sleep a bit to get someting more done |
||
1285 | // sleep(5); //50 |
||
1286 | |||
1287 | } // while |
||
1288 | // log("Leaving Communicator thread"); |
||
1289 | } // run() |
||
1290 | } |