File: reset.c

package info (click to toggle)
scantool 1.21+dfsg-5
  • links: PTS
  • area: main
  • in suites: sid
  • size: 2,956 kB
  • ctags: 2,392
  • sloc: ansic: 12,279; sh: 829; makefile: 344; asm: 86; perl: 29
file content (362 lines) | stat: -rw-r--r-- 10,693 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <string.h>

#include "reset.h"
#include "globals.h"
#include "custom_gui.h"
#include "main_menu.h"
#include "serial.h"


static int reset_proc(int msg, DIALOG *d, int c);

static char reset_status_msg[64];


static DIALOG reset_chip_dialog[] =
{
   /* (proc)            (x)  (y) (w)  (h) (fg)     (bg)          (key) (flags) (d1) (d2) (dp)              (dp2) (dp3) */
   { d_shadow_box_proc, 0,   0,  340, 64, C_BLACK, C_LIGHT_GRAY, 0,    0,      0,   0,   NULL,             NULL, NULL },
   { caption_proc,      170, 24, 158, 16, C_BLACK, C_TRANSP,     0,    0,      0,   0,   reset_status_msg, NULL, NULL },
   { reset_proc,        0,   0,  0,   0,  0,       0,            0,    0,      0,   0,   NULL,             NULL, NULL },
   { d_yield_proc,      0,   0,  0,   0,  0,       0,            0,    0,      0,   0,   NULL,             NULL, NULL },
   { NULL,              0,   0,  0,   0,  0,       0,            0,    0,      0,   0,   NULL,             NULL, NULL }
};


void reset_chip()
{
   centre_dialog(reset_chip_dialog);
   popup_dialog(reset_chip_dialog, -1);
}


enum ResetState
{
   RESET_SEND_RESET_REQUEST,
   RESET_GET_REPLY_TO_RESET,
   RESET_SEND_AT_AT1_REQUEST,
   RESET_GET_AT_AT1_RESPONSE,
   RESET_SEND_AT_AT2_REQUEST,
   RESET_GET_AT_AT2_RESPONSE,
   RESET_START_ECU_TIMER,
   RESET_WAIT_FOR_ECU_TIMEOUT,
   RESET_SEND_DETECT_PROTOCOL_REQUEST,
   RESET_GET_REPLY_TO_DETECT_PROTOCOL,
   RESET_CLOSE_DIALOG,
   RESET_INTERFACE_NOT_FOUND,
   RESET_HANDLE_CLONE
};


int Reset_send_reset_request(char *response)
{
   char buf[128];
   
   if (serial_timer_running) // if serial timer is running
   {
      // wait until we either get a prompt or the timer times out
      while ((read_comport(buf) != PROMPT) && !serial_time_out)
         ;
   }
   
   send_command("atz"); // reset the chip
   start_serial_timer(ATZ_TIMEOUT);  // start serial timer
   response[0] = 0;
   
   return RESET_GET_REPLY_TO_RESET;
}


int Reset_get_reply_to_reset(char *response, int *device)
{
   char buf[128];
   int next_state;
   int status;
   
   status = read_comport(buf);                  // read comport
   
   if (status == DATA)                           // if new data detected in com port buffer
   {
      strcat(response, buf);                    // append contents of buf to response
      next_state = RESET_GET_REPLY_TO_RESET;    // come back for more data
   }
   else if (status == PROMPT) // if '>' detected
   {
      stop_serial_timer();
      strcat(response, buf);
      *device = process_response("atz", response);
      
      if (*device == INTERFACE_ELM323)
         next_state = RESET_START_ECU_TIMER;
      else if (*device == INTERFACE_ELM327)
         next_state = RESET_SEND_AT_AT1_REQUEST;
      else
         next_state = RESET_CLOSE_DIALOG;
   }
   else if (serial_time_out) // if the timer timed out
   {
      stop_serial_timer(); // stop the timer
      alert("Interface was not found", NULL, NULL, "OK", NULL, 0, 0);
      next_state = RESET_CLOSE_DIALOG; // close dialog
   }  
   else  // serial buffer was empty, but we still got time
      next_state = RESET_GET_REPLY_TO_RESET;
   
   return next_state;
}


