Yihui Xiong / BLE_NODE_TEST

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Wed Oct 29 06:23:47 2014 +0000
Revision:
9:05f0b5a3a70a
initial

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 #include "platform.h"
yihui 9:05f0b5a3a70a 17 #include "FileHandle.h"
yihui 9:05f0b5a3a70a 18 #include "FileSystemLike.h"
yihui 9:05f0b5a3a70a 19 #include "FilePath.h"
yihui 9:05f0b5a3a70a 20 #include "serial_api.h"
yihui 9:05f0b5a3a70a 21 #include "toolchain.h"
yihui 9:05f0b5a3a70a 22 #include <errno.h>
yihui 9:05f0b5a3a70a 23
yihui 9:05f0b5a3a70a 24 #if defined(__ARMCC_VERSION)
yihui 9:05f0b5a3a70a 25 # include <rt_sys.h>
yihui 9:05f0b5a3a70a 26 # define PREFIX(x) _sys##x
yihui 9:05f0b5a3a70a 27 # define OPEN_MAX _SYS_OPEN
yihui 9:05f0b5a3a70a 28 # ifdef __MICROLIB
yihui 9:05f0b5a3a70a 29 # pragma import(__use_full_stdio)
yihui 9:05f0b5a3a70a 30 # endif
yihui 9:05f0b5a3a70a 31
yihui 9:05f0b5a3a70a 32 #elif defined(__ICCARM__)
yihui 9:05f0b5a3a70a 33 # include <yfuns.h>
yihui 9:05f0b5a3a70a 34 # define PREFIX(x) _##x
yihui 9:05f0b5a3a70a 35 # define OPEN_MAX 16
yihui 9:05f0b5a3a70a 36
yihui 9:05f0b5a3a70a 37 # define STDIN_FILENO 0
yihui 9:05f0b5a3a70a 38 # define STDOUT_FILENO 1
yihui 9:05f0b5a3a70a 39 # define STDERR_FILENO 2
yihui 9:05f0b5a3a70a 40
yihui 9:05f0b5a3a70a 41 #else
yihui 9:05f0b5a3a70a 42 # include <sys/stat.h>
yihui 9:05f0b5a3a70a 43 # include <sys/unistd.h>
yihui 9:05f0b5a3a70a 44 # include <sys/syslimits.h>
yihui 9:05f0b5a3a70a 45 # define PREFIX(x) x
yihui 9:05f0b5a3a70a 46 #endif
yihui 9:05f0b5a3a70a 47
yihui 9:05f0b5a3a70a 48 using namespace mbed;
yihui 9:05f0b5a3a70a 49
yihui 9:05f0b5a3a70a 50 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
yihui 9:05f0b5a3a70a 51 // Before version 5.03, we were using a patched version of microlib with proper names
yihui 9:05f0b5a3a70a 52 extern const char __stdin_name[] = ":tt";
yihui 9:05f0b5a3a70a 53 extern const char __stdout_name[] = ":tt";
yihui 9:05f0b5a3a70a 54 extern const char __stderr_name[] = ":tt";
yihui 9:05f0b5a3a70a 55
yihui 9:05f0b5a3a70a 56 #else
yihui 9:05f0b5a3a70a 57 extern const char __stdin_name[] = "/stdin";
yihui 9:05f0b5a3a70a 58 extern const char __stdout_name[] = "/stdout";
yihui 9:05f0b5a3a70a 59 extern const char __stderr_name[] = "/stderr";
yihui 9:05f0b5a3a70a 60 #endif
yihui 9:05f0b5a3a70a 61
yihui 9:05f0b5a3a70a 62 /* newlib has the filehandle field in the FILE struct as a short, so
yihui 9:05f0b5a3a70a 63 * we can't just return a Filehandle* from _open and instead have to
yihui 9:05f0b5a3a70a 64 * put it in a filehandles array and return the index into that array
yihui 9:05f0b5a3a70a 65 * (or rather index+3, as filehandles 0-2 are stdin/out/err).
yihui 9:05f0b5a3a70a 66 */
yihui 9:05f0b5a3a70a 67 static FileHandle *filehandles[OPEN_MAX];
yihui 9:05f0b5a3a70a 68
yihui 9:05f0b5a3a70a 69 FileHandle::~FileHandle() {
yihui 9:05f0b5a3a70a 70 /* Remove all open filehandles for this */
yihui 9:05f0b5a3a70a 71 for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
yihui 9:05f0b5a3a70a 72 if (filehandles[fh_i] == this) {
yihui 9:05f0b5a3a70a 73 filehandles[fh_i] = NULL;
yihui 9:05f0b5a3a70a 74 }
yihui 9:05f0b5a3a70a 75 }
yihui 9:05f0b5a3a70a 76 }
yihui 9:05f0b5a3a70a 77
yihui 9:05f0b5a3a70a 78 #if DEVICE_SERIAL
yihui 9:05f0b5a3a70a 79 extern int stdio_uart_inited;
yihui 9:05f0b5a3a70a 80 extern serial_t stdio_uart;
yihui 9:05f0b5a3a70a 81 #endif
yihui 9:05f0b5a3a70a 82
yihui 9:05f0b5a3a70a 83 static void init_serial() {
yihui 9:05f0b5a3a70a 84 #if DEVICE_SERIAL
yihui 9:05f0b5a3a70a 85 if (stdio_uart_inited) return;
yihui 9:05f0b5a3a70a 86 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
yihui 9:05f0b5a3a70a 87 #endif
yihui 9:05f0b5a3a70a 88 }
yihui 9:05f0b5a3a70a 89
yihui 9:05f0b5a3a70a 90 static inline int openmode_to_posix(int openmode) {
yihui 9:05f0b5a3a70a 91 int posix = openmode;
yihui 9:05f0b5a3a70a 92 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 93 if (openmode & OPEN_PLUS) {
yihui 9:05f0b5a3a70a 94 posix = O_RDWR;
yihui 9:05f0b5a3a70a 95 } else if(openmode & OPEN_W) {
yihui 9:05f0b5a3a70a 96 posix = O_WRONLY;
yihui 9:05f0b5a3a70a 97 } else if(openmode & OPEN_A) {
yihui 9:05f0b5a3a70a 98 posix = O_WRONLY|O_APPEND;
yihui 9:05f0b5a3a70a 99 } else {
yihui 9:05f0b5a3a70a 100 posix = O_RDONLY;
yihui 9:05f0b5a3a70a 101 }
yihui 9:05f0b5a3a70a 102 /* a, w, a+, w+ all create if file does not already exist */
yihui 9:05f0b5a3a70a 103 if (openmode & (OPEN_A|OPEN_W)) {
yihui 9:05f0b5a3a70a 104 posix |= O_CREAT;
yihui 9:05f0b5a3a70a 105 }
yihui 9:05f0b5a3a70a 106 /* w and w+ truncate */
yihui 9:05f0b5a3a70a 107 if (openmode & OPEN_W) {
yihui 9:05f0b5a3a70a 108 posix |= O_TRUNC;
yihui 9:05f0b5a3a70a 109 }
yihui 9:05f0b5a3a70a 110 #elif defined(__ICCARM__)
yihui 9:05f0b5a3a70a 111 switch (openmode & _LLIO_RDWRMASK) {
yihui 9:05f0b5a3a70a 112 case _LLIO_RDONLY: posix = O_RDONLY; break;
yihui 9:05f0b5a3a70a 113 case _LLIO_WRONLY: posix = O_WRONLY; break;
yihui 9:05f0b5a3a70a 114 case _LLIO_RDWR : posix = O_RDWR ; break;
yihui 9:05f0b5a3a70a 115 }
yihui 9:05f0b5a3a70a 116 if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
yihui 9:05f0b5a3a70a 117 if (openmode & _LLIO_APPEND) posix |= O_APPEND;
yihui 9:05f0b5a3a70a 118 if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
yihui 9:05f0b5a3a70a 119 #endif
yihui 9:05f0b5a3a70a 120 return posix;
yihui 9:05f0b5a3a70a 121 }
yihui 9:05f0b5a3a70a 122
yihui 9:05f0b5a3a70a 123 extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
yihui 9:05f0b5a3a70a 124 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
yihui 9:05f0b5a3a70a 125 // Before version 5.03, we were using a patched version of microlib with proper names
yihui 9:05f0b5a3a70a 126 // This is the workaround that the microlib author suggested us
yihui 9:05f0b5a3a70a 127 static int n = 0;
yihui 9:05f0b5a3a70a 128 if (!std::strcmp(name, ":tt")) return n++;
yihui 9:05f0b5a3a70a 129
yihui 9:05f0b5a3a70a 130 #else
yihui 9:05f0b5a3a70a 131 /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
yihui 9:05f0b5a3a70a 132 */
yihui 9:05f0b5a3a70a 133 if (std::strcmp(name, __stdin_name) == 0) {
yihui 9:05f0b5a3a70a 134 init_serial();
yihui 9:05f0b5a3a70a 135 return 0;
yihui 9:05f0b5a3a70a 136 } else if (std::strcmp(name, __stdout_name) == 0) {
yihui 9:05f0b5a3a70a 137 init_serial();
yihui 9:05f0b5a3a70a 138 return 1;
yihui 9:05f0b5a3a70a 139 } else if (std::strcmp(name, __stderr_name) == 0) {
yihui 9:05f0b5a3a70a 140 init_serial();
yihui 9:05f0b5a3a70a 141 return 2;
yihui 9:05f0b5a3a70a 142 }
yihui 9:05f0b5a3a70a 143 #endif
yihui 9:05f0b5a3a70a 144
yihui 9:05f0b5a3a70a 145 // find the first empty slot in filehandles
yihui 9:05f0b5a3a70a 146 unsigned int fh_i;
yihui 9:05f0b5a3a70a 147 for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
yihui 9:05f0b5a3a70a 148 if (filehandles[fh_i] == NULL) break;
yihui 9:05f0b5a3a70a 149 }
yihui 9:05f0b5a3a70a 150 if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
yihui 9:05f0b5a3a70a 151 return -1;
yihui 9:05f0b5a3a70a 152 }
yihui 9:05f0b5a3a70a 153
yihui 9:05f0b5a3a70a 154 FileHandle *res;
yihui 9:05f0b5a3a70a 155
yihui 9:05f0b5a3a70a 156 /* FILENAME: ":0x12345678" describes a FileLike* */
yihui 9:05f0b5a3a70a 157 if (name[0] == ':') {
yihui 9:05f0b5a3a70a 158 void *p;
yihui 9:05f0b5a3a70a 159 sscanf(name, ":%p", &p);
yihui 9:05f0b5a3a70a 160 res = (FileHandle*)p;
yihui 9:05f0b5a3a70a 161
yihui 9:05f0b5a3a70a 162 /* FILENAME: "/file_system/file_name" */
yihui 9:05f0b5a3a70a 163 } else {
yihui 9:05f0b5a3a70a 164 FilePath path(name);
yihui 9:05f0b5a3a70a 165
yihui 9:05f0b5a3a70a 166 if (!path.exists())
yihui 9:05f0b5a3a70a 167 return -1;
yihui 9:05f0b5a3a70a 168 else if (path.isFile()) {
yihui 9:05f0b5a3a70a 169 res = path.file();
yihui 9:05f0b5a3a70a 170 } else {
yihui 9:05f0b5a3a70a 171 FileSystemLike *fs = path.fileSystem();
yihui 9:05f0b5a3a70a 172 if (fs == NULL) return -1;
yihui 9:05f0b5a3a70a 173 int posix_mode = openmode_to_posix(openmode);
yihui 9:05f0b5a3a70a 174 res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
yihui 9:05f0b5a3a70a 175 }
yihui 9:05f0b5a3a70a 176 }
yihui 9:05f0b5a3a70a 177
yihui 9:05f0b5a3a70a 178 if (res == NULL) return -1;
yihui 9:05f0b5a3a70a 179 filehandles[fh_i] = res;
yihui 9:05f0b5a3a70a 180
yihui 9:05f0b5a3a70a 181 return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
yihui 9:05f0b5a3a70a 182 }
yihui 9:05f0b5a3a70a 183
yihui 9:05f0b5a3a70a 184 extern "C" int PREFIX(_close)(FILEHANDLE fh) {
yihui 9:05f0b5a3a70a 185 if (fh < 3) return 0;
yihui 9:05f0b5a3a70a 186
yihui 9:05f0b5a3a70a 187 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 188 filehandles[fh-3] = NULL;
yihui 9:05f0b5a3a70a 189 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 190
yihui 9:05f0b5a3a70a 191 return fhc->close();
yihui 9:05f0b5a3a70a 192 }
yihui 9:05f0b5a3a70a 193
yihui 9:05f0b5a3a70a 194 #if defined(__ICCARM__)
yihui 9:05f0b5a3a70a 195 extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) {
yihui 9:05f0b5a3a70a 196 #else
yihui 9:05f0b5a3a70a 197 extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
yihui 9:05f0b5a3a70a 198 #endif
yihui 9:05f0b5a3a70a 199 int n; // n is the number of bytes written
yihui 9:05f0b5a3a70a 200 if (fh < 3) {
yihui 9:05f0b5a3a70a 201 #if DEVICE_SERIAL
yihui 9:05f0b5a3a70a 202 if (!stdio_uart_inited) init_serial();
yihui 9:05f0b5a3a70a 203 for (unsigned int i = 0; i < length; i++) {
yihui 9:05f0b5a3a70a 204 serial_putc(&stdio_uart, buffer[i]);
yihui 9:05f0b5a3a70a 205 }
yihui 9:05f0b5a3a70a 206 #endif
yihui 9:05f0b5a3a70a 207 n = length;
yihui 9:05f0b5a3a70a 208 } else {
yihui 9:05f0b5a3a70a 209 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 210 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 211
yihui 9:05f0b5a3a70a 212 n = fhc->write(buffer, length);
yihui 9:05f0b5a3a70a 213 }
yihui 9:05f0b5a3a70a 214 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 215 return length-n;
yihui 9:05f0b5a3a70a 216 #else
yihui 9:05f0b5a3a70a 217 return n;
yihui 9:05f0b5a3a70a 218 #endif
yihui 9:05f0b5a3a70a 219 }
yihui 9:05f0b5a3a70a 220
yihui 9:05f0b5a3a70a 221 #if defined(__ICCARM__)
yihui 9:05f0b5a3a70a 222 extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) {
yihui 9:05f0b5a3a70a 223 #else
yihui 9:05f0b5a3a70a 224 extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
yihui 9:05f0b5a3a70a 225 #endif
yihui 9:05f0b5a3a70a 226 int n; // n is the number of bytes read
yihui 9:05f0b5a3a70a 227 if (fh < 3) {
yihui 9:05f0b5a3a70a 228 // only read a character at a time from stdin
yihui 9:05f0b5a3a70a 229 #if DEVICE_SERIAL
yihui 9:05f0b5a3a70a 230 if (!stdio_uart_inited) init_serial();
yihui 9:05f0b5a3a70a 231 *buffer = serial_getc(&stdio_uart);
yihui 9:05f0b5a3a70a 232 #endif
yihui 9:05f0b5a3a70a 233 n = 1;
yihui 9:05f0b5a3a70a 234 } else {
yihui 9:05f0b5a3a70a 235 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 236 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 237
yihui 9:05f0b5a3a70a 238 n = fhc->read(buffer, length);
yihui 9:05f0b5a3a70a 239 }
yihui 9:05f0b5a3a70a 240 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 241 return length-n;
yihui 9:05f0b5a3a70a 242 #else
yihui 9:05f0b5a3a70a 243 return n;
yihui 9:05f0b5a3a70a 244 #endif
yihui 9:05f0b5a3a70a 245 }
yihui 9:05f0b5a3a70a 246
yihui 9:05f0b5a3a70a 247 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 248 extern "C" int PREFIX(_istty)(FILEHANDLE fh)
yihui 9:05f0b5a3a70a 249 #else
yihui 9:05f0b5a3a70a 250 extern "C" int _isatty(FILEHANDLE fh)
yihui 9:05f0b5a3a70a 251 #endif
yihui 9:05f0b5a3a70a 252 {
yihui 9:05f0b5a3a70a 253 /* stdin, stdout and stderr should be tty */
yihui 9:05f0b5a3a70a 254 if (fh < 3) return 1;
yihui 9:05f0b5a3a70a 255
yihui 9:05f0b5a3a70a 256 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 257 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 258
yihui 9:05f0b5a3a70a 259 return fhc->isatty();
yihui 9:05f0b5a3a70a 260 }
yihui 9:05f0b5a3a70a 261
yihui 9:05f0b5a3a70a 262 extern "C"
yihui 9:05f0b5a3a70a 263 #if defined(__ARMCC_VERSION)
yihui 9:05f0b5a3a70a 264 int _sys_seek(FILEHANDLE fh, long position)
yihui 9:05f0b5a3a70a 265 #elif defined(__ICCARM__)
yihui 9:05f0b5a3a70a 266 long __lseek(int fh, long offset, int whence)
yihui 9:05f0b5a3a70a 267 #else
yihui 9:05f0b5a3a70a 268 int _lseek(FILEHANDLE fh, int offset, int whence)
yihui 9:05f0b5a3a70a 269 #endif
yihui 9:05f0b5a3a70a 270 {
yihui 9:05f0b5a3a70a 271 if (fh < 3) return 0;
yihui 9:05f0b5a3a70a 272
yihui 9:05f0b5a3a70a 273 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 274 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 275
yihui 9:05f0b5a3a70a 276 #if defined(__ARMCC_VERSION)
yihui 9:05f0b5a3a70a 277 return fhc->lseek(position, SEEK_SET);
yihui 9:05f0b5a3a70a 278 #else
yihui 9:05f0b5a3a70a 279 return fhc->lseek(offset, whence);
yihui 9:05f0b5a3a70a 280 #endif
yihui 9:05f0b5a3a70a 281 }
yihui 9:05f0b5a3a70a 282
yihui 9:05f0b5a3a70a 283 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 284 extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
yihui 9:05f0b5a3a70a 285 if (fh < 3) return 0;
yihui 9:05f0b5a3a70a 286
yihui 9:05f0b5a3a70a 287 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 288 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 289
yihui 9:05f0b5a3a70a 290 return fhc->fsync();
yihui 9:05f0b5a3a70a 291 }
yihui 9:05f0b5a3a70a 292
yihui 9:05f0b5a3a70a 293 extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
yihui 9:05f0b5a3a70a 294 if (fh < 3) return 0;
yihui 9:05f0b5a3a70a 295
yihui 9:05f0b5a3a70a 296 FileHandle* fhc = filehandles[fh-3];
yihui 9:05f0b5a3a70a 297 if (fhc == NULL) return -1;
yihui 9:05f0b5a3a70a 298
yihui 9:05f0b5a3a70a 299 return fhc->flen();
yihui 9:05f0b5a3a70a 300 }
yihui 9:05f0b5a3a70a 301 #endif
yihui 9:05f0b5a3a70a 302
yihui 9:05f0b5a3a70a 303
yihui 9:05f0b5a3a70a 304 #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
yihui 9:05f0b5a3a70a 305 extern "C" int _fstat(int fd, struct stat *st) {
yihui 9:05f0b5a3a70a 306 if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) {
yihui 9:05f0b5a3a70a 307 st->st_mode = S_IFCHR;
yihui 9:05f0b5a3a70a 308 return 0;
yihui 9:05f0b5a3a70a 309 }
yihui 9:05f0b5a3a70a 310
yihui 9:05f0b5a3a70a 311 errno = EBADF;
yihui 9:05f0b5a3a70a 312 return -1;
yihui 9:05f0b5a3a70a 313 }
yihui 9:05f0b5a3a70a 314 #endif
yihui 9:05f0b5a3a70a 315
yihui 9:05f0b5a3a70a 316 namespace std {
yihui 9:05f0b5a3a70a 317 extern "C" int remove(const char *path) {
yihui 9:05f0b5a3a70a 318 FilePath fp(path);
yihui 9:05f0b5a3a70a 319 FileSystemLike *fs = fp.fileSystem();
yihui 9:05f0b5a3a70a 320 if (fs == NULL) return -1;
yihui 9:05f0b5a3a70a 321
yihui 9:05f0b5a3a70a 322 return fs->remove(fp.fileName());
yihui 9:05f0b5a3a70a 323 }
yihui 9:05f0b5a3a70a 324
yihui 9:05f0b5a3a70a 325 extern "C" int rename(const char *oldname, const char *newname) {
yihui 9:05f0b5a3a70a 326 FilePath fpOld(oldname);
yihui 9:05f0b5a3a70a 327 FilePath fpNew(newname);
yihui 9:05f0b5a3a70a 328 FileSystemLike *fsOld = fpOld.fileSystem();
yihui 9:05f0b5a3a70a 329 FileSystemLike *fsNew = fpNew.fileSystem();
yihui 9:05f0b5a3a70a 330
yihui 9:05f0b5a3a70a 331 /* rename only if both files are on the same FS */
yihui 9:05f0b5a3a70a 332 if (fsOld != fsNew || fsOld == NULL) return -1;
yihui 9:05f0b5a3a70a 333
yihui 9:05f0b5a3a70a 334 return fsOld->rename(fpOld.fileName(), fpNew.fileName());
yihui 9:05f0b5a3a70a 335 }
yihui 9:05f0b5a3a70a 336
yihui 9:05f0b5a3a70a 337 extern "C" char *tmpnam(char *s) {
yihui 9:05f0b5a3a70a 338 return NULL;
yihui 9:05f0b5a3a70a 339 }
yihui 9:05f0b5a3a70a 340
yihui 9:05f0b5a3a70a 341 extern "C" FILE *tmpfile() {
yihui 9:05f0b5a3a70a 342 return NULL;
yihui 9:05f0b5a3a70a 343 }
yihui 9:05f0b5a3a70a 344 } // namespace std
yihui 9:05f0b5a3a70a 345
yihui 9:05f0b5a3a70a 346 #ifdef __ARMCC_VERSION
yihui 9:05f0b5a3a70a 347 extern "C" char *_sys_command_string(char *cmd, int len) {
yihui 9:05f0b5a3a70a 348 return NULL;
yihui 9:05f0b5a3a70a 349 }
yihui 9:05f0b5a3a70a 350 #endif
yihui 9:05f0b5a3a70a 351
yihui 9:05f0b5a3a70a 352 extern "C" DIR *opendir(const char *path) {
yihui 9:05f0b5a3a70a 353 /* root dir is FileSystemLike */
yihui 9:05f0b5a3a70a 354 if (path[0] == '/' && path[1] == 0) {
yihui 9:05f0b5a3a70a 355 return FileSystemLike::opendir();
yihui 9:05f0b5a3a70a 356 }
yihui 9:05f0b5a3a70a 357
yihui 9:05f0b5a3a70a 358 FilePath fp(path);
yihui 9:05f0b5a3a70a 359 FileSystemLike* fs = fp.fileSystem();
yihui 9:05f0b5a3a70a 360 if (fs == NULL) return NULL;
yihui 9:05f0b5a3a70a 361
yihui 9:05f0b5a3a70a 362 return fs->opendir(fp.fileName());
yihui 9:05f0b5a3a70a 363 }
yihui 9:05f0b5a3a70a 364
yihui 9:05f0b5a3a70a 365 extern "C" struct dirent *readdir(DIR *dir) {
yihui 9:05f0b5a3a70a 366 return dir->readdir();
yihui 9:05f0b5a3a70a 367 }
yihui 9:05f0b5a3a70a 368
yihui 9:05f0b5a3a70a 369 extern "C" int closedir(DIR *dir) {
yihui 9:05f0b5a3a70a 370 return dir->closedir();
yihui 9:05f0b5a3a70a 371 }
yihui 9:05f0b5a3a70a 372
yihui 9:05f0b5a3a70a 373 extern "C" void rewinddir(DIR *dir) {
yihui 9:05f0b5a3a70a 374 dir->rewinddir();
yihui 9:05f0b5a3a70a 375 }
yihui 9:05f0b5a3a70a 376
yihui 9:05f0b5a3a70a 377 extern "C" off_t telldir(DIR *dir) {
yihui 9:05f0b5a3a70a 378 return dir->telldir();
yihui 9:05f0b5a3a70a 379 }
yihui 9:05f0b5a3a70a 380
yihui 9:05f0b5a3a70a 381 extern "C" void seekdir(DIR *dir, off_t off) {
yihui 9:05f0b5a3a70a 382 dir->seekdir(off);
yihui 9:05f0b5a3a70a 383 }
yihui 9:05f0b5a3a70a 384
yihui 9:05f0b5a3a70a 385 extern "C" int mkdir(const char *path, mode_t mode) {
yihui 9:05f0b5a3a70a 386 FilePath fp(path);
yihui 9:05f0b5a3a70a 387 FileSystemLike *fs = fp.fileSystem();
yihui 9:05f0b5a3a70a 388 if (fs == NULL) return -1;
yihui 9:05f0b5a3a70a 389
yihui 9:05f0b5a3a70a 390 return fs->mkdir(fp.fileName(), mode);
yihui 9:05f0b5a3a70a 391 }
yihui 9:05f0b5a3a70a 392
yihui 9:05f0b5a3a70a 393 #if defined(TOOLCHAIN_GCC)
yihui 9:05f0b5a3a70a 394 /* prevents the exception handling name demangling code getting pulled in */
yihui 9:05f0b5a3a70a 395 #include "mbed_error.h"
yihui 9:05f0b5a3a70a 396 namespace __gnu_cxx {
yihui 9:05f0b5a3a70a 397 void __verbose_terminate_handler() {
yihui 9:05f0b5a3a70a 398 error("Exception");
yihui 9:05f0b5a3a70a 399 }
yihui 9:05f0b5a3a70a 400 }
yihui 9:05f0b5a3a70a 401 extern "C" WEAK void __cxa_pure_virtual(void);
yihui 9:05f0b5a3a70a 402 extern "C" WEAK void __cxa_pure_virtual(void) {
yihui 9:05f0b5a3a70a 403 exit(1);
yihui 9:05f0b5a3a70a 404 }
yihui 9:05f0b5a3a70a 405
yihui 9:05f0b5a3a70a 406 #endif
yihui 9:05f0b5a3a70a 407
yihui 9:05f0b5a3a70a 408 // ****************************************************************************
yihui 9:05f0b5a3a70a 409 // mbed_main is a function that is called before main()
yihui 9:05f0b5a3a70a 410 // mbed_sdk_init() is also a function that is called before main(), but unlike
yihui 9:05f0b5a3a70a 411 // mbed_main(), it is not meant for user code, but for the SDK itself to perform
yihui 9:05f0b5a3a70a 412 // initializations before main() is called.
yihui 9:05f0b5a3a70a 413
yihui 9:05f0b5a3a70a 414 extern "C" WEAK void mbed_main(void);
yihui 9:05f0b5a3a70a 415 extern "C" WEAK void mbed_main(void) {
yihui 9:05f0b5a3a70a 416 }
yihui 9:05f0b5a3a70a 417
yihui 9:05f0b5a3a70a 418 extern "C" WEAK void mbed_sdk_init(void);
yihui 9:05f0b5a3a70a 419 extern "C" WEAK void mbed_sdk_init(void) {
yihui 9:05f0b5a3a70a 420 }
yihui 9:05f0b5a3a70a 421
yihui 9:05f0b5a3a70a 422 #if defined(TOOLCHAIN_ARM)
yihui 9:05f0b5a3a70a 423 extern "C" int $Super$$main(void);
yihui 9:05f0b5a3a70a 424
yihui 9:05f0b5a3a70a 425 extern "C" int $Sub$$main(void) {
yihui 9:05f0b5a3a70a 426 mbed_sdk_init();
yihui 9:05f0b5a3a70a 427 mbed_main();
yihui 9:05f0b5a3a70a 428 return $Super$$main();
yihui 9:05f0b5a3a70a 429 }
yihui 9:05f0b5a3a70a 430 #elif defined(TOOLCHAIN_GCC)
yihui 9:05f0b5a3a70a 431 extern "C" int __real_main(void);
yihui 9:05f0b5a3a70a 432
yihui 9:05f0b5a3a70a 433 extern "C" int __wrap_main(void) {
yihui 9:05f0b5a3a70a 434 mbed_sdk_init();
yihui 9:05f0b5a3a70a 435 mbed_main();
yihui 9:05f0b5a3a70a 436 return __real_main();
yihui 9:05f0b5a3a70a 437 }
yihui 9:05f0b5a3a70a 438 #elif defined(TOOLCHAIN_IAR)
yihui 9:05f0b5a3a70a 439 // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
yihui 9:05f0b5a3a70a 440 // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
yihui 9:05f0b5a3a70a 441 // 'main' to another symbol looses the original 'main' symbol. However, its startup
yihui 9:05f0b5a3a70a 442 // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
yihui 9:05f0b5a3a70a 443 // Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
yihui 9:05f0b5a3a70a 444 extern "C" void __iar_argc_argv() {
yihui 9:05f0b5a3a70a 445 mbed_sdk_init();
yihui 9:05f0b5a3a70a 446 mbed_main();
yihui 9:05f0b5a3a70a 447 }
yihui 9:05f0b5a3a70a 448 #endif
yihui 9:05f0b5a3a70a 449
yihui 9:05f0b5a3a70a 450 // Provide implementation of _sbrk (low-level dynamic memory allocation
yihui 9:05f0b5a3a70a 451 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
yihui 9:05f0b5a3a70a 452 // SP. This make it compatible with RTX RTOS thread stacks.
yihui 9:05f0b5a3a70a 453 #if defined(TOOLCHAIN_GCC_ARM)
yihui 9:05f0b5a3a70a 454 // Linker defined symbol used by _sbrk to indicate where heap should start.
yihui 9:05f0b5a3a70a 455 extern "C" int __end__;
yihui 9:05f0b5a3a70a 456
yihui 9:05f0b5a3a70a 457 // Turn off the errno macro and use actual global variable instead.
yihui 9:05f0b5a3a70a 458 #undef errno
yihui 9:05f0b5a3a70a 459 extern "C" int errno;
yihui 9:05f0b5a3a70a 460
yihui 9:05f0b5a3a70a 461 // For ARM7 only
yihui 9:05f0b5a3a70a 462 register unsigned char * stack_ptr __asm ("sp");
yihui 9:05f0b5a3a70a 463
yihui 9:05f0b5a3a70a 464 // Dynamic memory allocation related syscall.
yihui 9:05f0b5a3a70a 465 extern "C" caddr_t _sbrk(int incr) {
yihui 9:05f0b5a3a70a 466 static unsigned char* heap = (unsigned char*)&__end__;
yihui 9:05f0b5a3a70a 467 unsigned char* prev_heap = heap;
yihui 9:05f0b5a3a70a 468 unsigned char* new_heap = heap + incr;
yihui 9:05f0b5a3a70a 469
yihui 9:05f0b5a3a70a 470 #if defined(TARGET_ARM7)
yihui 9:05f0b5a3a70a 471 if (new_heap >= stack_ptr) {
yihui 9:05f0b5a3a70a 472 #else
yihui 9:05f0b5a3a70a 473 if (new_heap >= (unsigned char*)__get_MSP()) {
yihui 9:05f0b5a3a70a 474 #endif
yihui 9:05f0b5a3a70a 475 errno = ENOMEM;
yihui 9:05f0b5a3a70a 476 return (caddr_t)-1;
yihui 9:05f0b5a3a70a 477 }
yihui 9:05f0b5a3a70a 478
yihui 9:05f0b5a3a70a 479 heap = new_heap;
yihui 9:05f0b5a3a70a 480 return (caddr_t) prev_heap;
yihui 9:05f0b5a3a70a 481 }
yihui 9:05f0b5a3a70a 482 #endif