portal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * File : portal.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2013-08-19 Grissiom initial version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #define PT_WRITE_DEV(pt) (((struct rt_portal_device*)pt)->write_dev)
  27. #define PT_READ_DEV(pt) (((struct rt_portal_device*)pt)->read_dev)
  28. static rt_err_t _portal_init(rt_device_t dev)
  29. {
  30. rt_err_t err;
  31. struct rt_portal_device *portal;
  32. RT_ASSERT(dev);
  33. portal = (struct rt_portal_device*)dev;
  34. err = rt_device_init(portal->write_dev);
  35. if (err != RT_EOK)
  36. return err;
  37. err = rt_device_init(portal->read_dev);
  38. return err;
  39. }
  40. static rt_err_t _portal_open(rt_device_t dev, rt_uint16_t oflag)
  41. {
  42. rt_err_t err;
  43. struct rt_portal_device *portal;
  44. RT_ASSERT(dev);
  45. if (!oflag)
  46. return -RT_ERROR;
  47. portal = (struct rt_portal_device*)dev;
  48. if (oflag & RT_DEVICE_OFLAG_RDONLY)
  49. {
  50. err = rt_device_open(portal->read_dev, RT_DEVICE_OFLAG_RDONLY);
  51. if (err != RT_EOK)
  52. return err;
  53. }
  54. if (oflag & RT_DEVICE_OFLAG_WRONLY)
  55. {
  56. err = rt_device_open(portal->write_dev, RT_DEVICE_OFLAG_WRONLY);
  57. if (err != RT_EOK)
  58. return err;
  59. }
  60. return RT_EOK;
  61. }
  62. static rt_err_t _portal_close(rt_device_t dev)
  63. {
  64. struct rt_portal_device *portal;
  65. RT_ASSERT(dev);
  66. portal = (struct rt_portal_device*)dev;
  67. rt_device_close(portal->write_dev);
  68. rt_device_close(portal->read_dev);
  69. return RT_EOK;
  70. }
  71. static rt_size_t _portal_read(rt_device_t dev,
  72. rt_off_t pos,
  73. void *buffer,
  74. rt_size_t size)
  75. {
  76. return rt_device_read(PT_READ_DEV(dev),
  77. pos, buffer, size);
  78. }
  79. static rt_size_t _portal_write(rt_device_t dev,
  80. rt_off_t pos,
  81. const void *buffer,
  82. rt_size_t size)
  83. {
  84. return rt_device_write(PT_WRITE_DEV(dev),
  85. pos, buffer, size);
  86. }
  87. static rt_err_t _portal_rx_indicate(rt_device_t dev, rt_size_t size)
  88. {
  89. struct rt_pipe_device *pipe;
  90. RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
  91. pipe = (struct rt_pipe_device*)dev;
  92. if (pipe->read_portal->parent.rx_indicate)
  93. return pipe->read_portal->parent.rx_indicate(
  94. (rt_device_t)pipe->read_portal, size);
  95. return -RT_ENOSYS;
  96. }
  97. static rt_err_t _portal_tx_complete(rt_device_t dev, void *buf)
  98. {
  99. struct rt_pipe_device *pipe;
  100. RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
  101. pipe = (struct rt_pipe_device*)dev;
  102. if (pipe->write_portal->parent.tx_complete)
  103. return pipe->write_portal->parent.tx_complete(
  104. (rt_device_t)pipe->write_portal, buf);
  105. return -RT_ENOSYS;
  106. }
  107. /**
  108. * This function will initialize a portal device and put it under control of
  109. * resource management.
  110. *
  111. * Portal is a device that connect devices
  112. *
  113. * Currently, you can only connect pipes in portal. Pipes are unidirectional.
  114. * But with portal, you can construct a bidirectional device with two pipes.
  115. * The inner connection is just like this:
  116. *
  117. * portal0 portal1
  118. * read || || write
  119. * <--<---||<---<---||<---<-- (pipe0)
  120. * || ||
  121. * -->--->||--->--->||--->--> (pipe1)
  122. * write || || read
  123. *
  124. * You will always construct two portals on two pipes, say, "portal0" and
  125. * "portal1". Data written into "portal0" can be retrieved in "portal1" and
  126. * vice versa. `rx_indicate` and `tx_complete` events are propagated
  127. * accordingly.
  128. *
  129. * @param portal the portal device
  130. * @param portal_name the name of the portal device
  131. * @param write_dev the name of the pipe device that this portal write into
  132. * @param read_dev the name of the pipe device that this portal read from
  133. *
  134. * @return the operation status, RT_EOK on successful. -RT_ENOSYS on one pipe
  135. * device could not be found.
  136. */
  137. rt_err_t rt_portal_init(struct rt_portal_device *portal,
  138. const char *portal_name,
  139. const char *write_dev,
  140. const char *read_dev)
  141. {
  142. rt_device_t dev;
  143. RT_ASSERT(portal);
  144. portal->parent.type = RT_Device_Class_Portal;
  145. portal->parent.init = _portal_init;
  146. portal->parent.open = _portal_open;
  147. portal->parent.close = _portal_close;
  148. portal->parent.write = _portal_write;
  149. portal->parent.read = _portal_read;
  150. /* single control of the two devices makes no sense */
  151. portal->parent.control = RT_NULL;
  152. dev = rt_device_find(write_dev);
  153. if (dev == RT_NULL)
  154. return -RT_ENOSYS;
  155. RT_ASSERT(dev->type == RT_Device_Class_Pipe);
  156. portal->write_dev = dev;
  157. rt_device_set_tx_complete(&portal->parent, dev->tx_complete);
  158. rt_device_set_tx_complete(dev, _portal_tx_complete);
  159. ((struct rt_pipe_device*)dev)->write_portal = portal;
  160. dev = rt_device_find(read_dev);
  161. if (dev == RT_NULL)
  162. {
  163. rt_device_set_tx_complete(dev, portal->parent.tx_complete);
  164. return -RT_ENOSYS;
  165. }
  166. RT_ASSERT(dev->type == RT_Device_Class_Pipe);
  167. portal->read_dev = dev;
  168. rt_device_set_rx_indicate(&portal->parent, dev->rx_indicate);
  169. rt_device_set_rx_indicate(dev, _portal_rx_indicate);
  170. ((struct rt_pipe_device*)dev)->read_portal = portal;
  171. return rt_device_register(&(portal->parent),
  172. portal_name,
  173. RT_DEVICE_FLAG_RDWR);
  174. }
  175. RTM_EXPORT(rt_portal_init);
  176. /**
  177. * This function will detach a portal device from resource management
  178. *
  179. * @param portal the portal device
  180. *
  181. * @return the operation status, RT_EOK on successful
  182. */
  183. rt_err_t rt_portal_detach(struct rt_portal_device *portal)
  184. {
  185. return rt_device_unregister(&portal->parent);
  186. }
  187. RTM_EXPORT(rt_portal_detach);
  188. #ifdef RT_USING_HEAP
  189. rt_err_t rt_portal_create(const char *name,
  190. const char *write_dev,
  191. const char *read_dev)
  192. {
  193. struct rt_portal_device *portal;
  194. portal = (struct rt_portal_device*)rt_calloc(1, sizeof(*portal));
  195. if (portal == RT_NULL)
  196. return -RT_ENOMEM;
  197. return rt_portal_init(portal, name, write_dev, read_dev);
  198. }
  199. RTM_EXPORT(rt_portal_create);
  200. void rt_portal_destroy(struct rt_portal_device *portal)
  201. {
  202. if (portal == RT_NULL)
  203. return;
  204. rt_portal_detach(portal);
  205. rt_free(portal);
  206. return;
  207. }
  208. RTM_EXPORT(rt_portal_destroy);
  209. #endif /* RT_USING_HEAP */