Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileHandle.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_FILEHANDLE_H
  17. #define MBED_FILEHANDLE_H
  18. typedef int FILEHANDLE;
  19. #include <stdio.h>
  20. #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
  21. typedef int ssize_t;
  22. typedef long off_t;
  23. #else
  24. # include <sys/types.h>
  25. #endif
  26. namespace mbed {
  27. /** An OO equivalent of the internal FILEHANDLE variable
  28. * and associated _sys_* functions.
  29. *
  30. * FileHandle is an abstract class, needing at least sys_write and
  31. * sys_read to be implmented for a simple interactive device.
  32. *
  33. * No one ever directly tals to/instanciates a FileHandle - it gets
  34. * created by FileSystem, and wrapped up by stdio.
  35. */
  36. class FileHandle {
  37. public:
  38. /** Write the contents of a buffer to the file
  39. *
  40. * @param buffer the buffer to write from
  41. * @param length the number of characters to write
  42. *
  43. * @returns
  44. * The number of characters written (possibly 0) on success, -1 on error.
  45. */
  46. virtual ssize_t write(const void* buffer, size_t length) = 0;
  47. /** Close the file
  48. *
  49. * @returns
  50. * Zero on success, -1 on error.
  51. */
  52. virtual int close() = 0;
  53. /** Function read
  54. * Reads the contents of the file into a buffer
  55. *
  56. * @param buffer the buffer to read in to
  57. * @param length the number of characters to read
  58. *
  59. * @returns
  60. * The number of characters read (zero at end of file) on success, -1 on error.
  61. */
  62. virtual ssize_t read(void* buffer, size_t length) = 0;
  63. /** Check if the handle is for a interactive terminal device.
  64. * If so, line buffered behaviour is used by default
  65. *
  66. * @returns
  67. * 1 if it is a terminal,
  68. * 0 otherwise
  69. */
  70. virtual int isatty() = 0;
  71. /** Move the file position to a given offset from a given location.
  72. *
  73. * @param offset The offset from whence to move to
  74. * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
  75. * current file position, or SEEK_END for the end of the file.
  76. *
  77. * @returns
  78. * new file position on success,
  79. * -1 on failure or unsupported
  80. */
  81. virtual off_t lseek(off_t offset, int whence) = 0;
  82. /** Flush any buffers associated with the FileHandle, ensuring it
  83. * is up to date on disk
  84. *
  85. * @returns
  86. * 0 on success or un-needed,
  87. * -1 on error
  88. */
  89. virtual int fsync() = 0;
  90. virtual off_t flen() {
  91. /* remember our current position */
  92. off_t pos = lseek(0, SEEK_CUR);
  93. if(pos == -1) return -1;
  94. /* seek to the end to get the file length */
  95. off_t res = lseek(0, SEEK_END);
  96. /* return to our old position */
  97. lseek(pos, SEEK_SET);
  98. return res;
  99. }
  100. virtual ~FileHandle();
  101. };
  102. } // namespace mbed
  103. #endif