Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format
Dependents: NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more
Fork of mbed by
Revision 8:00a04e5cd407, committed 2009-02-03
- Comitter:
- simon.ford@mbed.co.uk
- Date:
- Tue Feb 03 18:02:02 2009 +0000
- Parent:
- 7:15d74db76485
- Child:
- 9:cf0d45ce28a6
- Commit message:
- * Update to improve filesystem support
* Add a generic FATFileSystem
Changed in this revision
--- a/Base.h Fri Jan 23 16:26:21 2009 +0000
+++ b/Base.h Tue Feb 03 18:02:02 2009 +0000
@@ -7,6 +7,7 @@
#define MBED_BASE_H
#include <cstdlib>
+#include "DirHandle.h"
namespace mbed {
@@ -103,6 +104,9 @@
*/
static Base *lookup(const char *name, unsigned int len);
+ static DirHandle *opendir();
+ friend class BaseDirHandle;
+
protected:
static Base *_head;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DirHandle.h Tue Feb 03 18:02:02 2009 +0000
@@ -0,0 +1,94 @@
+/* Copyright 2008 ARM Limited. All rights reserved. */
+
+#ifndef MBED_DIRHANDLE_H
+#define MBED_DIRHANDLE_H
+
+#ifdef __ARMCC_VERSION
+# define NAME_MAX 255
+typedef int mode_t;
+#else
+# include <sys/syslimits.h>
+#endif
+#include "FileHandle.h"
+
+struct dirent {
+ char d_name[NAME_MAX+1];
+};
+
+namespace mbed {
+
+/* Class DirHandle
+ * Represents a directory stream. Objects of this type are returned
+ * by a FileSystemLike's opendir method. Implementations must define
+ * at least closedir, readdir and rewinddir.
+ *
+ * If a FileSystemLike class defines the opendir method, then the
+ * directories of an object of that type can be accessed by
+ * DIR *d = opendir("/example/directory") (or opendir("/example")
+ * to open the root of the filesystem), and then using readdir(d) etc.
+ *
+ * The root directory is considered to contain all FileLike and
+ * FileSystemLike objects, so the DIR* returned by opendir("/") will
+ * reflect this.
+ */
+class DirHandle {
+
+ public:
+ /* Function closedir
+ * Closes the directory.
+ *
+ * Variables
+ * returns - 0 on success, or -1 on error.
+ */
+ virtual int closedir()=0;
+
+ /* Function readdir
+ * Return the directory entry at the current position, and
+ * advances the position to the next entry.
+ *
+ * Variables
+ * returns - A pointer to a dirent structure representing the
+ * directory entry at the current position, or NULL on reaching
+ * end of directory or error.
+ */
+ virtual struct dirent *readdir()=0;
+
+ /* Function rewinddir
+ * Resets the position to the beginning of the directory.
+ */
+ virtual void rewinddir()=0;
+
+ /* Function telldir
+ * Returns the current position of the DirHandle.
+ *
+ * Variables
+ * returns - The current position, or -1 on error.
+ */
+ virtual off_t telldir() { return -1; }
+
+ /* Function seekdir
+ * Sets the position of the DirHandle.
+ *
+ * Variables
+ * location - The location to seek to. Must be a value returned
+ * by telldir.
+ */
+ virtual void seekdir(off_t location) { }
+
+};
+
+} /* namespace mbed */
+
+typedef mbed::DirHandle DIR;
+
+extern "C" {
+ DIR *opendir(const char*);
+ struct dirent *readdir(DIR *);
+ int closedir(DIR*);
+ void rewinddir(DIR*);
+ long telldir(DIR*);
+ void seekdir(DIR*, long);
+ int mkdir(const char *name, mode_t n);
+};
+
+#endif /* MBED_DIRHANDLE_H */
--- a/FileSystemLike.h Fri Jan 23 16:26:21 2009 +0000
+++ b/FileSystemLike.h Tue Feb 03 18:02:02 2009 +0000
@@ -3,8 +3,6 @@
#ifndef MBED_FILESYSTEMLIKE_H
#define MBED_FILESYSTEMLIKE_H
-#include "Base.h"
-#include "FileHandle.h"
#ifdef __ARMCC_VERSION
# define O_RDONLY 0
# define O_WRONLY 1
@@ -12,9 +10,13 @@
# define O_CREAT 0x0200
# define O_TRUNC 0x0400
# define O_APPEND 0x0008
+typedef int mode_t;
#else
# include <sys/fcntl.h>
#endif
+#include "Base.h"
+#include "FileHandle.h"
+#include "DirHandle.h"
namespace mbed {
@@ -66,6 +68,26 @@
*/
virtual int rename(const char *oldname, const char *newname) { return -1; };
+ /* Function opendir
+ * Opens a directory in the filesystem and returns a DirHandle
+ * representing the directory stream.
+ *
+ * Variables
+ * name - The name of the directory to open.
+ * returns - A DirHandle representing the directory stream, or
+ * NULL on failure.
+ */
+ virtual DirHandle *opendir(const char *name) { return NULL; };
+
+ /* Function mkdir
+ * Creates a directory in the filesystem.
+ *
+ * Variables
+ * name - The name of the directory to create.
+ * returns - 0 on success, -1 on failure.
+ */
+ virtual int mkdir(const char *name, mode_t mode) { return -1; }
+
// TODO other filesystem functions (mkdir, rm, rn, ls etc)
};
--- a/LocalFileSystem.h Fri Jan 23 16:26:21 2009 +0000
+++ b/LocalFileSystem.h Tue Feb 03 18:02:02 2009 +0000
@@ -25,6 +25,14 @@
* > FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
* > fprintf(fp, "Hello World!");
* > fclose(fp);
+ * > remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
+ * >
+ * > DIR *d = opendir("/local"); // Opens the root directory of the local file system
+ * > struct dirent *p;
+ * > while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
+ * > printf("%s\n", p->d_name); // to stdout.
+ * > }
+ * > closedir(d);
* > }
*
* Implementation Notes:
@@ -44,7 +52,7 @@
virtual FileHandle *open(const char* name, int flags);
virtual int remove(const char *filename);
-
+ virtual DirHandle *opendir(const char *name);
};
} // namespace mbed
--- a/Serial.h Fri Jan 23 16:26:21 2009 +0000 +++ b/Serial.h Tue Feb 03 18:02:02 2009 +0000 @@ -112,6 +112,9 @@ * returns - 1 if there is space to write a character, else 0 */ int writeable(); + + virtual const struct rpc_method *get_rpc_methods(); + static struct rpc_class *get_rpc_class(); protected:
--- a/Stream.h Fri Jan 23 16:26:21 2009 +0000
+++ b/Stream.h Tue Feb 03 18:02:02 2009 +0000
@@ -10,7 +10,7 @@
namespace mbed {
-class Stream : protected FileLike {
+class Stream : public FileLike {
public:
@@ -38,6 +38,8 @@
operator std::FILE*() { return _file; }
+ virtual const struct rpc_method *get_rpc_methods();
+
protected:
virtual int close();
@@ -46,6 +48,7 @@
virtual off_t lseek(off_t offset, int whence);
virtual int isatty();
virtual int fsync();
+ virtual off_t flen();
virtual int _putc(int c) = 0;
virtual int _getc() = 0;
Binary file mbed.ar has changed
--- a/mbed.h Fri Jan 23 16:26:21 2009 +0000 +++ b/mbed.h Tue Feb 03 18:02:02 2009 +0000 @@ -5,7 +5,7 @@ #ifndef MBED_H #define MBED_H -#define MBED_LIBRARY_VERSION 6 +#define MBED_LIBRARY_VERSION 9 // Useful C libraries #include <stdio.h>
--- a/rpc.h Fri Jan 23 16:26:21 2009 +0000
+++ b/rpc.h Tue Feb 03 18:02:02 2009 +0000
@@ -12,6 +12,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include "Base.h"
namespace mbed {
@@ -26,50 +27,99 @@
*/
template<typename T> T parse_arg(const char *arg, const char **next);
+inline char parse_char(const char *arg, const char **next) {
+ char c = *arg++;
+ if(c == '\\') {
+ c = *arg++;
+ switch(c) {
+ case 'a': c = '\a'; break;
+ case 'b': c = '\b'; break;
+ case 't': c = '\t'; break;
+ case 'n': c = '\n'; break;
+ case 'v': c = '\v'; break;
+ case 'f': c = '\f'; break;
+ case 'r': c = '\r'; break;
+ case 'x':
+ {
+ /* two-character hexadecimal */
+ char buf[3];
+ buf[0] = *arg++;
+ buf[1] = *arg++;
+ buf[2] = 0;
+ c = strtol(buf, NULL, 16);
+ }
+ break;
+ default:
+ if(isdigit(c)) {
+ /* three-character octal */
+ char buf[4];
+ buf[0] = c;
+ buf[1] = *arg++;
+ buf[2] = *arg++;
+ buf[3] = 0;
+ c = strtol(buf, NULL, 8);
+ }
+ break;
+ }
+ }
+ *next = arg;
+ return c;
+}
+
/* signed integer types */
+template<> inline int parse_arg<int>(const char *arg, const char **next) {
+ if(arg[0] == '\'') {
+ char c = parse_char(arg+1, &arg);
+ if(next != NULL) *next = arg+1;
+ return c;
+ } else {
+ return strtol(arg, const_cast<char**>(next), 0);
+ }
+}
+
template<> inline char parse_arg<char>(const char *arg, const char **next) {
- if(next != NULL) *next = arg+1;
- return *arg;
+ return parse_arg<int>(arg,next);
}
template<> inline short int parse_arg<short int>(const char *arg, const char **next) {
- return strtol(arg, const_cast<char**>(next), 10);
+ return parse_arg<int>(arg,next);
}
template<> inline long int parse_arg<long int>(const char *arg, const char **next) {
- return strtol(arg, const_cast<char**>(next), 10);
-}
-
-template<> inline int parse_arg<int>(const char *arg, const char **next) {
- return strtol(arg, const_cast<char**>(next), 10);
+ return parse_arg<int>(arg,next);
}
template<> inline long long parse_arg<long long>(const char *arg, const char **next) {
- return strtoll(arg, const_cast<char**>(next), 10);
+ return strtoll(arg, const_cast<char**>(next), 0);
}
/* unsigned integer types */
+template<> inline unsigned int parse_arg<unsigned int>(const char *arg, const char **next) {
+ if(arg[0] == '\'') {
+ char c = parse_char(arg+1, &arg);
+ if(next != NULL) *next = arg+1;
+ return c;
+ } else {
+ return strtoul(arg, const_cast<char**>(next), 0);
+ }
+}
+
template<> inline unsigned char parse_arg<unsigned char>(const char *arg, const char **next) {
- if(next != NULL) *next = arg+1;
- return *arg;
+ return parse_arg<unsigned int>(arg,next);
}
template<> inline unsigned short int parse_arg<unsigned short int>(const char *arg, const char **next) {
- return strtoul(arg, const_cast<char**>(next), 10);
+ return parse_arg<unsigned int>(arg,next);
}
template<> inline unsigned long int parse_arg<unsigned long int>(const char *arg, const char **next) {
- return strtoul(arg, const_cast<char**>(next), 10);
-}
-
-template<> inline unsigned int parse_arg<unsigned int>(const char *arg, const char **next) {
- return strtoul(arg, const_cast<char**>(next), 10);
+ return parse_arg<unsigned int>(arg,next);
}
template<> inline unsigned long long parse_arg<unsigned long long>(const char *arg, const char **next) {
- return strtoull(arg, const_cast<char**>(next), 10);
+ return strtoull(arg, const_cast<char**>(next), 0);
}
/* floating types */
@@ -98,22 +148,44 @@
template<> inline char *parse_arg<char*>(const char *arg, const char **next) {
const char *ptr = arg;
- while(*ptr >= '!' && *ptr != ',') {
- ptr++;
+ char *res = NULL;
+ if(*arg == '"') {
+ /* quoted string */
+ ptr = ++arg;
+ int len = 0;
+ /* find the end (and length) of the quoted string */
+ for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) {
+ len++;
+ if(c == '\\') {
+ ptr++;
+ }
+ }
+ /* copy the quoted string, and unescape characters */
+ if(len != 0) {
+ res = new char[len+1];
+ char *resptr = res;
+ while(arg != ptr) {
+ *resptr++ = parse_char(arg, &arg);
+ }
+ *resptr = 0;
+ }
+ } else {
+ /* unquoted string */
+ while(isalnum(*ptr) || *ptr=='_') {
+ ptr++;
+ }
+ int len = ptr-arg;
+ if(len!=0) {
+ res = new char[len+1];
+ memcpy(res, arg, len);
+ res[len] = 0;
+ }
}
- int len = ptr-arg;
- char *p;
- if(len==0) {
- p = NULL;
- } else {
- p = new char[len+1];
- memcpy(p, arg, len);
- p[len] = 0;
- }
+
if(next != NULL) {
*next = ptr;
}
- return p;
+ return res;
}
template<> inline const char *parse_arg<const char*>(const char *arg, const char **next) {
@@ -179,15 +251,15 @@
/* floating types */
template<> inline void write_result<float>(float val, char *result) {
- sprintf(result, "%g", val);
+ sprintf(result, "%.17g", val);
}
template<> inline void write_result<double>(double val, char *result) {
- sprintf(result, "%g", val);
+ sprintf(result, "%.17g", val);
}
template<> inline void write_result<long double>(long double val, char *result) {
- sprintf(result, "%Lg", val);
+ sprintf(result, "%.17Lg", val);
}
@@ -211,7 +283,9 @@
inline const char *next_arg(const char* next) {
- if(*next == ',' || *next == ' ') next++;
+ while(*next == ' ') next++;
+ if(*next == ',' || *next == '?') next++;
+ while(*next == ' ') next++;
return next;
}
@@ -268,6 +342,23 @@
/* Function rpc_method_caller
*/
+template<class T, typename A1, typename A2, typename A3, void (T::*member)(A1,A2,A3)>
+void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
+
+ const char *next = arguments;
+ A1 arg1 = parse_arg<A1>(next_arg(next),&next);
+ A2 arg2 = parse_arg<A2>(next_arg(next),&next);
+ A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
+
+ (static_cast<T*>(this_ptr)->*member)(arg1,arg2,arg3);
+ if(result != NULL) {
+ result[0] = '\0';
+ }
+}
+
+
+/* Function rpc_method_caller
+ */
template<typename R, class T, R (T::*member)()>
void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
R res = (static_cast<T*>(this_ptr)->*member)();
Mihail Stoyanov
