FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.

Dependencies:   mbed FXOS8700CQ MODSERIAL

Committer:
jwhammel
Date:
Wed Mar 06 21:11:49 2019 +0000
Revision:
84:fc8c9b39723a
Embedded Systems Midterm Project Spring 2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jwhammel 84:fc8c9b39723a 1 /* mbed Microcontroller Library
jwhammel 84:fc8c9b39723a 2 * Copyright (c) 2006-2013 ARM Limited
jwhammel 84:fc8c9b39723a 3 *
jwhammel 84:fc8c9b39723a 4 * Licensed under the Apache License, Version 2.0 (the "License");
jwhammel 84:fc8c9b39723a 5 * you may not use this file except in compliance with the License.
jwhammel 84:fc8c9b39723a 6 * You may obtain a copy of the License at
jwhammel 84:fc8c9b39723a 7 *
jwhammel 84:fc8c9b39723a 8 * http://www.apache.org/licenses/LICENSE-2.0
jwhammel 84:fc8c9b39723a 9 *
jwhammel 84:fc8c9b39723a 10 * Unless required by applicable law or agreed to in writing, software
jwhammel 84:fc8c9b39723a 11 * distributed under the License is distributed on an "AS IS" BASIS,
jwhammel 84:fc8c9b39723a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jwhammel 84:fc8c9b39723a 13 * See the License for the specific language governing permissions and
jwhammel 84:fc8c9b39723a 14 * limitations under the License.
jwhammel 84:fc8c9b39723a 15 */
jwhammel 84:fc8c9b39723a 16 #ifndef MBED_FILEHANDLE_H
jwhammel 84:fc8c9b39723a 17 #define MBED_FILEHANDLE_H
jwhammel 84:fc8c9b39723a 18
jwhammel 84:fc8c9b39723a 19 typedef int FILEHANDLE;
jwhammel 84:fc8c9b39723a 20
jwhammel 84:fc8c9b39723a 21 #include <stdio.h>
jwhammel 84:fc8c9b39723a 22
jwhammel 84:fc8c9b39723a 23 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
jwhammel 84:fc8c9b39723a 24 typedef int ssize_t;
jwhammel 84:fc8c9b39723a 25 typedef long off_t;
jwhammel 84:fc8c9b39723a 26
jwhammel 84:fc8c9b39723a 27 #else
jwhammel 84:fc8c9b39723a 28 # include <sys/types.h>
jwhammel 84:fc8c9b39723a 29 #endif
jwhammel 84:fc8c9b39723a 30
jwhammel 84:fc8c9b39723a 31 namespace mbed {
jwhammel 84:fc8c9b39723a 32
jwhammel 84:fc8c9b39723a 33 /** An OO equivalent of the internal FILEHANDLE variable
jwhammel 84:fc8c9b39723a 34 * and associated _sys_* functions.
jwhammel 84:fc8c9b39723a 35 *
jwhammel 84:fc8c9b39723a 36 * FileHandle is an abstract class, needing at least sys_write and
jwhammel 84:fc8c9b39723a 37 * sys_read to be implmented for a simple interactive device.
jwhammel 84:fc8c9b39723a 38 *
jwhammel 84:fc8c9b39723a 39 * No one ever directly tals to/instanciates a FileHandle - it gets
jwhammel 84:fc8c9b39723a 40 * created by FileSystem, and wrapped up by stdio.
jwhammel 84:fc8c9b39723a 41 */
jwhammel 84:fc8c9b39723a 42 class FileHandle {
jwhammel 84:fc8c9b39723a 43
jwhammel 84:fc8c9b39723a 44 public:
jwhammel 84:fc8c9b39723a 45 /** Write the contents of a buffer to the file
jwhammel 84:fc8c9b39723a 46 *
jwhammel 84:fc8c9b39723a 47 * @param buffer the buffer to write from
jwhammel 84:fc8c9b39723a 48 * @param length the number of characters to write
jwhammel 84:fc8c9b39723a 49 *
jwhammel 84:fc8c9b39723a 50 * @returns
jwhammel 84:fc8c9b39723a 51 * The number of characters written (possibly 0) on success, -1 on error.
jwhammel 84:fc8c9b39723a 52 */
jwhammel 84:fc8c9b39723a 53 virtual ssize_t write(const void* buffer, size_t length) = 0;
jwhammel 84:fc8c9b39723a 54
jwhammel 84:fc8c9b39723a 55 /** Close the file
jwhammel 84:fc8c9b39723a 56 *
jwhammel 84:fc8c9b39723a 57 * @returns
jwhammel 84:fc8c9b39723a 58 * Zero on success, -1 on error.
jwhammel 84:fc8c9b39723a 59 */
jwhammel 84:fc8c9b39723a 60 virtual int close() = 0;
jwhammel 84:fc8c9b39723a 61
jwhammel 84:fc8c9b39723a 62 /** Function read
jwhammel 84:fc8c9b39723a 63 * Reads the contents of the file into a buffer
jwhammel 84:fc8c9b39723a 64 *
jwhammel 84:fc8c9b39723a 65 * @param buffer the buffer to read in to
jwhammel 84:fc8c9b39723a 66 * @param length the number of characters to read
jwhammel 84:fc8c9b39723a 67 *
jwhammel 84:fc8c9b39723a 68 * @returns
jwhammel 84:fc8c9b39723a 69 * The number of characters read (zero at end of file) on success, -1 on error.
jwhammel 84:fc8c9b39723a 70 */
jwhammel 84:fc8c9b39723a 71 virtual ssize_t read(void* buffer, size_t length) = 0;
jwhammel 84:fc8c9b39723a 72
jwhammel 84:fc8c9b39723a 73 /** Check if the handle is for a interactive terminal device.
jwhammel 84:fc8c9b39723a 74 * If so, line buffered behaviour is used by default
jwhammel 84:fc8c9b39723a 75 *
jwhammel 84:fc8c9b39723a 76 * @returns
jwhammel 84:fc8c9b39723a 77 * 1 if it is a terminal,
jwhammel 84:fc8c9b39723a 78 * 0 otherwise
jwhammel 84:fc8c9b39723a 79 */
jwhammel 84:fc8c9b39723a 80 virtual int isatty() = 0;
jwhammel 84:fc8c9b39723a 81
jwhammel 84:fc8c9b39723a 82 /** Move the file position to a given offset from a given location.
jwhammel 84:fc8c9b39723a 83 *
jwhammel 84:fc8c9b39723a 84 * @param offset The offset from whence to move to
jwhammel 84:fc8c9b39723a 85 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
jwhammel 84:fc8c9b39723a 86 * current file position, or SEEK_END for the end of the file.
jwhammel 84:fc8c9b39723a 87 *
jwhammel 84:fc8c9b39723a 88 * @returns
jwhammel 84:fc8c9b39723a 89 * new file position on success,
jwhammel 84:fc8c9b39723a 90 * -1 on failure or unsupported
jwhammel 84:fc8c9b39723a 91 */
jwhammel 84:fc8c9b39723a 92 virtual off_t lseek(off_t offset, int whence) = 0;
jwhammel 84:fc8c9b39723a 93
jwhammel 84:fc8c9b39723a 94 /** Flush any buffers associated with the FileHandle, ensuring it
jwhammel 84:fc8c9b39723a 95 * is up to date on disk
jwhammel 84:fc8c9b39723a 96 *
jwhammel 84:fc8c9b39723a 97 * @returns
jwhammel 84:fc8c9b39723a 98 * 0 on success or un-needed,
jwhammel 84:fc8c9b39723a 99 * -1 on error
jwhammel 84:fc8c9b39723a 100 */
jwhammel 84:fc8c9b39723a 101 virtual int fsync() = 0;
jwhammel 84:fc8c9b39723a 102
jwhammel 84:fc8c9b39723a 103 virtual off_t flen() {
jwhammel 84:fc8c9b39723a 104 /* remember our current position */
jwhammel 84:fc8c9b39723a 105 off_t pos = lseek(0, SEEK_CUR);
jwhammel 84:fc8c9b39723a 106 if(pos == -1) return -1;
jwhammel 84:fc8c9b39723a 107 /* seek to the end to get the file length */
jwhammel 84:fc8c9b39723a 108 off_t res = lseek(0, SEEK_END);
jwhammel 84:fc8c9b39723a 109 /* return to our old position */
jwhammel 84:fc8c9b39723a 110 lseek(pos, SEEK_SET);
jwhammel 84:fc8c9b39723a 111 return res;
jwhammel 84:fc8c9b39723a 112 }
jwhammel 84:fc8c9b39723a 113
jwhammel 84:fc8c9b39723a 114 virtual ~FileHandle();
jwhammel 84:fc8c9b39723a 115 };
jwhammel 84:fc8c9b39723a 116
jwhammel 84:fc8c9b39723a 117 } // namespace mbed
jwhammel 84:fc8c9b39723a 118
jwhammel 84:fc8c9b39723a 119 #endif
jwhammel 84:fc8c9b39723a 120