-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.c
513 lines (381 loc) · 12.7 KB
/
machine.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "machine.h"
#include "stdio.h"
#include "string.h"
#define CB_TIMEOUT 2000
#define TIMEOUT_COUNTER_MAX 10
extern bool gsm_reinit_flag;
tgsm_uart gsm_uart;
void clear_gsm_uart(tgsm_uart *self)
{
memset(self -> buffer, 0, sizeof(self -> buffer));
self -> buffer_index = 0;
}
/**
* \brief Initialize Command Window
* \param[in] self: Pointer to command_window
* \param[in] table: Array of pointer for table
* \param[in] parent: Pointer to parent for current command_window
* \param[in] size : Size of given array of pointer table
* \param[in] is_repeatable
* \return error code
*/
error_code command_window_init(struct command_window *self, struct command** table, struct command_window* parent, uint8_t size, uint16_t is_repeatable, bool low_power_mode)
{
if (self == NULL)
{
gsm_machine_debug("NULL[command_window_init]");
return NULL_PTR;
}
self -> command_table = table;
self -> command_window_parent = parent;
self -> command_window_size = size;
self -> command_window_is_repetable = is_repeatable;
self -> low_power_flag = low_power_mode;
return OK;
}
/**
* \brief Show us details of given command window
* \param[in] self: Pointer to command machine
* \return error code
*/
error_code printf_command_window_details (const struct command_machine *self)
{
if (self == NULL)
{
gsm_machine_debug("NULL[printf_command_window_details]");
return NULL_PTR;
}
for (int i = 0; i < self -> current_command_window -> command_window_size; ++i)
{
gsm_machine_debug("\r\n Command Name = %s", self -> current_command_window -> command_table[i] -> command_name);
}
return OK;
}
/**
* \brief Go child window for each machine start
* \param[in] self: Pointer to command_machine
* \return error code
*/
error_code machine_start (struct command_machine* self)
{
if (self == NULL)
{
gsm_machine_debug("NULL[machine_start]");
return NULL_PTR;
}
gsm_machine_debug("\nMachine Start -> %s\r\n", self -> current_command_window -> command_table[self -> command_window_index] -> command_name);
self -> command_index = 0;
clear_gsm_uart(&gsm_uart);
if (self -> current_command_window -> command_table[self -> command_window_index] -> child_command_window != NULL)
{
self -> current_command_window = self -> current_command_window -> command_table[self -> command_window_index] -> child_command_window;
if (self -> current_command_window -> command_table[self -> command_index] -> command_cb != NULL)
{
self -> current_command_window -> command_table[self -> command_index] -> command_cb(self);
}
else
{
if (self -> uart_tx_cb)
{
self -> uart_tx_cb ((uint8_t *)self -> current_command_window -> command_table[self -> command_index] -> command_name,
strlen(self -> current_command_window -> command_table[self -> command_index] -> command_name));
}
}
self -> timer = 1;
return OK;
}
else
{
return NULL_CHILD;
}
}
//For systick proceses
static bool is_callback = false;
static int callback_timer = 0;
//Timeout Counter
static uint8_t timeout_counter = 0;
extern struct command_machine command_machine_t;
/**
* \brief Call this function inside of your systick!
* \param[in] self: Pointer to command_machine
* \return error code
*/
error_code machine_systick (struct command_machine* self)
{
if (self == NULL)
{
gsm_machine_debug("NULL[machine_systick]");
return NULL_PTR;
}
if (self -> timer)
{
self -> timer_cnt++;
if (gsm_uart.new_line_flag || self -> timer_cnt >= self -> current_command_window -> command_table[self -> command_index] -> timeout )
{
if ((strstr(gsm_uart.buffer, self -> current_command_window -> command_table[self -> command_index] -> expected_reply[0]) ||
strstr(gsm_uart.buffer, self -> current_command_window -> command_table[self -> command_index] -> expected_reply[1])) &&
gsm_uart.buffer_index > 0)
{
gsm_machine_debug("\n State Ok! = %s \n", self -> current_command_window -> command_table[self -> command_index] -> command_name);
timeout_counter = 0;
if(self -> current_command_window -> command_table[self -> command_index] -> success_cb != NULL)
{
self -> current_command_window -> command_table[self -> command_index] -> success_cb(self);
}
if (self -> current_command_window -> command_table[self -> command_index] -> parser_cb != NULL)
{
is_callback = true;
}
else
{
self -> command_index += 1;
self -> is_state_transition_available = 1;
}
}
else
{
if (self -> current_command_window -> command_table[self -> command_index] -> error_cb != NULL)
{
self -> current_command_window -> command_table[self -> command_index] -> error_cb(self);
}
gsm_machine_debug(" \n Timeout State! = %s \n", self -> current_command_window -> command_table[self -> command_index] -> command_name);
self -> is_state_transition_available = 1;
if (self -> command_window_index != 2)
{
timeout_counter++;
}
}
self -> timer = 0;
self -> timer_cnt = 0;
gsm_uart.new_line_flag = false;
}
}
if (timeout_counter >= TIMEOUT_COUNTER_MAX)
{
timeout_counter = 0;
gsm_machine_debug("\r\n Modem Reset Because Of Timeout_Counter Overflow \r\n");
self -> timer = 0;
self -> command_index = 0;
self -> command_window_index = 0;
self -> is_state_transition_available = 0;
while (self -> current_command_window -> command_window_parent != NULL)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
gsm_reinit_flag = true;
machine_start(&command_machine_t);
}
if (is_callback)
{
callback_timer++;
}
return OK;
}
/**
* \brief Call this function inside of your loop!
* \param[in] self: Pointer to command_machine
* \return error code
*/
extern bool reconnect;
error_code machine_loop (struct command_machine* self)
{
if (self == NULL)
{
gsm_machine_debug("NULL[machine_loop]");
return NULL_PTR;
}
if (self -> is_state_transition_available)
{
clear_gsm_uart(&gsm_uart);
self -> is_process = false;
if (self -> current_command_window -> command_window_size > self -> command_index)
{
if (self -> current_command_window -> command_table[self -> command_index] -> command_cb != NULL)
{
self -> current_command_window -> command_table[self -> command_index] -> command_cb(self);
}
else
{
if (self -> uart_tx_cb != NULL)
{
self -> uart_tx_cb((uint8_t *)self -> current_command_window -> command_table[self -> command_index] -> command_name,
strlen(self -> current_command_window -> command_table[self -> command_index] -> command_name));
}
}
self -> is_state_transition_available = 0;
self -> timer = 1;
self -> current_command_window -> command_window_status = 0;
}
/* it means, command window has just ended successfully, Now we can pass to parent of current window. Like linkedlist.*/
else
{
clear_gsm_uart(&gsm_uart);
self -> is_state_transition_available = 0;
self -> command_index = 0;
self -> timer = 0;
self -> current_command_window -> command_window_status = 1;
#ifdef LOW_POWER_MODE
if (self -> current_command_window -> low_power_flag == 1)
{
gsm_machine_debug("Entering Low Power Mode ASAP \r\n");
self -> low_power_cb(0);
}
#endif
while (self -> current_command_window -> command_window_parent != NULL)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
self -> is_process = true;
}
}
if (is_callback && callback_timer > CB_TIMEOUT && self -> current_command_window -> command_table[self -> command_index] -> parser_cb != NULL)
{
self -> current_command_window -> command_table[self -> command_index] -> parser_cb(self, gsm_uart.buffer, NULL);
is_callback = false;
clear_gsm_uart(&gsm_uart);
callback_timer = 0;
if (self -> current_command_window -> command_window_size == self -> command_index + 1)
{
self -> is_process = true;
while (self -> current_command_window -> command_window_parent != NULL)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
}
else
{
self -> command_index += 1;
self -> is_state_transition_available = 1;
}
}
return OK;
}
/**
* \brief [Not Impelemented]
* \param[in] self: Pointer to command_machine
* \return error code
*/
error_code machine_repeated_task (struct command_machine* self)
{
if (self == NULL)
{
gsm_machine_debug("NULL[machine_repeated_task]");
return NULL_PTR;
}
if (self -> is_all_command_completed)
{
while(self -> current_command_window -> command_window_parent != NULL)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
self -> is_all_command_completed = 0;
for (int i = 0; i < self -> current_command_window -> command_window_size; i++)
{
gsm_machine_debug("\r\n self -> current_command_window -> command name = %s", self -> current_command_window -> command_table[0] -> command_name);
if (self -> current_command_window -> command_table[i] -> child_command_window != NULL)
{
self -> current_command_window = self -> current_command_window -> command_table[i] -> child_command_window;
if (self -> current_command_window -> command_window_is_repetable)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
self -> command_window_index = i;
machine_start(self);
break;
}
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
}
}
return OK;
}
/**
* \brief You can change your process
* \param[in] self: Pointer to command_machine
* \param[in] tran_command_window_name: Name of the trans command_window_name's we want
* \return error code
*/
error_code machine_trans (struct command_machine* self, const char* tran_command_window_name)
{
if (self == NULL)
{
gsm_machine_debug("NULL[machine_trans]");
return NULL_PTR;
}
if (self -> is_process)
{
for (int i = 0; i < self -> current_command_window -> command_window_size; ++i)
{
gsm_machine_debug("Machine Trans = %s", self -> current_command_window -> command_table[i] -> command_name);
if (strstr(self -> current_command_window -> command_table[i] -> command_name, tran_command_window_name) != NULL)
{
self -> command_window_index = i;
machine_start(self);
break;
}
}
}
else
{
return IN_PROCESS;
}
return OK;
}
/**
* \brief Get current window
* \param[in] self: Pointer to command_machine
* \return error code
*/
struct command_window* get_current_window (struct command_machine* self)
{
static struct command_window* ret = NULL;
if (self == NULL)
{
gsm_machine_debug("NULL[get_current_window\r\n");
return NULL;
}
ret = self -> current_command_window;
return ret;
}
/**
* \brief get command window tatus
* \param[in] self: Pointer to command_machine
* \param[in] self: related command_window's name
* \return related command_window's status
*/
bool get_command_window_status(struct command_machine* self, const char* name)
{
bool status = false;
if (self -> is_process)
{
for (int i = 0; i < self -> current_command_window -> command_window_size; ++i)
{
gsm_machine_debug("Machine Trans = %s", self -> current_command_window -> command_table[i] -> command_name);
if (strstr(self -> current_command_window -> command_table[i] -> command_name, name) != NULL)
{
status = self -> current_command_window -> command_table[i] -> child_command_window -> command_window_status;
self -> current_command_window -> command_table[i] -> child_command_window -> command_window_status = false;
break;
}
}
}
return status;
}
/**
* \brief stop the machine
* \param[in] self: Pointer to command_machine
* \return OK
*/
error_code machine_stop_current (struct command_machine* self)
{
self -> current_command_window -> command_window_status = true;
while (self -> current_command_window -> command_window_parent != NULL)
{
self -> current_command_window = self -> current_command_window -> command_window_parent;
}
self -> is_process = true;
self -> timer = 0;
self -> command_index = 0;
self -> command_window_index = 0;
self -> is_state_transition_available = 0;
return OK;
}