OpenCores
URL https://opencores.org/ocsvn/igor/igor/trunk

Subversion Repositories igor

[/] [igor/] [trunk/] [avr/] [eth-test/] [uip/] [shell.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
 /*
2
 * Copyright (c) 2003, Adam Dunkels.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. The name of the author may not be used to endorse or promote
14
 *    products derived from this software without specific prior
15
 *    written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * This file is part of the uIP TCP/IP stack.
30
 *
31
 * $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
32
 *
33
 */
34
 
35
#include "shell.h"
36
#include <device.h>
37
#include <stdio.h>
38
#include <string.h>
39
#include <ctype.h>
40
#include "../dev/mmc.h"
41
 
42
 
43
static struct {
44
        struct buf readbuf;
45
        struct buf writebuf;
46
} telnet_data;
47
 
48
static igordev_read_fn_t  telnet_recv;
49
static igordev_write_fn_t telnet_send;
50
static igordev_init_fn_t  init;
51
static igordev_flush_fn_t telnet_flush;
52
void hexdump(uint8_t * p, uint16_t len);
53
 
54
struct igordev igordev_telnet = {
55
        .init = init,
56
        .read = telnet_recv,
57
        .write = telnet_send,
58
        .flush = telnet_flush,
59
        .read_status = 0,
60
        .write_status = 0,
61
        .priv = &telnet_data
62
};
63
 
64
void init() {
65
        volatile struct req *req;
66
        buf_init(&telnet_data.readbuf);
67
        buf_init(&telnet_data.writebuf);
68
        igordev_telnet.read_status = igordev_telnet.write_status = IDEV_STATUS_OK;
69
}
70
 
71
/*
72
 * Read num bytes from device and place it into data.
73
 * Data assumed to be a buffer large enough for num bytes.
74
 * Addr here is ignored.
75
 */
76
uint8_t
77
telnet_recv(uint64_t addr, uint8_t *data, uint8_t num)
78
{
79
        struct buf *buf;
80
        uint8_t byte;
81
        uint8_t i;
82
 
83
        buf = &telnet_data.readbuf;
84
 
85
        for (i = 0; i < num; i++) {
86
                buf_read(buf, &byte, 1);
87
                if (BUF_EMPTY(buf))
88
                        break;
89
                *(data + i) = byte;
90
        }
91
        return i;
92
}
93
 
94
/*
95
 * Read from data to device
96
 */
97
uint8_t
98
telnet_send(uint64_t addr, uint8_t *data, uint8_t num)
99
{
100
        struct buf *buf;
101
        uint8_t i = 0;
102
 
103
        if (num > 0 && data != NULL) {
104
                /* Copy data into write buffer. */
105
                buf = &telnet_data.writebuf;
106
                i = buf_write(buf, data, num);
107
        }
108
        return i;
109
}
110
 
111
/* Do nothing */
112
static void
113
telnet_flush(void)
114
{
115
        struct buf *buf;
116
        buf = &telnet_data.writebuf;
117
        char *data;
118
        int8_t i = 0;
119
 
120
        igordev_telnet.write_status = IDEV_STATUS_BUSY;
121
        /* Flush as long as it is ok. */
122
        while (!BUF_EMPTY(buf)) {
123
                buf_read(buf, data, (uint8_t)buf_readsleft(buf));
124
                i++;
125
        }
126
 
127
        *(data + i) = '\0';
128
 
129
        shell_output(data, "");
130
 
131
        igordev_telnet.write_status = IDEV_STATUS_OK;
132
}
133
 
134
 
135
struct ptentry {
136
  char *commandstr;
137
  void (* pfunc)(char *str);
138
};
139
 
140
#define COMMAND_MODE 0
141
#define LISP_MODE 1
142
#define SHELL_PROMPT "IGOR> "
143
#define LISP_PROMPT "LISP> "
144
 
145
uint8_t mode = COMMAND_MODE;
146
 
147
/*---------------------------------------------------------------------------*/
148
static void
149
parse(register char *str, struct ptentry *t)
150
{
151
  struct ptentry *p;
152
  for(p = t; p->commandstr != NULL; ++p) {
153
    if(strncmp(p->commandstr, str, strlen(p->commandstr)) == 0) {
154
      break;
155
    }
156
  }
157
 
158
  p->pfunc(str);
159
}
160
/*---------------------------------------------------------------------------*/
161
static void
162
inttostr(register char *str, unsigned int i)
163
{
164
  str[0] = '0' + i / 100;
165
  if(str[0] == '0') {
166
    str[0] = ' ';
167
  }
168
  str[1] = '0' + (i / 10) % 10;
169
  if(str[0] == ' ' && str[1] == '0') {
170
    str[1] = ' ';
171
  }
172
  str[2] = '0' + i % 10;
173
  str[3] = ' ';
174
  str[4] = 0;
175
}
176
/*---------------------------------------------------------------------------*/
177
static void
178
help(char *str)
179
{
180
  shell_output("Available commands:", "");
181
  shell_output("help, ?    - show help", "");
182
  shell_output("+, -       - read SD card", "");
183
  shell_output("lisp       - switch to lisp mode", "");
184
  shell_output("exit       - exit shell", "");
185
}
186
 
187
static void
188
switch_lisp(char *str)
189
{
190
        shell_output("Switching to Lisp mode", "");
191
        mode = LISP_MODE;
192
}
193
 
194
uint32_t lba = 0;
195
 
196
void
197
read_sd(void)
198
{
199
        char lbastr[5];
200
        inttostr(lbastr, lba);
201
 
202
        shell_output("Reading SD card: Sector", lbastr);
203
 
204
        uint8_t data[512];
205
 
206
        if (mmc_readsector(lba, data) == 0)
207
                hexdump(data, 512);
208
        else
209
                shell_output("Failed to read that sector", "");
210
}
211
 
212
 
213
static void
214
sd_plus(char *str)
215
{
216
        lba++;
217
        read_sd();
218
}
219
 
220
static void
221
sd_minus(char *str)
222
{
223
        if (lba > 0)
224
                lba--;
225
        read_sd();
226
}
227
 
228
/*---------------------------------------------------------------------------*/
229
static void
230
unknown(char *str)
231
{
232
  if(strlen(str) > 0) {
233
    shell_output("Unknown command: ", str);
234
  }
235
}
236
/*---------------------------------------------------------------------------*/
237
static struct ptentry parsetab[] =
238
  {{"lisp", switch_lisp},
239
   {"+", sd_plus},
240
   {"-", sd_minus},
241
   {"help", help},
242
   {"exit", shell_quit},
243
   {"?", help},
244
 
245
   /* Default action */
246
   {NULL, unknown}};
247
/*---------------------------------------------------------------------------*/
248
void
249
shell_init(void)
250
{
251
//FIXME remove, this is for testing only
252
mmc_init();
253
}
254
/*---------------------------------------------------------------------------*/
255
void
256
shell_start(void)
257
{
258
        mode = COMMAND_MODE;
259
        shell_output("Greetings and salutations, sir or madam.", "");
260
        shell_output("Welcome to the IGOR command shell!", "");
261
        shell_output("How may I serve you today?", "");
262
        shell_output("Type '?' and return for help.", "");
263
        shell_prompt(SHELL_PROMPT);
264
}
265
/*---------------------------------------------------------------------------*/
266
void
267
shell_input(char *cmd)
268
{
269
        if (mode == COMMAND_MODE) {
270
                parse(cmd, parsetab);
271
        }
272
        if (mode == COMMAND_MODE) {
273
                shell_prompt(SHELL_PROMPT);
274
        } else {
275
                struct buf *buf;
276
                buf = &telnet_data.readbuf;
277
                buf_write(buf, cmd, strlen(cmd));
278
                shell_prompt(LISP_PROMPT);
279
        }
280
}
281
/*---------------------------------------------------------------------------*/
282
void hexdump(uint8_t * p, uint16_t len)
283
{
284
        int i,j;
285
 
286
        for (i=0;i<len/16;i++)
287
        {
288
                char str0[70] = "";
289
 
290
                char str1[5];
291
                sprintf(str1, "%03x ",i*16);
292
                strcat(str0, str1);
293
                for (j=0;j<16;j++) {
294
                        char str2[5];
295
                        sprintf(str2, "%02x ",p[i*16+j]);
296
                        strcat(str0, str2);
297
                }
298
                for (j=0;j<16;j++) {
299
                        char str3[5];
300
                        sprintf(str3, "%c", isalpha(p[i*16+j]) ? p[i*16+j] : '.');
301
                        strcat(str0, str3);
302
                }
303
                shell_output(str0, "");
304
        }
305
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.