Subversion Repositories Projects

Rev

Rev 70 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 70 Rev 81
Line 11... Line 11...
11
 
11
 
12
import javax.microedition.io.*;
12
import javax.microedition.io.*;
Line 13... Line 13...
13
import java.io.*;
13
import java.io.*;
14
 
14
 
15
public class MKCommunicator
15
public class MKCommunicator
16
   implements Runnable
16
    implements Runnable
17
{
17
{
18
    /***************** Section: public Attributes **********************************************/
18
    /***************** Section: public Attributes **********************************************/
Line 23... Line 23...
23
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
23
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
Line 24... Line 24...
24
   
24
   
25
    public MKLCD LCD;
25
    public MKLCD LCD;
26
    public MKVersion version;
26
    public MKVersion version;
-
 
27
    public MKDebugData debug_data;
27
    public MKDebugData debug_data;
28
    public MKStickData stick_data;
28
    public MKParamsParser params;
29
    public MKParamsParser params;
29
    public MKWatchDog watchdog;
30
    public MKWatchDog watchdog;
Line 30... Line 31...
30
    public MKProxy proxy=null;
31
    public MKProxy proxy=null;
Line 50... Line 51...
50
 
51
 
51
 
52
 
-
 
53
 
52
 
54
    String name;
53
    String name;
55
    DUBwise root;
54
    DUBwise root;
56
    DUBwiseDebug debug;
55
   
57
 
-
 
58
    /******************  Section: public Methods ************************************************/
56
    /******************  Section: public Methods ************************************************/
59
    public MKCommunicator(DUBwise root_,DUBwiseDebug debug_)  
57
    public MKCommunicator(DUBwise root_)  
60
    {
58
    {
61
        debug=debug_;
-
 
62
        root=root_;
59
        root=root_;
63
        version=new MKVersion();
60
        version=new MKVersion();
64
        debug_data=new MKDebugData();
61
        debug_data=new MKDebugData();
65
        stick_data=new MKStickData();
62
        params=new MKParamsParser();
66
        params=new MKParamsParser();
63
        LCD= new MKLCD(this);
67
        LCD= new MKLCD(this);
Line 83... Line 87...
83
    /******************  Section: private Methods ************************************************/
87
    /******************  Section: private Methods ************************************************/
84
    private void connect()
88
    private void connect()
85
    {
89
    {
86
        System.out.println("trying to connect to" + mk_url);
90
        System.out.println("trying to connect to" + mk_url);
87
        try{
91
        try{
88
                connection = (StreamConnection) Connector.open(mk_url);
92
            connection = (StreamConnection) Connector.open(mk_url);
89
                // old call
93
            // old call
90
                // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
94
            // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
91
                reader=connection.openInputStream();
95
            reader=connection.openInputStream();
92
                writer=connection.openOutputStream();
96
            writer=connection.openOutputStream();
-
 
97
 
-
 
98
 
-
 
99
            //
-
 
100
            String magic="\rmk-mode\r";
-
 
101
            writer.write(magic.getBytes());
-
 
102
            writer.flush();
-
 
103
            //
-
 
104
 
93
               
105
 
94
                connection_start_time=System.currentTimeMillis();
106
            connection_start_time=System.currentTimeMillis();
95
                connected=true; // if we get here everything seems to be OK
107
            connected=true; // if we get here everything seems to be OK
96
                get_version();
108
            get_version();
97
                lcd_data_count=0;
109
            lcd_data_count=0;
98
                debug_data_count=0;
110
            debug_data_count=0;
99
                version_data_count=0;
111
            version_data_count=0;
Line 100... Line 112...
100
 
112
 
101
 
113
 
102
           }
114
        }
103
        catch (Exception ex)
115
        catch (Exception ex)
104
            {
116
            {
105
                // TODO difference fatal errors from those which will lead to reconnection
117
                // TODO difference fatal errors from those which will lead to reconnection
Line 190... Line 202...
190
    }
202
    }
Line 191... Line 203...
191
 
203
 
192
 
204
 
193
 
205
 
Line 194... Line 206...
194
 
206
 
Line 241... Line 253...
241
                send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f));
253
                send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f));
Line 242... Line 254...
242
 
254
 
243
                //send_buff[3+foo]='=';
255
                //send_buff[3+foo]='=';
Line 244... Line 256...
244
            }
256
            }
245
 
257
 
246
/*      for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++)
258
        /*      for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++)
247
            {
259
                {
Line 248... Line 260...
248
                int a = (foo<params.length) params[foo];
260
                int a = (foo<params.length) params[foo];
249
                int a = params[foo];
261
                int a = params[foo];
250
               
262
               
251
                //send_buff[3+foo]='=';
263
                //send_buff[3+foo]='=';
252
            }
264
                }
253
*/
265
        */
