aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <guskraro@student.gu.se>2017-10-19 11:41:33 +0000
committerRobin Krahl <guskraro@student.gu.se>2017-10-19 11:41:33 +0000
commit7c90d74d74a5988ceef17f5545c12289d09b4803 (patch)
tree01cb75a112ba2ea691ab9268439b1a5ef4999cac
parent51f92be11ee8989424281af0b1a2bc1f66e04e54 (diff)
downloadmidbro-7c90d74d74a5988ceef17f5545c12289d09b4803.tar.gz
midbro-7c90d74d74a5988ceef17f5545c12289d09b4803.tar.bz2
Add pasad.c by group 8 and Wissam with some minor adaptions
-rw-r--r--broccoli/Makefile5
-rw-r--r--broccoli/src/pasad.c142
2 files changed, 146 insertions, 1 deletions
diff --git a/broccoli/Makefile b/broccoli/Makefile
index d50fb9e..1ab59e8 100644
--- a/broccoli/Makefile
+++ b/broccoli/Makefile
@@ -7,7 +7,7 @@ DEBUG =
SRC = midbro.c broevent.c fifoqueue.c
OBJ = $(patsubst %.c, build/%.o, $(SRC))
-all: dirs lib/midbro
+all: dirs lib/midbro bin/pasad
dirs:
mkdir -p build bin lib
@@ -18,6 +18,9 @@ lib/midbro: $(OBJ)
midbro_test:
$(CC) test/midbro_test.c -I./includes -o bin/midbro_test -L./lib -lmidbro
+bin/pasad: build/pasad.o
+ $(CC) $^ $(INC) -o $@ -Llib -lmidbro
+
build/%.o: src/%.c
$(CC) $(LIBCFLAGS) $(DEBUG) $(INC) $< -o $@
diff --git a/broccoli/src/pasad.c b/broccoli/src/pasad.c
new file mode 100644
index 0000000..91b422c
--- /dev/null
+++ b/broccoli/src/pasad.c
@@ -0,0 +1,142 @@
+/*
+
+ -Course-
+ DAT300: Data-driven support for cyber-physical systems
+
+ -Project-
+ Intrusion Detection for Industrial Control Networks
+
+ -Group 8-
+ Hassan Ghalayini - hassang@student.chalmers.se
+ Malama Kasanda - malama@student.chalmers.se
+ Vaios Taxiarchis - vaios@student.chalmers.se
+
+ Modified by Robin Krahl <guskraro@student.gu.se>, Group 3:
+ - Write sensor readings and distance to text files
+ - Take the arguments N, L, r from argc (TODO)
+ - Formatting
+
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include "midbro.h"
+
+int main()
+{
+ clock_t begin;
+ int N=1000,L=500,r=18,l=0;
+ double t=30;
+
+ FILE *file_sensor = fopen("sensor.dat", "w");
+ FILE *file_distance = fopen("distance.dat", "w");
+
+ /* Arrays and variables */
+ int sL=86336;
+ double s[sL];
+ double U[L][r];
+ double X[L];
+ double product_Xt_P_X;
+ double product_Xt_X;
+ int i=0,j=0,c=0,d=0,k=0;
+ double sum=0.0,dist=0.0;
+
+ start_data_capture();
+
+ /* File Descriptor to read projection matrix from U.txt */
+ FILE *file;
+ char ch='a';
+ int flag=0;
+ file=fopen("U.txt", "r");
+ printf(" >Reading U[%dx%d] matrix from .txt file...",L,r);
+ /* Read all values to an array */
+ for(i=0;i<L;i++)
+ {
+ if(flag==1){break;}
+ for(j=0;j<r;j++)
+ {
+ /* Use %lf format specifier, to read doubles with scanf */
+ if(!fscanf(file,"%lf",&U[i][j]))
+ break;
+ /* Break the inner loop and set flag=true */
+ ch = getc(file);
+ if(ch == EOF)
+ {
+ flag=1;
+ break;
+ }
+ /* Break the inner loop when find \n */
+ else if(ch - '0'== -38)
+ break;
+ }
+ }
+ /* Close File Descriptor */
+ fclose(file);
+ printf("Completed!\n");
+
+ for(i=0;i<N;i++)
+ {
+ s[i] = (double)request_value();
+ }
+
+ int p=1;
+ X[0]=0;
+
+ for(i=N-L+1;i<N;i++)
+ {
+ X[p] = s[i];
+ sum = sum + X[p]*X[p];
+ p++;
+ }
+ product_Xt_X = sum;
+ sum = 0;
+
+ begin = clock();
+ while(1)
+ {
+ int value = request_value();
+ s[p] = (double) value;
+ fprintf(file_sensor, "%d\n", value);
+
+ /* Xt[1xL] * X[Lx1] */
+ product_Xt_X = product_Xt_X - X[0]*X[0] + s[p]*s[p];
+
+ /* Generate new test vector Xtest */
+ for(j=0;j<L-1;j++)
+ {
+ X[j] = X[j+1];
+ }
+ X[L-1] = s[p];
+
+ product_Xt_P_X = 0;
+ /* P_1 = Xt[1xL] * U[Lxr] */
+ for (d=0;d<r;d++)
+ {
+ for (k=0;k<L;k++)
+ {
+ sum = sum + X[k]*U[k][d];
+ }
+ product_Xt_P_X = product_Xt_P_X + sum*sum;
+ sum = 0;
+ }
+
+ dist = (product_Xt_X - product_Xt_P_X)/L;
+ p++;
+
+ printf("%lf\n",dist);
+ fprintf(file_distance, "%lf\n", dist);
+ }
+
+ fclose(file_sensor);
+ fclose(file_distance);
+
+ printf(" [Program exits]\n");
+ /* Stop timer */
+ clock_t end = clock();
+
+ /* Print the time elapsed */
+ printf("Time elapsed: %f milliseconds\n", 1000*((double)(end - begin) / CLOCKS_PER_SEC));
+
+ return 0;
+}