Skip to content

Commit baed53d

Browse files
committed
Implement user space tool to achieve interactivity
In order to interact with kmldrv device driver, we need an userspace tool which can performs different operations to "/dev/kmldrv". We implement kmldrv-user , currently the tool supports displaying the status of the driver and display the gaming process in kernel space.
1 parent 8cba362 commit baed53d

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ TARGET = kmldrv
22
kmldrv-objs = simrupt.o game.o wyhash.o mcts.o negamax.o zobrist.o
33
obj-m := $(TARGET).o
44

5+
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
56
KDIR ?= /lib/modules/$(shell uname -r)/build
67
PWD := $(shell pwd)
78

89
GIT_HOOKS := .git/hooks/applied
9-
all: $(GIT_HOOKS) simrupt.c
10+
all: kmod kmldrv-user
11+
12+
kmod: $(GIT_HOOKS) simrupt.c
1013
$(MAKE) -C $(KDIR) M=$(PWD) modules
1114

15+
kmldrv-user: kmldrv-user.c
16+
$(CC) $(ccflags-y) -o $@ $<
17+
1218
$(GIT_HOOKS):
1319
@scripts/install-git-hooks
1420
@echo
1521

1622

1723
clean:
1824
$(MAKE) -C $(KDIR) M=$(PWD) clean
25+
$(RM) kmldrv-user

kmldrv-user.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <getopt.h>
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
8+
#include "game.h"
9+
10+
#define KMLDRV_STATUS_FILE "/sys/module/kmldrv/initstate"
11+
#define KMLDRV_DEVICE_FILE "/dev/kmldrv"
12+
13+
bool kmldrv_status_check(void)
14+
{
15+
FILE *fp = fopen(KMLDRV_STATUS_FILE, "r");
16+
if (!fp) {
17+
printf("kmldrv status : not loaded\n");
18+
return false;
19+
}
20+
21+
char read_buf[20];
22+
fgets(read_buf, 20, fp);
23+
read_buf[strcspn(read_buf, "\n")] = 0;
24+
if (!strcmp("live", read_buf))
25+
printf("kmldrv status : live\n");
26+
else {
27+
printf("kmldrv status : %s\n", read_buf);
28+
fclose(fp);
29+
return false;
30+
}
31+
fclose(fp);
32+
return true;
33+
}
34+
35+
36+
int main(int argc, char *argv[])
37+
{
38+
int c;
39+
40+
while ((c = getopt(argc, argv, "d:s:ch")) != -1) {
41+
switch (c) {
42+
case 'h':
43+
printf(
44+
"kmldrv-user : A userspace tool which supports interactions "
45+
"with kmldrv from user-level\n");
46+
printf("Usage:\n\n");
47+
printf("\t./kmldrv-user [arguments]\n\n");
48+
printf("Arguments:\n\n");
49+
printf("\t--start - start a tic-tac-toe game\n");
50+
printf("\t--release - release kmldrv\n\n");
51+
printf("Control Options:\n\n");
52+
printf("\t Ctrl + S - Start a tic-tac-toe game\n");
53+
printf("\t Ctrl + P - Pause to show the game\n");
54+
printf("\t Ctrl + C - Continue to show the game\n");
55+
printf("\t Ctrl + R - Restart a tic-tac-toe game\n");
56+
return 0;
57+
default:
58+
printf("Invalid arguments\n");
59+
break;
60+
}
61+
}
62+
63+
if (!kmldrv_status_check())
64+
exit(1);
65+
66+
FILE *device_ptr = fopen(KMLDRV_DEVICE_FILE, "r");
67+
char display_buf[DRAWBUFFER_SIZE];
68+
while (fgets(display_buf, DRAWBUFFER_SIZE, device_ptr)) {
69+
printf("%s", display_buf);
70+
}
71+
72+
fclose(device_ptr);
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)