int Reset_send_at_at1_request(char *response)
{
   send_command("at@1");
   start_serial_timer(AT_TIMEOUT);  // start serial timer
   response[0] = 0;
   
   return RESET_GET_AT_AT1_RESPONSE;
}


int Reset_get_at_at1_response(char *response)
{
   char buf[128];
   int next_state;
   int status;
   
   status = read_comport(buf);                  // read comport
   
   if (status == DATA)                          // if new data detected in com port buffer
   {
      strcat(response, buf);                    // append contents of buf to response
      next_state = RESET_GET_AT_AT1_RESPONSE;   // come back for more data
   }
   else if (status == PROMPT) // if '>' detected
   {
      stop_serial_timer();
      strcat(response, buf);
      status = process_response("at@1", response);
      
      if (status == STN_MFR_STRING)
         next_state = RESET_START_ECU_TIMER;
      else if (status == ELM_MFR_STRING)
         next_state = RESET_SEND_AT_AT2_REQUEST;
      else
         next_state = RESET_HANDLE_CLONE;
   }
   else if (serial_time_out)
   {
      stop_serial_timer();
      alert("Connection with interface lost", NULL, NULL, "OK", NULL, 0, 0);
      next_state = RESET_CLOSE_DIALOG; // close dialog
   }
   else  // serial buffer was empty, but we still got time
      next_state = RESET_GET_AT_AT1_RESPONSE;
   
   return next_state;
}


int Reset_send_at_at2_request(char *response)
{
   send_command("at@2");
   start_serial_timer(AT_TIMEOUT);  // start serial timer
   response[0] = 0;
   
   return RESET_GET_AT_AT2_RESPONSE;
}


int Reset_get_at_at2_response(char *response)
{
   char buf[128];
   int next_state;
   int status;
   
   status = read_comport(buf);                  // read comport
   
   if (status == DATA)                          // if new data detected in com port buffer
   {
      strcat(response, buf);                    // append contents of buf to response
      next_state = RESET_GET_AT_AT2_RESPONSE;   // come back for more data
   }
   else if (status == PROMPT) // if '>' detected
   {
      stop_serial_timer();
      strcat(response, buf);
      status = process_response("at@2", response);
      
      if (status == STN_MFR_STRING)
         next_state = RESET_START_ECU_TIMER;
      else
         next_state = RESET_HANDLE_CLONE;
   }
   else if (serial_time_out)
   {
      stop_serial_timer();
      alert("Connection with interface lost", NULL, NULL, "OK", NULL, 0, 0);
      next_state = RESET_CLOSE_DIALOG; // close dialog
   }
   else  // serial buffer was empty, but we still got time
      next_state = RESET_GET_AT_AT2_RESPONSE;
   
   return next_state;
}


int Reset_start_ecu_timer()
{
   start_serial_timer(ECU_TIMEOUT);
   return RESET_WAIT_FOR_ECU_TIMEOUT;
}


int Reset_wait_for_ecu_timeout(int device)
{
   int next_state;
   
   if (serial_time_out) // if the timer timed out
   {
      stop_serial_timer(); // stop the timer
      if (device == INTERFACE_ELM327)
         next_state = RESET_SEND_DETECT_PROTOCOL_REQUEST;
      else
         next_state = RESET_CLOSE_DIALOG;
   }
   else   
      next_state = RESET_WAIT_FOR_ECU_TIMEOUT;
   
   return next_state;
}


int Reset_send_detect_protocol_request(char *response)
{
   int next_state;
   
   send_command("0100");
   start_serial_timer(OBD_REQUEST_TIMEOUT);  // start serial timer
   response[0] = 0;
   next_state = RESET_GET_REPLY_TO_DETECT_PROTOCOL;
   
   return next_state;
}


