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:
Mon May 06 04:04:18 2013 +0000
Revision:
5:3417ba8cb1e4
Parent:
4:6ff0a3d92778
Child:
9:5e1764ff5dfa
added bcat with formatting of binary data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:618e98bf18ce 1
sam_grove 2:b3107e463974 2 /**
sam_grove 2:b3107e463974 3 * @file SDShell.cpp
sam_grove 2:b3107e463974 4 * @brief SD Card Utility - Emulate a basic UNIX terminal interface
sam_grove 2:b3107e463974 5 * @author sam grove
sam_grove 2:b3107e463974 6 * @version 1.0
sam_grove 2:b3107e463974 7 * @see
sam_grove 2:b3107e463974 8 *
sam_grove 2:b3107e463974 9 * Copyright (c) 2013
sam_grove 2:b3107e463974 10 *
sam_grove 2:b3107e463974 11 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 2:b3107e463974 12 * you may not use this file except in compliance with the License.
sam_grove 2:b3107e463974 13 * You may obtain a copy of the License at
sam_grove 2:b3107e463974 14 *
sam_grove 2:b3107e463974 15 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 2:b3107e463974 16 *
sam_grove 2:b3107e463974 17 * Unless required by applicable law or agreed to in writing, software
sam_grove 2:b3107e463974 18 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 2:b3107e463974 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 2:b3107e463974 20 * See the License for the specific language governing permissions and
sam_grove 2:b3107e463974 21 * limitations under the License.
sam_grove 2:b3107e463974 22 */
sam_grove 2:b3107e463974 23
sam_grove 0:618e98bf18ce 24 #ifndef SDSHELL_H
sam_grove 0:618e98bf18ce 25 #define SDSHELL_H
sam_grove 0:618e98bf18ce 26
sam_grove 0:618e98bf18ce 27 #include "mbed.h"
sam_grove 0:618e98bf18ce 28 #include "SDFileSystem.h"
sam_grove 0:618e98bf18ce 29 #include "CommHandler.h"
sam_grove 0:618e98bf18ce 30
sam_grove 2:b3107e463974 31 /** Example using the CommHandler class
sam_grove 2:b3107e463974 32 * @code
sam_grove 2:b3107e463974 33 * #include "mbed.h"
sam_grove 2:b3107e463974 34 * #include "SDFileSystem.h"
sam_grove 2:b3107e463974 35 * #include "SDShell.h"
sam_grove 2:b3107e463974 36 *
sam_grove 2:b3107e463974 37 * Serial com(USBTX, USBRX);
sam_grove 2:b3107e463974 38 * SDFileSystem sd(p11, p12, p13, p14, "sd");
sam_grove 2:b3107e463974 39 * SDShell emulate;
sam_grove 2:b3107e463974 40 *
sam_grove 2:b3107e463974 41 * int main()
sam_grove 2:b3107e463974 42 * {
sam_grove 2:b3107e463974 43 * emulate.init();
sam_grove 2:b3107e463974 44 * emulate.shell(com, sd, "/sd");
sam_grove 2:b3107e463974 45 * }
sam_grove 2:b3107e463974 46 * @endcode
sam_grove 2:b3107e463974 47 */
sam_grove 2:b3107e463974 48
sam_grove 2:b3107e463974 49 /**
sam_grove 2:b3107e463974 50 * @class SDShell
sam_grove 2:b3107e463974 51 * @brief API abstraction for a UNIX terminal enumator to the SDFileSystem
sam_grove 2:b3107e463974 52 */
sam_grove 0:618e98bf18ce 53 class SDShell
sam_grove 0:618e98bf18ce 54 {
sam_grove 2:b3107e463974 55 public:
sam_grove 2:b3107e463974 56
sam_grove 2:b3107e463974 57 /** Create the SDShell object
sam_grove 2:b3107e463974 58 */
sam_grove 2:b3107e463974 59 SDShell();
sam_grove 2:b3107e463974 60
sam_grove 2:b3107e463974 61 /** Initialize members and compenents of the class
sam_grove 2:b3107e463974 62 */
sam_grove 2:b3107e463974 63 void init(void);
sam_grove 2:b3107e463974 64
sam_grove 2:b3107e463974 65 /** Run the UNIX terminal emulator
sam_grove 2:b3107e463974 66 * @param com - Address of an initialized Serial object
sam_grove 2:b3107e463974 67 * @param storage - Address of an initialized SDFileSystem object
sam_grove 2:b3107e463974 68 * @cwd - The name used to access the virtual filesystem
sam_grove 2:b3107e463974 69 */
sam_grove 2:b3107e463974 70 void shell(Serial &com, SDFileSystem &storage, char const *cwd);
sam_grove 2:b3107e463974 71
sam_grove 0:618e98bf18ce 72 private:
sam_grove 0:618e98bf18ce 73 Serial *_com;
sam_grove 0:618e98bf18ce 74 SDFileSystem *_storage;
sam_grove 0:618e98bf18ce 75 CommHandler _cmds;
sam_grove 0:618e98bf18ce 76
sam_grove 1:514f321aa528 77 #define SHELL_BUF_SIZE 64
sam_grove 1:514f321aa528 78 #define SHELL_BUF_MASK (SHELL_BUF_SIZE-1)
sam_grove 1:514f321aa528 79 char _cwd[SHELL_BUF_SIZE];
sam_grove 2:b3107e463974 80 char _buf[512];
sam_grove 2:b3107e463974 81 char _newpath[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 82 char _cmd[SHELL_BUF_SIZE];
sam_grove 2:b3107e463974 83 char _cmdline[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 84 char _path_to_file[SHELL_BUF_SIZE];
sam_grove 1:514f321aa528 85 char *_arg;
sam_grove 1:514f321aa528 86 uint32_t _debug;
sam_grove 0:618e98bf18ce 87
sam_grove 2:b3107e463974 88 enum {UNKNOWN = 0, OK = '1', EXIT = '2'};
sam_grove 2:b3107e463974 89
sam_grove 2:b3107e463974 90 // helper functions that build the interface
sam_grove 1:514f321aa528 91 void shellInput(void);
sam_grove 1:514f321aa528 92 char *split(char *dest, char *src, int max, char delim);
sam_grove 1:514f321aa528 93 void resolveDirectory(char *newpath, char *path);
sam_grove 1:514f321aa528 94 void splitName(char *path, char *dirname, char *basename);
sam_grove 5:3417ba8cb1e4 95 uint32_t match(char *arg1, char *arg2);
sam_grove 5:3417ba8cb1e4 96 char btoa(uint8_t b);
sam_grove 1:514f321aa528 97
sam_grove 2:b3107e463974 98 // handler functions for each known input
sam_grove 2:b3107e463974 99 char *ls(char *cmd);
sam_grove 2:b3107e463974 100 char *cd(char *cmd);
sam_grove 2:b3107e463974 101 char *pwd(char *cmd);
sam_grove 2:b3107e463974 102 char *head(char *cmd);
sam_grove 2:b3107e463974 103 char *cat(char *cmd);
sam_grove 5:3417ba8cb1e4 104 char *bcat(char *cmd);
sam_grove 2:b3107e463974 105 char *touch(char *cmd);
sam_grove 2:b3107e463974 106 char *create(char *cmd);
sam_grove 2:b3107e463974 107 char *rm(char *cmd);
sam_grove 2:b3107e463974 108 char *exit(char *cmd);
sam_grove 2:b3107e463974 109 char *debug(char *cmd);
sam_grove 0:618e98bf18ce 110 };
sam_grove 0:618e98bf18ce 111
sam_grove 0:618e98bf18ce 112 #endif