Rev 229 | 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; |
||
223 | ligi | 39 | public byte lib_version_minor=4; |
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); |
206 | ligi | 524 | wait4send(); |
229 | ligi | 525 | params.active_paramset=to; |
223 | ligi | 526 | send_command(FC_SLAVE_ADDR,'s',params.field_bak[to]); |
206 | ligi | 527 | } |
528 | |||
529 | public void send_command(int modul,char cmd) |
||
530 | { |
||
531 | send_command(modul,cmd,new int[0]); |
||
532 | } |
||
533 | |||
534 | public void send_command(int modul,char cmd,int param) |
||
535 | { |
||
536 | int[] params=new int[1]; |
||
537 | params[0]=param; |
||
538 | send_command(modul,cmd,params); |
||
539 | } |
||
540 | |||
541 | public void send_command_nocheck(byte modul,char cmd,int[] params) |
||
542 | { |
||
543 | // 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 |
||
544 | |||
545 | 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 |
||
546 | send_buff[0]='#'; |
||
547 | send_buff[1]=modul; |
||
548 | send_buff[2]=(byte)cmd; |
||
549 | |||
550 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
||
551 | { |
||
552 | int a = (param_pos*3<params.length)?params[param_pos*3]:0; |
||
553 | int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0; |
||
554 | int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0; |
||
555 | |||
556 | send_buff[3+param_pos*4] = (byte)((a >> 2)+'=' ); |
||
557 | send_buff[3+param_pos*4+1] = (byte)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
||
558 | send_buff[3+param_pos*4+2] = (byte)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
||
559 | send_buff[3+param_pos*4+3] = (byte)('=' + ( c & 0x3f)); |
||
560 | |||
561 | //send_buff[3+foo]='='; |
||
562 | } |
||
563 | |||
564 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
||
565 | { |
||
566 | int a = (foo<params.length) params[foo]; |
||
567 | int a = params[foo]; |
||
568 | |||
569 | //send_buff[3+foo]='='; |
||
570 | } |
||
571 | */ |
||
572 | try |
||
573 | { |
||
574 | int tmp_crc=0; |
||
575 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
||
576 | tmp_crc+=(int)send_buff[tmp_i]; |
||
577 | |||
578 | writer.write(send_buff,0,send_buff.length); |
||
579 | tmp_crc%=4096; |
||
580 | |||
581 | writer.write( (char)(tmp_crc/64 + '=')); |
||
582 | writer.write( (char)(tmp_crc%64 + '=')); |
||
583 | writer.write('\r'); |
||
584 | stats.bytes_out+=send_buff.length+3; |
||
585 | writer.flush(); |
||
586 | } |
||
587 | catch (Exception e) |
||
588 | { // problem sending data to FC |
||
589 | } |
||
590 | |||
591 | } |
||
592 | // send command to FC ( add crc and pack into pseudo Base64 |
||
593 | public void send_command(int modul,char cmd,int[] params) |
||
594 | { |
||
595 | // if (modul==0) return; |
||
596 | sending=true; |
||
597 | send_command_nocheck((byte)modul,cmd,params); |
||
598 | sending=false; |
||
599 | } |
||
600 | |||
601 | |||
602 | public int slave_addr=-1; |
||
603 | |||
604 | |||
605 | public int UBatt() |
||
606 | { |
||
607 | if (ufo_prober.is_mk()) |
||
608 | return debug_data.UBatt(); |
||
609 | else if (ufo_prober.is_navi()) |
||
610 | return gps_position.UBatt; |
||
611 | |||
612 | return -1; |
||
613 | |||
614 | } |
||
615 | |||
616 | |||
617 | |||
618 | public int SenderOkay() |
||
619 | { |
||
620 | if (ufo_prober.is_mk()) |
||
621 | return debug_data.SenderOkay(); |
||
622 | else if (ufo_prober.is_navi()) |
||
623 | return gps_position.SenderOkay; |
||
624 | |||
625 | return -1; |
||
626 | |||
627 | } |
||
628 | |||
629 | |||
630 | public void process_data(byte[] data,int len) |
||
631 | { |
||
632 | |||
633 | |||
634 | log("command " +(char)data[2] ); |
||
635 | switch((char)data[2]) |
||
636 | { |
||
637 | |||
638 | case 'A': // debug Data Names |
||
639 | stats.debug_names_count++; |
||
640 | debug_data.set_names_by_mk_data(Decode64(data,3,len-3)); |
||
641 | break; |
||
642 | |||
219 | ligi | 643 | case 'B': // external_control confirm frames |
644 | stats.external_control_confirm_frame_count++; |
||
645 | break; |
||
646 | |||
206 | ligi | 647 | case 'L': // LCD Data |
648 | stats.lcd_data_count++; |
||
649 | LCD.handle_lcd_data(Decode64(data,3,len-3)); |
||
650 | |||
651 | break; |
||
652 | |||
653 | case 'D': // debug Data |
||
654 | log("got debug data"); |
||
655 | stats.debug_data_count++; |
||
656 | debug_data.set_by_mk_data(Decode64(data,3,len-3),version); |
||
657 | log("processed debug data"); |
||
658 | break; |
||
659 | |||
660 | case 'V': // Version Info |
||
661 | stats.version_data_count++; |
||
662 | slave_addr=data[1]; |
||
663 | |||
664 | switch(slave_addr) |
||
665 | { |
||
666 | case FC_SLAVE_ADDR: |
||
667 | ufo_prober.set_to_mk(); |
||
668 | break; |
||
669 | |||
670 | case NAVI_SLAVE_ADDR: |
||
671 | ufo_prober.set_to_navi(); |
||
672 | break; |
||
673 | |||
674 | case MK3MAG_SLAVE_ADDR: |
||
675 | // ufo_prober.set_to_mk(); |
||
676 | ufo_prober.set_to_mk3mag(); |
||
677 | break; |
||
678 | |||
679 | default: |
||
680 | ufo_prober.set_to_incompatible(); |
||
681 | break; |
||
682 | } |
||
683 | |||
684 | |||
685 | version.set_by_mk_data(Decode64(data,3,len-3)); |
||
686 | break; |
||
687 | |||
688 | case 'w': |
||
222 | ligi | 689 | int[] dec=Decode64(data,3,len-3); |
223 | ligi | 690 | angle_nick=MKHelper.parse_signed_int_2(dec[0],dec[1]); |
691 | angle_roll=MKHelper.parse_signed_int_2(dec[2],dec[3]); |
||
206 | ligi | 692 | stats.angle_data_count++; |
693 | |||
694 | break; |
||
695 | |||
696 | |||
697 | case 'Q': |
||
698 | if (ufo_prober.is_mk()) |
||
699 | { |
||
700 | stats.params_data_count++; |
||
701 | params.set_by_mk_data(Decode64(data,3,len-3)); |
||
702 | } |
||
703 | break; |
||
704 | |||
705 | case 'P': |
||
706 | stats.stick_data_count++; |
||
707 | stick_data.set_by_mk_data(Decode64(data,3,20)); |
||
708 | break; |
||
709 | |||
710 | |||
711 | case 'E': |
||
712 | int[] dec_data=Decode64(data,3,len-3); |
||
713 | error_str=""; |
||
714 | for(int foo=0;foo<20;foo++) |
||
715 | if (dec_data[foo]!=0) |
||
716 | error_str+=(char)dec_data[foo]; |
||
717 | break; |
||
718 | |||
719 | |||
720 | case 'O': |
||
721 | stats.navi_data_count++; |
||
722 | log("got navi data(" + len +"):"); |
||
723 | |||
724 | gps_position.set_by_mk_data(Decode64(data,3,len-3),version); |
||
725 | |||
726 | log("long:" + gps_position.Longitude); |
||
727 | log("lat:" + gps_position.Latitude); |
||
728 | |||
729 | break; |
||
730 | |||
731 | |||
732 | // Error from Navi |
||
733 | |||
734 | |||
735 | default: |
||
736 | stats.other_data_count++; |
||
737 | break; |
||
738 | |||
739 | } |
||
740 | |||
741 | } |
||
742 | |||
743 | public boolean force_disconnect=true; |
||
744 | |||
745 | public void close_connections(boolean force) |
||
746 | { |
||
747 | // if ((!force)&&root.canvas.do_vibra) root.vibrate(500); |
||
748 | force_disconnect=force; |
||
749 | try{ reader.close(); } |
||
750 | catch (Exception inner_ex) { } |
||
751 | |||
752 | try{ writer.close(); } |
||
753 | catch (Exception inner_ex) { } |
||
754 | |||
755 | //#ifdef j2me |
||
756 | //# try{ connection.close(); } |
||
757 | //# catch (Exception inner_ex) { } |
||
758 | //#endif |
||
759 | ufo_prober.set_to_none(); |
||
219 | ligi | 760 | stats.reset(); |
206 | ligi | 761 | connected=false; |
762 | version=new MKVersion(); |
||
763 | } |
||
764 | |||
765 | // Thread to recieve data from Connection |
||
766 | public void run() |
||
767 | { |
||
219 | ligi | 768 | boolean sigfail=false; |
206 | ligi | 769 | if (bootloader_stage==BOOTLOADER_STAGE_GOT_MKBL) |
770 | { |
||
771 | try { |
||
772 | flash_msgs[msg_pos++]="reading avr_sig"; |
||
773 | |||
774 | writer.write( 't'); |
||
775 | writer.flush(); |
||
776 | |||
777 | int avr_sig=reader.read(); |
||
219 | ligi | 778 | |
224 | ligi | 779 | //while (avr_sig==63) |
780 | // avr_sig=reader.read(); |
||
219 | ligi | 781 | |
206 | ligi | 782 | flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
783 | |||
224 | ligi | 784 | int avrsig_suff=reader.read(); |
785 | if (avrsig_suff!=0) |
||
786 | throw new Exception("val after avrsig is" +avrsig_suff +"should b 0"); |
||
206 | ligi | 787 | |
788 | if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
||
219 | ligi | 789 | { |
790 | sigfail=true; |
||
791 | throw new Exception("avr sig" + avr_sig + " unknown"); |
||
792 | } |
||
206 | ligi | 793 | |
794 | writer.write('T'); |
||
795 | // writer.flush(); |
||
796 | writer.write(avr_sig); // set devicetyp = 0x74 oder 0x76 |
||
797 | writer.flush(); |
||
798 | |||
799 | if (reader.read()!=0x0d) |
||
800 | throw new Exception("cant get buffer size"); |
||
801 | |||
802 | writer.write('V'); |
||
803 | writer.flush(); |
||
804 | |||
805 | int bl_version_major=reader.read(); |
||
806 | int bl_version_minor=reader.read(); |
||
807 | |||
808 | flash_msgs[msg_pos++]="BL Version " + bl_version_major+"."+bl_version_minor; |
||
809 | |||
810 | |||
811 | writer.write('b'); |
||
812 | writer.flush(); |
||
813 | |||
814 | if (reader.read()!='Y') |
||
815 | throw new Exception("cant get buffer size"); |
||
816 | |||
817 | int send_buff_size1=reader.read(); |
||
818 | int send_buff_size2=reader.read(); |
||
819 | int send_buff_size=send_buff_size1*0x100+send_buff_size2; |
||
820 | |||
821 | flash_msgs[msg_pos++]="BUFF Size:" + send_buff_size; |
||
822 | // if (send_buff_size>128) |
||
823 | // send_buff_size=128; |
||
224 | ligi | 824 | |
825 | // if (!bootloader_intension |
||
826 | if (bootloader_intension_flash) |
||
219 | ligi | 827 | { |
206 | ligi | 828 | |
219 | ligi | 829 | byte[] flash_buff =new byte[send_buff_size]; ///!! |
830 | |||
831 | String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
||
832 | flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
||
833 | |||
834 | |||
835 | InputStream in; |
||
836 | try { |
||
837 | in=this.getClass().getResourceAsStream(firmware_filename); |
||
838 | } |
||
839 | |||
840 | catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
||
841 | int firmware_size=-1; |
||
842 | try { |
||
843 | |||
844 | firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
||
845 | } |
||
846 | catch (Exception e) { throw new Exception(" .. cant read size"); } |
||
847 | |||
848 | |||
849 | |||
850 | int blocks2write=((firmware_size/send_buff_size)); |
||
851 | flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
||
852 | |||
853 | |||
854 | |||
855 | // if (true) throw new Exception("before erasing"); |
||
856 | |||
857 | // if (true) throw new Exception("before erasing" ); |
||
858 | |||
859 | flash_msgs[msg_pos++]="Erasing Flash .."; |
||
860 | writer.write('e'); |
||
206 | ligi | 861 | writer.flush(); |
862 | |||
219 | ligi | 863 | if (reader.read()!=0x0d) |
864 | throw new Exception("cant erase flash"); |
||
865 | |||
866 | flash_msgs[msg_pos]+="OK"; |
||
867 | |||
868 | |||
869 | writer.write('A'); |
||
870 | writer.write(0); |
||
871 | writer.write(0); |
||
872 | writer.flush(); |
||
873 | |||
874 | if (reader.read()!=0x0d) |
||
875 | throw new Exception("cant set addr"); |
||
876 | |||
877 | flash_msgs[msg_pos++]="addr set"; |
||
878 | |||
879 | |||
880 | // int blocks2write=((firmware_size/send_buff_size)); |
||
881 | if ((firmware_size%send_buff_size)>0) |
||
882 | blocks2write++; |
||
883 | |||
884 | for ( int block=0; block<blocks2write; block ++) |
||
206 | ligi | 885 | { |
219 | ligi | 886 | int hex_bytes_read=in.read(flash_buff,0,send_buff_size); |
887 | |||
888 | flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
||
889 | |||
890 | |||
891 | writer.write('B'); |
||
892 | writer.write((hex_bytes_read>>8)& 0xFF); |
||
893 | writer.write((hex_bytes_read)& 0xFF); |
||
894 | writer.write('F'); |
||
895 | writer.flush(); |
||
896 | |||
897 | |||
898 | writer.write(flash_buff,0,hex_bytes_read); |
||
899 | writer.flush(); |
||
900 | |||
901 | |||
902 | if (avr_sig==224) |
||
903 | { |
||
904 | int crc=0xFFFF; |
||
905 | for (int crc_pos=0;crc_pos<hex_bytes_read;crc_pos++) |
||
906 | crc=CRC16(flash_buff[crc_pos],crc); |
||
907 | writer.write(crc>>8); |
||
908 | writer.write(crc&0xff); |
||
909 | writer.flush(); |
||
910 | } |
||
206 | ligi | 911 | // flash_msgs[msg_pos]+="ok"; |
912 | // writer.flush(); |
||
913 | |||
914 | |||
915 | |||
219 | ligi | 916 | if (reader.read()!=0x0d) |
917 | throw new Exception("abort write at block"+block); |
||
918 | |||
919 | |||
920 | |||
921 | // sleep(1000); |
||
922 | } |
||
923 | // flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
||
924 | /* |
||
925 | |||
206 | ligi | 926 | int inp=0; |
927 | int block=0; |
||
928 | while (inp!=-1) |
||
929 | { |
||
930 | int flash_buff_pos=0; |
||
931 | int crc=0xFFFF; |
||
932 | |||
933 | while ((flash_buff_pos<send_buff_size)&&(inp!=-1)) |
||
934 | { |
||
935 | inp=in.read(); |
||
936 | if (inp!=-1) |
||
937 | { |
||
938 | crc=CRC16(inp,crc); |
||
939 | flash_buff[flash_buff_pos++]=(byte)inp; |
||
940 | } |
||
941 | } |
||
942 | // flash_msgs[msg_pos]="block" + block + "size:"+flash_buff_pos; |
||
943 | |||
944 | block++; |
||
945 | |||
946 | boolean block_fin=false; |
||
947 | |||
948 | |||
949 | while(!block_fin) |
||
950 | { |
||
951 | |||
952 | writer.write('B'); |
||
953 | writer.write((flash_buff_pos>>8)& 0xFF); |
||
954 | writer.write((flash_buff_pos)& 0xFF); |
||
955 | writer.write('F'); |
||
956 | writer.flush(); |
||
957 | |||
958 | // int ret_v=-1; |
||
959 | |||
960 | writer.write(flash_buff,0,flash_buff_pos); |
||
961 | flash_msgs[msg_pos]="bl:" + block + "si:"+flash_buff_pos ; |
||
962 | |||
963 | writer.flush(); |
||
964 | // flash_msgs[msg_pos]+="wtc"; |
||
965 | |||
966 | |||
967 | // append crc if navi |
||
968 | if (avr_sig==224) |
||
969 | { |
||
970 | writer.write(crc>>8); |
||
971 | writer.write(crc&0xff); |
||
972 | writer.flush(); |
||
973 | } |
||
974 | // flash_msgs[msg_pos]+="ok"; |
||
975 | // writer.flush(); |
||
976 | // if (reader.read()!=0x0d) |
||
977 | // throw new Exception("abort write at block"+block); |
||
978 | |||
979 | |||
980 | //ret_v=reader.read(); |
||
981 | // flash_msgs[msg_pos]="ret"+ret_v + "crc"+crc; |
||
982 | |||
983 | if (reader.read()==0x0d) |
||
984 | block_fin=true; |
||
985 | |||
986 | } |
||
987 | |||
988 | } |
||
989 | */ |
||
990 | flash_msgs[++msg_pos]="written last block "; |
||
991 | msg_pos++; |
||
992 | flash_buff=null; |
||
993 | |||
994 | ufo_prober.set_to_none(); |
||
995 | stats.reset(); |
||
996 | version=new MKVersion(); |
||
997 | System.gc(); |
||
998 | } |
||
999 | else // bootloader intension clear settings |
||
1000 | { |
||
1001 | |||
1002 | flash_msgs[msg_pos]="reset params .."; |
||
1003 | writer.write('B'); |
||
1004 | writer.write(0); |
||
1005 | writer.write(4); |
||
1006 | writer.write('E'); |
||
1007 | writer.flush(); |
||
1008 | |||
1009 | writer.write(0xFF); |
||
1010 | writer.write(0xFF); |
||
1011 | writer.write(0xFF); |
||
1012 | writer.write(0xFF); |
||
1013 | writer.flush(); |
||
1014 | flash_msgs[msg_pos++]+=" done"; |
||
1015 | } |
||
1016 | |||
1017 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
||
1018 | params=new MKParamsParser(); |
||
1019 | try{ |
||
1020 | writer.write('E'); |
||
1021 | writer.flush(); |
||
1022 | } |
||
1023 | catch (Exception e) { |
||
1024 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
||
1025 | } |
||
1026 | flash_msgs[msg_pos++]="Exit BL done" ; |
||
1027 | |||
224 | ligi | 1028 | bootloader_finish_ok=true; |
206 | ligi | 1029 | } |
1030 | |||
1031 | catch (Exception e) { |
||
1032 | flash_msgs[msg_pos++]="Fail:" +e.getMessage() ; |
||
1033 | |||
1034 | |||
1035 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
||
1036 | params=new MKParamsParser(); |
||
1037 | try{ |
||
1038 | writer.write('E'); |
||
1039 | writer.flush(); |
||
1040 | } |
||
1041 | catch (Exception e2) { |
||
1042 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
||
1043 | } |
||
1044 | flash_msgs[msg_pos++]="Exit BL done" ; |
||
219 | ligi | 1045 | if (sigfail&&(bl_retrys<3)) |
1046 | { |
||
1047 | bl_retrys++; |
||
1048 | init_bootloader=true; |
||
1049 | } |
||
1050 | close_connections(false); |
||
206 | ligi | 1051 | } |
1052 | |||
1053 | |||
1054 | sending=false; |
||
1055 | } |
||
1056 | |||
1057 | |||
1058 | byte[] data_set=new byte[1024]; |
||
1059 | int data_set_pos=0; |
||
1060 | byte[] data_in_buff=new byte[2048]; |
||
1061 | |||
1062 | int input; |
||
1063 | int pos=0; |
||
1064 | |||
1065 | |||
1066 | |||
1067 | |||
1068 | |||
1069 | log("Thread started"); |
||
1070 | while(true) |
||
1071 | { |
||
1072 | |||
1073 | if (!connected) |
||
1074 | { |
||
1075 | if (!force_disconnect) connect(); |
||
1076 | sleep(100); |
||
1077 | } |
||
1078 | else |
||
1079 | try{ |
||
1080 | |||
1081 | /* |
||
1082 | while(sending) |
||
1083 | {try { Thread.sleep(50); } |
||
1084 | catch (Exception e) { } |
||
1085 | } |
||
1086 | */ |
||
1087 | |||
1088 | recieving=true; |
||
1089 | int read_count =reader.read(data_in_buff,0,reader.available()); |
||
1090 | log("Connected - reading data " + read_count); |
||
1091 | // pos=0; |
||
1092 | input=0; |
||
1093 | //data_buff[data_buff_pos]=""; |
||
1094 | // recieve data-set |
||
1095 | if (read_count==0) sleep(50); |
||
1096 | |||
1097 | // int read_count =reader.read(data_in_buff,0,reader.available()); |
||
1098 | stats.bytes_in+=read_count; |
||
1099 | if (read_count>0) |
||
1100 | { |
||
1101 | log("read" + read_count + " ds_pos" + data_set_pos); |
||
1102 | |||
1103 | for ( pos=0;pos<read_count;pos++) |
||
1104 | { |
||
1105 | if (data_in_buff[pos]==13) |
||
1106 | { |
||
233 | ligi | 1107 | data_buff[data_buff_pos]=new String(data_set, 0, data_set_pos); |
206 | ligi | 1108 | data_buff_pos++; |
1109 | data_buff_pos%=DATA_BUFF_LEN; |
||
1110 | |||
1111 | |||
1112 | try{process_data(data_set,data_set_pos); } |
||
1113 | catch (Exception e) |
||
1114 | { |
||
1115 | log(".. problem processing"); |
||
1116 | log(e.toString()); |
||
1117 | } |
||
1118 | |||
1119 | |||
1120 | |||
1121 | |||
1122 | proxy.write(data_set,0,data_set_pos); |
||
1123 | // proxy.writer.write('\r'); |
||
1124 | //proxy.writer.write('\n'); |
||
1125 | //proxy.writer.flush(); |
||
1126 | /* |
||
1127 | if (proxy!=null) |
||
1128 | { |
||
1129 | |||
1130 | |||
1131 | |||
1132 | } |
||
1133 | */ |
||
1134 | data_set_pos=0; |
||
1135 | |||
1136 | } |
||
1137 | else |
||
1138 | { |
||
1139 | data_set[data_set_pos++]=data_in_buff[pos]; |
||
1140 | |||
1141 | |||
1142 | |||
1143 | 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')) |
||
1144 | { |
||
1145 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
||
1146 | return; |
||
1147 | } |
||
1148 | |||
1149 | } |
||
1150 | |||
1151 | } |
||
1152 | |||
1153 | |||
1154 | } |
||
1155 | else |
||
1156 | sleep(50); |
||
1157 | /* |
||
1158 | while ((input != 13)) //&&(input!=-1)) |
||
1159 | { |
||
1160 | { |
||
1161 | //log("pre read"); |
||
1162 | log(""+reader.available()); |
||
1163 | input = reader.read() ; |
||
1164 | log("Byte rcv" + input +"pos"+ pos); |
||
1165 | |||
1166 | proxy.write(input); |
||
1167 | |||
1168 | data_buff[data_buff_pos]+=(char)input; |
||
1169 | |||
1170 | 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"))) |
||
1171 | { |
||
1172 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
||
1173 | return; |
||
1174 | } |
||
1175 | if (input==-1) throw new Exception("disconnect"); |
||
1176 | else |
||
1177 | { |
||
1178 | stats.bytes_in++; |
||
1179 | data_set[pos]=input; |
||
1180 | pos++; |
||
1181 | } |
||
1182 | } |
||
1183 | |||
1184 | } |
||
1185 | |||
1186 | |||
1187 | |||
1188 | data_buff_pos++; |
||
1189 | data_buff_pos%=DATA_BUFF_LEN; |
||
1190 | recieving=false; |
||
1191 | log("Data recieved (" + pos + "Bytes)"); |
||
1192 | log("processing .."); |
||
1193 | */ |
||
1194 | |||
1195 | /* |
||
1196 | if (proxy!=null) |
||
1197 | { |
||
1198 | proxy.writer.write('\r'); |
||
1199 | proxy.writer.write('\n'); |
||
1200 | proxy.writer.flush(); |
||
1201 | } |
||
1202 | */ |
||
1203 | /*if (pos>5) |
||
1204 | { |
||
1205 | try{process_data(data_set,pos); } |
||
1206 | catch (Exception e) |
||
1207 | { |
||
1208 | log(".. problem processing"); |
||
1209 | log(e.toString()); |
||
1210 | } |
||
1211 | |||
1212 | log(".. processing done"); |
||
1213 | } |
||
1214 | */ |
||
1215 | } |
||
1216 | catch (Exception ex) |
||
1217 | { |
||
1218 | log("Problem reading from MK -> closing conn"); |
||
1219 | log(ex.toString()); |
||
1220 | // close the connection |
||
1221 | close_connections(false); |
||
1222 | } |
||
1223 | |||
1224 | // sleep a bit to get someting more done |
||
1225 | // sleep(5); //50 |
||
1226 | |||
1227 | } // while |
||
1228 | // log("Leaving Communicator thread"); |
||
1229 | } // run() |
||
1230 | } |