From b7e558f2716389737d3654350732a2ec0c77a41b Mon Sep 17 00:00:00 2001 From: Ostap Pokornyi Date: Fri, 10 Dec 2021 12:34:30 +0200 Subject: [PATCH 1/5] 01_git/:Implement Scissors game --- 01_git/scissors.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 01_git/scissors.c diff --git a/01_git/scissors.c b/01_git/scissors.c new file mode 100644 index 0000000..66e6853 --- /dev/null +++ b/01_git/scissors.c @@ -0,0 +1,68 @@ +#include +#include +#include + +const char arr[3] = {'r', 'p', 's'}; + +int machine_rand(void) { + srand(time(0)); + int random = rand() % 3; + return random; +} + +int start_game(char input_user, char *machine_choice_c){ + int machine_choice_int = machine_rand(); + *machine_choice_c = arr[machine_choice_int]; + int result; + if(input_user == arr[machine_choice_int]) + { + result = -1; + } + else if (input_user == 'r' && arr[machine_choice_int] == 's') + { + result = 1; + } + else if (arr[machine_choice_int] == 'r' && input_user == 's') + { + result = 0; + } + else if (input_user == 'p' && arr[machine_choice_int] == 'r') + { + result = 1; + } + else if (arr[machine_choice_int] == 'p' && input_user == 'r') + { + result = 0; + } + else if (input_user == 's' && arr[machine_choice_int] == 'p') + { + result = 1; + } + else if (arr[machine_choice_int] == 's' && input_user == 'p') + { + result = 0; + } + + return result; +} + + +int main () { + int result; + char input_from_user, machine_chose; + printf("Please choose: rock (r) - paper (p) - scissors (s)\n"); + scanf("%c", &input_from_user); + result = start_game(input_from_user, &machine_chose); + if(result == -1) + { + printf("\nYou chose %c and I choose %c, The Friendship wins ! \n\n", input_from_user, machine_chose); + } + else if (result == 0) + { + printf("\nYou chose %c and I choose %c, HAHAHAH Machine wins ! \n\n", input_from_user, machine_chose); + } + else + { + printf("\nYou chose %c and I choose %c, You win !\n\n", input_from_user, machine_chose); + } +} \ No newline at end of file From 8a05b552c9a8e6bee74cc478f1f3db2354a0894a Mon Sep 17 00:00:00 2001 From: opokornyi Date: Wed, 22 Dec 2021 23:17:02 +0200 Subject: [PATCH 2/5] 02_bash/:Implemented Hardware detect program --- 02_bash/hwdetect | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 02_bash/hwdetect diff --git a/02_bash/hwdetect b/02_bash/hwdetect new file mode 100644 index 0000000..8e6b8a5 --- /dev/null +++ b/02_bash/hwdetect @@ -0,0 +1,64 @@ +# delete temporary files +cleanup() { + rm udev.txt + rm udev_new.txt + rm i2cdev.txt + rm i2cdev_new.txt + echo "cleaning up..." + exit +} + +echo "Device conecting monitor" +echo "For exit press Ctrl-C" + +# detect usb devices +lsusb > udev.txt + +# detect i2c devices +i2cdetect -l >i2cdev.txt + +while true +do +# create new list of usb devices + lsusb > udev_new.txt +# create new list of i2c devices + i2cdetect -l >i2cdev_new.txt + +# look for differences in usb devices list + diffresult=$(diff udev.txt udev_new.txt) + size=$(wc -c < udev.txt) + size_new=$(wc -c < udev_new.txt) +# show differences + if [ $size -ne $size_new ] + then + if [ $size -gt $size_new ] + then + echo "Disconnected USB devices:" + else + echo "Connected USB devices:" + fi + echo $diffresult + mv -f udev_new.txt udev.txt + fi + +# look for differences in i2c devices list + diffresult=$(diff i2cdev.txt i2cdev_new.txt) + size=$(wc -c < i2cdev.txt) + size_new=$(wc -c < i2cdev_new.txt) +# show differences + if [ $size -ne $size_new ] + then + if [ $size -gt $size_new ] + then + echo "Disconnected i2c devices:" + else + echo "Connected i2c devices:" + fi + echo $diffresult + mv -f i2cdev_new.txt i2cdev.txt + fi + + sleep 1 +# check if Ctrl-C pressed and exit + trap 'cleanup' INT +done \ No newline at end of file From 67de2241f69af52b1f5e49fb0c4aeec7b4512ee9 Mon Sep 17 00:00:00 2001 From: Ostap Pokornyi Date: Mon, 13 Dec 2021 03:48:08 +0200 Subject: [PATCH 3/5] 03_module/: Implemented First Kernel Module --- .gitignore | 2 ++ 01_git/scissors.c | 40 ++++++++++++++++++++++++---------------- 03_module/Makefile | 10 ++++++++++ 03_module/hello.c | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 03_module/Makefile create mode 100644 03_module/hello.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55e594c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +remove +04_basic_struct diff --git a/01_git/scissors.c b/01_git/scissors.c index 66e6853..b9cbd23 100644 --- a/01_git/scissors.c +++ b/01_git/scissors.c @@ -1,47 +1,51 @@ #include #include -#include +#include -const char arr[3] = {'r', 'p', 's'}; +#define ROCK 'r' +#define PAPER 'p' +#define SCISSORS 's' + +const char arr[] = {ROCK, PAPER, SCISSORS}; -int machine_rand(void) { - srand(time(0)); - int random = rand() % 3; - return random; -} int start_game(char input_user, char *machine_choice_c){ - int machine_choice_int = machine_rand(); + srand(time(0)); + int machine_choice_int = rand() % 3; *machine_choice_c = arr[machine_choice_int]; int result; if(input_user == arr[machine_choice_int]) { result = -1; } - else if (input_user == 'r' && arr[machine_choice_int] == 's') + else if (input_user == ROCK && arr[machine_choice_int] == SCISSORS) { result = 1; } - else if (arr[machine_choice_int] == 'r' && input_user == 's') + else if (arr[machine_choice_int] == ROCK && input_user == SCISSORS) { result = 0; } - else if (input_user == 'p' && arr[machine_choice_int] == 'r') + else if (input_user == PAPER && arr[machine_choice_int] == ROCK) { result = 1; } - else if (arr[machine_choice_int] == 'p' && input_user == 'r') + else if (arr[machine_choice_int] == PAPER && input_user == ROCK) { result = 0; } - else if (input_user == 's' && arr[machine_choice_int] == 'p') + else if (input_user == SCISSORS && arr[machine_choice_int] == PAPER) { result = 1; } - else if (arr[machine_choice_int] == 's' && input_user == 'p') + else if (arr[machine_choice_int] == SCISSORS && input_user == PAPER) { result = 0; } + else + { + result = -2; + } return result; } @@ -61,8 +65,12 @@ int main () { { printf("\nYou chose %c and I choose %c, HAHAHAH Machine wins ! \n\n", input_from_user, machine_chose); } - else + else if(result == 1) { printf("\nYou chose %c and I choose %c, You win !\n\n", input_from_user, machine_chose); } -} \ No newline at end of file + else + { + printf("\nYour chose :%c: is incorrect\n\n", input_from_user); + } +} diff --git a/03_module/Makefile b/03_module/Makefile new file mode 100644 index 0000000..2e28035 --- /dev/null +++ b/03_module/Makefile @@ -0,0 +1,10 @@ +KERNELDIR ?= /home/oporoknyi/Desktop/build/buildroot-2021.02.7/output/build/linux-5.10.7 #WARNING relative path + +obj-m := hello.o + +all: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(PWD) clean + diff --git a/03_module/hello.c b/03_module/hello.c new file mode 100644 index 0000000..ad782ce --- /dev/null +++ b/03_module/hello.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +static int a = 0; +static int b = 0; + +module_param(a, int, 0660); +module_param(b, int, 0660); + +int hello_init(void) +{ + printk(KERN_WARNING "\na + b = %i\n", a + b); + return 0; +} + +void hello_exit(void) +{ + printk(KERN_WARNING "\na - b = %i\n", a - b); + +} + +MODULE_LICENSE("GPL"); + +module_init(hello_init); +module_exit(hello_exit); From 1d24967069e5168d5f0b7112367012453387deee Mon Sep 17 00:00:00 2001 From: opokornyi Date: Mon, 20 Dec 2021 13:33:13 +0200 Subject: [PATCH 4/5] 04_basic_struct: Implemented Kernel Module with object named "hello" --- .gitignore | 3 +- 03_module/Makefile | 1 + 04_basic_struct/Makefile | 12 +++++++ 04_basic_struct/kobj.c | 75 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 04_basic_struct/Makefile create mode 100644 04_basic_struct/kobj.c diff --git a/.gitignore b/.gitignore index 55e594c..2e1c7d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -remove -04_basic_struct +05_timers/ diff --git a/03_module/Makefile b/03_module/Makefile index 2e28035..faf5aa2 100644 --- a/03_module/Makefile +++ b/03_module/Makefile @@ -1,6 +1,7 @@ KERNELDIR ?= /home/oporoknyi/Desktop/build/buildroot-2021.02.7/output/build/linux-5.10.7 #WARNING relative path obj-m := hello.o +.PHONY: clean all: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules diff --git a/04_basic_struct/Makefile b/04_basic_struct/Makefile new file mode 100644 index 0000000..3797d23 --- /dev/null +++ b/04_basic_struct/Makefile @@ -0,0 +1,12 @@ +KERNELDIR ?= /home/oporoknyi/Desktop/build/buildroot-2021.02.7/output/build/linux-5.10.7 #WARNING relative path + +obj-m := kobj.o + +.PHONY: clean + +all: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(PWD) clean + diff --git a/04_basic_struct/kobj.c b/04_basic_struct/kobj.c new file mode 100644 index 0000000..d92a580 --- /dev/null +++ b/04_basic_struct/kobj.c @@ -0,0 +1,75 @@ +#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME + +#include // Core header for loading LKMs into the kernel +#include +#include +#include +#include +#include +#include + +static struct msg_obj { + struct list_head list; + char *data; +}; + +struct list_head head; + +static ssize_t kobj_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf) +{ + struct msg_obj *curr_msg; + struct list_head *listptr; + size_t len = 0; + list_for_each(listptr, &head) { + curr_msg = list_entry(listptr, struct msg_obj, list); + strcpy(buf+len, curr_msg->data); + len += strlen(curr_msg->data); + } + + return len; +} + +static ssize_t kobj_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t count) +{ + struct msg_obj *new_data = kmalloc(sizeof(struct msg_obj), GFP_KERNEL); + new_data->data = kmalloc(sizeof(char)*count, GFP_KERNEL); + strcpy(new_data->data, buf); + list_add_tail(&new_data->list, &head); + return count; +} + +static struct kobj_attribute list_attribute = + __ATTR(param, 0664, kobj_show, kobj_store); + +static struct kobject *hello_kobj; + +static int hello_init(void) +{ + int res = 0; + INIT_LIST_HEAD(&head); + hello_kobj = kobject_create_and_add("hello", kernel_kobj); + if (!hello_kobj) + return -ENOMEM; + + res = sysfs_create_file(hello_kobj, &list_attribute.attr); + if (res) + kobject_put(hello_kobj); + + return res; +} + +static void hello_exit(void) +{ + kobject_put(hello_kobj); + pr_info("module exited\n"); +} + +module_init(hello_init); +module_exit(hello_exit); + +MODULE_AUTHOR("Ostap Pokornyi "); +MODULE_DESCRIPTION("Basic data structures module"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1"); From 9b90355fbb601cf993d74a05b6927637f3b7ca98 Mon Sep 17 00:00:00 2001 From: opokornyi Date: Sun, 26 Dec 2021 23:29:56 +0200 Subject: [PATCH 5/5] 05_timers/:Implemented module which returns relative time since previous read of it, userspace application which returns absolute time in user space --- 05_timers/App/main.c | 39 +++++++++++++ 05_timers/Kern_Module/Makefile | 10 ++++ 05_timers/Kern_Module/time_obj.c | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 05_timers/App/main.c create mode 100644 05_timers/Kern_Module/Makefile create mode 100644 05_timers/Kern_Module/time_obj.c diff --git a/05_timers/App/main.c b/05_timers/App/main.c new file mode 100644 index 0000000..6a970fd --- /dev/null +++ b/05_timers/App/main.c @@ -0,0 +1,39 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + struct timespec ts, ts_loc; + struct tm *loctime; + + printf("\n Absolute time Local time\n"); + printf(" tv_nsec tv_sec date time\n"); + //switch off cursor + printf("\033[?25l"); + + while (1) { + clock_gettime(CLOCK_REALTIME, &ts); + // clock_gettime(CLOCK_MONOTONIC, &ts); + //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + //clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); + printf("%10ld %10ld ", ts.tv_nsec, ts.tv_sec); + + clock_gettime(CLOCK_REALTIME, &ts_loc); + loctime = localtime((time_t *)&ts_loc.tv_sec); + + printf("%02d-%02d-%04d %02d:%02d:%02d", + loctime->tm_mday, + loctime->tm_mon+1, + loctime->tm_year+1900, + loctime->tm_hour, + loctime->tm_min, + loctime->tm_sec); + + usleep(1000); + printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/05_timers/Kern_Module/Makefile b/05_timers/Kern_Module/Makefile new file mode 100644 index 0000000..5d0386e --- /dev/null +++ b/05_timers/Kern_Module/Makefile @@ -0,0 +1,10 @@ +KERNELDIR ?= /home/oporoknyi/Desktop/build/buildroot-2021.02.7/output/build/linux-5.10.7 #WARNING relative path + +obj-m := timer_mod.o + +all: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(PWD) clean + diff --git a/05_timers/Kern_Module/time_obj.c b/05_timers/Kern_Module/time_obj.c new file mode 100644 index 0000000..a6db3ba --- /dev/null +++ b/05_timers/Kern_Module/time_obj.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static ssize_t time_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf) +{ + static cycles_t prev_tick; + static ktime_t prev_time; + + cycles_t curr_tick; + ktime_t curr_time; + + size_t wr_len; + + struct timespec64 ts; + struct tm dt; + + curr_tick = get_cycles(); + curr_time = ktime_get_real(); + + if(prev_tick == 0) + { + wr_len = sprintf(buf, "\nFirst Reading\n"); + } + else + { + ts = ns_to_timespec64(prev_time); + time64_to_tm(ts.tv_sec, 0, &dt); + + wr_len = sprintf(buf+wr_len, "\nPrev Reading was at %02d-%02d-%02ld %02d:%02d:%02d.%09ld\n", + dt.tm_mday, + dt.tm_mon+1, + dt.tm_year+1990, + dt.tm_hour, + dt.tm_min, + dt.tm_sec, + ts.tv_nsec); + } + + prev_tick = curr_tick; + prev_time = curr_time; + + ts = ns_to_timespec64(curr_time); + time64_to_tm(ts.tv_sec, 0, &dt); + + wr_len += sprintf(buf+wr_len, "Current time stamp %02d-%02d-%02ld %02d:%02d:%02d.%09ld\n", + dt.tm_mday, + dt.tm_mon+1, + dt.tm_year+1900, + dt.tm_hour, + dt.tm_min, + dt.tm_sec, + ts.tv_nsec); + + return wr_len; +} + +static struct kobj_attribute list_attribute = + __ATTR(GetTime, 0664, time_show, NULL); + +static struct kobject *time_kobj; + +static int hello_init(void){ + int res = 0; + printk(KERN_WARNING "\nWe are loaded\n"); + time_kobj = kobject_create_and_add("Time", kernel_kobj); + if (!time_kobj) + return -ENOMEM; + + res = sysfs_create_file(time_kobj, &list_attribute.attr); + if(res) + kobject_put(time_kobj); + + return res; +} + +static void hello_exit(void){ + kobject_put(time_kobj); + pr_info("module exited\n"); +} + +module_init(hello_init); +module_exit(hello_exit); + +MODULE_AUTHOR("Ostap Pokornyi "); +MODULE_DESCRIPTION("Basic data structures module"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1"); \ No newline at end of file