Skip to content
Snippets Groups Projects
Commit e7e1aa6b authored by David Maul's avatar David Maul :crab:
Browse files

feat: buffer messages in userspace and improve message handling in kernel module

parent d4413e6a
No related branches found
No related tags found
No related merge requests found
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include "../common/include/data.h"
#include "../common/include/message.h"
#include <linux/cdev.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <linux/crc32.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/hrtimer.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/crc32.h>
#include "../common/include/message.h"
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#define DEVICE_NAME "amogus"
#define CLASS_NAME "amogus_class"
#define SLOTS 4
#define SLOT_INTERVAL_NS 250000
#define SLOT_INTERVAL_NS 1000000
#define GPIO_PIN 12
#define MAX_VALUES 16
#define GPIO_PIN 575
static int major_number;
static struct class *class = NULL;
static struct cdev mycdev;
static message_t* message;
typedef struct packet {
data_t data;
uint32_t crc;
} packet_t;
static packet_t *packet;
static struct hrtimer timer;
static ktime_t interval;
......@@ -35,43 +43,55 @@ static atomic_t wake_counter = ATOMIC_INIT(0);
static int slot = 0;
typedef struct packet {
message_t data;
uint32_t crc;
} packet_t;
static int device_open(struct inode *inode, struct file *file) { return 0; }
static int device_open(struct inode *inode, struct file *file)
{
return 0;
}
static int device_release(struct inode *inode, struct file *file) { return 0; }
static int device_release(struct inode *inode, struct file *file)
{
return 0;
static uint32_t calculate_crc32(const data_t *data) {
uint32_t crc = crc32(0, (void *)&data->sensor_id, sizeof(uint8_t));
crc = crc32(crc, (void *)&data->count, sizeof(uint8_t));
crc = crc32(crc, (void *)&data->values, sizeof(message_t) * data->count);
return crc;
}
static ssize_t device_write(struct file *filp, const char *input,
size_t length, loff_t *offset)
{
if(length != sizeof(message_t)) {
static ssize_t device_write(struct file *filp, const char *input, size_t length,
loff_t *offset) {
if (length != sizeof(data_t)) {
printk(KERN_ALERT "lkm: Tried to write more bytes than allowed\n");
return -EFAULT;
}
if (copy_from_user((void*)message, input, length)) {
if (((data_t *)input)->count >= MAX_VALUES) {
printk(KERN_ALERT "lkm: Tried to write more messages than allowed\n");
return -EFAULT;
}
if (copy_from_user((void *)&(packet->data.sensor_id),
(void *)&(((data_t *)input)->sensor_id),
sizeof(uint8_t))) {
return -EFAULT;
}
if (copy_from_user((void *)&(packet->data.count),
(void *)&(((data_t *)input)->count), sizeof(uint8_t))) {
return -EFAULT;
}
if (copy_from_user((void *)packet->data.values,
(void *)((data_t *)input)->values,
sizeof(message_t) * packet->data.count)) {
return -EFAULT;
}
packet->crc = calculate_crc32(&packet->data);
printk(KERN_INFO "lkm: received write of size %zu\n", length);
return length;
}
static const struct file_operations fops = {
.write = device_write,
.open = device_open,
.release = device_release
};
.write = device_write, .open = device_open, .release = device_release};
static enum hrtimer_restart timer_callback(struct hrtimer *timer) {
atomic_inc(&wake_counter);
......@@ -97,16 +117,21 @@ static void send_byte(uint8_t byte) {
send_bit(1);
}
static void send_data(const void *data, size_t length) {
const uint8_t *bytes = (const uint8_t*)data;
static void send_data(const packet_t *packet) {
send_byte(packet->data.sensor_id);
send_byte(packet->data.count);
uint8_t *bytes = (uint8_t *)packet->data.values;
for (size_t i = 0; i < length; i++) {
for (size_t i = 0; i < sizeof(message_t) * packet->data.count; i++) {
send_byte(bytes[i]);
}
}
static uint32_t calculate_crc32(const void *data, size_t length){
return crc32(0, data, length);
bytes = (uint8_t *)&packet->crc;
for (size_t i = 0; i < sizeof(uint32_t); i++) {
send_byte(bytes[i]);
}
}
static int timed_thread_fn(void *args) {
......@@ -115,12 +140,7 @@ static int timed_thread_fn(void *args) {
atomic_dec(&wake_counter);
if (slot == 0) {
packet_t packet;
packet.data = *message;
packet.crc = calculate_crc32(&packet.data, sizeof(&packet.data));
send_data(&packet, sizeof(packet));
send_data(packet);
}
slot = (slot + 1) % SLOTS;
......@@ -129,19 +149,29 @@ static int timed_thread_fn(void *args) {
return 0;
}
static int __init lkm_init(void)
{
static int __init lkm_init(void) {
dev_t dev;
message = (message_t*)vmalloc(sizeof(message_t));
packet = (packet_t *)vmalloc(sizeof(packet_t));
if(message == NULL) {
return -1;
if (packet == NULL) {
return -EFAULT;
}
packet->data.sensor_id = 0x7C;
packet->data.count = 0;
packet->data.values = (message_t *)vmalloc(sizeof(message_t) * MAX_VALUES);
if (packet->data.values == NULL) {
vfree(packet);
return -EFAULT;
}
// Dynamically allocate major number
if (alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME) < 0) {
vfree(message);
vfree(packet->data.values);
vfree(packet);
printk(KERN_ALERT "Failed to allocate major number\n");
return -1;
}
......@@ -149,14 +179,17 @@ static int __init lkm_init(void)
class = class_create(CLASS_NAME);
if (IS_ERR(class)) {
vfree(message);
vfree(packet->data.values);
vfree(packet);
unregister_chrdev_region(MKDEV(major_number, 0), 1);
printk(KERN_ALERT "Failed to create device class\n");
return PTR_ERR(class);
}
if (device_create(class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME) == NULL) {
vfree(message);
if (device_create(class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME) ==
NULL) {
vfree(packet->data.values);
vfree(packet);
class_destroy(class);
unregister_chrdev_region(MKDEV(major_number, 0), 1);
printk(KERN_ALERT "Failed to create device\n");
......@@ -165,7 +198,8 @@ static int __init lkm_init(void)
cdev_init(&mycdev, &fops);
if (cdev_add(&mycdev, MKDEV(major_number, 0), 1) < 0) {
vfree(message);
vfree(packet->data.values);
vfree(packet);
device_destroy(class, MKDEV(major_number, 0));
class_destroy(class);
unregister_chrdev_region(MKDEV(major_number, 0), 1);
......@@ -176,7 +210,8 @@ static int __init lkm_init(void)
printk(KERN_INFO "lkm: device created successfully\n");
if (gpio_request(GPIO_PIN, "GPIO_PIN") < 0) {
vfree(message);
vfree(packet->data.values);
vfree(packet);
cdev_del(&mycdev);
device_destroy(class, MKDEV(major_number, 0));
class_destroy(class);
......@@ -188,7 +223,8 @@ static int __init lkm_init(void)
timed_thread = kthread_run(timed_thread_fn, NULL, "timed_thread");
if (IS_ERR(timed_thread)) {
vfree(message);
vfree(packet->data.values);
vfree(packet);
cdev_del(&mycdev);
device_destroy(class, MKDEV(major_number, 0));
class_destroy(class);
......@@ -205,9 +241,9 @@ static int __init lkm_init(void)
return 0;
}
static void __exit lkm_exit(void)
{
vfree(message);
static void __exit lkm_exit(void) {
vfree(packet->data.values);
vfree(packet);
cdev_del(&mycdev);
device_destroy(class, MKDEV(major_number, 0));
class_destroy(class);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment