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

fix: fix segfault when loading module and improve send_buffer size calculation

parent 9b986387
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,7 @@
#define CLASS_NAME "sibyl_class"
#define SLOTS 4
#define SLOT_INTERVAL_NS 1000000000
#define SLOT_INTERVAL_NS 100000000
#define MAX_MEASUREMENTS 16
......@@ -73,25 +73,33 @@ static uint32_t calculate_crc32(const packet_t *packet) {
static void prepare_for_sending(void) {
packet->crc = calculate_crc32(packet);
size_t n = 0;
secondary_send_buffer[0] = packet->magic;
secondary_send_buffer[1] = packet->data.sender_id;
secondary_send_buffer[2] = packet->data.count;
uint8_t *bytes = (uint8_t *)packet->data.measurements;
n += 3;
for (size_t a = 0; a < packet->data.count; a++) {
uint8_t *bytes = (uint8_t *)&(packet->data.measurements[a]);
for (size_t b = 0; b < sizeof(measurement_t); b++) {
secondary_send_buffer[n + b] = bytes[b];
}
for (size_t i = 0; i < sizeof(measurement_t) * packet->data.count; i++) {
secondary_send_buffer[3 + i] = bytes[i];
n += sizeof(measurement_t);
}
bytes = (uint8_t *)&packet->crc;
uint8_t *bytes = (uint8_t *)&packet->crc;
for (size_t i = 0; i < sizeof(uint32_t); i++) {
secondary_send_buffer[3 + sizeof(measurement_t) * packet->data.count + i] =
bytes[i];
secondary_send_buffer[n + i] = bytes[i];
}
secondary_send_buffer_size =
3 + sizeof(measurement_t) * packet->data.count + sizeof(uint32_t);
n += sizeof(uint32_t);
secondary_send_buffer_size = n;
// Swap buffers
mutex_lock(&send_buffer_mutex);
......@@ -220,8 +228,6 @@ static int __init lkm_init(void) {
return -EFAULT;
}
prepare_for_sending();
// Allocate memory for send buffers
primary_send_buffer = (uint8_t *)vmalloc(SEND_BUFFER_SIZE);
primary_send_buffer_size = 0;
......@@ -240,6 +246,8 @@ static int __init lkm_init(void) {
return -EFAULT;
}
prepare_for_sending();
if (alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME) < 0) {
vfree(primary_send_buffer);
vfree(secondary_send_buffer);
......@@ -325,6 +333,12 @@ static int __init lkm_init(void) {
}
static void __exit lkm_exit(void) {
if (timed_thread) {
kthread_stop(timed_thread);
}
hrtimer_cancel(&timer);
mutex_destroy(&send_buffer_mutex);
vfree(primary_send_buffer);
......@@ -339,12 +353,6 @@ static void __exit lkm_exit(void) {
gpio_set_value(GPIO_PIN, 0);
gpio_free(GPIO_PIN);
if (timed_thread) {
kthread_stop(timed_thread);
}
hrtimer_cancel(&timer);
}
module_init(lkm_init);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment