feat: hook up the keyboard input into our vfs
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/proc.h>
5 #include <lunaix/syslog.h>
6
7 LOG_MODULE("IOTEST")
8
9 void
10 _iotest_main()
11 {
12     char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
13                            "There were two regal sisters who ruled together "
14                            "and created harmony for all the land.";
15
16     // sda 设备 - 硬盘
17     //  sda设备属于容积设备(Volumetric Device),
18     //  Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能)
19     int fd = open("/dev/sda", 0);
20
21     // tty 设备 - 控制台。
22     //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
23     //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
24     int tty = open("/dev/tty", 0);
25
26     if (fd < 0 || tty < 0) {
27         kprintf(KERROR "fail to open (%d)\n", geterrno());
28         return;
29     }
30
31     // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处
32     lseek(fd, 512, FSEEK_SET);
33
34     // 总共写入 64 * 136 字节,会产生3个页作为缓存
35     for (size_t i = 0; i < 64; i++) {
36         write(fd, test_sequence, sizeof(test_sequence));
37     }
38
39     lseek(fd, 521, FSEEK_SET);
40     write(fd, test_sequence, sizeof(test_sequence));
41
42     char read_out[256];
43     write(tty, "input: ", 8);
44     int size = read(tty, read_out, 256);
45
46     write(tty, "your input: ", 13);
47     write(tty, read_out, size);
48     write(fd, read_out, size);
49     write(tty, "\n", 1);
50
51     // 读出我们写的内容
52     lseek(fd, 512, FSEEK_SET);
53     read(fd, read_out, sizeof(read_out));
54
55     // 将读出的内容直接写入tty设备
56     write(tty, read_out, sizeof(read_out));
57     write(tty, "\n", 1);
58
59     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
60     close(fd);
61     close(tty);
62
63     kprint_hex(read_out, sizeof(read_out));
64 }