1 #if AOS_COMP_CLI
2 #include "aos/cli.h"
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <aos/errno.h>
9 #include <aos/kernel.h>
10 #include <aos/vfs.h>
11 #include "aos/init.h"
12 #include "board.h"
13 #include <k_api.h>
14 
15 
16 #define LFS_MNT_PATH "/data"
17 #define TEST_FILE LFS_MNT_PATH"/demo"
18 #define TEST_CONTENT "haas100 littlefs demo\n"
read_write()19 void read_write()
20 {
21     int buff[128] = {0};
22     int ret=0;
23     int fd1, fd2;
24     printf("littlefs component example start !\r\n");
25 
26     fd1 = aos_open(TEST_FILE,  O_CREAT | O_RDWR | O_APPEND);
27     if(fd1 > 0){
28         printf("aos_open fd1=%d\n", fd1);
29     }else{
30         printf("littlefs comp test fail! aos_open ret:%d\r\n", fd1);
31         goto Exit;
32     }
33 
34     ret = aos_write(fd1, "haas100 littlefs demo\n", 22);
35     if(ret > 0){
36         printf("aos_write num=%d\n", ret);
37     }else{
38         printf("littlefs comp test fail! aos_write ret:%d\r\n", fd1);
39         goto Exit;
40     }
41 
42     ret = aos_sync(fd1);
43     if(ret){
44         printf("littlefs comp test fail! aos_sync ret:%d\r\n", ret);
45         goto Exit;
46     }
47 
48     fd2 = aos_open(TEST_FILE, O_RDWR);
49     if(fd2 > 0){
50         printf("aos_open fd2=%d\n", fd2);
51     }else{
52         printf("littlefs comp test fail! aos_open ret:%d\r\n", fd2);
53         goto Exit;
54     }
55     ret =  aos_lseek(fd2, 21, 0);
56     if(ret>0){
57         printf("aos_lseek position=%d\n", ret);
58     }else{
59         printf("littlefs comp test fail! aos_lseek ret:%d\r\n", ret);
60         goto Exit;
61     }
62 
63     ret = aos_read(fd2, buff, 128);
64     if (ret > 0) {
65         printf("aos_read: %s\n", buff);
66     }else{
67         printf("littlefs comp test fail! aos_read ret:%d\r\n", ret);
68         goto Exit;
69     }
70 
71 Exit:
72     if(fd1>0)
73     {
74         aos_close(fd1);
75     }
76     if(fd2>0)
77     {
78         aos_close(fd2);
79     }
80 
81 }
82 
read_dir()83 void read_dir()
84 {
85     aos_dir_t *dir = aos_opendir(LFS_MNT_PATH);
86 
87     aos_dirent_t *dp = NULL;
88     do {
89         dp = aos_readdir(dir);
90         if (dp)
91             printf("readdir: %s\n", dp->d_name);
92     } while (dp != NULL);
93 
94     aos_closedir(dir);
95 }
96 
97 
littlefs_comp_example(int argc,char ** argv)98 static void littlefs_comp_example(int argc, char **argv)
99 {
100     printf("littlefs component example start !\r\n");
101     read_write();
102     read_dir();
103     printf("littlefs component example end !\r\n");
104 }
105 
106 
107 #if AOS_COMP_CLI
108 /* reg args: fun, cmd, description*/
109 ALIOS_CLI_CMD_REGISTER(littlefs_comp_example, littlefs_example, littlefs component base example)
110 #endif