Skip to content

Commit 12ce65c

Browse files
committed
Add mbed_file_handle lookup function
Add a necessary helper to allow FileHandle objects to be obtained from POSIX file descriptors. Primary envisaged use case is to act on STDIN_FILENO etc, eg to set it non-blocking or use sigio, or to use the enable API.
1 parent 65f5193 commit 12ce65c

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

platform/mbed_retarget.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static FileHandle *get_console(int fd)
290290
}
291291

292292
/* Deal with the fact C library may not _open descriptors 0, 1, 2 - auto bind */
293-
static FileHandle *get_fhc(int fd)
293+
FileHandle *mbed::mbed_file_handle(int fd)
294294
{
295295
if (fd >= OPEN_MAX) {
296296
return NULL;
@@ -490,13 +490,13 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags)
490490
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.
491491
*/
492492
if (std::strcmp(name, __stdin_name) == 0) {
493-
get_fhc(STDIN_FILENO);
493+
mbed_file_handle(STDIN_FILENO);
494494
return STDIN_FILENO;
495495
} else if (std::strcmp(name, __stdout_name) == 0) {
496-
get_fhc(STDOUT_FILENO);
496+
mbed_file_handle(STDOUT_FILENO);
497497
return STDOUT_FILENO;
498498
} else if (std::strcmp(name, __stderr_name) == 0) {
499-
get_fhc(STDERR_FILENO);
499+
mbed_file_handle(STDERR_FILENO);
500500
return STDERR_FILENO;
501501
}
502502
#endif
@@ -555,7 +555,7 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh)
555555

556556
extern "C" int close(int fildes)
557557
{
558-
FileHandle *fhc = get_fhc(fildes);
558+
FileHandle *fhc = mbed_file_handle(fildes);
559559
filehandles[fildes] = NULL;
560560
if (fhc == NULL) {
561561
errno = EBADF;
@@ -667,7 +667,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
667667
extern "C" ssize_t write(int fildes, const void *buf, size_t length)
668668
{
669669

670-
FileHandle *fhc = get_fhc(fildes);
670+
FileHandle *fhc = mbed_file_handle(fildes);
671671
if (fhc == NULL) {
672672
errno = EBADF;
673673
return -1;
@@ -761,8 +761,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
761761

762762
extern "C" ssize_t read(int fildes, void *buf, size_t length)
763763
{
764-
765-
FileHandle *fhc = get_fhc(fildes);
764+
FileHandle *fhc = mbed_file_handle(fildes);
766765
if (fhc == NULL) {
767766
errno = EBADF;
768767
return -1;
@@ -789,7 +788,7 @@ extern "C" int _isatty(FILEHANDLE fh)
789788

790789
extern "C" int isatty(int fildes)
791790
{
792-
FileHandle *fhc = get_fhc(fildes);
791+
FileHandle *fhc = mbed_file_handle(fildes);
793792
if (fhc == NULL) {
794793
errno = EBADF;
795794
return 0;
@@ -828,7 +827,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
828827

829828
extern "C" off_t lseek(int fildes, off_t offset, int whence)
830829
{
831-
FileHandle *fhc = get_fhc(fildes);
830+
FileHandle *fhc = mbed_file_handle(fildes);
832831
if (fhc == NULL) {
833832
errno = EBADF;
834833
return -1;
@@ -844,7 +843,7 @@ extern "C" off_t lseek(int fildes, off_t offset, int whence)
844843

845844
extern "C" int ftruncate(int fildes, off_t length)
846845
{
847-
FileHandle *fhc = get_fhc(fildes);
846+
FileHandle *fhc = mbed_file_handle(fildes);
848847
if (fhc == NULL) {
849848
errno = EBADF;
850849
return -1;
@@ -868,7 +867,7 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh)
868867

869868
extern "C" int fsync(int fildes)
870869
{
871-
FileHandle *fhc = get_fhc(fildes);
870+
FileHandle *fhc = mbed_file_handle(fildes);
872871
if (fhc == NULL) {
873872
errno = EBADF;
874873
return -1;
@@ -886,7 +885,7 @@ extern "C" int fsync(int fildes)
886885
#ifdef __ARMCC_VERSION
887886
extern "C" long PREFIX(_flen)(FILEHANDLE fh)
888887
{
889-
FileHandle *fhc = get_fhc(fh);
888+
FileHandle *fhc = mbed_file_handle(fh);
890889
if (fhc == NULL) {
891890
errno = EBADF;
892891
return -1;
@@ -936,7 +935,7 @@ extern "C" int _fstat(int fh, struct stat *st)
936935

937936
extern "C" int fstat(int fildes, struct stat *st)
938937
{
939-
FileHandle *fhc = get_fhc(fildes);
938+
FileHandle *fhc = mbed_file_handle(fildes);
940939
if (fhc == NULL) {
941940
errno = EBADF;
942941
return -1;
@@ -949,7 +948,7 @@ extern "C" int fstat(int fildes, struct stat *st)
949948

950949
extern "C" int fcntl(int fildes, int cmd, ...)
951950
{
952-
FileHandle *fhc = get_fhc(fildes);
951+
FileHandle *fhc = mbed_file_handle(fildes);
953952
if (fhc == NULL) {
954953
errno = EBADF;
955954
return -1;
@@ -994,7 +993,7 @@ extern "C" int poll(struct pollfd fds[], nfds_t nfds, int timeout)
994993
for (nfds_t n = 0; n < nfds; n++) {
995994
// Underlying FileHandle poll returns POLLNVAL if given NULL, so
996995
// we don't need to take special action.
997-
fhs[n].fh = get_fhc(fds[n].fd);
996+
fhs[n].fh = mbed_file_handle(fds[n].fd);
998997
fhs[n].events = fds[n].events;
999998
}
1000999
int ret = poll(fhs, nfds, timeout);

platform/mbed_retarget.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ FileHandle *mbed_target_override_console(int fd);
133133
*/
134134
FileHandle *mbed_override_console(int fd);
135135

136+
/** Look up the Mbed file handle corresponding to a file descriptor
137+
*
138+
* This conversion function permits an application to find the underlying
139+
* FileHandle object corresponding to a POSIX file descriptor.
140+
*
141+
* This allows access to specialized behavior only available via the
142+
* FileHandle API.
143+
*
144+
* Example of saving power by disabling console input - for buffered serial,
145+
* this would release the RX interrupt handler, which would release the
146+
* deep sleep lock.
147+
* @code
148+
* mbed_file_handle(STDIN_FILENO)->enable_input(false);
149+
* @endcode
150+
*
151+
* @param fd file descriptor
152+
* @return FileHandle pointer
153+
* NULL if descriptor does not correspond to a FileHandle (only
154+
* possible if it's not open with current implementation).
155+
*/
156+
FileHandle *mbed_file_handle(int fd);
136157
}
137158

138159
typedef mbed::DirHandle DIR;

0 commit comments

Comments
 (0)