Subversion Repositories Projects

Rev

Rev 233 | Rev 264 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 233 Rev 262
1
/**************************************
1
/**************************************
2
 *
2
 *
3
 * WatchDog for MK-Connection
3
 * WatchDog for MK-Connection
4
 *                      
4
 *                      
5
 * Author:        Marcus -LiGi- Bueschleb
5
 * Author:        Marcus -LiGi- Bueschleb
6
 *
6
 *
7
 * see README for further Infos
7
 * see README for further Infos
8
 *
8
 *
9
 *
9
 *
10
 
10
 
11
 **************************************/
11
 **************************************/
12
package org.ligi.ufo;
12
package org.ligi.ufo;
13
 
13
 
14
public class MKWatchDog
14
public class MKWatchDog
15
    implements Runnable,DUBwiseDefinitions
15
    implements Runnable,DUBwiseDefinitions
16
{
16
{
17
    MKCommunicator mk=null;
17
    MKCommunicator mk=null;
18
 
18
 
19
    int debug_data_count_buff=-123;
19
    int debug_data_count_buff=-123;
20
 
20
 
21
    public MKWatchDog(MKCommunicator _mk)
21
    public MKWatchDog(MKCommunicator _mk)
22
    {
22
    {
23
 
23
 
24
        mk=_mk;
24
        mk=_mk;
25
        new Thread( this ).start(); // fire up main Thread 
25
        new Thread( this ).start(); // fire up main Thread 
26
    }
26
    }
27
 
27
 
28
 
28
 
29
    public int act_paramset=0;
29
    public int act_paramset=0;
30
    int conn_check_timeout=0;
30
    int conn_check_timeout=0;
31
 
31
 
32
 
32
 
33
    public byte resend_timeout=0;
33
    public byte resend_timeout=0;
34
    int last_count=0;
34
    int last_count=0;
35
 
35
 
36
 
36
 
37
    public boolean resend_check(int ref_count)
37
    public boolean resend_check(int ref_count)
38
    {
38
    {
39
        if (( last_count!=ref_count)||(resend_timeout<0))
39
        if (( last_count!=ref_count)||(resend_timeout<0))
40
            {
40
            {
41
                if (resend_timeout<0) mk.stats.resend_count++;
41
                if (resend_timeout<0) mk.stats.resend_count++;
42
                last_count=ref_count;
42
                last_count=ref_count;
43
                resend_timeout=20;
43
                resend_timeout=20;
44
                return true;
44
                return true;
45
            }
45
            }
46
        else
46
        else
47
            resend_timeout--;
47
            resend_timeout--;
48
       
48
       
49
        return false;
49
        return false;
50
    }
50
    }
51
 
51
 
52
//#ifdef android
52
//#ifdef android
53
//    public final static int BASE_SLEEP=50;
53
//    public final static int BASE_SLEEP=50;
54
//#else
54
//#else
55
    public final static int BASE_SLEEP=10;
55
    public final static int BASE_SLEEP=10;
56
//#endif
56
//#endif
57
 
57
 
58
    public void run()
58
    public void run()
59
    {
59
    {
60
        mk.log("starting Watchdog");           
60
        mk.log("starting Watchdog");           
61
        // get all params
61
        // get all params
62
        int act_debug_name=0;
62
        int act_debug_name=0;
63
        //      int sleeper=BASE_SLEEP;
63
        //      int sleeper=BASE_SLEEP;
64
        while(true)
64
        while(true)
65
            {
65
            {
66
                try {
66
                try {
67
                    Thread.sleep(BASE_SLEEP);
67
                    Thread.sleep(BASE_SLEEP);
68
                    //              sleeper=BASE_SLEEP;
68
                    //              sleeper=BASE_SLEEP;
69
                    if (mk.connected&&(!mk.force_disconnect))
69
                    if (mk.connected&&(!mk.force_disconnect))
70
                        {
70
                        {
71
 
71
 
72
                            mk.log("watchdog pre main loop");          
72
                            //                      mk.log("watchdog pre main loop");           
73
                            if (mk.init_bootloader)
73
                            if (mk.init_bootloader)
74
                                {
74
                                {
75
                                    mk.jump_bootloader();
75
                                    mk.jump_bootloader();
76
                                    mk.init_bootloader=false;
76
                                    mk.init_bootloader=false;
77
                                }
77
                                }
78
                            else if  ( mk.version.major==-1 )
78
                            else if  ( mk.version.major==-1 )
79
                                    mk.get_version();
79
                                    mk.get_version();
80
                            else if (mk.ufo_prober.is_navi()&&(mk.error_str==null))
80
                            else if (mk.ufo_prober.is_navi()&&(mk.error_str==null))
81
                                    mk.get_error_str();
81
                                    mk.get_error_str();
82
 
82
 
83
                            else if (mk.ufo_prober.is_mk()&&(mk.params.last_parsed_paramset==-1))
83
                            else if (mk.ufo_prober.is_mk()&&(mk.params.last_parsed_paramset==-1))
84
                                {
84
                                {
85
                                    mk.get_params(0xFF-1);
85
                                    mk.get_params(0xFF-1);
86
                                    Thread.sleep(150);
86
                                    Thread.sleep(150);
87
 
87
 
88
                                    act_paramset=0; // warning -  if dropped problem
88
                                    act_paramset=0; // warning -  if dropped problem
89
                                }
89
                                }
90
                            else   switch (mk.user_intent)
90
                            else   switch (mk.user_intent)
91
                                {
91
                                {
92
                                case USER_INTENT_PARAMS:
92
                                case USER_INTENT_PARAMS:
93
 
93
 
94
                                    if ((act_paramset<5))
94
                                    if ((act_paramset<5))
95
                                    {
95
                                    {
96
 
96
 
97
                                        if (resend_timeout==0)  {
97
                                        if (resend_timeout==0)  {
98
                                            mk.get_params(act_paramset);
98
                                            mk.get_params(act_paramset);
99
                                            resend_timeout=120;
99
                                            resend_timeout=120;
100
                                        }
100
                                        }
101
                                       
101
                                       
102
                                        if(mk.params.field[act_paramset]!=null)
102
                                        if(mk.params.field[act_paramset]!=null)
103
                                            {
103
                                            {
104
                                                mk.get_params(++act_paramset);
104
                                                mk.get_params(++act_paramset);
105
                                                resend_timeout=120;
105
                                                resend_timeout=120;
106
                                            }
106
                                            }
107
                                        else
107
                                        else
108
                                            resend_timeout--;
108
                                            resend_timeout--;
109
                                            /*
109
                                            /*
110
                                                //                                              act_paramset++;
110
                                                //                                              act_paramset++;
111
                                        else
111
                                        else
112
                                            mk.get_params(act_paramset);
112
                                            mk.get_params(act_paramset);
113
                                       
113
                                       
114
                                        sleeper+=1200;
114
                                        sleeper+=1200;
115
                                            */
115
                                            */
116
                                    }
116
                                    }
117
                                    break;
117
                                    break;
118
                                case USER_INTENT_RAWDEBUG:
118
                                case USER_INTENT_RAWDEBUG:
119
                                     if (act_debug_name<32)
119
                                     if (act_debug_name<32)
120
                                         {                                           
120
                                         {                                           
121
 
121
 
122
                                             if (resend_timeout==0)  {
122
                                             if (resend_timeout==0)  {
123
                                                 mk.get_debug_name(act_debug_name);
123
                                                 mk.get_debug_name(act_debug_name);
124
                                                 resend_timeout=50;
124
                                                 resend_timeout=50;
125
                                             }
125
                                             }
126
                                             
126
                                             
127
                                             
127
                                             
128
                                             //sleeper+=100;
128
                                             //sleeper+=100;
129
                                             if (mk.debug_data.got_name[act_debug_name])
129
                                             if (mk.debug_data.got_name[act_debug_name])
130
                                                 {
130
                                                 {
131
                                                     mk.get_debug_name(++act_debug_name);
131
                                                     mk.get_debug_name(++act_debug_name);
132
                                                     resend_timeout=50;
132
                                                     resend_timeout=50;
133
                                                 }
133
                                                 }
134
                                             else
134
                                             else
135
                                                 resend_timeout--;
135
                                                 resend_timeout--;
136
 
136
 
137
                                         }
137
                                         }
138
                                     else
138
                                     else
139
                                         if (!(mk.debug_data.got_name[0]))
139
                                         if (!(mk.debug_data.got_name[0]))
140
                                             act_debug_name=0;
140
                                             act_debug_name=0;
141
 
141
 
142
                                     break;
142
                                     break;
143
 
143
 
144
                                case USER_INTENT_RCDATA:
144
                                case USER_INTENT_RCDATA:
145
                                    if ( resend_check(mk.stats.stick_data_count) )
145
                                    if ( resend_check(mk.stats.stick_data_count) )
146
                                        mk.trigger_rcdata();
146
                                        mk.trigger_rcdata();
147
                                    break;
147
                                    break;
148
 
148
 
149
                                case USER_INTENT_EXTERNAL_CONTROL:
149
                                case USER_INTENT_EXTERNAL_CONTROL:
150
                                    if (resend_check(mk.stats.external_control_confirm_frame_count))
150
                                    if (resend_check(mk.stats.external_control_confirm_frame_count))
151
                                        {
151
                                        {
152
                                            mk.send_extern_control();
152
                                            mk.send_extern_control();
153
                                            mk.send_extern_control();
153
                                            mk.send_extern_control();
154
                                            mk.send_extern_control();
154
                                            mk.send_extern_control();
155
                                            mk.send_extern_control();
155
                                            mk.send_extern_control();
156
                                        }
156
                                        }
157
 
157
 
158
                                    break;
158
                                    break;
159
 
159
 
160
                                   
160
                                   
161
                                case USER_INTENT_LCD:
161
                                case USER_INTENT_LCD:
162
                                    if (resend_check(mk.stats.lcd_data_count))
162
                                    if (resend_check(mk.stats.lcd_data_count))
163
                                            mk.LCD.trigger_LCD();
163
                                            mk.LCD.trigger_LCD();
164
 
164
 
165
                                    break;
165
                                    break;
-
 
166
 
-
 
167
                                case USER_INTENT_GRAPH:
-
 
168
                                    mk.set_debug_interval(mk.debug_buff_interval);
166
 
169
                                    break;
167
                                default:
170
                                default:
168
                                    //                              mk.log("uncactched intent " +mk.root.canvas.user_intent );          
171
                                    //                              mk.log("uncactched intent " +mk.root.canvas.user_intent );          
169
                                    break;
172
                                    break;
170
                                }
173
                                }
171
 
174
 
172
                            //                  if ((!mk.ufo_prober.is_incompatible()))
175
                            //                  if ((!mk.ufo_prober.is_incompatible()))
173
                            {/*
176
                            {/*
174
                               
177
                               
175
                                mk.log("watchdog after main loop");            
178
                                mk.log("watchdog after main loop");            
176
                                if (debug_data_count_buff==mk.stats.debug_data_count)
179
                                if (debug_data_count_buff==mk.stats.debug_data_count)
177
                                    {
180
                                    {
178
                                        mk.log("timeout:" +conn_check_timeout );               
181
                                        mk.log("timeout:" +conn_check_timeout );               
179
                                        conn_check_timeout++;
182
                                        conn_check_timeout++;
180
                                        if  (conn_check_timeout==1000)
183
                                        if  (conn_check_timeout==1000)
181
                                            {
184
                                            {
182
                                                mk.log("disconnecting");               
185
                                                mk.log("disconnecting");               
183
                                                mk.close_connections(false);
186
                                                mk.close_connections(false);
184
                                                conn_check_timeout=0;
187
                                                conn_check_timeout=0;
185
                                            }
188
                                            }
186
                                        debug_data_count_buff=mk.stats.debug_data_count;
189
                                        debug_data_count_buff=mk.stats.debug_data_count;
187
                                    }
190
                                    }
188
                                else
191
                                else
189
                                    conn_check_timeout=0;
192
                                    conn_check_timeout=0;
190
                               
193
                               
191
 
194
 
192
                             */
195
                             */
193
                                /*else  if ((mk.root.canvas.user_intent==USER_INTENT_RAWDEBUG) || (mk.root.canvas.user_intent==USER_INTENT_GRAPH) )
196
                                /*else  if ((mk.root.canvas.user_intent==USER_INTENT_RAWDEBUG) || (mk.root.canvas.user_intent==USER_INTENT_GRAPH) )
194
                                  {
197
                                  {
195
                                  mk.trigger_debug();
198
                                  mk.trigger_debug();
196
                                  try { Thread.sleep(100); }
199
                                  try { Thread.sleep(100); }
197
                                  catch (Exception e)  {   }
200
                                  catch (Exception e)  {   }
198
                                  }
201
                                  }
199
                                */  
202
                                */  
200
 
203
 
201
               
204
               
202
           
205
           
203
 
206
 
204
 
207
 
205
 
208
 
206
                                /*
209
                                /*
207
 
210
 
208
 
211
 
209
                                if (debug_data_count_buff==mk.debug_data_count)
212
                                if (debug_data_count_buff==mk.debug_data_count)
210
                                {
213
                                {
211
                                //                              mk.close_connections(false);
214
                                //                              mk.close_connections(false);
212
                                }
215
                                }
213
 
216
 
214
                                */
217
                                */
215
 
218
 
216
                                /*if (mk.version.major==-1)
219
                                /*if (mk.version.major==-1)
217
                                  mk.get_version();
220
                                  mk.get_version();
218
                                  else
221
                                  else
219
                                */  
222
                                */  
220
                                /*for ( int cnt=0;cnt<5;cnt++)
223
                                /*for ( int cnt=0;cnt<5;cnt++)
221
                                  if (mk.params.field[cnt]==null)
224
                                  if (mk.params.field[cnt]==null)
222
                                  {
225
                                  {
223
                                  mk.get_params(cnt+1);
226
                                  mk.get_params(cnt+1);
224
                                  break;
227
                                  break;
225
                                  }*/
228
                                  }*/
226
                                /*
229
                                /*
227
                                  for (int c=0;c<32;c++)
230
                                  for (int c=0;c<32;c++)
228
                                  if (!mk.debug_data.got_name[c])
231
                                  if (!mk.debug_data.got_name[c])
229
                                  {
232
                                  {
230
                                  mk.get_debug_name(c);
233
                                  mk.get_debug_name(c);
231
                                  break;
234
                                  break;
232
                                  }
235
                                  }
233
                                */
236
                                */
234
 
237
 
235
                       
238
                       
236
                            }
239
                            }
237
                       
240
                       
238
                        }
241
                        }
239
               
242
               
240
 
243
 
241
                } // 3000
244
                } // 3000
242
                catch (Exception e)  {                 
245
                catch (Exception e)  {                 
243
                    mk.log("err in watchdog:");          
246
                    mk.log("err in watchdog:");          
244
                    mk.log(e.toString());                
247
                    mk.log(e.toString());                
245
 
248
 
246
                }
249
                }
247
            }
250
            }
248
 
251
 
249
 
252
 
250
        //      mk.log("watchdog quit");                
253
        //      mk.log("watchdog quit");                
251
    }
254
    }
252
 
255
 
253
   
256
   
254
}
257
}
255
 
258