funopen.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2013 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Id: funopen.c 5472 2013-12-06 00:16:28Z olereinhardt $
  36. */
  37. #include <sys/device.h>
  38. #include <sys/file.h>
  39. #include <fs/fs.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <errno.h>
  43. #include "nut_io.h"
  44. /*!
  45. * \addtogroup xgCrtStdio
  46. */
  47. /*@{*/
  48. typedef struct _USERDEVIF USERDEVIF;
  49. struct _USERDEVIF {
  50. int (*ifc_read) (void *, char *, int);
  51. int (*ifc_write) (void *, const char *, int);
  52. long (*ifc_seek)(void *, long, int);
  53. int (*ifc_close)(void *);
  54. };
  55. static int UserDevRead(NUTFILE *nfp, void *buffer, int size)
  56. {
  57. USERDEVIF *ifc;
  58. ifc = (USERDEVIF *) nfp->nf_dev->dev_icb;
  59. return ifc->ifc_read(nfp->nf_fcb, (char *) buffer, size);
  60. }
  61. static int UserDevWrite(NUTFILE *nfp, const void *buffer, int len)
  62. {
  63. USERDEVIF *ifc = (USERDEVIF *) nfp->nf_dev->dev_icb;
  64. return ifc->ifc_write(nfp->nf_fcb, (const char *) buffer, len);
  65. }
  66. static int UserDevClose(NUTFILE *nfp)
  67. {
  68. int rc;
  69. USERDEVIF *ifc = (USERDEVIF *) nfp->nf_dev->dev_icb;
  70. rc = ifc->ifc_close(nfp->nf_fcb);
  71. free(nfp->nf_dev->dev_icb);
  72. free(nfp->nf_dev);
  73. free(nfp);
  74. return rc;
  75. }
  76. static int UserDevIoCtl(NUTDEVICE * dev, int req, void *conf)
  77. {
  78. if (req == FS_FILE_SEEK) {
  79. IOCTL_ARG3 *args = (IOCTL_ARG3 *) conf;
  80. NUTFILE *nfp = (NUTFILE *) args->arg1;
  81. USERDEVIF *ifc = (USERDEVIF *) nfp->nf_dev->dev_icb;
  82. ifc->ifc_seek(nfp->nf_fcb, *((long *) args->arg2), (int) args->arg3);
  83. }
  84. return -1;
  85. }
  86. /*!
  87. * \brief Create a stream that is associated to user functions.
  88. *
  89. * \param cookie A generic pointer that is passed to the user functions
  90. * instead of a file descriptor.
  91. * \param readfn User function for reading.
  92. * \param writefn User function for writing.
  93. * \param seekfn User function for moving the file pointer.
  94. * \param closefn User function for closing the stream.
  95. *
  96. * \return A pointer to the open stream or a null pointer to indicate
  97. * an error.
  98. */
  99. FILE *funopen(void *cookie,
  100. int (*readfn) (void *, char *, int),
  101. int (*writefn) (void *, const char *, int),
  102. long (*seekfn)(void *, long, int),
  103. int (*closefn)(void *))
  104. {
  105. NUTFILE *nfp;
  106. USERDEVIF *ifc;
  107. NUTDEVICE *dev;
  108. int fd;
  109. int i;
  110. /*
  111. * Find an empty slot.
  112. */
  113. for (i = 0; __iob[i];) {
  114. if (++i >= FOPEN_MAX) {
  115. errno = ENFILE;
  116. return NULL;
  117. }
  118. }
  119. for (fd = 0; __fds[fd];) {
  120. if (++fd >= FOPEN_MAX) {
  121. errno = EMFILE;
  122. return NULL;
  123. }
  124. }
  125. __iob[i] = (FILE *) calloc(1, sizeof(FILE));
  126. nfp = (NUTFILE *) malloc(sizeof(NUTFILE));
  127. dev = (NUTDEVICE *) calloc(1, sizeof(NUTDEVICE));
  128. ifc = (USERDEVIF *) malloc(sizeof(USERDEVIF));
  129. if (__iob[i] && nfp && dev && ifc) {
  130. ifc->ifc_read = readfn;
  131. ifc->ifc_write = writefn;
  132. ifc->ifc_seek = seekfn;
  133. ifc->ifc_close = closefn;
  134. dev->dev_icb = ifc;
  135. dev->dev_ioctl = UserDevIoCtl;
  136. dev->dev_read = UserDevRead;
  137. dev->dev_write = UserDevWrite;
  138. dev->dev_close = UserDevClose;
  139. nfp->nf_dev = dev;
  140. nfp->nf_fcb = cookie;
  141. __iob[i]->iob_fd = (int) nfp;
  142. __fds[fd] = nfp;
  143. return __iob[i];
  144. }
  145. free(__iob[i]);
  146. __iob[i] = NULL;
  147. errno = ENOMEM;
  148. return NULL;
  149. }
  150. /*@}*/