254
        try
266
        try
255
            {
267
            {
Line 273... Line 285...
273
    }
285
    }
Line 274... Line 286...
274
 
286
 
275
 
287
 
276
    public void process_data(int[] data,int len)
-
 
Line 277... Line -...
277
    {
-
 
278
        int[] decoded_data;
288
    public void process_data(int[] data,int len)
279
 
289
    {
Line 280... Line 290...
280
       
290
 
281
        switch((char)data[2])
291
        switch((char)data[2])
Line 298... Line 308...
298
               
308
               
299
            case '0':
309
            case '0':
300
            case '1':
310
            case '1':
301
            case '2':
311
            case '2':
302
            case '3':
-
 
303
                LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0');
312
            case '3':
-
 
313
                lcd_data_count++;
-
 
314
                LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0');
-
 
315
 
-
 
316
                break;
-
 
317
            case '4':
-
 
318
                stick_data.set_by_mk_data(Decode64(data,3,20));
-
 
319
                String tmp_s="";
-
 
320
                for (int tmp_c=0;tmp_c<10;tmp_c++)
-
 
321
                    tmp_s+="s"+tmp_c+"v"+stick_data.stick[tmp_c]+" ";
304
                lcd_data_count++;
322
                debug.log(tmp_s);
Line 305... Line 323...
305
                break;
323
                break;
306
 
324
 
307
            case 'L':
325
            case 'L':
Line 313... Line 331...
313
                params_data_count++;
331
                params_data_count++;
314
                break;
332
                break;
Line 315... Line 333...
315
 
333
 
316
            default:
334
            default:
-
 
335
                other_data_count++;
-
 
336
                debug.log("got other data:" + (char)data[2] + "=>" + (byte)data[2]);
-
 
337
 
-
 
338
 
-
 
339
                String tmp_str="";
-
 
340
                for (int tmp_i=0;tmp_i<len;tmp_i++)
-
 
341
                    tmp_str+=(char)data[tmp_i];
317
                other_data_count++;
342
                debug.log(tmp_str);
Line 318... Line 343...
318
                break;
343
                break;
Line 351... Line 376...
351
        int input;
376
        int input;
352
        int pos=0;
377
        int pos=0;
353
        msg+="!!run started!!";
378
        msg+="!!run started!!";
354
        while(true)
379
        while(true)
355
            {
380
            {
356
            if (!connected)
381
                if (!connected)
357
                {
382
                    {
358
                   if (!force_disconnect) connect();
383
                        if (!force_disconnect) connect();
359
                }
384
                    }
360
            else
385
                else
361
                try{
386
                    try{
Line 362... Line 387...
362
               
387
               
363
                    pos=0;
388
                        pos=0;
364
                    input=0;
389
                        input=0;
365
                    // recieve data-set
390
                        // recieve data-set
366
                    while ((input != 13)) // &&(input!=-1))
391
                        while ((input != 13)) // &&(input!=-1))
367
                       {
392
                            {
368
 
393
 
369
                           input = reader.read() ;
394
                                input = reader.read() ;
370
                           if (proxy!=null)
395
                                if (proxy!=null)
371
                               proxy.writer.write(input);
396
                                    proxy.writer.write(input);
-
 
397
                                //                              if (input==-1) throw new Exception("test");
-
 
398
                                if (input!=-1)
372
                           if (input==-1) throw new Exception("test");
399
                                    {
373
                           data_set[pos]=input;
400
                                        data_set[pos]=input;
-
 
401
                                        pos++;
Line 374... Line 402...
374
                           pos++;
402
                                    }
Line 375... Line 403...
375
     
403
     
376
                       }
404
                            }
377
 
405
 
378
                           if (proxy!=null)
406
                        if (proxy!=null)
379
                               {
407
                            {
380
                                proxy.writer.write('\r');
408
                                proxy.writer.write('\r');
Line -... Line 409...
-
 
409
                                proxy.writer.write('\n');
381
                                proxy.writer.write('\n');
410
                                proxy.writer.flush();
Line 382... Line 411...
382
                                proxy.writer.flush();
411
                            }
383
                               }
412
 
384
 
413
                        if (pos>5)
385
                    process_data(data_set,pos);
414
                            process_data(data_set,pos);
-
 
415
                   
386
                   
416
                    }
387
                   }
417
                    catch (Exception ex)
Line 388... Line 418...
388
        catch (Exception ex)
418
                        {
Line 389... Line 419...
389
            {
419
                            debug.log("Problem reading from MK -> closing conn");
390
                msg="Problem reading from MK";
420
                            debug.log(ex.toString());
391
                // close the connection 
421
                            // close the connection 
Line 392... Line 422...
392
                close_connections(false);
422
                            close_connections(false);
Line 393... Line 423...
393
 
423