-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_consol.c
354 lines (310 loc) · 10.6 KB
/
task_consol.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
/*
* task_consol.c
*
*/
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "stm32f10x_iwdg.h"
#include "main.h"
#include "consol.h"
#include "queue_buf.h"
#include "env_config.h"
#include "heap_z.h"
#include "uip.h"
#include "i2c.h"
#include "printf_hal.h"
#include "task_uip.h"
#include "uptime.h"
#include "time_hal.h"
extern int table_mu[];
extern int table_di[];
extern const int k1;
extern const int k2;
extern struct uip_eth_addr mac_addr;
extern device_status_t device_status;
extern void task_list( void );
extern struct queue_buffer consol_rx;
extern const char *git_commit_str;
int ip_test(const char * s);
void print_byte_to_bit(uint8_t b);
#define CONSOL_BUF_SIZE (50)
char consol_buf[ CONSOL_BUF_SIZE ];
volatile consol_state_e consol_state;
////////////////////////////////////////////////////////////////////////////////
// îáðàáîòêà äàíííûõ ïðèøåäøèõ ñ êîíñîëè
////////////////////////////////////////////////////////////////////////////////
void vTaskConsol( void *pvParameters )
{
unsigned char consol_buf_n = 0;
char *adr = 0;
uint32_t step = 0;
char c;
int i;
uint8_t rec; // íîìåð çàïèñè â ñòðóêòóðå äàííûõ env_config
uptime_data_t ut; // âðåìÿ ðàáîòû ñèñòåìû
consol_state = OUT_PROMT;
reset_queue( &consol_rx );
step++;//out promt
while(1){
step = 0;
switch(consol_state){
case GET_CHAR:
if ( datalen_queue( &consol_rx ) == 0 ){
vTaskDelay(200);
step = 0;
break;
}
pop_data_queue_b( &consol_rx , (unsigned char *)&c );
if ( ((c == KEY_ENTER) || (c == KEY_BACKSPACE)) && (consol_buf_n == 0) ){ // ïóñòàÿ ñòðîêà
step++;
consol_state = OUT_PROMT;
break;
}
if ( c == KEY_BACKSPACE ){
consol_buf[ consol_buf_n ] = '\0';
printf_dos("\r%s",promt);
i = consol_buf_n;
while(i > 0){
printf_dos(" ");
i--;
}
printf_dos("\r");
consol_buf_n--;
consol_buf[ consol_buf_n ] = '\0';
printf_dos("%s%s",promt,consol_buf);
break;
}
if ( c == KEY_ENTER ){
consol_buf[ consol_buf_n ] = '\0';
adr = strchr(consol_buf, '=');
if (adr == NULL)
consol_state = CMD_ANALYS_LOW;
else
consol_state = CMD_ANALYS_SET;
step++;
if (c == 0x0d)
printf_dos("\r\n"); // äåëàåì ïåðåâîä ñòðîêè ÷òîá îòâåòû øëè íà÷èíàÿ ñ íîâîé ñòðîêè
break;
}
//putchar(c);// no sem ?!
printf_dos("%c",c); // vivodim otvetniy simvol
if (consol_buf_n < CONSOL_BUF_SIZE ){ // äîáàâëåíèå ñèìâîëà â áóôåð
consol_buf[ consol_buf_n ] = c;
consol_buf_n++;
}else{
consol_state = OUT_ERROR;
step++;
break;
}
break;
case OUT_PROMT:
consol_state = GET_CHAR;
consol_buf_n = 0;
printf_dos("\n\r%s",promt);
break;
case OUT_ERROR:
consol_buf_n = 0;
printf_dos("%s",error);
consol_state = OUT_PROMT;
step++;
break;
case CMD_ANALYS_LOW:
if (strcmp(consol_buf, cmd_help1)== 0 || strcmp(consol_buf, cmd_help2)==0){
printf_dos("%s",txt_help);
consol_state = OUT_PROMT;
step++;
break;
}
if (strcmp(consol_buf, cmd_view)== 0){
printf_dos("================================================================================\n");
uptime(time_get_sec_counter(), &ut);
printf_dos("System uptime: %02d Years, %03d Days, %02d:%02d:%02d\n", ut.years, ut.days, ut.hours, ut.min, ut.sec );
printf_dos("================================================================================\n");
printf_dos("MAC : ");
for(i=0;i<6;i++){
printf_dos("%02X%c", mac_addr.addr[i], i==5 ? ' ' : '-');
}
printf_dos("\n");
print_env();
printf_dos("---------------GPIO IN------------------------------------------------\n\r");
printf_dos("INPUT = 0x%02x\n\r",device_status.gpio_input);
printf_dos("7 6 5 4 3 2 1 0\n\r");
print_byte_to_bit(device_status.gpio_input);
printf_dos("\n\r---------------GPIO OUT-----------------------------------------------\n\r");
printf_dos("OUTPUT = 0x%02x\n\r",device_status.gpio_output);
printf_dos("7 6 5 4 3 2 1 0\n\r");
print_byte_to_bit(device_status.gpio_output);
printf_dos("\n\r---------------ADC 12 bit---------------------------------------------\n\r");
printf_dos("| ADC code: | U adc input: | K= mul/div | Uinput = Uadc * K |\n\r");
printf_dos("+-------------+---------------------+------------+-------------------+\n\r");
char *buf_p = NULL;
buf_p = (char*) pvPortMalloc( BUF_P_SIZE );
if (buf_p != NULL){
for(i=0;i<ADC_CHANNEL_QUANTITY;i++){
calc_volt_rdiv(buf_p, device_status.adc_converted_value[i], k1, k2, 1, 1);
printf_dos("| ADC(0x%04X) | U adc(%d) = %6s v | ", device_status.adc_converted_value[i], i, buf_p);
calc_volt_rdiv(buf_p, 1, 1, 1, table_mu[i], table_di[i]);
printf_dos("K = %6s | ", buf_p);
calc_volt_rdiv(buf_p, device_status.adc_converted_value[i], k1, k2, table_mu[i], table_di[i]);
printf_dos("U rdiv = %6s v |\n", buf_p);
}//for
vPortFree( buf_p );
}else printf_dos("ERROR: task_list: Malloc.\r\n");
printf_dos("----------------------------------------------------------------------\n\r");
step++;
consol_state = OUT_PROMT;
break;
}
if (strcmp(consol_buf, cmd_version)== 0 ){
printf_dos("%s\n\r",txt_device_name);
printf_dos("%s\n\r",txt_device_ver_soft);
printf_dos("%s\n\r",txt_device_ver_hard);
printf_dos("GIT version = %s\r\n",git_commit_str);
step++;
consol_state = OUT_PROMT;
break;
}
if (strcmp(consol_buf, cmd_root)== 0 ){
printf_dos("%s\n\r",txt_root_help);
step++;
consol_state = OUT_PROMT;
break;
}
if (strcmp(consol_buf, cmd_reboot)== 0 ){
printf_dos("\n\rReboot CPU...\n\r");
portDISABLE_INTERRUPTS();
SysTick->CTRL = 0;// îñòàíîâèëè ñèñòåìíûé òàéìåð òåïåðü äîëæåí ñðàáîòàòü âà÷äîã
while(1);
// step++;
// consol_state = OUT_PROMT;
break;
}
// êîìàíäà íå íàéäåíà
step++;
consol_state = OUT_ERROR;
break;
case CMD_ANALYS_SET:
// çà ìåñòî = â áóôåð ïèøèì êîíåö ñòðîêè, ÷òîá ìîæíî áûëî ïîëíîöåííî ñðàâíèòü ñòðîêè
consol_buf[ adr - consol_buf ] = '\0';
adr++; //óêàçûâàåò íà ïîñëå =
if (strcmp(consol_buf, cmd_sn)== 0){
if( (strlen(adr) != SN_LEN) && (strcspn(adr, STR_CIFRA) != 0) ){
step++;
consol_state = OUT_ERROR;
break;
}
step++;
rec = R_SN;//flash_w_adr = (char*)&device_config.sn;
consol_state = WRITE_CFG;
break;
}
if (strcmp(consol_buf, cmd_txt)== 0){
if ( strlen(adr) < TXT_LEN ){
rec = R_TXT;
consol_state = WRITE_CFG;
}else consol_state = OUT_ERROR;
step++;
break;
}
if (strcmp(consol_buf, cmd_ipdevice)== 0){
if ( strlen(adr) < SETIPLAN_LEN && ip_test(adr) == 0 ){
rec = R_IP_DEVICE;
consol_state = WRITE_CFG;
}else consol_state = OUT_ERROR;
step++;
break;
}
if (strcmp(consol_buf, cmd_ipgw)== 0){
if ( strlen(adr) < SETIPLAN_LEN && ip_test(adr) == 0 ){
rec = R_IP_GW;
consol_state = WRITE_CFG;
}else consol_state = OUT_ERROR;
step++;
break;
}
if (strcmp(consol_buf, cmd_ipmask)== 0){
if ( strlen(adr) < SETIPLAN_LEN && ip_test(adr) == 0 ){
rec = R_IP_MASK;
consol_state = WRITE_CFG;
}else consol_state = OUT_ERROR;
step++;
break;
}
// êîìàíäà íå íàéäåíà
step++;
consol_state = OUT_ERROR;
break;
case WRITE_CFG: // ñîõðàíåíèå ïàðàìåòðà ñ ñòðóêòóðå à çàòåì è flash
printf_dos("Write configure&setup device -> enc_config. . .\r\n");
if (env_write_value(rec, adr) != 0 )
printf_dos("ERROR save env_config.\n\r");
step++;
consol_state = OUT_PROMT;
break;
}
}
}
//------------------------------------------------------------------------------
// test s - string for 111.222.333.444
//
// return 0 -> IP OK
// return !0 -> IP ERROR
//
//------------------------------------------------------------------------------
int ip_test(const char * s)
{
size_t len = 0; // strlen(s)
size_t i = 0; // index
size_t m = 0; // index for st
char c; // char from S
int t = 0; // kolichestvo tochek
char st[4]; // sobiraem stroku dla convertachii str->int
int n = 0; // n = atoi(st)
// string to NULL
if (s == 0) return 1;
len = strlen(s);
// string LEN = 0
if (len == 0) return 1;
// string LEN > 192.168.130.100
if (len > 15) return 1;
i = 0;
while( 1 ){
c = *(s + i);
// esli Ne chislo i ne Tochka -> error
if ( !(c >= 0x30 && c <= 0x39) && c != '.' && c != 0) return 1;
if (c >= 0x30 && c <= 0x39){
if (m == 3) return 2; // eto budet 4 chislo -> error
st[m] = c;
m++;
st[m] = 0;
}
if (c == '.' || c == 0){
if (m == 0) return 3; // poluchili podrad vtoruu Tochku -> error
n = atoi(st);
if (n > 255) return 4;
st[0] = 0;
m = 0;
if (c == 0) break;
else t++;
}
i++;
}
if (t != 3) return 5;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Âûâîäèì áàéò ïî áèòàì íà êîíñîëü
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void print_byte_to_bit(uint8_t b)
{
uint8_t i;
for (i=0x80; i!=0; i=i>>1)
if ( (b & i) > 0)
printf_dos("1 ");
else
printf_dos("0 ");
}