db_devices.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_device.c
  17. *
  18. * This file contains operands to handle streams with URIs that start with
  19. * "dev:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. #define DEVICE_OPS(handle) \
  28. ({ int _type = (handle)->dev.dev_type; \
  29. (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
  30. NULL : pal_device_ops[_type]; \
  31. })
  32. enum {
  33. device_type_none = 0,
  34. device_type_term,
  35. PAL_DEVICE_TYPE_BOUND,
  36. };
  37. static struct handle_ops term_ops;
  38. static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
  39. NULL,
  40. &term_ops,
  41. };
  42. /* parse-device_uri scan the uri, parse the prefix of the uri and search
  43. for stream handler wich will open or access the device */
  44. static int parse_device_uri (const char ** uri, const char ** type,
  45. struct handle_ops ** ops)
  46. {
  47. struct handle_ops * dops = NULL;
  48. const char * p, * u = (*uri);
  49. for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
  50. if (strpartcmp_static(u, "tty"))
  51. dops = &term_ops;
  52. if (!dops)
  53. return -PAL_ERROR_NOTSUPPORT;
  54. *uri = (*p) ? p + 1 : p;
  55. if (type)
  56. *type = u;
  57. if (ops)
  58. *ops = dops;
  59. return 0;
  60. }
  61. static int char_read (PAL_HANDLE handle, int offset, int count, void * buffer);
  62. static int char_write (PAL_HANDLE handle, int offset, int count,
  63. const void * buffer);
  64. static int term_attrquery (const char * type, const char * uri,
  65. PAL_STREAM_ATTR * attr);
  66. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  67. PAL_STREAM_ATTR * attr);
  68. /* 'open' operation for terminal stream */
  69. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  70. int access, int share, int create, int options)
  71. {
  72. return -PAL_ERROR_NOTIMPLEMENTED;
  73. }
  74. static int term_close (PAL_HANDLE handle)
  75. {
  76. return -PAL_ERROR_NOTIMPLEMENTED;
  77. }
  78. /* 'attrquery' operation for terminal stream */
  79. static int term_attrquery (const char * type, const char * uri,
  80. PAL_STREAM_ATTR * attr)
  81. {
  82. return -PAL_ERROR_NOTIMPLEMENTED;
  83. }
  84. /* 'attrquery' operation for terminal stream */
  85. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  86. PAL_STREAM_ATTR * attr)
  87. {
  88. return -PAL_ERROR_NOTIMPLEMENTED;
  89. }
  90. static struct handle_ops term_ops = {
  91. .open = &term_open,
  92. .close = &term_close,
  93. .read = &char_read,
  94. .write = &char_write,
  95. .attrquery = &term_attrquery,
  96. .attrquerybyhdl = &term_attrquerybyhdl,
  97. };
  98. /* 'read' operation for character streams. */
  99. static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  100. {
  101. return -PAL_ERROR_NOTIMPLEMENTED;
  102. }
  103. /* 'write' operation for character streams. */
  104. static int char_write (PAL_HANDLE handle, int offset, int size,
  105. const void * buffer)
  106. {
  107. return -PAL_ERROR_NOTIMPLEMENTED;
  108. }
  109. /* 'open' operation for device streams */
  110. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  111. int access, int share, int create, int options)
  112. {
  113. return -PAL_ERROR_NOTIMPLEMENTED;
  114. }
  115. /* 'read' operation for device stream */
  116. static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  117. {
  118. const struct handle_ops * ops = DEVICE_OPS(handle);
  119. if (!ops || !ops->read)
  120. return -PAL_ERROR_NOTSUPPORT;
  121. return ops->read(handle, offset, size, buffer);
  122. }
  123. /* 'write' operation for device stream */
  124. static int dev_write (PAL_HANDLE handle, int offset, int size,
  125. const void * buffer)
  126. {
  127. const struct handle_ops * ops = DEVICE_OPS(handle);
  128. if (!ops || !ops->write)
  129. return -PAL_ERROR_NOTSUPPORT;
  130. return ops->write(handle, offset, size, buffer);
  131. }
  132. /* 'close' operation for device streams */
  133. static int dev_close (PAL_HANDLE handle)
  134. {
  135. const struct handle_ops * ops = DEVICE_OPS(handle);
  136. if (ops && ops->close)
  137. return ops->close(handle);
  138. return -PAL_ERROR_NOTIMPLEMENTED;
  139. }
  140. /* 'delete' operation for device streams */
  141. static int dev_delete (PAL_HANDLE handle, int access)
  142. {
  143. const struct handle_ops * ops = DEVICE_OPS(handle);
  144. if (!ops || !ops->delete)
  145. return -PAL_ERROR_DENIED;
  146. int ret = dev_close(handle);
  147. if (ret < 0)
  148. return ret;
  149. return ops->delete(handle, access);
  150. }
  151. /* 'flush' operation for device streams */
  152. static int dev_flush (PAL_HANDLE handle)
  153. {
  154. const struct handle_ops * ops = DEVICE_OPS(handle);
  155. if (ops && ops->flush)
  156. return ops->flush(handle);
  157. return -PAL_ERROR_NOTIMPLEMENTED;
  158. }
  159. /* 'attrquery' operation for device streams */
  160. static int dev_attrquery (const char * type, const char * uri,
  161. PAL_STREAM_ATTR * attr)
  162. {
  163. struct handle_ops * ops = NULL;
  164. const char * dev_type = NULL;
  165. int ret = 0;
  166. ret = parse_device_uri(&uri, &dev_type, &ops);
  167. if (ret < 0)
  168. return ret;
  169. if (!ops || !ops->attrquery)
  170. return -PAL_ERROR_NOTSUPPORT;
  171. return ops->attrquery(dev_type, uri, attr);
  172. }
  173. /* 'attrquerybyhdl' operation for device stream */
  174. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  175. PAL_STREAM_ATTR * attr)
  176. {
  177. const struct handle_ops * ops = DEVICE_OPS(handle);
  178. if (ops && ops->attrquerybyhdl)
  179. return ops->attrquerybyhdl(handle, attr);
  180. return -PAL_ERROR_NOTIMPLEMENTED;
  181. }
  182. static const char * dev_getrealpath (PAL_HANDLE handle)
  183. {
  184. return NULL;
  185. }
  186. struct handle_ops dev_ops = {
  187. .getrealpath = &dev_getrealpath,
  188. .open = &dev_open,
  189. .read = &dev_read,
  190. .write = &dev_write,
  191. .close = &dev_close,
  192. .delete = &dev_delete,
  193. .flush = &dev_flush,
  194. .attrquery = &dev_attrquery,
  195. .attrquerybyhdl = &dev_attrquerybyhdl,
  196. };