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