diff options
Diffstat (limited to 'broccoli')
-rw-r--r-- | broccoli/Makefile | 16 | ||||
-rw-r--r-- | broccoli/broevent.c | 71 | ||||
-rw-r--r-- | broccoli/fifoqueue.c | 82 | ||||
-rw-r--r-- | broccoli/modbus.bro | 26 |
4 files changed, 195 insertions, 0 deletions
diff --git a/broccoli/Makefile b/broccoli/Makefile new file mode 100644 index 0000000..d895274 --- /dev/null +++ b/broccoli/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS = -c -Wall -I/usr/local/include -I/usr/local/include -DBROCCOLI +LDFLAGS = -L/usr/local/lib -lbroccoli + +all: broevent + +broevent: broevent.o fifoqueue.o + $(CC) broevent.o fifoqueue.o -o broevent $(LDFLAGS) + +broevent.o: broevent.c + $(CC) $(CFLAGS) broevent.c + +fifoqueue.o: fifoqueue.c + $(CC) -c -Wall fifoqueue.c +clean: + rm *.o broevent diff --git a/broccoli/broevent.c b/broccoli/broevent.c new file mode 100644 index 0000000..a2b7a28 --- /dev/null +++ b/broccoli/broevent.c @@ -0,0 +1,71 @@ +#ifdef BROCCOLI +#include <broccoli.h> +#endif +#include "includes/fifoqueue.h" + +char *host_default = "127.0.0.1"; +char *port_default = "47760"; + + static void +bro_response(BroConn *conn, void *data, uint64* registers, uint64* uid) +{ + printf("Received value %"PRIu64" from uid=%"PRIu64"\n",*registers,*uid); + + conn = NULL; + data = NULL; +} + + void +bro_event_listener() +{ + + 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); +} + + int +main(int argc, char **argv) +{ + Fifo_q * q = init_queue(5); + + free(q); + return 0; +} diff --git a/broccoli/fifoqueue.c b/broccoli/fifoqueue.c new file mode 100644 index 0000000..56b1dda --- /dev/null +++ b/broccoli/fifoqueue.c @@ -0,0 +1,82 @@ +#include "includes/fifoqueue.h" + + 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; + 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) +{ + /* 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; + else + q->tail->next = new_elem; + q->tail = new_elem; + q->currentSize++; + return 1; +} + + Sensor_t * +pop_from_queue(Fifo_q * q) +{ + if(is_empty(q)){ + perror("The queue is empty"); + exit(-1); + } + Queue_t * head = q->head; + q->head = q->head->next; + Sensor_t * sensor = head->sensor; + free(head); + q->currentSize--; + return sensor; +} + + void +print_queue(Fifo_q * q) +{ + 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; + } +} diff --git a/broccoli/modbus.bro b/broccoli/modbus.bro new file mode 100644 index 0000000..a2caed9 --- /dev/null +++ b/broccoli/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); +} + |