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_FILEHANDLE_H
kbrahmbhatt6 0:2f388b030837 17 #define MBED_FILEHANDLE_H
kbrahmbhatt6 0:2f388b030837 18
kbrahmbhatt6 0:2f388b030837 19 typedef int FILEHANDLE;
kbrahmbhatt6 0:2f388b030837 20
kbrahmbhatt6 0:2f388b030837 21 #include <stdio.h>
kbrahmbhatt6 0:2f388b030837 22
kbrahmbhatt6 0:2f388b030837 23 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
kbrahmbhatt6 0:2f388b030837 24 typedef int ssize_t;
kbrahmbhatt6 0:2f388b030837 25 typedef long off_t;
kbrahmbhatt6 0:2f388b030837 26
kbrahmbhatt6 0:2f388b030837 27 #else
kbrahmbhatt6 0:2f388b030837 28 # include <sys/types.h>
kbrahmbhatt6 0:2f388b030837 29 #endif
kbrahmbhatt6 0:2f388b030837 30
kbrahmbhatt6 0:2f388b030837 31 namespace mbed {
kbrahmbhatt6 0:2f388b030837 32
kbrahmbhatt6 0:2f388b030837 33 /** An OO equivalent of the internal FILEHANDLE variable
kbrahmbhatt6 0:2f388b030837 34 * and associated _sys_* functions.
kbrahmbhatt6 0:2f388b030837 35 *
kbrahmbhatt6 0:2f388b030837 36 * FileHandle is an abstract class, needing at least sys_write and
kbrahmbhatt6 0:2f388b030837 37 * sys_read to be implmented for a simple interactive device.
kbrahmbhatt6 0:2f388b030837 38 *
kbrahmbhatt6 0:2f388b030837 39 * No one ever directly tals to/instanciates a FileHandle - it gets
kbrahmbhatt6 0:2f388b030837 40 * created by FileSystem, and wrapped up by stdio.
kbrahmbhatt6 0:2f388b030837 41 */
kbrahmbhatt6 0:2f388b030837 42 class FileHandle {
kbrahmbhatt6 0:2f388b030837 43
kbrahmbhatt6 0:2f388b030837 44 public:
kbrahmbhatt6 0:2f388b030837 45 /** Write the contents of a buffer to the file
kbrahmbhatt6 0:2f388b030837 46 *
kbrahmbhatt6 0:2f388b030837 47 * @param buffer the buffer to write from
kbrahmbhatt6 0:2f388b030837 48 * @param length the number of characters to write
kbrahmbhatt6 0:2f388b030837 49 *
kbrahmbhatt6 0:2f388b030837 50 * @returns
kbrahmbhatt6 0:2f388b030837 51 * The number of characters written (possibly 0) on success, -1 on error.
kbrahmbhatt6 0:2f388b030837 52 */
kbrahmbhatt6 0:2f388b030837 53 virtual ssize_t write(const void* buffer, size_t length) = 0;
kbrahmbhatt6 0:2f388b030837 54
kbrahmbhatt6 0:2f388b030837 55 /** Close the file
kbrahmbhatt6 0:2f388b030837 56 *
kbrahmbhatt6 0:2f388b030837 57 * @returns
kbrahmbhatt6 0:2f388b030837 58 * Zero on success, -1 on error.
kbrahmbhatt6 0:2f388b030837 59 */
kbrahmbhatt6 0:2f388b030837 60 virtual int close() = 0;
kbrahmbhatt6 0:2f388b030837 61
kbrahmbhatt6 0:2f388b030837 62 /** Function read
kbrahmbhatt6 0:2f388b030837 63 * Reads the contents of the file into a buffer
kbrahmbhatt6 0:2f388b030837 64 *
kbrahmbhatt6 0:2f388b030837 65 * @param buffer the buffer to read in to
kbrahmbhatt6 0:2f388b030837 66 * @param length the number of characters to read
kbrahmbhatt6 0:2f388b030837 67 *
kbrahmbhatt6 0:2f388b030837 68 * @returns
kbrahmbhatt6 0:2f388b030837 69 * The number of characters read (zero at end of file) on success, -1 on error.
kbrahmbhatt6 0:2f388b030837 70 */
kbrahmbhatt6 0:2f388b030837 71 virtual ssize_t read(void* buffer, size_t length) = 0;
kbrahmbhatt6 0:2f388b030837 72
kbrahmbhatt6 0:2f388b030837 73 /** Check if the handle is for a interactive terminal device.
kbrahmbhatt6 0:2f388b030837 74 * If so, line buffered behaviour is used by default
kbrahmbhatt6 0:2f388b030837 75 *
kbrahmbhatt6 0:2f388b030837 76 * @returns
kbrahmbhatt6 0:2f388b030837 77 * 1 if it is a terminal,
kbrahmbhatt6 0:2f388b030837 78 * 0 otherwise
kbrahmbhatt6 0:2f388b030837 79 */
kbrahmbhatt6 0:2f388b030837 80 virtual int isatty() = 0;
kbrahmbhatt6 0:2f388b030837 81
kbrahmbhatt6 0:2f388b030837 82 /** Move the file position to a given offset from a given location.
kbrahmbhatt6 0:2f388b030837 83 *
kbrahmbhatt6 0:2f388b030837 84 * @param offset The offset from whence to move to
kbrahmbhatt6 0:2f388b030837 85 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
kbrahmbhatt6 0:2f388b030837 86 * current file position, or SEEK_END for the end of the file.
kbrahmbhatt6 0:2f388b030837 87 *
kbrahmbhatt6 0:2f388b030837 88 * @returns
kbrahmbhatt6 0:2f388b030837 89 * new file position on success,
kbrahmbhatt6 0:2f388b030837 90 * -1 on failure or unsupported
kbrahmbhatt6 0:2f388b030837 91 */
kbrahmbhatt6 0:2f388b030837 92 virtual off_t lseek(off_t offset, int whence) = 0;
kbrahmbhatt6 0:2f388b030837 93
kbrahmbhatt6 0:2f388b030837 94 /** Flush any buffers associated with the FileHandle, ensuring it
kbrahmbhatt6 0:2f388b030837 95 * is up to date on disk
kbrahmbhatt6 0:2f388b030837 96 *
kbrahmbhatt6 0:2f388b030837 97 * @returns
kbrahmbhatt6 0:2f388b030837 98 * 0 on success or un-needed,
kbrahmbhatt6 0:2f388b030837 99 * -1 on error
kbrahmbhatt6 0:2f388b030837 100 */
kbrahmbhatt6 0:2f388b030837 101 virtual int fsync() = 0;
kbrahmbhatt6 0:2f388b030837 102
kbrahmbhatt6 0:2f388b030837 103 virtual off_t flen() {
kbrahmbhatt6 0:2f388b030837 104 /* remember our current position */
kbrahmbhatt6 0:2f388b030837 105 off_t pos = lseek(0, SEEK_CUR);
kbrahmbhatt6 0:2f388b030837 106 if(pos == -1) return -1;
kbrahmbhatt6 0:2f388b030837 107 /* seek to the end to get the file length */
kbrahmbhatt6 0:2f388b030837 108 off_t res = lseek(0, SEEK_END);
kbrahmbhatt6 0:2f388b030837 109 /* return to our old position */
kbrahmbhatt6 0:2f388b030837 110 lseek(pos, SEEK_SET);
kbrahmbhatt6 0:2f388b030837 111 return res;
kbrahmbhatt6 0:2f388b030837 112 }
kbrahmbhatt6 0:2f388b030837 113
kbrahmbhatt6 0:2f388b030837 114 virtual ~FileHandle();
kbrahmbhatt6 0:2f388b030837 115 };
kbrahmbhatt6 0:2f388b030837 116
kbrahmbhatt6 0:2f388b030837 117 } // namespace mbed
kbrahmbhatt6 0:2f388b030837 118
kbrahmbhatt6 0:2f388b030837 119 #endif