Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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