summaryrefslogtreecommitdiffstats
path: root/mig_test/software/monitor.c
blob: c5131e679056258ee1f9022164fcb5d8e627479b (plain)
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
/*
 * $HeadURL: https://svn.fzd.de/repo/concast/FWF_Projects/FWKE/hw_sp605/bsp_zpuahb/software/monitor.c $
 * $Date$
 * $Author$
 * $Revision$
 */

#include "monitor.h"

////////////////////////////////////////////////////////////
//  monitor functions

static char          command_list[MAX_COMMANDS][MAX_COMMAND_LENGTH];
static char          help_list   [MAX_COMMANDS][MAX_HELP_LENGTH];
static command_ptr_t command_ptr_list[MAX_COMMANDS];

uint8_t       buffer[BUFFER_LENGTH];
uint8_t       command_number;
uint8_t       buffer_position;
command_ptr_t exec_function;



/*
    reset some values for monitor
*/
void monitor_init( void)
{
    buffer_position = 0;
    command_number  = 0;
    exec_function   = 0;
}


/*
    add an command to the monitor list
*/
void monitor_add_command(char* new_command, char* new_help, command_ptr_t new_command_ptr) 
{
    if (command_number < MAX_COMMANDS)
    {
        strcpy( command_list[ command_number], new_command);
        strcpy( help_list[    command_number], new_help);
        command_ptr_list[ command_number] = new_command_ptr;
        command_number++;
    }
    else
    {
        putstr("ERROR: too much commands.\n");
    }
}


/*
    print out a nice promt
*/
void monitor_prompt( void) { 
    putstr("> ");
}


/*
    check the line buffer content and search command
*/
void process_buffer( void) {
    uint8_t command_index;
    uint8_t i;

    i = 0;

    while ( !((buffer[i] == ' ') || (buffer[i] == 0)) ) i++;
    
    if (!i) {
        monitor_prompt();
        return;
    }

    for ( command_index = 0; command_index < command_number; command_index++) {
        if ( !strncmp( command_list[ command_index], buffer, i) ) {
            exec_function = command_ptr_list[ command_index];
            return;
        }
    }
    putstr("command not found.\n");
    monitor_prompt();
}


uint8_t monitor_run;


/*
    execute the command
*/
void monitor_mainloop( void) 
{
    uint32_t return_value;

    // execute selected function 
    if (exec_function) {
        return_value = exec_function();
        exec_function = 0;
        
        // print return value (as hex)
        if (return_value > 0)
        {
            putstr("0x");
            if (return_value > 0xffff)
                puthex(32, return_value);
            else
                if (return_value > 0xff)
                    puthex(16, return_value);
                else
                    puthex(8, return_value);
            putchar('\n');
        }
        
        monitor_prompt();
    }
}


/*
    add an character to the monitor line buffer
*/
void monitor_input(uint8_t c) {
   
    // carrige return
    if (c == CR) {
        putchar( LF);
        buffer[ buffer_position++] = 0;
        process_buffer();
        buffer_position = 0;

    // backspace or delete
    } else if ( (c == BS) || (c == DEL)) {
        if (buffer_position > 0) {
            putchar( BS);
            putchar( ' ');
            putchar( BS);
            buffer_position--;
        }
    } else {
        // add to buffer
        if ((c >= 0x20) && (buffer_position < (BUFFER_LENGTH-1))) {
            putchar( c);
            buffer[ buffer_position++] = c;
        }
    }
}


/*
    parse the argument as string
*/
char* monitor_get_argument_string(uint8_t num)
{
    uint8_t index;
    uint8_t arg;

    // example line:
    // "   command   arg1   arg2 arg3 "

    index = 0;

    // search for first char (non space)
    while (( buffer[ index] != 0) && (buffer[ index] == ' ')) index++;

    for ( arg = 0; arg < num; arg++)
    {
        // next space 
        while (( buffer[ index] != 0) && (buffer[ index] != ' ')) index++;
        // next non space
        while (( buffer[ index] != 0) && (buffer[ index] == ' ')) index++;
    }
    return &buffer[ index];
}


/*
    parse the argument as integer
*/
int monitor_get_argument_int(uint8_t num)
{
    char *endptr;
    return strtol( monitor_get_argument_string(num), &endptr, 0);
}


/*
    parse the argument as hex number
*/
uint32_t monitor_get_argument_hex(uint8_t num)
{
    char *endptr;
    return strtoul( monitor_get_argument_string(num), &endptr, 16);
}



/*
    print all avalible functions as help screen
*/
uint32_t help_function( void)
{
    uint8_t command_index;
    uint8_t i;

    putchar( LF);
    putstr("supported commands:\n\n");
    for ( command_index = 0; command_index < command_number; command_index++) {
        putstr( command_list[ command_index]); 
        if (strlen( help_list[ command_index]) > 0 )
        {
            for (i = strlen( command_list[ command_index]); i < MAX_COMMAND_LENGTH; i++) putchar(' ');
            putstr( " - ");
            putstr( help_list[ command_index]); 
        }
        putchar('\n');
    }
    putchar( LF);
    return command_number;
}


OpenPOWER on IntegriCloud