This is the final version of Mini Gateway for Automation and Security desgined for Renesas GR Peach Design Contest

Dependencies:   GR-PEACH_video GraphicsFramework HTTPServer R_BSP mbed-rpc mbed-rtos Socket lwip-eth lwip-sys lwip FATFileSystem

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileHandle.h Source File

FileHandle.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_FILEHANDLE_H
00017 #define MBED_FILEHANDLE_H
00018 
00019 typedef int FILEHANDLE;
00020 
00021 #include <stdio.h>
00022 
00023 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
00024 typedef int ssize_t;
00025 typedef long off_t;
00026 
00027 #else
00028 #   include <sys/types.h>
00029 #endif
00030 
00031 namespace mbed {
00032 /** \addtogroup drivers */
00033 /** @{*/
00034 
00035 /** An OO equivalent of the internal FILEHANDLE variable
00036  *  and associated _sys_* functions.
00037  *
00038  * FileHandle is an abstract class, needing at least sys_write and
00039  *  sys_read to be implmented for a simple interactive device.
00040  *
00041  * No one ever directly tals to/instanciates a FileHandle - it gets
00042  *  created by FileSystem, and wrapped up by stdio.
00043  *
00044  * @Note Synchronization level: Set by subclass
00045  */
00046 class FileHandle {
00047 
00048 public:
00049     /** Write the contents of a buffer to the file
00050      *
00051      *  @param buffer the buffer to write from
00052      *  @param length the number of characters to write
00053      *
00054      *  @returns
00055      *  The number of characters written (possibly 0) on success, -1 on error.
00056      */
00057     virtual ssize_t write(const void* buffer, size_t length) = 0;
00058 
00059     /** Close the file
00060      *
00061      *  @returns
00062      *  Zero on success, -1 on error.
00063      */
00064     virtual int close() = 0;
00065 
00066     /** Function read
00067      *  Reads the contents of the file into a buffer
00068      *
00069      *  @param buffer the buffer to read in to
00070      *  @param length the number of characters to read
00071      *
00072      *  @returns
00073      *  The number of characters read (zero at end of file) on success, -1 on error.
00074      */
00075     virtual ssize_t read(void* buffer, size_t length) = 0;
00076 
00077     /** Check if the handle is for a interactive terminal device.
00078      * If so, line buffered behaviour is used by default
00079      *
00080      *  @returns
00081      *    1 if it is a terminal,
00082      *    0 otherwise
00083      */
00084     virtual int isatty() = 0;
00085 
00086     /** Move the file position to a given offset from a given location.
00087      *
00088      *  @param offset The offset from whence to move to
00089      *  @param whence SEEK_SET for the start of the file, SEEK_CUR for the
00090      *   current file position, or SEEK_END for the end of the file.
00091      *
00092      *  @returns
00093      *    new file position on success,
00094      *    -1 on failure or unsupported
00095      */
00096     virtual off_t lseek(off_t offset, int whence) = 0;
00097 
00098     /** Flush any buffers associated with the FileHandle, ensuring it
00099      *  is up to date on disk
00100      *
00101      *  @returns
00102      *    0 on success or un-needed,
00103      *   -1 on error
00104      */
00105     virtual int fsync() = 0;
00106 
00107     virtual off_t flen() {
00108         lock();
00109         /* remember our current position */
00110         off_t pos = lseek(0, SEEK_CUR);
00111         if(pos == -1) {
00112             unlock();
00113             return -1;
00114         }
00115         /* seek to the end to get the file length */
00116         off_t res = lseek(0, SEEK_END);
00117         /* return to our old position */
00118         lseek(pos, SEEK_SET);
00119         unlock();
00120         return res;
00121     }
00122 
00123     virtual ~FileHandle();
00124 
00125 protected:
00126 
00127     /** Acquire exclusive access to this object.
00128      */
00129     virtual void lock() {
00130         // Stub
00131     }
00132 
00133     /** Release exclusive access to this object.
00134      */
00135     virtual void unlock() {
00136         // Stub
00137     }
00138 };
00139 
00140 } // namespace mbed
00141 
00142 #endif
00143 
00144 /** @}*/