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

Subversion Repositories igor

[/] [igor/] [trunk/] [avr/] [src/] [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 <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
//Initialize device
65
void init() {
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
        /* Avoid making larger buffers for now. */
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
/* Flush buffered data to shell output */
112
static void
113
telnet_flush(void)
114
{
115
        struct buf *buf;
116
        buf = &telnet_data.writebuf;
117
        uint8_t data[2];
118
 
119
        data[1] = '\0';
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, 1);
124
                shell_output((char *)data, "");
125
        }
126
        igordev_telnet.write_status = IDEV_STATUS_OK;
127
}
128
 
129
 
130
struct ptentry {
131
  char *commandstr;
132
  void (* pfunc)(char *str);
133
};
134
 
135
#define COMMAND_MODE 0
136
#define LISP_MODE 1
137
#define SHELL_PROMPT "IGOR> "
138
 
139
uint8_t mode = COMMAND_MODE;
140
 
141
/*---------------------------------------------------------------------------*/
142
static void
143
parse(register char *str, struct ptentry *t)
144
{
145
  struct ptentry *p;
146
  for(p = t; p->commandstr != NULL; ++p) {
147
    if(strncmp(p->commandstr, str, strlen(p->commandstr)) == 0) {
148
      break;
149
    }
150
  }
151
 
152
  p->pfunc(str);
153
}
154
/*---------------------------------------------------------------------------*/
155
static void
156
inttostr(register char *str, unsigned int i)
157
{
158
  str[0] = '0' + i / 100;
159
  if(str[0] == '0') {
160
    str[0] = ' ';
161
  }
162
  str[1] = '0' + (i / 10) % 10;
163
  if(str[0] == ' ' && str[1] == '0') {
164
    str[1] = ' ';
165
  }
166
  str[2] = '0' + i % 10;
167
  str[3] = ' ';
168
  str[4] = 0;
169
}
170
/*---------------------------------------------------------------------------*/
171
static void
172
help(char *str)
173
{
174
  shell_output("Available commands:", "");
175
  shell_output("help, ?    - show help", "");
176
  shell_output("+, -       - read SD card", "");
177
  shell_output("lisp       - switch to lisp mode", "");
178
  shell_output("exit       - exit shell", "");
179
}
180
 
181
static void
182
switch_lisp(char *str)
183
{
184
        shell_output("Not yet implemented!", "");
185
//      mode = LISP_MODE;
186
}
187
 
188
uint32_t lba = 0;
189
 
190
//Read one sector from the memory card and dump it to the shell
191
void
192
read_sd(void)
193
{
194
        char lbastr[5];
195
        inttostr(lbastr, lba);
196
 
197
        shell_output("Reading SD card: Sector", lbastr);
198
 
199
        uint8_t data[512];
200
 
201
        if (mmc_readsector(lba, data) == 0)
202
                hexdump(data, 512);
203
        else
204
                shell_output("Failed to read that sector", "");
205
}
206
 
207
//Increase sector number by one and dump that sector
208
static void
209
sd_plus(char *str)
210
{
211
        lba++;
212
        read_sd();
213
}
214
 
215
//Decrease sector number by one and dump that sector
216
static void
217
sd_minus(char *str)
218
{
219
        if (lba > 0)
220
                lba--;
221
        read_sd();
222
}
223
 
224
/*---------------------------------------------------------------------------*/
225
static void
226
unknown(char *str)
227
{
228
  if(strlen(str) > 0) {
229
    shell_output("Unknown command: ", str);
230
  }
231
}
232
/*---------------------------------------------------------------------------*/
233
static struct ptentry parsetab[] =
234
  {{"lisp", switch_lisp},
235
   {"+", sd_plus},
236
   {"-", sd_minus},
237
   {"help", help},
238
   {"exit", shell_quit},
239
   {"?", help},
240
 
241
   /* Default action */
242
   {NULL, unknown}};
243
/*---------------------------------------------------------------------------*/
244
void
245
shell_init(void)
246
{}
247
/*---------------------------------------------------------------------------*/
248
void
249
shell_start(void)
250
{
251
        mode = COMMAND_MODE;
252
        shell_output("Greetings and salutations, sir or madam.", "");
253
        shell_output("I bid you welcome to the IGOR command shell!", "");
254
        shell_output("How may I serve you today?", "");
255
        shell_output("Please, type '?' and return for help.", "");
256
        shell_prompt(SHELL_PROMPT);
257
}
258
/*---------------------------------------------------------------------------*/
259
void
260
shell_input(char *cmd)
261
{
262
        if (mode == COMMAND_MODE) {
263
                parse(cmd, parsetab);
264
        }
265
        if (mode == COMMAND_MODE) {
266
                shell_prompt(SHELL_PROMPT);
267
        } else {
268
                //Lisp mode does not work yet
269
/*              struct buf *buf;
270
                buf = &telnet_data.readbuf;
271
                buf_write(buf, (uint8_t *)cmd, strlen(cmd));
272
*/      }
273
}
274
/*---------------------------------------------------------------------------*/
275
//Dump a memory card sector as a nice hex display
276
void hexdump(uint8_t * p, uint16_t len)
277
{
278
        int i,j;
279
 
280
        for (i=0;i<len/16;i++)
281
        {
282
                char str0[70] = "";
283
 
284
                char str1[5];
285
                sprintf(str1, "%03x ",i*16);
286
                strcat(str0, str1);
287
                for (j=0;j<16;j++) {
288
                        char str2[5];
289
                        sprintf(str2, "%02x ",p[i*16+j]);
290
                        strcat(str0, str2);
291
                }
292
                for (j=0;j<16;j++) {
293
                        char str3[5];
294
                        sprintf(str3, "%c", isalpha(p[i*16+j]) ? p[i*16+j] : '.');
295
                        strcat(str0, str3);
296
                }
297
                shell_output(str0, "");
298
        }
299
}

powered by: WebSVN 2.1.0

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