diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e1c7d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +05_timers/ diff --git a/01_git/scissors.c b/01_git/scissors.c new file mode 100644 index 0000000..b9cbd23 --- /dev/null +++ b/01_git/scissors.c @@ -0,0 +1,76 @@ +#include +#include +#include + +#define ROCK 'r' +#define PAPER 'p' +#define SCISSORS 's' + +const char arr[] = {ROCK, PAPER, SCISSORS}; + + +int start_game(char input_user, char *machine_choice_c){ + 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 == ROCK && arr[machine_choice_int] == SCISSORS) + { + result = 1; + } + else if (arr[machine_choice_int] == ROCK && input_user == SCISSORS) + { + result = 0; + } + else if (input_user == PAPER && arr[machine_choice_int] == ROCK) + { + result = 1; + } + else if (arr[machine_choice_int] == PAPER && input_user == ROCK) + { + result = 0; + } + else if (input_user == SCISSORS && arr[machine_choice_int] == PAPER) + { + result = 1; + } + else if (arr[machine_choice_int] == SCISSORS && input_user == PAPER) + { + result = 0; + } + else + { + result = -2; + } + + 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 if(result == 1) + { + printf("\nYou chose %c and I choose %c, You win !\n\n", input_from_user, machine_chose); + } + else + { + printf("\nYour chose :%c: is incorrect\n\n", input_from_user); + } +} 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 diff --git a/03_module/Makefile b/03_module/Makefile new file mode 100644 index 0000000..faf5aa2 --- /dev/null +++ b/03_module/Makefile @@ -0,0 +1,11 @@ +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 + +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); 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"); 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