Skip to content

Commit a301db5

Browse files
author
Cruz Monrreal
authored
Merge pull request #6791 from kjbracey-arm/fcntl
Add POSIX fcntl flag support
2 parents cd1ff94 + cf91b1c commit a301db5

File tree

4 files changed

+106
-42
lines changed

4 files changed

+106
-42
lines changed

drivers/UARTSerial.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UA
142142
return 0;
143143
}
144144

145+
/** Check current blocking or non-blocking mode for file operations.
146+
*
147+
* @return true for blocking mode, false for non-blocking mode.
148+
*/
149+
virtual bool is_blocking() const
150+
{
151+
return _blocking;
152+
}
153+
145154
/** Register a callback on state change of the file.
146155
*
147156
* The specified callback will be called on state changes such as when

platform/FileHandle.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,16 @@ class FileHandle : private NonCopyable<FileHandle> {
192192
*/
193193
virtual int set_blocking(bool blocking)
194194
{
195-
return -1;
195+
return blocking ? 0 : -ENOTTY;
196+
}
197+
198+
/** Check current blocking or non-blocking mode for file operations.
199+
*
200+
* @return true for blocking mode, false for non-blocking mode.
201+
*/
202+
virtual bool is_blocking() const
203+
{
204+
return true;
196205
}
197206

198207
/** Check for poll event flags

platform/mbed_retarget.cpp

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,16 @@ static int reserve_filehandle() {
344344
}
345345

346346
int mbed::bind_to_fd(FileHandle *fh) {
347-
int fh_i = reserve_filehandle();
348-
if (fh_i < 0) {
349-
return fh_i;
347+
int fildes = reserve_filehandle();
348+
if (fildes < 0) {
349+
return fildes;
350350
}
351351

352-
filehandles[fh_i] = fh;
353-
stdio_in_prev[fh_i] = 0;
354-
stdio_out_prev[fh_i] = 0;
352+
filehandles[fildes] = fh;
353+
stdio_in_prev[fildes] = 0;
354+
stdio_out_prev[fildes] = 0;
355355

356-
return fh_i;
356+
return fildes;
357357
}
358358

359359
static int unbind_from_fd(int fd, FileHandle *fh) {
@@ -464,9 +464,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags) {
464464
}
465465

466466
extern "C" int open(const char *name, int oflag, ...) {
467-
int fh_i = reserve_filehandle();
468-
if (fh_i < 0) {
469-
return fh_i;
467+
int fildes = reserve_filehandle();
468+
if (fildes < 0) {
469+
return fildes;
470470
}
471471

472472
FileHandle *res = NULL;
@@ -476,36 +476,36 @@ extern "C" int open(const char *name, int oflag, ...) {
476476
/* The first part of the filename (between first 2 '/') is not a
477477
* registered mount point in the namespace.
478478
*/
479-
return handle_open_errors(-ENODEV, fh_i);
479+
return handle_open_errors(-ENODEV, fildes);
480480
}
481481

482482
if (path.isFile()) {
483483
res = path.file();
484484
} else {
485485
FileSystemHandle *fs = path.fileSystem();
486486
if (fs == NULL) {
487-
return handle_open_errors(-ENODEV, fh_i);
487+
return handle_open_errors(-ENODEV, fildes);
488488
}
489489
int err = fs->open(&res, path.fileName(), oflag);
490490
if (err) {
491-
return handle_open_errors(err, fh_i);
491+
return handle_open_errors(err, fildes);
492492
}
493493
}
494494

495-
filehandles[fh_i] = res;
496-
stdio_in_prev[fh_i] = 0;
497-
stdio_out_prev[fh_i] = 0;
495+
filehandles[fildes] = res;
496+
stdio_in_prev[fildes] = 0;
497+
stdio_out_prev[fildes] = 0;
498498

499-
return fh_i;
499+
return fildes;
500500
}
501501

502502
extern "C" int PREFIX(_close)(FILEHANDLE fh) {
503503
return close(fh);
504504
}
505505

