Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opokornyi #126

Open
wants to merge 5 commits into
base: ostap.pokornyi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
05_timers/
76 changes: 76 additions & 0 deletions 01_git/scissors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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;
ziod marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
ziod marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
64 changes: 64 additions & 0 deletions 02_bash/hwdetect
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# delete temporary files
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#/bin/bash

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline

11 changes: 11 additions & 0 deletions 03_module/Makefile
Original file line number Diff line number Diff line change
@@ -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

27 changes: 27 additions & 0 deletions 03_module/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>

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);
12 changes: 12 additions & 0 deletions 04_basic_struct/Makefile
Original file line number Diff line number Diff line change
@@ -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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHONY

.PHONY: clean

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

75 changes: 75 additions & 0 deletions 04_basic_struct/kobj.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME

#include <linux/module.h> // Core header for loading LKMs into the kernel
#include <linux/kobject.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/slab.h>

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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strlen(buf)

}

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 <[email protected]>");
MODULE_DESCRIPTION("Basic data structures module");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
39 changes: 39 additions & 0 deletions 05_timers/App/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <time.h>
#include <unistd.h>

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not use endless loop

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;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline

10 changes: 10 additions & 0 deletions 05_timers/Kern_Module/Makefile
Original file line number Diff line number Diff line change
@@ -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

Loading