A UNIX emulation shell to access the underlying SDCard FileSystem through a terminal interface

Dependents:   Waldo_Embed_V2

Information

SDShell does not change the com baudrate. Access is made using the baud as initialized by the Serial object when passed into the SDShell object

Example

#include "mbed.h"
#include "SDFileSystem.h"
#include "SDShell.h"

Serial com(USBTX, USBRX);
SDFileSystem sd(p11, p12, p13, p14, "sd");
SDShell emulate;

int main()
{
    emulate.init();
    emulate.shell(com, sd, "/sd");
}
Committer:
sam_grove
Date:
Sat Apr 27 16:08:39 2013 +0000
Revision:
1:514f321aa528
Parent:
0:618e98bf18ce
Child:
2:b3107e463974
partially working implementation - has a bug in parsing come back to later

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:618e98bf18ce 1
sam_grove 0:618e98bf18ce 2 #ifndef SDSHELL_H
sam_grove 0:618e98bf18ce 3 #define SDSHELL_H
sam_grove 0:618e98bf18ce 4
sam_grove 0:618e98bf18ce 5 #include "mbed.h"
sam_grove 0:618e98bf18ce 6 #include "SDFileSystem.h"
sam_grove 0:618e98bf18ce 7 #include "CommHandler.h"
sam_grove 0:618e98bf18ce 8
sam_grove 0:618e98bf18ce 9 class SDShell
sam_grove 0:618e98bf18ce 10 {
sam_grove 0:618e98bf18ce 11 private:
sam_grove 0:618e98bf18ce 12 Serial *_com;
sam_grove 0:618e98bf18ce 13 SDFileSystem *_storage;
sam_grove 0:618e98bf18ce 14 CommHandler _cmds;
sam_grove 0:618e98bf18ce 15
sam_grove 1:514f321aa528 16 #define SHELL_BUF_SIZE 64
sam_grove 1:514f321aa528 17 #define SHELL_BUF_MASK (SHELL_BUF_SIZE-1)
sam_grove 1:514f321aa528 18 char _cwd[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 19 // char _arg[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 20 char _buf[128];
sam_grove 1:514f321aa528 21 char _new_path[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 22 char _cmd[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 23 char _cmd_line[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 24 char _path_to_file[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 25 char *_arg;
sam_grove 1:514f321aa528 26 uint32_t _debug;
sam_grove 0:618e98bf18ce 27
sam_grove 1:514f321aa528 28 void shellInput(void);
sam_grove 1:514f321aa528 29 char *split(char *dest, char *src, int max, char delim);
sam_grove 1:514f321aa528 30 void resolveDirectory(char *newpath, char *path);
sam_grove 1:514f321aa528 31 void splitName(char *path, char *dirname, char *basename);
sam_grove 1:514f321aa528 32 uint32_t match(char *arg1, char *arg2);
sam_grove 1:514f321aa528 33
sam_grove 1:514f321aa528 34 void ls(char *path, Serial *pc) ;
sam_grove 1:514f321aa528 35 void cd(char *path);
sam_grove 1:514f321aa528 36 void pwd(Serial *pc);
sam_grove 1:514f321aa528 37 void head(char *path, Serial *pc);
sam_grove 1:514f321aa528 38 void cat(char *path, Serial *pc);
sam_grove 1:514f321aa528 39 void touch(char *path, Serial *pc);
sam_grove 0:618e98bf18ce 40
sam_grove 1:514f321aa528 41 // char *ls(char *);
sam_grove 1:514f321aa528 42 // char *cd(char *);
sam_grove 1:514f321aa528 43 // char *pwd(char *);
sam_grove 1:514f321aa528 44 // char *head(char *);
sam_grove 1:514f321aa528 45 // char *cat(char *);
sam_grove 1:514f321aa528 46 // char *mkdir(char *);
sam_grove 1:514f321aa528 47 // char *touch(char *);
sam_grove 1:514f321aa528 48 // char *remove(char *);
sam_grove 1:514f321aa528 49 // char *resolvePath(char *string);
sam_grove 0:618e98bf18ce 50
sam_grove 0:618e98bf18ce 51 public:
sam_grove 0:618e98bf18ce 52 SDShell();
sam_grove 0:618e98bf18ce 53
sam_grove 0:618e98bf18ce 54 void init(void);
sam_grove 0:618e98bf18ce 55
sam_grove 0:618e98bf18ce 56 void shell(Serial &com, SDFileSystem &storage, char *cwd);
sam_grove 0:618e98bf18ce 57
sam_grove 0:618e98bf18ce 58 };
sam_grove 0:618e98bf18ce 59
sam_grove 0:618e98bf18ce 60 #endif
sam_grove 0:618e98bf18ce 61
sam_grove 1:514f321aa528 62 //void SDShell::shell(Serial &com, SDFileSystem &storage, char *cwd)
sam_grove 1:514f321aa528 63 //{
sam_grove 1:514f321aa528 64 // // should check on the device here
sam_grove 1:514f321aa528 65 // _com = &com;
sam_grove 1:514f321aa528 66 // strcpy(_cwd, cwd);
sam_grove 1:514f321aa528 67 //
sam_grove 1:514f321aa528 68 // _com->printf("%s \n", _cwd); // print out the directory that we're in
sam_grove 1:514f321aa528 69 // while(1)
sam_grove 1:514f321aa528 70 // {
sam_grove 1:514f321aa528 71 // char c = _com->getc();
sam_grove 1:514f321aa528 72 // if (c == '\r') // look for something to do
sam_grove 1:514f321aa528 73 // {
sam_grove 1:514f321aa528 74 // _cmds.serviceMessage(_rxbuf.data);
sam_grove 1:514f321aa528 75 // _com->printf("\nECHO: %s \n", _rxbuf.data);
sam_grove 1:514f321aa528 76 //
sam_grove 1:514f321aa528 77 // memset(_rxbuf.data, 0, _rxbuf.loc-1);
sam_grove 1:514f321aa528 78 // _rxbuf.loc = 0;
sam_grove 1:514f321aa528 79 // }
sam_grove 1:514f321aa528 80 // else if (c == 0x7f) // backspace
sam_grove 1:514f321aa528 81 // {
sam_grove 1:514f321aa528 82 // if (_rxbuf.loc > 0)
sam_grove 1:514f321aa528 83 // {
sam_grove 1:514f321aa528 84 // _rxbuf.loc--;
sam_grove 1:514f321aa528 85 // _com->printf("\b \b");
sam_grove 1:514f321aa528 86 // }
sam_grove 1:514f321aa528 87 // }
sam_grove 1:514f321aa528 88 // else
sam_grove 1:514f321aa528 89 // {
sam_grove 1:514f321aa528 90 // _com->putc(c);
sam_grove 1:514f321aa528 91 // _rxbuf.data[_rxbuf.loc++] = c;
sam_grove 1:514f321aa528 92 // _rxbuf.loc &= SHELL_RXBUF_MASK;
sam_grove 1:514f321aa528 93 // }
sam_grove 1:514f321aa528 94 // }
sam_grove 1:514f321aa528 95 // return;
sam_grove 1:514f321aa528 96 //}
sam_grove 1:514f321aa528 97 //
sam_grove 1:514f321aa528 98 //// not working - need to pick back up here
sam_grove 1:514f321aa528 99 //char *SDShell::ls(char *string)
sam_grove 1:514f321aa528 100 //{
sam_grove 1:514f321aa528 101 // //ls: lists files in the current working directory
sam_grove 1:514f321aa528 102 // _com->printf(" %s\n", _cwd);
sam_grove 1:514f321aa528 103 //
sam_grove 1:514f321aa528 104 // DIR *d = opendir(_cwd); // <- problem here??
sam_grove 1:514f321aa528 105 // _com->printf(" %s\n", _cwd);
sam_grove 1:514f321aa528 106 // if(d != NULL)
sam_grove 1:514f321aa528 107 // {
sam_grove 1:514f321aa528 108 // struct dirent *p;
sam_grove 1:514f321aa528 109 // _com->printf(" %s\n", _cwd);
sam_grove 1:514f321aa528 110 // do
sam_grove 1:514f321aa528 111 // {
sam_grove 1:514f321aa528 112 // p = readdir(d); // doesn't take parameter?? where does this go?
sam_grove 1:514f321aa528 113 // _com->printf(" file: %s\n", p->d_name);
sam_grove 1:514f321aa528 114 // }
sam_grove 1:514f321aa528 115 // while(p != NULL);
sam_grove 1:514f321aa528 116 //
sam_grove 1:514f321aa528 117 // closedir(d);
sam_grove 1:514f321aa528 118 // }
sam_grove 1:514f321aa528 119 // else
sam_grove 1:514f321aa528 120 // {
sam_grove 1:514f321aa528 121 // _com->printf("%s: No such directory\n", _cwd);
sam_grove 1:514f321aa528 122 // }
sam_grove 1:514f321aa528 123 //
sam_grove 1:514f321aa528 124 // return string;
sam_grove 1:514f321aa528 125 //}
sam_grove 1:514f321aa528 126 //
sam_grove 1:514f321aa528 127 //char *SDShell::cd(char *string){ return string;}
sam_grove 1:514f321aa528 128 //char *SDShell::pwd(char *string){ return string;}
sam_grove 1:514f321aa528 129 //char *SDShell::head(char *string){ return string;}
sam_grove 1:514f321aa528 130 //
sam_grove 1:514f321aa528 131 //char *SDShell::cat(char *string)
sam_grove 1:514f321aa528 132 //{
sam_grove 1:514f321aa528 133 // // display the content of a file eg: cat readme.txt
sam_grove 1:514f321aa528 134 // resolvePath((string+4));
sam_grove 1:514f321aa528 135 //
sam_grove 1:514f321aa528 136 // FILE *fp = fopen(_path_to_file, "r");
sam_grove 1:514f321aa528 137 // if(NULL != fp)
sam_grove 1:514f321aa528 138 // {
sam_grove 1:514f321aa528 139 // char *buf = new char [1024];
sam_grove 1:514f321aa528 140 // while (!feof(fp))
sam_grove 1:514f321aa528 141 // {
sam_grove 1:514f321aa528 142 // fgets(buf, sizeof(buf), fp);
sam_grove 1:514f321aa528 143 // _com->printf("%s", buf);
sam_grove 1:514f321aa528 144 // }
sam_grove 1:514f321aa528 145 // fclose(fp);
sam_grove 1:514f321aa528 146 // delete [] buf;
sam_grove 1:514f321aa528 147 // }
sam_grove 1:514f321aa528 148 // else
sam_grove 1:514f321aa528 149 // {
sam_grove 1:514f321aa528 150 // _com->printf("\n%s: No such file\n", _path_to_file);
sam_grove 1:514f321aa528 151 // }
sam_grove 1:514f321aa528 152 //
sam_grove 1:514f321aa528 153 // return string;
sam_grove 1:514f321aa528 154 // }
sam_grove 1:514f321aa528 155 //
sam_grove 1:514f321aa528 156 //char *SDShell::mkdir(char *string){ return string;}
sam_grove 1:514f321aa528 157 //char *SDShell::touch(char *string){ return string;}
sam_grove 1:514f321aa528 158 //char *SDShell::remove(char *string){ return string;}
sam_grove 1:514f321aa528 159 //
sam_grove 1:514f321aa528 160 //char *SDShell::resolvePath(char *string)
sam_grove 1:514f321aa528 161 //{
sam_grove 1:514f321aa528 162 // strcpy(_path_to_file, _cwd);
sam_grove 1:514f321aa528 163 // strcat(_path_to_file, "/");
sam_grove 1:514f321aa528 164 // strcat(_path_to_file, string);
sam_grove 1:514f321aa528 165 //
sam_grove 1:514f321aa528 166 // return _path_to_file;
sam_grove 1:514f321aa528 167 //}
sam_grove 1:514f321aa528 168
sam_grove 1:514f321aa528 169