506-
extern "C" int close(int fh) {
507-
FileHandle* fhc = get_fhc(fh);
508-
filehandles[fh] = NULL;
506+
extern "C" int close(int fildes) {
507+
FileHandle* fhc = get_fhc(fildes);
508+
filehandles[fildes] = NULL;
509509
if (fhc == NULL) {
510510
errno = EBADF;
511511
return -1;
@@ -610,9 +610,9 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
610610
#endif
611611
}
612612

613-
extern "C" ssize_t write(int fh, const void *buf, size_t length) {
613+
extern "C" ssize_t write(int fildes, const void *buf, size_t length) {
614614

615-
FileHandle* fhc = get_fhc(fh);
615+
FileHandle* fhc = get_fhc(fildes);
616616
if (fhc == NULL) {
617617
errno = EBADF;
618618
return -1;
@@ -700,9 +700,9 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
700700
#endif
701701
}
702702

703-
extern "C" ssize_t read(int fh, void *buf, size_t length) {
703+
extern "C" ssize_t read(int fildes, void *buf, size_t length) {
704704

705-
FileHandle* fhc = get_fhc(fh);
705+
FileHandle* fhc = get_fhc(fildes);
706706
if (fhc == NULL) {
707707
errno = EBADF;
708708
return -1;
@@ -727,8 +727,8 @@ extern "C" int _isatty(FILEHANDLE fh)
727727
return isatty(fh);
728728
}
729729

730-
extern "C" int isatty(int fh) {
731-
FileHandle* fhc = get_fhc(fh);
730+
extern "C" int isatty(int fildes) {
731+
FileHandle* fhc = get_fhc(fildes);
732732
if (fhc == NULL) {
733733
errno = EBADF;
734734
return 0;
@@ -765,8 +765,8 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
765765
return off;
766766
}
767767

768-
extern "C" off_t lseek(int fh, off_t offset, int whence) {
769-
FileHandle* fhc = get_fhc(fh);
768+
extern "C" off_t lseek(int fildes, off_t offset, int whence) {
769+
FileHandle* fhc = get_fhc(fildes);
770770
if (fhc == NULL) {
771771
errno = EBADF;
772772
return -1;
@@ -786,8 +786,8 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
786786
}
787787
#endif
788788

789-
extern "C" int fsync(int fh) {
790-
FileHandle* fhc = get_fhc(fh);
789+
extern "C" int fsync(int fildes) {
790+
FileHandle* fhc = get_fhc(fildes);
791791
if (fhc == NULL) {
792792
errno = EBADF;
793793
return -1;
@@ -850,8 +850,8 @@ extern "C" int _fstat(int fh, struct stat *st) {
850850
}
851851
#endif
852852

853-
extern "C" int fstat(int fh, struct stat *st) {
854-
FileHandle* fhc = get_fhc(fh);
853+
extern "C" int fstat(int fildes, struct stat *st) {
854+
FileHandle* fhc = get_fhc(fildes);
855855
if (fhc == NULL) {
856856
errno = EBADF;
857857
return -1;
@@ -862,6 +862,41 @@ extern "C" int fstat(int fh, struct stat *st) {
862862
return 0;
863863
}
864864

865+
extern "C" int fcntl(int fildes, int cmd, ...) {
866+
FileHandle *fhc = get_fhc(fildes);
867+
if (fhc == NULL) {
868+
errno = EBADF;
869+
return -1;
870+
}
871+
872+
switch (cmd) {
873+
case F_GETFL: {
874+
int flags = 0;
875+
if (fhc->is_blocking()) {
876+
flags |= O_NONBLOCK;
877+
}
878+
return flags;
879+
}
880+
case F_SETFL: {
881+
va_list ap;
882+
va_start(ap, cmd);
883+
int flags = va_arg(ap, int);
884+
va_end(ap);
885+
int ret = fhc->set_blocking(flags & O_NONBLOCK);
886+
if (ret < 0) {
887+
errno = -ret;
888+
return -1;
889+
}
890+
return 0;
891+
}
892+
893+
default: {
894+
errno = EINVAL;
895+
return -1;
896+
}
897+
}
898+
}
899+
865900
extern "C" int poll(struct pollfd fds[], nfds_t nfds, int timeout)
866901
{
867902
if (nfds > OPEN_MAX) {

platform/mbed_retarget.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ typedef unsigned int uid_t; ///< User ID
4343
typedef unsigned int gid_t; ///< Group ID
4444
#endif
4545

46-
#define O_RDONLY 0 ///< Open for reading
47-
#define O_WRONLY 1 ///< Open for writing
48-
#define O_RDWR 2 ///< Open for reading and writing
49-
#define O_CREAT 0x0200 ///< Create file if it does not exist
50-
#define O_TRUNC 0x0400 ///< Truncate file to zero length
51-
#define O_EXCL 0x0800 ///< Fail if file exists
52-
#define O_APPEND 0x0008 ///< Set file offset to end of file prior to each write
53-
#define O_BINARY 0x8000 ///< Open file in binary mode
46+
/* Flags for open() and fcntl(GETFL/SETFL)
47+
* At present, fcntl only supports reading and writing O_NONBLOCK
48+
*/
49+
#define O_RDONLY 0 ///< Open for reading
50+
#define O_WRONLY 1 ///< Open for writing
51+
#define O_RDWR 2 ///< Open for reading and writing
52+
#define O_NONBLOCK 0x0004 ///< Non-blocking mode
53+
#define O_APPEND 0x0008 ///< Set file offset to end of file prior to each write
54+
#define O_CREAT 0x0200 ///< Create file if it does not exist
55+
#define O_TRUNC 0x0400 ///< Truncate file to zero length
56+
#define O_EXCL 0x0800 ///< Fail if file exists
57+
#define O_BINARY 0x8000 ///< Open file in binary mode
58+
59+
#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
5460

5561
#define NAME_MAX 255 ///< Maximum size of a name in a file path
5662

@@ -480,6 +486,10 @@ enum {
480486
DT_SOCK, ///< This is a UNIX domain socket.
481487
};
482488

489+
/* fcntl.h defines */
490+
#define F_GETFL 3
491+
#define F_SETFL 4
492+
483493
struct pollfd {
484494
int fd;
485495
short events;
@@ -503,7 +513,8 @@ extern "C" {
503513
off_t lseek(int fildes, off_t offset, int whence);
504514
int isatty(int fildes);
505515
int fsync(int fildes);
506-
int fstat(int fh, struct stat *st);
516+
int fstat(int fildes, struct stat *st);
517+
int fcntl(int fildes, int cmd, ...);
507518
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
508519
int close(int fildes);
509520
int stat(const char *path, struct stat *st);

0 commit comments

Comments
 (0)