-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_show.c
302 lines (262 loc) · 11.2 KB
/
api_show.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
/*
Licensed under the EUPL V.1.1, Lizenziert unter EUPL V.1.1
*/
/*
* api-show.c
*
* Created on: Apr 12, 2010
* Author: [email protected]
*/
#include <avr/pgmspace.h>
#include <avr/iocanxx.h>
#include <stdio.h>
#include <stdarg.h>
#include "api_define.h"
#include "api_global.h"
#include "api_debug.h"#include "mem-check.h"
#include "twi_master.h"
#include "api_show.h"
static const char filename[] PROGMEM = __FILE__;
/* max length defined by MAX_LENGTH_PARAMETER */
static const char showCommandKeyword00[] PROGMEM = "unused_mem_start";
static const char showCommandKeyword01[] PROGMEM = "unused_mem";
static const char showCommandKeyword02[] PROGMEM = "free_mem";
static const char showCommandKeyword03[] PROGMEM = "mem";
static const char showCommandKeyword04[] PROGMEM = "all_errors";
static const char showCommandKeyword05[] PROGMEM = "reset_source";
static const char showCommandKeyword06[] PROGMEM = "watchdog_counter";
//const showCommand_t showCommands[] PROGMEM =
//{
// { (int8_t(*)(struct uartStruct)) showFreeMemNow, showCommandKeyword00 },
// { (int8_t(*)(struct uartStruct)) showUnusedMemNow, showCommandKeyword01 },
// { (int8_t(*)(struct uartStruct)) showUnusedMemStart, showCommandKeyword02 }
//};
const char* const showCommandKeywords[] PROGMEM = {
showCommandKeyword00,
showCommandKeyword01,
showCommandKeyword02,
showCommandKeyword03,
showCommandKeyword04,
showCommandKeyword05,
showCommandKeyword06
};
int8_t show(struct uartStruct *ptr_uartStruct)
{
uint8_t index;
//int8_t (*func)(struct uartStruct);
//struct showCommand_t
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("show begin"));
switch(ptr_uartStruct->number_of_arguments)
{
case 0:
for (index = 0; index < commandShowKeyNumber_MAXIMUM_NUMBER; index++)
{
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("unused mem now %i "), get_mem_unused());
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("show all begin %i"),index);
ptr_uartStruct->number_of_arguments = 1;
for (uint8_t i = 0; i < MAX_LENGTH_PARAMETER; i++) {setParameter[1][i]=STRING_END;}
snprintf_P(setParameter[1],MAX_LENGTH_PARAMETER -1, (PGM_P) (pgm_read_word( &(showCommandKeywords[index]))));
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("recursive call of show with parameter \"%s\" (%p)"), &setParameter[1][0], &setParameter[1][0]);
show(ptr_uartStruct);
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("show all end %i"), index);
ptr_uartStruct->number_of_arguments = 0;
}
break;
case 1:
// find matching show keyword
index = apiFindCommandKeywordIndex(setParameter[1], showCommandKeywords, commandShowKeyNumber_MAXIMUM_NUMBER);
switch (index)
{
case commandShowKeyNumber_FREE_MEM_NOW:
case commandShowKeyNumber_UNUSED_MEM_NOW:
case commandShowKeyNumber_UNUSED_MEM_START:
if ( debugLevelEventDebug <= globalDebugLevel && ((globalDebugSystemMask >> debugSystemSHOW) & 0x1))
{
strncat_P(uart_message_string,(const char*) (pgm_read_word( &(showCommandKeywords[index]))),BUFFER_SIZE -1);
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("call showMem for: %s"), uart_message_string);
}
showMem(ptr_uartStruct, index);
break;
case commandShowKeyNumber_MEM:
createExtendedSubCommandReceiveResponseHeader(ptr_uartStruct, commandKeyNumber_SHOW, index, showCommandKeywords);
UART0_Send_Message_String_p(NULL,0);
showMem(ptr_uartStruct, commandShowKeyNumber_FREE_MEM_NOW);
showMem(ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_START);
showMem(ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_NOW);
break;
case commandShowKeyNumber_ALL_ERRORS:
showErrors(ptr_uartStruct, index);
break;
case commandShowKeyNumber_RESET_SOURCE:
showResetSource(FALSE);
break;
case commandShowKeyNumber_WATCHDOG_COUNTER:
showWatchdogIncarnationsCounter(FALSE);
break;
default:
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("call Communication_Error"));
CommunicationError_p(ERRA, dynamicMessage_ErrorIndex, FALSE, PSTR("show:invalid argument"));
return 1;
break;
}
break;
default:
ptr_uartStruct->number_of_arguments = 1;
show(ptr_uartStruct);
break;
}
printDebug_p(debugLevelEventDebug, debugSystemSHOW, __LINE__, filename, PSTR("show end"));
return 0;
}
int8_t showMem(struct uartStruct * ptr_uartStruct, uint8_t index)
{
unsigned short memory;
switch (index)
{
case commandShowKeyNumber_FREE_MEM_NOW:
memory = get_mem_free();
break;
case commandShowKeyNumber_UNUSED_MEM_NOW:
memory = get_mem_unused();
break;
case commandShowKeyNumber_UNUSED_MEM_START:
memory = unusedMemoryStart;
break;
default:
CommunicationError_p(ERRA, dynamicMessage_ErrorIndex, FALSE, PSTR("show:invalid argument"));
return 1;
break;
}
createExtendedSubCommandReceiveResponseHeader(ptr_uartStruct, commandKeyNumber_SHOW, index, showCommandKeywords);
snprintf_P(uart_message_string,BUFFER_SIZE -1, PSTR("%s%i"), uart_message_string, memory);
UART0_Send_Message_String_p(NULL,0);
return 0;
}
int8_t showFreeMemNow(struct uartStruct * ptr_uartStruct)
{
return showMem(ptr_uartStruct, commandShowKeyNumber_FREE_MEM_NOW);
}
int8_t showUnusedMemNow(struct uartStruct * ptr_uartStruct)
{
return showMem(ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_NOW);
}
int8_t showUnusedMemStart(struct uartStruct * ptr_uartStruct)
{
return showMem(ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_START);
}
/*
void help_show(void)
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** show (internal) settings"));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** command : SHOW [command_key]"));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** all response: RECV SHOW command_key1 ... "));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** ... ... "));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** RECV SHOW command_keyN ... "));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** cmd response: RECV SHOW command_key"));
UART0_Send_Message_String_p(NULL,0);
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** SHOW available command_keys"));
UART0_Send_Message_String_p(NULL,0);
for (int i=0; i < commandShowKeyNumber_MAXIMUM_NUMBER; i++)
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("RECV HELP *** "));
// strncat_P(uart_message_string, (PGM_P) pgm_read_word( &(showCommandKeywords[i])), MAX_LENGTH_PARAMETER) ;
// strncat_P(uart_message_string, showCommandKeywords[i], MAX_LENGTH_PARAMETER) ;
strncat_P(uart_message_string,(const char*) (pgm_read_word( &(showCommandKeywords[i]))),BUFFER_SIZE -1);
// strncat_P(uart_message_string, (const char*) (pgm_read_word( &(showCommandKeywords[i]))), MAX_LENGTH_PARAMETER) ;
// strncmp_P(&setParameter[1][0], (const char*) (pgm_read_word( &(showCommandKeywords[index]))), MAX_LENGTH_PARAMETER);
UART0_Send_Message_String_p(NULL,0);
}
}
*/
void showErrors(struct uartStruct * ptr_uartStruct, uint8_t index)
{
unsigned char errmax[6];
errmax[ERRA] = SERIAL_ERROR_MAXIMUM_INDEX;
errmax[ERRC] = CAN_ERROR_MAXIMUM_INDEX;
errmax[ERRG] = GENERAL_ERROR_MAXIMUM_INDEX;
errmax[ERRM] = MOB_ERROR_MAXIMUM_INDEX;
errmax[ERRT] = TWI_ERROR_MAXIMUM_INDEX;
errmax[ERRU] = 0;
createExtendedSubCommandReceiveResponseHeader(ptr_uartStruct, commandKeyNumber_SHOW, index, showCommandKeywords);
strncat_P(uart_message_string,PSTR("following all default error messages:"),BUFFER_SIZE -1);
for (int errType=0; errType < ERR_MAXIMUM_NUMBER; errType++)
{
for (int err=0; err < errmax[errType]; err++)
{
CommunicationError_p(errType, err ,FALSE ,PSTR("SHOW messages"));
}
}
}
/*
* void showResetSource(uint8_t startup_flag)
* shows the reset source after an system (re)start
* the response header varies depending on startup_flag
* FALSE: RECV SHOW reset_source <message> MCUSR:0x<value>
* else: SYST <message> MCUSR:0x<value>
*/
void showResetSource(uint8_t startup_flag)
{
// The MCU Status Register provides information on which reset source caused an MCU reset.
// Bit 0 – PORF: Power-On Reset
// Bit 1 – EXTRF: External Reset
// Bit 2 – BORF: Brown-Out Reset
// Bit 3 – WDRF: Watchdog Reset
// Bit 4 – JTRF: JTAG Reset Flag
// This bit is set if a reset is being caused
// by a logic one in the JTAG Reset Register selected by
// the JTAG instruction AVR_RESET. This bit is reset by
// a Power-on Reset, or by writing a logic zero to the flag.
if ( FALSE == startup_flag ) /*called by SHOW command*/
{
createExtendedSubCommandReceiveResponseHeader(ptr_uartStruct, commandKeyNumber_SHOW, commandShowKeyNumber_RESET_SOURCE, showCommandKeywords);
strncat_P(uart_message_string, PSTR("- "), BUFFER_SIZE - 1);
}
else /* called at startup*/
{
strncat_P(uart_message_string, (const char*) ( pgm_read_word( &(responseKeywords[responseKeyNumber_SYST])) ), BUFFER_SIZE - 1);
strncat_P(uart_message_string, PSTR(" system (re)started by: "), BUFFER_SIZE - 1);
}
switch(resetSource)
{
case resetSource_WATCHDOG:
strncat_P(uart_message_string, PSTR("Watchdog Reset"), BUFFER_SIZE - 1);
break;
case resetSource_JTAG:
strncat_P(uart_message_string, PSTR("JTAG Reset"), BUFFER_SIZE - 1);
break;
case resetSource_BROWN_OUT:
strncat_P(uart_message_string, PSTR("Brown Out Reset"), BUFFER_SIZE - 1);
break;
case resetSource_EXTERNAL:
strncat_P(uart_message_string, PSTR("External Reset"), BUFFER_SIZE - 1);
break;
case resetSource_POWER_ON:
strncat_P(uart_message_string, PSTR("Power On Reset"), BUFFER_SIZE - 1);
break;
case resetSource_UNKNOWN_REASON:
default:
strncat_P(uart_message_string, PSTR("unknown reset reason"), BUFFER_SIZE - 1);
break;
}
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s (MCUSR: %#x)"),uart_message_string, mcusr );
UART0_Send_Message_String_p(NULL,0);
}
void showWatchdogIncarnationsCounter(uint8_t startup_flag)
{
if ( FALSE == startup_flag ) /*called by SHOW command*/
{
createExtendedSubCommandReceiveResponseHeader(ptr_uartStruct, commandKeyNumber_SHOW, commandShowKeyNumber_WATCHDOG_COUNTER, showCommandKeywords);
}
else /* called at startup*/
{
strncat_P(uart_message_string, PSTR("SYST watch dog incarnation no.: "), BUFFER_SIZE - 1 );
}
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s%i"), uart_message_string, watchdogIncarnationsCounter);
UART0_Send_Message_String_p(NULL,0);
}