customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

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