int Reset_get_reply_to_detect_protocol(char *response)
{
   char buf[128];
   int next_state;
   int status;
   
   status = read_comport(buf);
   
   if(status == DATA) // if new data detected in com port buffer
   {
      strcat(response, buf); // append contents of buf to response
      next_state = RESET_GET_REPLY_TO_DETECT_PROTOCOL; // come back for more
   }
   else if (status == PROMPT)  // if we got the prompt
   {
      stop_serial_timer();
      strcat(response, buf);
      status = process_response("0100", response);

      if (status == ERR_NO_DATA || status == UNABLE_TO_CONNECT)
         alert("Protocol could not be detected.", "Please check connection to the vehicle,", "and make sure the ignition is ON", "OK", NULL, 0, 0);
      else if (status != HEX_DATA)
         alert("Communication error", buf, NULL, "OK", NULL, 0, 0);
      
      next_state = RESET_CLOSE_DIALOG;
   }
   else if (serial_time_out)
   {
      stop_serial_timer();
      alert("Connection with interface lost", NULL, NULL, "OK", NULL, 0, 0);
      next_state = RESET_CLOSE_DIALOG;
   }
   else
      next_state = RESET_GET_REPLY_TO_DETECT_PROTOCOL;
   
   return next_state;
}


/* We don't care if it's a clone; let it run anyway. */
int Reset_handle_clone()
{
   is_not_genuine_scan_tool = FALSE;
   return RESET_START_ECU_TIMER;
}


int reset_proc(int msg, DIALOG *d, int c)
{
   static int state = RESET_SEND_RESET_REQUEST;
   static int device = 0;
   static char response[256];
    
   switch(msg)
   {
      case MSG_START:
         state = RESET_SEND_RESET_REQUEST;
         strcpy(reset_status_msg, "Resetting hardware interface...");
         break;
   
      case MSG_IDLE:
         switch (state)
         {
            case RESET_SEND_RESET_REQUEST:
               strcpy(reset_status_msg, "Resetting hardware interface...");
               state = Reset_send_reset_request(response);
               return D_REDRAW;
               
            case RESET_GET_REPLY_TO_RESET:
               state = Reset_get_reply_to_reset(response, &device);
               break;
               
            case RESET_SEND_AT_AT1_REQUEST:
               strcpy(reset_status_msg, "Making sure scan tool is genuine...");
               state = Reset_send_at_at1_request(response);
               return D_REDRAW;
               
            case RESET_GET_AT_AT1_RESPONSE:
               state = Reset_get_at_at1_response(response);
               break;
               
            case RESET_SEND_AT_AT2_REQUEST:
               state = Reset_send_at_at2_request(response);
               break;
               
            case RESET_GET_AT_AT2_RESPONSE:
               state = Reset_get_at_at2_response(response);
               break;
               
            case RESET_START_ECU_TIMER:
               strcpy(reset_status_msg, "Waiting for ECU timeout...");
               state = Reset_start_ecu_timer();
               return D_REDRAW;
               
            case RESET_WAIT_FOR_ECU_TIMEOUT:
               state = Reset_wait_for_ecu_timeout(device);
               break;
               
            case RESET_SEND_DETECT_PROTOCOL_REQUEST:
               strcpy(reset_status_msg, "Detecting OBD protocol...");
               state = Reset_send_detect_protocol_request(response);
               return D_REDRAW;
               
            case RESET_GET_REPLY_TO_DETECT_PROTOCOL:
               state = Reset_get_reply_to_detect_protocol(response);
               break;
               
            case RESET_HANDLE_CLONE:
               state = Reset_handle_clone();
               break;
               
            case RESET_CLOSE_DIALOG:
               return D_CLOSE;
         }
         break;
   }
   
   return D_O_K;
}