db_devices.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. switch (p - u) {
  51. case 3:
  52. if (!memcmp(u, "tty", 3))
  53. dops = &term_ops;
  54. break;
  55. default:
  56. break;
  57. }
  58. if (!dops)
  59. return -PAL_ERROR_NOTSUPPORT;
  60. *uri = (*p) ? p + 1 : p;
  61. if (type)
  62. *type = u;
  63. if (ops)
  64. *ops = dops;
  65. return 0;
  66. }
  67. static int char_read (PAL_HANDLE handle, int offset, int count, void * buffer);
  68. static int char_write (PAL_HANDLE handle, int offset, int count,
  69. const void * buffer);
  70. static int term_attrquery (const char * type, const char * uri,
  71. PAL_STREAM_ATTR * attr);
  72. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  73. PAL_STREAM_ATTR * attr);
  74. /* 'open' operation for terminal stream */
  75. static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
  76. int access, int share, int create, int options)
  77. {
  78. return -PAL_ERROR_NOTIMPLEMENTED;
  79. }
  80. static int term_close (PAL_HANDLE handle)
  81. {
  82. return -PAL_ERROR_NOTIMPLEMENTED;
  83. }
  84. /* 'attrquery' operation for terminal stream */
  85. static int term_attrquery (const char * type, const char * uri,
  86. PAL_STREAM_ATTR * attr)
  87. {
  88. return -PAL_ERROR_NOTIMPLEMENTED;
  89. }
  90. /* 'attrquery' operation for terminal stream */
  91. static int term_attrquerybyhdl (PAL_HANDLE hdl,
  92. PAL_STREAM_ATTR * attr)
  93. {
  94. return -PAL_ERROR_NOTIMPLEMENTED;
  95. }
  96. static struct handle_ops term_ops = {
  97. .open = &term_open,
  98. .close = &term_close,
  99. .read = &char_read,
  100. .write = &char_write,
  101. .attrquery = &term_attrquery,
  102. .attrquerybyhdl = &term_attrquerybyhdl,
  103. };
  104. /* 'read' operation for character streams. */
  105. static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  106. {
  107. return -PAL_ERROR_NOTIMPLEMENTED;
  108. }
  109. /* 'write' operation for character streams. */
  110. static int char_write (PAL_HANDLE handle, int offset, int size,
  111. const void * buffer)
  112. {
  113. return -PAL_ERROR_NOTIMPLEMENTED;
  114. }
  115. /* 'open' operation for device streams */
  116. static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
  117. int access, int share, int create, int options)
  118. {
  119. return -PAL_ERROR_NOTIMPLEMENTED;
  120. }
  121. /* 'read' operation for device stream */
  122. static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
  123. {
  124. const struct handle_ops * ops = DEVICE_OPS(handle);
  125. if (!ops || !ops->read)
  126. return -PAL_ERROR_NOTSUPPORT;
  127. return ops->read(handle, offset, size, buffer);
  128. }
  129. /* 'write' operation for device stream */
  130. static int dev_write (PAL_HANDLE handle, int offset, int size,
  131. const void * buffer)
  132. {
  133. const struct handle_ops * ops = DEVICE_OPS(handle);
  134. if (!ops || !ops->write)
  135. return -PAL_ERROR_NOTSUPPORT;
  136. return ops->write(handle, offset, size, buffer);
  137. }
  138. /* 'close' operation for device streams */
  139. static int dev_close (PAL_HANDLE handle)
  140. {
  141. const struct handle_ops * ops = DEVICE_OPS(handle);
  142. if (ops && ops->close)
  143. return ops->close(handle);
  144. return -PAL_ERROR_NOTIMPLEMENTED;
  145. }
  146. /* 'delete' operation for device streams */
  147. static int dev_delete (PAL_HANDLE handle, int access)
  148. {
  149. const struct handle_ops * ops = DEVICE_OPS(handle);
  150. if (!ops || !ops->delete)
  151. return -PAL_ERROR_DENIED;
  152. int ret = dev_close(handle);
  153. if (ret < 0)
  154. return ret;
  155. return ops->delete(handle, access);
  156. }
  157. /* 'flush' operation for device streams */
  158. static int dev_flush (PAL_HANDLE handle)
  159. {
  160. const struct handle_ops * ops = DEVICE_OPS(handle);
  161. if (ops && ops->flush)
  162. return ops->flush(handle);
  163. return -PAL_ERROR_NOTIMPLEMENTED;
  164. }
  165. /* 'attrquery' operation for device streams */
  166. static int dev_attrquery (const char * type, const char * uri,
  167. PAL_STREAM_ATTR * attr)
  168. {
  169. struct handle_ops * ops = NULL;
  170. const char * dev_type = NULL;
  171. int ret = 0;
  172. ret = parse_device_uri(&uri, &dev_type, &ops);
  173. if (ret < 0)
  174. return ret;
  175. if (!ops || !ops->attrquery)
  176. return -PAL_ERROR_NOTSUPPORT;
  177. return ops->attrquery(dev_type, uri, attr);
  178. }
  179. /* 'attrquerybyhdl' operation for device stream */
  180. static int dev_attrquerybyhdl (PAL_HANDLE handle,
  181. PAL_STREAM_ATTR * attr)
  182. {
  183. const struct handle_ops * ops = DEVICE_OPS(handle);
  184. if (ops && ops->attrquerybyhdl)
  185. return ops->attrquerybyhdl(handle, attr);
  186. return -PAL_ERROR_NOTIMPLEMENTED;
  187. }
  188. static const char * dev_getrealpath (PAL_HANDLE handle)
  189. {
  190. return NULL;
  191. }
  192. struct handle_ops dev_ops = {
  193. .getrealpath = &dev_getrealpath,
  194. .open = &dev_open,
  195. .read = &dev_read,
  196. .write = &dev_write,
  197. .close = &dev_close,
  198. .delete = &dev_delete,
  199. .flush = &dev_flush,
  200. .attrquery = &dev_attrquery,
  201. .attrquerybyhdl = &dev_attrquerybyhdl,
  202. };