Mistake on this page?
Report an issue in GitHub or email us
mbed_retarget.h
1 /*
2  * mbed Microcontroller Library
3  * Copyright (c) 2006-2019 ARM Limited
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #ifndef RETARGET_H
21 #define RETARGET_H
22 
23 #if __cplusplus
24 #include <cstdio>
25 #else
26 #include <stdio.h>
27 #endif //__cplusplus
28 #include <stdint.h>
29 #include <stddef.h>
30 
31 /* Include logic for errno so we can get errno defined but not bring in error_t,
32  * including errno here prevents an include later, which would redefine our
33  * error codes
34  */
35 #ifndef __error_t_defined
36 #define __error_t_defined 1
37 #include <errno.h>
38 #undef __error_t_defined
39 #else
40 #include <errno.h>
41 #endif
42 
43 /* We can get the following standard types from sys/types for gcc, but we
44  * need to define the types ourselves for the other compilers that normally
45  * target embedded systems */
46 typedef signed int ssize_t; ///< Signed size type, usually encodes negative errors
47 typedef signed long off_t; ///< Offset in a data stream
48 typedef unsigned int nfds_t; ///< Number of file descriptors
49 typedef unsigned long long fsblkcnt_t; ///< Count of file system blocks
50 #if defined(__ARMCC_VERSION) || !defined(__GNUC__)
51 typedef unsigned int mode_t; ///< Mode for opening files
52 typedef unsigned int dev_t; ///< Device ID type
53 typedef unsigned long ino_t; ///< File serial number
54 typedef unsigned int nlink_t; ///< Number of links to a file
55 typedef unsigned int uid_t; ///< User ID
56 typedef unsigned int gid_t; ///< Group ID
57 #endif
58 
59 /* Flags for open() and fcntl(GETFL/SETFL)
60  * At present, fcntl only supports reading and writing O_NONBLOCK
61  */
62 #define O_RDONLY 0 ///< Open for reading
63 #define O_WRONLY 1 ///< Open for writing
64 #define O_RDWR 2 ///< Open for reading and writing
65 #define O_NONBLOCK 0x0004 ///< Non-blocking mode
66 #define O_APPEND 0x0008 ///< Set file offset to end of file prior to each write
67 #define O_CREAT 0x0200 ///< Create file if it does not exist
68 #define O_TRUNC 0x0400 ///< Truncate file to zero length
69 #define O_EXCL 0x0800 ///< Fail if file exists
70 #define O_BINARY 0x8000 ///< Open file in binary mode
71 
72 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
73 
74 #define NAME_MAX 255 ///< Maximum size of a name in a file path
75 
76 #define STDIN_FILENO 0
77 #define STDOUT_FILENO 1
78 #define STDERR_FILENO 2
79 
80 #include <time.h>
81 
82 /** \addtogroup platform-public-api */
83 /** @{*/
84 
85 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
86 /**
87  * \defgroup platform_retarget Retarget functions
88  * @{
89  */
90 
91 /* DIR declarations must also be here */
92 #if __cplusplus
93 namespace mbed {
94 class FileHandle;
95 class DirHandle;
96 
97 /** Targets may implement this to change stdin, stdout, stderr.
98  *
99  * If the application hasn't provided mbed_override_console, this is called
100  * to give the target a chance to specify a FileHandle for the console.
101  *
102  * If this is not provided or returns NULL, the console will be:
103  * - UARTSerial if configuration option "platform.stdio-buffered-serial" is
104  * true and the target has DEVICE_SERIAL;
105  * - Raw HAL serial via serial_getc and serial_putc if
106  * "platform.stdio-buffered-serial" is false and the target has DEVICE_SERIAL;
107  * - stdout/stderr will be a sink and stdin will input a stream of 0s if the
108  * target does not have DEVICE_SERIAL.
109  *
110  * @param fd file descriptor - STDIN_FILENO, STDOUT_FILENO or STDERR_FILENO
111  * @return pointer to FileHandle to override normal stream otherwise NULL
112  */
113 FileHandle *mbed_target_override_console(int fd);
114 
115 /** Applications may implement this to change stdin, stdout, stderr.
116  *
117  * This hook gives the application a chance to specify a custom FileHandle
118  * for the console.
119  *
120  * If this is not provided or returns NULL, the console will be specified
121  * by mbed_target_override_console, else will default to serial - see
122  * mbed_target_override_console for more details.
123  *
124  * Example using UARTSerial:
125  * @code
126  * FileHandle *mbed::mbed_override_console(int) {
127  * static UARTSerial my_serial(D0, D1);
128  * return &my_serial;
129  * }
130  * @endcode
131  *
132  * Example using SingleWireOutput:
133  * @code
134  * FileHandle *mbed::mbed_override_console(int) {
135  * static SerialWireOutput swo;
136  * return &swo;
137  * }
138  * @endcode
139  *
140  * Example using arm semihosting:
141  * @code
142  * FileHandle *mbed::mbed_override_console(int fileno) {
143  * static LocalFileSystem fs("host");
144  * if (fileno == STDIN_FILENO) {
145  * static FileHandle *in_terminal;
146  * static int in_open_result = fs.open(&in_terminal, ":tt", O_RDONLY);
147  * return in_terminal;
148  * } else {
149  * static FileHandle *out_terminal;
150  * static int out_open_result = fs.open(&out_terminal, ":tt", O_WRONLY);
151  * return out_terminal;
152  * }
153  * }
154  * @endcode
155  *
156  * @param fd file descriptor - STDIN_FILENO, STDOUT_FILENO or STDERR_FILENO
157  * @return pointer to FileHandle to override normal stream otherwise NULL
158  */
159 FileHandle *mbed_override_console(int fd);
160 
161 /** Look up the Mbed file handle corresponding to a file descriptor
162  *
163  * This conversion function permits an application to find the underlying
164  * FileHandle object corresponding to a POSIX file descriptor.
165  *
166  * This allows access to specialized behavior only available via the
167  * FileHandle API.
168  *
169  * Example of saving power by disabling console input - for buffered serial,
170  * this would release the RX interrupt handler, which would release the
171  * deep sleep lock.
172  * @code
173  * mbed_file_handle(STDIN_FILENO)->enable_input(false);
174  * @endcode
175  *
176  * @param fd file descriptor
177  * @return FileHandle pointer
178  * NULL if descriptor does not correspond to a FileHandle (only
179  * possible if it's not open with current implementation).
180  */
181 FileHandle *mbed_file_handle(int fd);
182 }
183 
184 typedef mbed::DirHandle DIR;
185 #else
186 typedef struct Dir DIR;
187 #endif
188 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
189 
190 /* The intent of this section is to unify the errno error values to match
191  * the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is
192  * necessary because the ARMCC/IAR errno.h, or sys/stat.h are missing some
193  * symbol definitions used by the POSIX filesystem API to return errno codes.
194  * Note also that ARMCC errno.h defines some symbol values differently from
195  * the GCC_ARM/IAR/standard POSIX definitions. The definitions guard against
196  * this and future changes by changing the symbol definition as shown below.
197  */
198 #undef EPERM
199 #define EPERM 1 /* Operation not permitted */
200 #undef ENOENT
201 #define ENOENT 2 /* No such file or directory */
202 #undef ESRCH
203 #define ESRCH 3 /* No such process */
204 #undef EINTR
205 #define EINTR 4 /* Interrupted system call */
206 #undef EIO
207 #define EIO 5 /* I/O error */
208 #undef ENXIO
209 #define ENXIO 6 /* No such device or address */
210 #undef E2BIG
211 #define E2BIG 7 /* Argument list too long */
212 #undef ENOEXEC
213 #define ENOEXEC 8 /* Exec format error */
214 #undef EBADF
215 #define EBADF 9 /* Bad file number */
216 #undef ECHILD
217 #define ECHILD 10 /* No child processes */
218 #undef EAGAIN
219 #define EAGAIN 11 /* Try again */
220 #undef ENOMEM
221 #define ENOMEM 12 /* Out of memory */
222 #undef EACCES
223 #define EACCES 13 /* Permission denied */
224 #undef EFAULT
225 #define EFAULT 14 /* Bad address */
226 #undef ENOTBLK
227 #define ENOTBLK 15 /* Block device required */
228 #undef EBUSY
229 #define EBUSY 16 /* Device or resource busy */
230 #undef EEXIST
231 #define EEXIST 17 /* File exists */
232 #undef EXDEV
233 #define EXDEV 18 /* Cross-device link */
234 #undef ENODEV
235 #define ENODEV 19 /* No such device */
236 #undef ENOTDIR
237 #define ENOTDIR 20 /* Not a directory */
238 #undef EISDIR
239 #define EISDIR 21 /* Is a directory */
240 #undef EINVAL
241 #define EINVAL 22 /* Invalid argument */
242 #undef ENFILE
243 #define ENFILE 23 /* File table overflow */
244 #undef EMFILE
245 #define EMFILE 24 /* Too many open files */
246 #undef ENOTTY
247 #define ENOTTY 25 /* Not a typewriter */
248 #undef ETXTBSY
249 #define ETXTBSY 26 /* Text file busy */
250 #undef EFBIG
251 #define EFBIG 27 /* File too large */
252 #undef ENOSPC
253 #define ENOSPC 28 /* No space left on device */
254 #undef ESPIPE
255 #define ESPIPE 29 /* Illegal seek */
256 #undef EROFS
257 #define EROFS 30 /* Read-only file system */
258 #undef EMLINK
259 #define EMLINK 31 /* Too many links */
260 #undef EPIPE
261 #define EPIPE 32 /* Broken pipe */
262 #undef EDOM
263 #define EDOM 33 /* Math argument out of domain of func */
264 #undef ERANGE
265 #define ERANGE 34 /* Math result not representable */
266 #undef EDEADLK
267 #define EDEADLK 35 /* Resource deadlock would occur */
268 #undef ENAMETOOLONG
269 #define ENAMETOOLONG 36 /* File name too long */
270 #undef ENOLCK
271 #define ENOLCK 37 /* No record locks available */
272 #undef ENOSYS
273 #define ENOSYS 38 /* Function not implemented */
274 #undef ENOTEMPTY
275 #define ENOTEMPTY 39 /* Directory not empty */
276 #undef ELOOP
277 #define ELOOP 40 /* Too many symbolic links encountered */
278 #undef EWOULDBLOCK
279 #define EWOULDBLOCK EAGAIN /* Operation would block */
280 #undef ENOMSG
281 #define ENOMSG 42 /* No message of desired type */
282 #undef EIDRM
283 #define EIDRM 43 /* Identifier removed */
284 #undef ECHRNG
285 #define ECHRNG 44 /* Channel number out of range */
286 #undef EL2NSYNC
287 #define EL2NSYNC 45 /* Level 2 not synchronized */
288 #undef EL3HLT
289 #define EL3HLT 46 /* Level 3 halted */
290 #undef EL3RST
291 #define EL3RST 47 /* Level 3 reset */
292 #undef ELNRNG
293 #define ELNRNG 48 /* Link number out of range */
294 #undef EUNATCH
295 #define EUNATCH 49 /* Protocol driver not attached */
296 #undef ENOCSI
297 #define ENOCSI 50 /* No CSI structure available */
298 #undef EL2HLT
299 #define EL2HLT 51 /* Level 2 halted */
300 #undef EBADE
301 #define EBADE 52 /* Invalid exchange */
302 #undef EBADR
303 #define EBADR 53 /* Invalid request descriptor */
304 #undef EXFULL
305 #define EXFULL 54 /* Exchange full */
306 #undef ENOANO
307 #define ENOANO 55 /* No anode */
308 #undef EBADRQC
309 #define EBADRQC 56 /* Invalid request code */
310 #undef EBADSLT
311 #define EBADSLT 57 /* Invalid slot */
312 #undef EDEADLOCK
313 #define EDEADLOCK EDEADLK /* Resource deadlock would occur */
314 #undef EBFONT
315 #define EBFONT 59 /* Bad font file format */
316 #undef ENOSTR
317 #define ENOSTR 60 /* Device not a stream */
318 #undef ENODATA
319 #define ENODATA 61 /* No data available */
320 #undef ETIME
321 #define ETIME 62 /* Timer expired */
322 #undef ENOSR
323 #define ENOSR 63 /* Out of streams resources */
324 #undef ENONET
325 #define ENONET 64 /* Machine is not on the network */
326 #undef ENOPKG
327 #define ENOPKG 65 /* Package not installed */
328 #undef EREMOTE
329 #define EREMOTE 66 /* Object is remote */
330 #undef ENOLINK
331 #define ENOLINK 67 /* Link has been severed */
332 #undef EADV
333 #define EADV 68 /* Advertise error */
334 #undef ESRMNT
335 #define ESRMNT 69 /* Srmount error */
336 #undef ECOMM
337 #define ECOMM 70 /* Communication error on send */
338 #undef EPROTO
339 #define EPROTO 71 /* Protocol error */
340 #undef EMULTIHOP
341 #define EMULTIHOP 72 /* Multihop attempted */
342 #undef EDOTDOT
343 #define EDOTDOT 73 /* RFS specific error */
344 #undef EBADMSG
345 #define EBADMSG 74 /* Not a data message */
346 #undef EOVERFLOW
347 #define EOVERFLOW 75 /* Value too large for defined data type */
348 #undef ENOTUNIQ
349 #define ENOTUNIQ 76 /* Name not unique on network */
350 #undef EBADFD
351 #define EBADFD 77 /* File descriptor in bad state */
352 #undef EREMCHG
353 #define EREMCHG 78 /* Remote address changed */
354 #undef ELIBACC
355 #define ELIBACC 79 /* Can not access a needed shared library */
356 #undef ELIBBAD
357 #define ELIBBAD 80 /* Accessing a corrupted shared library */
358 #undef ELIBSCN
359 #define ELIBSCN 81 /* .lib section in a.out corrupted */
360 #undef ELIBMAX
361 #define ELIBMAX 82 /* Attempting to link in too many shared libraries */
362 #undef ELIBEXEC
363 #define ELIBEXEC 83 /* Cannot exec a shared library directly */
364 #undef EILSEQ
365 #define EILSEQ 84 /* Illegal byte sequence */
366 #undef ERESTART
367 #define ERESTART 85 /* Interrupted system call should be restarted */
368 #undef ESTRPIPE
369 #define ESTRPIPE 86 /* Streams pipe error */
370 #undef EUSERS
371 #define EUSERS 87 /* Too many users */
372 #undef ENOTSOCK
373 #define ENOTSOCK 88 /* Socket operation on non-socket */
374 #undef EDESTADDRREQ
375 #define EDESTADDRREQ 89 /* Destination address required */
376 #undef EMSGSIZE
377 #define EMSGSIZE 90 /* Message too long */
378 #undef EPROTOTYPE
379 #define EPROTOTYPE 91 /* Protocol wrong type for socket */
380 #undef ENOPROTOOPT
381 #define ENOPROTOOPT 92 /* Protocol not available */
382 #undef EPROTONOSUPPORT
383 #define EPROTONOSUPPORT 93 /* Protocol not supported */
384 #undef ESOCKTNOSUPPORT
385 #define ESOCKTNOSUPPORT 94 /* Socket type not supported */
386 #undef EOPNOTSUPP
387 #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
388 #undef EPFNOSUPPORT
389 #define EPFNOSUPPORT 96 /* Protocol family not supported */
390 #undef EAFNOSUPPORT
391 #define EAFNOSUPPORT 97 /* Address family not supported by protocol */
392 #undef EADDRINUSE
393 #define EADDRINUSE 98 /* Address already in use */
394 #undef EADDRNOTAVAIL
395 #define EADDRNOTAVAIL 99 /* Cannot assign requested address */
396 #undef ENETDOWN
397 #define ENETDOWN 100 /* Network is down */
398 #undef ENETUNREACH
399 #define ENETUNREACH 101 /* Network is unreachable */
400 #undef ENETRESET
401 #define ENETRESET 102 /* Network dropped connection because of reset */
402 #undef ECONNABORTED
403 #define ECONNABORTED 103 /* Software caused connection abort */
404 #undef ECONNRESET
405 #define ECONNRESET 104 /* Connection reset by peer */
406 #undef ENOBUFS
407 #define ENOBUFS 105 /* No buffer space available */
408 #undef EISCONN
409 #define EISCONN 106 /* Transport endpoint is already connected */
410 #undef ENOTCONN
411 #define ENOTCONN 107 /* Transport endpoint is not connected */
412 #undef ESHUTDOWN
413 #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
414 #undef ETOOMANYREFS
415 #define ETOOMANYREFS 109 /* Too many references: cannot splice */
416 #undef ETIMEDOUT
417 #define ETIMEDOUT 110 /* Connection timed out */
418 #undef ECONNREFUSED
419 #define ECONNREFUSED 111 /* Connection refused */
420 #undef EHOSTDOWN
421 #define EHOSTDOWN 112 /* Host is down */
422 #undef EHOSTUNREACH
423 #define EHOSTUNREACH 113 /* No route to host */
424 #undef EALREADY
425 #define EALREADY 114 /* Operation already in progress */
426 #undef EINPROGRESS
427 #define EINPROGRESS 115 /* Operation now in progress */
428 #undef ESTALE
429 #define ESTALE 116 /* Stale NFS file handle */
430 #undef EUCLEAN
431 #define EUCLEAN 117 /* Structure needs cleaning */
432 #undef ENOTNAM
433 #define ENOTNAM 118 /* Not a XENIX named type file */
434 #undef ENAVAIL
435 #define ENAVAIL 119 /* No XENIX semaphores available */
436 #undef EISNAM
437 #define EISNAM 120 /* Is a named type file */
438 #undef EREMOTEIO
439 #define EREMOTEIO 121 /* Remote I/O error */
440 #undef EDQUOT
441 #define EDQUOT 122 /* Quota exceeded */
442 #undef ENOMEDIUM
443 #define ENOMEDIUM 123 /* No medium found */
444 #undef EMEDIUMTYPE
445 #define EMEDIUMTYPE 124 /* Wrong medium type */
446 #undef ECANCELED
447 #define ECANCELED 125 /* Operation Canceled */
448 #undef ENOKEY
449 #define ENOKEY 126 /* Required key not available */
450 #undef EKEYEXPIRED
451 #define EKEYEXPIRED 127 /* Key has expired */
452 #undef EKEYREVOKED
453 #define EKEYREVOKED 128 /* Key has been revoked */
454 #undef EKEYREJECTED
455 #define EKEYREJECTED 129 /* Key was rejected by service */
456 #undef EOWNERDEAD
457 #define EOWNERDEAD 130 /* Owner died */
458 #undef ENOTRECOVERABLE
459 #define ENOTRECOVERABLE 131 /* State not recoverable */
460 
461 /* Missing stat.h defines.
462  * The following are sys/stat.h definitions not currently present in the ARMCC
463  * errno.h. Note, ARMCC errno.h defines some symbol values differing from
464  * GCC_ARM/IAR/standard POSIX definitions. Guard against this and future
465  * changes by changing the symbol definition for filesystem use.
466  */
467 #define _IFMT 0170000 //< type of file
468 #define _IFSOCK 0140000 //< socket
469 #define _IFLNK 0120000 //< symbolic link
470 #define _IFREG 0100000 //< regular
471 #define _IFBLK 0060000 //< block special
472 #define _IFDIR 0040000 //< directory
473 #define _IFCHR 0020000 //< character special
474 #define _IFIFO 0010000 //< fifo special
475 
476 #define S_IFMT _IFMT //< type of file
477 #define S_IFSOCK _IFSOCK //< socket
478 #define S_IFLNK _IFLNK //< symbolic link
479 #define S_IFREG _IFREG //< regular
480 #define S_IFBLK _IFBLK //< block special
481 #define S_IFDIR _IFDIR //< directory
482 #define S_IFCHR _IFCHR //< character special
483 #define S_IFIFO _IFIFO //< fifo special
484 
485 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
486 #define S_IRUSR 0000400 ///< read permission, owner
487 #define S_IWUSR 0000200 ///< write permission, owner
488 #define S_IXUSR 0000100 ///< execute/search permission, owner
489 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
490 #define S_IRGRP 0000040 ///< read permission, group
491 #define S_IWGRP 0000020 ///< write permission, group
492 #define S_IXGRP 0000010 ///< execute/search permission, group
493 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
494 #define S_IROTH 0000004 ///< read permission, other
495 #define S_IWOTH 0000002 ///< write permission, other
496 #define S_IXOTH 0000001 ///< execute/search permission, other
497 
498 /* Refer to sys/stat standard
499  * Note: Not all fields may be supported by the underlying filesystem
500  */
501 struct stat {
502  dev_t st_dev; ///< Device ID containing file
503  ino_t st_ino; ///< File serial number
504  mode_t st_mode; ///< Mode of file
505  nlink_t st_nlink; ///< Number of links to file
506 
507  uid_t st_uid; ///< User ID
508  gid_t st_gid; ///< Group ID
509 
510  off_t st_size; ///< Size of file in bytes
511 
512  time_t st_atime; ///< Time of last access
513  time_t st_mtime; ///< Time of last data modification
514  time_t st_ctime; ///< Time of last status change
515 };
516 
517 struct statvfs {
518  unsigned long f_bsize; ///< Filesystem block size
519  unsigned long f_frsize; ///< Fragment size (block size)
520 
521  fsblkcnt_t f_blocks; ///< Number of blocks
522  fsblkcnt_t f_bfree; ///< Number of free blocks
523  fsblkcnt_t f_bavail; ///< Number of free blocks for unprivileged users
524 
525  unsigned long f_fsid; ///< Filesystem ID
526 
527  unsigned long f_namemax; ///< Maximum filename length
528 };
529 
530 /* The following are dirent.h definitions are declared here to guarantee
531  * consistency where structure may be different with different toolchains
532  */
533 struct dirent {
534  char d_name[NAME_MAX + 1]; ///< Name of file
535  uint8_t d_type; ///< Type of file
536 };
537 
538 enum {
539  DT_UNKNOWN, ///< The file type could not be determined.
540  DT_FIFO, ///< This is a named pipe (FIFO).
541  DT_CHR, ///< This is a character device.
542  DT_DIR, ///< This is a directory.
543  DT_BLK, ///< This is a block device.
544  DT_REG, ///< This is a regular file.
545  DT_LNK, ///< This is a symbolic link.
546  DT_SOCK, ///< This is a UNIX domain socket.
547 };
548 
549 /* fcntl.h defines */
550 #define F_GETFL 3
551 #define F_SETFL 4
552 
553 struct pollfd {
554  int fd;
555  short events;
556  short revents;
557 };
558 
559 /* POSIX-compatible I/O functions */
560 #if __cplusplus
561 extern "C" {
562 #endif
563 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
564  int open(const char *path, int oflag, ...);
565 #ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
566 #if __cplusplus
567  std::FILE *fdopen(int fildes, const char *mode);
568 #else
569  FILE *fdopen(int fildes, const char *mode);
570 #endif
571 #endif
572 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
573  ssize_t write(int fildes, const void *buf, size_t nbyte);
574  ssize_t read(int fildes, void *buf, size_t nbyte);
575  int fsync(int fildes);
576  int isatty(int fildes);
577 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
578  off_t lseek(int fildes, off_t offset, int whence);
579  int ftruncate(int fildes, off_t length);
580  int fstat(int fildes, struct stat *st);
581  int fcntl(int fildes, int cmd, ...);
582  int poll(struct pollfd fds[], nfds_t nfds, int timeout);
583  int close(int fildes);
584  int stat(const char *path, struct stat *st);
585  int statvfs(const char *path, struct statvfs *buf);
586  DIR *opendir(const char *);
587  struct dirent *readdir(DIR *);
588  int closedir(DIR *);
589  void rewinddir(DIR *);
590  long telldir(DIR *);
591  void seekdir(DIR *, long);
592  int mkdir(const char *name, mode_t n);
593 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
594 #if __cplusplus
595 }; // extern "C"
596 
597 namespace mbed {
598 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
599 /** This call is an analogue to POSIX fdopen().
600  *
601  * It associates a C stream to an already-opened FileHandle, to allow you to
602  * use C printf/scanf/fwrite etc. The provided FileHandle must remain open -
603  * it will be closed by the C library when fclose(FILE) is called.
604  *
605  * The net effect is fdopen(bind_to_fd(fh), mode), with error handling.
606  *
607  * @param fh a pointer to an opened FileHandle
608  * @param mode operation upon the file descriptor, e.g., "w+"
609  *
610  * @returns a pointer to FILE
611  */
612 std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
613 
614 /** Bind an mbed FileHandle to a POSIX file descriptor
615  *
616  * This is similar to fdopen, but only operating at the POSIX layer - it
617  * associates a POSIX integer file descriptor with a FileHandle, to allow you
618  * to use POSIX read/write calls etc. The provided FileHandle must remain open -
619  * it will be closed when close(int) is called.
620  *
621  * @param fh a pointer to an opened FileHandle
622  *
623  * @return an integer file descriptor, or negative if no descriptors available
624  */
625 int bind_to_fd(mbed::FileHandle *fh);
626 
627 #else
628 /** Targets may implement this to override how to write to the console.
629  *
630  * If the target has provided minimal_console_putc, this is called
631  * to give the target a chance to specify an alternative minimal console.
632  *
633  * If this is not provided, serial_putc will be used if
634  * `target.console-uart` is `true`, else there will not be an output.
635  *
636  * @param c The char to write
637  * @return The written char
638  */
639 int minimal_console_putc(int c);
640 
641 /** Targets may implement this to override how to read from the console.
642  *
643  * If the target has provided minimal_console_getc, this is called
644  * to give the target a chance to specify an alternative minimal console.
645  *
646  * If this is not provided, serial_getc will be used if
647  * `target.console-uart` is `true`, else no input would be captured.
648  *
649  * @return The char read from the serial port
650  */
651 int minimal_console_getc();
652 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
653 
654 } // namespace mbed
655 
656 #endif // __cplusplus
657 
658 /**@}*/
659 
660 /**@}*/
661 
662 #endif /* RETARGET_H */
This is a block device.
uid_t st_uid
User ID.
fsblkcnt_t f_bfree
Number of free blocks.
Represents a directory stream.
Definition: DirHandle.h:55
This is a directory.
Class FileHandle.
Definition: FileHandle.h:46
This is a named pipe (FIFO).
This is a character device.
fsblkcnt_t f_blocks
Number of blocks.
This is a UNIX domain socket.
This is a regular file.
dev_t st_dev
Device ID containing file.
gid_t st_gid
Group ID.
unsigned long f_fsid
Filesystem ID.
mode_t st_mode
Mode of file.
The file type could not be determined.
This is a symbolic link.
nlink_t st_nlink
Number of links to file.
ino_t st_ino
File serial number.
time_t st_ctime
Time of last status change.
time_t st_atime
Time of last access.
fsblkcnt_t f_bavail
Number of free blocks for unprivileged users.
unsigned long f_namemax
Maximum filename length.
time_t st_mtime
Time of last data modification.
unsigned long f_frsize
Fragment size (block size)
unsigned long f_bsize
Filesystem block size.
off_t st_size
Size of file in bytes.
uint8_t d_type
Type of file.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.