System Calls cleanup, multiple Processes and context switch
[cortex-from-scratch] / term.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/8/14 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Small terminal with some built-in debug commands
9  * 
10  * */
11
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include <sys/robsys.h>
17 #include <sys/mmap.h>
18
19 #include <lib/stdio.h>
20 #include <lib/string.h>
21 #include <lib/regfunc.h>
22 #include <lib/tinyprintf.h>
23
24 #include <drivers/led.h>
25
26 #define SERIAL 1
27 #define BUFSIZE 200
28 #define MAXARGS 5
29 #define WHITESPACE "\t\r\n "
30 #define BUILTINCMDS 4
31
32 /* 
33  * Built in commands
34  *      info -- shows basic info of system
35  *      uptime -- uptime; read from the RTC register
36  *      reset -- software reset TODO
37  *      showmem xxxxxxxx -- shows address value
38  *      led -- led on/off
39  *      switchmode -- switch to unprivileged mode TODO
40  * */
41
42 static char buf[BUFSIZE];
43
44 struct cmd {
45         char * name;
46         int (*function)(int argc, char ** argsv);
47 };
48
49 struct cmd builtincmds[BUILTINCMDS];
50
51 int info(int argc, char ** argsv) {
52         /* extern mem_pool_t kheap_pool;
53         kheap_info(&kheap_pool); */
54         kalloc(get_kheap());
55         return 0;
56 }
57
58 int uptime(int arg, char ** argsv) {
59         printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL);
60 }
61
62 int led(int argc, char ** argsv) {
63
64         if (argsv[1] != NULL) {
65                 if (strcmp(argsv[1], "on")) {
66                         printf("LED ON\n");
67                         led_on();
68                         }
69                 else if (strcmp(argsv[1], "off")) {
70                         printf("LED OFF\n");
71                         led_off();
72                         }
73         }
74         return 0;
75 }
76
77 int showmem(int argc, char ** argsv) {
78
79         if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
80         
81                 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
82                 printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check);
83                 return 1;
84         
85         }
86
87         return 0;
88 }
89
90 int exec_cmd(char * buf) {
91
92         int argc;
93         char *argv[MAXARGS];
94         int i;
95
96         // Parse the command buffer into whitespace-separated arguments
97         argc = 0;
98         argv[argc] = 0;
99         while (1) {
100                 // gobble whitespace
101                 while (*buf && strchr(WHITESPACE, *buf))
102                         *buf++ = 0;
103                 if (*buf == 0)
104                         break;
105
106                 // save and scan past next arg
107                 if (argc == MAXARGS-1) {
108                         printf("Too many arguments\n");
109                         return 0;
110                 }
111                 argv[argc++] = buf;
112                 while (*buf && !strchr(WHITESPACE, *buf))
113                         buf++;
114         }
115         argv[argc] = 0;
116
117         // Lookup and invoke the command
118         if (argc == 0)
119                 return 0;
120         for (i = 0; i < BUILTINCMDS; i++) {
121                 if (strcmp(argv[0], builtincmds[i].name))
122                         return builtincmds[i].function(argc, argv);
123         }
124         printf("Unknown command\n");
125         return 0;
126
127
128
129 void terminal() {
130
131         builtincmds[0].name = "info";
132         builtincmds[0].function = info;
133
134         builtincmds[1].name = "led";
135         builtincmds[1].function = led;
136
137         builtincmds[2].name = "showmem";
138         builtincmds[2].function = showmem;
139
140         builtincmds[3].name = "uptime";
141         builtincmds[3].function = uptime;
142
143
144         char *buf;
145  
146          while (1) {
147                  buf = readline("root# ");
148                         if (buf != NULL)
149                          if (exec_cmd(buf) < 0)
150                                  break;
151          }
152 }
153