00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <netdb.h>
00037 #include <syslog.h>
00038
00039 #include "mesh_analysis.h"
00040 #include "mesh_jam_resist.h"
00041
00042
00043 #define STATISTIC_SIZE 128
00044 #define SNR_ALARM 3 // sigma
00045 #define NOISE_ALARM 6 // sigma
00046
00047
00048 static double mean(int array[], int size) {
00049 int i, sum = 0;
00050
00051 for (i = 0; i < size; i++)
00052 sum += array[i];
00053 return (double)sum / (double)size;
00054 }
00055
00056 static double stddev(int array[], int size) {
00057 int i, sum = 0;
00058 double mean = mean(array, size);
00059
00060 for (i = 0; i < size; i++)
00061 sum += pow((mean - array[i]), 2.0);
00062 return sqrt(sum / (double)size);
00063 }
00064
00065
00066 static int idx = 0, size = 0;
00067 static int signal[STATISTIC_SIZE], noise[STATISTIC_SIZE];
00068
00069
00070 void jam_analysis(struct msghdr *msg_header)
00071 int i, signal_str, noise_lvl;
00072 double snr_sigma, noise_sigma, snr[size];
00073
00074 for (control = CMSG_FIRSTHDR(msg_header); control != NULL;
00075 control = CMSG_NXTHDR(msg_header, control)) {
00076 if (control->cmsg_level == SOL_IP &&
00077 control->cmsg_type == IP_PKTINFO) {
00078 struct in_pktinfo *packet_info = (struct in_pktinfo*)CMSG_DATA(control);
00079 signal_str = packet_info->signal;
00080 noise_lvl = packet_info->noise;
00081 break;
00082 }
00083 }
00084
00085 for (i = 0; i < size; i++)
00086 snr[i] = (double)signal[i] / (double)noise[i];
00087
00088 snr_sigma = stddev(snr, size);
00089 snr_mean = mean(snr, size);
00090 noise_sigma = stddev(noise, size);
00091 noise_mean = mean(noise, size);
00092
00093 if (((double)signal_str / (double)noise_lvl <
00094 snr_mean + (SNR_ALARM * snr_sigma)) ||
00095 (double)noise_lvl > noise_mean + (NOISE_ALARM * noise_sigma)) {
00096
00097 return;
00098 }
00099
00100 signal[idx] = signal_str;
00101 noise[idx] = noise_lvl;
00102
00103 if (idx < STATISTIC_SIZE)
00104 idx++;
00105 else
00106 idx = 0;
00107
00108 if (size < STATISTIC_SIZE)
00109 size++;
00110 }