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) && defined(__GNUC__)
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 
472 #if defined ( __ICCARM__ )
473 #undef EILSEQ /* defined in IAR arm/inc/c/errno.h: #define EILSEQ 36 */
474 #define EILSEQ 84 /* Illegal byte sequence */
475 #else
476 #ifndef EILSEQ
477 #define EILSEQ 84 /* Illegal byte sequence */
478 #endif
479 #endif
480 
481 #ifndef ERESTART
482 #define ERESTART 285 /* Interrupted system call should be restarted */
483 #endif
484 #ifndef ESTRPIPE
485 #define ESTRPIPE 86 /* Streams pipe error */
486 #endif
487 #ifndef EUSERS
488 #define EUSERS 87 /* Too many users */
489 #endif
490 #ifndef ENOTSOCK
491 #define ENOTSOCK 88 /* Socket operation on non-socket */
492 #endif
493 #ifndef EDESTADDRREQ
494 #define EDESTADDRREQ 89 /* Destination address required */
495 #endif
496 #ifndef EMSGSIZE
497 #define EMSGSIZE 90 /* Message too long */
498 #endif
499 #ifndef EPROTOTYPE
500 #define EPROTOTYPE 91 /* Protocol wrong type for socket */
501 #endif
502 #ifndef ENOPROTOOPT
503 #define ENOPROTOOPT 92 /* Protocol not available */
504 #endif
505 #ifndef EPROTONOSUPPORT
506 #define EPROTONOSUPPORT 93 /* Protocol not supported */
507 #endif
508 #ifndef ESOCKTNOSUPPORT
509 #define ESOCKTNOSUPPORT 94 /* Socket type not supported */
510 #endif
511 #ifndef EOPNOTSUPP
512 #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
513 #endif
514 #ifndef EPFNOSUPPORT
515 #define EPFNOSUPPORT 96 /* Protocol family not supported */
516 #endif
517 #ifndef EAFNOSUPPORT
518 #define EAFNOSUPPORT 97 /* Address family not supported by protocol */
519 #endif
520 #ifndef EADDRINUSE
521 #define EADDRINUSE 98 /* Address already in use */
522 #endif
523 #ifndef EADDRNOTAVAIL
524 #define EADDRNOTAVAIL 99 /* Cannot assign requested address */
525 #endif
526 #ifndef ENETDOWN
527 #define ENETDOWN 100 /* Network is down */
528 #endif
529 #ifndef ENETUNREACH
530 #define ENETUNREACH 101 /* Network is unreachable */
531 #endif
532 #ifndef ENETRESET
533 #define ENETRESET 102 /* Network dropped connection because of reset */
534 #endif
535 #ifndef ECONNABORTED
536 #define ECONNABORTED 103 /* Software caused connection abort */
537 #endif
538 #ifndef ECONNRESET
539 #define ECONNRESET 104 /* Connection reset by peer */
540 #endif
541 #ifndef ENOBUFS
542 #define ENOBUFS 105 /* No buffer space available */
543 #endif
544 #ifndef EISCONN
545 #define EISCONN 106 /* Transport endpoint is already connected */
546 #endif
547 #ifndef ENOTCONN
548 #define ENOTCONN 107 /* Transport endpoint is not connected */
549 #endif
550 #ifndef ESHUTDOWN
551 #define ESHUTDOWN 208 /* Cannot send after transport endpoint shutdown */
552 #endif
553 #ifndef ETOOMANYREFS
554 #define ETOOMANYREFS 109 /* Too many references: cannot splice */
555 #endif
556 #ifndef ETIMEDOUT
557 #define ETIMEDOUT 110 /* Connection timed out */
558 #endif
559 #ifndef ECONNREFUSED
560 #define ECONNREFUSED 111 /* Connection refused */
561 #endif
562 #ifndef EHOSTDOWN
563 #define EHOSTDOWN 112 /* Host is down */
564 #endif
565 #ifndef EHOSTUNREACH
566 #define EHOSTUNREACH 113 /* No route to host */
567 #endif
568 #ifndef EALREADY
569 #define EALREADY 114 /* Operation already in progress */
570 #endif
571 #ifndef EINPROGRESS
572 #define EINPROGRESS 115 /* Operation now in progress */
573 #endif
574 #ifndef ESTALE
575 #define ESTALE 116 /* Stale NFS file handle */
576 #endif
577 #ifndef EUCLEAN
578 #define EUCLEAN 217 /* Structure needs cleaning */
579 #endif
580 #ifndef ENOTNAM
581 #define ENOTNAM 218 /* Not a XENIX named type file */
582 #endif
583 #ifndef ENAVAIL
584 #define ENAVAIL 219 /* No XENIX semaphores available */
585 #endif
586 #ifndef EISNAM
587 #define EISNAM 220 /* Is a named type file */
588 #endif
589 #ifndef EREMOTEIO
590 #define EREMOTEIO 221 /* Remote I/O error */
591 #endif
592 #ifndef EDQUOT
593 #define EDQUOT 122 /* Quota exceeded */
594 #endif
595 #ifndef ENOMEDIUM
596 #define ENOMEDIUM 223 /* No medium found */
597 #endif
598 #ifndef EMEDIUMTYPE
599 #define EMEDIUMTYPE 224 /* Wrong medium type */
600 #endif
601 #ifndef ECANCELED
602 #define ECANCELED 125 /* Operation Canceled */
603 #endif
604 #ifndef ENOKEY
605 #define ENOKEY 226 /* Required key not available */
606 #endif
607 #ifndef EKEYEXPIRED
608 #define EKEYEXPIRED 227 /* Key has expired */
609 #endif
610 #ifndef EKEYREVOKED
611 #define EKEYREVOKED 228 /* Key has been revoked */
612 #endif
613 #ifndef EKEYREJECTED
614 #define EKEYREJECTED 229 /* Key was rejected by service */
615 #endif
616 #ifndef EOWNERDEAD
617 #define EOWNERDEAD 130 /* Owner died */
618 #endif
619 #ifndef ENOTRECOVERABLE
620 #define ENOTRECOVERABLE 131 /* State not recoverable */
621 #endif
622 
623 /* Missing stat.h defines.
624  * The following are sys/stat.h definitions not currently present in the ARMCC
625  * errno.h. Note, ARMCC errno.h defines some symbol values differing from
626  * GCC_ARM/IAR/standard POSIX definitions. Guard against this and future
627  * changes by changing the symbol definition for filesystem use.
628  */
629 #define _IFMT 0170000 //< type of file
630 #define _IFSOCK 0140000 //< socket
631 #define _IFLNK 0120000 //< symbolic link
632 #define _IFREG 0100000 //< regular
633 #define _IFBLK 0060000 //< block special
634 #define _IFDIR 0040000 //< directory
635 #define _IFCHR 0020000 //< character special
636 #define _IFIFO 0010000 //< fifo special
637 
638 #define S_IFMT _IFMT //< type of file
639 #define S_IFSOCK _IFSOCK //< socket
640 #define S_IFLNK _IFLNK //< symbolic link
641 #define S_IFREG _IFREG //< regular
642 #define S_IFBLK _IFBLK //< block special
643 #define S_IFDIR _IFDIR //< directory
644 #define S_IFCHR _IFCHR //< character special
645 #define S_IFIFO _IFIFO //< fifo special
646 
647 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
648 #define S_IRUSR 0000400 ///< read permission, owner
649 #define S_IWUSR 0000200 ///< write permission, owner
650 #define S_IXUSR 0000100 ///< execute/search permission, owner
651 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
652 #define S_IRGRP 0000040 ///< read permission, group
653 #define S_IWGRP 0000020 ///< write permission, group
654 #define S_IXGRP 0000010 ///< execute/search permission, group
655 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
656 #define S_IROTH 0000004 ///< read permission, other
657 #define S_IWOTH 0000002 ///< write permission, other
658 #define S_IXOTH 0000001 ///< execute/search permission, other
659 
660 #ifndef HAVE_SYS_STAT_H
661 
662 /* Refer to sys/stat standard
663  * Note: Not all fields may be supported by the underlying filesystem
664  */
665 struct stat {
666  dev_t st_dev; ///< Device ID containing file
667  ino_t st_ino; ///< File serial number
668  mode_t st_mode; ///< Mode of file
669  nlink_t st_nlink; ///< Number of links to file
670 
671  uid_t st_uid; ///< User ID
672  gid_t st_gid; ///< Group ID
673 
674  off_t st_size; ///< Size of file in bytes
675 
676  time_t st_atime; ///< Time of last access
677  time_t st_mtime; ///< Time of last data modification
678  time_t st_ctime; ///< Time of last status change
679 };
680 
681 #endif
682 
683 struct statvfs {
684  unsigned long f_bsize; ///< Filesystem block size
685  unsigned long f_frsize; ///< Fragment size (block size)
686 
687  fsblkcnt_t f_blocks; ///< Number of blocks
688  fsblkcnt_t f_bfree; ///< Number of free blocks
689  fsblkcnt_t f_bavail; ///< Number of free blocks for unprivileged users
690 
691  unsigned long f_fsid; ///< Filesystem ID
692 
693  unsigned long f_namemax; ///< Maximum filename length
694 };
695 
696 /* The following are dirent.h definitions are declared here to guarantee
697  * consistency where structure may be different with different toolchains
698  */
699 struct dirent {
700  char d_name[NAME_MAX + 1]; ///< Name of file
701  uint8_t d_type; ///< Type of file
702 };
703 
704 enum {
705  DT_UNKNOWN, ///< The file type could not be determined.
706  DT_FIFO, ///< This is a named pipe (FIFO).
707  DT_CHR, ///< This is a character device.
708  DT_DIR, ///< This is a directory.
709  DT_BLK, ///< This is a block device.
710  DT_REG, ///< This is a regular file.
711  DT_LNK, ///< This is a symbolic link.
712  DT_SOCK, ///< This is a UNIX domain socket.
713 };
714 
715 /* fcntl.h defines */
716 #define F_GETFL 3
717 #define F_SETFL 4
718 
719 struct pollfd {
720  int fd;
721  short events;
722  short revents;
723 };
724 
725 /* POSIX-compatible I/O functions */
726 #if __cplusplus
727 extern "C" {
728 #endif
729 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
730  int open(const char *path, int oflag, ...);
731 #ifndef __IAR_SYSTEMS_ICC__ /* IAR provides fdopen itself */
732 #if __cplusplus
733  std::FILE *fdopen(int fildes, const char *mode);
734 #else
735  FILE *fdopen(int fildes, const char *mode);
736 #endif
737 #endif
738 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
739  ssize_t write(int fildes, const void *buf, size_t nbyte);
740  ssize_t read(int fildes, void *buf, size_t nbyte);
741  int fsync(int fildes);
742  int isatty(int fildes);
743 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
744  off_t lseek(int fildes, off_t offset, int whence);
745  int ftruncate(int fildes, off_t length);
746  int fstat(int fildes, struct stat *st);
747  int fcntl(int fildes, int cmd, ...);
748  int poll(struct pollfd fds[], nfds_t nfds, int timeout);
749  int close(int fildes);
750  int stat(const char *path, struct stat *st);
751  int statvfs(const char *path, struct statvfs *buf);
752  DIR *opendir(const char *);
753  struct dirent *readdir(DIR *);
754  int closedir(DIR *);
755  void rewinddir(DIR *);
756  long telldir(DIR *);
757  void seekdir(DIR *, long);
758  int mkdir(const char *name, mode_t n);
759 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
760 #if __cplusplus
761 } // extern "C"
762 
763 namespace mbed {
764 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
765 /** This call is an analogue to POSIX fdopen().
766  *
767  * It associates a C stream to an already-opened FileHandle, to allow you to
768  * use C printf/scanf/fwrite etc. The provided FileHandle must remain open -
769  * it will be closed by the C library when fclose(FILE) is called.
770  *
771  * The net effect is fdopen(bind_to_fd(fh), mode), with error handling.
772  *
773  * @param fh a pointer to an opened FileHandle
774  * @param mode operation upon the file descriptor, e.g., "w+"
775  *
776  * @returns a pointer to FILE
777  */
778 std::FILE *fdopen(mbed::FileHandle *fh, const char *mode);
779 
780 /** Bind an mbed FileHandle to a POSIX file descriptor
781  *
782  * This is similar to fdopen, but only operating at the POSIX layer - it
783  * associates a POSIX integer file descriptor with a FileHandle, to allow you
784  * to use POSIX read/write calls etc. The provided FileHandle must remain open -
785  * it will be closed when close(int) is called.
786  *
787  * @param fh a pointer to an opened FileHandle
788  *
789  * @return an integer file descriptor, or negative if no descriptors available
790  */
791 int bind_to_fd(mbed::FileHandle *fh);
792 
793 #else
794 /** Targets may implement this to override how to write to the console.
795  *
796  * If the target has provided minimal_console_putc, this is called
797  * to give the target a chance to specify an alternative minimal console.
798  *
799  * If this is not provided, serial_putc will be used if
800  * `target.console-uart` is `true`, else there will not be an output.
801  *
802  * @param c The char to write
803  * @return The written char
804  */
805 int minimal_console_putc(int c);
806 
807 /** Targets may implement this to override how to read from the console.
808  *
809  * If the target has provided minimal_console_getc, this is called
810  * to give the target a chance to specify an alternative minimal console.
811  *
812  * If this is not provided, serial_getc will be used if
813  * `target.console-uart` is `true`, else no input would be captured.
814  *
815  * @return The char read from the serial port
816  */
817 int minimal_console_getc();
818 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
819 
820 } // namespace mbed
821 
822 #endif // __cplusplus
823 
824 /**@}*/
825 
826 /**@}*/
827 
828 #endif /* RETARGET_H */
uid_t st_uid
User ID.
fsblkcnt_t f_bfree
Number of free blocks.
This file is a posix/stdc wrapper for lwip/errno.h.
Class FileHandle.
Definition: FileHandle.h:46
This is a block device.
fsblkcnt_t f_blocks
Number of blocks.
The file type could not be determined.
This is a symbolic link.
dev_t st_dev
Device ID containing file.
gid_t st_gid
Group ID.
This is a directory.
unsigned long f_fsid
Filesystem ID.
mode_t st_mode
Mode of file.
nlink_t st_nlink
Number of links to file.
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 character device.
This is a named pipe (FIFO).
This is a regular file.
Definition: ATHandler.h:46
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.