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 
10 #include "proc.h"
11 #include "procfs.h"
12 
13 #include <rthw.h>
14 #include <rtdbg.h>
15 
16 #include <fcntl.h>
17 #include <errno.h>
18 
19 #include <dfs_dentry.h>
20 
seq_start(struct dfs_seq_file * seq,off_t * index)21 static void *seq_start(struct dfs_seq_file *seq, off_t *index)
22 {
23     off_t i = *index; // seq->index
24 
25     return NULL + (i == 0);
26 }
27 
seq_stop(struct dfs_seq_file * seq,void * data)28 static void seq_stop(struct dfs_seq_file *seq, void *data)
29 {
30 }
31 
seq_next(struct dfs_seq_file * seq,void * data,off_t * index)32 static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
33 {
34     /* data: The return value of the start or next*/
35     off_t i = *index + 1; // seq->index
36 
37     *index = i;
38 
39     return NULL;
40 }
41 
seq_show(struct dfs_seq_file * seq,void * data)42 static int seq_show(struct dfs_seq_file *seq, void *data)
43 {
44     /* data: The return value of the start or next*/
45     dfs_seq_puts(seq, "rt_weak const struct dfs_seq_ops *cpuinfo_get_seq_ops(void)\n--need your own function--\n");
46 
47     return 0;
48 }
49 
50 static const struct dfs_seq_ops seq_ops = {
51     .start  = seq_start,
52     .stop   = seq_stop,
53     .next   = seq_next,
54     .show   = seq_show,
55 };
56 
cpuinfo_get_seq_ops(void)57 rt_weak const struct dfs_seq_ops *cpuinfo_get_seq_ops(void)
58 {
59     return &seq_ops;
60 }
61 
proc_open(struct dfs_file * file)62 static int proc_open(struct dfs_file *file)
63 {
64     return dfs_seq_open(file, cpuinfo_get_seq_ops());
65 }
66 
proc_close(struct dfs_file * file)67 static int proc_close(struct dfs_file *file)
68 {
69     return dfs_seq_release(file);
70 }
71 
72 static const struct dfs_file_ops file_ops = {
73     .open   = proc_open,
74     .read   = dfs_seq_read,
75     .lseek  = dfs_seq_lseek,
76     .close  = proc_close,
77 };
78 
proc_cpuinfo_init(void)79 int proc_cpuinfo_init(void)
80 {
81     struct proc_dentry *dentry = proc_create_data("cpuinfo", 0, NULL, &file_ops, NULL);
82     proc_release(dentry);
83 
84     return 0;
85 }
86 INIT_ENV_EXPORT(proc_cpuinfo_init);
87