1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <string.h>
11 
12 #include "vfs_types.h"
13 #include "vfs_api.h"
14 
15 #if AOS_COMP_CLI
16 #include "aos/cli.h"
17 #endif
18 
driver_vfs_open(vfs_file_t * fp,const char * path,int32_t flags)19 static int32_t driver_vfs_open(vfs_file_t *fp, const char *path, int32_t flags)
20 {
21     printf("%s\n", __func__);
22     return 0;
23 }
24 
driver_vfs_close(vfs_file_t * fp)25 static int32_t driver_vfs_close(vfs_file_t *fp)
26 {
27     printf("%s\n", __func__);
28     return 0;
29 }
30 
driver_vfs_read(vfs_file_t * fp,char * buf,uint32_t len)31 static int32_t driver_vfs_read(vfs_file_t *fp, char *buf, uint32_t len)
32 {
33     printf("%s\n", __func__);
34     return 0;
35 }
36 
driver_vfs_write(vfs_file_t * fp,const char * buf,uint32_t len)37 static int32_t driver_vfs_write(vfs_file_t *fp, const char *buf, uint32_t len)
38 {
39     printf("%s\n", __func__);
40     return 0;
41 }
42 
driver_vfs_ioctl(vfs_file_t * fp,int32_t cmd,uint32_t arg)43 static int32_t driver_vfs_ioctl(vfs_file_t *fp, int32_t cmd, uint32_t arg)
44 {
45     printf("%s\n", __func__);
46     return 0;
47 }
48 
driver_vfs_lseek(vfs_file_t * fp,int64_t off,int32_t whence)49 static uint32_t driver_vfs_lseek(vfs_file_t *fp, int64_t off, int32_t whence)
50 {
51     printf("%s\n", __func__);
52     return 0;
53 }
54 
55 vfs_file_ops_t driver_ops = {
56     .open      = &driver_vfs_open,
57     .close     = &driver_vfs_close,
58     .read      = &driver_vfs_read,
59     .write     = &driver_vfs_write,
60     .ioctl     = &driver_vfs_ioctl,
61     .lseek     = &driver_vfs_lseek,
62 };
63 
driver_vfs_example(void)64 static void driver_vfs_example(void)
65 {
66     char *mount_path = "/dev";
67     int ret = 0;
68     char *buf[10];
69     uint32_t len = 0;
70     int fd = 0;
71     int32_t cmd = 0;
72     uint32_t arg = 0;
73     int64_t off = 0;
74     int32_t whence = 0;
75 
76     printf("driver vfs example start\r\n");
77 
78     ret = vfs_register_driver(mount_path, &driver_ops, NULL);
79     if (ret < 0) {
80         printf("vfs_register_driver failed!\n");
81         return;
82     }
83 
84     fd = vfs_open(mount_path, O_WRONLY);
85     if (fd < 0) {
86         printf("vfs_open failed!\n");
87         vfs_unregister_driver(mount_path);
88         return;
89     }
90     vfs_read(fd, buf, len);
91     vfs_write(fd, buf, len);
92     vfs_ioctl(fd, cmd, arg);
93     vfs_lseek(fd, off, whence);
94 
95     vfs_close(fd);
96     vfs_unregister_driver(mount_path);
97     printf("driver vfs example end\r\n");
98 }
99 
fs_vfs_open(vfs_file_t * fp,const char * path,int32_t flags)100 static int32_t fs_vfs_open(vfs_file_t *fp, const char *path, int32_t flags)
101 {
102     printf("%s\n", __func__);
103     return 0;
104 }
fs_vfs_close(vfs_file_t * fp)105 static int32_t fs_vfs_close(vfs_file_t *fp)
106 {
107     printf("%s\n", __func__);
108     return 0;
109 }
fs_vfs_read(vfs_file_t * fp,char * buf,uint32_t len)110 static int32_t fs_vfs_read(vfs_file_t *fp, char *buf, uint32_t len)
111 {
112     printf("%s\n", __func__);
113     return 0;
114 }
fs_vfs_write(vfs_file_t * fp,const char * buf,uint32_t len)115 static int32_t fs_vfs_write(vfs_file_t *fp, const char *buf, uint32_t len)
116 {
117     printf("%s\n", __func__);
118     return 0;
119 }
120 vfs_filesystem_ops_t fs_ops = {
121     .open      = &fs_vfs_open,
122     .close     = &fs_vfs_close,
123     .read      = &fs_vfs_read,
124     .write     = &fs_vfs_write,
125 };
126 
fs_vfs_example(void)127 static void fs_vfs_example(void)
128 {
129     char *mount_path = "/fs";
130     int ret = 0;
131     char *buf[10];
132     uint32_t len = 0;
133     int fd = 0;
134 
135     printf("fs vfs example start\r\n");
136 
137     ret = vfs_register_fs(mount_path, &fs_ops, NULL);
138     if (ret < 0) {
139         printf("vfs_register_fs failed!\n");
140         return;
141     }
142 
143     fd = vfs_open(mount_path, O_WRONLY | O_CREAT | O_TRUNC);
144     if (fd < 0) {
145         printf("vfs_open failed!\n");
146         vfs_unregister_fs(mount_path);
147         return;
148     }
149 
150     vfs_read(fd, buf, len);
151     vfs_write(fd, buf, len);
152     vfs_close(fd);
153 
154     vfs_unregister_fs(mount_path);
155     printf("fs vfs example end\r\n");
156 }
157 
vfs_comp_example(int argc,char ** argv)158 static void vfs_comp_example(int argc, char **argv)
159 {
160     driver_vfs_example();
161     fs_vfs_example();
162     printf("vfs example test success!\r\n");
163 }
164 
165 #if AOS_COMP_CLI
166 /* reg args: fun, cmd, description*/
167 ALIOS_CLI_CMD_REGISTER(vfs_comp_example, vfs_example, vfs component base example)
168 #endif
169