null.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /* @file null.c - a nut/os null device driver
  34. * @brief A /dev/null device driver
  35. * This can be useful if your application might write unwanted output to stdout.
  36. * With this deveice you can redirect stdout to the nullDev which discards any output
  37. * Any IOCTL will result in OK, and any read will give back zero read bytes
  38. *
  39. * 2005.06.21 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  40. *
  41. */
  42. #include <compiler.h>
  43. #include <stdlib.h>
  44. #include <sys/file.h>
  45. #include <sys/device.h>
  46. /*!
  47. * \addtogroup xgDevice
  48. */
  49. /*@{*/
  50. /*!
  51. * \brief Open UnixDev
  52. *
  53. * \return Pointer to a static NUTFILE structure.
  54. */
  55. static NUTFILE *NullOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  56. {
  57. NUTFILE *nf;
  58. nf = malloc(sizeof(NUTFILE));
  59. if (!nf) {
  60. return NUTFILE_EOF;
  61. }
  62. // enter data
  63. nf->nf_dev = dev;
  64. return nf;
  65. }
  66. /*!
  67. * \brief Blocking write bytes to file
  68. *
  69. * \return Number of characters sent.
  70. */
  71. static int NullWrite(NUTFILE * nf, const void *buffer, int len)
  72. {
  73. return len;
  74. }
  75. #ifdef __HARVARD_ARCH__
  76. static int NullWriteP(NUTFILE * nf, PGM_P buffer, int len)
  77. {
  78. return len;
  79. }
  80. #endif
  81. /*!
  82. * \brief Read bytes from file
  83. *
  84. * \return Number of characters read.
  85. */
  86. static int NullRead(NUTFILE * nf, void *buffer, int len)
  87. {
  88. // test for read len. len == 0 => flush fd
  89. if (len == 0){
  90. return 0;
  91. }
  92. // otherwise also just return 0 bytes without blocking
  93. return 0;
  94. }
  95. /*!
  96. * \brief Close ...
  97. *
  98. * \return Always 0.
  99. */
  100. static int NullClose(NUTFILE * nf)
  101. {
  102. if (nf)
  103. free (nf);
  104. return 0;
  105. }
  106. /*!
  107. * \brief Perform control functions.
  108. *
  109. * This function is called by the ioctl() function of the C runtime
  110. * library.
  111. *
  112. * \param dev Identifies the device that receives the device-control
  113. * function.
  114. * \param req Requested control function. We do return ok for any function
  115. * \param conf Points to a buffer that contains any data required for
  116. * the given control function or receives data from that
  117. * function.
  118. * \return 0 on success, -1 otherwise.
  119. *
  120. */
  121. int NullIOCTL(NUTDEVICE * dev, int req, void *conf)
  122. {
  123. return 0;
  124. }
  125. /* ======================= Devices ======================== */
  126. /*!
  127. * \brief Null device information structure.
  128. */
  129. NUTDEVICE devNull = {
  130. 0, /*!< Pointer to next device. */
  131. {'n', 'u', 'l', 'l', 0, 0, 0, 0, 0},
  132. /*!< Unique device name. */
  133. 0, /*!< Type of device. */
  134. 0, /*!< Base address. */
  135. 0, /*!< First interrupt number. */
  136. 0, /*!< Interface control block. */
  137. 0, /*!< Driver control block. */
  138. 0, /*!< Driver initialization routine. */
  139. NullIOCTL, /*!< Driver specific control function. */
  140. NullRead,
  141. NullWrite,
  142. #ifdef __HARVARD_ARCH__
  143. NullWriteP,
  144. #endif
  145. NullOpen,
  146. NullClose,
  147. 0,
  148. 0,
  149. };
  150. /*@}*/