A UNIX emulation shell to access the underlying SDCard FileSystem through a terminal interface
Information
Dependencies not included with library:
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"); }
Diff: SDShell.h
- Revision:
- 2:b3107e463974
- Parent:
- 1:514f321aa528
- Child:
- 4:6ff0a3d92778
--- a/SDShell.h Sat Apr 27 16:08:39 2013 +0000 +++ b/SDShell.h Sat Apr 27 23:09:06 2013 +0000 @@ -1,4 +1,26 @@ +/** + * @file SDShell.cpp + * @brief SD Card Utility - Emulate a basic UNIX terminal interface + * @author sam grove + * @version 1.0 + * @see + * + * Copyright (c) 2013 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef SDSHELL_H #define SDSHELL_H @@ -6,8 +28,47 @@ #include "SDFileSystem.h" #include "CommHandler.h" +/** Example using the CommHandler class + * @code + * #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"); + * } + * @endcode + */ + +/** + * @class SDShell + * @brief API abstraction for a UNIX terminal enumator to the SDFileSystem + */ class SDShell { +public: + + /** Create the SDShell object + */ + SDShell(); + + /** Initialize members and compenents of the class + */ + void init(void); + + /** Run the UNIX terminal emulator + * @param com - Address of an initialized Serial object + * @param storage - Address of an initialized SDFileSystem object + * @cwd - The name used to access the virtual filesystem + */ + void shell(Serial &com, SDFileSystem &storage, char const *cwd); + private: Serial *_com; SDFileSystem *_storage; @@ -16,154 +77,34 @@ #define SHELL_BUF_SIZE 64 #define SHELL_BUF_MASK (SHELL_BUF_SIZE-1) char _cwd[SHELL_BUF_SIZE]; -// char _arg[SHELL_BUF_SIZE]; - char _buf[128]; - char _new_path[SHELL_BUF_SIZE]; + char _buf[512]; + char _newpath[SHELL_BUF_SIZE]; char _cmd[SHELL_BUF_SIZE]; - char _cmd_line[SHELL_BUF_SIZE]; + char _cmdline[SHELL_BUF_SIZE]; char _path_to_file[SHELL_BUF_SIZE]; char *_arg; uint32_t _debug; + enum {UNKNOWN = 0, OK = '1', EXIT = '2'}; + + // helper functions that build the interface void shellInput(void); char *split(char *dest, char *src, int max, char delim); void resolveDirectory(char *newpath, char *path); void splitName(char *path, char *dirname, char *basename); uint32_t match(char *arg1, char *arg2); - void ls(char *path, Serial *pc) ; - void cd(char *path); - void pwd(Serial *pc); - void head(char *path, Serial *pc); - void cat(char *path, Serial *pc); - void touch(char *path, Serial *pc); - -// char *ls(char *); -// char *cd(char *); -// char *pwd(char *); -// char *head(char *); -// char *cat(char *); -// char *mkdir(char *); -// char *touch(char *); -// char *remove(char *); -// char *resolvePath(char *string); - -public: - SDShell(); - - void init(void); - - void shell(Serial &com, SDFileSystem &storage, char *cwd); - + // handler functions for each known input + char *ls(char *cmd); + char *cd(char *cmd); + char *pwd(char *cmd); + char *head(char *cmd); + char *cat(char *cmd); + char *touch(char *cmd); + char *create(char *cmd); + char *rm(char *cmd); + char *exit(char *cmd); + char *debug(char *cmd); }; #endif - -//void SDShell::shell(Serial &com, SDFileSystem &storage, char *cwd) -//{ -// // should check on the device here -// _com = &com; -// strcpy(_cwd, cwd); -// -// _com->printf("%s \n", _cwd); // print out the directory that we're in -// while(1) -// { -// char c = _com->getc(); -// if (c == '\r') // look for something to do -// { -// _cmds.serviceMessage(_rxbuf.data); -// _com->printf("\nECHO: %s \n", _rxbuf.data); -// -// memset(_rxbuf.data, 0, _rxbuf.loc-1); -// _rxbuf.loc = 0; -// } -// else if (c == 0x7f) // backspace -// { -// if (_rxbuf.loc > 0) -// { -// _rxbuf.loc--; -// _com->printf("\b \b"); -// } -// } -// else -// { -// _com->putc(c); -// _rxbuf.data[_rxbuf.loc++] = c; -// _rxbuf.loc &= SHELL_RXBUF_MASK; -// } -// } -// return; -//} -// -//// not working - need to pick back up here -//char *SDShell::ls(char *string) -//{ -// //ls: lists files in the current working directory -// _com->printf(" %s\n", _cwd); -// -// DIR *d = opendir(_cwd); // <- problem here?? -// _com->printf(" %s\n", _cwd); -// if(d != NULL) -// { -// struct dirent *p; -// _com->printf(" %s\n", _cwd); -// do -// { -// p = readdir(d); // doesn't take parameter?? where does this go? -// _com->printf(" file: %s\n", p->d_name); -// } -// while(p != NULL); -// -// closedir(d); -// } -// else -// { -// _com->printf("%s: No such directory\n", _cwd); -// } -// -// return string; -//} -// -//char *SDShell::cd(char *string){ return string;} -//char *SDShell::pwd(char *string){ return string;} -//char *SDShell::head(char *string){ return string;} -// -//char *SDShell::cat(char *string) -//{ -// // display the content of a file eg: cat readme.txt -// resolvePath((string+4)); -// -// FILE *fp = fopen(_path_to_file, "r"); -// if(NULL != fp) -// { -// char *buf = new char [1024]; -// while (!feof(fp)) -// { -// fgets(buf, sizeof(buf), fp); -// _com->printf("%s", buf); -// } -// fclose(fp); -// delete [] buf; -// } -// else -// { -// _com->printf("\n%s: No such file\n", _path_to_file); -// } -// -// return string; -// } -// -//char *SDShell::mkdir(char *string){ return string;} -//char *SDShell::touch(char *string){ return string;} -//char *SDShell::remove(char *string){ return string;} -// -//char *SDShell::resolvePath(char *string) -//{ -// strcpy(_path_to_file, _cwd); -// strcat(_path_to_file, "/"); -// strcat(_path_to_file, string); -// -// return _path_to_file; -//} - -