diff --git a/docs/PMU data and config frames structures-1.pdf b/docs/PMU data and config frames structures-1.pdf new file mode 100644 index 0000000..da607b7 Binary files /dev/null and b/docs/PMU data and config frames structures-1.pdf differ diff --git a/iPDC/assets/utility_tools.ui b/iPDC/assets/utility_tools.ui index 8b010e4..3dba50e 100644 --- a/iPDC/assets/utility_tools.ui +++ b/iPDC/assets/utility_tools.ui @@ -1,92 +1,84 @@ - + - - 100 - 1 - 10 - - 800 - 600 - False - - - + 800 + 600 + False True - False - 10 - 10 - 3 + False + 10 + 10 + 3 - 800 + 800 True - False - True - immediate + False + True + immediate True - False + False _File - True + True True - False + False gtk-new True - False - True - True + False + True + True gtk-open True - False - True - True + False + True + True gtk-save True - False - True - True + False + True + True gtk-save-as True - False - True - True + False + True + True True - False + False gtk-quit True - False - True - True + False + True + True @@ -96,47 +88,47 @@ True - False + False _Edit - True + True True - False + False gtk-cut True - False - True - True + False + True + True gtk-copy True - False - True - True + False + True + True gtk-paste True - False - True - True + False + True + True gtk-delete True - False - True - True + False + True + True @@ -146,28 +138,28 @@ True - False + False _View - True + True True - False + False _Help - True + True True - False + False gtk-about True - False - True - True + False + True + True @@ -177,88 +169,128 @@ + - 100 - 80 + 100 + 80 True - True - False - 5 - 6 + True + False + 10 + 6 + - 80 + 80 True - False - 5 - 35 + False + 35 Voltage True - True - True + True + True - 0 - 0 - - - - - Current - True - True - True - - - 0 - 1 + 0 + 0 Frequency True - True - True + True + True - 0 - 2 + 0 + 1 Attack Detection True - True - True + True + True - 0 - 3 + 0 + 2 + + + + + True + False + 0 + + kmeans + simple + + + + 0 + 3 + + + + + True + False + 0 + + frequency only + frequency,voltage + voltage + + + + 0 + 4 - 0 - 0 + 0 + 0 - 600 - 500 + 600 + 500 True - False - 5 + False - 1 - 0 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + 30 diff --git a/iPDC/inc/connections.h b/iPDC/inc/connections.h index b87d944..3124239 100755 --- a/iPDC/inc/connections.h +++ b/iPDC/inc/connections.h @@ -63,6 +63,8 @@ struct Lower_Layer_Details { int data_transmission_off; int pmu_remove; int request_cfg_frame; + float latitude; + float longitude; struct Lower_Layer_Details *next; struct Lower_Layer_Details *prev; diff --git a/iPDC/inc/ipdcGui.h b/iPDC/inc/ipdcGui.h index f489e44..6193671 100755 --- a/iPDC/inc/ipdcGui.h +++ b/iPDC/inc/ipdcGui.h @@ -114,6 +114,7 @@ GtkWidget *add_pmu_window, *chkBtn; GtkWidget *new_window, *setup_display_window; GtkWidget *p_id, *p_udp, *p_tcp, *p_ip; GtkWidget *p_port, *p_protocol; +GtkWidget *p_lattitude, *p_longitude; pthread_t show_sys_time; diff --git a/iPDC/inc/map_vis.h b/iPDC/inc/map_vis.h index a2c3d2a..90baf2b 100644 --- a/iPDC/inc/map_vis.h +++ b/iPDC/inc/map_vis.h @@ -5,6 +5,9 @@ typedef struct{ GdkPixbuf *g_red_image; GdkPixbuf *g_green_image; OsmGpsMapImage *g_last_image; + int current_selection; } myParameters; + + gboolean update_images(gpointer* pars); \ No newline at end of file diff --git a/iPDC/inc/new_pmu_or_pdc.h b/iPDC/inc/new_pmu_or_pdc.h index f347996..9205554 100755 --- a/iPDC/inc/new_pmu_or_pdc.h +++ b/iPDC/inc/new_pmu_or_pdc.h @@ -31,7 +31,7 @@ /* Function prototypes */ /* ------------------------------------------------------------------ */ -int add_PMU(char pmuid[], char ip[], char port[], char protocol[]); +int add_PMU(char pmuid[], char ip[], char port[], char protocol[], char latitude[], char longitude[]); void add_PMU_Node(struct Lower_Layer_Details *temp_pmu); diff --git a/iPDC/inc/parser.h b/iPDC/inc/parser.h index d4ce901..c96e2c6 100755 --- a/iPDC/inc/parser.h +++ b/iPDC/inc/parser.h @@ -166,4 +166,6 @@ unsigned long int to_long_int_convertor(unsigned char array[]); uint16_t compute_CRC(unsigned char *message,int length); +float decode_ieee_single(const void *v); + /**************************************** End of File *******************************************************/ diff --git a/iPDC/inc/utility_tools.h b/iPDC/inc/utility_tools.h index 9e3d6a9..5e49756 100644 --- a/iPDC/inc/utility_tools.h +++ b/iPDC/inc/utility_tools.h @@ -8,6 +8,31 @@ #define CH_GET_WIDGET(builder, name, data) \ CH_GET_OBJECT(builder, name, GTK_WIDGET, data) +/* Main data structure definition */ +typedef struct _UtData UtData; + +struct _UtData +{ + /* Main window */ + GtkWidget *util_window; + + /* ui buttons */ + GtkWidget *voltage; + GtkWidget *frequency; + GtkWidget *attack_detection; + GtkWidget *algorithm; + GtkWidget *dimmension; + OsmGpsMap *util_map; + GtkContainer *map_layout; +}; + +UtData *utdata; + +/* UI variables */ +int curr_measurement = 0; +int algorithm =0; +int dimmensions = 0; + void utility_tools(GtkButton *but, gpointer udata); \ No newline at end of file diff --git a/iPDC/src/ipdcGui.c b/iPDC/src/ipdcGui.c index 0ad37bb..69b65e5 100755 --- a/iPDC/src/ipdcGui.c +++ b/iPDC/src/ipdcGui.c @@ -673,13 +673,16 @@ int add_pmu_validation (GtkButton *but, gpointer udata) /* local variables */ int ret; char *errmsg1, ip[20], tmp[4]; - const char *text, *text1, *text2, *text3; + const char *text, *text1, *text2, *text3, *lattitude, *longitude; /* Get the text entries filled by user */ text = gtk_entry_get_text(GTK_ENTRY(p_id)); text1 = gtk_entry_get_text(GTK_ENTRY(p_ip)); text2 = gtk_entry_get_text(GTK_ENTRY(p_port)); text3 = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX(p_protocol)); + lattitude = gtk_entry_get_text(GTK_ENTRY(p_lattitude)); + longitude = gtk_entry_get_text(GTK_ENTRY(p_longitude)); + memset(ip,'\0',20); strcat(ip, text1); strcpy (tmp, text3); @@ -702,10 +705,22 @@ int add_pmu_validation (GtkButton *but, gpointer udata) validation_result (errmsg1); /* Show the unsuccess message to user */ return 0; } + else if(atof(lattitude) > 90 || atof(lattitude) < -90) + { + errmsg1 = " Not a valid Lattitude of Source Device! Try again "; + validation_result (errmsg1); /* Show the unsuccess message to user */ + return 0; + } + else if(atof(longitude) > 180 || atof(longitude) < -180) + { + errmsg1 = " Not a valid Longitude of Source Device! Try again "; + validation_result (errmsg1); /* Show the unsuccess message to user */ + return 0; + } else /* Only if all the text_box entries were right */ { /* call add_PMU() to actual add pmu/pdc and start communication */ - ret = add_PMU((char *)text, (char *)text1, (char *)text2, (char *)text3); + ret = add_PMU((char *)text, (char *)text1, (char *)text2, (char *)text3, (char *)lattitude, (char *)longitude); if (ret == 0) { /* Close/destroy the add_pmu_window */ @@ -1256,7 +1271,7 @@ void add_pmu (GtkButton *but, gpointer udata) gtk_window_set_resizable ( GTK_WINDOW (add_pmu_window), FALSE); /* Create a table of 5 by 2 squares. */ - table = gtk_table_new (5, 2, FALSE); + table = gtk_table_new (7, 2, FALSE); /* Set the spacing to 20 on x and 30 on y */ gtk_table_set_row_spacings (GTK_TABLE (table), 16); @@ -1300,6 +1315,16 @@ void add_pmu (GtkButton *but, gpointer udata) gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 4, 5); gtk_widget_show (label); + label = gtk_label_new ("x-co-ordinate"); + gtk_misc_set_alignment (GTK_MISC(label),0,0); + gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6); + gtk_widget_show (label); + + label = gtk_label_new ("y-co-ordinate"); + gtk_misc_set_alignment (GTK_MISC(label),0,0); + gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 6, 7); + gtk_widget_show (label); + /* Create text boxes for user to enter appropriate values */ p_id = gtk_entry_new(); gtk_entry_set_max_length ((GtkEntry *)p_id, 5); @@ -1316,6 +1341,16 @@ void add_pmu (GtkButton *but, gpointer udata) gtk_table_attach_defaults (GTK_TABLE (table), p_port, 1, 2, 3, 4); gtk_widget_show (p_port); + p_lattitude = gtk_entry_new(); + gtk_entry_set_max_length ((GtkEntry *)p_lattitude, 10); + gtk_table_attach_defaults (GTK_TABLE (table), p_lattitude, 1, 2, 5, 6); + gtk_widget_show (p_lattitude); + + p_longitude = gtk_entry_new(); + gtk_entry_set_max_length ((GtkEntry *)p_longitude, 10); + gtk_table_attach_defaults (GTK_TABLE (table), p_longitude, 1, 2, 6, 7); + gtk_widget_show (p_longitude); + /* Create combo boxe for user with some fixed values */ p_protocol = gtk_combo_box_text_new (); gtk_combo_box_text_append_text(GTK_COMBO_BOX(p_protocol), "UDP"); diff --git a/iPDC/src/map_vis.c b/iPDC/src/map_vis.c index d35e723..14be98a 100644 --- a/iPDC/src/map_vis.c +++ b/iPDC/src/map_vis.c @@ -4,33 +4,70 @@ #include "parser.h" #include "osm-gps-map.h" #include "Attack_detect.h" +#include "connections.h" +// #include "utility_tools.h" gboolean update_images(gpointer* pars){ + int match=0,id; myParameters* parameters = (myParameters*) pars; struct data_frame *df = TSB[0].first_data_frame; + struct Lower_Layer_Details *LLptr; + if (df == NULL){ return TRUE; } - int freq = to_intconvertor(df->dpmu[0]->freq); - gboolean green =attack_detect(df,&START,&COUNT,&SUM_OF_FREQUENCY); + // if (curr_measurement==1) + // { + while (df != NULL){ + id = to_intconvertor(df->idcode); + printf("id = %d\n",id); + + LLptr = LLfirst; + + while(LLptr != NULL){ + printf("pmuid = %d\n",LLptr->pmuid); + if(LLptr->pmuid == id){ + match = 1; + break; + } + LLptr = LLptr->next; + } + + if(match == 1){ + float lat = LLptr->latitude; + float lon = LLptr->longitude; + float freq = to_intconvertor(df->dpmu[0]->freq)*0.001+50; + printf("lat = %f, lon = %f, freq = %f\n",lat,lon,freq); + // gboolean green =attack_detect(df,&START,&COUNT,&SUM_OF_FREQUENCY); + + if(parameters->g_last_image != 0){ + osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image); + } + if (freq > 50.300){ + parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image); + }else{ + parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image); + } + // if(parameters->g_last_image != 0){ + // osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image); + // } + // if (green){ + // parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_green_image); + // }else{ + // parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image); + // } + + + } + df = df->dnext; + } + // } + + + + - // if(parameters->g_last_image != 0){ - // osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image); - // } - // if (freq > 300){ - // parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_green_image); - // }else{ - // parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image); - // } - if(parameters->g_last_image != 0){ - osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image); - } - if (green){ - parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_green_image); - }else{ - parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image); - } gtk_widget_queue_draw(GTK_WIDGET(parameters->util_map)); return TRUE; } \ No newline at end of file diff --git a/iPDC/src/new_pmu_or_pdc.c b/iPDC/src/new_pmu_or_pdc.c index b0cb390..fff224c 100755 --- a/iPDC/src/new_pmu_or_pdc.c +++ b/iPDC/src/new_pmu_or_pdc.c @@ -82,7 +82,7 @@ /* created for each added lower layer PMU/PDC accoring to the protcol (TCP/UDP).*/ /* ---------------------------------------------------------------------------- */ -int add_PMU(char pmuid[], char ip[], char port[], char protocol[]) { +int add_PMU(char pmuid[], char ip[], char port[], char protocol[], char latitude[], char longitude[]) { int err; int flag = 0; @@ -161,6 +161,8 @@ int add_PMU(char pmuid[], char ip[], char port[], char protocol[]) { temp_pmu->data_transmission_off = 0; temp_pmu->pmu_remove = 0; temp_pmu->request_cfg_frame = 0; + temp_pmu->latitude = atof(latitude); + temp_pmu->longitude = atof(longitude); temp_pmu->next = NULL; temp_pmu->prev = NULL; diff --git a/iPDC/src/parser.c b/iPDC/src/parser.c index daa5b50..2f88ec7 100755 --- a/iPDC/src/parser.c +++ b/iPDC/src/parser.c @@ -55,6 +55,9 @@ #include #include #include +#include +#include +#include #include "parser.h" #include "global.h" #include "dallocate.h" @@ -62,6 +65,58 @@ #include "connections.h" #include "recreate.h" + +float decode_ieee_single(const void *v) { + + const unsigned char *data = v; + int s, e; + unsigned long src; + long f; + float value; + + src = ((unsigned long)data[0] << 24) | + ((unsigned long)data[1] << 16) | + ((unsigned long)data[2] << 8) | + ((unsigned long)data[3]); + + s = (src & 0x80000000UL) >> 31; + e = (src & 0x7F800000UL) >> 23; + f = (src & 0x007FFFFFUL); + + if (e == 255 && f != 0) { + /* NaN (Not a Number) */ + value = DBL_MAX; + + } else if (e == 255 && f == 0 && s == 1) { + /* Negative infinity */ + value = -DBL_MAX; + } else if (e == 255 && f == 0 && s == 0) { + /* Positive infinity */ + value = DBL_MAX; + } else if (e > 0 && e < 255) { + /* Normal number */ + f += 0x00800000UL; + if (s) f = -f; + value = ldexp(f, e - 150); + } else if (e == 0 && f != 0) { + /* Denormal number */ + if (s) f = -f; + value = ldexp(f, -149); + } else if (e == 0 && f == 0 && s == 1) { + /* Negative zero */ + value = 0; + } else if (e == 0 && f == 0 && s == 0) { + /* Positive zero */ + value = 0; + } else { + /* Never happens */ + printf("s = %d, e = %d, f = %lu\n", s, e, f); + assert(!"Woops, unhandled case in decode_ieee_single()"); + } + + return value; +} + /* ---------------------------------------------------------------------------- */ /* FUNCTION cfgparser(): */ /* It creates configuration objects for the received configuration frames. */ diff --git a/iPDC/src/utility_tools.c b/iPDC/src/utility_tools.c index 1ec7c0c..fce1144 100644 --- a/iPDC/src/utility_tools.c +++ b/iPDC/src/utility_tools.c @@ -3,6 +3,7 @@ #include "parser.h" #include "osm-gps-map.h" #include "map_vis.h" +#include "utility_tools.h" #define UI_fILE "./assets/utility_tools.ui" #define RED_IMAGE "./assets/red.png" @@ -24,10 +25,47 @@ void on_window_destroy(GtkWidget *widget, gpointer data) gtk_main_quit(); } +// on clicking the button frequency +void on_frequency_clicked(GtkButton *but, gpointer udata) +{ + curr_measurement = 1; + gtk_widget_hide(utdata->algorithm); + gtk_widget_hide(utdata->dimmension); + gtk_widget_set_sensitive(utdata->voltage, TRUE); + gtk_widget_set_sensitive(utdata->frequency, FALSE); + gtk_widget_set_sensitive(utdata->attack_detection, TRUE); + + + // printf("Frequency\n"); +} + +// on clicking the button attack_detection +void on_attack_detection_clicked(GtkButton *but, gpointer udata) +{ + curr_measurement = 2; + gtk_widget_set_sensitive(utdata->voltage, TRUE); + gtk_widget_set_sensitive(utdata->frequency, TRUE); + gtk_widget_set_sensitive(utdata->attack_detection, FALSE); + + gtk_widget_show(utdata->algorithm); + gtk_widget_show(utdata->dimmension); + // printf("Attack Detection\n"); +} + +// on clicking the button voltage +void on_voltage_clicked(GtkButton *but, gpointer udata) +{ + curr_measurement = 0; + gtk_widget_hide(utdata->algorithm); + gtk_widget_hide(utdata->dimmension); + gtk_widget_set_sensitive(utdata->voltage, FALSE); + gtk_widget_set_sensitive(utdata->frequency, TRUE); + gtk_widget_set_sensitive(utdata->attack_detection, TRUE); + // printf("Voltage\n"); +} + void utility_tools(GtkButton *but, gpointer udata) { - OsmGpsMap *util_map; - GtkWidget *window; GdkPixbuf *g_red_image; GdkPixbuf *g_green_image; OsmGpsMapImage *g_last_image; @@ -35,35 +73,66 @@ void utility_tools(GtkButton *but, gpointer udata) GtkBuilder *builder; GError *error = NULL; builder = gtk_builder_new(); + + + if(!gtk_builder_add_from_file(builder, UI_fILE, &error)) { g_warning("%s", error->message); g_free(error); } + + utdata = g_slice_new(UtData); + utdata->util_window = GTK_WIDGET(gtk_builder_get_object(builder, "util_window")); + utdata->voltage = GTK_WIDGET(gtk_builder_get_object(builder, "voltage")); + utdata->frequency = GTK_WIDGET(gtk_builder_get_object(builder, "freq")); + utdata->attack_detection = GTK_WIDGET(gtk_builder_get_object(builder, "attack_detection")); + utdata->algorithm = GTK_WIDGET(gtk_builder_get_object(builder, "algorithm")); + utdata->dimmension = GTK_WIDGET(gtk_builder_get_object(builder, "dimmension")); + // utdata->util_map = GTK_WIDGET(gtk_builder_get_object(builder, "util_map")); + utdata->map_layout = GTK_CONTAINER(gtk_builder_get_object(builder, "map_layout")); + + gtk_widget_set_sensitive(utdata->voltage, FALSE); + gtk_widget_set_visible(utdata->algorithm, FALSE); + gtk_widget_set_visible(utdata->dimmension, FALSE); + + g_signal_connect(utdata->attack_detection, "clicked", G_CALLBACK(on_attack_detection_clicked), NULL); + g_signal_connect(utdata->frequency, "clicked", G_CALLBACK(on_frequency_clicked), NULL); + g_signal_connect(utdata->voltage, "clicked", G_CALLBACK(on_voltage_clicked), NULL); + + + + g_red_image = gdk_pixbuf_new_from_file_at_size (RED_IMAGE, 24,24,NULL); g_green_image = gdk_pixbuf_new_from_file_at_size (GREEN_IMAGE, 24,24,NULL); - window = GTK_WIDGET(gtk_builder_get_object(builder, "util_window")); - GtkContainer *map_container = GTK_CONTAINER(gtk_builder_get_object(builder, "map_layout")); - util_map = g_object_new (OSM_TYPE_GPS_MAP, + utdata->util_map = g_object_new (OSM_TYPE_GPS_MAP, "map-source", OSM_GPS_MAP_SOURCE_OPENSTREETMAP, "tile-cache", "/tmp/", NULL); - osm_gps_map_set_center_and_zoom (util_map, 15.4589, 75.0078, 10); + osm_gps_map_set_center_and_zoom (utdata->util_map, 15.4589, 75.0078, 10); - g_last_image = osm_gps_map_image_add(util_map,15.4589, 75.0078, g_red_image); - g_last_image = osm_gps_map_image_add(util_map,15.518597, 74.925584, g_green_image); - myParameters parameters = {util_map, g_red_image, g_green_image, g_last_image}; + // TODO: add the points to window. + g_last_image = osm_gps_map_image_add(utdata->util_map,15.4589, 75.0078, g_red_image); + g_last_image = osm_gps_map_image_add(utdata->util_map,15.518597, 74.925584, g_green_image); + + + + myParameters parameters = {utdata->util_map, g_red_image, g_green_image, g_last_image}; gpointer data = (gpointer) ¶meters; guint pid = g_timeout_add(20, (GSourceFunc) update_images, data); - gtk_widget_set_size_request(GTK_WIDGET(util_map), 600, 500); + gtk_widget_set_size_request(GTK_WIDGET(utdata->util_map), 600, 500); - gtk_container_add(map_container, GTK_WIDGET(util_map)); - g_signal_connect(window, "destroy", G_CALLBACK(on_window_destroy), GUINT_TO_POINTER(pid)); + gtk_container_add(utdata->map_layout, GTK_WIDGET(utdata->util_map)); + g_signal_connect(utdata->util_window, "destroy", G_CALLBACK(on_window_destroy), GUINT_TO_POINTER(pid)); + + gtk_widget_show_all(utdata->util_window); + + gtk_widget_hide(utdata->algorithm); + gtk_widget_hide(utdata->dimmension); - gtk_widget_show_all(window); gtk_main(); } \ No newline at end of file