proc_cpuinfo.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include "proc.h"
  10. #include "procfs.h"
  11. #include <rthw.h>
  12. #include <rtdbg.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <dfs_dentry.h>
  16. static void *seq_start(struct dfs_seq_file *seq, off_t *index)
  17. {
  18. off_t i = *index; // seq->index
  19. return NULL + (i == 0);
  20. }
  21. static void seq_stop(struct dfs_seq_file *seq, void *data)
  22. {
  23. }
  24. static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
  25. {
  26. /* data: The return value of the start or next*/
  27. off_t i = *index + 1; // seq->index
  28. *index = i;
  29. return NULL;
  30. }
  31. static int seq_show(struct dfs_seq_file *seq, void *data)
  32. {
  33. /* data: The return value of the start or next*/
  34. dfs_seq_puts(seq, "rt_weak const struct dfs_seq_ops *cpuinfo_get_seq_ops(void)\n--need your own function--\n");
  35. return 0;
  36. }
  37. static const struct dfs_seq_ops seq_ops = {
  38. .start = seq_start,
  39. .stop = seq_stop,
  40. .next = seq_next,
  41. .show = seq_show,
  42. };
  43. rt_weak const struct dfs_seq_ops *cpuinfo_get_seq_ops(void)
  44. {
  45. return &seq_ops;
  46. }
  47. static int proc_open(struct dfs_file *file)
  48. {
  49. return dfs_seq_open(file, cpuinfo_get_seq_ops());
  50. }
  51. static int proc_close(struct dfs_file *file)
  52. {
  53. return dfs_seq_release(file);
  54. }
  55. static const struct dfs_file_ops file_ops = {
  56. .open = proc_open,
  57. .read = dfs_seq_read,
  58. .lseek = dfs_seq_lseek,
  59. .close = proc_close,
  60. };
  61. int proc_cpuinfo_init(void)
  62. {
  63. struct proc_dentry *dentry = proc_create_data("cpuinfo", 0, NULL, &file_ops, NULL);
  64. proc_release(dentry);
  65. return 0;
  66. }
  67. INIT_ENV_EXPORT(proc_cpuinfo_init);