From cd393a04244181e73599b24ac38e70778501c120 Mon Sep 17 00:00:00 2001 From: Robert Gustafsson Date: Tue, 3 Oct 2017 09:14:55 +0200 Subject: Some refactoring --- broccoli/Makefile | 31 +++++------ broccoli/broevent.c | 62 --------------------- broccoli/fifoqueue.c | 111 ------------------------------------- broccoli/includes/broevent.h | 7 +-- broccoli/includes/fifoqueue.h | 5 +- broccoli/midbropasad.c | 28 ---------- broccoli/modbus.bro | 26 --------- broccoli/script/modbus.bro | 26 +++++++++ broccoli/src/broevent.c | 65 ++++++++++++++++++++++ broccoli/src/fifoqueue.c | 114 ++++++++++++++++++++++++++++++++++++++ broccoli/src/midbropasad.c | 33 +++++++++++ broccoli/test/tests.c | 126 ++++++++++++++++++++++++++++++++++++++++++ broccoli/tests.c | 124 ----------------------------------------- 13 files changed, 384 insertions(+), 374 deletions(-) delete mode 100644 broccoli/broevent.c delete mode 100644 broccoli/fifoqueue.c delete mode 100644 broccoli/midbropasad.c delete mode 100644 broccoli/modbus.bro create mode 100644 broccoli/script/modbus.bro create mode 100644 broccoli/src/broevent.c create mode 100644 broccoli/src/fifoqueue.c create mode 100644 broccoli/src/midbropasad.c create mode 100644 broccoli/test/tests.c delete mode 100644 broccoli/tests.c diff --git a/broccoli/Makefile b/broccoli/Makefile index 329c60c..ea5465b 100644 --- a/broccoli/Makefile +++ b/broccoli/Makefile @@ -1,26 +1,23 @@ CC=gcc -CFLAGS = -c -Wall -I/usr/local/include -I/usr/local/include -DBROCCOLI +CFLAGS =-c -DBROCCOLI +INC =-I/usr/local/include -I/usr/local/include -I./includes LDFLAGS = -L/usr/local/lib -lbroccoli -pthread +SRC = midbropasad.c broevent.c fifoqueue.c +OBJ = $(patsubst %.c, build/%.o, $(SRC)) -all: midbropasad tests +all: bin/midbropasad bin/tests -midbropasad: midbropasad.o broevent.o fifoqueue.o - $(CC) midbropasad.o broevent.o fifoqueue.o -o midbropasad $(LDFLAGS) +bin/midbropasad: $(OBJ) + $(CC) $^ -o bin/midbropasad $(LDFLAGS) -tests: tests.o fifoqueue.o - $(CC) tests.o fifoqueue.o -o tests +build/%.o: src/%.c + $(CC) $(CFLAGS) $(INC) $< -o $@ -midbropasad.o: midbropasad.c - $(CC) $(CFLAGS) midbropasad.c +bin/tests: build/fifoqueue.o build/tests.o + $(CC) $^ -o bin/tests $(LDFLAGS) -broevent.o: broevent.c - $(CC) $(CFLAGS) broevent.c - -fifoqueue.o: fifoqueue.c - $(CC) -c -Wall fifoqueue.c - -tests.o: tests.c - $(CC) -c -Wall tests.c +build/tests.o: test/tests.c + $(CC) $(CFLAGS) $(INC) $< -o $@ clean: - rm *.o midbropasad tests + rm build/* bin/* diff --git a/broccoli/broevent.c b/broccoli/broevent.c deleted file mode 100644 index 2b3db8d..0000000 --- a/broccoli/broevent.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "includes/fifoqueue.h" -#include "includes/broevent.h" - -char *host_default = "127.0.0.1"; -char *port_default = "47760"; -Fifo_q * q; - - static void -bro_response(BroConn *conn, void *data, uint64* registers, uint64* uid) -{ - add_to_queue(q,create_sensor_object(*registers,*uid)); - //printf("Received value %"PRIu64" from uid=%"PRIu64"\n",*registers,*uid); - - conn = NULL; - data = NULL; -} - - void * -bro_event_listener(void * args) -{ - q = (Fifo_q *) args; - int fd = -1; - BroConn *bc = NULL; - bro_init(NULL); - char hostname[512]; - - snprintf(hostname, 512, "%s:%s", host_default, port_default); - if (! (bc = bro_conn_new_str(hostname, BRO_CFLAG_RECONNECT | BRO_CFLAG_ALWAYS_QUEUE))) - { - printf("Could not get Bro connection handle.\n"); - exit(-1); - } - bro_debug_calltrace = 0; - bro_debug_messages = 0; - - bro_event_registry_add(bc, "response",(BroEventFunc) bro_response, NULL); - - if (! bro_conn_connect(bc)) - { - printf("Could not connect to Bro at %s:%s.\n", host_default, - port_default); - exit(-1); - } - - fd =bro_conn_get_fd(bc); - fd_set rfds; - setbuf(stdout,NULL); - - while(true) - { - FD_ZERO(&rfds); - FD_SET(fd,&rfds); - if(select(fd+1,&rfds,NULL,NULL,NULL) == -1){ - perror("select()"); - break; - } - - bro_conn_process_input(bc); - } - - bro_conn_delete(bc); -} diff --git a/broccoli/fifoqueue.c b/broccoli/fifoqueue.c deleted file mode 100644 index e8a1525..0000000 --- a/broccoli/fifoqueue.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "includes/fifoqueue.h" -#include - -pthread_mutex_t lock; -pthread_mutex_t bufferEmptyBlock; - - Fifo_q * -init_queue(int size) -{ - Fifo_q * q = (Fifo_q *) malloc(sizeof(Fifo_q)); - q->head = NULL; - q->tail = NULL; - q->maxSize = size; - q->currentSize = 0; - if (pthread_mutex_init(&lock, NULL) != 0) - { - printf("WARNING: Couldn't initialize lock\n"); - } - if (pthread_mutex_init(&bufferEmptyBlock, NULL) != 0) - { - printf("WARNING: Couldn't initialize blocking lock\n"); - } - pthread_mutex_lock(&bufferEmptyBlock); - return q; -} - - boolean -is_full(Fifo_q * q) -{ - if(q->currentSize < q->maxSize) - return false; - else - return true; -} - - boolean -is_empty(Fifo_q * q) -{ - if(q->head==NULL) - return true; - else - return false; -} - - int -add_to_queue(Fifo_q * q, Sensor_t * sensor) -{ - - pthread_mutex_lock(&lock); - /* TODO delete first one if full */ - if(q == NULL){ - return -1; - } - else if(is_full(q)){ - return -1; - } - Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t)); - new_elem->next = NULL; - new_elem->sensor = sensor; - if(is_empty(q)){ - q->head = new_elem; - pthread_mutex_unlock(&bufferEmptyBlock); - }else - q->tail->next = new_elem; - q->tail = new_elem; - q->currentSize++; - pthread_mutex_unlock(&lock); - return 1; -} - - Sensor_t * -pop_from_queue(Fifo_q * q) -{ - - if(is_empty(q)){ - perror("The queue is empty"); - pthread_mutex_lock(&bufferEmptyBlock); - } - pthread_mutex_lock(&lock); - Queue_t * head = q->head; - q->head = head->next; - Sensor_t * sensor = head->sensor; - free(head); - q->currentSize--; - pthread_mutex_unlock(&lock); - return sensor; -} - - Sensor_t * -create_sensor_object(int value, int uid){ - Sensor_t * sensor = (Sensor_t *) malloc(sizeof(Sensor_t)); - sensor->value = value; - sensor->uid = uid; - return sensor; -} - void -print_queue(Fifo_q * q) -{ - pthread_mutex_lock(&lock); - Queue_t * current = q->head; - if(current == NULL){ - printf("The queue is empty!"); - return; - } - while(current != NULL){ - printf("sensor value=%d, sensor uid=%d\n", - current->sensor->value, current->sensor->uid); - current = current->next; - } - pthread_mutex_unlock(&lock); -} diff --git a/broccoli/includes/broevent.h b/broccoli/includes/broevent.h index 653796e..5e34f5c 100644 --- a/broccoli/includes/broevent.h +++ b/broccoli/includes/broevent.h @@ -1,5 +1,4 @@ -#ifdef BROCCOLI -#include -#endif - +#ifndef BROEVENT_H +#define BROEVENT_H void * bro_event_listener(void * args); +#endif diff --git a/broccoli/includes/fifoqueue.h b/broccoli/includes/fifoqueue.h index 398e1e9..75b09c4 100644 --- a/broccoli/includes/fifoqueue.h +++ b/broccoli/includes/fifoqueue.h @@ -1,5 +1,5 @@ -#include -#include +#ifndef FIFOQUEUE_H +#define FIFOQUEUE_H #include "types.h" Fifo_q * init_queue(int size); @@ -15,3 +15,4 @@ int add_to_queue(Fifo_q * q, Sensor_t * sensor); Sensor_t * pop_from_queue(Fifo_q * q); void print_queue(Fifo_q * q); +#endif diff --git a/broccoli/midbropasad.c b/broccoli/midbropasad.c deleted file mode 100644 index 70a290f..0000000 --- a/broccoli/midbropasad.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "includes/fifoqueue.h" -#include "includes/broevent.h" - - void -start_data_capture(Fifo_q * q) -{ - int res; - pthread_t event_listener; - res = pthread_create(&event_listener, NULL, bro_event_listener, q); - if(res){ - perror("Unable to create thread"); - exit(-1); - } -} - - int -main(int argc, char **argv) -{ - Fifo_q * q = init_queue(50); - start_data_capture(q); - while(true){ - printf("Main thread\n"); - sleep(10); - print_queue(q); - } - free(q); - return 0; -} diff --git a/broccoli/modbus.bro b/broccoli/modbus.bro deleted file mode 100644 index a2caed9..0000000 --- a/broccoli/modbus.bro +++ /dev/null @@ -1,26 +0,0 @@ -@load frameworks/communication/listen -module Pasad; - -redef Communication::listen_port = 47760/tcp; - -redef Communication::listen_ssl = F; - -global response: event(register: count, uid: count); - -redef Communication::nodes += { - ["broevent"] = [$host = 127.0.0.1, $events = /broevent/, $connect=F, $ssl=F] -}; - -event modbus_read_holding_registers_request(c: connection, headers: ModbusHeaders, start_adress: count, quantity: count) - -{ - print fmt("Request: %d", quantity); -} - -event modbus_read_holding_registers_response(c: connection, headers: ModbusHeaders, registers: ModbusRegisters) - -{ - print fmt("Response: %d", registers[0]); - event response(registers[0],headers$uid); -} - diff --git a/broccoli/script/modbus.bro b/broccoli/script/modbus.bro new file mode 100644 index 0000000..a2caed9 --- /dev/null +++ b/broccoli/script/modbus.bro @@ -0,0 +1,26 @@ +@load frameworks/communication/listen +module Pasad; + +redef Communication::listen_port = 47760/tcp; + +redef Communication::listen_ssl = F; + +global response: event(register: count, uid: count); + +redef Communication::nodes += { + ["broevent"] = [$host = 127.0.0.1, $events = /broevent/, $connect=F, $ssl=F] +}; + +event modbus_read_holding_registers_request(c: connection, headers: ModbusHeaders, start_adress: count, quantity: count) + +{ + print fmt("Request: %d", quantity); +} + +event modbus_read_holding_registers_response(c: connection, headers: ModbusHeaders, registers: ModbusRegisters) + +{ + print fmt("Response: %d", registers[0]); + event response(registers[0],headers$uid); +} + diff --git a/broccoli/src/broevent.c b/broccoli/src/broevent.c new file mode 100644 index 0000000..3813270 --- /dev/null +++ b/broccoli/src/broevent.c @@ -0,0 +1,65 @@ +#include "fifoqueue.h" +#include "broevent.h" +#ifdef BROCCOLI +#include +#endif + +char *host_default = "127.0.0.1"; +char *port_default = "47760"; +Fifo_q * q; + + static void +bro_response(BroConn *conn, void *data, uint64* registers, uint64* uid) +{ + add_to_queue(q,create_sensor_object(*registers,*uid)); + //printf("Received value %"PRIu64" from uid=%"PRIu64"\n",*registers,*uid); + + conn = NULL; + data = NULL; +} + + void * +bro_event_listener(void * args) +{ + q = (Fifo_q *) args; + int fd = -1; + BroConn *bc = NULL; + bro_init(NULL); + char hostname[512]; + + snprintf(hostname, 512, "%s:%s", host_default, port_default); + if (! (bc = bro_conn_new_str(hostname, BRO_CFLAG_RECONNECT | BRO_CFLAG_ALWAYS_QUEUE))) + { + printf("Could not get Bro connection handle.\n"); + exit(-1); + } + bro_debug_calltrace = 0; + bro_debug_messages = 0; + + bro_event_registry_add(bc, "response",(BroEventFunc) bro_response, NULL); + + if (! bro_conn_connect(bc)) + { + printf("Could not connect to Bro at %s:%s.\n", host_default, + port_default); + exit(-1); + } + + fd =bro_conn_get_fd(bc); + fd_set rfds; + setbuf(stdout,NULL); + + while(true) + { + FD_ZERO(&rfds); + FD_SET(fd,&rfds); + if(select(fd+1,&rfds,NULL,NULL,NULL) == -1){ + perror("select()"); + break; + } + + bro_conn_process_input(bc); + } + + bro_conn_delete(bc); +} diff --git a/broccoli/src/fifoqueue.c b/broccoli/src/fifoqueue.c new file mode 100644 index 0000000..9b972e7 --- /dev/null +++ b/broccoli/src/fifoqueue.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include "types.h" +#include "fifoqueue.h" + +pthread_mutex_t lock; +pthread_mutex_t bufferEmptyBlock; + + Fifo_q * +init_queue(int size) +{ + Fifo_q * q = (Fifo_q *) malloc(sizeof(Fifo_q)); + q->head = NULL; + q->tail = NULL; + q->maxSize = size; + q->currentSize = 0; + if (pthread_mutex_init(&lock, NULL) != 0) + { + printf("WARNING: Couldn't initialize lock\n"); + } + if (pthread_mutex_init(&bufferEmptyBlock, NULL) != 0) + { + printf("WARNING: Couldn't initialize blocking lock\n"); + } + pthread_mutex_lock(&bufferEmptyBlock); + return q; +} + + boolean +is_full(Fifo_q * q) +{ + if(q->currentSize < q->maxSize) + return false; + else + return true; +} + + boolean +is_empty(Fifo_q * q) +{ + if(q->head==NULL) + return true; + else + return false; +} + + int +add_to_queue(Fifo_q * q, Sensor_t * sensor) +{ + + pthread_mutex_lock(&lock); + /* TODO delete first one if full */ + if(q == NULL){ + return -1; + } + else if(is_full(q)){ + return -1; + } + Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t)); + new_elem->next = NULL; + new_elem->sensor = sensor; + if(is_empty(q)){ + q->head = new_elem; + pthread_mutex_unlock(&bufferEmptyBlock); + }else + q->tail->next = new_elem; + q->tail = new_elem; + q->currentSize++; + pthread_mutex_unlock(&lock); + return 1; +} + + Sensor_t * +pop_from_queue(Fifo_q * q) +{ + + if(is_empty(q)){ + perror("The queue is empty"); + pthread_mutex_lock(&bufferEmptyBlock); + } + pthread_mutex_lock(&lock); + Queue_t * head = q->head; + q->head = head->next; + Sensor_t * sensor = head->sensor; + free(head); + q->currentSize--; + pthread_mutex_unlock(&lock); + return sensor; +} + + Sensor_t * +create_sensor_object(int value, int uid){ + Sensor_t * sensor = (Sensor_t *) malloc(sizeof(Sensor_t)); + sensor->value = value; + sensor->uid = uid; + return sensor; +} + void +print_queue(Fifo_q * q) +{ + pthread_mutex_lock(&lock); + Queue_t * current = q->head; + if(current == NULL){ + printf("The queue is empty!"); + return; + } + while(current != NULL){ + printf("sensor value=%d, sensor uid=%d\n", + current->sensor->value, current->sensor->uid); + current = current->next; + } + pthread_mutex_unlock(&lock); +} diff --git a/broccoli/src/midbropasad.c b/broccoli/src/midbropasad.c new file mode 100644 index 0000000..756e558 --- /dev/null +++ b/broccoli/src/midbropasad.c @@ -0,0 +1,33 @@ +#include +#include +#include "fifoqueue.h" +#include "broevent.h" +#ifdef BROCCOLI +#include +#endif + + void +start_data_capture(Fifo_q * q) +{ + int res; + pthread_t event_listener; + res = pthread_create(&event_listener, NULL, bro_event_listener, q); + if(res){ + perror("Unable to create thread"); + exit(-1); + } +} + + int +main(int argc, char **argv) +{ + Fifo_q * q = init_queue(50); + start_data_capture(q); + while(true){ + printf("Main thread\n"); + sleep(10); + print_queue(q); + } + free(q); + return 0; +} diff --git a/broccoli/test/tests.c b/broccoli/test/tests.c new file mode 100644 index 0000000..5b91feb --- /dev/null +++ b/broccoli/test/tests.c @@ -0,0 +1,126 @@ +#include "fifoqueue.h" +#include +#include +#include + + void +create_sensor_object_test() { + int value, uid; + value = 1; + uid = 2; + Sensor_t * sensor = create_sensor_object(value, uid); + assert(sensor->value == value); + assert(sensor->uid == uid); + printf("create_sensor_object_test SUCCESS\n"); + free(sensor); +} + + void +init_queue_test() +{ + int size; + Fifo_q * q; + size = 50; + q = init_queue(size); + assert(q->maxSize == size); + printf("init_queue_test SUCCESS\n"); + free(q); +} + + void +add_to_queue_test() +{ + int size, value, uid; + Fifo_q * q; + Sensor_t * sensor; + size = 50; + value = 1; + uid = 2; + q = init_queue(size); + sensor = create_sensor_object(value, uid); + add_to_queue(q,sensor); + assert(q->currentSize == 1); + assert(q->head->sensor == sensor); + assert(q->tail->sensor == sensor); + printf("add_to_queue_test SUCCESS\n"); + free(sensor); + free(q->head); + free(q); +} + + void +pop_from_queue_test() +{ + int size, value, uid; + Fifo_q * q; + Sensor_t * actual; + Sensor_t * expected; + size = 50; + value = 1; + uid = 2; + q = init_queue(size); + actual = create_sensor_object(value, uid); + add_to_queue(q, actual); + expected = pop_from_queue(q); + assert(actual == expected); + printf("pop_from_queue_test SUCCESS\n"); + free(actual); + free(q->head); + free(q); +} + + void +is_full_test() +{ + int size, value, uid; + Fifo_q * q; + Sensor_t * sensor; + size = 1; + value = 1; + uid = 2; + q = init_queue(size); + sensor = create_sensor_object(value, uid); + assert(is_full(q) == false); + add_to_queue(q,sensor); + assert(is_full(q) == true); + printf("is_full_test SUCCESS\n"); + free(sensor); + free(q->head); + free(q); +} + void +is_empty_test() +{ + int size, value, uid; + Fifo_q * q; + Sensor_t * sensor; + size = 1; + value = 1; + uid = 2; + q = init_queue(size); + sensor = create_sensor_object(value, uid); + assert(is_empty(q) == true); + add_to_queue(q,sensor); + assert(is_empty(q) == false); + printf("is_empty_test SUCCESS\n"); + free(sensor); + free(q->head); + free(q); +} + void +all_tests() +{ + create_sensor_object_test(); + init_queue_test(); + add_to_queue_test(); + pop_from_queue_test(); + is_full_test(); + is_empty_test(); + printf("TEST SUITE PASSED\n"); +} + int +main(int argc, char **argv) +{ + all_tests(); + return 0; +} diff --git a/broccoli/tests.c b/broccoli/tests.c deleted file mode 100644 index 73e0346..0000000 --- a/broccoli/tests.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "includes/fifoqueue.h" -#include - - void -create_sensor_object_test() { - int value, uid; - value = 1; - uid = 2; - Sensor_t * sensor = create_sensor_object(value, uid); - assert(sensor->value == value); - assert(sensor->uid == uid); - printf("create_sensor_object_test SUCCESS\n"); - free(sensor); -} - - void -init_queue_test() -{ - int size; - Fifo_q * q; - size = 50; - q = init_queue(size); - assert(q->maxSize == size); - printf("init_queue_test SUCCESS\n"); - free(q); -} - - void -add_to_queue_test() -{ - int size, value, uid; - Fifo_q * q; - Sensor_t * sensor; - size = 50; - value = 1; - uid = 2; - q = init_queue(size); - sensor = create_sensor_object(value, uid); - add_to_queue(q,sensor); - assert(q->currentSize == 1); - assert(q->head->sensor == sensor); - assert(q->tail->sensor == sensor); - printf("add_to_queue_test SUCCESS\n"); - free(sensor); - free(q->head); - free(q); -} - - void -pop_from_queue_test() -{ - int size, value, uid; - Fifo_q * q; - Sensor_t * actual; - Sensor_t * expected; - size = 50; - value = 1; - uid = 2; - q = init_queue(size); - actual = create_sensor_object(value, uid); - add_to_queue(q, actual); - expected = pop_from_queue(q); - assert(actual == expected); - printf("pop_from_queue_test SUCCESS\n"); - free(actual); - free(q->head); - free(q); -} - - void -is_full_test() -{ - int size, value, uid; - Fifo_q * q; - Sensor_t * sensor; - size = 1; - value = 1; - uid = 2; - q = init_queue(size); - sensor = create_sensor_object(value, uid); - assert(is_full(q) == false); - add_to_queue(q,sensor); - assert(is_full(q) == true); - printf("is_full_test SUCCESS\n"); - free(sensor); - free(q->head); - free(q); -} - void -is_empty_test() -{ - int size, value, uid; - Fifo_q * q; - Sensor_t * sensor; - size = 1; - value = 1; - uid = 2; - q = init_queue(size); - sensor = create_sensor_object(value, uid); - assert(is_empty(q) == true); - add_to_queue(q,sensor); - assert(is_empty(q) == false); - printf("is_empty_test SUCCESS\n"); - free(sensor); - free(q->head); - free(q); -} - void -all_tests() -{ - create_sensor_object_test(); - init_queue_test(); - add_to_queue_test(); - pop_from_queue_test(); - is_full_test(); - is_empty_test(); - printf("TEST SUITE PASSED\n"); -} - int -main(int argc, char **argv) -{ - all_tests(); - return 0; -} -- cgit v1.2.3