IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbrahmbhatt6 0:2f388b030837 1 /* mbed Microcontroller Library
kbrahmbhatt6 0:2f388b030837 2 * Copyright (c) 2006-2013 ARM Limited
kbrahmbhatt6 0:2f388b030837 3 *
kbrahmbhatt6 0:2f388b030837 4 * Licensed under the Apache License, Version 2.0 (the "License");
kbrahmbhatt6 0:2f388b030837 5 * you may not use this file except in compliance with the License.
kbrahmbhatt6 0:2f388b030837 6 * You may obtain a copy of the License at
kbrahmbhatt6 0:2f388b030837 7 *
kbrahmbhatt6 0:2f388b030837 8 * http://www.apache.org/licenses/LICENSE-2.0
kbrahmbhatt6 0:2f388b030837 9 *
kbrahmbhatt6 0:2f388b030837 10 * Unless required by applicable law or agreed to in writing, software
kbrahmbhatt6 0:2f388b030837 11 * distributed under the License is distributed on an "AS IS" BASIS,
kbrahmbhatt6 0:2f388b030837 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kbrahmbhatt6 0:2f388b030837 13 * See the License for the specific language governing permissions and
kbrahmbhatt6 0:2f388b030837 14 * limitations under the License.
kbrahmbhatt6 0:2f388b030837 15 */
kbrahmbhatt6 0:2f388b030837 16 #ifndef MBED_LOCALFILESYSTEM_H
kbrahmbhatt6 0:2f388b030837 17 #define MBED_LOCALFILESYSTEM_H
kbrahmbhatt6 0:2f388b030837 18
kbrahmbhatt6 0:2f388b030837 19 #include "platform.h"
kbrahmbhatt6 0:2f388b030837 20
kbrahmbhatt6 0:2f388b030837 21 #if DEVICE_LOCALFILESYSTEM
kbrahmbhatt6 0:2f388b030837 22
kbrahmbhatt6 0:2f388b030837 23 #include "FileSystemLike.h"
kbrahmbhatt6 0:2f388b030837 24
kbrahmbhatt6 0:2f388b030837 25 namespace mbed {
kbrahmbhatt6 0:2f388b030837 26
kbrahmbhatt6 0:2f388b030837 27 FILEHANDLE local_file_open(const char* name, int flags);
kbrahmbhatt6 0:2f388b030837 28
kbrahmbhatt6 0:2f388b030837 29 class LocalFileHandle : public FileHandle {
kbrahmbhatt6 0:2f388b030837 30
kbrahmbhatt6 0:2f388b030837 31 public:
kbrahmbhatt6 0:2f388b030837 32 LocalFileHandle(FILEHANDLE fh);
kbrahmbhatt6 0:2f388b030837 33
kbrahmbhatt6 0:2f388b030837 34 virtual int close();
kbrahmbhatt6 0:2f388b030837 35
kbrahmbhatt6 0:2f388b030837 36 virtual ssize_t write(const void *buffer, size_t length);
kbrahmbhatt6 0:2f388b030837 37
kbrahmbhatt6 0:2f388b030837 38 virtual ssize_t read(void *buffer, size_t length);
kbrahmbhatt6 0:2f388b030837 39
kbrahmbhatt6 0:2f388b030837 40 virtual int isatty();
kbrahmbhatt6 0:2f388b030837 41
kbrahmbhatt6 0:2f388b030837 42 virtual off_t lseek(off_t position, int whence);
kbrahmbhatt6 0:2f388b030837 43
kbrahmbhatt6 0:2f388b030837 44 virtual int fsync();
kbrahmbhatt6 0:2f388b030837 45
kbrahmbhatt6 0:2f388b030837 46 virtual off_t flen();
kbrahmbhatt6 0:2f388b030837 47
kbrahmbhatt6 0:2f388b030837 48 protected:
kbrahmbhatt6 0:2f388b030837 49 FILEHANDLE _fh;
kbrahmbhatt6 0:2f388b030837 50 int pos;
kbrahmbhatt6 0:2f388b030837 51 };
kbrahmbhatt6 0:2f388b030837 52
kbrahmbhatt6 0:2f388b030837 53 /** A filesystem for accessing the local mbed Microcontroller USB disk drive
kbrahmbhatt6 0:2f388b030837 54 *
kbrahmbhatt6 0:2f388b030837 55 * This allows programs to read and write files on the same disk drive that is used to program the
kbrahmbhatt6 0:2f388b030837 56 * mbed Microcontroller. Once created, the standard C file access functions are used to open,
kbrahmbhatt6 0:2f388b030837 57 * read and write files.
kbrahmbhatt6 0:2f388b030837 58 *
kbrahmbhatt6 0:2f388b030837 59 * Example:
kbrahmbhatt6 0:2f388b030837 60 * @code
kbrahmbhatt6 0:2f388b030837 61 * #include "mbed.h"
kbrahmbhatt6 0:2f388b030837 62 *
kbrahmbhatt6 0:2f388b030837 63 * LocalFileSystem local("local"); // Create the local filesystem under the name "local"
kbrahmbhatt6 0:2f388b030837 64 *
kbrahmbhatt6 0:2f388b030837 65 * int main() {
kbrahmbhatt6 0:2f388b030837 66 * FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
kbrahmbhatt6 0:2f388b030837 67 * fprintf(fp, "Hello World!");
kbrahmbhatt6 0:2f388b030837 68 * fclose(fp);
kbrahmbhatt6 0:2f388b030837 69 * remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
kbrahmbhatt6 0:2f388b030837 70 *
kbrahmbhatt6 0:2f388b030837 71 * DIR *d = opendir("/local"); // Opens the root directory of the local file system
kbrahmbhatt6 0:2f388b030837 72 * struct dirent *p;
kbrahmbhatt6 0:2f388b030837 73 * while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
kbrahmbhatt6 0:2f388b030837 74 * printf("%s\n", p->d_name); // to stdout.
kbrahmbhatt6 0:2f388b030837 75 * }
kbrahmbhatt6 0:2f388b030837 76 * closedir(d);
kbrahmbhatt6 0:2f388b030837 77 * }
kbrahmbhatt6 0:2f388b030837 78 * @endcode
kbrahmbhatt6 0:2f388b030837 79 *
kbrahmbhatt6 0:2f388b030837 80 * @note
kbrahmbhatt6 0:2f388b030837 81 * If the microcontroller program makes an access to the local drive, it will be marked as "removed"
kbrahmbhatt6 0:2f388b030837 82 * on the Host computer. This means it is no longer accessible from the Host Computer.
kbrahmbhatt6 0:2f388b030837 83 *
kbrahmbhatt6 0:2f388b030837 84 * The drive will only re-appear when the microcontroller program exists. Note that if the program does
kbrahmbhatt6 0:2f388b030837 85 * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again!
kbrahmbhatt6 0:2f388b030837 86 */
kbrahmbhatt6 0:2f388b030837 87 class LocalFileSystem : public FileSystemLike {
kbrahmbhatt6 0:2f388b030837 88
kbrahmbhatt6 0:2f388b030837 89 public:
kbrahmbhatt6 0:2f388b030837 90 LocalFileSystem(const char* n) : FileSystemLike(n) {
kbrahmbhatt6 0:2f388b030837 91
kbrahmbhatt6 0:2f388b030837 92 }
kbrahmbhatt6 0:2f388b030837 93
kbrahmbhatt6 0:2f388b030837 94 virtual FileHandle *open(const char* name, int flags);
kbrahmbhatt6 0:2f388b030837 95 virtual int remove(const char *filename);
kbrahmbhatt6 0:2f388b030837 96 virtual DirHandle *opendir(const char *name);
kbrahmbhatt6 0:2f388b030837 97 };
kbrahmbhatt6 0:2f388b030837 98
kbrahmbhatt6 0:2f388b030837 99 } // namespace mbed
kbrahmbhatt6 0:2f388b030837 100
kbrahmbhatt6 0:2f388b030837 101 #endif
kbrahmbhatt6 0:2f388b030837 102
kbrahmbhatt6 0:2f388b030837 103 #endif